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 "leveldb/db.h"7#include "db/memtable.h"8#include "db/write_batch_internal.h"9#include "leveldb/env.h"10#include "util/logging.h"11#include "util/testharness.h"13namespace leveldb {15static std::string PrintContents(WriteBatch* b) {16InternalKeyComparator cmp(BytewiseComparator());17MemTable* mem = new MemTable(cmp);18mem->Ref();19std::string state;20Status s = WriteBatchInternal::InsertInto(b, mem);21int count = 0;22Iterator* iter = mem->NewIterator();23for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {24ParsedInternalKey ikey;25ASSERT_TRUE(ParseInternalKey(iter->key(), &ikey));26switch (ikey.type) {27case kTypeValue:28state.append("Put(");29state.append(ikey.user_key.ToString());30state.append(", ");31state.append(iter->value().ToString());32state.append(")");33count++;34break;35case kTypeDeletion:36state.append("Delete(");37state.append(ikey.user_key.ToString());38state.append(")");39count++;40break;41}42state.append("@");43state.append(NumberToString(ikey.sequence));44}45delete iter;46if (!s.ok()) {47state.append("ParseError()");48} else if (count != WriteBatchInternal::Count(b)) {49state.append("CountMismatch()");50}51mem->Unref();52return state;53}55class WriteBatchTest {};57TEST(WriteBatchTest, Empty) {58WriteBatch batch;59ASSERT_EQ("", PrintContents(&batch));60ASSERT_EQ(0, WriteBatchInternal::Count(&batch));61}63TEST(WriteBatchTest, Multiple) {64WriteBatch batch;65batch.Put(Slice("foo"), Slice("bar"));66batch.Delete(Slice("box"));67batch.Put(Slice("baz"), Slice("boo"));68WriteBatchInternal::SetSequence(&batch, 100);69ASSERT_EQ(100, WriteBatchInternal::Sequence(&batch));70ASSERT_EQ(3, WriteBatchInternal::Count(&batch));71ASSERT_EQ(72"Put(baz, boo)@102"73"Delete(box)@101"74"Put(foo, bar)@100",75PrintContents(&batch));76}78TEST(WriteBatchTest, Corruption) {79WriteBatch batch;80batch.Put(Slice("foo"), Slice("bar"));81batch.Delete(Slice("box"));82WriteBatchInternal::SetSequence(&batch, 200);83Slice contents = WriteBatchInternal::Contents(&batch);84WriteBatchInternal::SetContents(&batch,85Slice(contents.data(), contents.size() - 1));86ASSERT_EQ(87"Put(foo, bar)@200"88"ParseError()",89PrintContents(&batch));90}92TEST(WriteBatchTest, Append) {93WriteBatch b1, b2;94WriteBatchInternal::SetSequence(&b1, 200);95WriteBatchInternal::SetSequence(&b2, 300);96b1.Append(b2);97ASSERT_EQ("", PrintContents(&b1));98b2.Put("a", "va");99b1.Append(b2);100ASSERT_EQ("Put(a, va)@200", PrintContents(&b1));101b2.Clear();102b2.Put("b", "vb");103b1.Append(b2);104ASSERT_EQ(105"Put(a, va)@200"106"Put(b, vb)@201",107PrintContents(&b1));108b2.Delete("foo");109b1.Append(b2);110ASSERT_EQ(111"Put(a, va)@200"112"Put(b, vb)@202"113"Put(b, vb)@201"114"Delete(foo)@203",115PrintContents(&b1));116}118TEST(WriteBatchTest, ApproximateSize) {119WriteBatch batch;120size_t empty_size = batch.ApproximateSize();122batch.Put(Slice("foo"), Slice("bar"));123size_t one_key_size = batch.ApproximateSize();124ASSERT_LT(empty_size, one_key_size);126batch.Put(Slice("baz"), Slice("boo"));127size_t two_keys_size = batch.ApproximateSize();128ASSERT_LT(one_key_size, two_keys_size);130batch.Delete(Slice("box"));131size_t post_delete_size = batch.ApproximateSize();132ASSERT_LT(two_keys_size, post_delete_size);133}135} // namespace leveldb137int 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.