db/repair.cc

filter: Statementstmt 0.0% 0 / 211 Branchbran 0.0% 0 / 298 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 509 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary include/leveldb/options.h »
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 // We recover the contents of the descriptor from the other files we find.
6 // (1) Any log files are first converted to tables
7 // (2) We scan every table to compute
8 // (a) smallest/largest for the table
9 // (b) largest sequence number in the table
10 // (3) We generate descriptor contents:
11 // - log number is set to zero
12 // - next-file-number is set to 1 + largest file number we found
13 // - last-sequence-number is set to largest sequence# found across
14 // all tables (see 2c)
15 // - compaction pointers are cleared
16 // - every table file is added at level 0
17 //
18 // Possible optimization 1:
19 // (a) Compute total size and use to pick appropriate max-level M
20 // (b) Sort tables by largest sequence# in the table
21 // (c) For each table: if it overlaps earlier table, place in level-0,
22 // else place in level-M.
23 // Possible optimization 2:
24 // Store per-table metadata (smallest, largest, largest-seq#, ...)
25 // in the table's meta section to speed up ScanTable.
26  
27 #include "db/builder.h"
28 #include "db/db_impl.h"
29 #include "db/dbformat.h"
30 #include "db/filename.h"
31 #include "db/log_reader.h"
32 #include "db/log_writer.h"
33 #include "db/memtable.h"
34 #include "db/table_cache.h"
35 #include "db/version_edit.h"
36 #include "db/write_batch_internal.h"
37 #include "leveldb/comparator.h"
38 #include "leveldb/db.h"
39 #include "leveldb/env.h"
40  
41 namespace leveldb {
42  
43 namespace {
44  
45 class Repairer {
46 public:
47 0 Repairer(const std::string& dbname, const Options& options)
48 0 : dbname_(dbname),
49 0 env_(options.env),
50 0 icmp_(options.comparator),
51 0 ▶ options_(SanitizeOptions(dbname, &icmp_, options)),
Branch: 1 branch
BranchTrueFalseTotal
options_(SanitizeOptions(dbname, &icmp_, options)), 0 0 0
52 0 owns_info_log_(options_.info_log != options.info_log),
53 0 owns_cache_(options_.block_cache != options.block_cache),
54 0 ▶ next_file_number_(1) {
Branch: 1 branch
BranchTrueFalseTotal
next_file_number_(1) { 0 0 0
55 // TableCache can be small since we expect each table to be opened once.
56 0 ▶ table_cache_ = new TableCache(dbname_, &options_, 10);
Branch: 3 branches
BranchTrueFalseTotal
table_cache_ = new TableCache(dbname_, &options_, 10); 0 0 0
table_cache_ = new TableCache(dbname_, &options_, 10); 0 0 0
table_cache_ = new TableCache(dbname_, &options_, 10); 0 0 0
57 0 }
58  
59 0 ~Repairer() {
60 0 ▶ delete table_cache_;
Branch: 1 branch
BranchTrueFalseTotal
delete table_cache_; 0 0 0
61 0 ▶ if (owns_info_log_) {
Branch: 1 branch
BranchTrueFalseTotal
if (owns_info_log_) { 0 0 0
62 0 ▶ delete options_.info_log;
Branch: 1 branch
BranchTrueFalseTotal
delete options_.info_log; 0 0 0
63 }
64 0 ▶ if (owns_cache_) {
Branch: 1 branch
BranchTrueFalseTotal
if (owns_cache_) { 0 0 0
65 0 ▶ delete options_.block_cache;
Branch: 1 branch
BranchTrueFalseTotal
delete options_.block_cache; 0 0 0
66 }
67 0 }
68  
69 0 Status Run() {
70 0 Status status = FindFiles();
71 0 ▶ if (status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (status.ok()) { 0 0 0
72 0 ▶ ConvertLogFilesToTables();
Branch: 1 branch
BranchTrueFalseTotal
ConvertLogFilesToTables(); 0 0 0
73 0 ▶ ExtractMetaData();
Branch: 1 branch
BranchTrueFalseTotal
ExtractMetaData(); 0 0 0
74 0 ▶ status = WriteDescriptor();
Branch: 2 branches
BranchTrueFalseTotal
status = WriteDescriptor(); 0 0 0
status = WriteDescriptor(); 0 0 0
75 }
76 0 ▶ if (status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (status.ok()) { 0 0 0
77 0 unsigned long long bytes = 0;
78 0 ▶ for (size_t i = 0; i < tables_.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < tables_.size(); i++) { 0 0 0
79 0 bytes += tables_[i].meta.file_size;
80 }
81 0 ▶ Log(options_.info_log,
Branch: 1 branch
BranchTrueFalseTotal
Log(options_.info_log, 0 0 0
82 "**** Repaired leveldb %s; "
83 "recovered %d files; %llu bytes. "
84 "Some data may have been lost. "
85 "****",
86 dbname_.c_str(),
87 0 static_cast<int>(tables_.size()),
88 bytes);
89 }
90 0 return status;
91 }
92  
93 private:
94 struct TableInfo {
95 FileMetaData meta;
96 SequenceNumber max_sequence;
97 };
98  
99 std::string const dbname_;
100 Env* const env_;
101 InternalKeyComparator const icmp_;
102 Options const options_;
103 bool owns_info_log_;
104 bool owns_cache_;
105 TableCache* table_cache_;
106 VersionEdit edit_;
107  
108 std::vector<std::string> manifests_;
109 std::vector<uint64_t> table_numbers_;
110 std::vector<uint64_t> logs_;
111 std::vector<TableInfo> tables_;
112 uint64_t next_file_number_;
113  
114 0 Status FindFiles() {
115 0 std::vector<std::string> filenames;
116 0 ▶ Status status = env_->GetChildren(dbname_, &filenames);
Branch: 1 branch
BranchTrueFalseTotal
Status status = env_->GetChildren(dbname_, &filenames); 0 0 0
117 0 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 0 0
118 0 ▶ return status;
Branch: 1 branch
BranchTrueFalseTotal
return status; 0 0 0
119 }
120 0 ▶ if (filenames.empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (filenames.empty()) { 0 0 0
121 0 ▶ return Status::IOError(dbname_, "repair found no files");
Branch: 1 branch
BranchTrueFalseTotal
return Status::IOError(dbname_, "repair found no files"); 0 0 0
122 }
123  
124 uint64_t number;
125 FileType type;
126 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
127 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
128 0 ▶ if (type == kDescriptorFile) {
Branch: 1 branch
BranchTrueFalseTotal
if (type == kDescriptorFile) { 0 0 0
129 0 ▶ manifests_.push_back(filenames[i]);
Branch: 1 branch
BranchTrueFalseTotal
manifests_.push_back(filenames[i]); 0 0 0
130 } else {
131 0 ▶ if (number + 1 > next_file_number_) {
Branch: 1 branch
BranchTrueFalseTotal
if (number + 1 > next_file_number_) { 0 0 0
132 0 next_file_number_ = number + 1;
133 }
134 0 ▶ if (type == kLogFile) {
Branch: 1 branch
BranchTrueFalseTotal
if (type == kLogFile) { 0 0 0
135 0 ▶ logs_.push_back(number);
Branch: 1 branch
BranchTrueFalseTotal
logs_.push_back(number); 0 0 0
136 0 ▶ } else if (type == kTableFile) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (type == kTableFile) { 0 0 0
137 0 ▶ table_numbers_.push_back(number);
Branch: 1 branch
BranchTrueFalseTotal
table_numbers_.push_back(number); 0 0 0
138 } else {
139 // Ignore other files
140 }
141 }
142 }
143 }
144 0 ▶ return status;
Branch: 1 branch
BranchTrueFalseTotal
return status; 0 0 0
145 0 }
146  
147 0 void ConvertLogFilesToTables() {
148 0 ▶ for (size_t i = 0; i < logs_.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < logs_.size(); i++) { 0 0 0
149 0 ▶ std::string logname = LogFileName(dbname_, logs_[i]);
Branch: 1 branch
BranchTrueFalseTotal
std::string logname = LogFileName(dbname_, logs_[i]); 0 0 0
150 0 ▶ Status status = ConvertLogToTable(logs_[i]);
Branch: 1 branch
BranchTrueFalseTotal
Status status = ConvertLogToTable(logs_[i]); 0 0 0
151 0 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 0 0
152 0 ▶ Log(options_.info_log, "Log #%llu: ignoring conversion error: %s",
Branch: 1 branch
BranchTrueFalseTotal
Log(options_.info_log, "Log #%llu: ignoring conversion error: %s", 0 0 0
153 0 (unsigned long long) logs_[i],
154 0 ▶ status.ToString().c_str());
Branch: 1 branch
BranchTrueFalseTotal
status.ToString().c_str()); 0 0 0
155 }
156 0 ▶ ArchiveFile(logname);
Branch: 1 branch
BranchTrueFalseTotal
ArchiveFile(logname); 0 0 0
157 0 }
158 0 }
159  
160 0 Status ConvertLogToTable(uint64_t log) {
161 struct LogReporter : public log::Reader::Reporter {
162 Env* env;
163 Logger* info_log;
164 uint64_t lognum;
165 0 virtual void Corruption(size_t bytes, const Status& s) {
166 // We print error messages for corruption, but continue repairing.
167 0 Log(info_log, "Log #%llu: dropping %d bytes; %s",
168 0 ▶ (unsigned long long) lognum,
Branch: 1 branch
BranchTrueFalseTotal
(unsigned long long) lognum, 0 0 0
169 static_cast<int>(bytes),
170 0 ▶ s.ToString().c_str());
Branch: 1 branch
BranchTrueFalseTotal
s.ToString().c_str()); 0 0 0
171 0 }
172 };
173  
174 // Open the log file
175 0 ▶ std::string logname = LogFileName(dbname_, log);
Branch: 1 branch
BranchTrueFalseTotal
std::string logname = LogFileName(dbname_, log); 0 0 0
176 SequentialFile* lfile;
177 0 ▶ Status status = env_->NewSequentialFile(logname, &lfile);
Branch: 1 branch
BranchTrueFalseTotal
Status status = env_->NewSequentialFile(logname, &lfile); 0 0 0
178 0 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 0 0
179 0 return status;
180 }
181  
182 // Create the log reader.
183 0 LogReporter reporter;
184 0 reporter.env = env_;
185 0 reporter.info_log = options_.info_log;
186 0 reporter.lognum = log;
187 // We intentially make log::Reader do checksumming so that
188 // corruptions cause entire commits to be skipped instead of
189 // propagating bad information (like overly large sequence
190 // numbers).
191 log::Reader reader(lfile, &reporter, false/*do not checksum*/,
192 0 ▶ 0/*initial_offset*/);
Branch: 1 branch
BranchTrueFalseTotal
0/*initial_offset*/); 0 0 0
193  
194 // Read all the records and add to a memtable
195 0 std::string scratch;
196 0 Slice record;
197 0 ▶ WriteBatch batch;
Branch: 1 branch
BranchTrueFalseTotal
WriteBatch batch; 0 0 0
198 0 ▶ MemTable* mem = new MemTable(icmp_);
Branch: 3 branches
BranchTrueFalseTotal
MemTable* mem = new MemTable(icmp_); 0 0 0
MemTable* mem = new MemTable(icmp_); 0 0 0
MemTable* mem = new MemTable(icmp_); 0 0 0
199 0 mem->Ref();
200 0 int counter = 0;
201 0 ▶ while (reader.ReadRecord(&record, &scratch)) {
Branch: 2 branches
BranchTrueFalseTotal
while (reader.ReadRecord(&record, &scratch)) { 0 0 0
while (reader.ReadRecord(&record, &scratch)) { 0 0 0
202 0 ▶ if (record.size() < 12) {
Branch: 1 branch
BranchTrueFalseTotal
if (record.size() < 12) { 0 0 0
203 0 ▶ reporter.Corruption(
Branch: 1 branch
BranchTrueFalseTotal
reporter.Corruption( 0 0 0
204 0 ▶ record.size(), Status::Corruption("log record too small"));
Branch: 1 branch
BranchTrueFalseTotal
record.size(), Status::Corruption("log record too small")); 0 0 0
205 0 continue;
206 }
207 0 ▶ WriteBatchInternal::SetContents(&batch, record);
Branch: 1 branch
BranchTrueFalseTotal
WriteBatchInternal::SetContents(&batch, record); 0 0 0
208 0 ▶ status = WriteBatchInternal::InsertInto(&batch, mem);
Branch: 2 branches
BranchTrueFalseTotal
status = WriteBatchInternal::InsertInto(&batch, mem); 0 0 0
status = WriteBatchInternal::InsertInto(&batch, mem); 0 0 0
209 0 ▶ if (status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (status.ok()) { 0 0 0
210 0 ▶ counter += WriteBatchInternal::Count(&batch);
Branch: 1 branch
BranchTrueFalseTotal
counter += WriteBatchInternal::Count(&batch); 0 0 0
211 } else {
212 0 ▶ Log(options_.info_log, "Log #%llu: ignoring %s",
Branch: 1 branch
BranchTrueFalseTotal
Log(options_.info_log, "Log #%llu: ignoring %s", 0 0 0
213 (unsigned long long) log,
214 0 ▶ status.ToString().c_str());
Branch: 1 branch
BranchTrueFalseTotal
status.ToString().c_str()); 0 0 0
215 0 ▶ status = Status::OK(); // Keep going with rest of file
Branch: 1 branch
BranchTrueFalseTotal
status = Status::OK(); // Keep going with rest of file 0 0 0
216 }
217 }
218 0 ▶ delete lfile;
Branch: 1 branch
BranchTrueFalseTotal
delete lfile; 0 0 0
219  
220 // Do not record a version edit for this conversion to a Table
221 // since ExtractMetaData() will also generate edits.
222 0 FileMetaData meta;
223 0 meta.number = next_file_number_++;
224 0 ▶ Iterator* iter = mem->NewIterator();
Branch: 1 branch
BranchTrueFalseTotal
Iterator* iter = mem->NewIterator(); 0 0 0
225 0 ▶ status = BuildTable(dbname_, env_, options_, table_cache_, iter, &meta);
Branch: 2 branches
BranchTrueFalseTotal
status = BuildTable(dbname_, env_, options_, table_cache_, iter, &meta); 0 0 0
status = BuildTable(dbname_, env_, options_, table_cache_, iter, &meta); 0 0 0
226 0 ▶ delete iter;
Branch: 1 branch
BranchTrueFalseTotal
delete iter; 0 0 0
227 0 mem->Unref();
228 0 mem = NULL;
229 0 ▶ if (status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (status.ok()) { 0 0 0
230 0 ▶ if (meta.file_size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta.file_size > 0) { 0 0 0
231 0 ▶ table_numbers_.push_back(meta.number);
Branch: 1 branch
BranchTrueFalseTotal
table_numbers_.push_back(meta.number); 0 0 0
232 }
233 }
234 0 Log(options_.info_log, "Log #%llu: %d ops saved to Table #%llu %s",
235 (unsigned long long) log,
236 counter,
237 0 ▶ (unsigned long long) meta.number,
Branch: 1 branch
BranchTrueFalseTotal
(unsigned long long) meta.number, 0 0 0
238 0 ▶ status.ToString().c_str());
Branch: 1 branch
BranchTrueFalseTotal
status.ToString().c_str()); 0 0 0
239 0 return status;
240 0 }
241  
242 0 void ExtractMetaData() {
243 0 std::vector<TableInfo> kept;
244 0 ▶ for (size_t i = 0; i < table_numbers_.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < table_numbers_.size(); i++) { 0 0 0
245 0 TableInfo t;
246 0 t.meta.number = table_numbers_[i];
247 0 ▶ Status status = ScanTable(&t);
Branch: 1 branch
BranchTrueFalseTotal
Status status = ScanTable(&t); 0 0 0
248 0 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 0 0
249 0 ▶ std::string fname = TableFileName(dbname_, table_numbers_[i]);
Branch: 1 branch
BranchTrueFalseTotal
std::string fname = TableFileName(dbname_, table_numbers_[i]); 0 0 0
250 0 ▶ Log(options_.info_log, "Table #%llu: ignoring %s",
Branch: 1 branch
BranchTrueFalseTotal
Log(options_.info_log, "Table #%llu: ignoring %s", 0 0 0
251 0 (unsigned long long) table_numbers_[i],
252 0 ▶ status.ToString().c_str());
Branch: 1 branch
BranchTrueFalseTotal
status.ToString().c_str()); 0 0 0
253 0 ▶ ArchiveFile(fname);
Branch: 1 branch
BranchTrueFalseTotal
ArchiveFile(fname); 0 0 0
254 0 } else {
255 0 ▶ tables_.push_back(t);
Branch: 1 branch
BranchTrueFalseTotal
tables_.push_back(t); 0 0 0
256 }
257 0 }
258 0 }
259  
260 0 Status ScanTable(TableInfo* t) {
261 0 ▶ std::string fname = TableFileName(dbname_, t->meta.number);
Branch: 1 branch
BranchTrueFalseTotal
std::string fname = TableFileName(dbname_, t->meta.number); 0 0 0
262 0 int counter = 0;
263 0 ▶ Status status = env_->GetFileSize(fname, &t->meta.file_size);
Branch: 1 branch
BranchTrueFalseTotal
Status status = env_->GetFileSize(fname, &t->meta.file_size); 0 0 0
264 0 ▶ if (status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (status.ok()) { 0 0 0
265 0 ▶ Iterator* iter = table_cache_->NewIterator(
Branch: 1 branch
BranchTrueFalseTotal
Iterator* iter = table_cache_->NewIterator( 0 0 0
266 0 ReadOptions(), t->meta.number, t->meta.file_size);
267 0 bool empty = true;
268 0 ParsedInternalKey parsed;
269 0 t->max_sequence = 0;
270 0 ▶ for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
Branch: 4 branches
BranchTrueFalseTotal
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { 0 0 0
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { 0 0 0
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { 0 0 0
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { 0 0 0
271 0 ▶ Slice key = iter->key();
Branch: 1 branch
BranchTrueFalseTotal
Slice key = iter->key(); 0 0 0
272 0 ▶ if (!ParseInternalKey(key, &parsed)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ParseInternalKey(key, &parsed)) { 0 0 0
if (!ParseInternalKey(key, &parsed)) { 0 0 0
273 0 Log(options_.info_log, "Table #%llu: unparsable key %s",
274 0 ▶ (unsigned long long) t->meta.number,
Branch: 1 branch
BranchTrueFalseTotal
(unsigned long long) t->meta.number, 0 0 0
275 0 ▶ EscapeString(key).c_str());
Branch: 1 branch
BranchTrueFalseTotal
EscapeString(key).c_str()); 0 0 0
276 0 continue;
277 }
278  
279 0 counter++;
280 0 ▶ if (empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (empty) { 0 0 0
281 0 empty = false;
282 0 ▶ t->meta.smallest.DecodeFrom(key);
Branch: 1 branch
BranchTrueFalseTotal
t->meta.smallest.DecodeFrom(key); 0 0 0
283 }
284 0 ▶ t->meta.largest.DecodeFrom(key);
Branch: 1 branch
BranchTrueFalseTotal
t->meta.largest.DecodeFrom(key); 0 0 0
285 0 ▶ if (parsed.sequence > t->max_sequence) {
Branch: 1 branch
BranchTrueFalseTotal
if (parsed.sequence > t->max_sequence) { 0 0 0
286 0 t->max_sequence = parsed.sequence;
287 }
288 }
289 0 ▶ if (!iter->status().ok()) {
Branch: 2 branches
BranchTrueFalseTotal
if (!iter->status().ok()) { 0 0 0
if (!iter->status().ok()) { 0 0 0
290 0 ▶ status = iter->status();
Branch: 2 branches
BranchTrueFalseTotal
status = iter->status(); 0 0 0
status = iter->status(); 0 0 0
291 }
292 0 ▶ delete iter;
Branch: 1 branch
BranchTrueFalseTotal
delete iter; 0 0 0
293 }
294 0 Log(options_.info_log, "Table #%llu: %d entries %s",
295 0 ▶ (unsigned long long) t->meta.number,
Branch: 1 branch
BranchTrueFalseTotal
(unsigned long long) t->meta.number, 0 0 0
296 counter,
297 0 ▶ status.ToString().c_str());
Branch: 1 branch
BranchTrueFalseTotal
status.ToString().c_str()); 0 0 0
298 0 return status;
299 0 }
300  
301 0 Status WriteDescriptor() {
302 0 ▶ std::string tmp = TempFileName(dbname_, 1);
Branch: 1 branch
BranchTrueFalseTotal
std::string tmp = TempFileName(dbname_, 1); 0 0 0
303 WritableFile* file;
304 0 ▶ Status status = env_->NewWritableFile(tmp, &file);
Branch: 1 branch
BranchTrueFalseTotal
Status status = env_->NewWritableFile(tmp, &file); 0 0 0
305 0 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 0 0
306 0 return status;
307 }
308  
309 0 SequenceNumber max_sequence = 0;
310 0 ▶ for (size_t i = 0; i < tables_.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < tables_.size(); i++) { 0 0 0
311 0 ▶ if (max_sequence < tables_[i].max_sequence) {
Branch: 1 branch
BranchTrueFalseTotal
if (max_sequence < tables_[i].max_sequence) { 0 0 0
312 0 max_sequence = tables_[i].max_sequence;
313 }
314 }
315  
316 0 ▶ edit_.SetComparatorName(icmp_.user_comparator()->Name());
Branch: 2 branches
BranchTrueFalseTotal
edit_.SetComparatorName(icmp_.user_comparator()->Name()); 0 0 0
edit_.SetComparatorName(icmp_.user_comparator()->Name()); 0 0 0
317 0 edit_.SetLogNumber(0);
318 0 edit_.SetNextFile(next_file_number_);
319 0 edit_.SetLastSequence(max_sequence);
320  
321 0 ▶ for (size_t i = 0; i < tables_.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < tables_.size(); i++) { 0 0 0
322 // TODO(opt): separate out into multiple levels
323 0 const TableInfo& t = tables_[i];
324 0 edit_.AddFile(0, t.meta.number, t.meta.file_size,
325 0 ▶ t.meta.smallest, t.meta.largest);
Branch: 1 branch
BranchTrueFalseTotal
t.meta.smallest, t.meta.largest); 0 0 0
326 }
327  
328 //fprintf(stderr, "NewDescriptor:\n%s\n", edit_.DebugString().c_str());
329 {
330 0 ▶ log::Writer log(file);
Branch: 1 branch
BranchTrueFalseTotal
log::Writer log(file); 0 0 0
331 0 std::string record;
332 0 ▶ edit_.EncodeTo(&record);
Branch: 1 branch
BranchTrueFalseTotal
edit_.EncodeTo(&record); 0 0 0
333 0 ▶ status = log.AddRecord(record);
Branch: 2 branches
BranchTrueFalseTotal
status = log.AddRecord(record); 0 0 0
status = log.AddRecord(record); 0 0 0
334 0 }
335 0 ▶ if (status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (status.ok()) { 0 0 0
336 0 ▶ status = file->Close();
Branch: 2 branches
BranchTrueFalseTotal
status = file->Close(); 0 0 0
status = file->Close(); 0 0 0
337 }
338 0 ▶ delete file;
Branch: 1 branch
BranchTrueFalseTotal
delete file; 0 0 0
339 0 file = NULL;
340  
341 0 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 0 0
342 0 ▶ env_->DeleteFile(tmp);
Branch: 1 branch
BranchTrueFalseTotal
env_->DeleteFile(tmp); 0 0 0
343 } else {
344 // Discard older manifests
345 0 ▶ for (size_t i = 0; i < manifests_.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < manifests_.size(); i++) { 0 0 0
346 0 ▶ ArchiveFile(dbname_ + "/" + manifests_[i]);
Branch: 3 branches
BranchTrueFalseTotal
ArchiveFile(dbname_ + "/" + manifests_[i]); 0 0 0
ArchiveFile(dbname_ + "/" + manifests_[i]); 0 0 0
ArchiveFile(dbname_ + "/" + manifests_[i]); 0 0 0
347 }
348  
349 // Install new manifest
350 0 ▶ status = env_->RenameFile(tmp, DescriptorFileName(dbname_, 1));
Branch: 3 branches
BranchTrueFalseTotal
status = env_->RenameFile(tmp, DescriptorFileName(dbname_, 1)); 0 0 0
status = env_->RenameFile(tmp, DescriptorFileName(dbname_, 1)); 0 0 0
status = env_->RenameFile(tmp, DescriptorFileName(dbname_, 1)); 0 0 0
351 0 ▶ if (status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (status.ok()) { 0 0 0
352 0 ▶ status = SetCurrentFile(env_, dbname_, 1);
Branch: 2 branches
BranchTrueFalseTotal
status = SetCurrentFile(env_, dbname_, 1); 0 0 0
status = SetCurrentFile(env_, dbname_, 1); 0 0 0
353 } else {
354 0 ▶ env_->DeleteFile(tmp);
Branch: 1 branch
BranchTrueFalseTotal
env_->DeleteFile(tmp); 0 0 0
355 }
356 }
357 0 return status;
358 0 }
359  
360 0 void ArchiveFile(const std::string& fname) {
361 // Move into another directory. E.g., for
362 // dir/foo
363 // rename to
364 // dir/lost/foo
365 0 const char* slash = strrchr(fname.c_str(), '/');
366 0 std::string new_dir;
367 0 ▶ if (slash != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (slash != NULL) { 0 0 0
368 0 ▶ new_dir.assign(fname.data(), slash - fname.data());
Branch: 1 branch
BranchTrueFalseTotal
new_dir.assign(fname.data(), slash - fname.data()); 0 0 0
369 }
370 0 ▶ new_dir.append("/lost");
Branch: 1 branch
BranchTrueFalseTotal
new_dir.append("/lost"); 0 0 0
371 0 ▶ env_->CreateDir(new_dir); // Ignore error
Branch: 1 branch
BranchTrueFalseTotal
env_->CreateDir(new_dir); // Ignore error 0 0 0
372 0 ▶ std::string new_file = new_dir;
Branch: 1 branch
BranchTrueFalseTotal
std::string new_file = new_dir; 0 0 0
373 0 ▶ new_file.append("/");
Branch: 1 branch
BranchTrueFalseTotal
new_file.append("/"); 0 0 0
374 0 ▶ new_file.append((slash == NULL) ? fname.c_str() : slash + 1);
Branch: 2 branches
BranchTrueFalseTotal
new_file.append((slash == NULL) ? fname.c_str() : slash + 1); 0 0 0
new_file.append((slash == NULL) ? fname.c_str() : slash + 1); 0 0 0
375 0 ▶ Status s = env_->RenameFile(fname, new_file);
Branch: 1 branch
BranchTrueFalseTotal
Status s = env_->RenameFile(fname, new_file); 0 0 0
376 0 ▶ Log(options_.info_log, "Archiving %s: %s\n",
Branch: 1 branch
BranchTrueFalseTotal
Log(options_.info_log, "Archiving %s: %s\n", 0 0 0
377 0 ▶ fname.c_str(), s.ToString().c_str());
Branch: 1 branch
BranchTrueFalseTotal
fname.c_str(), s.ToString().c_str()); 0 0 0
378 0 }
379 };
380 }
381  
382 0 Status RepairDB(const std::string& dbname, const Options& options) {
383 0 ▶ Repairer repairer(dbname, options);
Branch: 1 branch
BranchTrueFalseTotal
Repairer repairer(dbname, options); 0 0 0
384 0 ▶ return repairer.Run();
Branch: 1 branch
BranchTrueFalseTotal
return repairer.Run(); 0 0 0
385 0 }
386  
387 }
↑ Summary include/leveldb/options.h »