db/log_reader.cc

filter: Statementstmt 43.3% 68 / 157 Branchbran 19.3% 19 / 98 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 34.1% 87 / 255 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« db/db_impl.cc ↑ Summary util/comparator.cc »
1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4  
5 #include "db/log_reader.h"
6  
7 #include <stdio.h>
8 #include "leveldb/env.h"
9 #include "util/coding.h"
10 #include "util/crc32c.h"
11  
12 namespace leveldb {
13 namespace log {
14  
15 0 Reader::Reporter::~Reporter() {
16 0 }
17  
18 4 Reader::Reader(SequentialFile* file, Reporter* reporter, bool checksum,
19 4 uint64_t initial_offset)
20 4 : file_(file),
21 4 reporter_(reporter),
22 4 checksum_(checksum),
23 4 backing_store_(new char[kBlockSize]),
24 4 buffer_(),
25 4 eof_(false),
26 4 last_record_offset_(0),
27 4 end_of_buffer_offset_(0),
28 4 initial_offset_(initial_offset) {
29 4 }
30  
31 4 Reader::~Reader() {
32 4 ▶ delete[] backing_store_;
Branch: 1 branch
BranchTrueFalseTotal
delete[] backing_store_; 4 0 4
33 4 }
34  
35 0 bool Reader::SkipToInitialBlock() {
36 0 size_t offset_in_block = initial_offset_ % kBlockSize;
37 0 uint64_t block_start_location = initial_offset_ - offset_in_block;
38  
39 // Don't search a block if we'd be in the trailer
40 0 ▶ if (offset_in_block > kBlockSize - 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset_in_block > kBlockSize - 6) { 0 0 0
41 0 offset_in_block = 0;
42 0 block_start_location += kBlockSize;
43 }
44  
45 0 end_of_buffer_offset_ = block_start_location;
46  
47 // Skip to start of first block that can contain the initial record
48 0 ▶ if (block_start_location > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (block_start_location > 0) { 0 0 0
49 0 ▶ Status skip_status = file_->Skip(block_start_location);
Branch: 1 branch
BranchTrueFalseTotal
Status skip_status = file_->Skip(block_start_location); 0 0 0
50 0 ▶ if (!skip_status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_status.ok()) { 0 0 0
51 0 ▶ ReportDrop(block_start_location, skip_status);
Branch: 1 branch
BranchTrueFalseTotal
ReportDrop(block_start_location, skip_status); 0 0 0
52 0 return false;
53 }
54 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 0 0
55  
56 0 return true;
57 }
58  
59 21 bool Reader::ReadRecord(Slice* record, std::string* scratch) {
60 21 ▶ if (last_record_offset_ < initial_offset_) {
Branch: 1 branch
BranchTrueFalseTotal
if (last_record_offset_ < initial_offset_) { 0 21 21
61 0 ▶ if (!SkipToInitialBlock()) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SkipToInitialBlock()) { 0 0 0
if (!SkipToInitialBlock()) { 0 0 0
62 0 return false;
63 }
64 }
65  
66 21 scratch->clear();
67 21 record->clear();
68 21 bool in_fragmented_record = false;
69 // Record offset of the logical record that we're reading
70 // 0 is a dummy value to make compilers happy
71 21 uint64_t prospective_record_offset = 0;
72  
73 21 Slice fragment;
74 while (true) {
75 21 uint64_t physical_record_offset = end_of_buffer_offset_ - buffer_.size();
76 21 ▶ const unsigned int record_type = ReadPhysicalRecord(&fragment);
Branch: 1 branch
BranchTrueFalseTotal
const unsigned int record_type = ReadPhysicalRecord(&fragment); 21 0 21
77 21 switch (record_type) {
78 17 case kFullType:
79 17 ▶ if (in_fragmented_record) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_fragmented_record) { 0 17 17
80 // Handle bug in earlier versions of log::Writer where
81 // it could emit an empty kFirstType record at the tail end
82 // of a block followed by a kFullType or kFirstType record
83 // at the beginning of the next block.
84 0 ▶ if (scratch->empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (scratch->empty()) { 0 0 0
85 0 in_fragmented_record = false;
86 } else {
87 0 ▶ ReportCorruption(scratch->size(), "partial record without end(1)");
Branch: 1 branch
BranchTrueFalseTotal
ReportCorruption(scratch->size(), "partial record without end(1)"); 0 0 0
88 }
89 }
90 17 prospective_record_offset = physical_record_offset;
91 17 scratch->clear();
92 17 *record = fragment;
93 17 last_record_offset_ = prospective_record_offset;
94 17 return true;
95  
96 0 case kFirstType:
97 0 ▶ if (in_fragmented_record) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_fragmented_record) { 0 0 0
98 // Handle bug in earlier versions of log::Writer where
99 // it could emit an empty kFirstType record at the tail end
100 // of a block followed by a kFullType or kFirstType record
101 // at the beginning of the next block.
102 0 ▶ if (scratch->empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (scratch->empty()) { 0 0 0
103 0 in_fragmented_record = false;
104 } else {
105 0 ▶ ReportCorruption(scratch->size(), "partial record without end(2)");
Branch: 1 branch
BranchTrueFalseTotal
ReportCorruption(scratch->size(), "partial record without end(2)"); 0 0 0
106 }
107 }
108 0 prospective_record_offset = physical_record_offset;
109 0 ▶ scratch->assign(fragment.data(), fragment.size());
Branch: 1 branch
BranchTrueFalseTotal
scratch->assign(fragment.data(), fragment.size()); 0 0 0
110 0 in_fragmented_record = true;
111 0 break;
112  
113 0 case kMiddleType:
114 0 ▶ if (!in_fragmented_record) {
Branch: 1 branch
BranchTrueFalseTotal
if (!in_fragmented_record) { 0 0 0
115 0 ▶ ReportCorruption(fragment.size(),
Branch: 1 branch
BranchTrueFalseTotal
ReportCorruption(fragment.size(), 0 0 0
116 "missing start of fragmented record(1)");
117 } else {
118 0 ▶ scratch->append(fragment.data(), fragment.size());
Branch: 1 branch
BranchTrueFalseTotal
scratch->append(fragment.data(), fragment.size()); 0 0 0
119 }
120 0 break;
121  
122 0 case kLastType:
123 0 ▶ if (!in_fragmented_record) {
Branch: 1 branch
BranchTrueFalseTotal
if (!in_fragmented_record) { 0 0 0
124 0 ▶ ReportCorruption(fragment.size(),
Branch: 1 branch
BranchTrueFalseTotal
ReportCorruption(fragment.size(), 0 0 0
125 "missing start of fragmented record(2)");
126 } else {
127 0 ▶ scratch->append(fragment.data(), fragment.size());
Branch: 1 branch
BranchTrueFalseTotal
scratch->append(fragment.data(), fragment.size()); 0 0 0
128 0 *record = Slice(*scratch);
129 0 last_record_offset_ = prospective_record_offset;
130 0 return true;
131 }
132 0 break;
133  
134 4 case kEof:
135 4 ▶ if (in_fragmented_record) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_fragmented_record) { 0 4 4
136 0 ▶ ReportCorruption(scratch->size(), "partial record without end(3)");
Branch: 1 branch
BranchTrueFalseTotal
ReportCorruption(scratch->size(), "partial record without end(3)"); 0 0 0
137 0 scratch->clear();
138 }
139 4 return false;
140  
141 0 case kBadRecord:
142 0 ▶ if (in_fragmented_record) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_fragmented_record) { 0 0 0
143 0 ▶ ReportCorruption(scratch->size(), "error in middle of record");
Branch: 1 branch
BranchTrueFalseTotal
ReportCorruption(scratch->size(), "error in middle of record"); 0 0 0
144 0 in_fragmented_record = false;
145 0 scratch->clear();
146 }
147 0 break;
148  
149 0 default: {
150 char buf[40];
151 0 snprintf(buf, sizeof(buf), "unknown record type %u", record_type);
152 0 ▶ ReportCorruption(
Branch: 1 branch
BranchTrueFalseTotal
ReportCorruption( 0 0 0
153 0 ▶ (fragment.size() + (in_fragmented_record ? scratch->size() : 0)),
Branch: 1 branch
BranchTrueFalseTotal
(fragment.size() + (in_fragmented_record ? scratch->size() : 0)), 0 0 0
154 buf);
155 0 in_fragmented_record = false;
156 0 scratch->clear();
157 0 break;
158 }
159 }
160 0 }
161 return false;
162 }
163  
164 0 uint64_t Reader::LastRecordOffset() {
165 0 return last_record_offset_;
166 }
167  
168 0 void Reader::ReportCorruption(size_t bytes, const char* reason) {
169 0 ▶ ReportDrop(bytes, Status::Corruption(reason));
Branch: 2 branches
BranchTrueFalseTotal
ReportDrop(bytes, Status::Corruption(reason)); 0 0 0
ReportDrop(bytes, Status::Corruption(reason)); 0 0 0
170 0 }
171  
172 0 void Reader::ReportDrop(size_t bytes, const Status& reason) {
173 0 ▶ if (reporter_ != NULL &&
Branch: 2 branches
BranchTrueFalseTotal
if (reporter_ != NULL && 0 0 0
if (reporter_ != NULL && 0 0 0
174 0 ▶ end_of_buffer_offset_ - buffer_.size() - bytes >= initial_offset_) {
Branch: 1 branch
BranchTrueFalseTotal
end_of_buffer_offset_ - buffer_.size() - bytes >= initial_offset_) { 0 0 0
175 0 reporter_->Corruption(bytes, reason);
176 }
177 0 }
178  
179 21 unsigned int Reader::ReadPhysicalRecord(Slice* result) {
180 while (true) {
181 25 ▶ if (buffer_.size() < kHeaderSize) {
Branch: 1 branch
BranchTrueFalseTotal
if (buffer_.size() < kHeaderSize) { 8 17 25
182 8 ▶ if (!eof_) {
Branch: 1 branch
BranchTrueFalseTotal
if (!eof_) { 4 4 8
183 // Last read was a full read, so this is a trailer to skip
184 4 buffer_.clear();
185 4 ▶ Status status = file_->Read(kBlockSize, &buffer_, backing_store_);
Branch: 1 branch
BranchTrueFalseTotal
Status status = file_->Read(kBlockSize, &buffer_, backing_store_); 4 0 4
186 4 end_of_buffer_offset_ += buffer_.size();
187 4 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 4 4
188 0 buffer_.clear();
189 0 ▶ ReportDrop(kBlockSize, status);
Branch: 1 branch
BranchTrueFalseTotal
ReportDrop(kBlockSize, status); 0 0 0
190 0 eof_ = true;
191 0 return kEof;
192 4 ▶ } else if (buffer_.size() < kBlockSize) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (buffer_.size() < kBlockSize) { 4 0 4
193 4 eof_ = true;
194 }
195 4 continue;
196 8 ▶ } else if (buffer_.size() == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (buffer_.size() == 0) { 0 4 4
} else if (buffer_.size() == 0) { 4 0 4
197 // End of file
198 4 return kEof;
199 } else {
200 0 size_t drop_size = buffer_.size();
201 0 buffer_.clear();
202 0 ReportCorruption(drop_size, "truncated record at end of file");
203 0 return kEof;
204 }
205 }
206  
207 // Parse the header
208 17 const char* header = buffer_.data();
209 17 const uint32_t a = static_cast<uint32_t>(header[4]) & 0xff;
210 17 const uint32_t b = static_cast<uint32_t>(header[5]) & 0xff;
211 17 const unsigned int type = header[6];
212 17 const uint32_t length = a | (b << 8);
213 17 ▶ if (kHeaderSize + length > buffer_.size()) {
Branch: 1 branch
BranchTrueFalseTotal
if (kHeaderSize + length > buffer_.size()) { 0 17 17
214 0 size_t drop_size = buffer_.size();
215 0 buffer_.clear();
216 0 ReportCorruption(drop_size, "bad record length");
217 0 return kBadRecord;
218 }
219  
220 17 ▶ if (type == kZeroType && length == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (type == kZeroType && length == 0) { 0 17 17
if (type == kZeroType && length == 0) { 0 0 0
221 // Skip zero length record without reporting any drops since
222 // such records are produced by the mmap based writing code in
223 // env_posix.cc that preallocates file regions.
224 0 buffer_.clear();
225 0 return kBadRecord;
226 }
227  
228 // Check crc
229 17 ▶ if (checksum_) {
Branch: 1 branch
BranchTrueFalseTotal
if (checksum_) { 17 0 17
230 17 uint32_t expected_crc = crc32c::Unmask(DecodeFixed32(header));
231 17 uint32_t actual_crc = crc32c::Value(header + 6, 1 + length);
232 17 ▶ if (actual_crc != expected_crc) {
Branch: 1 branch
BranchTrueFalseTotal
if (actual_crc != expected_crc) { 0 17 17
233 // Drop the rest of the buffer since "length" itself may have
234 // been corrupted and if we trust it, we could find some
235 // fragment of a real log record that just happens to look
236 // like a valid log record.
237 0 size_t drop_size = buffer_.size();
238 0 buffer_.clear();
239 0 ReportCorruption(drop_size, "checksum mismatch");
240 0 return kBadRecord;
241 }
242 }
243  
244 17 buffer_.remove_prefix(kHeaderSize + length);
245  
246 // Skip physical record that started before initial_offset_
247 17 if (end_of_buffer_offset_ - buffer_.size() - kHeaderSize - length <
248 17 ▶ initial_offset_) {
Branch: 1 branch
BranchTrueFalseTotal
initial_offset_) { 0 17 17
249 0 result->clear();
250 0 return kBadRecord;
251 }
252  
253 17 *result = Slice(header + kHeaderSize, length);
254 17 return type;
255 4 }
256 }
257  
258 }
259 }
« db/db_impl.cc ↑ Summary util/comparator.cc »