DataReelDataReelC++ Transition Guide

Examples › Database Examples

database_btree

C++17 or laterDataReel 6.4.3Portable C++ (tested on Linux)

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

Modern equivalent of the gxBtree test. The B-tree index file stays a gxBtree (std::map is an in-memory tree with no file format), and the modern C++ goes into the key class and the code around it:

Legacy calls in this program

LegacyModern C++StandardNotes
gxString / UStringstd::string, std::string_viewC++98 / C++17string_view looks at text without copying it.
Find(), IFind(), FindLast()find(), a std::search with a case-folding predicate, rfind()C++98Watch out: rfind("") returns size(), where FindLast("") returns -1.
strcpy(), strncpy(), memmove() into char arraysstd::string; std::array<char, N> + string_view::copy()C++11 / C++17
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.
gxBtree, DatabaseKeyB key classeskeep gxBtree; key classes with std::array storage and correct copies (rule of three); std::set for in-memory indexesC++11 / C++17DatabaseKeyB's copy keeps the other object's db_key pointer: define the copies.

From the transition tables.

Side by side

DataReel 6.4 (legacy)

examples_database/btree/testprog.cpp 461 lines
#include "gxdlcode.h"

#if defined (__USE_ANSI_CPP__) // Use the ANSI Standard C++ library
#include <iostream>
using namespace std; // Use unqualified names for Standard C++ library
#else // Use the old iostream library by default
#include <iostream.h>
#endif // __USE_ANSI_CPP__

#include "gxdstats.h"
#include "gxbtree.h"
#include "ustring.h"

const BtreeNodeOrder_t MyKeyClassOrder = 7;
const __WORD__ MyKeyNameSize = 64;

class MyKeyClass : public DatabaseKeyB
{
public:
  MyKeyClass();
  MyKeyClass(const char *name);
  void operator=(const char *name);
  ~MyKeyClass() { }

public: // Base class interface
  size_t KeySize() { return sizeof(key_name); }
  int operator==(const DatabaseKeyB& key) const;
  int operator>(const DatabaseKeyB& key) const;

  // NOTE: This comparison function is only used if the 
  // __USE_SINGLE_COMPARE__ preprocessor directive is 
  // defined when the program is compiled.
  int CompareKey(const DatabaseKeyB& key) const;

public: // Persistent data member
  char key_name[MyKeyNameSize];
};

MyKeyClass::MyKeyClass() : DatabaseKeyB((char *)key_name)
{
  for(int i = 0; i < MyKeyNameSize; i++) key_name[i] = 0;
}

MyKeyClass::MyKeyClass(const char *name) : DatabaseKeyB((char *)key_name)
{
  strncpy(key_name, name,  MyKeyNameSize);
  key_name[ MyKeyNameSize-1] = 0; // Ensure null termination
}

void MyKeyClass::operator=(const char *name)
{
  strncpy(key_name, name,  MyKeyNameSize);
  key_name[ MyKeyNameSize-1] = 0; // Ensure null termination
}

int MyKeyClass::CompareKey(const DatabaseKeyB& key) const
// NOTE: This comparison function is only used if the 
// __USE_SINGLE_COMPARE__ preprocessor directive is 
// defined when the program is compiled.
{
  const MyKeyClass *kptr = (const MyKeyClass *)(&key);
  return strcmp(key_name, (char *)kptr->db_key);
}

int MyKeyClass::operator==(const DatabaseKeyB& key) const
{
  const MyKeyClass *kptr = (const MyKeyClass *)(&key);
  return (strcmp(key_name, (char *)kptr->db_key) == 0);
}

int MyKeyClass::operator>(const DatabaseKeyB& key) const
{
  const MyKeyClass *kptr = (const MyKeyClass *)(&key);
  return (strcmp(key_name, (char *)kptr->db_key) > 0);
}

