db/version_set.cc

filter: Statementstmt 41.8% 310 / 741 Branchbran 25.1% 207 / 822 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 33.0% 517 / 1563 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« db/version_edit.cc ↑ Summary db/db_impl.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/version_set.h"
6  
7 #include <algorithm>
8 #include <stdio.h>
9 #include "db/filename.h"
10 #include "db/log_reader.h"
11 #include "db/log_writer.h"
12 #include "db/memtable.h"
13 #include "db/table_cache.h"
14 #include "leveldb/env.h"
15 #include "leveldb/table_builder.h"
16 #include "table/merger.h"
17 #include "table/two_level_iterator.h"
18 #include "util/coding.h"
19 #include "util/logging.h"
20  
21 namespace leveldb {
22  
23 static const int kTargetFileSize = 2 * 1048576;
24  
25 // Maximum bytes of overlaps in grandparent (i.e., level+2) before we
26 // stop building a single file in a level->level+1 compaction.
27 static const int64_t kMaxGrandParentOverlapBytes = 10 * kTargetFileSize;
28  
29 30 static double MaxBytesForLevel(int level) {
30 // Note: the result for level zero is not really used since we set
31 // the level-0 compaction threshold based on number of files.
32 30 double result = 10 * 1048576.0; // Result for both level-0 and level-1
33 90 ▶ while (level > 1) {
Branch: 1 branch
BranchTrueFalseTotal
while (level > 1) { 60 30 90
34 60 result *= 10;
35 60 level--;
36 }
37 30 return result;
38 }
39  
40 0 static uint64_t MaxFileSizeForLevel(int level) {
41 0 return kTargetFileSize; // We could vary per level to reduce number of files?
42 }
43  
44 namespace {
45 0 std::string IntSetToString(const std::set<uint64_t>& s) {
46 0 ▶ std::string result = "{";
Branch: 1 branch
BranchTrueFalseTotal
std::string result = "{"; 0 0 0
47 0 for (std::set<uint64_t>::const_iterator it = s.begin();
48 0 ▶ it != s.end();
Branch: 1 branch
BranchTrueFalseTotal
it != s.end(); 0 0 0
49 0 ++it) {
50 0 ▶ result += (result.size() > 1) ? "," : "";
Branch: 2 branches
BranchTrueFalseTotal
result += (result.size() > 1) ? "," : ""; 0 0 0
result += (result.size() > 1) ? "," : ""; 0 0 0
51 0 ▶ result += NumberToString(*it);
Branch: 2 branches
BranchTrueFalseTotal
result += NumberToString(*it); 0 0 0
result += NumberToString(*it); 0 0 0
52 }
53 0 ▶ result += "}";
Branch: 1 branch
BranchTrueFalseTotal
result += "}"; 0 0 0
54 0 return result;
55 }
56 }
57  
58 112 ▶Version::~Version() {
Branch: 1 branch
BranchTrueFalseTotal
Version::~Version() { 14 0 14
59 14 ▶ assert(refs_ == 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(refs_ == 0); 0 14 14
60  
61 // Remove from linked list
62 14 prev_->next_ = next_;
63 14 next_->prev_ = prev_;
64  
65 // Drop references to files
66 112 ▶ for (int level = 0; level < config::kNumLevels; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 0; level < config::kNumLevels; level++) { 98 14 112
67 99 ▶ for (size_t i = 0; i < files_[level].size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < files_[level].size(); i++) { 1 98 99
68 1 FileMetaData* f = files_[level][i];
69 1 ▶ assert(f->refs > 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(f->refs > 0); 0 1 1
70 1 f->refs--;
71 1 ▶ if (f->refs <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->refs <= 0) { 1 0 1
72 1 ▶ delete f;
Branch: 1 branch
BranchTrueFalseTotal
delete f; 1 0 1
73 }
74 }
75 }
76 112 ▶}
Branch: 1 branch
BranchTrueFalseTotal
} 98 14 112
77  
78 0 int FindFile(const InternalKeyComparator& icmp,
79 const std::vector<FileMetaData*>& files,
80 const Slice& key) {
81 0 uint32_t left = 0;
82 0 uint32_t right = files.size();
83 0 ▶ while (left < right) {
Branch: 1 branch
BranchTrueFalseTotal
while (left < right) { 0 0 0
84 0 uint32_t mid = (left + right) / 2;
85 0 const FileMetaData* f = files[mid];
86 0 ▶ if (icmp.InternalKeyComparator::Compare(f->largest.Encode(), key) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (icmp.InternalKeyComparator::Compare(f->largest.Encode(), key) < 0) { 0 0 0
if (icmp.InternalKeyComparator::Compare(f->largest.Encode(), key) < 0) { 0 0 0
87 // Key at "mid.largest" is < "target". Therefore all
88 // files at or before "mid" are uninteresting.
89 0 left = mid + 1;
90 } else {
91 // Key at "mid.largest" is >= "target". Therefore all files
92 // after "mid" are uninteresting.
93 0 right = mid;
94 }
95 }
96 0 return right;
97 }
98  
99 0 bool SomeFileOverlapsRange(
100 const InternalKeyComparator& icmp,
101 const std::vector<FileMetaData*>& files,
102 const Slice& smallest_user_key,
103 const Slice& largest_user_key) {
104 // Find the earliest possible internal key for smallest_user_key
105 0 ▶ InternalKey small(smallest_user_key, kMaxSequenceNumber, kValueTypeForSeek);
Branch: 1 branch
BranchTrueFalseTotal
InternalKey small(smallest_user_key, kMaxSequenceNumber, kValueTypeForSeek); 0 0 0
106 0 ▶ const uint32_t index = FindFile(icmp, files, small.Encode());
Branch: 1 branch
BranchTrueFalseTotal
const uint32_t index = FindFile(icmp, files, small.Encode()); 0 0 0
107 0 ▶ return ((index < files.size()) &&
Branch: 1 branch
BranchTrueFalseTotal
return ((index < files.size()) && 0 0 0
108 0 ▶ icmp.user_comparator()->Compare(
Branch: 2 branches
BranchTrueFalseTotal
icmp.user_comparator()->Compare( 0 0 0
icmp.user_comparator()->Compare( 0 0 0
109 0 largest_user_key, files[index]->smallest.user_key()) >= 0);
110 0 }
111  
112 // An internal iterator. For a given version/level pair, yields
113 // information about the files in the level. For a given entry, key()
114 // is the largest key that occurs in the file, and value() is an
115 // 16-byte value containing the file number and file size, both
116 // encoded using EncodeFixed64.
117 class Version::LevelFileNumIterator : public Iterator {
118 public:
119 0 LevelFileNumIterator(const InternalKeyComparator& icmp,
120 const std::vector<FileMetaData*>* flist)
121 0 : icmp_(icmp),
122 0 flist_(flist),
123 0 index_(flist->size()) { // Marks as invalid
124 0 }
125 0 virtual bool Valid() const {
126 0 return index_ < flist_->size();
127 }
128 0 virtual void Seek(const Slice& target) {
129 0 index_ = FindFile(icmp_, *flist_, target);
130 0 }
131 0 virtual void SeekToFirst() { index_ = 0; }
132 0 virtual void SeekToLast() {
133 0 ▶ index_ = flist_->empty() ? 0 : flist_->size() - 1;
Branch: 1 branch
BranchTrueFalseTotal
index_ = flist_->empty() ? 0 : flist_->size() - 1; 0 0 0
134 0 }
135 0 virtual void Next() {
136 0 ▶ assert(Valid());
Branch: 1 branch
BranchTrueFalseTotal
assert(Valid()); 0 0 0
137 0 index_++;
138 0 }
139 0 virtual void Prev() {
140 0 ▶ assert(Valid());
Branch: 1 branch
BranchTrueFalseTotal
assert(Valid()); 0 0 0
141 0 ▶ if (index_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index_ == 0) { 0 0 0
142 0 index_ = flist_->size(); // Marks as invalid
143 } else {
144 0 index_--;
145 }
146 0 }
147 0 Slice key() const {
148 0 ▶ assert(Valid());
Branch: 1 branch
BranchTrueFalseTotal
assert(Valid()); 0 0 0
149 0 return (*flist_)[index_]->largest.Encode();
150 }
151 0 Slice value() const {
152 0 ▶ assert(Valid());
Branch: 1 branch
BranchTrueFalseTotal
assert(Valid()); 0 0 0
153 0 EncodeFixed64(value_buf_, (*flist_)[index_]->number);
154 0 EncodeFixed64(value_buf_+8, (*flist_)[index_]->file_size);
155 0 return Slice(value_buf_, sizeof(value_buf_));
156 }
157 0 virtual Status status() const { return Status::OK(); }
158 private:
159 const InternalKeyComparator icmp_;
160 const std::vector<FileMetaData*>* const flist_;
161 uint32_t index_;
162  
163 // Backing store for value(). Holds the file number and size.
164 mutable char value_buf_[16];
165 };
166  
167 0 static Iterator* GetFileIterator(void* arg,
168 const ReadOptions& options,
169 const Slice& file_value) {
170 0 TableCache* cache = reinterpret_cast<TableCache*>(arg);
171 0 ▶ if (file_value.size() != 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (file_value.size() != 16) { 0 0 0
172 0 ▶ return NewErrorIterator(
Branch: 1 branch
BranchTrueFalseTotal
return NewErrorIterator( 0 0 0
173 0 ▶ Status::Corruption("FileReader invoked with unexpected value"));
Branch: 1 branch
BranchTrueFalseTotal
Status::Corruption("FileReader invoked with unexpected value")); 0 0 0
174 } else {
175 0 return cache->NewIterator(options,
176 DecodeFixed64(file_value.data()),
177 0 DecodeFixed64(file_value.data() + 8));
178 }
179 }
180  
181 0 Iterator* Version::NewConcatenatingIterator(const ReadOptions& options,
182 int level) const {
183 0 ▶ return NewTwoLevelIterator(
Branch: 2 branches
BranchTrueFalseTotal
return NewTwoLevelIterator( 0 0 0
return NewTwoLevelIterator( 0 0 0
184 0 ▶ new LevelFileNumIterator(vset_->icmp_, &files_[level]),
Branch: 1 branch
BranchTrueFalseTotal
new LevelFileNumIterator(vset_->icmp_, &files_[level]), 0 0 0
185 0 &GetFileIterator, vset_->table_cache_, options);
186 }
187  
188 14 void Version::AddIterators(const ReadOptions& options,
189 std::vector<Iterator*>* iters) {
190 // Merge all level zero files together since they may overlap
191 15 ▶ for (size_t i = 0; i < files_[0].size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < files_[0].size(); i++) { 1 14 15
192 2 iters->push_back(
193 1 ▶ vset_->table_cache_->NewIterator(
Branch: 2 branches
BranchTrueFalseTotal
vset_->table_cache_->NewIterator( 1 0 1
vset_->table_cache_->NewIterator( 1 0 1
194 1 options, files_[0][i]->number, files_[0][i]->file_size));
195 }
196  
197 // For levels > 0, we can use a concatenating iterator that sequentially
198 // walks through the non-overlapping files in the level, opening them
199 // lazily.
200 98 ▶ for (int level = 1; level < config::kNumLevels; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 1; level < config::kNumLevels; level++) { 84 14 98
201 84 ▶ if (!files_[level].empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!files_[level].empty()) { 0 84 84
202 0 ▶ iters->push_back(NewConcatenatingIterator(options, level));
Branch: 2 branches
BranchTrueFalseTotal
iters->push_back(NewConcatenatingIterator(options, level)); 0 0 0
iters->push_back(NewConcatenatingIterator(options, level)); 0 0 0
203 }
204 }
205 14 }
206  
207 // If "*iter" points at a value or deletion for user_key, store
208 // either the value, or a NotFound error and return true.
209 // Else return false.
210 0 static bool GetValue(Iterator* iter, const Slice& user_key,
211 std::string* value,
212 Status* s) {
213 0 ▶ if (!iter->Valid()) {
Branch: 2 branches
BranchTrueFalseTotal
if (!iter->Valid()) { 0 0 0
if (!iter->Valid()) { 0 0 0
214 0 return false;
215 }
216 0 ParsedInternalKey parsed_key;
217 0 ▶ if (!ParseInternalKey(iter->key(), &parsed_key)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!ParseInternalKey(iter->key(), &parsed_key)) { 0 0 0
if (!ParseInternalKey(iter->key(), &parsed_key)) { 0 0 0
if (!ParseInternalKey(iter->key(), &parsed_key)) { 0 0 0
218 0 ▶ *s = Status::Corruption("corrupted key for ", user_key);
Branch: 2 branches
BranchTrueFalseTotal
*s = Status::Corruption("corrupted key for ", user_key); 0 0 0
*s = Status::Corruption("corrupted key for ", user_key); 0 0 0
219 0 return true;
220 }
221 0 ▶ if (parsed_key.user_key != user_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (parsed_key.user_key != user_key) { 0 0 0
222 0 return false;
223 }
224 0 switch (parsed_key.type) {
225 0 case kTypeDeletion:
226 0 ▶ *s = Status::NotFound(Slice()); // Use an empty error message for speed
Branch: 2 branches
BranchTrueFalseTotal
*s = Status::NotFound(Slice()); // Use an empty error message for speed 0 0 0
*s = Status::NotFound(Slice()); // Use an empty error message for speed 0 0 0
227 0 break;
228 0 case kTypeValue: {
229 0 ▶ Slice v = iter->value();
Branch: 1 branch
BranchTrueFalseTotal
Slice v = iter->value(); 0 0 0
230 0 ▶ value->assign(v.data(), v.size());
Branch: 1 branch
BranchTrueFalseTotal
value->assign(v.data(), v.size()); 0 0 0
231 0 break;
232 }
233 }
234 0 return true;
235 }
236  
237 0 static bool NewestFirst(FileMetaData* a, FileMetaData* b) {
238 0 return a->number > b->number;
239 }
240  
241 2 Status Version::Get(const ReadOptions& options,
242 const LookupKey& k,
243 std::string* value,
244 GetStats* stats) {
245 2 Slice ikey = k.internal_key();
246 2 Slice user_key = k.user_key();
247 2 const Comparator* ucmp = vset_->icmp_.user_comparator();
248 2 Status s;
249  
250 2 stats->seek_file = NULL;
251 2 stats->seek_file_level = -1;
252 2 FileMetaData* last_file_read = NULL;
253 2 int last_file_read_level = -1;
254  
255 // We can search level-by-level since entries never hop across
256 // levels. Therefore we are guaranteed that if we find data
257 // in an smaller level, later levels are irrelevant.
258 2 std::vector<FileMetaData*> tmp;
259 FileMetaData* tmp2;
260 16 ▶ for (int level = 0; level < config::kNumLevels; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 0; level < config::kNumLevels; level++) { 14 2 16
261 14 size_t num_files = files_[level].size();
262 14 ▶ if (num_files == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (num_files == 0) continue; 14 0 14
263  
264 // Get the list of files to search in this level
265 0 FileMetaData* const* files = &files_[level][0];
266 0 ▶ if (level == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (level == 0) { 0 0 0
267 // Level-0 files may overlap each other. Find all files that
268 // overlap user_key and process them in order from newest to oldest.
269 0 ▶ tmp.reserve(num_files);
Branch: 1 branch
BranchTrueFalseTotal
tmp.reserve(num_files); 0 0 0
270 0 ▶ for (uint32_t i = 0; i < num_files; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t i = 0; i < num_files; i++) { 0 0 0
271 0 FileMetaData* f = files[i];
272 0 ▶ if (ucmp->Compare(user_key, f->smallest.user_key()) >= 0 &&
Branch: 2 branches
BranchTrueFalseTotal
if (ucmp->Compare(user_key, f->smallest.user_key()) >= 0 && 0 0 0
if (ucmp->Compare(user_key, f->smallest.user_key()) >= 0 && 0 0 0
273 0 ▶ ucmp->Compare(user_key, f->largest.user_key()) <= 0) {
Branch: 3 branches
BranchTrueFalseTotal
ucmp->Compare(user_key, f->largest.user_key()) <= 0) { 0 0 0
ucmp->Compare(user_key, f->largest.user_key()) <= 0) { 0 0 0
ucmp->Compare(user_key, f->largest.user_key()) <= 0) { 0 0 0
274 0 ▶ tmp.push_back(f);
Branch: 1 branch
BranchTrueFalseTotal
tmp.push_back(f); 0 0 0
275 }
276 }
277 0 ▶ if (tmp.empty()) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (tmp.empty()) continue; 0 0 0
278  
279 0 ▶ std::sort(tmp.begin(), tmp.end(), NewestFirst);
Branch: 1 branch
BranchTrueFalseTotal
std::sort(tmp.begin(), tmp.end(), NewestFirst); 0 0 0
280 0 files = &tmp[0];
281 0 num_files = tmp.size();
282 } else {
283 // Binary search to find earliest index whose largest key >= ikey.
284 0 ▶ uint32_t index = FindFile(vset_->icmp_, files_[level], ikey);
Branch: 1 branch
BranchTrueFalseTotal
uint32_t index = FindFile(vset_->icmp_, files_[level], ikey); 0 0 0
285 0 ▶ if (index >= num_files) {
Branch: 1 branch
BranchTrueFalseTotal
if (index >= num_files) { 0 0 0
286 0 files = NULL;
287 0 num_files = 0;
288 } else {
289 0 tmp2 = files[index];
290 0 ▶ if (ucmp->Compare(user_key, tmp2->smallest.user_key()) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ucmp->Compare(user_key, tmp2->smallest.user_key()) < 0) { 0 0 0
if (ucmp->Compare(user_key, tmp2->smallest.user_key()) < 0) { 0 0 0
291 // All of "tmp2" is past any data for user_key
292 0 files = NULL;
293 0 num_files = 0;
294 } else {
295 0 files = &tmp2;
296 0 num_files = 1;
297 }
298 }
299 }
300  
301 0 ▶ for (uint32_t i = 0; i < num_files; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t i = 0; i < num_files; ++i) { 0 0 0
302 0 ▶ if (last_file_read != NULL && stats->seek_file == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (last_file_read != NULL && stats->seek_file == NULL) { 0 0 0
if (last_file_read != NULL && stats->seek_file == NULL) { 0 0 0
303 // We have had more than one seek for this read. Charge the 1st file.
304 0 stats->seek_file = last_file_read;
305 0 stats->seek_file_level = last_file_read_level;
306 }
307  
308 0 FileMetaData* f = files[i];
309 0 last_file_read = f;
310 0 last_file_read_level = level;
311  
312 0 ▶ Iterator* iter = vset_->table_cache_->NewIterator(
Branch: 1 branch
BranchTrueFalseTotal
Iterator* iter = vset_->table_cache_->NewIterator( 0 0 0
313 options,
314 f->number,
315 f->file_size);
316 0 ▶ iter->Seek(ikey);
Branch: 1 branch
BranchTrueFalseTotal
iter->Seek(ikey); 0 0 0
317 0 ▶ const bool done = GetValue(iter, user_key, value, &s);
Branch: 1 branch
BranchTrueFalseTotal
const bool done = GetValue(iter, user_key, value, &s); 0 0 0
318 0 ▶ if (!iter->status().ok()) {
Branch: 2 branches
BranchTrueFalseTotal
if (!iter->status().ok()) { 0 0 0
if (!iter->status().ok()) { 0 0 0
319 0 ▶ s = iter->status();
Branch: 2 branches
BranchTrueFalseTotal
s = iter->status(); 0 0 0
s = iter->status(); 0 0 0
320 0 ▶ delete iter;
Branch: 1 branch
BranchTrueFalseTotal
delete iter; 0 0 0
321 0 ▶ return s;
Branch: 1 branch
BranchTrueFalseTotal
return s; 0 0 0
322 } else {
323 0 ▶ delete iter;
Branch: 1 branch
BranchTrueFalseTotal
delete iter; 0 0 0
324 0 ▶ if (done) {
Branch: 1 branch
BranchTrueFalseTotal
if (done) { 0 0 0
325 0 ▶ return s;
Branch: 1 branch
BranchTrueFalseTotal
return s; 0 0 0
326 }
327 }
328 }
329 }
330  
331 4 ▶ return Status::NotFound(Slice()); // Use an empty error message for speed
Branch: 1 branch
BranchTrueFalseTotal
return Status::NotFound(Slice()); // Use an empty error message for speed 2 0 2
332 2 }
333  
334 2 bool Version::UpdateStats(const GetStats& stats) {
335 2 FileMetaData* f = stats.seek_file;
336 2 ▶ if (f != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (f != NULL) { 0 2 2
337 0 f->allowed_seeks--;
338 0 ▶ if (f->allowed_seeks <= 0 && file_to_compact_ == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (f->allowed_seeks <= 0 && file_to_compact_ == NULL) { 0 0 0
if (f->allowed_seeks <= 0 && file_to_compact_ == NULL) { 0 0 0
339 0 file_to_compact_ = f;
340 0 file_to_compact_level_ = stats.seek_file_level;
341 0 return true;
342 }
343 }
344 2 return false;
345 }
346  
347 38 void Version::Ref() {
348 38 ++refs_;
349 38 }
350  
351 38 void Version::Unref() {
352 38 ▶ assert(this != &vset_->dummy_versions_);
Branch: 1 branch
BranchTrueFalseTotal
assert(this != &vset_->dummy_versions_); 0 38 38
353 38 ▶ assert(refs_ >= 1);
Branch: 1 branch
BranchTrueFalseTotal
assert(refs_ >= 1); 0 38 38
354 38 --refs_;
355 38 ▶ if (refs_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (refs_ == 0) { 10 28 38
356 10 ▶ delete this;
Branch: 1 branch
BranchTrueFalseTotal
delete this; 10 0 10
357 }
358 38 }
359  
360 0 bool Version::OverlapInLevel(int level,
361 const Slice& smallest_user_key,
362 const Slice& largest_user_key) {
363 0 return SomeFileOverlapsRange(vset_->icmp_, files_[level],
364 smallest_user_key,
365 0 largest_user_key);
366 }
367  
368 0 std::string Version::DebugString() const {
369 0 std::string r;
370 0 ▶ for (int level = 0; level < config::kNumLevels; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 0; level < config::kNumLevels; level++) { 0 0 0
371 // E.g.,
372 // --- level 1 ---
373 // 17:123['a' .. 'd']
374 // 20:43['e' .. 'g']
375 0 ▶ r.append("--- level ");
Branch: 1 branch
BranchTrueFalseTotal
r.append("--- level "); 0 0 0
376 0 ▶ AppendNumberTo(&r, level);
Branch: 1 branch
BranchTrueFalseTotal
AppendNumberTo(&r, level); 0 0 0
377 0 ▶ r.append(" ---\n");
Branch: 1 branch
BranchTrueFalseTotal
r.append(" ---\n"); 0 0 0
378 0 const std::vector<FileMetaData*>& files = files_[level];
379 0 ▶ for (size_t i = 0; i < files.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < files.size(); i++) { 0 0 0
380 0 ▶ r.push_back(' ');
Branch: 1 branch
BranchTrueFalseTotal
r.push_back(' '); 0 0 0
381 0 ▶ AppendNumberTo(&r, files[i]->number);
Branch: 1 branch
BranchTrueFalseTotal
AppendNumberTo(&r, files[i]->number); 0 0 0
382 0 ▶ r.push_back(':');
Branch: 1 branch
BranchTrueFalseTotal
r.push_back(':'); 0 0 0
383 0 ▶ AppendNumberTo(&r, files[i]->file_size);
Branch: 1 branch
BranchTrueFalseTotal
AppendNumberTo(&r, files[i]->file_size); 0 0 0
384 0 ▶ r.append("['");
Branch: 1 branch
BranchTrueFalseTotal
r.append("['"); 0 0 0
385 0 ▶ AppendEscapedStringTo(&r, files[i]->smallest.Encode());
Branch: 1 branch
BranchTrueFalseTotal
AppendEscapedStringTo(&r, files[i]->smallest.Encode()); 0 0 0
386 0 ▶ r.append("' .. '");
Branch: 1 branch
BranchTrueFalseTotal
r.append("' .. '"); 0 0 0
387 0 ▶ AppendEscapedStringTo(&r, files[i]->largest.Encode());
Branch: 1 branch
BranchTrueFalseTotal
AppendEscapedStringTo(&r, files[i]->largest.Encode()); 0 0 0
388 0 ▶ r.append("']\n");
Branch: 1 branch
BranchTrueFalseTotal
r.append("']\n"); 0 0 0
389 }
390 }
391 0 return r;
392 }
393  
394 // A helper class so we can efficiently apply a whole sequence
395 // of edits to a particular state without creating intermediate
396 // Versions that contain full copies of the intermediate state.
397 class VersionSet::Builder {
398 private:
399 // Helper to sort by v->files_[file_number].smallest
400 struct BySmallestKey {
401 const InternalKeyComparator* internal_comparator;
402  
403 0 bool operator()(FileMetaData* f1, FileMetaData* f2) const {
404 0 int r = internal_comparator->Compare(f1->smallest, f2->smallest);
405 0 ▶ if (r != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r != 0) { 0 0 0
406 0 return (r < 0);
407 } else {
408 // Break ties by file number
409 0 return (f1->number < f2->number);
410 }
411 }
412 };
413  
414 typedef std::set<FileMetaData*, BySmallestKey> FileSet;
415 struct LevelState {
416 std::set<uint64_t> deleted_files;
417 FileSet* added_files;
418 };
419  
420 VersionSet* vset_;
421 Version* base_;
422 LevelState levels_[config::kNumLevels];
423  
424 public:
425 // Initialize a builder with the files from *base and other info from *vset
426 6 Builder(VersionSet* vset, Version* base)
427 6 : vset_(vset),
428 48 ▶ base_(base) {
Branch: 2 branches
BranchTrueFalseTotal
base_(base) { 42 6 48
base_(base) { 0 0 0
429 6 ▶ base_->Ref();
Branch: 1 branch
BranchTrueFalseTotal
base_->Ref(); 6 0 6
430 BySmallestKey cmp;
431 6 cmp.internal_comparator = &vset_->icmp_;
432 48 ▶ for (int level = 0; level < config::kNumLevels; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 0; level < config::kNumLevels; level++) { 42 6 48
433 84 ▶ levels_[level].added_files = new FileSet(cmp);
Branch: 3 branches
BranchTrueFalseTotal
levels_[level].added_files = new FileSet(cmp); 42 0 42
levels_[level].added_files = new FileSet(cmp); 42 0 42
levels_[level].added_files = new FileSet(cmp); 0 0 0
434 }
435 6 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 0 0
436  
437 48 ▶ ~Builder() {
Branch: 1 branch
BranchTrueFalseTotal
~Builder() { 6 0 6
438 48 ▶ for (int level = 0; level < config::kNumLevels; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 0; level < config::kNumLevels; level++) { 42 6 48
439 42 const FileSet* added = levels_[level].added_files;
440 42 std::vector<FileMetaData*> to_unref;
441 42 to_unref.reserve(added->size());
442 42 for (FileSet::const_iterator it = added->begin();
443 43 ▶ it != added->end(); ++it) {
Branch: 1 branch
BranchTrueFalseTotal
it != added->end(); ++it) { 1 42 43
444 1 to_unref.push_back(*it);
445 }
446 42 ▶ delete added;
Branch: 1 branch
BranchTrueFalseTotal
delete added; 42 0 42
447 43 ▶ for (uint32_t i = 0; i < to_unref.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t i = 0; i < to_unref.size(); i++) { 1 42 43
448 1 FileMetaData* f = to_unref[i];
449 1 f->refs--;
450 1 ▶ if (f->refs <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->refs <= 0) { 0 1 1
451 0 ▶ delete f;
Branch: 1 branch
BranchTrueFalseTotal
delete f; 0 0 0
452 }
453 }
454 42 }
455 6 base_->Unref();
456 48 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 42 6 48
457  
458 // Apply all of the edits in *edit to the current state.
459 7 void Apply(VersionEdit* edit) {
460 // Update compaction pointers
461 7 ▶ for (size_t i = 0; i < edit->compact_pointers_.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < edit->compact_pointers_.size(); i++) { 0 7 7
462 0 const int level = edit->compact_pointers_[i].first;
463 0 vset_->compact_pointer_[level] =
464 0 ▶ edit->compact_pointers_[i].second.Encode().ToString();
Branch: 1 branch
BranchTrueFalseTotal
edit->compact_pointers_[i].second.Encode().ToString(); 0 0 0
465 }
466  
467 // Delete files
468 7 const VersionEdit::DeletedFileSet& del = edit->deleted_files_;
469 7 for (VersionEdit::DeletedFileSet::const_iterator iter = del.begin();
470 7 ▶ iter != del.end();
Branch: 1 branch
BranchTrueFalseTotal
iter != del.end(); 0 7 7
471 0 ++iter) {
472 0 const int level = iter->first;
473 0 const uint64_t number = iter->second;
474 0 ▶ levels_[level].deleted_files.insert(number);
Branch: 1 branch
BranchTrueFalseTotal
levels_[level].deleted_files.insert(number); 0 0 0
475 }
476  
477 // Add new files
478 8 ▶ for (size_t i = 0; i < edit->new_files_.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < edit->new_files_.size(); i++) { 1 7 8
479 1 const int level = edit->new_files_[i].first;
480 1 ▶ FileMetaData* f = new FileMetaData(edit->new_files_[i].second);
Branch: 3 branches
BranchTrueFalseTotal
FileMetaData* f = new FileMetaData(edit->new_files_[i].second); 1 0 1
FileMetaData* f = new FileMetaData(edit->new_files_[i].second); 1 0 1
FileMetaData* f = new FileMetaData(edit->new_files_[i].second); 0 0 0
481 1 f->refs = 1;
482  
483 // We arrange to automatically compact this file after
484 // a certain number of seeks. Let's assume:
485 // (1) One seek costs 10ms
486 // (2) Writing or reading 1MB costs 10ms (100MB/s)
487 // (3) A compaction of 1MB does 25MB of IO:
488 // 1MB read from this level
489 // 10-12MB read from next level (boundaries may be misaligned)
490 // 10-12MB written to next level
491 // This implies that 25 seeks cost the same as the compaction
492 // of 1MB of data. I.e., one seek costs approximately the
493 // same as the compaction of 40KB of data. We are a little
494 // conservative and allow approximately one seek for every 16KB
495 // of data before triggering a compaction.
496 1 f->allowed_seeks = (f->file_size / 16384);
497 1 ▶ if (f->allowed_seeks < 100) f->allowed_seeks = 100;
Branch: 1 branch
BranchTrueFalseTotal
if (f->allowed_seeks < 100) f->allowed_seeks = 100; 1 0 1
498  
499 1 ▶ levels_[level].deleted_files.erase(f->number);
Branch: 1 branch
BranchTrueFalseTotal
levels_[level].deleted_files.erase(f->number); 1 0 1
500 1 ▶ levels_[level].added_files->insert(f);
Branch: 1 branch
BranchTrueFalseTotal
levels_[level].added_files->insert(f); 1 0 1
501 }
502 7 }
503  
504 // Save the current state in *v.
505 6 void SaveTo(Version* v) {
506 BySmallestKey cmp;
507 6 cmp.internal_comparator = &vset_->icmp_;
508 48 ▶ for (int level = 0; level < config::kNumLevels; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 0; level < config::kNumLevels; level++) { 42 6 48
509 // Merge the set of added files with the set of pre-existing files.
510 // Drop any deleted files. Store the result in *v.
511 42 const std::vector<FileMetaData*>& base_files = base_->files_[level];
512 42 std::vector<FileMetaData*>::const_iterator base_iter = base_files.begin();
513 42 std::vector<FileMetaData*>::const_iterator base_end = base_files.end();
514 42 const FileSet* added = levels_[level].added_files;
515 42 ▶ v->files_[level].reserve(base_files.size() + added->size());
Branch: 1 branch
BranchTrueFalseTotal
v->files_[level].reserve(base_files.size() + added->size()); 42 0 42
516 42 for (FileSet::const_iterator added_iter = added->begin();
517 43 ▶ added_iter != added->end();
Branch: 1 branch
BranchTrueFalseTotal
added_iter != added->end(); 1 42 43
518 1 ++added_iter) {
519 // Add all smaller files listed in base_
520 1 for (std::vector<FileMetaData*>::const_iterator bpos
521 1 ▶ = std::upper_bound(base_iter, base_end, *added_iter, cmp);
Branch: 1 branch
BranchTrueFalseTotal
= std::upper_bound(base_iter, base_end, *added_iter, cmp); 1 0 1
522 1 ▶ base_iter != bpos;
Branch: 1 branch
BranchTrueFalseTotal
base_iter != bpos; 0 1 1
523 0 ++base_iter) {
524 0 ▶ MaybeAddFile(v, level, *base_iter);
Branch: 1 branch
BranchTrueFalseTotal
MaybeAddFile(v, level, *base_iter); 0 0 0
525 }
526  
527 1 ▶ MaybeAddFile(v, level, *added_iter);
Branch: 1 branch
BranchTrueFalseTotal
MaybeAddFile(v, level, *added_iter); 1 0 1
528 }
529  
530 // Add remaining base files
531 42 ▶ for (; base_iter != base_end; ++base_iter) {
Branch: 1 branch
BranchTrueFalseTotal
for (; base_iter != base_end; ++base_iter) { 0 42 42
532 0 ▶ MaybeAddFile(v, level, *base_iter);
Branch: 1 branch
BranchTrueFalseTotal
MaybeAddFile(v, level, *base_iter); 0 0 0
533 }
534  
535 #ifndef NDEBUG
536 // Make sure there is no overlap in levels > 0
537 42 ▶ if (level > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (level > 0) { 36 6 42
538 36 ▶ for (uint32_t i = 1; i < v->files_[level].size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t i = 1; i < v->files_[level].size(); i++) { 0 36 36
539 0 const InternalKey& prev_end = v->files_[level][i-1]->largest;
540 0 const InternalKey& this_begin = v->files_[level][i]->smallest;
541 0 ▶ if (vset_->icmp_.Compare(prev_end, this_begin) >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (vset_->icmp_.Compare(prev_end, this_begin) >= 0) { 0 0 0
if (vset_->icmp_.Compare(prev_end, this_begin) >= 0) { 0 0 0
542 0 ▶ fprintf(stderr, "overlapping ranges in same level %s vs. %s\n",
Branch: 1 branch
BranchTrueFalseTotal
fprintf(stderr, "overlapping ranges in same level %s vs. %s\n", 0 0 0
543 0 ▶ EscapeString(prev_end.Encode()).c_str(),
Branch: 1 branch
BranchTrueFalseTotal
EscapeString(prev_end.Encode()).c_str(), 0 0 0
544 0 ▶ EscapeString(this_begin.Encode()).c_str());
Branch: 1 branch
BranchTrueFalseTotal
EscapeString(this_begin.Encode()).c_str()); 0 0 0
545 0 abort();
546 }
547 }
548 }
549 #endif
550 }
551 6 }
552  
553 1 void MaybeAddFile(Version* v, int level, FileMetaData* f) {
554 1 ▶ if (levels_[level].deleted_files.count(f->number) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (levels_[level].deleted_files.count(f->number) > 0) { 1 0 1
555 // File is deleted: do nothing
556 } else {
557 1 std::vector<FileMetaData*>* files = &v->files_[level];
558 1 ▶ if (level > 0 && !files->empty()) {
Branch: 3 branches
BranchTrueFalseTotal
if (level > 0 && !files->empty()) { 0 1 1
if (level > 0 && !files->empty()) { 0 0 0
if (level > 0 && !files->empty()) { 0 1 1
559 // Must not overlap
560 0 ▶ assert(vset_->icmp_.Compare((*files)[files->size()-1]->largest,
Branch: 1 branch
BranchTrueFalseTotal
assert(vset_->icmp_.Compare((*files)[files->size()-1]->largest, 0 0 0
561 f->smallest) < 0);
562 }
563 1 f->refs++;
564 1 files->push_back(f);
565 }
566 1 }
567 };
568  
569 4 VersionSet::VersionSet(const std::string& dbname,
570 const Options* options,
571 TableCache* table_cache,
572 4 const InternalKeyComparator* cmp)
573 4 : env_(options->env),
574 4 dbname_(dbname),
575 4 options_(options),
576 4 table_cache_(table_cache),
577 4 icmp_(*cmp),
578 4 next_file_number_(2),
579 4 manifest_file_number_(0), // Filled by Recover()
580 4 last_sequence_(0),
581 4 log_number_(0),
582 4 prev_log_number_(0),
583 4 descriptor_file_(NULL),
584 4 descriptor_log_(NULL),
585 4 dummy_versions_(this),
586 32 ▶ current_(NULL) {
Branch: 2 branches
BranchTrueFalseTotal
current_(NULL) { 28 4 32
current_(NULL) { 0 0 0
587 4 ▶ AppendVersion(new Version(this));
Branch: 3 branches
BranchTrueFalseTotal
AppendVersion(new Version(this)); 4 0 4
AppendVersion(new Version(this)); 4 0 4
AppendVersion(new Version(this)); 0 0 0
588 4 ▶}
Branch: 1 branch
BranchTrueFalseTotal
} 0 0 0
589  
590 32 ▶VersionSet::~VersionSet() {
Branch: 1 branch
BranchTrueFalseTotal
VersionSet::~VersionSet() { 4 0 4
591 4 current_->Unref();
592 4 ▶ assert(dummy_versions_.next_ == &dummy_versions_); // List must be empty
Branch: 1 branch
BranchTrueFalseTotal
assert(dummy_versions_.next_ == &dummy_versions_); // List must be empty 0 4 4
593 4 ▶ delete descriptor_log_;
Branch: 1 branch
BranchTrueFalseTotal
delete descriptor_log_; 3 1 4
594 4 ▶ delete descriptor_file_;
Branch: 1 branch
BranchTrueFalseTotal
delete descriptor_file_; 3 1 4
595 36 ▶}
Branch: 1 branch
BranchTrueFalseTotal
} 28 4 32
596  
597 10 void VersionSet::AppendVersion(Version* v) {
598 // Make "v" current
599 10 ▶ assert(v->refs_ == 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(v->refs_ == 0); 0 10 10
600 10 ▶ assert(v != current_);
Branch: 1 branch
BranchTrueFalseTotal
assert(v != current_); 0 10 10
601 10 ▶ if (current_ != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (current_ != NULL) { 6 4 10
602 6 current_->Unref();
603 }
604 10 current_ = v;
605 10 v->Ref();
606  
607 // Append to linked list
608 10 v->prev_ = dummy_versions_.prev_;
609 10 v->next_ = &dummy_versions_;
610 10 v->prev_->next_ = v;
611 10 v->next_->prev_ = v;
612 10 }
613  
614 3 Status VersionSet::LogAndApply(VersionEdit* edit, port::Mutex* mu) {
615 3 ▶ if (edit->has_log_number_) {
Branch: 1 branch
BranchTrueFalseTotal
if (edit->has_log_number_) { 3 0 3
616 3 ▶ assert(edit->log_number_ >= log_number_);
Branch: 1 branch
BranchTrueFalseTotal
assert(edit->log_number_ >= log_number_); 0 3 3
617 3 ▶ assert(edit->log_number_ < next_file_number_);
Branch: 1 branch
BranchTrueFalseTotal
assert(edit->log_number_ < next_file_number_); 0 3 3
618 } else {
619 0 edit->SetLogNumber(log_number_);
620 }
621  
622 3 ▶ if (!edit->has_prev_log_number_) {
Branch: 1 branch
BranchTrueFalseTotal
if (!edit->has_prev_log_number_) { 3 0 3
623 3 edit->SetPrevLogNumber(prev_log_number_);
624 }
625  
626 3 edit->SetNextFile(next_file_number_);
627 3 edit->SetLastSequence(last_sequence_);
628  
629 3 ▶ Version* v = new Version(this);
Branch: 1 branch
BranchTrueFalseTotal
Version* v = new Version(this); 3 0 3
630 {
631 3 ▶ Builder builder(this, current_);
Branch: 1 branch
BranchTrueFalseTotal
Builder builder(this, current_); 3 0 3
632 3 ▶ builder.Apply(edit);
Branch: 1 branch
BranchTrueFalseTotal
builder.Apply(edit); 3 0 3
633 3 ▶ builder.SaveTo(v);
Branch: 1 branch
BranchTrueFalseTotal
builder.SaveTo(v); 3 0 3
634 3 }
635 3 ▶ Finalize(v);
Branch: 1 branch
BranchTrueFalseTotal
Finalize(v); 3 0 3
636  
637 // Initialize new descriptor log file if necessary by creating
638 // a temporary file that contains a snapshot of the current version.
639 3 std::string new_manifest_file;
640 3 Status s;
641 3 ▶ if (descriptor_log_ == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (descriptor_log_ == NULL) { 3 0 3
642 // No reason to unlock *mu here since we only hit this path in the
643 // first call to LogAndApply (when opening the database).
644 3 ▶ assert(descriptor_file_ == NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(descriptor_file_ == NULL); 0 3 3
645 3 ▶ new_manifest_file = DescriptorFileName(dbname_, manifest_file_number_);
Branch: 1 branch
BranchTrueFalseTotal
new_manifest_file = DescriptorFileName(dbname_, manifest_file_number_); 3 0 3
646 3 edit->SetNextFile(next_file_number_);
647 3 ▶ s = env_->NewWritableFile(new_manifest_file, &descriptor_file_);
Branch: 2 branches
BranchTrueFalseTotal
s = env_->NewWritableFile(new_manifest_file, &descriptor_file_); 3 0 3
s = env_->NewWritableFile(new_manifest_file, &descriptor_file_); 3 0 3
648 3 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 3 0 3
649 3 ▶ descriptor_log_ = new log::Writer(descriptor_file_);
Branch: 3 branches
BranchTrueFalseTotal
descriptor_log_ = new log::Writer(descriptor_file_); 3 0 3
descriptor_log_ = new log::Writer(descriptor_file_); 3 0 3
descriptor_log_ = new log::Writer(descriptor_file_); 0 0 0
650 3 ▶ s = WriteSnapshot(descriptor_log_);
Branch: 2 branches
BranchTrueFalseTotal
s = WriteSnapshot(descriptor_log_); 3 0 3
s = WriteSnapshot(descriptor_log_); 3 0 3
651 }
652 }
653  
654 // Unlock during expensive MANIFEST log write
655 {
656 3 ▶ mu->Unlock();
Branch: 1 branch
BranchTrueFalseTotal
mu->Unlock(); 3 0 3
657  
658 // Write new record to MANIFEST log
659 3 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 3 0 3
660 3 std::string record;
661 3 ▶ edit->EncodeTo(&record);
Branch: 1 branch
BranchTrueFalseTotal
edit->EncodeTo(&record); 3 0 3
662 3 ▶ s = descriptor_log_->AddRecord(record);
Branch: 2 branches
BranchTrueFalseTotal
s = descriptor_log_->AddRecord(record); 3 0 3
s = descriptor_log_->AddRecord(record); 3 0 3
663 3 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 3 0 3
664 3 ▶ s = descriptor_file_->Sync();
Branch: 2 branches
BranchTrueFalseTotal
s = descriptor_file_->Sync(); 3 0 3
s = descriptor_file_->Sync(); 3 0 3
665 }
666 3 }
667  
668 // If we just created a new descriptor file, install it by writing a
669 // new CURRENT file that points to it.
670 3 ▶ if (s.ok() && !new_manifest_file.empty()) {
Branch: 3 branches
BranchTrueFalseTotal
if (s.ok() && !new_manifest_file.empty()) { 3 0 3
if (s.ok() && !new_manifest_file.empty()) { 3 0 3
if (s.ok() && !new_manifest_file.empty()) { 3 0 3
671 3 ▶ s = SetCurrentFile(env_, dbname_, manifest_file_number_);
Branch: 2 branches
BranchTrueFalseTotal
s = SetCurrentFile(env_, dbname_, manifest_file_number_); 3 0 3
s = SetCurrentFile(env_, dbname_, manifest_file_number_); 3 0 3
672 }
673  
674 3 ▶ mu->Lock();
Branch: 1 branch
BranchTrueFalseTotal
mu->Lock(); 3 0 3
675 }
676  
677 // Install the new version
678 3 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 3 0 3
679 3 ▶ AppendVersion(v);
Branch: 1 branch
BranchTrueFalseTotal
AppendVersion(v); 3 0 3
680 3 log_number_ = edit->log_number_;
681 3 prev_log_number_ = edit->prev_log_number_;
682 } else {
683 0 ▶ delete v;
Branch: 1 branch
BranchTrueFalseTotal
delete v; 0 0 0
684 0 ▶ if (!new_manifest_file.empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!new_manifest_file.empty()) { 0 0 0
685 0 ▶ delete descriptor_log_;
Branch: 1 branch
BranchTrueFalseTotal
delete descriptor_log_; 0 0 0
686 0 ▶ delete descriptor_file_;
Branch: 1 branch
BranchTrueFalseTotal
delete descriptor_file_; 0 0 0
687 0 descriptor_log_ = NULL;
688 0 descriptor_file_ = NULL;
689 0 ▶ env_->DeleteFile(new_manifest_file);
Branch: 1 branch
BranchTrueFalseTotal
env_->DeleteFile(new_manifest_file); 0 0 0
690 }
691 }
692  
693 6 return s;
694 3 }
695  
696 3 Status VersionSet::Recover() {
697 struct LogReporter : public log::Reader::Reporter {
698 Status* status;
699 0 virtual void Corruption(size_t bytes, const Status& s) {
700 0 ▶ if (this->status->ok()) *this->status = s;
Branch: 1 branch
BranchTrueFalseTotal
if (this->status->ok()) *this->status = s; 0 0 0
701 0 }
702 };
703  
704 // Read "CURRENT" file, which contains a pointer to the current manifest file
705 3 std::string current;
706 3 ▶ Status s = ReadFileToString(env_, CurrentFileName(dbname_), &current);
Branch: 2 branches
BranchTrueFalseTotal
Status s = ReadFileToString(env_, CurrentFileName(dbname_), &current); 3 0 3
Status s = ReadFileToString(env_, CurrentFileName(dbname_), &current); 3 0 3
707 3 ▶ if (!s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s.ok()) { 0 3 3
708 0 ▶ return s;
Branch: 1 branch
BranchTrueFalseTotal
return s; 0 0 0
709 }
710 3 ▶ if (current.empty() || current[current.size()-1] != '\n') {
Branch: 4 branches
BranchTrueFalseTotal
if (current.empty() || current[current.size()-1] != '\n') { 3 0 3
if (current.empty() || current[current.size()-1] != '\n') { 3 0 3
if (current.empty() || current[current.size()-1] != '\n') { 0 3 3
if (current.empty() || current[current.size()-1] != '\n') { 0 3 3
711 0 ▶ return Status::Corruption("CURRENT file does not end with newline");
Branch: 1 branch
BranchTrueFalseTotal
return Status::Corruption("CURRENT file does not end with newline"); 0 0 0
712 }
713 3 ▶ current.resize(current.size() - 1);
Branch: 1 branch
BranchTrueFalseTotal
current.resize(current.size() - 1); 3 0 3
714  
715 3 ▶ std::string dscname = dbname_ + "/" + current;
Branch: 2 branches
BranchTrueFalseTotal
std::string dscname = dbname_ + "/" + current; 3 0 3
std::string dscname = dbname_ + "/" + current; 3 0 3
716 SequentialFile* file;
717 3 ▶ s = env_->NewSequentialFile(dscname, &file);
Branch: 2 branches
BranchTrueFalseTotal
s = env_->NewSequentialFile(dscname, &file); 3 0 3
s = env_->NewSequentialFile(dscname, &file); 3 0 3
718 3 ▶ if (!s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s.ok()) { 0 3 3
719 0 ▶ return s;
Branch: 1 branch
BranchTrueFalseTotal
return s; 0 0 0
720 }
721  
722 3 bool have_log_number = false;
723 3 bool have_prev_log_number = false;
724 3 bool have_next_file = false;
725 3 bool have_last_sequence = false;
726 3 uint64_t next_file = 0;
727 3 uint64_t last_sequence = 0;
728 3 uint64_t log_number = 0;
729 3 uint64_t prev_log_number = 0;
730 3 ▶ Builder builder(this, current_);
Branch: 1 branch
BranchTrueFalseTotal
Builder builder(this, current_); 3 0 3
731  
732 {
733 3 LogReporter reporter;
734 3 reporter.status = &s;
735 3 ▶ log::Reader reader(file, &reporter, true/*checksum*/, 0/*initial_offset*/);
Branch: 1 branch
BranchTrueFalseTotal
log::Reader reader(file, &reporter, true/*checksum*/, 0/*initial_offset*/); 3 0 3
736 3 Slice record;
737 3 std::string scratch;
738 7 ▶ while (reader.ReadRecord(&record, &scratch) && s.ok()) {
Branch: 4 branches
BranchTrueFalseTotal
while (reader.ReadRecord(&record, &scratch) && s.ok()) { 7 0 7
while (reader.ReadRecord(&record, &scratch) && s.ok()) { 4 3 7
while (reader.ReadRecord(&record, &scratch) && s.ok()) { 4 0 4
while (reader.ReadRecord(&record, &scratch) && s.ok()) { 4 3 7
739 4 ▶ VersionEdit edit;
Branch: 1 branch
BranchTrueFalseTotal
VersionEdit edit; 4 0 4
740 4 ▶ s = edit.DecodeFrom(record);
Branch: 2 branches
BranchTrueFalseTotal
s = edit.DecodeFrom(record); 4 0 4
s = edit.DecodeFrom(record); 4 0 4
741 4 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 4 0 4
742 7 ▶ if (edit.has_comparator_ &&
Branch: 2 branches
BranchTrueFalseTotal
if (edit.has_comparator_ && 3 1 4
if (edit.has_comparator_ && 0 4 4
743 3 ▶ edit.comparator_ != icmp_.user_comparator()->Name()) {
Branch: 3 branches
BranchTrueFalseTotal
edit.comparator_ != icmp_.user_comparator()->Name()) { 3 0 3
edit.comparator_ != icmp_.user_comparator()->Name()) { 3 0 3
edit.comparator_ != icmp_.user_comparator()->Name()) { 0 3 3
744 0 ▶ s = Status::InvalidArgument(
Branch: 2 branches
BranchTrueFalseTotal
s = Status::InvalidArgument( 0 0 0
s = Status::InvalidArgument( 0 0 0
745 0 ▶ edit.comparator_ + "does not match existing comparator ",
Branch: 1 branch
BranchTrueFalseTotal
edit.comparator_ + "does not match existing comparator ", 0 0 0
746 0 ▶ icmp_.user_comparator()->Name());
Branch: 1 branch
BranchTrueFalseTotal
icmp_.user_comparator()->Name()); 0 0 0
747 }
748 }
749  
750 4 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 4 0 4
751 4 ▶ builder.Apply(&edit);
Branch: 1 branch
BranchTrueFalseTotal
builder.Apply(&edit); 4 0 4
752 }
753  
754 4 ▶ if (edit.has_log_number_) {
Branch: 1 branch
BranchTrueFalseTotal
if (edit.has_log_number_) { 3 1 4
755 3 log_number = edit.log_number_;
756 3 have_log_number = true;
757 }
758  
759 4 ▶ if (edit.has_prev_log_number_) {
Branch: 1 branch
BranchTrueFalseTotal
if (edit.has_prev_log_number_) { 1 3 4
760 1 prev_log_number = edit.prev_log_number_;
761 1 have_prev_log_number = true;
762 }
763  
764 4 ▶ if (edit.has_next_file_number_) {
Branch: 1 branch
BranchTrueFalseTotal
if (edit.has_next_file_number_) { 3 1 4
765 3 next_file = edit.next_file_number_;
766 3 have_next_file = true;
767 }
768  
769 4 ▶ if (edit.has_last_sequence_) {
Branch: 1 branch
BranchTrueFalseTotal
if (edit.has_last_sequence_) { 3 1 4
770 3 last_sequence = edit.last_sequence_;
771 3 have_last_sequence = true;
772 }
773 4 }
774 3 }
775 3 ▶ delete file;
Branch: 1 branch
BranchTrueFalseTotal
delete file; 3 0 3
776 3 file = NULL;
777  
778 3 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 3 0 3
779 3 ▶ if (!have_next_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (!have_next_file) { 0 3 3
780 0 ▶ s = Status::Corruption("no meta-nextfile entry in descriptor");
Branch: 2 branches
BranchTrueFalseTotal
s = Status::Corruption("no meta-nextfile entry in descriptor"); 0 0 0
s = Status::Corruption("no meta-nextfile entry in descriptor"); 0 0 0
781 3 ▶ } else if (!have_log_number) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!have_log_number) { 0 3 3
782 0 ▶ s = Status::Corruption("no meta-lognumber entry in descriptor");
Branch: 2 branches
BranchTrueFalseTotal
s = Status::Corruption("no meta-lognumber entry in descriptor"); 0 0 0
s = Status::Corruption("no meta-lognumber entry in descriptor"); 0 0 0
783 3 ▶ } else if (!have_last_sequence) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!have_last_sequence) { 0 3 3
784 0 ▶ s = Status::Corruption("no last-sequence-number entry in descriptor");
Branch: 2 branches
BranchTrueFalseTotal
s = Status::Corruption("no last-sequence-number entry in descriptor"); 0 0 0
s = Status::Corruption("no last-sequence-number entry in descriptor"); 0 0 0
785 }
786  
787 3 ▶ if (!have_prev_log_number) {
Branch: 1 branch
BranchTrueFalseTotal
if (!have_prev_log_number) { 2 1 3
788 2 prev_log_number = 0;
789 }
790  
791 3 ▶ MarkFileNumberUsed(prev_log_number);
Branch: 1 branch
BranchTrueFalseTotal
MarkFileNumberUsed(prev_log_number); 3 0 3
792 3 ▶ MarkFileNumberUsed(log_number);
Branch: 1 branch
BranchTrueFalseTotal
MarkFileNumberUsed(log_number); 3 0 3
793 }
794  
795 3 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 3 0 3
796 3 ▶ Version* v = new Version(this);
Branch: 1 branch
BranchTrueFalseTotal
Version* v = new Version(this); 3 0 3
797 3 ▶ builder.SaveTo(v);
Branch: 1 branch
BranchTrueFalseTotal
builder.SaveTo(v); 3 0 3
798 // Install recovered version
799 3 ▶ Finalize(v);
Branch: 1 branch
BranchTrueFalseTotal
Finalize(v); 3 0 3
800 3 ▶ AppendVersion(v);
Branch: 1 branch
BranchTrueFalseTotal
AppendVersion(v); 3 0 3
801 3 manifest_file_number_ = next_file;
802 3 next_file_number_ = next_file + 1;
803 3 last_sequence_ = last_sequence;
804 3 log_number_ = log_number;
805 3 prev_log_number_ = prev_log_number;
806 }
807  
808 3 ▶ return s;
Branch: 1 branch
BranchTrueFalseTotal
return s; 3 0 3
809 3 }
810  
811 7 void VersionSet::MarkFileNumberUsed(uint64_t number) {
812 7 ▶ if (next_file_number_ <= number) {
Branch: 1 branch
BranchTrueFalseTotal
if (next_file_number_ <= number) { 1 6 7
813 1 next_file_number_ = number + 1;
814 }
815 7 }
816  
817 30 static int64_t TotalFileSize(const std::vector<FileMetaData*>& files) {
818 30 int64_t sum = 0;
819 30 ▶ for (size_t i = 0; i < files.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < files.size(); i++) { 0 30 30
820 0 sum += files[i]->file_size;
821 }
822 30 return sum;
823 }
824  
825 6 void VersionSet::Finalize(Version* v) {
826 // Precomputed best level for next compaction
827 6 int best_level = -1;
828 6 double best_score = -1;
829  
830 42 ▶ for (int level = 0; level < config::kNumLevels-1; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 0; level < config::kNumLevels-1; level++) { 36 6 42
831 double score;
832 36 ▶ if (level == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (level == 0) { 6 30 36
833 // We treat level-0 specially by bounding the number of files
834 // instead of number of bytes for two reasons:
835 //
836 // (1) With larger write-buffer sizes, it is nice not to do too
837 // many level-0 compactions.
838 //
839 // (2) The files in level-0 are merged on every read and
840 // therefore we wish to avoid too many files when the individual
841 // file size is small (perhaps because of a small write-buffer
842 // setting, or very high compression ratios, or lots of
843 // overwrites/deletions).
844 6 score = v->files_[level].size() /
845 static_cast<double>(config::kL0_CompactionTrigger);
846 } else {
847 // Compute the ratio of current size to size limit.
848 30 const uint64_t level_bytes = TotalFileSize(v->files_[level]);
849 30 score = static_cast<double>(level_bytes) / MaxBytesForLevel(level);
850 }
851  
852 36 ▶ if (score > best_score) {
Branch: 1 branch
BranchTrueFalseTotal
if (score > best_score) { 6 30 36
853 6 best_level = level;
854 6 best_score = score;
855 }
856 }
857  
858 6 v->compaction_level_ = best_level;
859 6 v->compaction_score_ = best_score;
860 6 }
861  
862 3 Status VersionSet::WriteSnapshot(log::Writer* log) {
863 // TODO: Break up into multiple records to reduce memory usage on recovery?
864  
865 // Save metadata
866 3 ▶ VersionEdit edit;
Branch: 1 branch
BranchTrueFalseTotal
VersionEdit edit; 3 0 3
867 3 ▶ edit.SetComparatorName(icmp_.user_comparator()->Name());
Branch: 2 branches
BranchTrueFalseTotal
edit.SetComparatorName(icmp_.user_comparator()->Name()); 3 0 3
edit.SetComparatorName(icmp_.user_comparator()->Name()); 3 0 3
868  
869 // Save compaction pointers
870 24 ▶ for (int level = 0; level < config::kNumLevels; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 0; level < config::kNumLevels; level++) { 21 3 24
871 21 ▶ if (!compact_pointer_[level].empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!compact_pointer_[level].empty()) { 0 21 21
872 0 InternalKey key;
873 0 ▶ key.DecodeFrom(compact_pointer_[level]);
Branch: 1 branch
BranchTrueFalseTotal
key.DecodeFrom(compact_pointer_[level]); 0 0 0
874 0 ▶ edit.SetCompactPointer(level, key);
Branch: 1 branch
BranchTrueFalseTotal
edit.SetCompactPointer(level, key); 0 0 0
875 0 }
876 }
877  
878 // Save files
879 24 ▶ for (int level = 0; level < config::kNumLevels; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 0; level < config::kNumLevels; level++) { 21 3 24
880 21 const std::vector<FileMetaData*>& files = current_->files_[level];
881 21 ▶ for (size_t i = 0; i < files.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < files.size(); i++) { 0 21 21
882 0 const FileMetaData* f = files[i];
883 0 ▶ edit.AddFile(level, f->number, f->file_size, f->smallest, f->largest);
Branch: 1 branch
BranchTrueFalseTotal
edit.AddFile(level, f->number, f->file_size, f->smallest, f->largest); 0 0 0
884 }
885 }
886  
887 3 std::string record;
888 3 ▶ edit.EncodeTo(&record);
Branch: 1 branch
BranchTrueFalseTotal
edit.EncodeTo(&record); 3 0 3
889 6 ▶ return log->AddRecord(record);
Branch: 1 branch
BranchTrueFalseTotal
return log->AddRecord(record); 3 0 3
890 3 }
891  
892 18 int VersionSet::NumLevelFiles(int level) const {
893 18 ▶ assert(level >= 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(level >= 0); 0 18 18
894 18 ▶ assert(level < config::kNumLevels);
Branch: 1 branch
BranchTrueFalseTotal
assert(level < config::kNumLevels); 0 18 18
895 18 return current_->files_[level].size();
896 }
897  
898 0 const char* VersionSet::LevelSummary(LevelSummaryStorage* scratch) const {
899 // Update code if kNumLevels changes
900 assert(config::kNumLevels == 7);
901 0 snprintf(scratch->buffer, sizeof(scratch->buffer),
902 "files[ %d %d %d %d %d %d %d ]",
903 0 int(current_->files_[0].size()),
904 0 int(current_->files_[1].size()),
905 0 int(current_->files_[2].size()),
906 0 int(current_->files_[3].size()),
907 0 int(current_->files_[4].size()),
908 0 int(current_->files_[5].size()),
909 0 int(current_->files_[6].size()));
910 0 return scratch->buffer;
911 }
912  
913 0 uint64_t VersionSet::ApproximateOffsetOf(Version* v, const InternalKey& ikey) {
914 0 uint64_t result = 0;
915 0 ▶ for (int level = 0; level < config::kNumLevels; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 0; level < config::kNumLevels; level++) { 0 0 0
916 0 const std::vector<FileMetaData*>& files = v->files_[level];
917 0 ▶ for (size_t i = 0; i < files.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < files.size(); i++) { 0 0 0
918 0 ▶ if (icmp_.Compare(files[i]->largest, ikey) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (icmp_.Compare(files[i]->largest, ikey) <= 0) { 0 0 0
919 // Entire file is before "ikey", so just add the file size
920 0 result += files[i]->file_size;
921 0 ▶ } else if (icmp_.Compare(files[i]->smallest, ikey) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (icmp_.Compare(files[i]->smallest, ikey) > 0) { 0 0 0
922 // Entire file is after "ikey", so ignore
923 0 ▶ if (level > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (level > 0) { 0 0 0
924 // Files other than level 0 are sorted by meta->smallest, so
925 // no further files in this level will contain data for
926 // "ikey".
927 0 break;
928 }
929 } else {
930 // "ikey" falls in the range for this table. Add the
931 // approximate offset of "ikey" within the table.
932 Table* tableptr;
933 0 ▶ Iterator* iter = table_cache_->NewIterator(
Branch: 1 branch
BranchTrueFalseTotal
Iterator* iter = table_cache_->NewIterator( 0 0 0
934 0 ReadOptions(), files[i]->number, files[i]->file_size, &tableptr);
935 0 ▶ if (tableptr != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tableptr != NULL) { 0 0 0
936 0 ▶ result += tableptr->ApproximateOffsetOf(ikey.Encode());
Branch: 1 branch
BranchTrueFalseTotal
result += tableptr->ApproximateOffsetOf(ikey.Encode()); 0 0 0
937 }
938 0 ▶ delete iter;
Branch: 1 branch
BranchTrueFalseTotal
delete iter; 0 0 0
939 }
940 }
941 }
942 0 return result;
943 }
944  
945 3 void VersionSet::AddLiveFiles(std::set<uint64_t>* live) {
946 3 for (Version* v = dummy_versions_.next_;
947 6 ▶ v != &dummy_versions_;
Branch: 1 branch
BranchTrueFalseTotal
v != &dummy_versions_; 3 3 6
948 3 v = v->next_) {
949 24 ▶ for (int level = 0; level < config::kNumLevels; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 0; level < config::kNumLevels; level++) { 21 3 24
950 21 const std::vector<FileMetaData*>& files = v->files_[level];
951 22 ▶ for (size_t i = 0; i < files.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < files.size(); i++) { 1 21 22
952 1 live->insert(files[i]->number);
953 }
954 }
955 }
956 3 }
957  
958 0 int64_t VersionSet::NumLevelBytes(int level) const {
959 0 ▶ assert(level >= 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(level >= 0); 0 0 0
960 0 ▶ assert(level < config::kNumLevels);
Branch: 1 branch
BranchTrueFalseTotal
assert(level < config::kNumLevels); 0 0 0
961 0 return TotalFileSize(current_->files_[level]);
962 }
963  
964 0 int64_t VersionSet::MaxNextLevelOverlappingBytes() {
965 0 int64_t result = 0;
966 0 std::vector<FileMetaData*> overlaps;
967 0 ▶ for (int level = 1; level < config::kNumLevels - 1; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 1; level < config::kNumLevels - 1; level++) { 0 0 0
968 0 ▶ for (size_t i = 0; i < current_->files_[level].size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < current_->files_[level].size(); i++) { 0 0 0
969 0 const FileMetaData* f = current_->files_[level][i];
970 0 ▶ GetOverlappingInputs(level+1, f->smallest, f->largest, &overlaps);
Branch: 1 branch
BranchTrueFalseTotal
GetOverlappingInputs(level+1, f->smallest, f->largest, &overlaps); 0 0 0
971 0 const int64_t sum = TotalFileSize(overlaps);
972 0 ▶ if (sum > result) {
Branch: 1 branch
BranchTrueFalseTotal
if (sum > result) { 0 0 0
973 0 result = sum;
974 }
975 }
976 }
977 0 return result;
978 0 }
979  
980 // Store in "*inputs" all files in "level" that overlap [begin,end]
981 0 void VersionSet::GetOverlappingInputs(
982 int level,
983 const InternalKey& begin,
984 const InternalKey& end,
985 std::vector<FileMetaData*>* inputs) {
986 0 inputs->clear();
987 0 Slice user_begin = begin.user_key();
988 0 Slice user_end = end.user_key();
989 0 const Comparator* user_cmp = icmp_.user_comparator();
990 0 ▶ for (size_t i = 0; i < current_->files_[level].size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < current_->files_[level].size(); i++) { 0 0 0
991 0 FileMetaData* f = current_->files_[level][i];
992 0 ▶ if (user_cmp->Compare(f->largest.user_key(), user_begin) < 0 ||
Branch: 2 branches
BranchTrueFalseTotal
if (user_cmp->Compare(f->largest.user_key(), user_begin) < 0 || 0 0 0
if (user_cmp->Compare(f->largest.user_key(), user_begin) < 0 || 0 0 0
993 0 ▶ user_cmp->Compare(f->smallest.user_key(), user_end) > 0) {
Branch: 3 branches
BranchTrueFalseTotal
user_cmp->Compare(f->smallest.user_key(), user_end) > 0) { 0 0 0
user_cmp->Compare(f->smallest.user_key(), user_end) > 0) { 0 0 0
user_cmp->Compare(f->smallest.user_key(), user_end) > 0) { 0 0 0
994 // Either completely before or after range; skip it
995 } else {
996 0 ▶ inputs->push_back(f);
Branch: 1 branch
BranchTrueFalseTotal
inputs->push_back(f); 0 0 0
997 }
998 }
999 0 }
1000  
1001 // Stores the minimal range that covers all entries in inputs in
1002 // *smallest, *largest.
1003 // REQUIRES: inputs is not empty
1004 0 void VersionSet::GetRange(const std::vector<FileMetaData*>& inputs,
1005 InternalKey* smallest,
1006 InternalKey* largest) {
1007 0 ▶ assert(!inputs.empty());
Branch: 1 branch
BranchTrueFalseTotal
assert(!inputs.empty()); 0 0 0
1008 0 smallest->Clear();
1009 0 largest->Clear();
1010 0 ▶ for (size_t i = 0; i < inputs.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < inputs.size(); i++) { 0 0 0
1011 0 FileMetaData* f = inputs[i];
1012 0 ▶ if (i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) { 0 0 0
1013 0 *smallest = f->smallest;
1014 0 *largest = f->largest;
1015 } else {
1016 0 ▶ if (icmp_.Compare(f->smallest, *smallest) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (icmp_.Compare(f->smallest, *smallest) < 0) { 0 0 0
1017 0 *smallest = f->smallest;
1018 }
1019 0 ▶ if (icmp_.Compare(f->largest, *largest) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (icmp_.Compare(f->largest, *largest) > 0) { 0 0 0
1020 0 *largest = f->largest;
1021 }
1022 }
1023 }
1024 0 }
1025  
1026 // Stores the minimal range that covers all entries in inputs1 and inputs2
1027 // in *smallest, *largest.
1028 // REQUIRES: inputs is not empty
1029 0 void VersionSet::GetRange2(const std::vector<FileMetaData*>& inputs1,
1030 const std::vector<FileMetaData*>& inputs2,
1031 InternalKey* smallest,
1032 InternalKey* largest) {
1033 0 ▶ std::vector<FileMetaData*> all = inputs1;
Branch: 1 branch
BranchTrueFalseTotal
std::vector<FileMetaData*> all = inputs1; 0 0 0
1034 0 ▶ all.insert(all.end(), inputs2.begin(), inputs2.end());
Branch: 1 branch
BranchTrueFalseTotal
all.insert(all.end(), inputs2.begin(), inputs2.end()); 0 0 0
1035 0 ▶ GetRange(all, smallest, largest);
Branch: 1 branch
BranchTrueFalseTotal
GetRange(all, smallest, largest); 0 0 0
1036 0 }
1037  
1038 0 Iterator* VersionSet::MakeInputIterator(Compaction* c) {
1039 0 ReadOptions options;
1040 0 options.verify_checksums = options_->paranoid_checks;
1041 0 options.fill_cache = false;
1042  
1043 // Level-0 files have to be merged together. For other levels,
1044 // we will make a concatenating iterator per level.
1045 // TODO(opt): use concatenating iterator for level-0 if there is no overlap
1046 0 ▶ const int space = (c->level() == 0 ? c->inputs_[0].size() + 1 : 2);
Branch: 1 branch
BranchTrueFalseTotal
const int space = (c->level() == 0 ? c->inputs_[0].size() + 1 : 2); 0 0 0
1047 0 ▶ Iterator** list = new Iterator*[space];
Branch: 2 branches
BranchTrueFalseTotal
Iterator** list = new Iterator*[space]; 0 0 0
Iterator** list = new Iterator*[space]; 0 0 0
1048 0 int num = 0;
1049 0 ▶ for (int which = 0; which < 2; which++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int which = 0; which < 2; which++) { 0 0 0
1050 0 ▶ if (!c->inputs_[which].empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->inputs_[which].empty()) { 0 0 0
1051 0 ▶ if (c->level() + which == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->level() + which == 0) { 0 0 0
1052 0 const std::vector<FileMetaData*>& files = c->inputs_[which];
1053 0 ▶ for (size_t i = 0; i < files.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < files.size(); i++) { 0 0 0
1054 0 ▶ list[num++] = table_cache_->NewIterator(
Branch: 1 branch
BranchTrueFalseTotal
list[num++] = table_cache_->NewIterator( 0 0 0
1055 0 options, files[i]->number, files[i]->file_size);
1056 }
1057 } else {
1058 // Create concatenating iterator for the files from this level
1059 0 ▶ list[num++] = NewTwoLevelIterator(
Branch: 2 branches
BranchTrueFalseTotal
list[num++] = NewTwoLevelIterator( 0 0 0
list[num++] = NewTwoLevelIterator( 0 0 0
1060 0 ▶ new Version::LevelFileNumIterator(icmp_, &c->inputs_[which]),
Branch: 1 branch
BranchTrueFalseTotal
new Version::LevelFileNumIterator(icmp_, &c->inputs_[which]), 0 0 0
1061 0 ▶ &GetFileIterator, table_cache_, options);
Branch: 1 branch
BranchTrueFalseTotal
&GetFileIterator, table_cache_, options); 0 0 0
1062 }
1063 }
1064 }
1065 0 ▶ assert(num <= space);
Branch: 1 branch
BranchTrueFalseTotal
assert(num <= space); 0 0 0
1066 0 ▶ Iterator* result = NewMergingIterator(&icmp_, list, num);
Branch: 1 branch
BranchTrueFalseTotal
Iterator* result = NewMergingIterator(&icmp_, list, num); 0 0 0
1067 0 ▶ delete[] list;
Branch: 1 branch
BranchTrueFalseTotal
delete[] list; 0 0 0
1068 0 return result;
1069 }
1070  
1071 0 Compaction* VersionSet::PickCompaction() {
1072 Compaction* c;
1073 int level;
1074  
1075 // We prefer compactions triggered by too much data in a level over
1076 // the compactions triggered by seeks.
1077 0 const bool size_compaction = (current_->compaction_score_ >= 1);
1078 0 const bool seek_compaction = (current_->file_to_compact_ != NULL);
1079 0 ▶ if (size_compaction) {
Branch: 1 branch
BranchTrueFalseTotal
if (size_compaction) { 0 0 0
1080 0 level = current_->compaction_level_;
1081 0 ▶ assert(level >= 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(level >= 0); 0 0 0
1082 0 ▶ assert(level+1 < config::kNumLevels);
Branch: 1 branch
BranchTrueFalseTotal
assert(level+1 < config::kNumLevels); 0 0 0
1083 0 ▶ c = new Compaction(level);
Branch: 2 branches
BranchTrueFalseTotal
c = new Compaction(level); 0 0 0
c = new Compaction(level); 0 0 0
1084  
1085 // Pick the first file that comes after compact_pointer_[level]
1086 0 ▶ for (size_t i = 0; i < current_->files_[level].size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < current_->files_[level].size(); i++) { 0 0 0
1087 0 FileMetaData* f = current_->files_[level][i];
1088 0 ▶ if (compact_pointer_[level].empty() ||
Branch: 1 branch
BranchTrueFalseTotal
if (compact_pointer_[level].empty() || 0 0 0
1089 0 ▶ icmp_.Compare(f->largest.Encode(), compact_pointer_[level]) > 0) {
Branch: 3 branches
BranchTrueFalseTotal
icmp_.Compare(f->largest.Encode(), compact_pointer_[level]) > 0) { 0 0 0
icmp_.Compare(f->largest.Encode(), compact_pointer_[level]) > 0) { 0 0 0
icmp_.Compare(f->largest.Encode(), compact_pointer_[level]) > 0) { 0 0 0
1090 0 ▶ c->inputs_[0].push_back(f);
Branch: 1 branch
BranchTrueFalseTotal
c->inputs_[0].push_back(f); 0 0 0
1091 0 break;
1092 }
1093 }
1094 0 ▶ if (c->inputs_[0].empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->inputs_[0].empty()) { 0 0 0
1095 // Wrap-around to the beginning of the key space
1096 0 c->inputs_[0].push_back(current_->files_[level][0]);
1097 }
1098 0 ▶ } else if (seek_compaction) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (seek_compaction) { 0 0 0
1099 0 level = current_->file_to_compact_level_;
1100 0 ▶ c = new Compaction(level);
Branch: 2 branches
BranchTrueFalseTotal
c = new Compaction(level); 0 0 0
c = new Compaction(level); 0 0 0
1101 0 c->inputs_[0].push_back(current_->file_to_compact_);
1102 } else {
1103 0 return NULL;
1104 }
1105  
1106 0 c->input_version_ = current_;
1107 0 c->input_version_->Ref();
1108  
1109 // Files in level 0 may overlap each other, so pick up all overlapping ones
1110 0 ▶ if (level == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (level == 0) { 0 0 0
1111 0 InternalKey smallest, largest;
1112 0 ▶ GetRange(c->inputs_[0], &smallest, &largest);
Branch: 1 branch
BranchTrueFalseTotal
GetRange(c->inputs_[0], &smallest, &largest); 0 0 0
1113 // Note that the next call will discard the file we placed in
1114 // c->inputs_[0] earlier and replace it with an overlapping set
1115 // which will include the picked file.
1116 0 ▶ GetOverlappingInputs(0, smallest, largest, &c->inputs_[0]);
Branch: 1 branch
BranchTrueFalseTotal
GetOverlappingInputs(0, smallest, largest, &c->inputs_[0]); 0 0 0
1117 0 ▶ assert(!c->inputs_[0].empty());
Branch: 1 branch
BranchTrueFalseTotal
assert(!c->inputs_[0].empty()); 0 0 0
1118 0 }
1119  
1120 0 SetupOtherInputs(c);
1121  
1122 0 return c;
1123 }
1124  
1125 0 void VersionSet::SetupOtherInputs(Compaction* c) {
1126 0 const int level = c->level();
1127 0 InternalKey smallest, largest;
1128 0 ▶ GetRange(c->inputs_[0], &smallest, &largest);
Branch: 1 branch
BranchTrueFalseTotal
GetRange(c->inputs_[0], &smallest, &largest); 0 0 0
1129  
1130 0 ▶ GetOverlappingInputs(level+1, smallest, largest, &c->inputs_[1]);
Branch: 1 branch
BranchTrueFalseTotal
GetOverlappingInputs(level+1, smallest, largest, &c->inputs_[1]); 0 0 0
1131  
1132 // Get entire range covered by compaction
1133 0 InternalKey all_start, all_limit;
1134 0 ▶ GetRange2(c->inputs_[0], c->inputs_[1], &all_start, &all_limit);
Branch: 1 branch
BranchTrueFalseTotal
GetRange2(c->inputs_[0], c->inputs_[1], &all_start, &all_limit); 0 0 0
1135  
1136 // See if we can grow the number of inputs in "level" without
1137 // changing the number of "level+1" files we pick up.
1138 0 ▶ if (!c->inputs_[1].empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->inputs_[1].empty()) { 0 0 0
1139 0 std::vector<FileMetaData*> expanded0;
1140 0 ▶ GetOverlappingInputs(level, all_start, all_limit, &expanded0);
Branch: 1 branch
BranchTrueFalseTotal
GetOverlappingInputs(level, all_start, all_limit, &expanded0); 0 0 0
1141 0 ▶ if (expanded0.size() > c->inputs_[0].size()) {
Branch: 1 branch
BranchTrueFalseTotal
if (expanded0.size() > c->inputs_[0].size()) { 0 0 0
1142 0 InternalKey new_start, new_limit;
1143 0 ▶ GetRange(expanded0, &new_start, &new_limit);
Branch: 1 branch
BranchTrueFalseTotal
GetRange(expanded0, &new_start, &new_limit); 0 0 0
1144 0 std::vector<FileMetaData*> expanded1;
1145 0 ▶ GetOverlappingInputs(level+1, new_start, new_limit, &expanded1);
Branch: 1 branch
BranchTrueFalseTotal
GetOverlappingInputs(level+1, new_start, new_limit, &expanded1); 0 0 0
1146 0 ▶ if (expanded1.size() == c->inputs_[1].size()) {
Branch: 1 branch
BranchTrueFalseTotal
if (expanded1.size() == c->inputs_[1].size()) { 0 0 0
1147 0 ▶ Log(options_->info_log,
Branch: 1 branch
BranchTrueFalseTotal
Log(options_->info_log, 0 0 0
1148 "Expanding@%d %d+%d to %d+%d\n",
1149 level,
1150 0 int(c->inputs_[0].size()),
1151 0 int(c->inputs_[1].size()),
1152 0 int(expanded0.size()),
1153 0 int(expanded1.size()));
1154 0 ▶ smallest = new_start;
Branch: 1 branch
BranchTrueFalseTotal
smallest = new_start; 0 0 0
1155 0 ▶ largest = new_limit;
Branch: 1 branch
BranchTrueFalseTotal
largest = new_limit; 0 0 0
1156 0 ▶ c->inputs_[0] = expanded0;
Branch: 1 branch
BranchTrueFalseTotal
c->inputs_[0] = expanded0; 0 0 0
1157 0 ▶ c->inputs_[1] = expanded1;
Branch: 1 branch
BranchTrueFalseTotal
c->inputs_[1] = expanded1; 0 0 0
1158 0 ▶ GetRange2(c->inputs_[0], c->inputs_[1], &all_start, &all_limit);
Branch: 1 branch
BranchTrueFalseTotal
GetRange2(c->inputs_[0], c->inputs_[1], &all_start, &all_limit); 0 0 0
1159 }
1160 0 }
1161 0 }
1162  
1163 // Compute the set of grandparent files that overlap this compaction
1164 // (parent == level+1; grandparent == level+2)
1165 0 ▶ if (level + 2 < config::kNumLevels) {
Branch: 1 branch
BranchTrueFalseTotal
if (level + 2 < config::kNumLevels) { 0 0 0
1166 0 ▶ GetOverlappingInputs(level + 2, all_start, all_limit, &c->grandparents_);
Branch: 1 branch
BranchTrueFalseTotal
GetOverlappingInputs(level + 2, all_start, all_limit, &c->grandparents_); 0 0 0
1167 }
1168  
1169 if (false) {
1170 Log(options_->info_log, "Compacting %d '%s' .. '%s'",
1171 level,
1172 EscapeString(smallest.Encode()).c_str(),
1173 EscapeString(largest.Encode()).c_str());
1174 }
1175  
1176 // Update the place where we will do the next compaction for this level.
1177 // We update this immediately instead of waiting for the VersionEdit
1178 // to be applied so that if the compaction fails, we will try a different
1179 // key range next time.
1180 0 ▶ compact_pointer_[level] = largest.Encode().ToString();
Branch: 1 branch
BranchTrueFalseTotal
compact_pointer_[level] = largest.Encode().ToString(); 0 0 0
1181 0 ▶ c->edit_.SetCompactPointer(level, largest);
Branch: 1 branch
BranchTrueFalseTotal
c->edit_.SetCompactPointer(level, largest); 0 0 0
1182 0 }
1183  
1184 0 Compaction* VersionSet::CompactRange(
1185 int level,
1186 const InternalKey& begin,
1187 const InternalKey& end) {
1188 0 std::vector<FileMetaData*> inputs;
1189 0 ▶ GetOverlappingInputs(level, begin, end, &inputs);
Branch: 1 branch
BranchTrueFalseTotal
GetOverlappingInputs(level, begin, end, &inputs); 0 0 0
1190 0 ▶ if (inputs.empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (inputs.empty()) { 0 0 0
1191 0 return NULL;
1192 }
1193  
1194 0 ▶ Compaction* c = new Compaction(level);
Branch: 3 branches
BranchTrueFalseTotal
Compaction* c = new Compaction(level); 0 0 0
Compaction* c = new Compaction(level); 0 0 0
Compaction* c = new Compaction(level); 0 0 0
1195 0 c->input_version_ = current_;
1196 0 ▶ c->input_version_->Ref();
Branch: 1 branch
BranchTrueFalseTotal
c->input_version_->Ref(); 0 0 0
1197 0 ▶ c->inputs_[0] = inputs;
Branch: 1 branch
BranchTrueFalseTotal
c->inputs_[0] = inputs; 0 0 0
1198 0 ▶ SetupOtherInputs(c);
Branch: 1 branch
BranchTrueFalseTotal
SetupOtherInputs(c); 0 0 0
1199 0 return c;
1200 0 }
1201  
1202 0 Compaction::Compaction(int level)
1203 0 : level_(level),
1204 0 max_output_file_size_(MaxFileSizeForLevel(level)),
1205 0 input_version_(NULL),
1206 0 grandparent_index_(0),
1207 0 seen_key_(false),
1208 0 ▶ overlapped_bytes_(0) {
Branch: 1 branch
BranchTrueFalseTotal
overlapped_bytes_(0) { 0 0 0
1209 0 ▶ for (int i = 0; i < config::kNumLevels; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < config::kNumLevels; i++) { 0 0 0
1210 0 level_ptrs_[i] = 0;
1211 }
1212 0 }
1213  
1214 0 ▶Compaction::~Compaction() {
Branch: 1 branch
BranchTrueFalseTotal
Compaction::~Compaction() { 0 0 0
1215 0 ▶ if (input_version_ != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (input_version_ != NULL) { 0 0 0
1216 0 input_version_->Unref();
1217 }
1218 0 ▶}
Branch: 1 branch
BranchTrueFalseTotal
} 0 0 0
1219  
1220 0 bool Compaction::IsTrivialMove() const {
1221 // Avoid a move if there is lots of overlapping grandparent data.
1222 // Otherwise, the move could create a parent file that will require
1223 // a very expensive merge later on.
1224 0 ▶ return (num_input_files(0) == 1 &&
Branch: 1 branch
BranchTrueFalseTotal
return (num_input_files(0) == 1 && 0 0 0
1225 0 ▶ num_input_files(1) == 0 &&
Branch: 2 branches
BranchTrueFalseTotal
num_input_files(1) == 0 && 0 0 0
num_input_files(1) == 0 && 0 0 0
1226 0 TotalFileSize(grandparents_) <= kMaxGrandParentOverlapBytes);
1227 }
1228  
1229 0 void Compaction::AddInputDeletions(VersionEdit* edit) {
1230 0 ▶ for (int which = 0; which < 2; which++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int which = 0; which < 2; which++) { 0 0 0
1231 0 ▶ for (size_t i = 0; i < inputs_[which].size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < inputs_[which].size(); i++) { 0 0 0
1232 0 edit->DeleteFile(level_ + which, inputs_[which][i]->number);
1233 }
1234 }
1235 0 }
1236  
1237 0 bool Compaction::IsBaseLevelForKey(const Slice& user_key) {
1238 // Maybe use binary search to find right entry instead of linear search?
1239 0 const Comparator* user_cmp = input_version_->vset_->icmp_.user_comparator();
1240 0 ▶ for (int lvl = level_ + 2; lvl < config::kNumLevels; lvl++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int lvl = level_ + 2; lvl < config::kNumLevels; lvl++) { 0 0 0
1241 0 const std::vector<FileMetaData*>& files = input_version_->files_[lvl];
1242 0 ▶ for (; level_ptrs_[lvl] < files.size(); ) {
Branch: 1 branch
BranchTrueFalseTotal
for (; level_ptrs_[lvl] < files.size(); ) { 0 0 0
1243 0 FileMetaData* f = files[level_ptrs_[lvl]];
1244 0 ▶ if (user_cmp->Compare(user_key, f->largest.user_key()) <= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (user_cmp->Compare(user_key, f->largest.user_key()) <= 0) { 0 0 0
if (user_cmp->Compare(user_key, f->largest.user_key()) <= 0) { 0 0 0
1245 // We've advanced far enough
1246 0 ▶ if (user_cmp->Compare(user_key, f->smallest.user_key()) >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (user_cmp->Compare(user_key, f->smallest.user_key()) >= 0) { 0 0 0
if (user_cmp->Compare(user_key, f->smallest.user_key()) >= 0) { 0 0 0
1247 // Key falls in this file's range, so definitely not base level
1248 0 return false;
1249 }
1250 0 break;
1251 }
1252 0 level_ptrs_[lvl]++;
1253 }
1254 }
1255 0 return true;
1256 }
1257  
1258 0 bool Compaction::ShouldStopBefore(const Slice& internal_key) {
1259 // Scan to find earliest grandparent file that contains key.
1260 0 const InternalKeyComparator* icmp = &input_version_->vset_->icmp_;
1261 0 ▶ while (grandparent_index_ < grandparents_.size() &&
Branch: 1 branch
BranchTrueFalseTotal
while (grandparent_index_ < grandparents_.size() && 0 0 0
1262 0 ▶ icmp->Compare(internal_key,
Branch: 2 branches
BranchTrueFalseTotal
icmp->Compare(internal_key, 0 0 0
icmp->Compare(internal_key, 0 0 0
1263 0 ▶ grandparents_[grandparent_index_]->largest.Encode()) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
grandparents_[grandparent_index_]->largest.Encode()) > 0) { 0 0 0
1264 0 ▶ if (seen_key_) {
Branch: 1 branch
BranchTrueFalseTotal
if (seen_key_) { 0 0 0
1265 0 overlapped_bytes_ += grandparents_[grandparent_index_]->file_size;
1266 }
1267 0 grandparent_index_++;
1268 }
1269 0 seen_key_ = true;
1270  
1271 0 ▶ if (overlapped_bytes_ > kMaxGrandParentOverlapBytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (overlapped_bytes_ > kMaxGrandParentOverlapBytes) { 0 0 0
1272 // Too much overlap for current output; start new output
1273 0 overlapped_bytes_ = 0;
1274 0 return true;
1275 } else {
1276 0 return false;
1277 }
1278 }
1279  
1280 0 void Compaction::ReleaseInputs() {
1281 0 ▶ if (input_version_ != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (input_version_ != NULL) { 0 0 0
1282 0 input_version_->Unref();
1283 0 input_version_ = NULL;
1284 }
1285 0 }
1286  
1287 }
« db/version_edit.cc ↑ Summary db/db_impl.cc »