Coverage Report

Created: 2026-02-14 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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 <algorithm>
32
#include <cassert>
33
34
#include "leveldb/comparator.h"
35
#include "leveldb/options.h"
36
#include "util/coding.h"
37
38
namespace leveldb {
39
40
BlockBuilder::BlockBuilder(const Options* options)
41
125k
    : options_(options), restarts_(), counter_(0), finished_(false) {
42
125k
  assert(options->block_restart_interval >= 1);
43
125k
  restarts_.push_back(0);  // First restart point is at offset 0
44
125k
}
45
46
128k
void BlockBuilder::Reset() {
47
128k
  buffer_.clear();
48
128k
  restarts_.clear();
49
128k
  restarts_.push_back(0);  // First restart point is at offset 0
50
128k
  counter_ = 0;
51
128k
  finished_ = false;
52
128k
  last_key_.clear();
53
128k
}
54
55
451k
size_t BlockBuilder::CurrentSizeEstimate() const {
56
451k
  return (buffer_.size() +                       // Raw data buffer
57
451k
          restarts_.size() * sizeof(uint32_t) +  // Restart array
58
451k
          sizeof(uint32_t));                     // Restart array length
59
451k
}
60
61
128k
Slice BlockBuilder::Finish() {
62
  // Append restart array
63
285k
  for (size_t i = 0; i < restarts_.size(); i++) {
64
156k
    PutFixed32(&buffer_, restarts_[i]);
65
156k
  }
66
128k
  PutFixed32(&buffer_, restarts_.size());
67
128k
  finished_ = true;
68
128k
  return Slice(buffer_);
69
128k
}
70
71
499k
void BlockBuilder::Add(const Slice& key, const Slice& value) {
72
499k
  Slice last_key_piece(last_key_);
73
499k
  assert(!finished_);
74
499k
  assert(counter_ <= options_->block_restart_interval);
75
499k
  assert(buffer_.empty()  // No values yet?
76
499k
         || options_->comparator->Compare(key, last_key_piece) > 0);
77
499k
  size_t shared = 0;
78
499k
  if (counter_ < options_->block_restart_interval) {
79
    // See how much sharing to do with previous string
80
471k
    const size_t min_length = std::min(last_key_piece.size(), key.size());
81
2.72M
    while ((shared < min_length) && (last_key_piece[shared] == key[shared])) {
82
2.25M
      shared++;
83
2.25M
    }
84
471k
  } else {
85
    // Restart compression
86
27.4k
    restarts_.push_back(buffer_.size());
87
27.4k
    counter_ = 0;
88
27.4k
  }
89
499k
  const size_t non_shared = key.size() - shared;
90
91
  // Add "<shared><non_shared><value_size>" to buffer_
92
499k
  PutVarint32(&buffer_, shared);
93
499k
  PutVarint32(&buffer_, non_shared);
94
499k
  PutVarint32(&buffer_, value.size());
95
96
  // Add string delta to buffer_ followed by value
97
499k
  buffer_.append(key.data() + shared, non_shared);
98
499k
  buffer_.append(value.data(), value.size());
99
100
  // Update state
101
499k
  last_key_.resize(shared);
102
499k
  last_key_.append(key.data() + shared, non_shared);
103
  assert(Slice(last_key_) == key);
104
499k
  counter_++;
105
499k
}
106
107
}  // namespace leveldb