Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/leveldb/table/block_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
// BlockBuilder generates blocks where keys are prefix-compressed:
6
//
7
// When we store a key, we drop the prefix shared with the previous
8
// string.  This helps reduce the space requirement significantly.
9
// Furthermore, once every K keys, we do not apply the prefix
10
// compression and store the entire key.  We call this a "restart
11
// point".  The tail end of the block stores the offsets of all of the
12
// restart points, and can be used to do a binary search when looking
13
// for a particular key.  Values are stored as-is (without compression)
14
// immediately following the corresponding key.
15
//
16
// An entry for a particular key-value pair has the form:
17
//     shared_bytes: varint32
18
//     unshared_bytes: varint32
19
//     value_length: varint32
20
//     key_delta: char[unshared_bytes]
21
//     value: char[value_length]
22
// shared_bytes == 0 for restart points.
23
//
24
// The trailer of the block has the form:
25
//     restarts: uint32[num_restarts]
26
//     num_restarts: uint32
27
// restarts[i] contains the offset within the block of the ith restart point.
28
29
#include "table/block_builder.h"
30
31
#include <assert.h>
32
33
#include <algorithm>
34
35
#include "leveldb/comparator.h"
36
#include "leveldb/options.h"
37
#include "util/coding.h"
38
39
namespace leveldb {
40
41
BlockBuilder::BlockBuilder(const Options* options)
42
72
    : options_(options), restarts_(), counter_(0), finished_(false) {
43
72
  assert(options->block_restart_interval >= 1);
  Branch (43:3): [True: 72, False: 0]
44
72
  restarts_.push_back(0);  // First restart point is at offset 0
45
72
}
46
47
673
void BlockBuilder::Reset() {
48
673
  buffer_.clear();
49
673
  restarts_.clear();
50
673
  restarts_.push_back(0);  // First restart point is at offset 0
51
673
  counter_ = 0;
52
673
  finished_ = false;
53
673
  last_key_.clear();
54
673
}
55
56
11.7k
size_t BlockBuilder::CurrentSizeEstimate() const {
57
11.7k
  return (buffer_.size() +                       // Raw data buffer
58
11.7k
          restarts_.size() * sizeof(uint32_t) +  // Restart array
59
11.7k
          sizeof(uint32_t));                     // Restart array length
60
11.7k
}
61
62
673
Slice BlockBuilder::Finish() {
63
  // Append restart array
64
2.41k
  for (size_t i = 0; i < restarts_.size(); i++) {
  Branch (64:22): [True: 1.73k, False: 673]
65
1.73k
    PutFixed32(&buffer_, restarts_[i]);
66
1.73k
  }
67
673
  PutFixed32(&buffer_, restarts_.size());
68
673
  finished_ = true;
69
673
  return Slice(buffer_);
70
673
}
71
72
12.3k
void BlockBuilder::Add(const Slice& key, const Slice& value) {
73
12.3k
  Slice last_key_piece(last_key_);
74
12.3k
  assert(!finished_);
  Branch (74:3): [True: 12.3k, False: 0]
75
12.3k
  assert(counter_ <= options_->block_restart_interval);
  Branch (75:3): [True: 12.3k, False: 0]
76
12.3k
  assert(buffer_.empty()  // No values yet?
  Branch (76:3): [True: 673, False: 11.7k]
  Branch (76:3): [True: 11.7k, False: 0]
  Branch (76:3): [True: 12.3k, False: 0]
77
12.3k
         || options_->comparator->Compare(key, last_key_piece) > 0);
78
12.3k
  size_t shared = 0;
79
12.3k
  if (counter_ < options_->block_restart_interval) {
  Branch (79:7): [True: 11.3k, False: 1.06k]
80
    // See how much sharing to do with previous string
81
11.3k
    const size_t min_length = std::min(last_key_piece.size(), key.size());
82
43.3k
    while ((shared < min_length) && (last_key_piece[shared] == key[shared])) {
  Branch (82:12): [True: 42.6k, False: 673]
  Branch (82:37): [True: 32.0k, False: 10.6k]
83
32.0k
      shared++;
84
32.0k
    }
85
11.3k
  } else {
86
    // Restart compression
87
1.06k
    restarts_.push_back(buffer_.size());
88
1.06k
    counter_ = 0;
89
1.06k
  }
90
12.3k
  const size_t non_shared = key.size() - shared;
91
92
  // Add "<shared><non_shared><value_size>" to buffer_
93
12.3k
  PutVarint32(&buffer_, shared);
94
12.3k
  PutVarint32(&buffer_, non_shared);
95
12.3k
  PutVarint32(&buffer_, value.size());
96
97
  // Add string delta to buffer_ followed by value
98
12.3k
  buffer_.append(key.data() + shared, non_shared);
99
12.3k
  buffer_.append(value.data(), value.size());
100
101
  // Update state
102
12.3k
  last_key_.resize(shared);
103
12.3k
  last_key_.append(key.data() + shared, non_shared);
104
12.3k
  assert(Slice(last_key_) == key);
  Branch (104:3): [True: 12.3k, False: 0]
105
12.3k
  counter_++;
106
12.3k
}
107
108
}  // namespace leveldb