Blame docs/README

rpm-build ca8475
tdb - a trivial database system
rpm-build ca8475
tridge@linuxcare.com December 1999
rpm-build ca8475
==================================
rpm-build ca8475
rpm-build ca8475
This is a simple database API. It was inspired by the realisation that
rpm-build ca8475
in Samba we have several ad-hoc bits of code that essentially
rpm-build ca8475
implement small databases for sharing structures between parts of
rpm-build ca8475
Samba. As I was about to add another I realised that a generic
rpm-build ca8475
database module was called for to replace all the ad-hoc bits.
rpm-build ca8475
rpm-build ca8475
I based the interface on gdbm. I couldn't use gdbm as we need to be
rpm-build ca8475
able to have multiple writers to the databases at one time.
rpm-build ca8475
rpm-build ca8475
Compilation
rpm-build ca8475
-----------
rpm-build ca8475
rpm-build ca8475
add HAVE_MMAP=1 to use mmap instead of read/write
rpm-build ca8475
add NOLOCK=1 to disable locking code
rpm-build ca8475
rpm-build ca8475
Testing
rpm-build ca8475
-------
rpm-build ca8475
rpm-build ca8475
Compile tdbtest.c and link with gdbm for testing. tdbtest will perform
rpm-build ca8475
identical operations via tdb and gdbm then make sure the result is the
rpm-build ca8475
same
rpm-build ca8475
rpm-build ca8475
Also included is tdbtool, which allows simple database manipulation
rpm-build ca8475
on the commandline.
rpm-build ca8475
rpm-build ca8475
tdbtest and tdbtool are not built as part of Samba, but are included
rpm-build ca8475
for completeness.
rpm-build ca8475
rpm-build ca8475
Interface
rpm-build ca8475
---------
rpm-build ca8475
rpm-build ca8475
The interface is very similar to gdbm except for the following:
rpm-build ca8475
rpm-build ca8475
- different open interface. The tdb_open call is more similar to a
rpm-build ca8475
  traditional open()
rpm-build ca8475
- no tdbm_reorganise() function
rpm-build ca8475
- no tdbm_sync() function. No operations are cached in the library anyway
rpm-build ca8475
- added a tdb_traverse() function for traversing the whole database
rpm-build ca8475
- added transactions support
rpm-build ca8475
rpm-build ca8475
A general rule for using tdb is that the caller frees any returned
rpm-build ca8475
TDB_DATA structures. Just call free(p.dptr) to free a TDB_DATA
rpm-build ca8475
return value called p. This is the same as gdbm.
rpm-build ca8475
rpm-build ca8475
here is a full list of tdb functions with brief descriptions.
rpm-build ca8475
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
TDB_CONTEXT *tdb_open(char *name, int hash_size, int tdb_flags,
rpm-build ca8475
		      int open_flags, mode_t mode)
rpm-build ca8475
rpm-build ca8475
   open the database, creating it if necessary 
rpm-build ca8475
rpm-build ca8475
   The open_flags and mode are passed straight to the open call on the database
rpm-build ca8475
   file. A flags value of O_WRONLY is invalid
rpm-build ca8475
rpm-build ca8475
   The hash size is advisory, use zero for a default value. 
rpm-build ca8475
rpm-build ca8475
   return is NULL on error
rpm-build ca8475
rpm-build ca8475
   possible tdb_flags are:
rpm-build ca8475
    TDB_CLEAR_IF_FIRST - clear database if we are the only one with it open
rpm-build ca8475
    TDB_INTERNAL - don't use a file, instead store the data in
rpm-build ca8475
                   memory. The filename is ignored in this case.
rpm-build ca8475
    TDB_NOLOCK - don't do any locking
rpm-build ca8475
    TDB_NOMMAP - don't use mmap
rpm-build ca8475
    TDB_NOSYNC - don't synchronise transactions to disk
rpm-build ca8475
    TDB_SEQNUM - maintain a sequence number
rpm-build ca8475
    TDB_VOLATILE - activate the per-hashchain freelist, default 5
rpm-build ca8475
    TDB_ALLOW_NESTING - allow transactions to nest
rpm-build ca8475
    TDB_DISALLOW_NESTING - disallow transactions to nest
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
TDB_CONTEXT *tdb_open_ex(char *name, int hash_size, int tdb_flags,
rpm-build ca8475
		         int open_flags, mode_t mode,
rpm-build ca8475
			 const struct tdb_logging_context *log_ctx,
rpm-build ca8475
			 tdb_hash_func hash_fn)
