Coverage Report

Created: 2025-06-10 13:21

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