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/table_cache.h"7#include "db/filename.h"8#include "leveldb/env.h"9#include "leveldb/table.h"10#include "util/coding.h"12namespace leveldb {14struct TableAndFile {15RandomAccessFile* file;16Table* table;17};19static void DeleteEntry(const Slice& key, void* value) {20TableAndFile* tf = reinterpret_cast<TableAndFile*>(value);21delete tf->table;22delete tf->file;23delete tf;24}26static void UnrefEntry(void* arg1, void* arg2) {27Cache* cache = reinterpret_cast<Cache*>(arg1);28Cache::Handle* h = reinterpret_cast<Cache::Handle*>(arg2);29cache->Release(h);30}32TableCache::TableCache(const std::string& dbname, const Options& options,33int entries)34: env_(options.env),35dbname_(dbname),36options_(options),37cache_(NewLRUCache(entries)) {}39TableCache::~TableCache() { delete cache_; }41Status TableCache::FindTable(uint64_t file_number, uint64_t file_size,42Cache::Handle** handle) {43Status s;44char buf[sizeof(file_number)];45EncodeFixed64(buf, file_number);46Slice key(buf, sizeof(buf));47*handle = cache_->Lookup(key);48if (*handle == nullptr) {49std::string fname = TableFileName(dbname_, file_number);50RandomAccessFile* file = nullptr;51Table* table = nullptr;52s = env_->NewRandomAccessFile(fname, &file);53if (!s.ok()) {54std::string old_fname = SSTTableFileName(dbname_, file_number);55if (env_->NewRandomAccessFile(old_fname, &file).ok()) {56s = Status::OK();57}58}59if (s.ok()) {60s = Table::Open(options_, file, file_size, &table);61}63if (!s.ok()) {64assert(table == nullptr);65delete file;66// We do not cache error results so that if the error is transient,67// or somebody repairs the file, we recover automatically.68} else {69TableAndFile* tf = new TableAndFile;70tf->file = file;71tf->table = table;72*handle = cache_->Insert(key, tf, 1, &DeleteEntry);73}74}75return s;76}78Iterator* TableCache::NewIterator(const ReadOptions& options,79uint64_t file_number, uint64_t file_size,80Table** tableptr) {81if (tableptr != nullptr) {82*tableptr = nullptr;83}85Cache::Handle* handle = nullptr;86Status s = FindTable(file_number, file_size, &handle);87if (!s.ok()) {88return NewErrorIterator(s);89}91Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;92Iterator* result = table->NewIterator(options);93result->RegisterCleanup(&UnrefEntry, cache_, handle);94if (tableptr != nullptr) {95*tableptr = table;96}97return result;98}100Status TableCache::Get(const ReadOptions& options, uint64_t file_number,101uint64_t file_size, const Slice& k, void* arg,102void (*handle_result)(void*, const Slice&,103const Slice&)) {104Cache::Handle* handle = nullptr;105Status s = FindTable(file_number, file_size, &handle);106if (s.ok()) {107Table* t = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;108s = t->InternalGet(options, k, arg, handle_result);109cache_->Release(handle);110}111return s;112}114void TableCache::Evict(uint64_t file_number) {115char buf[sizeof(file_number)];116EncodeFixed64(buf, file_number);117cache_->Erase(Slice(buf, sizeof(buf)));118}120} // 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.