rpm-build ca8475
rpm-build ca8475
This is like tdb_open(), but allows you to pass an initial logging and
rpm-build ca8475
hash function. Be careful when passing a hash function - all users of
rpm-build ca8475
the database must use the same hash function or you will get data
rpm-build ca8475
corruption.
rpm-build ca8475
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
char *tdb_error(TDB_CONTEXT *tdb);
rpm-build ca8475
rpm-build ca8475
     return a error string for the last tdb error
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_close(TDB_CONTEXT *tdb);
rpm-build ca8475
rpm-build ca8475
   close a database
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key);
rpm-build ca8475
rpm-build ca8475
   fetch an entry in the database given a key 
rpm-build ca8475
   if the return value has a null dptr then a error occurred
rpm-build ca8475
rpm-build ca8475
   caller must free the resulting data
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
rpm-build ca8475
		     int (*parser)(TDB_DATA key, TDB_DATA data,
rpm-build ca8475
				   void *private_data),
rpm-build ca8475
		     void *private_data);
rpm-build ca8475
rpm-build ca8475
   Hand a record to a parser function without allocating it.
rpm-build ca8475
rpm-build ca8475
   This function is meant as a fast tdb_fetch alternative for large records
rpm-build ca8475
   that are frequently read. The "key" and "data" arguments point directly
rpm-build ca8475
   into the tdb shared memory, they are not aligned at any boundary.
rpm-build ca8475
rpm-build ca8475
   WARNING: The parser is called while tdb holds a lock on the record. DO NOT
rpm-build ca8475
   call other tdb routines from within the parser. Also, for good performance
rpm-build ca8475
   you should make the parser fast to allow parallel operations.
rpm-build ca8475
rpm-build ca8475
   tdb_parse_record returns -1 if the record was not found.  If the record was
rpm-build ca8475
   found, the return value of "parser" is passed up to the caller.
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key);
rpm-build ca8475
rpm-build ca8475
   check if an entry in the database exists 
rpm-build ca8475
rpm-build ca8475
   note that 1 is returned if the key is found and 0 is returned if not found
rpm-build ca8475
   this doesn't match the conventions in the rest of this module, but is
rpm-build ca8475
   compatible with gdbm
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_traverse(TDB_CONTEXT *tdb, int (*fn)(TDB_CONTEXT *tdb,
rpm-build ca8475
                 TDB_DATA key, TDB_DATA dbuf, void *state), void *state);
rpm-build ca8475
rpm-build ca8475
   traverse the entire database - calling fn(tdb, key, data, state) on each 
rpm-build ca8475
   element.
rpm-build ca8475
rpm-build ca8475
   return -1 on error or the record count traversed
rpm-build ca8475
rpm-build ca8475
   if fn is NULL then it is not called
rpm-build ca8475
rpm-build ca8475
   a non-zero return value from fn() indicates that the traversal
rpm-build ca8475
   should stop. Traversal callbacks may not start transactions.
rpm-build ca8475
rpm-build ca8475
   WARNING: The data buffer given to the callback fn does NOT meet the
rpm-build ca8475
   alignment restrictions malloc gives you.
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_traverse_read(TDB_CONTEXT *tdb, int (*fn)(TDB_CONTEXT *tdb,
rpm-build ca8475
                     TDB_DATA key, TDB_DATA dbuf, void *state), void *state);
rpm-build ca8475
rpm-build ca8475
   traverse the entire database - calling fn(tdb, key, data, state) on
rpm-build ca8475
   each element, but marking the database read only during the
rpm-build ca8475
   traversal, so any write operations will fail. This allows tdb to
rpm-build ca8475
   use read locks, which increases the parallelism possible during the
rpm-build ca8475
   traversal.
rpm-build ca8475
rpm-build ca8475
   return -1 on error or the record count traversed
rpm-build ca8475
rpm-build ca8475
   if fn is NULL then it is not called
rpm-build ca8475
rpm-build ca8475
   a non-zero return value from fn() indicates that the traversal
rpm-build ca8475
   should stop. Traversal callbacks may not start transactions.
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb);
rpm-build ca8475
rpm-build ca8475
   find the first entry in the database and return its key
