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#include "db/log_writer.h"7#include <stdint.h>9#include "leveldb/env.h"10#include "util/coding.h"11#include "util/crc32c.h"13namespace leveldb {14namespace log {16static void InitTypeCrc(uint32_t* type_crc) {17for (int i = 0; i <= kMaxRecordType; i++) {18char t = static_cast<char>(i);19type_crc[i] = crc32c::Value(&t, 1);20}21}23Writer::Writer(WritableFile* dest) : dest_(dest), block_offset_(0) {24InitTypeCrc(type_crc_);25}27Writer::Writer(WritableFile* dest, uint64_t dest_length)28: dest_(dest), block_offset_(dest_length % kBlockSize) {29InitTypeCrc(type_crc_);30}32Writer::~Writer() = default;34Status Writer::AddRecord(const Slice& slice) {35const char* ptr = slice.data();36size_t left = slice.size();38// Fragment the record if necessary and emit it. Note that if slice39// is empty, we still want to iterate once to emit a single40// zero-length record41Status s;42bool begin = true;43do {44const int leftover = kBlockSize - block_offset_;45assert(leftover >= 0);46if (leftover < kHeaderSize) {47// Switch to a new block48if (leftover > 0) {49// Fill the trailer (literal below relies on kHeaderSize being 7)50static_assert(kHeaderSize == 7, "");51dest_->Append(Slice("\x00\x00\x00\x00\x00\x00", leftover));52}53block_offset_ = 0;54}56// Invariant: we never leave < kHeaderSize bytes in a block.57assert(kBlockSize - block_offset_ - kHeaderSize >= 0);59const size_t avail = kBlockSize - block_offset_ - kHeaderSize;60const size_t fragment_length = (left < avail) ? left : avail;62RecordType type;63const bool end = (left == fragment_length);64if (begin && end) {65type = kFullType;66} else if (begin) {67type = kFirstType;68} else if (end) {69type = kLastType;70} else {71type = kMiddleType;72}74s = EmitPhysicalRecord(type, ptr, fragment_length);75ptr += fragment_length;76left -= fragment_length;77begin = false;78} while (s.ok() && left > 0);79return s;80}82Status Writer::EmitPhysicalRecord(RecordType t, const char* ptr,83size_t length) {84assert(length <= 0xffff); // Must fit in two bytes85assert(block_offset_ + kHeaderSize + length <= kBlockSize);87// Format the header88char buf[kHeaderSize];89buf[4] = static_cast<char>(length & 0xff);90buf[5] = static_cast<char>(length >> 8);91buf[6] = static_cast<char>(t);93// Compute the crc of the record type and the payload.94uint32_t crc = crc32c::Extend(type_crc_[t], ptr, length);95crc = crc32c::Mask(crc); // Adjust for storage96EncodeFixed32(buf, crc);98// Write the header and the payload99Status s = dest_->Append(Slice(buf, kHeaderSize));100if (s.ok()) {101s = dest_->Append(Slice(ptr, length));102if (s.ok()) {103s = dest_->Flush();104}105}106block_offset_ += kHeaderSize + length;107return s;108}110} // namespace log111} // 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.