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// Test for issue 178: a manual compaction causes deleted data to reappear.6#include <cstdlib>7#include <iostream>8#include <sstream>10#include "leveldb/db.h"11#include "leveldb/write_batch.h"12#include "util/testharness.h"14namespace {16const int kNumKeys = 1100000;18std::string Key1(int i) {19char buf[100];20snprintf(buf, sizeof(buf), "my_key_%d", i);21return buf;22}24std::string Key2(int i) { return Key1(i) + "_xxx"; }26class Issue178 {};28TEST(Issue178, Test) {29// Get rid of any state from an old run.30std::string dbpath = leveldb::test::TmpDir() + "/leveldb_cbug_test";31DestroyDB(dbpath, leveldb::Options());33// Open database. Disable compression since it affects the creation34// of layers and the code below is trying to test against a very35// specific scenario.36leveldb::DB* db;37leveldb::Options db_options;38db_options.create_if_missing = true;39db_options.compression = leveldb::kNoCompression;40ASSERT_OK(leveldb::DB::Open(db_options, dbpath, &db));42// create first key range43leveldb::WriteBatch batch;44for (size_t i = 0; i < kNumKeys; i++) {45batch.Put(Key1(i), "value for range 1 key");46}47ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch));49// create second key range50batch.Clear();51for (size_t i = 0; i < kNumKeys; i++) {52batch.Put(Key2(i), "value for range 2 key");53}54ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch));56// delete second key range57batch.Clear();58for (size_t i = 0; i < kNumKeys; i++) {59batch.Delete(Key2(i));60}61ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch));63// compact database64std::string start_key = Key1(0);65std::string end_key = Key1(kNumKeys - 1);66leveldb::Slice least(start_key.data(), start_key.size());67leveldb::Slice greatest(end_key.data(), end_key.size());69// commenting out the line below causes the example to work correctly70db->CompactRange(&least, &greatest);72// count the keys73leveldb::Iterator* iter = db->NewIterator(leveldb::ReadOptions());74size_t num_keys = 0;75for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {76num_keys++;77}78delete iter;79ASSERT_EQ(kNumKeys, num_keys) << "Bad number of keys";81// close database82delete db;83DestroyDB(dbpath, leveldb::Options());84}86} // anonymous namespace88int 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.