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.5#ifndef STORAGE_LEVELDB_TABLE_FORMAT_H_6#define STORAGE_LEVELDB_TABLE_FORMAT_H_8#include <stdint.h>10#include <string>12#include "leveldb/slice.h"13#include "leveldb/status.h"14#include "leveldb/table_builder.h"16namespace leveldb {18class Block;19class RandomAccessFile;20struct ReadOptions;22// BlockHandle is a pointer to the extent of a file that stores a data23// block or a meta block.24class BlockHandle {25public:26// Maximum encoding length of a BlockHandle27enum { kMaxEncodedLength = 10 + 10 };29BlockHandle();31// The offset of the block in the file.32uint64_t offset() const { return offset_; }33void set_offset(uint64_t offset) { offset_ = offset; }35// The size of the stored block36uint64_t size() const { return size_; }37void set_size(uint64_t size) { size_ = size; }39void EncodeTo(std::string* dst) const;40Status DecodeFrom(Slice* input);42private:43uint64_t offset_;44uint64_t size_;45};47// Footer encapsulates the fixed information stored at the tail48// end of every table file.49class Footer {50public:51// Encoded length of a Footer. Note that the serialization of a52// Footer will always occupy exactly this many bytes. It consists53// of two block handles and a magic number.54enum { kEncodedLength = 2 * BlockHandle::kMaxEncodedLength + 8 };56Footer() = default;58// The block handle for the metaindex block of the table59const BlockHandle& metaindex_handle() const { return metaindex_handle_; }60void set_metaindex_handle(const BlockHandle& h) { metaindex_handle_ = h; }62// The block handle for the index block of the table63const BlockHandle& index_handle() const { return index_handle_; }64void set_index_handle(const BlockHandle& h) { index_handle_ = h; }66void EncodeTo(std::string* dst) const;67Status DecodeFrom(Slice* input);69private:70BlockHandle metaindex_handle_;71BlockHandle index_handle_;72};74// kTableMagicNumber was picked by running75// echo http://code.google.com/p/leveldb/ | sha1sum76// and taking the leading 64 bits.77static const uint64_t kTableMagicNumber = 0xdb4775248b80fb57ull;79// 1-byte type + 32-bit crc80static const size_t kBlockTrailerSize = 5;82struct BlockContents {83Slice data; // Actual contents of data84bool cachable; // True iff data can be cached85bool heap_allocated; // True iff caller should delete[] data.data()86};88// Read the block identified by "handle" from "file". On failure89// return non-OK. On success fill *result and return OK.90Status ReadBlock(RandomAccessFile* file, const ReadOptions& options,91const BlockHandle& handle, BlockContents* result);93// Implementation details follow. Clients should ignore,95inline BlockHandle::BlockHandle()96: offset_(~static_cast<uint64_t>(0)), size_(~static_cast<uint64_t>(0)) {}98} // namespace leveldb100#endif // STORAGE_LEVELDB_TABLE_FORMAT_H_
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.