| 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/builder.h" |
| 6 |
|
| |
| 7 |
|
| #include "db/filename.h" |
| 8 |
|
| #include "db/dbformat.h" |
| 9 |
|
| #include "db/table_cache.h" |
| 10 |
|
| #include "db/version_edit.h" |
| 11 |
|
| #include "leveldb/db.h" |
| 12 |
|
| #include "leveldb/env.h" |
| 13 |
|
| #include "leveldb/iterator.h" |
| 14 |
|
| |
| 15 |
|
| namespace leveldb { |
| 16 |
|
| |
| 17 |
1 |
| Status BuildTable(const std::string& dbname, |
| 18 |
|
| Env* env, |
| 19 |
|
| const Options& options, |
| 20 |
|
| TableCache* table_cache, |
| 21 |
|
| Iterator* iter, |
| 22 |
|
| FileMetaData* meta) { |
| 23 |
1 |
| Status s; |
| 24 |
1 |
| meta->file_size = 0; |
| 25 |
1 |
▶ | iter->SeekToFirst(); |
Branch: 1 branch
| Branch | True | False | Total |
| iter->SeekToFirst(); |
1 |
0 |
1 |
|
| 26 |
|
| |
| 27 |
1 |
▶ | std::string fname = TableFileName(dbname, meta->number); |
Branch: 1 branch
| Branch | True | False | Total |
| std::string fname = TableFileName(dbname, meta->number); |
1 |
0 |
1 |
|
| 28 |
1 |
▶ | if (iter->Valid()) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (iter->Valid()) { |
1 |
0 |
1 |
| if (iter->Valid()) { |
1 |
0 |
1 |
|
| 29 |
|
| WritableFile* file; |
| 30 |
1 |
▶ | s = env->NewWritableFile(fname, &file); |
Branch: 2 branches
| Branch | True | False | Total |
| s = env->NewWritableFile(fname, &file); |
1 |
0 |
1 |
| s = env->NewWritableFile(fname, &file); |
1 |
0 |
1 |
|
| 31 |
1 |
▶ | if (!s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s.ok()) { |
0 |
1 |
1 |
|
| 32 |
0 |
| return s; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
1 |
▶ | TableBuilder* builder = new TableBuilder(options, file); |
Branch: 3 branches
| Branch | True | False | Total |
| TableBuilder* builder = new TableBuilder(options, file); |
1 |
0 |
1 |
| TableBuilder* builder = new TableBuilder(options, file); |
1 |
0 |
1 |
| TableBuilder* builder = new TableBuilder(options, file); |
0 |
0 |
0 |
|
| 36 |
1 |
▶ | meta->smallest.DecodeFrom(iter->key()); |
Branch: 2 branches
| Branch | True | False | Total |
| meta->smallest.DecodeFrom(iter->key()); |
1 |
0 |
1 |
| meta->smallest.DecodeFrom(iter->key()); |
1 |
0 |
1 |
|
| 37 |
14 |
▶ | for (; iter->Valid(); iter->Next()) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (; iter->Valid(); iter->Next()) { |
14 |
0 |
14 |
| for (; iter->Valid(); iter->Next()) { |
13 |
1 |
14 |
|
| 38 |
13 |
▶ | Slice key = iter->key(); |
Branch: 1 branch
| Branch | True | False | Total |
| Slice key = iter->key(); |
13 |
0 |
13 |
|
| 39 |
13 |
▶ | meta->largest.DecodeFrom(key); |
Branch: 1 branch
| Branch | True | False | Total |
| meta->largest.DecodeFrom(key); |
13 |
0 |
13 |
|
| 40 |
13 |
▶ | builder->Add(key, iter->value()); |
Branch: 3 branches
| Branch | True | False | Total |
| builder->Add(key, iter->value()); |
13 |
0 |
13 |
| builder->Add(key, iter->value()); |
13 |
0 |
13 |
| builder->Add(key, iter->value()); |
13 |
0 |
13 |
|
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
|
| // Finish and check for builder errors |
| 44 |
1 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
1 |
0 |
1 |
|
| 45 |
1 |
▶ | s = builder->Finish(); |
Branch: 2 branches
| Branch | True | False | Total |
| s = builder->Finish(); |
1 |
0 |
1 |
| s = builder->Finish(); |
1 |
0 |
1 |
|
| 46 |
1 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
1 |
0 |
1 |
|
| 47 |
1 |
▶ | meta->file_size = builder->FileSize(); |
Branch: 1 branch
| Branch | True | False | Total |
| meta->file_size = builder->FileSize(); |
1 |
0 |
1 |
|
| 48 |
1 |
▶ | assert(meta->file_size > 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(meta->file_size > 0); |
0 |
1 |
1 |
|
| 49 |
|
| } |
| 50 |
|
| } else { |
| 51 |
0 |
▶ | builder->Abandon(); |
Branch: 1 branch
| Branch | True | False | Total |
| builder->Abandon(); |
0 |
0 |
0 |
|
| 52 |
|
| } |
| 53 |
1 |
▶ | delete builder; |
Branch: 1 branch
| Branch | True | False | Total |
| delete builder; |
1 |
0 |
1 |
|
| 54 |
|
| |
| 55 |
|
| // Finish and check for file errors |
| 56 |
1 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
1 |
0 |
1 |
|
| 57 |
1 |
▶ | s = file->Sync(); |
Branch: 2 branches
| Branch | True | False | Total |
| s = file->Sync(); |
1 |
0 |
1 |
| s = file->Sync(); |
1 |
0 |
1 |
|
| 58 |
|
| } |
| 59 |
1 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
1 |
0 |
1 |
|
| 60 |
1 |
▶ | s = file->Close(); |
Branch: 2 branches
| Branch | True | False | Total |
| s = file->Close(); |
1 |
0 |
1 |
| s = file->Close(); |
1 |
0 |
1 |
|
| 61 |
|
| } |
| 62 |
1 |
▶ | delete file; |
Branch: 1 branch
| Branch | True | False | Total |
| delete file; |
1 |
0 |
1 |
|
| 63 |
1 |
| file = NULL; |
| 64 |
|
| |
| 65 |
1 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
1 |
0 |
1 |
|
| 66 |
|
| // Verify that the table is usable |
| 67 |
1 |
▶ | Iterator* it = table_cache->NewIterator(ReadOptions(), |
Branch: 1 branch
| Branch | True | False | Total |
| Iterator* it = table_cache->NewIterator(ReadOptions(), |
1 |
0 |
1 |
|
| 68 |
|
| meta->number, |
| 69 |
|
| meta->file_size); |
| 70 |
1 |
▶ | s = it->status(); |
Branch: 2 branches
| Branch | True | False | Total |
| s = it->status(); |
1 |
0 |
1 |
| s = it->status(); |
1 |
0 |
1 |
|
| 71 |
1 |
▶ | delete it; |
Branch: 1 branch
| Branch | True | False | Total |
| delete it; |
1 |
0 |
1 |
|
| 72 |
|
| } |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
|
| // Check for input iterator errors |
| 76 |
1 |
▶ | if (!iter->status().ok()) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!iter->status().ok()) { |
1 |
0 |
1 |
| if (!iter->status().ok()) { |
0 |
1 |
1 |
|
| 77 |
0 |
▶ | s = iter->status(); |
Branch: 2 branches
| Branch | True | False | Total |
| s = iter->status(); |
0 |
0 |
0 |
| s = iter->status(); |
0 |
0 |
0 |
|
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
1 |
▶ | if (s.ok() && meta->file_size > 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| 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) { |
0 |
1 |
1 |
|
| 81 |
|
| // Keep it |
| 82 |
|
| } else { |
| 83 |
0 |
▶ | env->DeleteFile(fname); |
Branch: 1 branch
| Branch | True | False | Total |
| env->DeleteFile(fname); |
0 |
0 |
0 |
|
| 84 |
|
| } |
| 85 |
1 |
| return s; |
| 86 |
1 |
| } |
| 87 |
|
| |
| 88 |
|
| } |