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.
501std::string end_trans_str = "END TRANSACTION;";503// Check for synchronous flag in options504std::string sync_stmt =505(write_sync) ? "PRAGMA synchronous = FULL" : "PRAGMA synchronous = OFF";506status = sqlite3_exec(db_, sync_stmt.c_str(), nullptr, nullptr, &err_msg);507ExecErrorCheck(status, err_msg);509// Preparing sqlite3 statements510status = sqlite3_prepare_v2(db_, replace_str.c_str(), -1, &replace_stmt,511nullptr);512ErrorCheck(status);513status = sqlite3_prepare_v2(db_, begin_trans_str.c_str(), -1,514&begin_trans_stmt, nullptr);515ErrorCheck(status);516status = sqlite3_prepare_v2(db_, end_trans_str.c_str(), -1, &end_trans_stmt,517nullptr);518ErrorCheck(status);520bool transaction = (entries_per_batch > 1);521for (int i = 0; i < num_entries; i += entries_per_batch) {522// Begin write transaction523if (FLAGS_transaction && transaction) {524status = sqlite3_step(begin_trans_stmt);525StepErrorCheck(status);526status = sqlite3_reset(begin_trans_stmt);527ErrorCheck(status);528}530// Create and execute SQL statements531for (int j = 0; j < entries_per_batch; j++) {532const char* value = gen_.Generate(value_size).data();534// Create values for key-value pair535const int k =536(order == SEQUENTIAL) ? i + j : (rand_.Next() % num_entries);537char key[100];538snprintf(key, sizeof(key), "%016d", k);540// Bind KV values into replace_stmt541status = sqlite3_bind_blob(replace_stmt, 1, key, 16, SQLITE_STATIC);542ErrorCheck(status);543status = sqlite3_bind_blob(replace_stmt, 2, value, value_size,544SQLITE_STATIC);545ErrorCheck(status);547// Execute replace_stmt548bytes_ += value_size + strlen(key);549status = sqlite3_step(replace_stmt);550StepErrorCheck(status);552// Reset SQLite statement for another use553status = sqlite3_clear_bindings(replace_stmt);554ErrorCheck(status);555status = sqlite3_reset(replace_stmt);556ErrorCheck(status);558FinishedSingleOp();559}561// End write transaction562if (FLAGS_transaction && transaction) {563status = sqlite3_step(end_trans_stmt);564StepErrorCheck(status);565status = sqlite3_reset(end_trans_stmt);566ErrorCheck(status);567}568}570status = sqlite3_finalize(replace_stmt);571ErrorCheck(status);572status = sqlite3_finalize(begin_trans_stmt);573ErrorCheck(status);574status = sqlite3_finalize(end_trans_stmt);575ErrorCheck(status);576}578void Read(Order order, int entries_per_batch) {579int status;580sqlite3_stmt *read_stmt, *begin_trans_stmt, *end_trans_stmt;582std::string read_str = "SELECT * FROM test WHERE key = ?";583std::string begin_trans_str = "BEGIN TRANSACTION;";584std::string end_trans_str = "END TRANSACTION;";586// Preparing sqlite3 statements587status = sqlite3_prepare_v2(db_, begin_trans_str.c_str(), -1,588&begin_trans_stmt, nullptr);589ErrorCheck(status);590status = sqlite3_prepare_v2(db_, end_trans_str.c_str(), -1, &end_trans_stmt,591nullptr);592ErrorCheck(status);593status = sqlite3_prepare_v2(db_, read_str.c_str(), -1, &read_stmt, nullptr);594ErrorCheck(status);596bool transaction = (entries_per_batch > 1);597for (int i = 0; i < reads_; i += entries_per_batch) {598// Begin read transaction599if (FLAGS_transaction && transaction) {600status = sqlite3_step(begin_trans_stmt);601StepErrorCheck(status);602status = sqlite3_reset(begin_trans_stmt);603ErrorCheck(status);604}606// Create and execute SQL statements607for (int j = 0; j < entries_per_batch; j++) {608// Create key value609char key[100];610int k = (order == SEQUENTIAL) ? i + j : (rand_.Next() % reads_);611snprintf(key, sizeof(key), "%016d", k);613// Bind key value into read_stmt614status = sqlite3_bind_blob(read_stmt, 1, key, 16, SQLITE_STATIC);615ErrorCheck(status);617// Execute read statement618while ((status = sqlite3_step(read_stmt)) == SQLITE_ROW) {619}620StepErrorCheck(status);622// Reset SQLite statement for another use623status = sqlite3_clear_bindings(read_stmt);624ErrorCheck(status);625status = sqlite3_reset(read_stmt);626ErrorCheck(status);627FinishedSingleOp();628}630// End read transaction631if (FLAGS_transaction && transaction) {632status = sqlite3_step(end_trans_stmt);633StepErrorCheck(status);634status = sqlite3_reset(end_trans_stmt);635ErrorCheck(status);636}637}639status = sqlite3_finalize(read_stmt);640ErrorCheck(status);641status = sqlite3_finalize(begin_trans_stmt);642ErrorCheck(status);643status = sqlite3_finalize(end_trans_stmt);644ErrorCheck(status);645}647void ReadSequential() {648int status;649sqlite3_stmt* pStmt;650std::string read_str = "SELECT * FROM test ORDER BY key";652status = sqlite3_prepare_v2(db_, read_str.c_str(), -1, &pStmt, nullptr);653ErrorCheck(status);654for (int i = 0; i < reads_ && SQLITE_ROW == sqlite3_step(pStmt); i++) {655bytes_ += sqlite3_column_bytes(pStmt, 1) + sqlite3_column_bytes(pStmt, 2);656FinishedSingleOp();657}659status = sqlite3_finalize(pStmt);660ErrorCheck(status);661}662};664} // namespace leveldb666int main(int argc, char** argv) {667std::string default_db_path;668for (int i = 1; i < argc; i++) {669double d;670int n;671char junk;672if (leveldb::Slice(argv[i]).starts_with("--benchmarks=")) {673FLAGS_benchmarks = argv[i] + strlen("--benchmarks=");674} else if (sscanf(argv[i], "--histogram=%d%c", &n, &junk) == 1 &&675(n == 0 || n == 1)) {676FLAGS_histogram = n;677} else if (sscanf(argv[i], "--compression_ratio=%lf%c", &d, &junk) == 1) {678FLAGS_compression_ratio = d;679} else if (sscanf(argv[i], "--use_existing_db=%d%c", &n, &junk) == 1 &&680(n == 0 || n == 1)) {681FLAGS_use_existing_db = n;682} else if (sscanf(argv[i], "--num=%d%c", &n, &junk) == 1) {683FLAGS_num = n;684} else if (sscanf(argv[i], "--reads=%d%c", &n, &junk) == 1) {685FLAGS_reads = n;686} else if (sscanf(argv[i], "--value_size=%d%c", &n, &junk) == 1) {687FLAGS_value_size = n;688} else if (leveldb::Slice(argv[i]) == leveldb::Slice("--no_transaction")) {689FLAGS_transaction = false;690} else if (sscanf(argv[i], "--page_size=%d%c", &n, &junk) == 1) {691FLAGS_page_size = n;692} else if (sscanf(argv[i], "--num_pages=%d%c", &n, &junk) == 1) {693FLAGS_num_pages = n;694} else if (sscanf(argv[i], "--WAL_enabled=%d%c", &n, &junk) == 1 &&695(n == 0 || n == 1)) {696FLAGS_WAL_enabled = n;697} else if (strncmp(argv[i], "--db=", 5) == 0) {698FLAGS_db = argv[i] + 5;699} else {700fprintf(stderr, "Invalid flag '%s'\n", argv[i]);701exit(1);702}703}705// Choose a location for the test database if none given with --db=<path>706if (FLAGS_db == nullptr) {707leveldb::Env::Default()->GetTestDirectory(&default_db_path);708default_db_path += "/dbbench";709FLAGS_db = default_db_path.c_str();710}712leveldb::Benchmark benchmark;713benchmark.Run();714return 0;715}
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.