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/builder.h"7#include "db/dbformat.h"8#include "db/filename.h"9#include "db/table_cache.h"10#include "db/version_edit.h"11#include "leveldb/db.h"12#include "leveldb/env.h"13#include "leveldb/iterator.h"15namespace leveldb {17Status BuildTable(const std::string& dbname, Env* env, const Options& options,18TableCache* table_cache, Iterator* iter, FileMetaData* meta) {19Status s;20meta->file_size = 0;21iter->SeekToFirst();23std::string fname = TableFileName(dbname, meta->number);24if (iter->Valid()) {25WritableFile* file;26s = env->NewWritableFile(fname, &file);27if (!s.ok()) {28return s;29}31TableBuilder* builder = new TableBuilder(options, file);32meta->smallest.DecodeFrom(iter->key());33for (; iter->Valid(); iter->Next()) {34Slice key = iter->key();35meta->largest.DecodeFrom(key);36builder->Add(key, iter->value());37}39// Finish and check for builder errors40s = builder->Finish();41if (s.ok()) {42meta->file_size = builder->FileSize();43assert(meta->file_size > 0);44}45delete builder;47// Finish and check for file errors48if (s.ok()) {49s = file->Sync();50}51if (s.ok()) {52s = file->Close();53}54delete file;55file = nullptr;57if (s.ok()) {58// Verify that the table is usable59Iterator* it = table_cache->NewIterator(ReadOptions(), meta->number,60meta->file_size);61s = it->status();62delete it;63}64}66// Check for input iterator errors67if (!iter->status().ok()) {68s = iter->status();69}71if (s.ok() && meta->file_size > 0) {72// Keep it73} else {74env->DeleteFile(fname);75}76return s;77}79} // 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.