Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
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 be3// 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 previous8// string. This helps reduce the space requirement significantly.9// Furthermore, once every K keys, we do not apply the prefix10// compression and store the entire key. We call this a "restart11// point". The tail end of the block stores the offsets of all of the12// restart points, and can be used to do a binary search when looking13// 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: varint3218// unshared_bytes: varint3219// value_length: varint3220// 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: uint3227// restarts[i] contains the offset within the block of the ith restart point.29#include "table/block_builder.h"31#include <assert.h>33#include <algorithm>35#include "leveldb/comparator.h"36#include "leveldb/options.h"37#include "util/coding.h"39namespace leveldb {41BlockBuilder::BlockBuilder(const Options* options)42: options_(options), restarts_(), counter_(0), finished_(false) {43assert(options->block_restart_interval >= 1);44restarts_.push_back(0); // First restart point is at offset 045}47void BlockBuilder::Reset() {48buffer_.clear();49restarts_.clear();50restarts_.push_back(0); // First restart point is at offset 051counter_ = 0;52finished_ = false;53last_key_.clear();54}56size_t BlockBuilder::CurrentSizeEstimate() const {57return (buffer_.size() + // Raw data buffer58restarts_.size() * sizeof(uint32_t) + // Restart array59sizeof(uint32_t)); // Restart array length60}62Slice BlockBuilder::Finish() {63// Append restart array64for (size_t i = 0; i < restarts_.size(); i++) {65PutFixed32(&buffer_, restarts_[i]);66}67PutFixed32(&buffer_, restarts_.size());68finished_ = true;69return Slice(buffer_);70}72void BlockBuilder::Add(const Slice& key, const Slice& value) {73Slice last_key_piece(last_key_);74assert(!finished_);75assert(counter_ <= options_->block_restart_interval);76assert(buffer_.empty() // No values yet?77|| options_->comparator->Compare(key, last_key_piece) > 0);78size_t shared = 0;79if (counter_ < options_->block_restart_interval) {80// See how much sharing to do with previous string81const size_t min_length = std::min(last_key_piece.size(), key.size());82while ((shared < min_length) && (last_key_piece[shared] == key[shared])) {83shared++;84}85} else {86// Restart compression87restarts_.push_back(buffer_.size());88counter_ = 0;89}90const size_t non_shared = key.size() - shared;92// Add "<shared><non_shared><value_size>" to buffer_93PutVarint32(&buffer_, shared);94PutVarint32(&buffer_, non_shared);95PutVarint32(&buffer_, value.size());97// Add string delta to buffer_ followed by value98buffer_.append(key.data() + shared, non_shared);99buffer_.append(value.data(), value.size());101// Update state102last_key_.resize(shared);103last_key_.append(key.data() + shared, non_shared);104assert(Slice(last_key_) == key);105counter_++;106}108} // namespace leveldb
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.