void PrintNode(BtreeNode *n)
// Prints a single B-tree node.
{
  UString intbuf;
  MyKeyClass key;
  BtreeKeyCount_t i = (BtreeKeyCount_t)0;
  cout << "[";
  intbuf << clear << (FAU_t)n->node_address;
  cout << "Node Address: " << intbuf.c_str(); 
  cout << ", ";
  intbuf << clear << (FAU_t)n->left_child;
  cout << "Left Node: " << intbuf.c_str();
  cout << "]";
  while (i < n->key_count) {
    n->LoadKey(key, (BtreeKeyLocation_t)i);
    cout << " <";
    cout << key.key_name;
    intbuf << clear << (FAU_t)key.right_child;
    cout << ", " << intbuf.c_str();
    cout << "> ";
    i++;
  }
  cout << "\n";
}

typedef void (*BtreeVisitFunc)(BtreeNode *Node); 

void BtreeWalk(FAU_t t, BtreeVisitFunc Visit, gxBtree *tree)
// This is a recursive function demo used to walk through
// the B-tree node by node.
{
  BtreeKeyCount_t i;

  // Ensure that the in memory buffers and the file data
  // stay in sync during multiple file access.
  if(tree) {
    tree->TestTree();
  }
  else {
    return;
  }

  BtreeNode n(tree->KeySize(), tree->NodeOrder());
  
  if(t != (FAU_t)0) {
    tree->ReadNode(n, t);
    n.node_address = t;
    (*Visit)(&n); // Process the node data
    BtreeKeyCount_t nc = n.key_count;
    FAU_t p;

    for(i = (BtreeKeyCount_t)-1; i < nc; i++) {
      p = n.GetBranch(i);
      BtreeWalk(p, Visit, tree);  
    }
  }
}

void PausePrg()
{
  cout << "\n";
  cout << "Press enter to continue..." << "\n";
  cin.get();
}

void BtreeStatus(gxBtree &btx)
{
  UString intbuf;
  cout << "\n";
  intbuf << clear << (FAU_t)btx.Root();
  cout << "Root address =      " << intbuf.c_str() << "\n";
  cout << "Number of trees =   " << btx.NumTrees() << "\n";
  cout << "Number of entries = " << btx.NumKeys() << "\n";
  cout << "Number of nodes =   " << btx.NumNodes() << "\n";
  cout << "B-Tree order =      " << btx.NodeOrder() << "\n";
  cout << "B-Tree height =     " << btx.BtreeHeight() << "\n";
  PausePrg();
}

