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) 2013 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/db_impl.h"6#include "leveldb/cache.h"7#include "leveldb/db.h"8#include "util/testharness.h"9#include "util/testutil.h"11namespace leveldb {13class AutoCompactTest {14public:15AutoCompactTest() {16dbname_ = test::TmpDir() + "/autocompact_test";17tiny_cache_ = NewLRUCache(100);18options_.block_cache = tiny_cache_;19DestroyDB(dbname_, options_);20options_.create_if_missing = true;21options_.compression = kNoCompression;22ASSERT_OK(DB::Open(options_, dbname_, &db_));23}25~AutoCompactTest() {26delete db_;27DestroyDB(dbname_, Options());28delete tiny_cache_;29}31std::string Key(int i) {32char buf[100];33snprintf(buf, sizeof(buf), "key%06d", i);34return std::string(buf);35}37uint64_t Size(const Slice& start, const Slice& limit) {38Range r(start, limit);39uint64_t size;40db_->GetApproximateSizes(&r, 1, &size);41return size;42}44void DoReads(int n);46private:47std::string dbname_;48Cache* tiny_cache_;49Options options_;50DB* db_;51};53static const int kValueSize = 200 * 1024;54static const int kTotalSize = 100 * 1024 * 1024;55static const int kCount = kTotalSize / kValueSize;57// Read through the first n keys repeatedly and check that reads do NOT58// trigger compaction (seek compaction is disabled in this fork).59void AutoCompactTest::DoReads(int n) {60std::string value(kValueSize, 'x');61DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);63// Fill database64for (int i = 0; i < kCount; i++) {65ASSERT_OK(db_->Put(WriteOptions(), Key(i), value));66}67ASSERT_OK(dbi->TEST_CompactMemTable());69// Delete everything70for (int i = 0; i < kCount; i++) {71ASSERT_OK(db_->Delete(WriteOptions(), Key(i)));72}73ASSERT_OK(dbi->TEST_CompactMemTable());75// Get initial measurement of the space we will be reading.76const int64_t initial_size = Size(Key(0), Key(n));77const int64_t initial_other_size = Size(Key(n), Key(kCount));79// Read repeatedly. The size of the read range must NOT shrink: with80// seek compaction disabled, reads never schedule a compaction.81std::string limit_key = Key(n);82for (int read = 0; read < 100; read++) {83Iterator* iter = db_->NewIterator(ReadOptions());84for (iter->SeekToFirst();85iter->Valid() && iter->key().ToString() < limit_key; iter->Next()) {86// Drop data87}88delete iter;89uint64_t size = Size(Key(0), Key(n));90fprintf(stderr, "iter %3d => %7.3f MB [other %7.3f MB]\n", read + 1,91size / 1048576.0, Size(Key(n), Key(kCount)) / 1048576.0);92}93// Give any background work a chance to run, even though none should.94Env::Default()->SleepForMicroseconds(1000000);95ASSERT_EQ(Size(Key(0), Key(n)), static_cast<uint64_t>(initial_size));97// Verify that the size of the key space not touched by the reads98// is pretty much unchanged.99const int64_t final_other_size = Size(Key(n), Key(kCount));100ASSERT_LE(final_other_size, initial_other_size + 1048576);101ASSERT_GE(final_other_size, initial_other_size / 5 - 1048576);102}104TEST(AutoCompactTest, ReadAll) { DoReads(kCount); }106TEST(AutoCompactTest, ReadHalf) { DoReads(kCount / 2); }108} // namespace leveldb110int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }
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.