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_DB_LOG_READER_H_6#define STORAGE_LEVELDB_DB_LOG_READER_H_8#include <stdint.h>10#include "db/log_format.h"11#include "leveldb/slice.h"12#include "leveldb/status.h"14namespace leveldb {16class SequentialFile;18namespace log {20class Reader {21public:22// Interface for reporting errors.23class Reporter {24public:25virtual ~Reporter();27// Some corruption was detected. "size" is the approximate number28// of bytes dropped due to the corruption.29virtual void Corruption(size_t bytes, const Status& status) = 0;30};32// Create a reader that will return log records from "*file".33// "*file" must remain live while this Reader is in use.34//35// If "reporter" is non-null, it is notified whenever some data is36// dropped due to a detected corruption. "*reporter" must remain37// live while this Reader is in use.38//39// If "checksum" is true, verify checksums if available.40//41// The Reader will start reading at the first record located at physical42// position >= initial_offset within the file.43Reader(SequentialFile* file, Reporter* reporter, bool checksum,44uint64_t initial_offset);46Reader(const Reader&) = delete;47Reader& operator=(const Reader&) = delete;49~Reader();51// Read the next record into *record. Returns true if read52// successfully, false if we hit end of the input. May use53// "*scratch" as temporary storage. The contents filled in *record54// will only be valid until the next mutating operation on this55// reader or the next mutation to *scratch.56bool ReadRecord(Slice* record, std::string* scratch);58// Returns the physical offset of the last record returned by ReadRecord.59//60// Undefined before the first call to ReadRecord.61uint64_t LastRecordOffset();63private:64// Extend record types with the following special values65enum {66kEof = kMaxRecordType + 1,67// Returned whenever we find an invalid physical record.68// Currently there are three situations in which this happens:69// * The record has an invalid CRC (ReadPhysicalRecord reports a drop)70// * The record is a 0-length record (No drop is reported)71// * The record is below constructor's initial_offset (No drop is reported)72kBadRecord = kMaxRecordType + 273};75// Skips all blocks that are completely before "initial_offset_".76//77// Returns true on success. Handles reporting.78bool SkipToInitialBlock();80// Return type, or one of the preceding special values81unsigned int ReadPhysicalRecord(Slice* result);83// Reports dropped bytes to the reporter.84// buffer_ must be updated to remove the dropped bytes prior to invocation.85void ReportCorruption(uint64_t bytes, const char* reason);86void ReportDrop(uint64_t bytes, const Status& reason);88SequentialFile* const file_;89Reporter* const reporter_;90bool const checksum_;91char* const backing_store_;92Slice buffer_;93bool eof_; // Last Read() indicated EOF by returning < kBlockSize95// Offset of the last record returned by ReadRecord.96uint64_t last_record_offset_;97// Offset of the first location past the end of buffer_.98uint64_t end_of_buffer_offset_;100// Offset at which to start looking for the first record to return101uint64_t const initial_offset_;103// True if we are resynchronizing after a seek (initial_offset_ > 0). In104// particular, a run of kMiddleType and kLastType records can be silently105// skipped in this mode106bool resyncing_;107};109} // namespace log110} // namespace leveldb112#endif // STORAGE_LEVELDB_DB_LOG_READER_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.