void BuildTree(gxBtree &btx)
{
  // Set to true to print the tree with each insertion and deletion
  int print_tree = 0;

  char *aa1 = "DOG";
  char *bb1 = "CAT";
  char *cc1 = "FISH";
  char *dd1 = "MOUSE";
  char *ee1 = "BIRD";
  char *ff1 = "PIG";
  char *gg1 = "HORSE";
  char *hh1 = "LION";
  char *ii1 = "SNAKE";
  char *jj1 = "COW";
  char *kk1 = "ARMADILLO";
  char *ll1 = "GROUPER";
  char *mm1 = "RAT";
  char *nn1 = "MONKEY";
  char *oo1 = "ZEBRA";
  char *pp1 = "STARFISH";
  char *qq1 = "LIZARD";
  char *rr1 = "CRAB";
  char *ss1 = "SNAIL";
  char *tt1 = "GORILLA";
  char *uu1 = "LOBSTER";
  char *vv1 = "TURKEY";
  char *ww1 = "BEETLE";
  char *xx1 = "SHARK";
  char *yy1 = "CLAM";
  char *zz1 = "OYSTER";

  char *aa2 = "FLEA";
  char *bb2 = "BUTTERFLY";
  char *cc2 = "SPARROW";
  char *dd2 = "GOLDFISH";
  char *ee2 = "TIGER";
  char *ff2 = "BEAR";
  char *gg2 = "TROUTE";
  char *hh2 = "MOOSE";
  char *ii2 = "DEAR";
  char *jj2 = "SALMON";
  char *kk2 = "TUNA";
  char *ll2 = "GAZELLE";
  char *mm2 = "SLOTH";
  char *nn2 = "SPIDER";
  char *oo2 = "LEAPORD";
  char *pp2 = "GIRAFFE";
  char *qq2 = "MUSTANG";
  char *rr2 = "CONDOR";
  char *ss2 = "KANGAROO";
  char *tt2 = "SKUNK";
  char *uu2 = "FOX";
  char *vv2 = "PANTER";
  char *ww2 = "CHEETAH";
  char *xx2 = "TOUCAN";
  char *yy2 = "PARROT";
  char *zz2 = "BUFFALO";

  char *aa3 = "KOALA";
  char *bb3 = "HORSEFLY";
  char *cc3 = "ANACONDA";
  char *dd3 = "CROCODILE";
  char *ee3 = "RACCOON";
  char *ff3 = "ALLIGATOR";
  char *gg3 = "RABBIT";
  char *hh3 = "WHALE";
  char *ii3 = "ANT";
  char *jj3 = "CRANE";
  char *kk3 = "LONGHORN";
  char *ll3 = "CANARY";
  char *mm3 = "WOMBAT";
  char *nn3 = "WOLFHOUND";
  char *oo3 = "COUGAR";
  char *pp3 = "BAT";
  char *qq3 = "OWL";
  char *rr3 = "SHRIMP";
  char *ss3 = "SCALLOP";
  char *tt3 = "SQUID";
  char *uu3 = "PYTHON";
  char *vv3 = "SARDINE";
  char *ww3 = "TAPIR";
  char *xx3 = "ELEPHANT";
  char *yy3 = "EEL";
  char *zz3 = "RHINOCEROS";

  char *aa4 = "LAMB";
  char *bb4 = "BISON";
  char *cc4 = "GRASSHOPPER";
  char *dd4 = "MACKEREL";
  char *ee4 = "FERRET";
  char *ff4 = "WASP";
  char *gg4 = "CATERPILLAR";
  char *hh4 = "MILLIPEDE";
  char *ii4 = "CENTIPEDE";
  char *jj4 = "MOSQUITO";
  char *kk4 = "POSSUM";
  char *ll4 = "DUCK";
  char *mm4 = "WEASEL";
  char *nn4 = "CARIBOU";
  char *oo4 = "ANTELOPE";
  char *pp4 = "SALAMANDER";
  char *qq4 = "NEWT";
  char *rr4 = "CHICKEN";
  char *ss4 = "BULL";
  char *tt4 = "COBRA";
  char *uu4 = "CHIMPANZEE";
  char *vv4 = "URCHIN";
  char *ww4 = "CROW";
  char *xx4 = "WOLF";
  char *yy4 = "SPONGE";
  char *zz4 = "JELLYFISH";

  const int NKEYS = 104;
  char *keys[NKEYS] = { aa1, bb1, cc1, dd1, ee1, ff1, gg1, hh1, ii1, jj1,
			kk1, ll1, mm1, nn1, oo1, pp1, qq1, rr1, ss1, tt1,
                        uu1, vv1, ww1, xx1, yy1, zz1,

			aa2, bb2, cc2, dd2, ee2, ff2, gg2, hh2, ii2, jj2,
			kk2, ll2, mm2, nn2, oo2, pp2, qq2, rr2, ss2, tt2,
                        uu2, vv2, ww2, xx2, yy2, zz2,

			aa3, bb3, cc3, dd3, ee3, ff3, gg3, hh3, ii3, jj3,
			kk3, ll3, mm3, nn3, oo3, pp3, qq3, rr3, ss3, tt3,
                        uu3, vv3, ww3, xx3, yy3, zz3,

			aa4, bb4, cc4, dd4, ee4, ff4, gg4, hh4, ii4, jj4,
			kk4, ll4, mm4, nn4, oo4, pp4, qq4, rr4, ss4, tt4,
                        uu4, vv4, ww4, xx4, yy4, zz4 };

  MyKeyClass key;
  MyKeyClass compare_key;
  int i, rv;

  const int INSERTIONS = 104;

  cout << "Inserting " << INSERTIONS << " keys..." << "\n";
  for(i = 0; i < INSERTIONS; i++) {
    key = keys[i];  

    rv = btx.Insert(key, compare_key);
    
    if(print_tree) {
      cout << "Inserting " << keys[i] << " - " << i << "\n";
      BtreeWalk(btx.Root(), PrintNode, &btx);
      PausePrg();
    }

    if(rv != 1) {
      cout << "\n" << "Problem adding " << keys[i] << " - " << i << "\n";
      return;
    }
  }

  btx.Flush(); // Flush the tree following a batch insert
  BtreeStatus(btx);

  cout << "Verifying the insertions..." << "\n";
  for(i = 0; i < INSERTIONS; i++) {
    key = keys[i];      
    rv = btx.Find(key, compare_key);
    if(rv != 1) {
      cout << "Error finding key " << keys[i] << " - " << i << "\n";
      return;
    }
  }

  cout << "Deleting all the entries..." << "\n";
  for(i = 0; i < INSERTIONS; i++) {
    key = keys[i];      

    // Testing the deletion functions
    // rv = btx.LazyDelete(key, compare_key);
    rv = btx.Delete(key, compare_key);

    if(rv != 1) {
      cout << "Error deleting key " << keys[i] << " - " << i << "\n";
      return;
    }

    if(print_tree) {
      cout << "Deleting " << keys[i] << " - " << i << "\n";
      BtreeWalk(btx.Root(), PrintNode, &btx);
      PausePrg();
    }

    // Verify the remaining key locations
    for(int j = INSERTIONS-1; j != i; j--) {
      key = keys[j];      
      rv = btx.Find(key, compare_key);
      if(rv != 1) {
	cout << "Error finding key " << keys[j] << " - " << j << "\n";
	cout << "After deleting key " << keys[i] << " - " << i << "\n";
	return;
      }
    }
  }

  btx.Flush(); // Flush the tree following a batch delete
  BtreeStatus(btx);

  cout << "Re-inserting " << INSERTIONS << " keys..." << "\n";
  for(i = 0; i < INSERTIONS; i++) {
    key = keys[i];  

    rv = btx.Insert(key, compare_key);

    if(rv != 1) {
      cout << "\n" << "Problem adding " << keys[i] << " - " << i << "\n";
      return;
    }
  }

  btx.Flush(); // Flush the tree following a batch insert
  BtreeStatus(btx);
}

