Write-Ahead Journal (gxdJournal)
Overview
gxdJournal (gxdjrnl.h, new in 6.3) makes changes to one or more gxDatabase and gxBtree files (and BtreeCache) atomic and durable: changes between Begin() and Commit() reach the files all or none, after a program crash or a power loss. Open the journal before the files; Open() replays committed transactions left in it. The journal file is created owner-only (mode 0600, or a protected DACL on Windows), an existing journal is made owner-only before use, and a clean Close() removes it. Keep journals in a folder only the database owner can write: a journal names the files it repairs. Programs that compile the library sources must add gxdjrnl.cpp and gxdpcache.cpp.
gxdJournal j;
j.Open("orders.jnl"); // Replays committed transactions
db.Open("orders.gxd"); // Open the files after the journal
btx.Open("orders.btx");
j.Attach(&db); j.Attach(&btx);
j.Begin();
// ... changes to db and btx ...
j.Commit(); // Durable when it returns
j.Close(); // Checkpoint, before closing the files
btx.Close(); db.Close();
6.4: the journal header salt mixes the OS random source (/dev/urandom; on Windows the performance counter, the process id and the tick count), a fine clock, the process id, a counter and the object's address, so processes forked from one parent in the same second no longer get the same salt (a recovery could replay the previous session's records). The journal format is unchanged.
Functions
gxDatabaseError gxdJournal::Open(const char *jname) / gxDatabaseError gxdJournal::Close()Recover and open the journal; roll back or checkpoint and close it.
gxDatabaseError gxdJournal::Attach(gxDatabase *f, size_t cache_bytes = gxdDefaultPageCacheSize) / Attach(gxBtree *btx, size_t cache_bytes = gxdDefaultPageCacheSize) / Attach(BtreeCache *cache) / gxDatabaseError gxdJournal::Detach()Add a file (or a B-tree cache) to the journal; Detach() checkpoints and removes every file.
gxDatabaseError gxdJournal::Begin() / Commit(int durable = 1) / Rollback() / Checkpoint()Start, commit (durable: synced to disk before it returns) or roll back a transaction, and write committed pages to the files.
void gxdJournal::SetWriteMode(gxdJournalWriteMode mode) / SetSyncLevel(gxdJournalSyncLevel level) / SetCheckpointSize(size_t bytes) / SetWriteLock(int lock)gxdJOURNAL_WRITE_THROUGH (at each commit, the default) or gxdJOURNAL_WRITE_BACK (at each checkpoint), gxdJOURNAL_SYNC_NORMAL (checkpoints also sync, the default) or gxdJOURNAL_SYNC_OFF, how often to sync, the journal size that triggers a checkpoint, and an exclusive lock for the writer.
int gxdJournal::InTransaction() const / unsigned long gxdJournal::Recovered() const / gxDatabaseError gxdJournal::GetJournalError() constWhether a transaction is open, the number of transactions Open() replayed, and the last journal error.
gxdJournalReadLock::Lock(const char *jname) / void gxdJournalReadLock::Unlock()Readers in other processes take this lock so a checkpoint does not change the files under them.
See the 6.3 release notes and examples_database/journal.