-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.cpp
More file actions
281 lines (251 loc) · 8.88 KB
/
Copy pathreader.cpp
File metadata and controls
281 lines (251 loc) · 8.88 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "reader.h"
#include "file_env.h"
#include "version.h"
#include "util/crc32c.h"
#include "util/coding.h"
namespace levelque {
Reader::Reader(SequentialFile* file, bool checksum,
uint64_t initial_offset)
: file_(file),
checksum_(checksum),
backing_store_(new char[kBlockSize]),
buffer_(),
eof_(false),
last_record_offset_(0),
end_of_buffer_offset_(0),
read_block_num_(-1),
real_read_block_num_(0),
last_record_end_offset_(0),
offset_in_block_(0),
initial_offset_(initial_offset) {
}
Reader::~Reader() {
delete[] backing_store_;
}
bool Reader::SkipToInitialBlock() {
offset_in_block_ = initial_offset_ % kBlockSize;
uint64_t block_start_location = initial_offset_ - offset_in_block_;
// Don't search a block if we'd be in the trailer
if (offset_in_block_ > kBlockSize - 6) {
offset_in_block_ = 0;
block_start_location += kBlockSize;
}
end_of_buffer_offset_ = block_start_location;
// Skip to start of first block that can contain the initial record
if (block_start_location > 0) {
bool s = file_->Skip(block_start_location);
if (!s) {
ReportDrop(block_start_location);
return false;
}
}
return true;
}
uint32_t Reader::ReadRecord(string &record, std::string &scratch, uint32_t &blockid) {
if (!SkipToInitialBlock()) {
return 0;
}
initial_offset_ = 0;
scratch.clear();
record.clear();
bool in_fragmented_record = false;
// Record offset of the logical record that we're reading
// 0 is a dummy value to make compilers happy
uint64_t prospective_record_offset = 0;
string fragment;
fragment.clear();
while (true) {
uint64_t physical_record_offset = end_of_buffer_offset_ - buffer_.size();
uint32_t id;
const unsigned int record_type = ReadPhysicalRecord(fragment, id, blockid);
//cout << "record_type: " << record_type << endl;
switch (record_type) {
case kFullType:
if (in_fragmented_record) {
// Handle bug in earlier versions of log::Writer where
// it could emit an empty kFirstType record at the tail end
// of a block followed by a kFullType or kFirstType record
// at the beginning of the next block.
if (scratch.empty()) {
in_fragmented_record = false;
} else {
ReportCorruption(scratch.size(), "partial record without end(1)");
}
}
prospective_record_offset = physical_record_offset;
scratch.clear();
record = fragment;
last_record_offset_ = prospective_record_offset;
last_record_end_offset_ = kBlockSize - buffer_.size();
if(read_block_num_ != -1) {
real_read_block_num_ = read_block_num_;
}
//Version::Instance()->SetBlockInterId();
//cout << "kFullType: " << last_record_end_offset_ << endl;
return id;
case kFirstType:
if (in_fragmented_record) {
// Handle bug in earlier versions of log::Writer where
// it could emit an empty kFirstType record at the tail end
// of a block followed by a kFullType or kFirstType record
// at the beginning of the next block.
if (scratch.empty()) {
in_fragmented_record = false;
} else {
ReportCorruption(scratch.size(), "partial record without end(2)");
}
}
prospective_record_offset = physical_record_offset;
scratch.assign(fragment.data(), fragment.size());
in_fragmented_record = true;
break;
case kMiddleType:
if (!in_fragmented_record) {
ReportCorruption(fragment.size(),
"missing start of fragmented record(1)");
} else {
scratch.append(fragment.data(), fragment.size());
}
break;
case kLastType:
if (!in_fragmented_record) {
ReportCorruption(fragment.size(),
"missing start of fragmented record(2)");
} else {
scratch.append(fragment.data(), fragment.size());
record = scratch;
last_record_offset_ = prospective_record_offset;
last_record_end_offset_ = kBlockSize - buffer_.size();
if(read_block_num_ != -1) {
real_read_block_num_ = read_block_num_;
}
//Version::Instance()->SetBlockInterId();
return id;
}
break;
case kEof:
if (in_fragmented_record) {
ReportCorruption(scratch.size(), "partial record without end(3)");
scratch.clear();
}
return 0;
case kBadRecord:
if (in_fragmented_record) {
ReportCorruption(scratch.size(), "error in middle of record");
in_fragmented_record = false;
scratch.clear();
}
break;
default: {
char buf[40];
snprintf(buf, sizeof(buf), "unknown record type %u", record_type);
ReportCorruption(
(fragment.size() + (in_fragmented_record ? scratch.size() : 0)),
buf);
in_fragmented_record = false;
scratch.clear();
break;
}
}
}
return 0;
}
uint64_t Reader::LastRecordOffset() {
return last_record_offset_;
}
uint64_t Reader::FileEndOffset() {
return real_read_block_num_*kBlockSize + last_record_end_offset_;
}
uint64_t Reader::BlockEndOffset() {
return last_record_end_offset_;
}
void Reader::ReportCorruption(size_t bytes, const char* reason) {
cout << reason << " bytes:"<<bytes <<endl;
}
void Reader::ReportDrop(size_t bytes) {
//if (reporter_ != NULL &&
// end_of_buffer_offset_ - buffer_.size() - bytes >= initial_offset_) {
//reporter_->Corruption(bytes, reason);
//}
}
unsigned int Reader::ReadPhysicalRecord(string &result, uint32_t &id, uint32_t &blockid) {
while (true) {
//cout << "buffer_.size: "<< buffer_.size() << endl;
if (buffer_.size() < kHeaderSize) {
if (!eof_) {
// Last read was a full read, so this is a trailer to skip
buffer_.clear();
bool s = file_->Read(kBlockSize, buffer_, backing_store_);
end_of_buffer_offset_ += buffer_.size();
read_block_num_++;
//Version::Instance()->SetBlockId();
if (!s) {
buffer_.clear();
ReportDrop(kBlockSize);
eof_ = true;
return kEof;
} else if (buffer_.size() < kBlockSize) {
eof_ = true;
}
continue;
} else if (buffer_.size() == 0) {
// End of file
return kEof;
} else {
//size_t drop_size = buffer_.size();
buffer_.clear();
return kEof;
}
}
if(offset_in_block_ > 0) {
buffer_ = buffer_.substr(offset_in_block_);
offset_in_block_ = 0;
}
// Parse the header
const char* header = buffer_.data();
const uint32_t a = static_cast<uint32_t>(header[4]) & 0xff;
const uint32_t b = static_cast<uint32_t>(header[5]) & 0xff;
const unsigned int type = header[6];
const uint32_t length = a | (b << 8);
//blockid
const uint32_t c = static_cast<uint32_t>(header[7]) & 0xff;
const uint32_t d = static_cast<uint32_t>(header[8]) & 0xff;
const uint32_t bid = c | (d << 8);
//interid
const uint32_t e = static_cast<uint32_t>(header[9]) & 0xff;
const uint32_t f = static_cast<uint32_t>(header[10]) & 0xff;
const uint32_t iid = e | (f << 8);
id = iid | (bid << 16);
blockid = bid;
if (kHeaderSize + length > buffer_.size()) {
size_t drop_size = buffer_.size();
buffer_.clear();
cout << drop_size << " bad record length" << endl;
return kBadRecord;
}
if (type == kZeroType && length == 0) {
// Skip zero length record without reporting any drops since
// such records are produced by the mmap based writing code in
// env_posix.cc that preallocates file regions.
buffer_.clear();
return kBadRecord;
}
uint32_t expected_crc = crc32c::Unmask(DecodeFixed32(header));
uint32_t actual_crc = crc32c::Value(header + 11, length);
if (actual_crc != expected_crc) {
cout << "crc32 failed " << endl;
return kBadRecord;
}
//buffer_.remove_prefix(kHeaderSize + length);
// Skip physical record that started before initial_offset_
if (end_of_buffer_offset_ - buffer_.size() - kHeaderSize - length <
initial_offset_) {
result.clear();
return kBadRecord;
}
result.assign(header + kHeaderSize, length);
buffer_ = buffer_.substr(kHeaderSize + length);
return type;
}
}
}