int main(int argv, char **argc)
{
  const char *fname = "testfile.btx"; // File name of this database
  char rev_letter = gxDatabaseRevisionLetter; // Set the default rev letter
  if(argv == 2) { // Set a specified revision letter
    rev_letter = *argc[1];
    if(rev_letter == '0') rev_letter = '\0';
    // Valid rev letters are:
    // Rev 0
    // Rev 'A' or 'a'
    // Rev 'B' or 'b'
    // Rev 'C' or 'c'
    // Rev 'D' or 'd'
    // Rev 'E' or 'e'
    // NOTE: The gxDatabase class will set invalid revision letters
    // to the version set by the gxDatabaseRevisionLetter constant.  
  }

  MyKeyClass key, compare_key;
  gxBtree btx(key, MyKeyClassOrder);

  // Create a new B-Tree index file
  btx.Create(fname, rev_letter);
  if(CheckError(btx.gxDatabasePtr()) != 0) return 1;
  
  cout << "Database key size      = " << key.SizeOfDatabaseKey() << "\n";
  cout << "Key entry size         = "
       << (key.SizeOfDatabaseKey() * (MyKeyClassOrder-1)) << "\n";

  BtreeNode n(key.SizeOfDatabaseKey(), MyKeyClassOrder);
  cout << "B-Tree node size       = " << n.SizeOfBtreeNode() << "\n";
  cout << "B-Tree header size     = " << sizeof(gxBtreeHeader) << "\n";
  cout << "B-Tree order           = " << MyKeyClassOrder << "\n";
  cout << "Total B-Tree node size = " << btx.TotalNodeSize() << "\n";

  PausePrg();

  // Build the Btree
  BuildTree(btx);
  
  btx.FindFirst(key);
  cout << "First key = " << key.key_name << "\n";

  btx.FindLast(key);
  cout << "Last key = " << key.key_name << "\n";

  cout << "\n";
  
  cout << "Recursively walking through the B-Tree node by node..." << "\n";
  PausePrg();

  BtreeWalk(btx.Root(), PrintNode, &btx);

  cout << "\n";
  cout << "Walking through the tree in sort order" << "\n";
  PausePrg();
  
  // Walk through the tree starting at the first node
  if(btx.FindFirst(key)) {
    cout << key.key_name << ' ';
    while(btx.FindNext(key, compare_key))
      cout << key.key_name << ' ';
  }
  else {
    cout << "Could not find first key" << "\n";
    cout << btx.DatabaseExceptionMessage();
  }
  cout  << "\n";
  PausePrg();

  // Walk backward through the tree starting at the last node
  if(btx.FindLast(key)) {
    cout << key.key_name << ' ';
    while(btx.FindPrev(key, compare_key)) {
      cout << key.key_name << ' ';
    }
  }
  else {
    cout << "Could not find last key" << "\n";
    cout << btx.DatabaseExceptionMessage();
  }
  cout << "\n";

  return 0;
}
// ----------------------------------------------------------- //
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //

