table/table_builder.cc

filter: Statementstmt 79.2% 103 / 130 Branchbran 44.0% 59 / 134 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 61.3% 162 / 264 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« util/env.cc ↑ Summary db/table_cache.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 "leveldb/table_builder.h"
6  
7 #include <assert.h>
8 #include <stdio.h>
9 #include "leveldb/comparator.h"
10 #include "leveldb/env.h"
11 #include "table/block_builder.h"
12 #include "table/format.h"
13 #include "util/coding.h"
14 #include "util/crc32c.h"
15 #include "util/logging.h"
16  
17 namespace leveldb {
18  
19 struct TableBuilder::Rep {
20 Options options;
21 Options index_block_options;
22 WritableFile* file;
23 uint64_t offset;
24 Status status;
25 BlockBuilder data_block;
26 BlockBuilder index_block;
27 std::string last_key;
28 int64_t num_entries;
29 bool closed; // Either Finish() or Abandon() has been called.
30  
31 // We do not emit the index entry for a block until we have seen the
32 // first key for the next data block. This allows us to use shorter
33 // keys in the index block. For example, consider a block boundary
34 // between the keys "the quick brown fox" and "the who". We can use
35 // "the r" as the key for the index block entry since it is >= all
36 // entries in the first block and < all entries in subsequent
37 // blocks.
38 //
39 // Invariant: r->pending_index_entry is true only if data_block is empty.
40 bool pending_index_entry;
41 BlockHandle pending_handle; // Handle to add to index block
42  
43 std::string compressed_output;
44  
45 1 Rep(const Options& opt, WritableFile* f)
46 1 : options(opt),
47 1 index_block_options(opt),
48 1 file(f),
49 1 offset(0),
50 1 ▶ data_block(&options),
Branch: 1 branch
BranchTrueFalseTotal
data_block(&options), 1 0 1
51 1 ▶ index_block(&index_block_options),
Branch: 1 branch
BranchTrueFalseTotal
index_block(&index_block_options), 1 0 1
52 1 num_entries(0),
53 1 closed(false),
54 2 ▶ pending_index_entry(false) {
Branch: 1 branch
BranchTrueFalseTotal
pending_index_entry(false) { 1 0 1
55 1 index_block_options.block_restart_interval = 1;
56 1 }
57 };
58  
59 1 TableBuilder::TableBuilder(const Options& options, WritableFile* file)
60 1 ▶ : rep_(new Rep(options, file)) {
Branch: 2 branches
BranchTrueFalseTotal
: rep_(new Rep(options, file)) { 1 0 1
: rep_(new Rep(options, file)) { 0 0 0
61 1 }
62  
63 1 TableBuilder::~TableBuilder() {
64 1 ▶ assert(rep_->closed); // Catch errors where caller forgot to call Finish()
Branch: 1 branch
BranchTrueFalseTotal
assert(rep_->closed); // Catch errors where caller forgot to call Finish() 0 1 1
65 1 ▶ delete rep_;
Branch: 1 branch
BranchTrueFalseTotal
delete rep_; 1 0 1
66 1 }
67  
68 0 Status TableBuilder::ChangeOptions(const Options& options) {
69 // Note: if more fields are added to Options, update
70 // this function to catch changes that should not be allowed to
71 // change in the middle of building a Table.
72 0 ▶ if (options.comparator != rep_->options.comparator) {
Branch: 1 branch
BranchTrueFalseTotal
if (options.comparator != rep_->options.comparator) { 0 0 0
73 0 ▶ return Status::InvalidArgument("changing comparator while building table");
Branch: 1 branch
BranchTrueFalseTotal
return Status::InvalidArgument("changing comparator while building table"); 0 0 0
74 }
75  
76 // Note that any live BlockBuilders point to rep_->options and therefore
77 // will automatically pick up the updated options.
78 0 rep_->options = options;
79 0 rep_->index_block_options = options;
80 0 rep_->index_block_options.block_restart_interval = 1;
81 0 return Status::OK();
82 }
83  
84 13 void TableBuilder::Add(const Slice& key, const Slice& value) {
85 13 Rep* r = rep_;
86 13 ▶ assert(!r->closed);
Branch: 1 branch
BranchTrueFalseTotal
assert(!r->closed); 0 13 13
87 13 ▶ if (!ok()) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok()) return; 0 13 13
88 13 ▶ if (r->num_entries > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->num_entries > 0) { 12 1 13
89 12 ▶ assert(r->options.comparator->Compare(key, Slice(r->last_key)) > 0);
Branch: 2 branches
BranchTrueFalseTotal
assert(r->options.comparator->Compare(key, Slice(r->last_key)) > 0); 12 0 12
assert(r->options.comparator->Compare(key, Slice(r->last_key)) > 0); 0 12 12
90 }
91  
92 13 ▶ if (r->pending_index_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->pending_index_entry) { 0 13 13
93 0 ▶ assert(r->data_block.empty());
Branch: 1 branch
BranchTrueFalseTotal
assert(r->data_block.empty()); 0 0 0
94 0 ▶ r->options.comparator->FindShortestSeparator(&r->last_key, key);
Branch: 1 branch
BranchTrueFalseTotal
r->options.comparator->FindShortestSeparator(&r->last_key, key); 0 0 0
95 0 std::string handle_encoding;
96 0 ▶ r->pending_handle.EncodeTo(&handle_encoding);
Branch: 1 branch
BranchTrueFalseTotal
r->pending_handle.EncodeTo(&handle_encoding); 0 0 0
97 0 ▶ r->index_block.Add(r->last_key, Slice(handle_encoding));
Branch: 1 branch
BranchTrueFalseTotal
r->index_block.Add(r->last_key, Slice(handle_encoding)); 0 0 0
98 0 r->pending_index_entry = false;
99 0 }
100  
101 13 r->last_key.assign(key.data(), key.size());
102 13 r->num_entries++;
103 13 r->data_block.Add(key, value);
104  
105 13 const size_t estimated_block_size = r->data_block.CurrentSizeEstimate();
106 13 ▶ if (estimated_block_size >= r->options.block_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (estimated_block_size >= r->options.block_size) { 0 13 13
107 0 Flush();
108 }
109 }
110  
111 1 void TableBuilder::Flush() {
112 1 Rep* r = rep_;
113 1 ▶ assert(!r->closed);
Branch: 1 branch
BranchTrueFalseTotal
assert(!r->closed); 0 1 1
114 1 ▶ if (!ok()) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok()) return; 0 1 1
115 1 ▶ if (r->data_block.empty()) return;
Branch: 1 branch
BranchTrueFalseTotal
if (r->data_block.empty()) return; 0 1 1
116 1 ▶ assert(!r->pending_index_entry);
Branch: 1 branch
BranchTrueFalseTotal
assert(!r->pending_index_entry); 0 1 1
117 1 WriteBlock(&r->data_block, &r->pending_handle);
118 1 ▶ if (ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok()) { 1 0 1
119 1 r->pending_index_entry = true;
120 1 ▶ r->status = r->file->Flush();
Branch: 2 branches
BranchTrueFalseTotal
r->status = r->file->Flush(); 1 0 1
r->status = r->file->Flush(); 1 0 1
121 }
122 }
123  
124 3 void TableBuilder::WriteBlock(BlockBuilder* block, BlockHandle* handle) {
125 // File format contains a sequence of blocks where each block has:
126 // block_data: uint8[n]
127 // type: uint8
128 // crc: uint32
129 3 ▶ assert(ok());
Branch: 2 branches
BranchTrueFalseTotal
assert(ok()); 3 0 3
assert(ok()); 0 3 3
130 3 Rep* r = rep_;
131 3 ▶ Slice raw = block->Finish();
Branch: 1 branch
BranchTrueFalseTotal
Slice raw = block->Finish(); 3 0 3
132  
133 3 Slice block_contents;
134 3 CompressionType type = r->options.compression;
135 // TODO(postrelease): Support more compression options: zlib?
136 3 switch (type) {
137 0 case kNoCompression:
138 0 block_contents = raw;
139 0 break;
140  
141 3 case kSnappyCompression: {
142 3 std::string* compressed = &r->compressed_output;
143 3 ▶ if (port::Snappy_Compress(raw.data(), raw.size(), compressed) &&
Branch: 2 branches
BranchTrueFalseTotal
if (port::Snappy_Compress(raw.data(), raw.size(), compressed) && 0 3 3
if (port::Snappy_Compress(raw.data(), raw.size(), compressed) && 0 3 3
144 0 ▶ compressed->size() < raw.size() - (raw.size() / 8u)) {
Branch: 1 branch
BranchTrueFalseTotal
compressed->size() < raw.size() - (raw.size() / 8u)) { 0 0 0
145 0 block_contents = *compressed;
146 } else {
147 // Snappy not supported, or compressed less than 12.5%, so just
148 // store uncompressed form
149 3 block_contents = raw;
150 3 type = kNoCompression;
151 }
152 3 break;
153 }
154 }
155 3 handle->set_offset(r->offset);
156 3 handle->set_size(block_contents.size());
157 3 ▶ r->status = r->file->Append(block_contents);
Branch: 2 branches
BranchTrueFalseTotal
r->status = r->file->Append(block_contents); 3 0 3
r->status = r->file->Append(block_contents); 3 0 3
158 3 ▶ if (r->status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->status.ok()) { 3 0 3
159 char trailer[kBlockTrailerSize];
160 3 trailer[0] = type;
161 3 ▶ uint32_t crc = crc32c::Value(block_contents.data(), block_contents.size());
Branch: 1 branch
BranchTrueFalseTotal
uint32_t crc = crc32c::Value(block_contents.data(), block_contents.size()); 3 0 3
162 3 ▶ crc = crc32c::Extend(crc, trailer, 1); // Extend crc to cover block type
Branch: 1 branch
BranchTrueFalseTotal
crc = crc32c::Extend(crc, trailer, 1); // Extend crc to cover block type 3 0 3
163 3 ▶ EncodeFixed32(trailer+1, crc32c::Mask(crc));
Branch: 1 branch
BranchTrueFalseTotal
EncodeFixed32(trailer+1, crc32c::Mask(crc)); 3 0 3
164 3 ▶ r->status = r->file->Append(Slice(trailer, kBlockTrailerSize));
Branch: 2 branches
BranchTrueFalseTotal
r->status = r->file->Append(Slice(trailer, kBlockTrailerSize)); 3 0 3
r->status = r->file->Append(Slice(trailer, kBlockTrailerSize)); 3 0 3
165 3 ▶ if (r->status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->status.ok()) { 3 0 3
166 3 r->offset += block_contents.size() + kBlockTrailerSize;
167 }
168 }
169 3 r->compressed_output.clear();
170 3 ▶ block->Reset();
Branch: 1 branch
BranchTrueFalseTotal
block->Reset(); 3 0 3
171 3 }
172  
173 21 Status TableBuilder::status() const {
174 21 return rep_->status;
175 }
176  
177 1 Status TableBuilder::Finish() {
178 1 Rep* r = rep_;
179 1 ▶ Flush();
Branch: 1 branch
BranchTrueFalseTotal
Flush(); 1 0 1
180 1 ▶ assert(!r->closed);
Branch: 1 branch
BranchTrueFalseTotal
assert(!r->closed); 0 1 1
181 1 r->closed = true;
182 1 ▶ BlockHandle metaindex_block_handle;
Branch: 1 branch
BranchTrueFalseTotal
BlockHandle metaindex_block_handle; 1 0 1
183 1 ▶ BlockHandle index_block_handle;
Branch: 1 branch
BranchTrueFalseTotal
BlockHandle index_block_handle; 1 0 1
184 1 ▶ if (ok()) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok()) { 1 0 1
if (ok()) { 1 0 1
185 1 ▶ BlockBuilder meta_index_block(&r->options);
Branch: 1 branch
BranchTrueFalseTotal
BlockBuilder meta_index_block(&r->options); 1 0 1
186 // TODO(postrelease): Add stats and other meta blocks
187 1 ▶ WriteBlock(&meta_index_block, &metaindex_block_handle);
Branch: 1 branch
BranchTrueFalseTotal
WriteBlock(&meta_index_block, &metaindex_block_handle); 1 0 1
188 1 }
189 1 ▶ if (ok()) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok()) { 1 0 1
if (ok()) { 1 0 1
190 1 ▶ if (r->pending_index_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->pending_index_entry) { 1 0 1
191 1 ▶ r->options.comparator->FindShortSuccessor(&r->last_key);
Branch: 1 branch
BranchTrueFalseTotal
r->options.comparator->FindShortSuccessor(&r->last_key); 1 0 1
192 1 std::string handle_encoding;
193 1 ▶ r->pending_handle.EncodeTo(&handle_encoding);
Branch: 1 branch
BranchTrueFalseTotal
r->pending_handle.EncodeTo(&handle_encoding); 1 0 1
194 1 ▶ r->index_block.Add(r->last_key, Slice(handle_encoding));
Branch: 1 branch
BranchTrueFalseTotal
r->index_block.Add(r->last_key, Slice(handle_encoding)); 1 0 1
195 1 r->pending_index_entry = false;
196 1 }
197 1 ▶ WriteBlock(&r->index_block, &index_block_handle);
Branch: 1 branch
BranchTrueFalseTotal
WriteBlock(&r->index_block, &index_block_handle); 1 0 1
198 }
199 1 ▶ if (ok()) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok()) { 1 0 1
if (ok()) { 1 0 1
200 1 ▶ Footer footer;
Branch: 1 branch
BranchTrueFalseTotal
Footer footer; 1 0 1
201 1 footer.set_metaindex_handle(metaindex_block_handle);
202 1 footer.set_index_handle(index_block_handle);
203 1 std::string footer_encoding;
204 1 ▶ footer.EncodeTo(&footer_encoding);
Branch: 1 branch
BranchTrueFalseTotal
footer.EncodeTo(&footer_encoding); 1 0 1
205 1 ▶ r->status = r->file->Append(footer_encoding);
Branch: 2 branches
BranchTrueFalseTotal
r->status = r->file->Append(footer_encoding); 1 0 1
r->status = r->file->Append(footer_encoding); 1 0 1
206 1 ▶ if (r->status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->status.ok()) { 1 0 1
207 1 r->offset += footer_encoding.size();
208 }
209 1 }
210 2 ▶ return r->status;
Branch: 1 branch
BranchTrueFalseTotal
return r->status; 1 0 1
211 }
212  
213 0 void TableBuilder::Abandon() {
214 0 Rep* r = rep_;
215 0 ▶ assert(!r->closed);
Branch: 1 branch
BranchTrueFalseTotal
assert(!r->closed); 0 0 0
216 0 r->closed = true;
217 0 }
218  
219 0 uint64_t TableBuilder::NumEntries() const {
220 0 return rep_->num_entries;
221 }
222  
223 1 uint64_t TableBuilder::FileSize() const {
224 1 return rep_->offset;
225 }
226  
227 }
« util/env.cc ↑ Summary db/table_cache.cc »