Examples › Database Examples
database_fuzz_gen_seeds
C++20 or laterDataReel 6.4.3Portable C++ (tested on Linux)
Legacy: examples_database/fuzz/gen_seeds.cpp · Modern: examples_modern/database/fuzz/gen_seeds.cpp
Modern equivalent of the seed file generator for the database fuzz targets: a data file, an index and a journal with two committed batches, copied into gxd/, btx/ and jnl/ folders. The legacy program is already mostly C++17 (std::filesystem); this one also uses a C++20 std::format_to_n into a std::array record instead of snprintf, a std::filesystem::path built with operator/, a key class with safe copies, and an RAII guard that commits a batch or rolls it back.
Usage: gen_seeds [DIR] (default: modern_seeds)
Legacy calls in this program
| Legacy | Modern C++ | Standard | Notes |
|---|---|---|---|
| strcpy(), strncpy(), memmove() into char arrays | std::string; std::array<char, N> + string_view::copy() | C++11 / C++17 | |
| gxDatabase | keep gxDatabase: a stack object, errors as exceptions, static_assert(std::is_trivially_copyable_v<Record>) | C++17 | The standard library has no database file; the file format is DataReel's. |
| gxBtree, DatabaseKeyB key classes | keep gxBtree; key classes with std::array storage and correct copies (rule of three); std::set for in-memory indexes | C++11 / C++17 | DatabaseKeyB's copy keeps the other object's db_key pointer: define the copies. |
| gxdJournal Begin()/Commit()/Rollback() | keep gxdJournal; an RAII Transaction that rolls back unless commit() was called | C++11 |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_database/fuzz/gen_seeds.cpp 70 lines
// gen_seeds.cpp - seed files for fuzz_db: a small data file, its index // and a journal holding two committed transactions (copied before the // journal is checkpointed). Usage: gen_seeds DIR #include "gxdlcode.h" #include <cstdio> #include <cstring> #include <filesystem> #include <string> #include "gxbtree.h" #include "gxdjrnl.h" namespace fs = std::filesystem; struct SdKeyData { char k[16]; FAU addr; }; class SdKey : public DatabaseKeyB { public: SdKey() : DatabaseKeyB((char *)&d) { memset(&d, 0, sizeof(d)); } size_t KeySize() { return sizeof(d); } int CompareKey(const DatabaseKeyB &o) const { return memcmp(d.k, ((const SdKeyData *)o.db_key)->k, 16); } int operator==(const DatabaseKeyB &o) const { return CompareKey(o) == 0; } int operator>(const DatabaseKeyB &o) const { return CompareKey(o) > 0; } SdKeyData d; }; int main(int argc, char **argv) { std::string dir = argc > 1 ? argv[1] : "."; fs::create_directories(dir + "/db"); std::string gxd = dir + "/db/seed.gxd", btx_name = dir + "/db/seed.btx"; gxdJournal j; j.Open((dir + "/db/seed.jnl").c_str()); j.SetWriteMode(gxdJOURNAL_WRITE_BACK); gxDatabase db; SdKey type; gxBtree btx(type, 16); db.Create(gxd.c_str()); btx.Create(btx_name.c_str()); j.Attach(&db); j.Attach(&btx); for(int b = 0; b < 2; b++) { j.Begin(); for(int i = 0; i < 150; i++) { char rec[64]; int id = b * 150 + i; snprintf(rec, sizeof(rec), "%016d record %d", id, id); FAU_t a = db.Alloc(sizeof(rec)); db.Write(rec, sizeof(rec), a); SdKey key, cmp; memcpy(key.d.k, rec, 16); key.d.addr = a; btx.Insert(key, cmp); } j.Commit(1); } fs::create_directories(dir + "/jnl"); fs::copy_file(dir + "/db/seed.jnl", dir + "/jnl/seed.jnl", fs::copy_options::overwrite_existing); j.Close(); btx.Close(); db.Close(); fs::create_directories(dir + "/gxd"); fs::create_directories(dir + "/btx"); fs::copy_file(gxd, dir + "/gxd/seed.gxd", fs::copy_options::overwrite_existing); fs::copy_file(btx_name, dir + "/btx/seed.btx", fs::copy_options::overwrite_existing); printf("seeds in %s/{gxd,btx,jnl}\n", dir.c_str()); return 0; }
Modern C++ (C++20)
examples_modern/database/fuzz/gen_seeds.cpp 89 lines
#include <algorithm> #include <array> #include <cstring> #include <filesystem> #include <format> #include <iostream> #include "gxbtree.h" #include "gxdjrnl.h" namespace fs = std::filesystem; struct SeedKeyData { std::array<char, 16> k; FAU addr; }; class SeedKey : public DatabaseKeyB { public: SeedKey() : DatabaseKeyB(&d_) { std::memset(&d_, 0, sizeof d_); } SeedKey(const SeedKey &o) : DatabaseKeyB(&d_), d_(o.d_) { right_child = o.right_child; } SeedKey &operator=(const SeedKey &o) { d_ = o.d_; right_child = o.right_child; return *this; } SeedKeyData &data() { return d_; } size_t KeySize() override { return sizeof d_; } int CompareKey(const DatabaseKeyB &o) const { return std::memcmp(d_.k.data(), static_cast<const SeedKeyData *>(o.db_key)->k.data(), 16); } int operator==(const DatabaseKeyB &o) const { return CompareKey(o) == 0; } int operator>(const DatabaseKeyB &o) const { return CompareKey(o) > 0; } private: SeedKeyData d_; }; class Batch { // Commit or roll back public: explicit Batch(gxdJournal &j) : j_(j) { j_.Begin(); } void commit() { j_.Commit(1); done_ = true; } ~Batch() { if(!done_) j_.Rollback(); } private: gxdJournal &j_; bool done_ = false; }; int main(int argc, char **argv) { const fs::path dir = argc > 1 ? argv[1] : "modern_seeds"; fs::create_directories(dir / "db"); const fs::path gxd = dir / "db" / "seed.gxd", btx_name = dir / "db" / "seed.btx", jnl = dir / "db" / "seed.jnl"; { gxdJournal j; j.Open(jnl.string().c_str()); j.SetWriteMode(gxdJOURNAL_WRITE_BACK); gxDatabase db; SeedKey type; gxBtree btx(type, 16); db.Create(gxd.string().c_str()); btx.Create(btx_name.string().c_str()); j.Attach(&db); j.Attach(&btx); for(int b = 0; b < 2; b++) { Batch batch(j); for(int i = 0; i < 150; i++) { std::array<char, 64> rec{}; int id = b * 150 + i; std::format_to_n(rec.data(), rec.size() - 1, "{:016} record {}", id, id); FAU_t a = db.Alloc(__ULWORD__(rec.size())); db.Write(rec.data(), __ULWORD__(rec.size()), a); SeedKey key, cmp; std::copy_n(rec.begin(), 16, key.data().k.begin()); key.data().addr = a; btx.Insert(key, cmp); } batch.commit(); } fs::create_directories(dir / "jnl"); fs::copy_file(jnl, dir / "jnl" / "seed.jnl", fs::copy_options::overwrite_existing); j.Close(); // Before the files close } fs::create_directories(dir / "gxd"); fs::create_directories(dir / "btx"); fs::copy_file(gxd, dir / "gxd" / "seed.gxd", fs::copy_options::overwrite_existing); fs::copy_file(btx_name, dir / "btx" / "seed.btx", fs::copy_options::overwrite_existing); std::cout << "seeds in " << dir.string() << "/{gxd,btx,jnl}\n"; for(auto sub : {"gxd", "btx", "jnl"}) for(const auto &e : fs::directory_iterator(dir / sub)) std::cout << " " << e.path().string() << " (" << e.file_size() << " bytes)\n"; return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=20 cmake --build build-modern --target database_fuzz_gen_seeds ./build-modern/database_fuzz_gen_seeds
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
seeds in modern_seeds/{gxd,btx,jnl}
modern_seeds/gxd/seed.gxd (28896 bytes)
modern_seeds/btx/seed.btx (19688 bytes)
modern_seeds/jnl/seed.jnl (4194304 bytes)