Modern C++ (C++17)

examples_modern/database/btree/modern.cpp 132 lines
#include <algorithm>
#include <array>
#include <cstring>
#include <filesystem>
#include <iostream>
#include <set>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>

#include "gxbtree.h"

class NameKey : public DatabaseKeyB {
public:
  static constexpr std::size_t name_size = 64;

  NameKey() : DatabaseKeyB(key_.data()) {}
  explicit NameKey(std::string_view s) : NameKey() { assign(s); }
  NameKey(const NameKey &o) : DatabaseKeyB(key_.data()), key_(o.key_) {
    right_child = o.right_child;   // db_key keeps pointing at our key_
  }
  NameKey &operator=(const NameKey &o) {
    key_ = o.key_;
    right_child = o.right_child;
    return *this;
  }

  void assign(std::string_view s) {
    key_.fill(0);
    s.copy(key_.data(), std::min(s.size(), name_size - 1));
  }
  std::string_view name() const { return key_.data(); }

  // Base class interface
  size_t KeySize() override { return key_.size(); }
  int CompareKey(const DatabaseKeyB &k) const
  { return std::strncmp(key_.data(), static_cast<const char *>(k.db_key), name_size); }
  int operator==(const DatabaseKeyB &k) const { return CompareKey(k) == 0; }
  int operator>(const DatabaseKeyB &k) const { return CompareKey(k) > 0; }

private:
  std::array<char, name_size> key_{};
};

const std::vector<std::string_view> keys = {
  "DOG", "CAT", "FISH", "MOUSE", "BIRD", "PIG", "HORSE", "LION", "SNAKE",
  "COW", "ARMADILLO", "GROUPER", "RAT", "MONKEY", "ZEBRA", "STARFISH",
  "LIZARD", "CRAB", "SNAIL", "GORILLA", "LOBSTER", "TURKEY", "BEETLE",
  "SHARK", "CLAM", "OYSTER", "FLEA", "BUTTERFLY", "SPARROW", "GOLDFISH",
  "TIGER", "BEAR", "TROUTE", "MOOSE", "DEAR", "SALMON", "TUNA", "GAZELLE",
  "SLOTH", "SPIDER", "LEAPORD", "GIRAFFE", "MUSTANG", "CONDOR", "KANGAROO",
  "SKUNK", "FOX", "PANTER", "CHEETAH", "TOUCAN", "PARROT", "BUFFALO",
  "KOALA", "HORSEFLY", "ANACONDA", "CROCODILE", "RACCOON", "ALLIGATOR",
  "RABBIT", "WHALE", "ANT", "CRANE", "LONGHORN", "CANARY", "WOMBAT",
  "WOLFHOUND", "COUGAR", "BAT", "OWL", "SHRIMP", "SCALLOP", "SQUID",
  "PYTHON", "SARDINE", "TAPIR", "ELEPHANT", "EEL", "RHINOCEROS", "LAMB",
  "BISON", "GRASSHOPPER", "MACKEREL", "FERRET", "WASP", "CATERPILLAR",
  "MILLIPEDE", "CENTIPEDE", "MOSQUITO", "POSSUM", "DUCK", "WEASEL",
  "CARIBOU", "ANTELOPE", "SALAMANDER", "NEWT", "CHICKEN", "BULL", "COBRA",
  "CHIMPANZEE", "URCHIN", "CROW", "WOLF", "SPONGE", "JELLYFISH"};

