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