Next: , Previous: , Up: Crash Tolerance   [Contents][Index]


17.2 Enabling crash tolerance

Open a GNU dbm database with gdbm_open. Whenever possible, use the extended GDBM format (see Numsync). Generally speaking, this means using the GDBM_NUMSYNC flag when creating the database. Unless you know what you are doing, do not specify the GDBM_SYNC flag when opening the database. The reason is that you want your application to explicitly control when gdbm_sync is called; you don’t want an implicit sync on every database operation (see Sync).

Request crash tolerance by invoking the following interface:

int gdbm_failure_atomic (GDBM_FILE dbf, const char *even,
                         const char *odd);

The even and odd arguments are the pathnames of two files that will be created and filled with snapshots of the database file. These two files must not exist when gdbm_failure_atomic is called and must reside on the same reflink-capable filesystem as the database file.

After you call gdbm_failure_atomic, every call to gdbm_sync will make an efficient reflink snapshot of the database file in either the even or the odd snapshot file; consecutive gdbm_sync calls alternate between the two, hence the names. The permission bits and mtime timestamps on the snapshot files determine which one contains the state of the database file corresponding to the most recent successful gdbm_sync. See Crash recovery, for discussion of crash recovery.