void status(gxBtree &btx)
{
  std::cout << "Root " << btx.Root() << ", keys " << btx.NumKeys()
            << ", nodes " << btx.NumNodes() << ", order " << btx.NodeOrder()
            << ", height " << btx.BtreeHeight() << "\n";
}

void require(bool ok, const std::string &what)
{
  if(!ok) throw std::runtime_error(what);
}

int main()
{
  const std::filesystem::path fname = "modern_btree.btx";
  const BtreeNodeOrder_t order = 7;
  try {
    NameKey key, compare_key;
    gxBtree btx(key, order);
    btx.Create(fname.string().c_str());
    require(btx.GetDatabaseError() == gxDBASE_NO_ERROR, "create failed");

    std::cout << "Inserting " << keys.size() << " keys...\n";
    for(auto k : keys) { key.assign(k); require(btx.Insert(key, compare_key) == 1, "insert"); }
    btx.Flush();
    status(btx);

    std::cout << "Verifying the insertions...\n";
    for(auto k : keys) { key.assign(k); require(btx.Find(key, compare_key) == 1, "find"); }

    std::cout << "Deleting all the entries...\n";
    for(auto k : keys) { key.assign(k); require(btx.Delete(key, compare_key) == 1, "delete"); }
    btx.Flush();
    status(btx);

    std::cout << "Re-inserting " << keys.size() << " keys...\n";
    for(auto k : keys) { key.assign(k); require(btx.Insert(key, compare_key) == 1, "insert"); }
    btx.Flush();
    status(btx);

    std::vector<std::string> on_disk;          // Sort order from the file
    for(bool ok = btx.FindFirst(key); ok; ok = btx.FindNext(key, compare_key))
      on_disk.emplace_back(key.name());

    std::set<std::string_view> in_memory(keys.begin(), keys.end());
    bool same = std::equal(on_disk.begin(), on_disk.end(), in_memory.begin(),
                           in_memory.end());
    std::cout << "First key " << on_disk.front() << ", last key "
              << on_disk.back() << "\n";
    std::cout << "gxBtree order matches std::set order: "
              << (same ? "yes" : "no") << "\n";

    std::cout << "Walking backward:";
    int n = 0;
    for(bool ok = btx.FindLast(key); ok && n < 5; ok = btx.FindPrev(key, compare_key), n++)
      std::cout << ' ' << key.name();
    std::cout << " ...\n";
    btx.Close();
    std::filesystem::remove(fname);
    return same ? 0 : 1;
  }
  catch(const std::exception &e) {
    std::cout << "B-tree error: " << e.what() << "\n";
    return 1;
  }
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //

Build and run

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

Output of the modern program

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

Inserting 104 keys...
Root 4984, keys 104, nodes 27, order 7, height 3
Verifying the insertions...
Deleting all the entries...
Root 184, keys 0, nodes 1, order 7, height 1
Re-inserting 104 keys...
Root 7384, keys 104, nodes 27, order 7, height 3
First key ALLIGATOR, last key ZEBRA
gxBtree order matches std::set order: yes
Walking backward: ZEBRA WOMBAT WOLFHOUND WOLF WHALE ...