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.
752static bool Between(uint64_t val, uint64_t low, uint64_t high) {753bool result = (val >= low) && (val <= high);754if (!result) {755fprintf(stderr, "Value %llu is not in range [%llu, %llu]\n",756(unsigned long long)(val), (unsigned long long)(low),757(unsigned long long)(high));758}759return result;760}762class TableTest {};764TEST(TableTest, ApproximateOffsetOfPlain) {765TableConstructor c(BytewiseComparator());766c.Add("k01", "hello");767c.Add("k02", "hello2");768c.Add("k03", std::string(10000, 'x'));769c.Add("k04", std::string(200000, 'x'));770c.Add("k05", std::string(300000, 'x'));771c.Add("k06", "hello3");772c.Add("k07", std::string(100000, 'x'));773std::vector<std::string> keys;774KVMap kvmap;775Options options;776options.block_size = 1024;777options.compression = kNoCompression;778c.Finish(options, &keys, &kvmap);780ASSERT_TRUE(Between(c.ApproximateOffsetOf("abc"), 0, 0));781ASSERT_TRUE(Between(c.ApproximateOffsetOf("k01"), 0, 0));782ASSERT_TRUE(Between(c.ApproximateOffsetOf("k01a"), 0, 0));783ASSERT_TRUE(Between(c.ApproximateOffsetOf("k02"), 0, 0));784ASSERT_TRUE(Between(c.ApproximateOffsetOf("k03"), 0, 0));785ASSERT_TRUE(Between(c.ApproximateOffsetOf("k04"), 10000, 11000));786ASSERT_TRUE(Between(c.ApproximateOffsetOf("k04a"), 210000, 211000));787ASSERT_TRUE(Between(c.ApproximateOffsetOf("k05"), 210000, 211000));788ASSERT_TRUE(Between(c.ApproximateOffsetOf("k06"), 510000, 511000));789ASSERT_TRUE(Between(c.ApproximateOffsetOf("k07"), 510000, 511000));790ASSERT_TRUE(Between(c.ApproximateOffsetOf("xyz"), 610000, 612000));791}793static bool SnappyCompressionSupported() {794std::string out;795Slice in = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";796return port::Snappy_Compress(in.data(), in.size(), &out);797}799TEST(TableTest, ApproximateOffsetOfCompressed) {800if (!SnappyCompressionSupported()) {801fprintf(stderr, "skipping compression tests\n");802return;803}805Random rnd(301);806TableConstructor c(BytewiseComparator());807std::string tmp;808c.Add("k01", "hello");809c.Add("k02", test::CompressibleString(&rnd, 0.25, 10000, &tmp));810c.Add("k03", "hello3");811c.Add("k04", test::CompressibleString(&rnd, 0.25, 10000, &tmp));812std::vector<std::string> keys;813KVMap kvmap;814Options options;815options.block_size = 1024;816options.compression = kSnappyCompression;817c.Finish(options, &keys, &kvmap);819// Expected upper and lower bounds of space used by compressible strings.820static const int kSlop = 1000; // Compressor effectiveness varies.821const int expected = 2500; // 10000 * compression ratio (0.25)822const int min_z = expected - kSlop;823const int max_z = expected + kSlop;825ASSERT_TRUE(Between(c.ApproximateOffsetOf("abc"), 0, kSlop));826ASSERT_TRUE(Between(c.ApproximateOffsetOf("k01"), 0, kSlop));827ASSERT_TRUE(Between(c.ApproximateOffsetOf("k02"), 0, kSlop));828// Have now emitted a large compressible string, so adjust expected offset.829ASSERT_TRUE(Between(c.ApproximateOffsetOf("k03"), min_z, max_z));830ASSERT_TRUE(Between(c.ApproximateOffsetOf("k04"), min_z, max_z));831// Have now emitted two large compressible strings, so adjust expected offset.832ASSERT_TRUE(Between(c.ApproximateOffsetOf("xyz"), 2 * min_z, 2 * max_z));833}835} // namespace leveldb837int 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.