rpm-build ca8475
rpm-build ca8475
   the caller must free the returned data
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA key);
rpm-build ca8475
rpm-build ca8475
   find the next entry in the database, returning its key
rpm-build ca8475
rpm-build ca8475
   the caller must free the returned data
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key);
rpm-build ca8475
rpm-build ca8475
   delete an entry in the database given a key
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
rpm-build ca8475
rpm-build ca8475
   store an element in the database, replacing any existing element
rpm-build ca8475
   with the same key 
rpm-build ca8475
rpm-build ca8475
   If flag==TDB_INSERT then don't overwrite an existing entry
rpm-build ca8475
   If flag==TDB_MODIFY then don't create a new entry
rpm-build ca8475
rpm-build ca8475
   return 0 on success, -1 on failure
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_writelock(TDB_CONTEXT *tdb);
rpm-build ca8475
rpm-build ca8475
   lock the database. If we already have it locked then don't do anything
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_writeunlock(TDB_CONTEXT *tdb);
rpm-build ca8475
   unlock the database
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_chainlock(TDB_CONTEXT *tdb, TDB_DATA key);
rpm-build ca8475
rpm-build ca8475
   lock one hash chain. This is meant to be used to reduce locking
rpm-build ca8475
   contention - it cannot guarantee how many records will be locked
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_chainunlock(TDB_CONTEXT *tdb, TDB_DATA key);
rpm-build ca8475
rpm-build ca8475
   unlock one hash chain
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_transaction_start(TDB_CONTEXT *tdb)
rpm-build ca8475
rpm-build ca8475
   start a transaction. All operations after the transaction start can
rpm-build ca8475
   either be committed with tdb_transaction_commit() or cancelled with
rpm-build ca8475
   tdb_transaction_cancel(). 
rpm-build ca8475
rpm-build ca8475
   If you call tdb_transaction_start() again on the same tdb context
rpm-build ca8475
   while a transaction is in progress, then the same transaction
rpm-build ca8475
   buffer is re-used. The number of tdb_transaction_{commit,cancel}
rpm-build ca8475
   operations must match the number of successful
rpm-build ca8475
   tdb_transaction_start() calls.
rpm-build ca8475
rpm-build ca8475
   Note that transactions are by default disk synchronous, and use a
rpm-build ca8475
   recover area in the database to automatically recover the database
rpm-build ca8475
   on the next open if the system crashes during a transaction. You
rpm-build ca8475
   can disable the synchronous transaction recovery setup using the
rpm-build ca8475
   TDB_NOSYNC flag, which will greatly speed up operations at the risk
rpm-build ca8475
   of corrupting your database if the system crashes.
rpm-build ca8475
rpm-build ca8475
   Operations made within a transaction are not visible to other users
rpm-build ca8475
   of the database until a successful commit.
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_transaction_cancel(TDB_CONTEXT *tdb)
rpm-build ca8475
rpm-build ca8475
   cancel a current transaction, discarding all write and lock
rpm-build ca8475
   operations that have been made since the transaction started.
rpm-build ca8475
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_transaction_commit(TDB_CONTEXT *tdb)
rpm-build ca8475
rpm-build ca8475
   commit a current transaction, updating the database and releasing
rpm-build ca8475
   the transaction locks.
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_transaction_prepare_commit(TDB_CONTEXT *tdb)
rpm-build ca8475
rpm-build ca8475
   prepare to commit a current transaction, for two-phase commits.
rpm-build ca8475
   Once prepared for commit, the only allowed calls are
rpm-build ca8475
   tdb_transaction_commit() or tdb_transaction_cancel(). Preparing
rpm-build ca8475
   allocates disk space for the pending updates, so a subsequent
rpm-build ca8475
   commit should succeed (barring any hardware failures).
rpm-build ca8475
rpm-build ca8475
----------------------------------------------------------------------
rpm-build ca8475
int tdb_check(TDB_CONTEXT *tdb,
rpm-build ca8475
	      int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
rpm-build ca8475
	      void *private_data);)
rpm-build ca8475
rpm-build ca8475
   check the consistency of the database, calling back the check function
rpm-build ca8475
   (if non-NULL) with each record.  If some consistency check fails, or
rpm-build ca8475
   the supplied check function returns -1, tdb_check returns -1, otherwise
rpm-build ca8475
   0.  Note that logging function (if set) will be called with additional
rpm-build ca8475
   information on the corruption found.