forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileByteSource.h
148 lines (121 loc) · 3.59 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
141
142
143
144
145
146
147
148
/**
* 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 "ByteSource.h"
#include "Reporting.h"
#include <folly/ThreadLocal.h>
namespace facebook {
namespace wdt {
/**
* ByteSource that reads data from a file. The buffer used is thread-local
* for efficiency reasons so only one FileByteSource can be created/used
* per thread. It's also unsafe to access the same FileByteSource from
* multiple threads.
*/
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
* @param bufferSize size of buffer for temporarily storing read
* bytes
*/
FileByteSource(SourceMetaData *metadata, int64_t size, int64_t offset,
int64_t bufferSize);
/// close file descriptor if still open
virtual ~FileByteSource() {
this->close();
}
/// @return filepath
virtual const std::string &getIdentifier() const override {
return metadata_->relPath;
}
/// @return size of file in bytes
virtual int64_t getSize() const override {
return size_;
}
/// @return offset from which to start reading
virtual int64_t getOffset() const override {
return offset_;
}
/// @see ByteSource.h
virtual const SourceMetaData &getMetaData() const override {
return *metadata_;
}
/// @return true iff finished reading file successfully
virtual bool finished() const override {
return bytesRead_ == size_ && !hasError();
}
/// @return true iff there was an error reading file
virtual bool hasError() const override {
return fd_ < 0;
}
/// @see ByteSource.h
virtual char *read(int64_t &size) override;
/// open the source for reading
virtual ErrorCode open() override;
/// close the source for reading
virtual void close() override {
if (fd_ >= 0) {
START_PERF_TIMER
::close(fd_);
RECORD_PERF_RESULT(PerfStatReport::FILE_CLOSE)
fd_ = -1;
}
}
/**
* @return transfer stats for the source. If the stats is moved by the
* caller, then this function can not be called again
*/
virtual TransferStats &getTransferStats() override {
return transferStats_;
}
/// @param stats Stats to be added
virtual void addTransferStats(const TransferStats &stats) override {
transferStats_ += stats;
}
private:
struct Buffer {
explicit Buffer(int64_t size) : size_(size) {
data_ = new char[size + 1];
}
~Buffer() {
delete[] data_;
}
char *data_;
int64_t size_;
};
/**
* Buffer for temporarily holding bytes read from file. This is thread-local
* for efficiency reasons, so only one FileByteSource can be used at once
* per thread.
*/
static folly::ThreadLocalPtr<Buffer> buffer_;
/// shared file information
SourceMetaData *metadata_;
/// filesize
const int64_t size_;
/// open file descriptor for file (set to < 0 on error)
int fd_{-1};
/// block offset
const int64_t offset_;
/// number of bytes read so far from file
int64_t bytesRead_;
/// buffer size
int64_t bufferSize_;
/// transfer stats
TransferStats transferStats_;
};
}
}