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) 2018 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/env.h"7#include "port/port.h"8#include "util/env_windows_test_helper.h"9#include "util/testharness.h"11namespace leveldb {13static const int kMMapLimit = 4;15class EnvWindowsTest {16public:17static void SetFileLimits(int mmap_limit) {18EnvWindowsTestHelper::SetReadOnlyMMapLimit(mmap_limit);19}21EnvWindowsTest() : env_(Env::Default()) {}23Env* env_;24};26TEST(EnvWindowsTest, TestOpenOnRead) {27// Write some test data to a single file that will be opened |n| times.28std::string test_dir;29ASSERT_OK(env_->GetTestDirectory(&test_dir));30std::string test_file = test_dir + "/open_on_read.txt";32FILE* f = fopen(test_file.c_str(), "w");33ASSERT_TRUE(f != nullptr);34const char kFileData[] = "abcdefghijklmnopqrstuvwxyz";35fputs(kFileData, f);36fclose(f);38// Open test file some number above the sum of the two limits to force39// leveldb::WindowsEnv to switch from mapping the file into memory40// to basic file reading.41const int kNumFiles = kMMapLimit + 5;42leveldb::RandomAccessFile* files[kNumFiles] = {0};43for (int i = 0; i < kNumFiles; i++) {44ASSERT_OK(env_->NewRandomAccessFile(test_file, &files[i]));45}46char scratch;47Slice read_result;48for (int i = 0; i < kNumFiles; i++) {49ASSERT_OK(files[i]->Read(i, 1, &read_result, &scratch));50ASSERT_EQ(kFileData[i], read_result[0]);51}52for (int i = 0; i < kNumFiles; i++) {53delete files[i];54}55ASSERT_OK(env_->DeleteFile(test_file));56}58} // namespace leveldb60int main(int argc, char** argv) {61// All tests currently run with the same read-only file limits.62leveldb::EnvWindowsTest::SetFileLimits(leveldb::kMMapLimit);63return leveldb::test::RunAllTests();64}
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.