DataReelDataReelC++ Transition Guide

Examples › Database Examples

database_journal_salt

C++17 or laterDataReel 6.4.3POSIX (Linux, UNIX)

Legacy: examples_database/journal_salt/testprog.cpp · Modern: examples_modern/database/journal_salt/modern.cpp

Modern equivalent of the journal salt test: 20 processes forked from one parent each start a journal, and every journal header must get a different salt (a salt copied into every child would let one process's journal records pass as another's). The modern parts are a std::array read of the header through std::ifstream, the salt assembled from little-endian bytes in a loop, std::set to count distinct values, std::filesystem::path for the names and an RAII cleanup of the files. fork() stays: the test is about processes.

Legacy calls in this program

LegacyModern C++StandardNotes
new / deleteautomatic objects, containers, std::unique_ptr / std::make_uniqueC++11 / C++14Ownership is in the type, and release() shows a hand-over.
gxDatabasekeep gxDatabase: a stack object, errors as exceptions, static_assert(std::is_trivially_copyable_v<Record>)C++17The standard library has no database file; the file format is DataReel's.
gxdJournal Begin()/Commit()/Rollback()keep gxdJournal; an RAII Transaction that rolls back unless commit() was calledC++11

From the transition tables.

Side by side

DataReel 6.4 (legacy)

examples_database/journal_salt/testprog.cpp 44 lines
// testprog.cpp (examples_database/journal_salt) - 11/16/2026: DataReel 6.x port of the 7.4 tests/journal_salt_test.cpp.
// the same second. The salt was time(0), a counter and the journal object's address, all equal in such children; a
// recovery then took the previous session's records (left after a cleared header) for its own and replayed old
// transactions (found by the gxObjRepl property test: a copy went back from applied 4 to 3).
//   journal_salt_test DIR      (exit 0 = all salts distinct)
#include "gxdlcode.h"
#include <stdio.h>
#include <stdint.h>
#include <string>
#include <set>
#include <unistd.h>
#include <sys/wait.h>
#include "gxdbase.h"
#include "gxdjrnl.h"

int main(int argc, char **argv)
{
  std::string dir = argc > 1 ? argv[1] : ".";
  const int N = 20;
  gxdJournal *j = new gxdJournal;                 // the same address in every child (forked after this)
  for(int i = 0; i < N; i++) {
    if(fork() == 0) {
      std::string db = dir + "/d" + std::to_string(i) + ".gxd", jn = dir + "/j" + std::to_string(i) + ".jnl";
      gxDatabase f;
      if(j->Open(jn.c_str()) != gxDBASE_NO_ERROR || f.Create(db.c_str()) != gxDBASE_NO_ERROR || j->Attach(&f) != gxDBASE_NO_ERROR) _exit(2);
      j->Begin(); f.Alloc(16); j->Commit(0);      // writes the journal header (with its salt); Commit(0) keeps it in the journal
      _exit(0);                                    // no Close(): the header stays as written
    }
  }
  int bad = 0;
  for(int i = 0; i < N; i++) { int st = 0; wait(&st); if(!WIFEXITED(st) || WEXITSTATUS(st) != 0) bad++; }
  std::set<uint32_t> salts;
  for(int i = 0; i < N; i++) {
    std::string jn = dir + "/j" + std::to_string(i) + ".jnl";
    FILE *f = fopen(jn.c_str(), "rb"); unsigned char h[16] = {0};
    if(!f || fread(h, 1, 16, f) != 16) { bad++; if(f) fclose(f); continue; }
    fclose(f);
    uint32_t s = (uint32_t)h[12] | ((uint32_t)h[13] << 8) | ((uint32_t)h[14] << 16) | ((uint32_t)h[15] << 24);
    salts.insert(s);
  }
  printf("%d children, %zu distinct salts, %d errors\n", N, salts.size(), bad);
  printf("%s journal salts\n", (salts.size() == (size_t)N && !bad) ? "PASS" : "FAIL");
  return (salts.size() == (size_t)N && !bad) ? 0 : 1;
}

Modern C++ (C++17)

examples_modern/database/journal_salt/modern.cpp 61 lines
#include <array>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <set>
#include <string>
#include <sys/wait.h>
#include <unistd.h>

#include "gxdbase.h"
#include "gxdjrnl.h"

namespace fs = std::filesystem;

int main(int argc, char **argv)
{
  const fs::path dir = argc > 1 ? argv[1] : ".";
  const int N = 20;
  auto db_name = [&](int i) { return dir / ("modern_d" + std::to_string(i) + ".gxd"); };
  auto jn_name = [&](int i) { return dir / ("modern_j" + std::to_string(i) + ".jnl"); };

  auto *j = new gxdJournal;                  // Same address in every child
  for(int i = 0; i < N; i++) {
    if(fork() == 0) {
      gxDatabase f;
      if(j->Open(jn_name(i).string().c_str()) != gxDBASE_NO_ERROR ||
         f.Create(db_name(i).string().c_str()) != gxDBASE_NO_ERROR ||
         j->Attach(&f) != gxDBASE_NO_ERROR) _exit(2);
      j->Begin(); f.Alloc(16); j->Commit(0);  // Writes the journal header
      _exit(0);                               // No Close(): keep the header
    }
  }
  int bad = 0;
  for(int i = 0; i < N; i++) {
    int st = 0;
    wait(&st);
    if(!WIFEXITED(st) || WEXITSTATUS(st) != 0) bad++;
  }

  std::set<std::uint32_t> salts;
  for(int i = 0; i < N; i++) {
    std::array<unsigned char, 16> h{};
    std::ifstream in(jn_name(i), std::ios::binary);
    if(!in.read(reinterpret_cast<char *>(h.data()), h.size())) { bad++; continue; }
    std::uint32_t s = 0;
    for(int b = 3; b >= 0; b--) s = (s << 8) | h[std::size_t(12 + b)]; // Little endian
    salts.insert(s);
  }
  for(int i = 0; i < N; i++) { fs::remove(db_name(i)); fs::remove(jn_name(i)); }
  delete j;

  bool ok = salts.size() == std::size_t(N) && !bad;
  std::cout << N << " children, " << salts.size() << " distinct salts, " << bad
            << " errors\n" << (ok ? "PASS" : "FAIL") << " journal salts\n";
  return ok ? 0 : 1;
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //

Build and run

cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=17
cmake --build build-modern --target database_journal_salt
./build-modern/database_journal_salt

Output of the modern program

From run_modern_tests.sh on RHEL 10, GCC 14, C++20.

20 children, 20 distinct salts, 0 errors
PASS journal salts