db/db_impl.cc

filter: Statementstmt 47.4% 401 / 846 Branchbran 23.5% 277 / 1174 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 33.5% 678 / 2020 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« db/version_set.cc ↑ Summary db/log_reader.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/db_impl.h"
6  
7 #include <algorithm>
8 #include <set>
9 #include <string>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <vector>
13 #include "db/builder.h"
14 #include "db/db_iter.h"
15 #include "db/dbformat.h"
16 #include "db/filename.h"
17 #include "db/log_reader.h"
18 #include "db/log_writer.h"
19 #include "db/memtable.h"
20 #include "db/table_cache.h"
21 #include "db/version_set.h"
22 #include "db/write_batch_internal.h"
23 #include "leveldb/db.h"
24 #include "leveldb/env.h"
25 #include "leveldb/status.h"
26 #include "leveldb/table.h"
27 #include "leveldb/table_builder.h"
28 #include "port/port.h"
29 #include "table/block.h"
30 #include "table/merger.h"
31 #include "table/two_level_iterator.h"
32 #include "util/coding.h"
33 #include "util/logging.h"
34 #include "util/mutexlock.h"
35  
36 namespace leveldb {
37  
38 struct DBImpl::CompactionState {
39 Compaction* const compaction;
40  
41 // Sequence numbers < smallest_snapshot are not significant since we
42 // will never have to service a snapshot below smallest_snapshot.
43 // Therefore if we have seen a sequence number S <= smallest_snapshot,
44 // we can drop all entries for the same key with sequence numbers < S.
45 SequenceNumber smallest_snapshot;
46  
47 // Files produced by compaction
48 struct Output {
49 uint64_t number;
50 uint64_t file_size;
51 InternalKey smallest, largest;
52 };
53 std::vector<Output> outputs;
54  
55 // State kept for output being generated
56 WritableFile* outfile;
57 TableBuilder* builder;
58  
59 uint64_t total_bytes;
60  
61 0 Output* current_output() { return &outputs[outputs.size()-1]; }
62  
63 0 explicit CompactionState(Compaction* c)
64 0 : compaction(c),
65 0 outfile(NULL),
66 0 builder(NULL),
67 0 total_bytes(0) {
68 0 }
69 };
70  
71 // Fix user-supplied options to be reasonable
72 template <class T,class V>
73 12 static void ClipToRange(T* ptr, V minvalue, V maxvalue) {
74 12 ▶ if (static_cast<V>(*ptr) > maxvalue) *ptr = maxvalue;
Branch: 2 branches
BranchTrueFalseTotal
if (static_cast<V>(*ptr) > maxvalue) *ptr = maxvalue; 0 8 8
if (static_cast<V>(*ptr) > maxvalue) *ptr = maxvalue; 0 4 4
75 12 ▶ if (static_cast<V>(*ptr) < minvalue) *ptr = minvalue;
Branch: 2 branches
BranchTrueFalseTotal
if (static_cast<V>(*ptr) < minvalue) *ptr = minvalue; 0 8 8
if (static_cast<V>(*ptr) < minvalue) *ptr = minvalue; 0 4 4
76 12 }
77 4 Options SanitizeOptions(const std::string& dbname,
78 const InternalKeyComparator* icmp,
79 const Options& src) {
80 4 Options result = src;
81 4 result.comparator = icmp;
82 4 ClipToRange(&result.max_open_files, 20, 50000);
83 4 ClipToRange(&result.write_buffer_size, 64<<10, 1<<30);
84 4 ClipToRange(&result.block_size, 1<<10, 4<<20);
85 4 ▶ if (result.info_log == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (result.info_log == NULL) { 4 0 4
86 // Open a log file in the same directory as the db
87 4 ▶ src.env->CreateDir(dbname); // In case it does not exist
Branch: 1 branch
BranchTrueFalseTotal
src.env->CreateDir(dbname); // In case it does not exist 4 0 4
88 4 ▶ src.env->RenameFile(InfoLogFileName(dbname), OldInfoLogFileName(dbname));
Branch: 3 branches
BranchTrueFalseTotal
src.env->RenameFile(InfoLogFileName(dbname), OldInfoLogFileName(dbname)); 4 0 4
src.env->RenameFile(InfoLogFileName(dbname), OldInfoLogFileName(dbname)); 4 0 4
src.env->RenameFile(InfoLogFileName(dbname), OldInfoLogFileName(dbname)); 4 0 4
89 4 ▶ Status s = src.env->NewLogger(InfoLogFileName(dbname), &result.info_log);
Branch: 2 branches
BranchTrueFalseTotal
Status s = src.env->NewLogger(InfoLogFileName(dbname), &result.info_log); 4 0 4
Status s = src.env->NewLogger(InfoLogFileName(dbname), &result.info_log); 4 0 4
90 4 ▶ if (!s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s.ok()) { 0 4 4
91 // No place suitable for logging
92 0 result.info_log = NULL;
93 }
94 4 }
95 4 ▶ if (result.block_cache == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (result.block_cache == NULL) { 4 0 4
96 4 result.block_cache = NewLRUCache(8 << 20);
97 }
98 4 return result;
99 }
100  
101 4 DBImpl::DBImpl(const Options& options, const std::string& dbname)
102 4 : env_(options.env),
103 4 internal_comparator_(options.comparator),
104 4 ▶ options_(SanitizeOptions(dbname, &internal_comparator_, options)),
Branch: 1 branch
BranchTrueFalseTotal
options_(SanitizeOptions(dbname, &internal_comparator_, options)), 4 0 4
105 4 owns_info_log_(options_.info_log != options.info_log),
106 4 owns_cache_(options_.block_cache != options.block_cache),
107 4 ▶ dbname_(dbname),
Branch: 1 branch
BranchTrueFalseTotal
dbname_(dbname), 4 0 4
108 4 db_lock_(NULL),
109 4 shutting_down_(NULL),
110 4 ▶ bg_cv_(&mutex_),
Branch: 1 branch
BranchTrueFalseTotal
bg_cv_(&mutex_), 4 0 4
111 4 ▶ mem_(new MemTable(internal_comparator_)),
Branch: 3 branches
BranchTrueFalseTotal
mem_(new MemTable(internal_comparator_)), 4 0 4
mem_(new MemTable(internal_comparator_)), 4 0 4
mem_(new MemTable(internal_comparator_)), 0 0 0
112 4 imm_(NULL),
113 4 logfile_(NULL),
114 4 logfile_number_(0),
115 4 log_(NULL),
116 4 logger_(NULL),
117 4 ▶ logger_cv_(&mutex_),
Branch: 1 branch
BranchTrueFalseTotal
logger_cv_(&mutex_), 4 0 4
118 4 bg_compaction_scheduled_(false),
119 44 ▶ manual_compaction_(NULL) {
Branch: 2 branches
BranchTrueFalseTotal
manual_compaction_(NULL) { 4 0 4
manual_compaction_(NULL) { 28 4 32
120 4 mem_->Ref();
121 4 has_imm_.Release_Store(NULL);
122  
123 // Reserve ten files or so for other uses and give the rest to TableCache.
124 4 const int table_cache_size = options.max_open_files - 10;
125 4 ▶ table_cache_ = new TableCache(dbname_, &options_, table_cache_size);
Branch: 3 branches
BranchTrueFalseTotal
table_cache_ = new TableCache(dbname_, &options_, table_cache_size); 4 0 4
table_cache_ = new TableCache(dbname_, &options_, table_cache_size); 4 0 4
table_cache_ = new TableCache(dbname_, &options_, table_cache_size); 0 0 0
126  
127 0 ▶ versions_ = new VersionSet(dbname_, &options_, table_cache_,
Branch: 1 branch
BranchTrueFalseTotal
versions_ = new VersionSet(dbname_, &options_, table_cache_, 0 0 0
128 4 ▶ &internal_comparator_);
Branch: 2 branches
BranchTrueFalseTotal
&internal_comparator_); 4 0 4
&internal_comparator_); 4 0 4
129 4 }
130  
131 16 DBImpl::~DBImpl() {
132 // Wait for background work to finish
133 4 mutex_.Lock();
134 4 shutting_down_.Release_Store(this); // Any non-NULL value is ok
135 4 ▶ while (bg_compaction_scheduled_) {
Branch: 1 branch
BranchTrueFalseTotal
while (bg_compaction_scheduled_) { 0 4 4
136 0 bg_cv_.Wait();
137 }
138 4 mutex_.Unlock();
139  
140 4 ▶ if (db_lock_ != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (db_lock_ != NULL) { 3 1 4
141 3 env_->UnlockFile(db_lock_);
142 }
143  
144 4 ▶ delete versions_;
Branch: 1 branch
BranchTrueFalseTotal
delete versions_; 4 0 4
145 4 ▶ if (mem_ != NULL) mem_->Unref();
Branch: 1 branch
BranchTrueFalseTotal
if (mem_ != NULL) mem_->Unref(); 4 0 4
146 4 ▶ if (imm_ != NULL) imm_->Unref();
Branch: 1 branch
BranchTrueFalseTotal
if (imm_ != NULL) imm_->Unref(); 0 4 4
147 4 ▶ delete log_;
Branch: 1 branch
BranchTrueFalseTotal
delete log_; 3 1 4
148 4 ▶ delete logfile_;
Branch: 1 branch
BranchTrueFalseTotal
delete logfile_; 3 1 4
149 4 ▶ delete table_cache_;
Branch: 1 branch
BranchTrueFalseTotal
delete table_cache_; 4 0 4
150  
151 4 ▶ if (owns_info_log_) {
Branch: 1 branch
BranchTrueFalseTotal
if (owns_info_log_) { 4 0 4
152 4 ▶ delete options_.info_log;
Branch: 1 branch
BranchTrueFalseTotal
delete options_.info_log; 4 0 4
153 }
154 4 ▶ if (owns_cache_) {
Branch: 1 branch
BranchTrueFalseTotal
if (owns_cache_) { 4 0 4
155 4 ▶ delete options_.block_cache;
Branch: 1 branch
BranchTrueFalseTotal
delete options_.block_cache; 4 0 4
156 }
157 8 }
158  
159 2 Status DBImpl::NewDB() {
160 2 ▶ VersionEdit new_db;
Branch: 1 branch
BranchTrueFalseTotal
VersionEdit new_db; 2 0 2
161 2 ▶ new_db.SetComparatorName(user_comparator()->Name());
Branch: 2 branches
BranchTrueFalseTotal
new_db.SetComparatorName(user_comparator()->Name()); 2 0 2
new_db.SetComparatorName(user_comparator()->Name()); 2 0 2
162 2 new_db.SetLogNumber(0);
163 2 new_db.SetNextFile(2);
164 2 new_db.SetLastSequence(0);
165  
166 2 ▶ const std::string manifest = DescriptorFileName(dbname_, 1);
Branch: 1 branch
BranchTrueFalseTotal
const std::string manifest = DescriptorFileName(dbname_, 1); 2 0 2
167 WritableFile* file;
168 2 ▶ Status s = env_->NewWritableFile(manifest, &file);
Branch: 1 branch
BranchTrueFalseTotal
Status s = env_->NewWritableFile(manifest, &file); 2 0 2
169 2 ▶ if (!s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s.ok()) { 0 2 2
170 0 return s;
171 }
172 {
173 2 ▶ log::Writer log(file);
Branch: 1 branch
BranchTrueFalseTotal
log::Writer log(file); 2 0 2
174 2 std::string record;
175 2 ▶ new_db.EncodeTo(&record);
Branch: 1 branch
BranchTrueFalseTotal
new_db.EncodeTo(&record); 2 0 2
176 2 ▶ s = log.AddRecord(record);
Branch: 2 branches
BranchTrueFalseTotal
s = log.AddRecord(record); 2 0 2
s = log.AddRecord(record); 2 0 2
177 2 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 2 0 2
178 2 ▶ s = file->Close();
Branch: 2 branches
BranchTrueFalseTotal
s = file->Close(); 2 0 2
s = file->Close(); 2 0 2
179 }
180 2 }
181 2 ▶ delete file;
Branch: 1 branch
BranchTrueFalseTotal
delete file; 2 0 2
182 2 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 2 0 2
183 // Make "CURRENT" file that points to the new manifest file.
184 2 ▶ s = SetCurrentFile(env_, dbname_, 1);
Branch: 2 branches
BranchTrueFalseTotal
s = SetCurrentFile(env_, dbname_, 1); 2 0 2
s = SetCurrentFile(env_, dbname_, 1); 2 0 2
185 } else {
186 0 ▶ env_->DeleteFile(manifest);
Branch: 1 branch
BranchTrueFalseTotal
env_->DeleteFile(manifest); 0 0 0
187 }
188 2 return s;
189 2 }
190  
191 13 void DBImpl::MaybeIgnoreError(Status* s) const {
192 13 ▶ if (s->ok() || options_.paranoid_checks) {
Branch: 3 branches
BranchTrueFalseTotal
if (s->ok() || options_.paranoid_checks) { 0 13 13
if (s->ok() || options_.paranoid_checks) { 0 0 0
if (s->ok() || options_.paranoid_checks) { 0 13 13
193 // No change needed
194 } else {
195 0 ▶ Log(options_.info_log, "Ignoring error %s", s->ToString().c_str());
Branch: 2 branches
BranchTrueFalseTotal
Log(options_.info_log, "Ignoring error %s", s->ToString().c_str()); 0 0 0
Log(options_.info_log, "Ignoring error %s", s->ToString().c_str()); 0 0 0
196 0 ▶ *s = Status::OK();
Branch: 1 branch
BranchTrueFalseTotal
*s = Status::OK(); 0 0 0
197 }
198 13 }
199  
200 3 void DBImpl::DeleteObsoleteFiles() {
201 // Make a set of all of the live files
202 3 ▶ std::set<uint64_t> live = pending_outputs_;
Branch: 1 branch
BranchTrueFalseTotal
std::set<uint64_t> live = pending_outputs_; 3 0 3
203 3 ▶ versions_->AddLiveFiles(&live);
Branch: 1 branch
BranchTrueFalseTotal
versions_->AddLiveFiles(&live); 3 0 3
204  
205 3 std::vector<std::string> filenames;
206 3 ▶ env_->GetChildren(dbname_, &filenames); // Ignoring errors on purpose
Branch: 1 branch
BranchTrueFalseTotal
env_->GetChildren(dbname_, &filenames); // Ignoring errors on purpose 3 0 3
207 uint64_t number;
208 FileType type;
209 30 ▶ for (size_t i = 0; i < filenames.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < filenames.size(); i++) { 27 3 30
210 27 ▶ if (ParseFileName(filenames[i], &number, &type)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ParseFileName(filenames[i], &number, &type)) { 27 0 27
if (ParseFileName(filenames[i], &number, &type)) { 21 6 27
211 21 bool keep = true;
212 21 switch (type) {
213 4 case kLogFile:
214 5 ▶ keep = ((number >= versions_->LogNumber()) ||
Branch: 1 branch
BranchTrueFalseTotal
keep = ((number >= versions_->LogNumber()) || 1 3 4
215 1 ▶ (number == versions_->PrevLogNumber()));
Branch: 1 branch
BranchTrueFalseTotal
(number == versions_->PrevLogNumber())); 0 1 1
216 4 break;
217 6 case kDescriptorFile:
218 // Keep my manifest file, and any newer incarnations'
219 // (in case there is a race that allows other incarnations)
220 6 keep = (number >= versions_->ManifestFileNumber());
221 6 break;
222 1 case kTableFile:
223 1 ▶ keep = (live.find(number) != live.end());
Branch: 1 branch
BranchTrueFalseTotal
keep = (live.find(number) != live.end()); 1 0 1
224 1 break;
225 0 case kTempFile:
226 // Any temp files that are currently being written to must
227 // be recorded in pending_outputs_, which is inserted into "live"
228 0 ▶ keep = (live.find(number) != live.end());
Branch: 1 branch
BranchTrueFalseTotal
keep = (live.find(number) != live.end()); 0 0 0
229 0 break;
230 10 case kCurrentFile:
231 case kDBLockFile:
232 case kInfoLogFile:
233 10 keep = true;
234 10 break;
235 }
236  
237 21 ▶ if (!keep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!keep) { 4 17 21
238 4 ▶ if (type == kTableFile) {
Branch: 1 branch
BranchTrueFalseTotal
if (type == kTableFile) { 0 4 4
239 0 ▶ table_cache_->Evict(number);
Branch: 1 branch
BranchTrueFalseTotal
table_cache_->Evict(number); 0 0 0
240 }
241 4 ▶ Log(options_.info_log, "Delete type=%d #%lld\n",
Branch: 1 branch
BranchTrueFalseTotal
Log(options_.info_log, "Delete type=%d #%lld\n", 4 0 4
242 int(type),
243 static_cast<unsigned long long>(number));
244 4 ▶ env_->DeleteFile(dbname_ + "/" + filenames[i]);
Branch: 3 branches
BranchTrueFalseTotal
env_->DeleteFile(dbname_ + "/" + filenames[i]); 4 0 4
env_->DeleteFile(dbname_ + "/" + filenames[i]); 4 0 4
env_->DeleteFile(dbname_ + "/" + filenames[i]); 4 0 4
245 }
246 }
247 }
248 3 }
249  
250 4 Status DBImpl::Recover(VersionEdit* edit) {
251 4 mutex_.AssertHeld();
252  
253 // Ignore error from CreateDir since the creation of the DB is
254 // committed only when the descriptor is created, and this directory
255 // may already exist from a previous failed creation attempt.
256 4 ▶ env_->CreateDir(dbname_);
Branch: 1 branch
BranchTrueFalseTotal
env_->CreateDir(dbname_); 4 0 4
257 4 ▶ assert(db_lock_ == NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(db_lock_ == NULL); 0 4 4
258 4 ▶ Status s = env_->LockFile(LockFileName(dbname_), &db_lock_);
Branch: 2 branches
BranchTrueFalseTotal
Status s = env_->LockFile(LockFileName(dbname_), &db_lock_); 4 0 4
Status s = env_->LockFile(LockFileName(dbname_), &db_lock_); 4 0 4
259 4 ▶ if (!s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s.ok()) { 1 3 4
260 1 ▶ return s;
Branch: 1 branch
BranchTrueFalseTotal
return s; 1 0 1
261 }
262  
263 3 ▶ if (!env_->FileExists(CurrentFileName(dbname_))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!env_->FileExists(CurrentFileName(dbname_))) { 3 0 3
if (!env_->FileExists(CurrentFileName(dbname_))) { 3 0 3
if (!env_->FileExists(CurrentFileName(dbname_))) { 2 1 3
264 2 ▶ if (options_.create_if_missing) {
Branch: 1 branch
BranchTrueFalseTotal
if (options_.create_if_missing) { 2 0 2
265 2 ▶ s = NewDB();
Branch: 2 branches
BranchTrueFalseTotal
s = NewDB(); 2 0 2
s = NewDB(); 2 0 2
266 2 ▶ if (!s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s.ok()) { 0 2 2
267 0 ▶ return s;
Branch: 1 branch
BranchTrueFalseTotal
return s; 0 0 0
268 }
269 } else {
270 return Status::InvalidArgument(
271 0 ▶ dbname_, "does not exist (create_if_missing is false)");
Branch: 1 branch
BranchTrueFalseTotal
dbname_, "does not exist (create_if_missing is false)"); 0 0 0
272 }
273 } else {
274 1 ▶ if (options_.error_if_exists) {
Branch: 1 branch
BranchTrueFalseTotal
if (options_.error_if_exists) { 0 1 1
275 return Status::InvalidArgument(
276 0 ▶ dbname_, "exists (error_if_exists is true)");
Branch: 1 branch
BranchTrueFalseTotal
dbname_, "exists (error_if_exists is true)"); 0 0 0
277 }
278 }
279  
280 3 ▶ s = versions_->Recover();
Branch: 2 branches
BranchTrueFalseTotal
s = versions_->Recover(); 3 0 3
s = versions_->Recover(); 3 0 3
281 3 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 3 0 3
282 3 SequenceNumber max_sequence(0);
283  
284 // Recover from all newer log files than the ones named in the
285 // descriptor (new log files may have been added by the previous
286 // incarnation without registering them in the descriptor).
287 //
288 // Note that PrevLogNumber() is no longer used, but we pay
289 // attention to it in case we are recovering a database
290 // produced by an older version of leveldb.
291 3 const uint64_t min_log = versions_->LogNumber();
292 3 const uint64_t prev_log = versions_->PrevLogNumber();
293 3 std::vector<std::string> filenames;
294 3 ▶ s = env_->GetChildren(dbname_, &filenames);
Branch: 2 branches
BranchTrueFalseTotal
s = env_->GetChildren(dbname_, &filenames); 3 0 3
s = env_->GetChildren(dbname_, &filenames); 3 0 3
295 3 ▶ if (!s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s.ok()) { 0 3 3
296 0 ▶ return s;
Branch: 1 branch
BranchTrueFalseTotal
return s; 0 0 0
297 }
298 uint64_t number;
299 FileType type;
300 3 std::vector<uint64_t> logs;
301 23 ▶ for (size_t i = 0; i < filenames.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < filenames.size(); i++) { 20 3 23
302 20 ▶ if (ParseFileName(filenames[i], &number, &type)
Branch: 1 branch
BranchTrueFalseTotal
if (ParseFileName(filenames[i], &number, &type) 20 0 20
303 14 ▶ && type == kLogFile
Branch: 1 branch
BranchTrueFalseTotal
&& type == kLogFile 1 13 14
304 34 ▶ && ((number >= min_log) || (number == prev_log))) {
Branch: 4 branches
BranchTrueFalseTotal
&& ((number >= min_log) || (number == prev_log))) { 14 6 20
&& ((number >= min_log) || (number == prev_log))) { 0 1 1
&& ((number >= min_log) || (number == prev_log))) { 0 0 0
&& ((number >= min_log) || (number == prev_log))) { 1 19 20
305 1 ▶ logs.push_back(number);
Branch: 1 branch
BranchTrueFalseTotal
logs.push_back(number); 1 0 1
306 }
307 }
308  
309 // Recover in the order in which the logs were generated
310 3 ▶ std::sort(logs.begin(), logs.end());
Branch: 1 branch
BranchTrueFalseTotal
std::sort(logs.begin(), logs.end()); 3 0 3
311 4 ▶ for (size_t i = 0; i < logs.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < logs.size(); i++) { 1 3 4
312 1 ▶ s = RecoverLogFile(logs[i], edit, &max_sequence);
Branch: 2 branches
BranchTrueFalseTotal
s = RecoverLogFile(logs[i], edit, &max_sequence); 1 0 1
s = RecoverLogFile(logs[i], edit, &max_sequence); 1 0 1
313  
314 // The previous incarnation may not have written any MANIFEST
315 // records after allocating this log number. So we manually
316 // update the file number allocation counter in VersionSet.
317 1 ▶ versions_->MarkFileNumberUsed(logs[i]);
Branch: 1 branch
BranchTrueFalseTotal
versions_->MarkFileNumberUsed(logs[i]); 1 0 1
318 }
319  
320 3 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 3 0 3
321 3 ▶ if (versions_->LastSequence() < max_sequence) {
Branch: 1 branch
BranchTrueFalseTotal
if (versions_->LastSequence() < max_sequence) { 1 2 3
322 1 versions_->SetLastSequence(max_sequence);
323 }
324 }
325 3 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 3 0 3
326  
327 3 ▶ return s;
Branch: 1 branch
BranchTrueFalseTotal
return s; 3 0 3
328 4 }
329  
330 1 Status DBImpl::RecoverLogFile(uint64_t log_number,
331 VersionEdit* edit,
332 SequenceNumber* max_sequence) {
333 struct LogReporter : public log::Reader::Reporter {
334 Env* env;
335 Logger* info_log;
336 const char* fname;
337 Status* status; // NULL if options_.paranoid_checks==false
338 0 virtual void Corruption(size_t bytes, const Status& s) {
339 0 ▶ Log(info_log, "%s%s: dropping %d bytes; %s",
Branch: 1 branch
BranchTrueFalseTotal
Log(info_log, "%s%s: dropping %d bytes; %s", 0 0 0
340 0 ▶ (this->status == NULL ? "(ignoring error) " : ""),
Branch: 1 branch
BranchTrueFalseTotal
(this->status == NULL ? "(ignoring error) " : ""), 0 0 0
341 0 ▶ fname, static_cast<int>(bytes), s.ToString().c_str());
Branch: 1 branch
BranchTrueFalseTotal
fname, static_cast<int>(bytes), s.ToString().c_str()); 0 0 0
342 0 ▶ if (this->status != NULL && this->status->ok()) *this->status = s;
Branch: 3 branches
BranchTrueFalseTotal
if (this->status != NULL && this->status->ok()) *this->status = s; 0 0 0
if (this->status != NULL && this->status->ok()) *this->status = s; 0 0 0
if (this->status != NULL && this->status->ok()) *this->status = s; 0 0 0
343 0 }
344 };
345  
346 1 mutex_.AssertHeld();
347  
348 // Open the log file
349 1 ▶ std::string fname = LogFileName(dbname_, log_number);
Branch: 1 branch
BranchTrueFalseTotal
std::string fname = LogFileName(dbname_, log_number); 1 0 1
350 SequentialFile* file;
351 1 ▶ Status status = env_->NewSequentialFile(fname, &file);
Branch: 1 branch
BranchTrueFalseTotal
Status status = env_->NewSequentialFile(fname, &file); 1 0 1
352 1 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 1 1
353 0 ▶ MaybeIgnoreError(&status);
Branch: 1 branch
BranchTrueFalseTotal
MaybeIgnoreError(&status); 0 0 0
354 0 return status;
355 }
356  
357 // Create the log reader.
358 1 LogReporter reporter;
359 1 reporter.env = env_;
360 1 reporter.info_log = options_.info_log;
361 1 reporter.fname = fname.c_str();
362 1 ▶ reporter.status = (options_.paranoid_checks ? &status : NULL);
Branch: 1 branch
BranchTrueFalseTotal
reporter.status = (options_.paranoid_checks ? &status : NULL); 0 1 1
363 // We intentially make log::Reader do checksumming even if
364 // paranoid_checks==false so that corruptions cause entire commits
365 // to be skipped instead of propagating bad information (like overly
366 // large sequence numbers).
367 log::Reader reader(file, &reporter, true/*checksum*/,
368 1 ▶ 0/*initial_offset*/);
Branch: 1 branch
BranchTrueFalseTotal
0/*initial_offset*/); 1 0 1
369 1 ▶ Log(options_.info_log, "Recovering log #%llu",
Branch: 1 branch
BranchTrueFalseTotal
Log(options_.info_log, "Recovering log #%llu", 1 0 1
370 (unsigned long long) log_number);
371  
372 // Read all the records and add to a memtable
373 1 std::string scratch;
374 1 Slice record;
375 1 ▶ WriteBatch batch;
Branch: 1 branch
BranchTrueFalseTotal
WriteBatch batch; 1 0 1
376 1 MemTable* mem = NULL;
377 27 ▶ while (reader.ReadRecord(&record, &scratch) &&
Branch: 2 branches
BranchTrueFalseTotal
while (reader.ReadRecord(&record, &scratch) && 14 0 14
while (reader.ReadRecord(&record, &scratch) && 13 1 14
378 13 status.ok()) {
379 13 ▶ if (record.size() < 12) {
Branch: 1 branch
BranchTrueFalseTotal
if (record.size() < 12) { 0 13 13
380 0 ▶ reporter.Corruption(
Branch: 1 branch
BranchTrueFalseTotal
reporter.Corruption( 0 0 0
381 0 ▶ record.size(), Status::Corruption("log record too small"));
Branch: 1 branch
BranchTrueFalseTotal
record.size(), Status::Corruption("log record too small")); 0 0 0
382 0 continue;
383 }
384 13 ▶ WriteBatchInternal::SetContents(&batch, record);
Branch: 1 branch
BranchTrueFalseTotal
WriteBatchInternal::SetContents(&batch, record); 13 0 13
385  
386 13 ▶ if (mem == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (mem == NULL) { 1 12 13
387 1 ▶ mem = new MemTable(internal_comparator_);
Branch: 3 branches
BranchTrueFalseTotal
mem = new MemTable(internal_comparator_); 1 0 1
mem = new MemTable(internal_comparator_); 1 0 1
mem = new MemTable(internal_comparator_); 0 0 0
388 1 mem->Ref();
389 }
390 13 ▶ status = WriteBatchInternal::InsertInto(&batch, mem);
Branch: 2 branches
BranchTrueFalseTotal
status = WriteBatchInternal::InsertInto(&batch, mem); 13 0 13
status = WriteBatchInternal::InsertInto(&batch, mem); 13 0 13
391 13 ▶ MaybeIgnoreError(&status);
Branch: 1 branch
BranchTrueFalseTotal
MaybeIgnoreError(&status); 13 0 13
392 13 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 13 13
393 0 break;
394 }
395 const SequenceNumber last_seq =
396 13 ▶ WriteBatchInternal::Sequence(&batch) +
Branch: 1 branch
BranchTrueFalseTotal
WriteBatchInternal::Sequence(&batch) + 13 0 13
397 13 ▶ WriteBatchInternal::Count(&batch) - 1;
Branch: 1 branch
BranchTrueFalseTotal
WriteBatchInternal::Count(&batch) - 1; 13 0 13
398 13 ▶ if (last_seq > *max_sequence) {
Branch: 1 branch
BranchTrueFalseTotal
if (last_seq > *max_sequence) { 13 0 13
399 13 *max_sequence = last_seq;
400 }
401  
402 13 ▶ if (mem->ApproximateMemoryUsage() > options_.write_buffer_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (mem->ApproximateMemoryUsage() > options_.write_buffer_size) { 13 0 13
if (mem->ApproximateMemoryUsage() > options_.write_buffer_size) { 0 13 13
403 0 ▶ status = WriteLevel0Table(mem, edit, NULL);
Branch: 2 branches
BranchTrueFalseTotal
status = WriteLevel0Table(mem, edit, NULL); 0 0 0
status = WriteLevel0Table(mem, edit, NULL); 0 0 0
404 0 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 0 0
405 // Reflect errors immediately so that conditions like full
406 // file-systems cause the DB::Open() to fail.
407 0 break;
408 }
409 0 mem->Unref();
410 0 mem = NULL;
411 }
412 }
413  
414 1 ▶ if (status.ok() && mem != NULL) {
Branch: 3 branches
BranchTrueFalseTotal
if (status.ok() && mem != NULL) { 1 0 1
if (status.ok() && mem != NULL) { 1 0 1
if (status.ok() && mem != NULL) { 1 0 1
415 1 ▶ status = WriteLevel0Table(mem, edit, NULL);
Branch: 2 branches
BranchTrueFalseTotal
status = WriteLevel0Table(mem, edit, NULL); 1 0 1
status = WriteLevel0Table(mem, edit, NULL); 1 0 1
416 // Reflect errors immediately so that conditions like full
417 // file-systems cause the DB::Open() to fail.
418 }
419  
420 1 ▶ if (mem != NULL) mem->Unref();
Branch: 1 branch
BranchTrueFalseTotal
if (mem != NULL) mem->Unref(); 1 0 1
421 1 ▶ delete file;
Branch: 1 branch
BranchTrueFalseTotal
delete file; 1 0 1
422 1 return status;
423 1 }
424  
425 1 Status DBImpl::WriteLevel0Table(MemTable* mem, VersionEdit* edit,
426 Version* base) {
427 1 mutex_.AssertHeld();
428 1 ▶ const uint64_t start_micros = env_->NowMicros();
Branch: 1 branch
BranchTrueFalseTotal
const uint64_t start_micros = env_->NowMicros(); 1 0 1
429 1 FileMetaData meta;
430 1 meta.number = versions_->NewFileNumber();
431 1 ▶ pending_outputs_.insert(meta.number);
Branch: 1 branch
BranchTrueFalseTotal
pending_outputs_.insert(meta.number); 1 0 1
432 1 ▶ Iterator* iter = mem->NewIterator();
Branch: 1 branch
BranchTrueFalseTotal
Iterator* iter = mem->NewIterator(); 1 0 1
433 1 Log(options_.info_log, "Level-0 table #%llu: started",
434 1 ▶ (unsigned long long) meta.number);
Branch: 1 branch
BranchTrueFalseTotal
(unsigned long long) meta.number); 1 0 1
435  
436 1 Status s;
437 {
438 1 ▶ mutex_.Unlock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Unlock(); 1 0 1
439 1 ▶ s = BuildTable(dbname_, env_, options_, table_cache_, iter, &meta);
Branch: 2 branches
BranchTrueFalseTotal
s = BuildTable(dbname_, env_, options_, table_cache_, iter, &meta); 1 0 1
s = BuildTable(dbname_, env_, options_, table_cache_, iter, &meta); 1 0 1
440 1 ▶ mutex_.Lock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Lock(); 1 0 1
441 }
442  
443 1 Log(options_.info_log, "Level-0 table #%llu: %lld bytes %s",
444 1 (unsigned long long) meta.number,
445 1 ▶ (unsigned long long) meta.file_size,
Branch: 1 branch
BranchTrueFalseTotal
(unsigned long long) meta.file_size, 1 0 1
446 2 ▶ s.ToString().c_str());
Branch: 1 branch
BranchTrueFalseTotal
s.ToString().c_str()); 1 0 1
447 1 ▶ delete iter;
Branch: 1 branch
BranchTrueFalseTotal
delete iter; 1 0 1
448 1 ▶ pending_outputs_.erase(meta.number);
Branch: 1 branch
BranchTrueFalseTotal
pending_outputs_.erase(meta.number); 1 0 1
449  
450  
451 // Note that if file_size is zero, the file has been deleted and
452 // should not be added to the manifest.
453 1 int level = 0;
454 1 ▶ if (s.ok() && meta.file_size > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (s.ok() && meta.file_size > 0) { 1 0 1
if (s.ok() && meta.file_size > 0) { 1 0 1
if (s.ok() && meta.file_size > 0) { 1 0 1
455 1 const Slice min_user_key = meta.smallest.user_key();
456 1 const Slice max_user_key = meta.largest.user_key();
457 1 ▶ if (base != NULL && !base->OverlapInLevel(0, min_user_key, max_user_key)) {
Branch: 4 branches
BranchTrueFalseTotal
if (base != NULL && !base->OverlapInLevel(0, min_user_key, max_user_key)) { 0 1 1
if (base != NULL && !base->OverlapInLevel(0, min_user_key, max_user_key)) { 0 0 0
if (base != NULL && !base->OverlapInLevel(0, min_user_key, max_user_key)) { 0 0 0
if (base != NULL && !base->OverlapInLevel(0, min_user_key, max_user_key)) { 0 1 1
458 // Push the new sstable to a higher level if possible to reduce
459 // expensive manifest file ops.
460 0 ▶ while (level < config::kMaxMemCompactLevel &&
Branch: 2 branches
BranchTrueFalseTotal
while (level < config::kMaxMemCompactLevel && 0 0 0
while (level < config::kMaxMemCompactLevel && 0 0 0
461 0 ▶ !base->OverlapInLevel(level + 1, min_user_key, max_user_key)) {
Branch: 2 branches
BranchTrueFalseTotal
!base->OverlapInLevel(level + 1, min_user_key, max_user_key)) { 0 0 0
!base->OverlapInLevel(level + 1, min_user_key, max_user_key)) { 0 0 0
462 0 level++;
463 }
464 }
465 1 ▶ edit->AddFile(level, meta.number, meta.file_size,
Branch: 1 branch
BranchTrueFalseTotal
edit->AddFile(level, meta.number, meta.file_size, 1 0 1
466 meta.smallest, meta.largest);
467 }
468  
469 1 CompactionStats stats;
470 1 ▶ stats.micros = env_->NowMicros() - start_micros;
Branch: 1 branch
BranchTrueFalseTotal
stats.micros = env_->NowMicros() - start_micros; 1 0 1
471 1 stats.bytes_written = meta.file_size;
472 1 stats_[level].Add(stats);
473 2 return s;
474 1 }
475  
476 0 Status DBImpl::CompactMemTable() {
477 0 mutex_.AssertHeld();
478 0 ▶ assert(imm_ != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(imm_ != NULL); 0 0 0
479  
480 // Save the contents of the memtable as a new Table
481 0 ▶ VersionEdit edit;
Branch: 1 branch
BranchTrueFalseTotal
VersionEdit edit; 0 0 0
482 0 Version* base = versions_->current();
483 0 ▶ base->Ref();
Branch: 1 branch
BranchTrueFalseTotal
base->Ref(); 0 0 0
484 0 ▶ Status s = WriteLevel0Table(imm_, &edit, base);
Branch: 1 branch
BranchTrueFalseTotal
Status s = WriteLevel0Table(imm_, &edit, base); 0 0 0
485 0 ▶ base->Unref();
Branch: 1 branch
BranchTrueFalseTotal
base->Unref(); 0 0 0
486  
487 0 ▶ if (s.ok() && shutting_down_.Acquire_Load()) {
Branch: 3 branches
BranchTrueFalseTotal
if (s.ok() && shutting_down_.Acquire_Load()) { 0 0 0
if (s.ok() && shutting_down_.Acquire_Load()) { 0 0 0
if (s.ok() && shutting_down_.Acquire_Load()) { 0 0 0
488 0 ▶ s = Status::IOError("Deleting DB during memtable compaction");
Branch: 2 branches
BranchTrueFalseTotal
s = Status::IOError("Deleting DB during memtable compaction"); 0 0 0
s = Status::IOError("Deleting DB during memtable compaction"); 0 0 0
489 }
490  
491 // Replace immutable memtable with the generated Table
492 0 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 0 0 0
493 0 edit.SetPrevLogNumber(0);
494 0 edit.SetLogNumber(logfile_number_); // Earlier logs no longer needed
495 0 ▶ s = versions_->LogAndApply(&edit, &mutex_);
Branch: 2 branches
BranchTrueFalseTotal
s = versions_->LogAndApply(&edit, &mutex_); 0 0 0
s = versions_->LogAndApply(&edit, &mutex_); 0 0 0
496 }
497  
498 0 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 0 0 0
499 // Commit to the new state
500 0 imm_->Unref();
501 0 imm_ = NULL;
502 0 has_imm_.Release_Store(NULL);
503 0 ▶ DeleteObsoleteFiles();
Branch: 1 branch
BranchTrueFalseTotal
DeleteObsoleteFiles(); 0 0 0
504 }
505  
506 0 return s;
507 0 }
508  
509 0 void DBImpl::TEST_CompactRange(
510 int level,
511 const std::string& begin,
512 const std::string& end) {
513 0 ▶ assert(level >= 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(level >= 0); 0 0 0
514 0 ▶ assert(level + 1 < config::kNumLevels);
Branch: 1 branch
BranchTrueFalseTotal
assert(level + 1 < config::kNumLevels); 0 0 0
515  
516 0 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 0 0 0
517 0 ▶ while (manual_compaction_ != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (manual_compaction_ != NULL) { 0 0 0
518 0 ▶ bg_cv_.Wait();
Branch: 1 branch
BranchTrueFalseTotal
bg_cv_.Wait(); 0 0 0
519 }
520 0 ManualCompaction manual;
521 0 manual.level = level;
522 0 ▶ manual.begin = begin;
Branch: 1 branch
BranchTrueFalseTotal
manual.begin = begin; 0 0 0
523 0 ▶ manual.end = end;
Branch: 1 branch
BranchTrueFalseTotal
manual.end = end; 0 0 0
524 0 manual_compaction_ = &manual;
525 0 ▶ MaybeScheduleCompaction();
Branch: 1 branch
BranchTrueFalseTotal
MaybeScheduleCompaction(); 0 0 0
526 0 ▶ while (manual_compaction_ == &manual) {
Branch: 1 branch
BranchTrueFalseTotal
while (manual_compaction_ == &manual) { 0 0 0
527 0 ▶ bg_cv_.Wait();
Branch: 1 branch
BranchTrueFalseTotal
bg_cv_.Wait(); 0 0 0
528 }
529 0 }
530  
531 0 Status DBImpl::TEST_CompactMemTable() {
532 0 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 0 0 0
533 LoggerId self;
534 0 ▶ AcquireLoggingResponsibility(&self);
Branch: 1 branch
BranchTrueFalseTotal
AcquireLoggingResponsibility(&self); 0 0 0
535 0 ▶ Status s = MakeRoomForWrite(true /* force compaction */);
Branch: 1 branch
BranchTrueFalseTotal
Status s = MakeRoomForWrite(true /* force compaction */); 0 0 0
536 0 ▶ ReleaseLoggingResponsibility(&self);
Branch: 1 branch
BranchTrueFalseTotal
ReleaseLoggingResponsibility(&self); 0 0 0
537 0 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 0 0 0
538 // Wait until the compaction completes
539 0 ▶ while (imm_ != NULL && bg_error_.ok()) {
Branch: 3 branches
BranchTrueFalseTotal
while (imm_ != NULL && bg_error_.ok()) { 0 0 0
while (imm_ != NULL && bg_error_.ok()) { 0 0 0
while (imm_ != NULL && bg_error_.ok()) { 0 0 0
540 0 ▶ bg_cv_.Wait();
Branch: 1 branch
BranchTrueFalseTotal
bg_cv_.Wait(); 0 0 0
541 }
542 0 ▶ if (imm_ != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (imm_ != NULL) { 0 0 0
543 0 ▶ s = bg_error_;
Branch: 1 branch
BranchTrueFalseTotal
s = bg_error_; 0 0 0
544 }
545 }
546 0 return s;
547 0 }
548  
549 3 void DBImpl::MaybeScheduleCompaction() {
550 3 mutex_.AssertHeld();
551 3 ▶ if (bg_compaction_scheduled_) {
Branch: 1 branch
BranchTrueFalseTotal
if (bg_compaction_scheduled_) { 3 0 3
552 // Already scheduled
553 3 ▶ } else if (shutting_down_.Acquire_Load()) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (shutting_down_.Acquire_Load()) { 3 0 3
554 // DB is being deleted; no more background compactions
555 9 } else if (imm_ == NULL &&
556 6 ▶ manual_compaction_ == NULL &&
Branch: 3 branches
BranchTrueFalseTotal
manual_compaction_ == NULL && 3 0 3
manual_compaction_ == NULL && 3 0 3
manual_compaction_ == NULL && 0 3 3
557 3 ▶ !versions_->NeedsCompaction()) {
Branch: 1 branch
BranchTrueFalseTotal
!versions_->NeedsCompaction()) { 3 0 3
558 // No work to be done
559 } else {
560 0 bg_compaction_scheduled_ = true;
561 0 env_->Schedule(&DBImpl::BGWork, this);
562 }
563 3 }
564  
565 0 void DBImpl::BGWork(void* db) {
566 0 reinterpret_cast<DBImpl*>(db)->BackgroundCall();
567 0 }
568  
569 0 void DBImpl::BackgroundCall() {
570 0 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 0 0 0
571 0 ▶ assert(bg_compaction_scheduled_);
Branch: 1 branch
BranchTrueFalseTotal
assert(bg_compaction_scheduled_); 0 0 0
572 0 ▶ if (!shutting_down_.Acquire_Load()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!shutting_down_.Acquire_Load()) { 0 0 0
573 0 ▶ BackgroundCompaction();
Branch: 1 branch
BranchTrueFalseTotal
BackgroundCompaction(); 0 0 0
574 }
575 0 bg_compaction_scheduled_ = false;
576  
577 // Previous compaction may have produced too many files in a level,
578 // so reschedule another compaction if needed.
579 0 ▶ MaybeScheduleCompaction();
Branch: 1 branch
BranchTrueFalseTotal
MaybeScheduleCompaction(); 0 0 0
580 0 ▶ bg_cv_.SignalAll();
Branch: 1 branch
BranchTrueFalseTotal
bg_cv_.SignalAll(); 0 0 0
581 0 }
582  
583 0 void DBImpl::BackgroundCompaction() {
584 0 mutex_.AssertHeld();
585  
586 0 ▶ if (imm_ != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (imm_ != NULL) { 0 0 0
587 0 ▶ CompactMemTable();
Branch: 1 branch
BranchTrueFalseTotal
CompactMemTable(); 0 0 0
588 0 return;
589 }
590  
591 Compaction* c;
592 0 bool is_manual = (manual_compaction_ != NULL);
593 0 ▶ if (is_manual) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_manual) { 0 0 0
594 0 const ManualCompaction* m = manual_compaction_;
595 0 c = versions_->CompactRange(
596 0 ▶ m->level,
Branch: 1 branch
BranchTrueFalseTotal
m->level, 0 0 0
597 0 ▶ InternalKey(m->begin, kMaxSequenceNumber, kValueTypeForSeek),
Branch: 1 branch
BranchTrueFalseTotal
InternalKey(m->begin, kMaxSequenceNumber, kValueTypeForSeek), 0 0 0
598 0 ▶ InternalKey(m->end, 0, static_cast<ValueType>(0)));
Branch: 1 branch
BranchTrueFalseTotal
InternalKey(m->end, 0, static_cast<ValueType>(0))); 0 0 0
599 } else {
600 0 ▶ c = versions_->PickCompaction();
Branch: 1 branch
BranchTrueFalseTotal
c = versions_->PickCompaction(); 0 0 0
601 }
602  
603 0 Status status;
604 0 ▶ if (c == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == NULL) { 0 0 0
605 // Nothing to do
606 0 ▶ } else if (!is_manual && c->IsTrivialMove()) {
Branch: 4 branches
BranchTrueFalseTotal
} else if (!is_manual && c->IsTrivialMove()) { 0 0 0
} else if (!is_manual && c->IsTrivialMove()) { 0 0 0
} else if (!is_manual && c->IsTrivialMove()) { 0 0 0
} else if (!is_manual && c->IsTrivialMove()) { 0 0 0
607 // Move file to next level
608 0 ▶ assert(c->num_input_files(0) == 1);
Branch: 1 branch
BranchTrueFalseTotal
assert(c->num_input_files(0) == 1); 0 0 0
609 0 FileMetaData* f = c->input(0, 0);
610 0 ▶ c->edit()->DeleteFile(c->level(), f->number);
Branch: 1 branch
BranchTrueFalseTotal
c->edit()->DeleteFile(c->level(), f->number); 0 0 0
611 0 ▶ c->edit()->AddFile(c->level() + 1, f->number, f->file_size,
Branch: 1 branch
BranchTrueFalseTotal
c->edit()->AddFile(c->level() + 1, f->number, f->file_size, 0 0 0
612 0 f->smallest, f->largest);
613 0 ▶ status = versions_->LogAndApply(c->edit(), &mutex_);
Branch: 2 branches
BranchTrueFalseTotal
status = versions_->LogAndApply(c->edit(), &mutex_); 0 0 0
status = versions_->LogAndApply(c->edit(), &mutex_); 0 0 0
614 VersionSet::LevelSummaryStorage tmp;
615 0 Log(options_.info_log, "Moved #%lld to level-%d %lld bytes %s: %s\n",
616 0 ▶ static_cast<unsigned long long>(f->number),
Branch: 1 branch
BranchTrueFalseTotal
static_cast<unsigned long long>(f->number), 0 0 0
617 0 c->level() + 1,
618 0 static_cast<unsigned long long>(f->file_size),
619 0 ▶ status.ToString().c_str(),
Branch: 1 branch
BranchTrueFalseTotal
status.ToString().c_str(), 0 0 0
620 0 ▶ versions_->LevelSummary(&tmp));
Branch: 1 branch
BranchTrueFalseTotal
versions_->LevelSummary(&tmp)); 0 0 0
621 } else {
622 0 ▶ CompactionState* compact = new CompactionState(c);
Branch: 1 branch
BranchTrueFalseTotal
CompactionState* compact = new CompactionState(c); 0 0 0
623 0 ▶ status = DoCompactionWork(compact);
Branch: 2 branches
BranchTrueFalseTotal
status = DoCompactionWork(compact); 0 0 0
status = DoCompactionWork(compact); 0 0 0
624 0 ▶ CleanupCompaction(compact);
Branch: 1 branch
BranchTrueFalseTotal
CleanupCompaction(compact); 0 0 0
625 }
626 0 ▶ delete c;
Branch: 1 branch
BranchTrueFalseTotal
delete c; 0 0 0
627  
628 0 ▶ if (status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (status.ok()) { 0 0 0
629 // Done
630 0 ▶ } else if (shutting_down_.Acquire_Load()) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (shutting_down_.Acquire_Load()) { 0 0 0
631 // Ignore compaction errors found during shutting down
632 } else {
633 0 ▶ Log(options_.info_log,
Branch: 1 branch
BranchTrueFalseTotal
Log(options_.info_log, 0 0 0
634 0 ▶ "Compaction error: %s", status.ToString().c_str());
Branch: 1 branch
BranchTrueFalseTotal
"Compaction error: %s", status.ToString().c_str()); 0 0 0
635 0 ▶ if (options_.paranoid_checks && bg_error_.ok()) {
Branch: 3 branches
BranchTrueFalseTotal
if (options_.paranoid_checks && bg_error_.ok()) { 0 0 0
if (options_.paranoid_checks && bg_error_.ok()) { 0 0 0
if (options_.paranoid_checks && bg_error_.ok()) { 0 0 0
636 0 ▶ bg_error_ = status;
Branch: 1 branch
BranchTrueFalseTotal
bg_error_ = status; 0 0 0
637 }
638 }
639  
640 0 ▶ if (is_manual) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_manual) { 0 0 0
641 // Mark it as done
642 0 manual_compaction_ = NULL;
643 }
644 0 }
645  
646 0 void DBImpl::CleanupCompaction(CompactionState* compact) {
647 0 mutex_.AssertHeld();
648 0 ▶ if (compact->builder != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (compact->builder != NULL) { 0 0 0
649 // May happen if we get a shutdown call in the middle of compaction
650 0 compact->builder->Abandon();
651 0 ▶ delete compact->builder;
Branch: 1 branch
BranchTrueFalseTotal
delete compact->builder; 0 0 0
652 } else {
653 0 ▶ assert(compact->outfile == NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(compact->outfile == NULL); 0 0 0
654 }
655 0 ▶ delete compact->outfile;
Branch: 1 branch
BranchTrueFalseTotal
delete compact->outfile; 0 0 0
656 0 ▶ for (size_t i = 0; i < compact->outputs.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < compact->outputs.size(); i++) { 0 0 0
657 0 const CompactionState::Output& out = compact->outputs[i];
658 0 pending_outputs_.erase(out.number);
659 }
660 0 ▶ delete compact;
Branch: 1 branch
BranchTrueFalseTotal
delete compact; 0 0 0
661 0 }
662  
663 0 Status DBImpl::OpenCompactionOutputFile(CompactionState* compact) {
664 0 ▶ assert(compact != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(compact != NULL); 0 0 0
665 0 ▶ assert(compact->builder == NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(compact->builder == NULL); 0 0 0
666 uint64_t file_number;
667 {
668 0 ▶ mutex_.Lock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Lock(); 0 0 0
669 0 file_number = versions_->NewFileNumber();
670 0 ▶ pending_outputs_.insert(file_number);
Branch: 1 branch
BranchTrueFalseTotal
pending_outputs_.insert(file_number); 0 0 0
671 0 CompactionState::Output out;
672 0 out.number = file_number;
673 0 out.smallest.Clear();
674 0 out.largest.Clear();
675 0 ▶ compact->outputs.push_back(out);
Branch: 1 branch
BranchTrueFalseTotal
compact->outputs.push_back(out); 0 0 0
676 0 ▶ mutex_.Unlock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Unlock(); 0 0 0
677 0 }
678  
679 // Make the output file
680 0 ▶ std::string fname = TableFileName(dbname_, file_number);
Branch: 1 branch
BranchTrueFalseTotal
std::string fname = TableFileName(dbname_, file_number); 0 0 0
681 0 ▶ Status s = env_->NewWritableFile(fname, &compact->outfile);
Branch: 1 branch
BranchTrueFalseTotal
Status s = env_->NewWritableFile(fname, &compact->outfile); 0 0 0
682 0 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 0 0 0
683 0 ▶ compact->builder = new TableBuilder(options_, compact->outfile);
Branch: 3 branches
BranchTrueFalseTotal
compact->builder = new TableBuilder(options_, compact->outfile); 0 0 0
compact->builder = new TableBuilder(options_, compact->outfile); 0 0 0
compact->builder = new TableBuilder(options_, compact->outfile); 0 0 0
684 }
685 0 return s;
686 0 }
687  
688 0 Status DBImpl::FinishCompactionOutputFile(CompactionState* compact,
689 Iterator* input) {
690 0 ▶ assert(compact != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(compact != NULL); 0 0 0
691 0 ▶ assert(compact->outfile != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(compact->outfile != NULL); 0 0 0
692 0 ▶ assert(compact->builder != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(compact->builder != NULL); 0 0 0
693  
694 0 const uint64_t output_number = compact->current_output()->number;
695 0 ▶ assert(output_number != 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(output_number != 0); 0 0 0
696  
697 // Check for iterator errors
698 0 Status s = input->status();
699 0 ▶ const uint64_t current_entries = compact->builder->NumEntries();
Branch: 1 branch
BranchTrueFalseTotal
const uint64_t current_entries = compact->builder->NumEntries(); 0 0 0
700 0 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 0 0 0
701 0 ▶ s = compact->builder->Finish();
Branch: 2 branches
BranchTrueFalseTotal
s = compact->builder->Finish(); 0 0 0
s = compact->builder->Finish(); 0 0 0
702 } else {
703 0 ▶ compact->builder->Abandon();
Branch: 1 branch
BranchTrueFalseTotal
compact->builder->Abandon(); 0 0 0
704 }
705 0 ▶ const uint64_t current_bytes = compact->builder->FileSize();
Branch: 1 branch
BranchTrueFalseTotal
const uint64_t current_bytes = compact->builder->FileSize(); 0 0 0
706 0 compact->current_output()->file_size = current_bytes;
707 0 compact->total_bytes += current_bytes;
708 0 ▶ delete compact->builder;
Branch: 1 branch
BranchTrueFalseTotal
delete compact->builder; 0 0 0
709 0 compact->builder = NULL;
710  
711 // Finish and check for file errors
712 0 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 0 0 0
713 0 ▶ s = compact->outfile->Sync();
Branch: 2 branches
BranchTrueFalseTotal
s = compact->outfile->Sync(); 0 0 0
s = compact->outfile->Sync(); 0 0 0
714 }
715 0 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 0 0 0
716 0 ▶ s = compact->outfile->Close();
Branch: 2 branches
BranchTrueFalseTotal
s = compact->outfile->Close(); 0 0 0
s = compact->outfile->Close(); 0 0 0
717 }
718 0 ▶ delete compact->outfile;
Branch: 1 branch
BranchTrueFalseTotal
delete compact->outfile; 0 0 0
719 0 compact->outfile = NULL;
720  
721 0 ▶ if (s.ok() && current_entries > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (s.ok() && current_entries > 0) { 0 0 0
if (s.ok() && current_entries > 0) { 0 0 0
if (s.ok() && current_entries > 0) { 0 0 0
722 // Verify that the table is usable
723 0 ▶ Iterator* iter = table_cache_->NewIterator(ReadOptions(),
Branch: 1 branch
BranchTrueFalseTotal
Iterator* iter = table_cache_->NewIterator(ReadOptions(), 0 0 0
724 output_number,
725 current_bytes);
726 0 ▶ s = iter->status();
Branch: 2 branches
BranchTrueFalseTotal
s = iter->status(); 0 0 0
s = iter->status(); 0 0 0
727 0 ▶ delete iter;
Branch: 1 branch
BranchTrueFalseTotal
delete iter; 0 0 0
728 0 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 0 0 0
729 0 ▶ Log(options_.info_log,
Branch: 1 branch
BranchTrueFalseTotal
Log(options_.info_log, 0 0 0
730 "Generated table #%llu: %lld keys, %lld bytes",
731 (unsigned long long) output_number,
732 (unsigned long long) current_entries,
733 (unsigned long long) current_bytes);
734 }
735 }
736 0 return s;
737 }
738  
739  
740 0 Status DBImpl::InstallCompactionResults(CompactionState* compact) {
741 0 mutex_.AssertHeld();
742 0 Log(options_.info_log, "Compacted %d@%d + %d@%d files => %lld bytes",
743 0 compact->compaction->num_input_files(0),
744 0 compact->compaction->level(),
745 0 compact->compaction->num_input_files(1),
746 0 compact->compaction->level() + 1,
747 0 static_cast<long long>(compact->total_bytes));
748  
749 // Add compaction outputs
750 0 compact->compaction->AddInputDeletions(compact->compaction->edit());
751 0 const int level = compact->compaction->level();
752 0 ▶ for (size_t i = 0; i < compact->outputs.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < compact->outputs.size(); i++) { 0 0 0
753 0 const CompactionState::Output& out = compact->outputs[i];
754 0 compact->compaction->edit()->AddFile(
755 level + 1,
756 0 out.number, out.file_size, out.smallest, out.largest);
757 0 pending_outputs_.erase(out.number);
758 }
759 0 compact->outputs.clear();
760  
761 0 Status s = versions_->LogAndApply(compact->compaction->edit(), &mutex_);
762 0 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 0 0 0
763 0 ▶ compact->compaction->ReleaseInputs();
Branch: 1 branch
BranchTrueFalseTotal
compact->compaction->ReleaseInputs(); 0 0 0
764 0 ▶ DeleteObsoleteFiles();
Branch: 1 branch
BranchTrueFalseTotal
DeleteObsoleteFiles(); 0 0 0
765 } else {
766 // Discard any files we may have created during this failed compaction
767 0 ▶ for (size_t i = 0; i < compact->outputs.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < compact->outputs.size(); i++) { 0 0 0
768 0 ▶ env_->DeleteFile(TableFileName(dbname_, compact->outputs[i].number));
Branch: 2 branches
BranchTrueFalseTotal
env_->DeleteFile(TableFileName(dbname_, compact->outputs[i].number)); 0 0 0
env_->DeleteFile(TableFileName(dbname_, compact->outputs[i].number)); 0 0 0
769 }
770 }
771 0 return s;
772 }
773  
774 0 Status DBImpl::DoCompactionWork(CompactionState* compact) {
775 0 ▶ const uint64_t start_micros = env_->NowMicros();
Branch: 1 branch
BranchTrueFalseTotal
const uint64_t start_micros = env_->NowMicros(); 0 0 0
776 0 int64_t imm_micros = 0; // Micros spent doing imm_ compactions
777  
778 0 ▶ Log(options_.info_log, "Compacting %d@%d + %d@%d files",
Branch: 1 branch
BranchTrueFalseTotal
Log(options_.info_log, "Compacting %d@%d + %d@%d files", 0 0 0
779 0 compact->compaction->num_input_files(0),
780 0 compact->compaction->level(),
781 0 compact->compaction->num_input_files(1),
782 0 compact->compaction->level() + 1);
783  
784 0 ▶ assert(versions_->NumLevelFiles(compact->compaction->level()) > 0);
Branch: 2 branches
BranchTrueFalseTotal
assert(versions_->NumLevelFiles(compact->compaction->level()) > 0); 0 0 0
assert(versions_->NumLevelFiles(compact->compaction->level()) > 0); 0 0 0
785 0 ▶ assert(compact->builder == NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(compact->builder == NULL); 0 0 0
786 0 ▶ assert(compact->outfile == NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(compact->outfile == NULL); 0 0 0
787 0 ▶ if (snapshots_.empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (snapshots_.empty()) { 0 0 0
788 0 compact->smallest_snapshot = versions_->LastSequence();
789 } else {
790 0 compact->smallest_snapshot = snapshots_.oldest()->number_;
791 }
792  
793 // Release mutex while we're actually doing the compaction work
794 0 ▶ mutex_.Unlock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Unlock(); 0 0 0
795  
796 0 ▶ Iterator* input = versions_->MakeInputIterator(compact->compaction);
Branch: 1 branch
BranchTrueFalseTotal
Iterator* input = versions_->MakeInputIterator(compact->compaction); 0 0 0
797 0 ▶ input->SeekToFirst();
Branch: 1 branch
BranchTrueFalseTotal
input->SeekToFirst(); 0 0 0
798 0 Status status;
799 0 ParsedInternalKey ikey;
800 0 std::string current_user_key;
801 0 bool has_current_user_key = false;
802 0 SequenceNumber last_sequence_for_key = kMaxSequenceNumber;
803 0 ▶ for (; input->Valid() && !shutting_down_.Acquire_Load(); ) {
Branch: 4 branches
BranchTrueFalseTotal
for (; input->Valid() && !shutting_down_.Acquire_Load(); ) { 0 0 0
for (; input->Valid() && !shutting_down_.Acquire_Load(); ) { 0 0 0
for (; input->Valid() && !shutting_down_.Acquire_Load(); ) { 0 0 0
for (; input->Valid() && !shutting_down_.Acquire_Load(); ) { 0 0 0
804 // Prioritize immutable compaction work
805 0 ▶ if (has_imm_.NoBarrier_Load() != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_imm_.NoBarrier_Load() != NULL) { 0 0 0
806 0 ▶ const uint64_t imm_start = env_->NowMicros();
Branch: 1 branch
BranchTrueFalseTotal
const uint64_t imm_start = env_->NowMicros(); 0 0 0
807 0 ▶ mutex_.Lock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Lock(); 0 0 0
808 0 ▶ if (imm_ != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (imm_ != NULL) { 0 0 0
809 0 ▶ CompactMemTable();
Branch: 1 branch
BranchTrueFalseTotal
CompactMemTable(); 0 0 0
810 0 ▶ bg_cv_.SignalAll(); // Wakeup MakeRoomForWrite() if necessary
Branch: 1 branch
BranchTrueFalseTotal
bg_cv_.SignalAll(); // Wakeup MakeRoomForWrite() if necessary 0 0 0
811 }
812 0 ▶ mutex_.Unlock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Unlock(); 0 0 0
813 0 ▶ imm_micros += (env_->NowMicros() - imm_start);
Branch: 1 branch
BranchTrueFalseTotal
imm_micros += (env_->NowMicros() - imm_start); 0 0 0
814 }
815  
816 0 ▶ Slice key = input->key();
Branch: 1 branch
BranchTrueFalseTotal
Slice key = input->key(); 0 0 0
817 0 ▶ if (compact->compaction->ShouldStopBefore(key) &&
Branch: 3 branches
BranchTrueFalseTotal
if (compact->compaction->ShouldStopBefore(key) && 0 0 0
if (compact->compaction->ShouldStopBefore(key) && 0 0 0
if (compact->compaction->ShouldStopBefore(key) && 0 0 0
818 0 ▶ compact->builder != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
compact->builder != NULL) { 0 0 0
819 0 ▶ status = FinishCompactionOutputFile(compact, input);
Branch: 2 branches
BranchTrueFalseTotal
status = FinishCompactionOutputFile(compact, input); 0 0 0
status = FinishCompactionOutputFile(compact, input); 0 0 0
820 0 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 0 0
821 0 break;
822 }
823 }
824  
825 // Handle key/value, add to state, etc.
826 0 bool drop = false;
827 0 ▶ if (!ParseInternalKey(key, &ikey)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ParseInternalKey(key, &ikey)) { 0 0 0
if (!ParseInternalKey(key, &ikey)) { 0 0 0
828 // Do not hide error keys
829 0 current_user_key.clear();
830 0 has_current_user_key = false;
831 0 last_sequence_for_key = kMaxSequenceNumber;
832 } else {
833 0 ▶ if (!has_current_user_key ||
Branch: 1 branch
BranchTrueFalseTotal
if (!has_current_user_key || 0 0 0
834 0 ▶ user_comparator()->Compare(ikey.user_key,
Branch: 2 branches
BranchTrueFalseTotal
user_comparator()->Compare(ikey.user_key, 0 0 0
user_comparator()->Compare(ikey.user_key, 0 0 0
835 0 ▶ Slice(current_user_key)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
Slice(current_user_key)) != 0) { 0 0 0
836 // First occurrence of this user key
837 0 ▶ current_user_key.assign(ikey.user_key.data(), ikey.user_key.size());
Branch: 1 branch
BranchTrueFalseTotal
current_user_key.assign(ikey.user_key.data(), ikey.user_key.size()); 0 0 0
838 0 has_current_user_key = true;
839 0 last_sequence_for_key = kMaxSequenceNumber;
840 }
841  
842 0 ▶ if (last_sequence_for_key <= compact->smallest_snapshot) {
Branch: 1 branch
BranchTrueFalseTotal
if (last_sequence_for_key <= compact->smallest_snapshot) { 0 0 0
843 // Hidden by an newer entry for same user key
844 0 drop = true; // (A)
845 0 } else if (ikey.type == kTypeDeletion &&
846 0 ▶ ikey.sequence <= compact->smallest_snapshot &&
Branch: 3 branches
BranchTrueFalseTotal
ikey.sequence <= compact->smallest_snapshot && 0 0 0
ikey.sequence <= compact->smallest_snapshot && 0 0 0
ikey.sequence <= compact->smallest_snapshot && 0 0 0
847 0 ▶ compact->compaction->IsBaseLevelForKey(ikey.user_key)) {
Branch: 2 branches
BranchTrueFalseTotal
compact->compaction->IsBaseLevelForKey(ikey.user_key)) { 0 0 0
compact->compaction->IsBaseLevelForKey(ikey.user_key)) { 0 0 0
848 // For this user key:
849 // (1) there is no data in higher levels
850 // (2) data in lower levels will have larger sequence numbers
851 // (3) data in layers that are being compacted here and have
852 // smaller sequence numbers will be dropped in the next
853 // few iterations of this loop (by rule (A) above).
854 // Therefore this deletion marker is obsolete and can be dropped.
855 0 drop = true;
856 }
857  
858 0 last_sequence_for_key = ikey.sequence;
859 }
860 #if 0
861 Log(options_.info_log,
862 " Compact: %s, seq %d, type: %d %d, drop: %d, is_base: %d, "
863 "%d smallest_snapshot: %d",
864 ikey.user_key.ToString().c_str(),
865 (int)ikey.sequence, ikey.type, kTypeValue, drop,
866 compact->compaction->IsBaseLevelForKey(ikey.user_key),
867 (int)last_sequence_for_key, (int)compact->smallest_snapshot);
868 #endif
869  
870 0 ▶ if (!drop) {
Branch: 1 branch
BranchTrueFalseTotal
if (!drop) { 0 0 0
871 // Open output file if necessary
872 0 ▶ if (compact->builder == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (compact->builder == NULL) { 0 0 0
873 0 ▶ status = OpenCompactionOutputFile(compact);
Branch: 2 branches
BranchTrueFalseTotal
status = OpenCompactionOutputFile(compact); 0 0 0
status = OpenCompactionOutputFile(compact); 0 0 0
874 0 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 0 0
875 0 break;
876 }
877 }
878 0 ▶ if (compact->builder->NumEntries() == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (compact->builder->NumEntries() == 0) { 0 0 0
if (compact->builder->NumEntries() == 0) { 0 0 0
879 0 ▶ compact->current_output()->smallest.DecodeFrom(key);
Branch: 1 branch
BranchTrueFalseTotal
compact->current_output()->smallest.DecodeFrom(key); 0 0 0
880 }
881 0 ▶ compact->current_output()->largest.DecodeFrom(key);
Branch: 1 branch
BranchTrueFalseTotal
compact->current_output()->largest.DecodeFrom(key); 0 0 0
882 0 ▶ compact->builder->Add(key, input->value());
Branch: 2 branches
BranchTrueFalseTotal
compact->builder->Add(key, input->value()); 0 0 0
compact->builder->Add(key, input->value()); 0 0 0
883  
884 // Close output file if it is big enough
885 0 if (compact->builder->FileSize() >=
886 0 compact->compaction->MaxOutputFileSize()) {
887 0 ▶ status = FinishCompactionOutputFile(compact, input);
Branch: 2 branches
BranchTrueFalseTotal
status = FinishCompactionOutputFile(compact, input); 0 0 0
status = FinishCompactionOutputFile(compact, input); 0 0 0
888 0 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 0 0
889 0 break;
890 }
891 }
892 }
893  
894 0 ▶ input->Next();
Branch: 1 branch
BranchTrueFalseTotal
input->Next(); 0 0 0
895 }
896  
897 0 ▶ if (status.ok() && shutting_down_.Acquire_Load()) {
Branch: 3 branches
BranchTrueFalseTotal
if (status.ok() && shutting_down_.Acquire_Load()) { 0 0 0
if (status.ok() && shutting_down_.Acquire_Load()) { 0 0 0
if (status.ok() && shutting_down_.Acquire_Load()) { 0 0 0
898 0 ▶ status = Status::IOError("Deleting DB during compaction");
Branch: 2 branches
BranchTrueFalseTotal
status = Status::IOError("Deleting DB during compaction"); 0 0 0
status = Status::IOError("Deleting DB during compaction"); 0 0 0
899 }
900 0 ▶ if (status.ok() && compact->builder != NULL) {
Branch: 3 branches
BranchTrueFalseTotal
if (status.ok() && compact->builder != NULL) { 0 0 0
if (status.ok() && compact->builder != NULL) { 0 0 0
if (status.ok() && compact->builder != NULL) { 0 0 0
901 0 ▶ status = FinishCompactionOutputFile(compact, input);
Branch: 2 branches
BranchTrueFalseTotal
status = FinishCompactionOutputFile(compact, input); 0 0 0
status = FinishCompactionOutputFile(compact, input); 0 0 0
902 }
903 0 ▶ if (status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (status.ok()) { 0 0 0
904 0 ▶ status = input->status();
Branch: 2 branches
BranchTrueFalseTotal
status = input->status(); 0 0 0
status = input->status(); 0 0 0
905 }
906 0 ▶ delete input;
Branch: 1 branch
BranchTrueFalseTotal
delete input; 0 0 0
907 0 input = NULL;
908  
909 0 CompactionStats stats;
910 0 ▶ stats.micros = env_->NowMicros() - start_micros - imm_micros;
Branch: 1 branch
BranchTrueFalseTotal
stats.micros = env_->NowMicros() - start_micros - imm_micros; 0 0 0
911 0 ▶ for (int which = 0; which < 2; which++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int which = 0; which < 2; which++) { 0 0 0
912 0 ▶ for (int i = 0; i < compact->compaction->num_input_files(which); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < compact->compaction->num_input_files(which); i++) { 0 0 0
913 0 stats.bytes_read += compact->compaction->input(which, i)->file_size;
914 }
915 }
916 0 ▶ for (size_t i = 0; i < compact->outputs.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < compact->outputs.size(); i++) { 0 0 0
917 0 stats.bytes_written += compact->outputs[i].file_size;
918 }
919  
920 0 ▶ mutex_.Lock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Lock(); 0 0 0
921 0 stats_[compact->compaction->level() + 1].Add(stats);
922  
923 0 ▶ if (status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (status.ok()) { 0 0 0
924 0 ▶ status = InstallCompactionResults(compact);
Branch: 2 branches
BranchTrueFalseTotal
status = InstallCompactionResults(compact); 0 0 0
status = InstallCompactionResults(compact); 0 0 0
925 }
926 VersionSet::LevelSummaryStorage tmp;
927 0 ▶ Log(options_.info_log,
Branch: 1 branch
BranchTrueFalseTotal
Log(options_.info_log, 0 0 0
928 0 ▶ "compacted to: %s", versions_->LevelSummary(&tmp));
Branch: 1 branch
BranchTrueFalseTotal
"compacted to: %s", versions_->LevelSummary(&tmp)); 0 0 0
929 0 return status;
930 0 }
931  
932 namespace {
933 struct IterState {
934 port::Mutex* mu;
935 Version* version;
936 MemTable* mem;
937 MemTable* imm;
938 };
939  
940 14 static void CleanupIteratorState(void* arg1, void* arg2) {
941 14 IterState* state = reinterpret_cast<IterState*>(arg1);
942 14 state->mu->Lock();
943 14 state->mem->Unref();
944 14 ▶ if (state->imm != NULL) state->imm->Unref();
Branch: 1 branch
BranchTrueFalseTotal
if (state->imm != NULL) state->imm->Unref(); 0 14 14
945 14 state->version->Unref();
946 14 state->mu->Unlock();
947 14 ▶ delete state;
Branch: 1 branch
BranchTrueFalseTotal
delete state; 14 0 14
948 14 }
949 }
950  
951 14 Iterator* DBImpl::NewInternalIterator(const ReadOptions& options,
952 SequenceNumber* latest_snapshot) {
953 14 ▶ IterState* cleanup = new IterState;
Branch: 1 branch
BranchTrueFalseTotal
IterState* cleanup = new IterState; 14 0 14
954 14 ▶ mutex_.Lock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Lock(); 14 0 14
955 14 *latest_snapshot = versions_->LastSequence();
956  
957 // Collect together all needed child iterators
958 14 std::vector<Iterator*> list;
959 14 ▶ list.push_back(mem_->NewIterator());
Branch: 2 branches
BranchTrueFalseTotal
list.push_back(mem_->NewIterator()); 14 0 14
list.push_back(mem_->NewIterator()); 14 0 14
960 14 mem_->Ref();
961 14 ▶ if (imm_ != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (imm_ != NULL) { 0 14 14
962 0 ▶ list.push_back(imm_->NewIterator());
Branch: 2 branches
BranchTrueFalseTotal
list.push_back(imm_->NewIterator()); 0 0 0
list.push_back(imm_->NewIterator()); 0 0 0
963 0 imm_->Ref();
964 }
965 14 ▶ versions_->current()->AddIterators(options, &list);
Branch: 1 branch
BranchTrueFalseTotal
versions_->current()->AddIterators(options, &list); 14 0 14
966 Iterator* internal_iter =
967 14 ▶ NewMergingIterator(&internal_comparator_, &list[0], list.size());
Branch: 1 branch
BranchTrueFalseTotal
NewMergingIterator(&internal_comparator_, &list[0], list.size()); 14 0 14
968 14 ▶ versions_->current()->Ref();
Branch: 1 branch
BranchTrueFalseTotal
versions_->current()->Ref(); 14 0 14
969  
970 14 cleanup->mu = &mutex_;
971 14 cleanup->mem = mem_;
972 14 cleanup->imm = imm_;
973 14 cleanup->version = versions_->current();
974 14 ▶ internal_iter->RegisterCleanup(CleanupIteratorState, cleanup, NULL);
Branch: 1 branch
BranchTrueFalseTotal
internal_iter->RegisterCleanup(CleanupIteratorState, cleanup, NULL); 14 0 14
975  
976 14 ▶ mutex_.Unlock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Unlock(); 14 0 14
977 14 return internal_iter;
978 14 }
979  
980 0 Iterator* DBImpl::TEST_NewInternalIterator() {
981 SequenceNumber ignored;
982 0 ▶ return NewInternalIterator(ReadOptions(), &ignored);
Branch: 1 branch
BranchTrueFalseTotal
return NewInternalIterator(ReadOptions(), &ignored); 0 0 0
983 }
984  
985 0 int64_t DBImpl::TEST_MaxNextLevelOverlappingBytes() {
986 0 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 0 0 0
987 0 ▶ return versions_->MaxNextLevelOverlappingBytes();
Branch: 1 branch
BranchTrueFalseTotal
return versions_->MaxNextLevelOverlappingBytes(); 0 0 0
988 0 }
989  
990 8 Status DBImpl::Get(const ReadOptions& options,
991 const Slice& key,
992 std::string* value) {
993 8 Status s;
994 8 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 8 0 8
995 SequenceNumber snapshot;
996 8 ▶ if (options.snapshot != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (options.snapshot != NULL) { 0 8 8
997 0 snapshot = reinterpret_cast<const SnapshotImpl*>(options.snapshot)->number_;
998 } else {
999 8 snapshot = versions_->LastSequence();
1000 }
1001  
1002 8 MemTable* mem = mem_;
1003 8 MemTable* imm = imm_;
1004 8 Version* current = versions_->current();
1005 8 mem->Ref();
1006 8 ▶ if (imm != NULL) imm->Ref();
Branch: 1 branch
BranchTrueFalseTotal
if (imm != NULL) imm->Ref(); 0 8 8
1007 8 ▶ current->Ref();
Branch: 1 branch
BranchTrueFalseTotal
current->Ref(); 8 0 8
1008  
1009 8 bool have_stat_update = false;
1010 Version::GetStats stats;
1011  
1012 // Unlock while reading from files and memtables
1013 {
1014 8 ▶ mutex_.Unlock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Unlock(); 8 0 8
1015 // First look in the memtable, then in the immutable memtable (if any).
1016 8 ▶ LookupKey lkey(key, snapshot);
Branch: 1 branch
BranchTrueFalseTotal
LookupKey lkey(key, snapshot); 8 0 8
1017 8 ▶ if (mem->Get(lkey, value, &s)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mem->Get(lkey, value, &s)) { 8 0 8
if (mem->Get(lkey, value, &s)) { 2 6 8
1018 // Done
1019 2 ▶ } else if (imm != NULL && imm->Get(lkey, value, &s)) {
Branch: 4 branches
BranchTrueFalseTotal
} else if (imm != NULL && imm->Get(lkey, value, &s)) { 0 2 2
} else if (imm != NULL && imm->Get(lkey, value, &s)) { 0 0 0
} else if (imm != NULL && imm->Get(lkey, value, &s)) { 0 0 0
} else if (imm != NULL && imm->Get(lkey, value, &s)) { 2 0 2
1020 // Done
1021 } else {
1022 2 ▶ s = current->Get(options, lkey, value, &stats);
Branch: 2 branches
BranchTrueFalseTotal
s = current->Get(options, lkey, value, &stats); 2 0 2
s = current->Get(options, lkey, value, &stats); 2 0 2
1023 2 have_stat_update = true;
1024 }
1025 8 ▶ mutex_.Lock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Lock(); 8 0 8
1026 8 }
1027  
1028 8 ▶ if (have_stat_update && current->UpdateStats(stats)) {
Branch: 4 branches
BranchTrueFalseTotal
if (have_stat_update && current->UpdateStats(stats)) { 2 6 8
if (have_stat_update && current->UpdateStats(stats)) { 2 0 2
if (have_stat_update && current->UpdateStats(stats)) { 0 2 2
if (have_stat_update && current->UpdateStats(stats)) { 0 8 8
1029 0 ▶ MaybeScheduleCompaction();
Branch: 1 branch
BranchTrueFalseTotal
MaybeScheduleCompaction(); 0 0 0
1030 }
1031 8 mem->Unref();
1032 8 ▶ if (imm != NULL) imm->Unref();
Branch: 1 branch
BranchTrueFalseTotal
if (imm != NULL) imm->Unref(); 0 8 8
1033 8 ▶ current->Unref();
Branch: 1 branch
BranchTrueFalseTotal
current->Unref(); 8 0 8
1034 16 return s;
1035 8 }
1036  
1037 14 Iterator* DBImpl::NewIterator(const ReadOptions& options) {
1038 SequenceNumber latest_snapshot;
1039 14 ▶ Iterator* internal_iter = NewInternalIterator(options, &latest_snapshot);
Branch: 1 branch
BranchTrueFalseTotal
Iterator* internal_iter = NewInternalIterator(options, &latest_snapshot); 14 0 14
1040 14 return NewDBIterator(
1041 14 ▶ &dbname_, env_, user_comparator(), internal_iter,
Branch: 1 branch
BranchTrueFalseTotal
&dbname_, env_, user_comparator(), internal_iter, 14 0 14
1042 14 ▶ (options.snapshot != NULL
Branch: 1 branch
BranchTrueFalseTotal
(options.snapshot != NULL 0 14 14
1043 0 ? reinterpret_cast<const SnapshotImpl*>(options.snapshot)->number_
1044 28 : latest_snapshot));
1045 }
1046  
1047 0 const Snapshot* DBImpl::GetSnapshot() {
1048 0 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 0 0 0
1049 0 ▶ return snapshots_.New(versions_->LastSequence());
Branch: 1 branch
BranchTrueFalseTotal
return snapshots_.New(versions_->LastSequence()); 0 0 0
1050 0 }
1051  
1052 0 void DBImpl::ReleaseSnapshot(const Snapshot* s) {
1053 0 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 0 0 0
1054 0 snapshots_.Delete(reinterpret_cast<const SnapshotImpl*>(s));
1055 0 }
1056  
1057 // Convenience methods
1058 9 Status DBImpl::Put(const WriteOptions& o, const Slice& key, const Slice& val) {
1059 9 return DB::Put(o, key, val);
1060 }
1061  
1062 8 Status DBImpl::Delete(const WriteOptions& options, const Slice& key) {
1063 8 return DB::Delete(options, key);
1064 }
1065  
1066 // There is at most one thread that is the current logger. This call
1067 // waits until preceding logger(s) have finished and becomes the
1068 // current logger.
1069 18 void DBImpl::AcquireLoggingResponsibility(LoggerId* self) {
1070 18 ▶ while (logger_ != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (logger_ != NULL) { 0 18 18
1071 0 logger_cv_.Wait();
1072 }
1073 18 logger_ = self;
1074 18 }
1075  
1076 18 void DBImpl::ReleaseLoggingResponsibility(LoggerId* self) {
1077 18 ▶ assert(logger_ == self);
Branch: 1 branch
BranchTrueFalseTotal
assert(logger_ == self); 0 18 18
1078 18 logger_ = NULL;
1079 18 logger_cv_.SignalAll();
1080 18 }
1081  
1082 18 Status DBImpl::Write(const WriteOptions& options, WriteBatch* updates) {
1083 18 Status status;
1084 18 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 18 0 18
1085 LoggerId self;
1086 18 ▶ AcquireLoggingResponsibility(&self);
Branch: 1 branch
BranchTrueFalseTotal
AcquireLoggingResponsibility(&self); 18 0 18
1087 18 ▶ status = MakeRoomForWrite(false); // May temporarily release lock and wait
Branch: 2 branches
BranchTrueFalseTotal
status = MakeRoomForWrite(false); // May temporarily release lock and wait 18 0 18
status = MakeRoomForWrite(false); // May temporarily release lock and wait 18 0 18
1088 18 uint64_t last_sequence = versions_->LastSequence();
1089 18 ▶ if (status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (status.ok()) { 18 0 18
1090 18 ▶ WriteBatchInternal::SetSequence(updates, last_sequence + 1);
Branch: 1 branch
BranchTrueFalseTotal
WriteBatchInternal::SetSequence(updates, last_sequence + 1); 18 0 18
1091 18 ▶ last_sequence += WriteBatchInternal::Count(updates);
Branch: 1 branch
BranchTrueFalseTotal
last_sequence += WriteBatchInternal::Count(updates); 18 0 18
1092  
1093 // Add to log and apply to memtable. We can release the lock during
1094 // this phase since the "logger_" flag protects against concurrent
1095 // loggers and concurrent writes into mem_.
1096 {
1097 18 ▶ assert(logger_ == &self);
Branch: 1 branch
BranchTrueFalseTotal
assert(logger_ == &self); 0 18 18
1098 18 ▶ mutex_.Unlock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Unlock(); 18 0 18
1099 18 ▶ status = log_->AddRecord(WriteBatchInternal::Contents(updates));
Branch: 2 branches
BranchTrueFalseTotal
status = log_->AddRecord(WriteBatchInternal::Contents(updates)); 18 0 18
status = log_->AddRecord(WriteBatchInternal::Contents(updates)); 18 0 18
1100 18 ▶ if (status.ok() && options.sync) {
Branch: 3 branches
BranchTrueFalseTotal
if (status.ok() && options.sync) { 18 0 18
if (status.ok() && options.sync) { 0 18 18
if (status.ok() && options.sync) { 0 18 18
1101 0 ▶ status = logfile_->Sync();
Branch: 2 branches
BranchTrueFalseTotal
status = logfile_->Sync(); 0 0 0
status = logfile_->Sync(); 0 0 0
1102 }
1103 18 ▶ if (status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (status.ok()) { 18 0 18
1104 18 ▶ status = WriteBatchInternal::InsertInto(updates, mem_);
Branch: 2 branches
BranchTrueFalseTotal
status = WriteBatchInternal::InsertInto(updates, mem_); 18 0 18
status = WriteBatchInternal::InsertInto(updates, mem_); 18 0 18
1105 }
1106 18 ▶ mutex_.Lock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Lock(); 18 0 18
1107 18 ▶ assert(logger_ == &self);
Branch: 1 branch
BranchTrueFalseTotal
assert(logger_ == &self); 0 18 18
1108 }
1109  
1110 18 versions_->SetLastSequence(last_sequence);
1111 }
1112 18 ▶ if (options.post_write_snapshot != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (options.post_write_snapshot != NULL) { 0 18 18
1113 0 *options.post_write_snapshot =
1114 0 ▶ status.ok() ? snapshots_.New(last_sequence) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
status.ok() ? snapshots_.New(last_sequence) : NULL; 0 0 0
status.ok() ? snapshots_.New(last_sequence) : NULL; 0 0 0
1115 }
1116 18 ▶ ReleaseLoggingResponsibility(&self);
Branch: 1 branch
BranchTrueFalseTotal
ReleaseLoggingResponsibility(&self); 18 0 18
1117 36 return status;
1118 18 }
1119  
1120 // REQUIRES: mutex_ is held
1121 // REQUIRES: this thread is the current logger
1122 18 Status DBImpl::MakeRoomForWrite(bool force) {
1123 18 mutex_.AssertHeld();
1124 18 ▶ assert(logger_ != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(logger_ != NULL); 0 18 18
1125 18 bool allow_delay = !force;
1126 18 Status s;
1127 while (true) {
1128 18 ▶ if (!bg_error_.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bg_error_.ok()) { 0 18 18
1129 // Yield previous error
1130 0 ▶ s = bg_error_;
Branch: 1 branch
BranchTrueFalseTotal
s = bg_error_; 0 0 0
1131 0 break;
1132 18 } else if (
1133 36 ▶ allow_delay &&
Branch: 2 branches
BranchTrueFalseTotal
allow_delay && 18 0 18
allow_delay && 0 18 18
1134 18 ▶ versions_->NumLevelFiles(0) >= config::kL0_SlowdownWritesTrigger) {
Branch: 2 branches
BranchTrueFalseTotal
versions_->NumLevelFiles(0) >= config::kL0_SlowdownWritesTrigger) { 18 0 18
versions_->NumLevelFiles(0) >= config::kL0_SlowdownWritesTrigger) { 0 18 18
1135 // We are getting close to hitting a hard limit on the number of
1136 // L0 files. Rather than delaying a single write by several
1137 // seconds when we hit the hard limit, start delaying each
1138 // individual write by 1ms to reduce latency variance. Also,
1139 // this delay hands over some CPU to the compaction thread in
1140 // case it is sharing the same core as the writer.
1141 0 ▶ mutex_.Unlock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Unlock(); 0 0 0
1142 0 ▶ env_->SleepForMicroseconds(1000);
Branch: 1 branch
BranchTrueFalseTotal
env_->SleepForMicroseconds(1000); 0 0 0
1143 0 allow_delay = false; // Do not delay a single write more than once
1144 0 ▶ mutex_.Lock();
Branch: 1 branch
BranchTrueFalseTotal
mutex_.Lock(); 0 0 0
1145 36 ▶ } else if (!force &&
Branch: 2 branches
BranchTrueFalseTotal
} else if (!force && 18 0 18
} else if (!force && 18 0 18
1146 18 ▶ (mem_->ApproximateMemoryUsage() <= options_.write_buffer_size)) {
Branch: 2 branches
BranchTrueFalseTotal
(mem_->ApproximateMemoryUsage() <= options_.write_buffer_size)) { 18 0 18
(mem_->ApproximateMemoryUsage() <= options_.write_buffer_size)) { 18 0 18
1147 // There is room in current memtable
1148 18 break;
1149 0 ▶ } else if (imm_ != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (imm_ != NULL) { 0 0 0
1150 // We have filled up the current memtable, but the previous
1151 // one is still being compacted, so we wait.
1152 0 ▶ bg_cv_.Wait();
Branch: 1 branch
BranchTrueFalseTotal
bg_cv_.Wait(); 0 0 0
1153 0 ▶ } else if (versions_->NumLevelFiles(0) >= config::kL0_StopWritesTrigger) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (versions_->NumLevelFiles(0) >= config::kL0_StopWritesTrigger) { 0 0 0
} else if (versions_->NumLevelFiles(0) >= config::kL0_StopWritesTrigger) { 0 0 0
1154 // There are too many level-0 files.
1155 0 ▶ Log(options_.info_log, "waiting...\n");
Branch: 1 branch
BranchTrueFalseTotal
Log(options_.info_log, "waiting...\n"); 0 0 0
1156 0 ▶ bg_cv_.Wait();
Branch: 1 branch
BranchTrueFalseTotal
bg_cv_.Wait(); 0 0 0
1157 } else {
1158 // Attempt to switch to a new memtable and trigger compaction of old
1159 0 ▶ assert(versions_->PrevLogNumber() == 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(versions_->PrevLogNumber() == 0); 0 0 0
1160 0 uint64_t new_log_number = versions_->NewFileNumber();
1161 0 WritableFile* lfile = NULL;
1162 0 ▶ s = env_->NewWritableFile(LogFileName(dbname_, new_log_number), &lfile);
Branch: 3 branches
BranchTrueFalseTotal
s = env_->NewWritableFile(LogFileName(dbname_, new_log_number), &lfile); 0 0 0
s = env_->NewWritableFile(LogFileName(dbname_, new_log_number), &lfile); 0 0 0
s = env_->NewWritableFile(LogFileName(dbname_, new_log_number), &lfile); 0 0 0
1163 0 ▶ if (!s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s.ok()) { 0 0 0
1164 0 break;
1165 }
1166 0 ▶ delete log_;
Branch: 1 branch
BranchTrueFalseTotal
delete log_; 0 0 0
1167 0 ▶ delete logfile_;
Branch: 1 branch
BranchTrueFalseTotal
delete logfile_; 0 0 0
1168 0 logfile_ = lfile;
1169 0 logfile_number_ = new_log_number;
1170 0 ▶ log_ = new log::Writer(lfile);
Branch: 3 branches
BranchTrueFalseTotal
log_ = new log::Writer(lfile); 0 0 0
log_ = new log::Writer(lfile); 0 0 0
log_ = new log::Writer(lfile); 0 0 0
1171 0 imm_ = mem_;
1172 0 has_imm_.Release_Store(imm_);
1173 0 ▶ mem_ = new MemTable(internal_comparator_);
Branch: 3 branches
BranchTrueFalseTotal
mem_ = new MemTable(internal_comparator_); 0 0 0
mem_ = new MemTable(internal_comparator_); 0 0 0
mem_ = new MemTable(internal_comparator_); 0 0 0
1174 0 mem_->Ref();
1175 0 force = false; // Do not force another compaction if have room
1176 0 ▶ MaybeScheduleCompaction();
Branch: 1 branch
BranchTrueFalseTotal
MaybeScheduleCompaction(); 0 0 0
1177 }
1178 0 }
1179 18 return s;
1180 }
1181  
1182 0 bool DBImpl::GetProperty(const Slice& property, std::string* value) {
1183 0 value->clear();
1184  
1185 0 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 0 0 0
1186 0 Slice in = property;
1187 0 Slice prefix("leveldb.");
1188 0 ▶ if (!in.starts_with(prefix)) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (!in.starts_with(prefix)) return false; 0 0 0
1189 0 in.remove_prefix(prefix.size());
1190  
1191 0 ▶ if (in.starts_with("num-files-at-level")) {
Branch: 1 branch
BranchTrueFalseTotal
if (in.starts_with("num-files-at-level")) { 0 0 0
1192 0 in.remove_prefix(strlen("num-files-at-level"));
1193 uint64_t level;
1194 0 ▶ bool ok = ConsumeDecimalNumber(&in, &level) && in.empty();
Branch: 3 branches
BranchTrueFalseTotal
bool ok = ConsumeDecimalNumber(&in, &level) && in.empty(); 0 0 0
bool ok = ConsumeDecimalNumber(&in, &level) && in.empty(); 0 0 0
bool ok = ConsumeDecimalNumber(&in, &level) && in.empty(); 0 0 0
1195 0 ▶ if (!ok || level >= config::kNumLevels) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ok || level >= config::kNumLevels) { 0 0 0
if (!ok || level >= config::kNumLevels) { 0 0 0
1196 0 return false;
1197 } else {
1198 char buf[100];
1199 0 snprintf(buf, sizeof(buf), "%d",
1200 0 ▶ versions_->NumLevelFiles(static_cast<int>(level)));
Branch: 1 branch
BranchTrueFalseTotal
versions_->NumLevelFiles(static_cast<int>(level))); 0 0 0
1201 0 ▶ *value = buf;
Branch: 1 branch
BranchTrueFalseTotal
*value = buf; 0 0 0
1202 0 return true;
1203 }
1204 0 ▶ } else if (in == "stats") {
Branch: 1 branch
BranchTrueFalseTotal
} else if (in == "stats") { 0 0 0
1205 char buf[200];
1206 0 snprintf(buf, sizeof(buf),
1207 " Compactions\n"
1208 "Level Files Size(MB) Time(sec) Read(MB) Write(MB)\n"
1209 "--------------------------------------------------\n"
1210 );
1211 0 ▶ value->append(buf);
Branch: 1 branch
BranchTrueFalseTotal
value->append(buf); 0 0 0
1212 0 ▶ for (int level = 0; level < config::kNumLevels; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int level = 0; level < config::kNumLevels; level++) { 0 0 0
1213 0 ▶ int files = versions_->NumLevelFiles(level);
Branch: 1 branch
BranchTrueFalseTotal
int files = versions_->NumLevelFiles(level); 0 0 0
1214 0 ▶ if (stats_[level].micros > 0 || files > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (stats_[level].micros > 0 || files > 0) { 0 0 0
if (stats_[level].micros > 0 || files > 0) { 0 0 0
1215 0 snprintf(
1216 buf, sizeof(buf),
1217 "%3d %8d %8.0f %9.0f %8.0f %9.0f\n",
1218 level,
1219 files,
1220 0 versions_->NumLevelBytes(level) / 1048576.0,
1221 0 stats_[level].micros / 1e6,
1222 0 stats_[level].bytes_read / 1048576.0,
1223 0 ▶ stats_[level].bytes_written / 1048576.0);
Branch: 1 branch
BranchTrueFalseTotal
stats_[level].bytes_written / 1048576.0); 0 0 0
1224 0 ▶ value->append(buf);
Branch: 1 branch
BranchTrueFalseTotal
value->append(buf); 0 0 0
1225 }
1226 }
1227 0 return true;
1228 }
1229  
1230 0 return false;
1231 0 }
1232  
1233 0 void DBImpl::GetApproximateSizes(
1234 const Range* range, int n,
1235 uint64_t* sizes) {
1236 // TODO(opt): better implementation
1237 Version* v;
1238 {
1239 0 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 0 0 0
1240 0 ▶ versions_->current()->Ref();
Branch: 1 branch
BranchTrueFalseTotal
versions_->current()->Ref(); 0 0 0
1241 0 v = versions_->current();
1242 0 }
1243  
1244 0 ▶ for (int i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < n; i++) { 0 0 0
1245 // Convert user_key into a corresponding internal key.
1246 0 ▶ InternalKey k1(range[i].start, kMaxSequenceNumber, kValueTypeForSeek);
Branch: 1 branch
BranchTrueFalseTotal
InternalKey k1(range[i].start, kMaxSequenceNumber, kValueTypeForSeek); 0 0 0
1247 0 ▶ InternalKey k2(range[i].limit, kMaxSequenceNumber, kValueTypeForSeek);
Branch: 1 branch
BranchTrueFalseTotal
InternalKey k2(range[i].limit, kMaxSequenceNumber, kValueTypeForSeek); 0 0 0
1248 0 ▶ uint64_t start = versions_->ApproximateOffsetOf(v, k1);
Branch: 1 branch
BranchTrueFalseTotal
uint64_t start = versions_->ApproximateOffsetOf(v, k1); 0 0 0
1249 0 ▶ uint64_t limit = versions_->ApproximateOffsetOf(v, k2);
Branch: 1 branch
BranchTrueFalseTotal
uint64_t limit = versions_->ApproximateOffsetOf(v, k2); 0 0 0
1250 0 ▶ sizes[i] = (limit >= start ? limit - start : 0);
Branch: 1 branch
BranchTrueFalseTotal
sizes[i] = (limit >= start ? limit - start : 0); 0 0 0
1251 0 }
1252  
1253 {
1254 0 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 0 0 0
1255 0 ▶ v->Unref();
Branch: 1 branch
BranchTrueFalseTotal
v->Unref(); 0 0 0
1256 0 }
1257 0 }
1258  
1259 // Default implementations of convenience methods that subclasses of DB
1260 // can call if they wish
1261 9 Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value) {
1262 9 ▶ WriteBatch batch;
Branch: 1 branch
BranchTrueFalseTotal
WriteBatch batch; 9 0 9
1263 9 ▶ batch.Put(key, value);
Branch: 1 branch
BranchTrueFalseTotal
batch.Put(key, value); 9 0 9
1264 18 ▶ return Write(opt, &batch);
Branch: 1 branch
BranchTrueFalseTotal
return Write(opt, &batch); 9 0 9
1265 9 }
1266  
1267 8 Status DB::Delete(const WriteOptions& opt, const Slice& key) {
1268 8 ▶ WriteBatch batch;
Branch: 1 branch
BranchTrueFalseTotal
WriteBatch batch; 8 0 8
1269 8 ▶ batch.Delete(key);
Branch: 1 branch
BranchTrueFalseTotal
batch.Delete(key); 8 0 8
1270 16 ▶ return Write(opt, &batch);
Branch: 1 branch
BranchTrueFalseTotal
return Write(opt, &batch); 8 0 8
1271 8 }
1272  
1273 0 DB::~DB() { }
1274  
1275 4 Status DB::Open(const Options& options, const std::string& dbname,
1276 DB** dbptr) {
1277 4 *dbptr = NULL;
1278  
1279 4 ▶ DBImpl* impl = new DBImpl(options, dbname);
Branch: 3 branches
BranchTrueFalseTotal
DBImpl* impl = new DBImpl(options, dbname); 4 0 4
DBImpl* impl = new DBImpl(options, dbname); 4 0 4
DBImpl* impl = new DBImpl(options, dbname); 0 0 0
1280 4 ▶ impl->mutex_.Lock();
Branch: 1 branch
BranchTrueFalseTotal
impl->mutex_.Lock(); 4 0 4
1281 4 ▶ VersionEdit edit;
Branch: 1 branch
BranchTrueFalseTotal
VersionEdit edit; 4 0 4
1282 4 ▶ Status s = impl->Recover(&edit); // Handles create_if_missing, error_if_exists
Branch: 1 branch
BranchTrueFalseTotal
Status s = impl->Recover(&edit); // Handles create_if_missing, error_if_exists 4 0 4
1283 4 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 3 1 4
1284 3 uint64_t new_log_number = impl->versions_->NewFileNumber();
1285 WritableFile* lfile;
1286 3 ▶ s = options.env->NewWritableFile(LogFileName(dbname, new_log_number),
Branch: 3 branches
BranchTrueFalseTotal
s = options.env->NewWritableFile(LogFileName(dbname, new_log_number), 3 0 3
s = options.env->NewWritableFile(LogFileName(dbname, new_log_number), 3 0 3
s = options.env->NewWritableFile(LogFileName(dbname, new_log_number), 3 0 3
1287 &lfile);
1288 3 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 3 0 3
1289 3 edit.SetLogNumber(new_log_number);
1290 3 impl->logfile_ = lfile;
1291 3 impl->logfile_number_ = new_log_number;
1292 3 ▶ impl->log_ = new log::Writer(lfile);
Branch: 3 branches
BranchTrueFalseTotal
impl->log_ = new log::Writer(lfile); 3 0 3
impl->log_ = new log::Writer(lfile); 3 0 3
impl->log_ = new log::Writer(lfile); 0 0 0
1293 3 ▶ s = impl->versions_->LogAndApply(&edit, &impl->mutex_);
Branch: 2 branches
BranchTrueFalseTotal
s = impl->versions_->LogAndApply(&edit, &impl->mutex_); 3 0 3
s = impl->versions_->LogAndApply(&edit, &impl->mutex_); 3 0 3
1294 }
1295 3 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 3 0 3
1296 3 ▶ impl->DeleteObsoleteFiles();
Branch: 1 branch
BranchTrueFalseTotal
impl->DeleteObsoleteFiles(); 3 0 3
1297 3 ▶ impl->MaybeScheduleCompaction();
Branch: 1 branch
BranchTrueFalseTotal
impl->MaybeScheduleCompaction(); 3 0 3
1298 }
1299 }
1300 4 ▶ impl->mutex_.Unlock();
Branch: 1 branch
BranchTrueFalseTotal
impl->mutex_.Unlock(); 4 0 4
1301 4 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 3 1 4
1302 3 *dbptr = impl;
1303 } else {
1304 1 ▶ delete impl;
Branch: 1 branch
BranchTrueFalseTotal
delete impl; 1 0 1
1305 }
1306 8 return s;
1307 4 }
1308  
1309 0 Snapshot::~Snapshot() {
1310 0 }
1311  
1312 0 Status DestroyDB(const std::string& dbname, const Options& options) {
1313 0 Env* env = options.env;
1314 0 std::vector<std::string> filenames;
1315 // Ignore error in case directory does not exist
1316 0 ▶ env->GetChildren(dbname, &filenames);
Branch: 1 branch
BranchTrueFalseTotal
env->GetChildren(dbname, &filenames); 0 0 0
1317 0 ▶ if (filenames.empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (filenames.empty()) { 0 0 0
1318 0 return Status::OK();
1319 }
1320  
1321 FileLock* lock;
1322 0 ▶ const std::string lockname = LockFileName(dbname);
Branch: 1 branch
BranchTrueFalseTotal
const std::string lockname = LockFileName(dbname); 0 0 0
1323 0 ▶ Status result = env->LockFile(lockname, &lock);
Branch: 1 branch
BranchTrueFalseTotal
Status result = env->LockFile(lockname, &lock); 0 0 0
1324 0 ▶ if (result.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (result.ok()) { 0 0 0
1325 uint64_t number;
1326 FileType type;
1327 0 ▶ for (size_t i = 0; i < filenames.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < filenames.size(); i++) { 0 0 0
1328 0 ▶ if (ParseFileName(filenames[i], &number, &type) &&
Branch: 2 branches
BranchTrueFalseTotal
if (ParseFileName(filenames[i], &number, &type) && 0 0 0
if (ParseFileName(filenames[i], &number, &type) && 0 0 0
1329 0 filenames[i] != lockname) { // Lock file will be deleted at end
1330 0 ▶ Status del = env->DeleteFile(dbname + "/" + filenames[i]);
Branch: 3 branches
BranchTrueFalseTotal
Status del = env->DeleteFile(dbname + "/" + filenames[i]); 0 0 0
Status del = env->DeleteFile(dbname + "/" + filenames[i]); 0 0 0
Status del = env->DeleteFile(dbname + "/" + filenames[i]); 0 0 0
1331 0 ▶ if (result.ok() && !del.ok()) {
Branch: 3 branches
BranchTrueFalseTotal
if (result.ok() && !del.ok()) { 0 0 0
if (result.ok() && !del.ok()) { 0 0 0
if (result.ok() && !del.ok()) { 0 0 0
1332 0 ▶ result = del;
Branch: 1 branch
BranchTrueFalseTotal
result = del; 0 0 0
1333 }
1334 0 }
1335 }
1336 0 ▶ env->UnlockFile(lock); // Ignore error since state is already gone
Branch: 1 branch
BranchTrueFalseTotal
env->UnlockFile(lock); // Ignore error since state is already gone 0 0 0
1337 0 ▶ env->DeleteFile(lockname);
Branch: 1 branch
BranchTrueFalseTotal
env->DeleteFile(lockname); 0 0 0
1338 0 ▶ env->DeleteDir(dbname); // Ignore error in case dir contains other files
Branch: 1 branch
BranchTrueFalseTotal
env->DeleteDir(dbname); // Ignore error in case dir contains other files 0 0 0
1339 }
1340 0 ▶ return result;
Branch: 1 branch
BranchTrueFalseTotal
return result; 0 0 0
1341 0 }
1342  
1343 }
« db/version_set.cc ↑ Summary db/log_reader.cc »