forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileByteSource.h
140 lines (113 loc) · 3.38 KB
/
FileByteSource.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#pragma once
#include <unistd.h>
#include <wdt/ByteSource.h>
#include <wdt/util/CommonImpl.h>
namespace facebook {
namespace wdt {
/// File related code
class FileUtil {
public:
/**
* Opens the file for reading.
*
* @param threadCtx thread context
* @param filename name of the file
* @param isDirectReads whether to open for direct reads
*
* @return If successful, fd is returned, else -1 is returned
*/
static int openForRead(ThreadCtx &threadCtx, const std::string &filename,
bool isDirectReads);
// TODO: create a separate file for this class and move other file related
// code here
};
/**
* ByteSource that reads data from a file.
*/
class FileByteSource : public ByteSource {
public:
/**
* Create a new FileByteSource for a given path.
*
* @param metadata shared file data
* @param size size of file; if actual size is larger we'll
* truncate, if it's smaller we'll fail
* @param offset block offset
*/
FileByteSource(SourceMetaData *metadata, int64_t size, int64_t offset);
/// close file descriptor if still open
~FileByteSource() override {
this->close();
}
/// @return filepath
const std::string &getIdentifier() const override {
return metadata_->relPath;
}
/// @return size of file in bytes
int64_t getSize() const override {
return size_;
}
/// @return offset from which to start reading
int64_t getOffset() const override {
return offset_;
}
/// @see ByteSource.h
const SourceMetaData &getMetaData() const override {
return *metadata_;
}
/// @return true iff finished reading file successfully
bool finished() const override {
return bytesRead_ == size_ && !hasError();
}
/// @return true iff there was an error reading file
bool hasError() const override {
return (metadata_->allocationStatus != TO_BE_DELETED) && (fd_ < 0);
}
/// @see ByteSource.h
char *read(int64_t &size) override;
/// @see ByteSource.h
void advanceOffset(int64_t numBytes) override;
/// @see ByteSource.h
ErrorCode open(ThreadCtx *threadCtx) override;
/// close the source for reading
void close() override;
/**
* @return transfer stats for the source. If the stats is moved by the
* caller, then this function can not be called again
*/
TransferStats &getTransferStats() override {
return transferStats_;
}
/// @param stats Stats to be added
void addTransferStats(const TransferStats &stats) override {
transferStats_ += stats;
}
private:
/// clears page cache
void clearPageCache();
ThreadCtx *threadCtx_{nullptr};
/// shared file information
SourceMetaData *metadata_;
/// filesize
int64_t size_;
/// open file descriptor for file (set to < 0 on error)
int fd_{-1};
/// block offset
int64_t offset_;
/// number of bytes read so far from file
int64_t bytesRead_;
/// Whether reads have to be done using aligned buffer and size
bool alignedReadNeeded_{false};
/// transfer stats
TransferStats transferStats_;
};
}
}