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