tdb 1.2.9
Data Structures | Macros | Typedefs | Enumerations | Functions
The tdb API

tdb is a Trivial database. More...

Data Structures

struct  TDB_DATA
 The tdb data structure. More...
 

Macros

#define TDB_REPLACE   1 /** Unused */
 Flags to tdb_store()
 
#define TDB_DEFAULT   0 /** just a readability place holder */
 Flags for tdb_open()
 

Typedefs

typedef struct TDB_DATA TDB_DATA
 The tdb data structure.
 
typedef struct tdb_context TDB_CONTEXT
 This is the context structure that is returned from a db open.
 

Enumerations

enum  TDB_ERROR
 The tdb error codes.
 
enum  tdb_debug_level
 Debugging uses one of the following levels.
 

Functions

_PUBLIC_ struct tdb_context * tdb_open (const char *name, int hash_size, int tdb_flags, int open_flags, mode_t mode)
 Open the database and creating it if necessary. More...
 
_PUBLIC_ struct tdb_context * tdb_open_ex (const char *name, int hash_size, int tdb_flags, int open_flags, mode_t mode, const struct tdb_logging_context *log_ctx, tdb_hash_func hash_fn)
 Open the database and creating it if necessary. More...
 
_PUBLIC_ void tdb_set_max_dead (struct tdb_context *tdb, int max_dead)
 Set the maximum number of dead records per hash chain. More...
 
_PUBLIC_ int tdb_reopen (struct tdb_context *tdb)
 Reopen a tdb. More...
 
_PUBLIC_ int tdb_reopen_all (int parent_longlived)
 Reopen all tdb's. More...
 
_PUBLIC_ void tdb_set_logging_function (struct tdb_context *tdb, const struct tdb_logging_context *log_ctx)
 Set a different tdb logging function. More...
 
_PUBLIC_ enum TDB_ERROR tdb_error (struct tdb_context *tdb)
 Get the tdb last error code. More...
 
_PUBLIC_ const char * tdb_errorstr (struct tdb_context *tdb)
 Get a error string for the last tdb error. More...
 
_PUBLIC_ TDB_DATA tdb_fetch (struct tdb_context *tdb, TDB_DATA key)
 Fetch an entry in the database given a key. More...
 
_PUBLIC_ int tdb_parse_record (struct tdb_context *tdb, TDB_DATA key, int(*parser)(TDB_DATA key, TDB_DATA data, void *private_data), void *private_data)
 Hand a record to a parser function without allocating it. More...
 
_PUBLIC_ int tdb_delete (struct tdb_context *tdb, TDB_DATA key)
 Delete an entry in the database given a key. More...
 
_PUBLIC_ int tdb_store (struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
 Store an element in the database. More...
 
_PUBLIC_ int tdb_storev (struct tdb_context *tdb, TDB_DATA key, const TDB_DATA *dbufs, int num_dbufs, int flag)
 Store an element in the database. More...
 
_PUBLIC_ int tdb_append (struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
 Append data to an entry. More...
 
_PUBLIC_ int tdb_close (struct tdb_context *tdb)
 Close a database. More...
 
_PUBLIC_ TDB_DATA tdb_firstkey (struct tdb_context *tdb)
 Find the first entry in the database and return its key. More...
 
_PUBLIC_ TDB_DATA tdb_nextkey (struct tdb_context *tdb, TDB_DATA key)
 Find the next entry in the database, returning its key. More...
 
_PUBLIC_ int tdb_traverse (struct tdb_context *tdb, tdb_traverse_func fn, void *private_data)
 Traverse the entire database. More...
 
_PUBLIC_ int tdb_traverse_read (struct tdb_context *tdb, tdb_traverse_func fn, void *private_data)
 Traverse the entire database. More...
 
_PUBLIC_ int tdb_traverse_chain (struct tdb_context *tdb, unsigned chain, tdb_traverse_func fn, void *private_data)
 Traverse a single hash chain. More...
 
_PUBLIC_ int tdb_traverse_key_chain (struct tdb_context *tdb, TDB_DATA key, tdb_traverse_func fn, void *private_data)
 Traverse a single hash chain. More...
 
_PUBLIC_ int tdb_exists (struct tdb_context *tdb, TDB_DATA key)
 Check if an entry in the database exists. More...
 
_PUBLIC_ int tdb_lockall (struct tdb_context *tdb)
 Lock entire database with a write lock. More...
 
_PUBLIC_ int tdb_lockall_nonblock (struct tdb_context *tdb)
 Lock entire database with a write lock. More...
 
_PUBLIC_ int tdb_unlockall (struct tdb_context *tdb)
 Unlock entire database with write lock. More...
 
_PUBLIC_ int tdb_lockall_read (struct tdb_context *tdb)
 Lock entire database with a read lock. More...
 
_PUBLIC_ int tdb_lockall_read_nonblock (struct tdb_context *tdb)
 Lock entire database with a read lock. More...
 
_PUBLIC_ int tdb_unlockall_read (struct tdb_context *tdb)
 Unlock entire database with read lock. More...
 
_PUBLIC_ int tdb_lockall_mark (struct tdb_context *tdb)
 Lock entire database with write lock - mark only. More...
 
_PUBLIC_ int tdb_lockall_unmark (struct tdb_context *tdb)
 Lock entire database with write lock - unmark only. More...
 
_PUBLIC_ const char * tdb_name (struct tdb_context *tdb)
 Get the name of the current tdb file. More...
 
_PUBLIC_ int tdb_fd (struct tdb_context *tdb)
 Get the underlying file descriptor being used by tdb. More...
 
_PUBLIC_ tdb_log_func tdb_log_fn (struct tdb_context *tdb)
 Get the current logging function. More...
 
_PUBLIC_ void * tdb_get_logging_private (struct tdb_context *tdb)
 Get the private data of the logging function. More...
 
_PUBLIC_ bool tdb_transaction_active (struct tdb_context *tdb)
 Is a transaction active? More...
 
_PUBLIC_ int tdb_transaction_start (struct tdb_context *tdb)
 Start a transaction. More...
 
_PUBLIC_ int tdb_transaction_start_nonblock (struct tdb_context *tdb)
 Start a transaction, non-blocking. More...
 
_PUBLIC_ int tdb_transaction_prepare_commit (struct tdb_context *tdb)
 Prepare to commit a current transaction, for two-phase commits. More...
 
_PUBLIC_ int tdb_transaction_commit (struct tdb_context *tdb)
 Commit a current transaction. More...
 
_PUBLIC_ int tdb_transaction_cancel (struct tdb_context *tdb)
 Cancel a current transaction. More...
 
_PUBLIC_ int tdb_get_seqnum (struct tdb_context *tdb)
 Get the tdb sequence number. More...
 
_PUBLIC_ int tdb_hash_size (struct tdb_context *tdb)
 Get the hash size. More...
 
_PUBLIC_ size_t tdb_map_size (struct tdb_context *tdb)
 Get the map size. More...
 
_PUBLIC_ int tdb_get_flags (struct tdb_context *tdb)
 Get the tdb flags set during open. More...
 
_PUBLIC_ void tdb_add_flags (struct tdb_context *tdb, unsigned flag)
 Add flags to the database. More...
 
_PUBLIC_ void tdb_remove_flags (struct tdb_context *tdb, unsigned flag)
 Remove flags from the database. More...
 
_PUBLIC_ void tdb_enable_seqnum (struct tdb_context *tdb)
 Enable sequence number handling on an open tdb. More...
 
_PUBLIC_ void tdb_increment_seqnum_nonblock (struct tdb_context *tdb)
 Increment the tdb sequence number. More...
 
_PUBLIC_ unsigned int tdb_jenkins_hash (TDB_DATA *key)
 Create a hash of the key. More...
 
_PUBLIC_ int tdb_check (struct tdb_context *tdb, int(*check)(TDB_DATA key, TDB_DATA data, void *private_data), void *private_data)
 Check the consistency of the database. More...
 
_PUBLIC_ int tdb_rescue (struct tdb_context *tdb, void(*walk)(TDB_DATA key, TDB_DATA data, void *private_data), void *private_data)
 Dump all possible records in a corrupt database. More...
 
_PUBLIC_ bool tdb_runtime_check_for_robust_mutexes (void)
 Check if support for TDB_MUTEX_LOCKING is available at runtime. More...
 

Detailed Description

tdb is a Trivial database.

In concept, it is very much like GDBM, and BSD's DB except that it allows multiple simultaneous writers and uses locking internally to keep writers from trampling on each other. tdb is also extremely small.

Interface

The interface is very similar to gdbm except for the following:

A general rule for using tdb is that the caller frees any returned TDB_DATA structures. Just call free(p.dptr) to free a TDB_DATA return value called p. This is the same as gdbm.

Function Documentation

◆ tdb_add_flags()

_PUBLIC_ void tdb_add_flags ( struct tdb_context *  tdb,
unsigned  flag 
)

Add flags to the database.

Parameters
[in]tdbThe database to add the flags.
[in]flagThe tdb flags to add.

◆ tdb_append()

_PUBLIC_ int tdb_append ( struct tdb_context *  tdb,
TDB_DATA  key,
TDB_DATA  new_dbuf 
)

Append data to an entry.

If the entry doesn't exist, it will create a new one.

Parameters
[in]tdbThe database to use.
[in]keyThe key to append the data.
[in]new_dbufThe data to append to the key.
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_check()

_PUBLIC_ int tdb_check ( struct tdb_context *  tdb,
int(*)(TDB_DATA key, TDB_DATA data, void *private_data)  check,
void *  private_data 
)

Check the consistency of the database.

This check the consistency of the database calling back the check function (if non-NULL) on each record. If some consistency check fails, or the supplied check function returns -1, tdb_check returns -1, otherwise 0.

Note
The logging function (if set) will be called with additional information on the corruption found.
Parameters
[in]tdbThe database to check.
[in]checkThe check function to use.
[in]private_datathe private data to pass to the check function.
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_close()

_PUBLIC_ int tdb_close ( struct tdb_context *  tdb)

Close a database.

Parameters
[in]tdbThe database to close. The context will be free'd.
Returns
0 for success, -1 on error.
Note
Don't call tdb_error() after this function cause the tdb context will be freed on error.

◆ tdb_delete()

_PUBLIC_ int tdb_delete ( struct tdb_context *  tdb,
TDB_DATA  key 
)

Delete an entry in the database given a key.

Parameters
[in]tdbThe tdb to delete the key.
[in]keyThe key to delete.
Returns
0 on success, -1 if the key doesn't exist.

◆ tdb_enable_seqnum()

_PUBLIC_ void tdb_enable_seqnum ( struct tdb_context *  tdb)

Enable sequence number handling on an open tdb.

Parameters
[in]tdbThe database to enable sequence number handling.
See also
tdb_get_seqnum()

◆ tdb_error()

_PUBLIC_ enum TDB_ERROR tdb_error ( struct tdb_context *  tdb)

Get the tdb last error code.

Parameters
[in]tdbThe tdb to get the error code from.
Returns
A TDB_ERROR code.
See also
TDB_ERROR

◆ tdb_errorstr()

_PUBLIC_ const char * tdb_errorstr ( struct tdb_context *  tdb)

Get a error string for the last tdb error.

Parameters
[in]tdbThe tdb to get the error code from.
Returns
An error string.

◆ tdb_exists()

_PUBLIC_ int tdb_exists ( struct tdb_context *  tdb,
TDB_DATA  key 
)

Check if an entry in the database exists.

Note
1 is returned if the key is found and 0 is returned if not found this doesn't match the conventions in the rest of this module, but is compatible with gdbm.
Parameters
[in]tdbThe database to check if the entry exists.
[in]keyThe key to check if the entry exists.
Returns
1 if the key is found, 0 if not.

◆ tdb_fd()

_PUBLIC_ int tdb_fd ( struct tdb_context *  tdb)

Get the underlying file descriptor being used by tdb.

This is useful for external routines that want to check the device/inode of the fd.

Parameters
[in]tdbThe database to get the fd from.
Returns
The file descriptor or -1.

◆ tdb_fetch()

_PUBLIC_ TDB_DATA tdb_fetch ( struct tdb_context *  tdb,
TDB_DATA  key 
)

Fetch an entry in the database given a key.

The caller must free the resulting data.

Parameters
[in]tdbThe tdb to fetch the key.
[in]keyThe key to fetch.
Returns
The key entry found in the database, NULL on error with TDB_ERROR set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_firstkey()

_PUBLIC_ TDB_DATA tdb_firstkey ( struct tdb_context *  tdb)

Find the first entry in the database and return its key.

The caller must free the returned data.

Parameters
[in]tdbThe database to use.
Returns
The first entry of the database, an empty TDB_DATA entry if the database is empty.

◆ tdb_get_flags()

_PUBLIC_ int tdb_get_flags ( struct tdb_context *  tdb)

Get the tdb flags set during open.

Parameters
[in]tdbThe database to get the flags form.
Returns
The flags set to on the database.

◆ tdb_get_logging_private()

_PUBLIC_ void * tdb_get_logging_private ( struct tdb_context *  tdb)

Get the private data of the logging function.

Parameters
[in]tdbThe database to get the data from.
Returns
The private data pointer of the logging function.
See also
tdb_log_fn()

◆ tdb_get_seqnum()

_PUBLIC_ int tdb_get_seqnum ( struct tdb_context *  tdb)

Get the tdb sequence number.

Only makes sense if the writers opened with TDB_SEQNUM set. Note that this sequence number will wrap quite quickly, so it should only be used for a 'has something changed' test, not for code that relies on the count of the number of changes made. If you want a counter then use a tdb record.

The aim of this sequence number is to allow for a very lightweight test of a possible tdb change.

Parameters
[in]tdbThe database to get the sequence number from.
Returns
The sequence number or 0.
See also
tdb_open()
tdb_enable_seqnum()

◆ tdb_hash_size()

_PUBLIC_ int tdb_hash_size ( struct tdb_context *  tdb)

Get the hash size.

Parameters
[in]tdbThe database to get the hash size from.
Returns
The hash size.

◆ tdb_increment_seqnum_nonblock()

_PUBLIC_ void tdb_increment_seqnum_nonblock ( struct tdb_context *  tdb)

Increment the tdb sequence number.

This only works if the tdb has been opened using the TDB_SEQNUM flag or enabled using tdb_enable_seqnum().

Parameters
[in]tdbThe database to increment the sequence number.
See also
tdb_enable_seqnum()
tdb_get_seqnum()

◆ tdb_jenkins_hash()

_PUBLIC_ unsigned int tdb_jenkins_hash ( TDB_DATA key)

Create a hash of the key.

Parameters
[in]keyThe key to hash
Returns
The hash.

◆ tdb_lockall()

_PUBLIC_ int tdb_lockall ( struct tdb_context *  tdb)

Lock entire database with a write lock.

Parameters
[in]tdbThe database to lock.
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_lockall_mark()

_PUBLIC_ int tdb_lockall_mark ( struct tdb_context *  tdb)

Lock entire database with write lock - mark only.

Todo:
Add more details.
Parameters
[in]tdbThe database to mark.
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_lockall_nonblock()

_PUBLIC_ int tdb_lockall_nonblock ( struct tdb_context *  tdb)

Lock entire database with a write lock.

This is the non-blocking call.

Parameters
[in]tdbThe database to lock.
Returns
0 on success, -1 on error with error code set.
See also
tdb_lockall()
tdb_error()
tdb_errorstr()

◆ tdb_lockall_read()

_PUBLIC_ int tdb_lockall_read ( struct tdb_context *  tdb)

Lock entire database with a read lock.

Parameters
[in]tdbThe database to lock.
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_lockall_read_nonblock()

_PUBLIC_ int tdb_lockall_read_nonblock ( struct tdb_context *  tdb)

Lock entire database with a read lock.

This is the non-blocking call.

Parameters
[in]tdbThe database to lock.
Returns
0 on success, -1 on error with error code set.
See also
tdb_lockall_read()
tdb_error()
tdb_errorstr()

◆ tdb_lockall_unmark()

_PUBLIC_ int tdb_lockall_unmark ( struct tdb_context *  tdb)

Lock entire database with write lock - unmark only.

Todo:
Add more details.
Parameters
[in]tdbThe database to mark.
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_log_fn()

_PUBLIC_ tdb_log_func tdb_log_fn ( struct tdb_context *  tdb)

Get the current logging function.

This is useful for external tdb routines that wish to log tdb errors.

Parameters
[in]tdbThe database to get the logging function from.
Returns
The logging function of the database.
See also
tdb_get_logging_private()

◆ tdb_map_size()

_PUBLIC_ size_t tdb_map_size ( struct tdb_context *  tdb)

Get the map size.

Parameters
[in]tdbThe database to get the map size from.
Returns
The map size.

◆ tdb_name()

_PUBLIC_ const char * tdb_name ( struct tdb_context *  tdb)

Get the name of the current tdb file.

This is useful for external logging functions.

Parameters
[in]tdbThe database to get the name from.
Returns
The name of the database.

◆ tdb_nextkey()

_PUBLIC_ TDB_DATA tdb_nextkey ( struct tdb_context *  tdb,
TDB_DATA  key 
)

Find the next entry in the database, returning its key.

The caller must free the returned data.

Parameters
[in]tdbThe database to use.
[in]keyThe key from which you want the next key.
Returns
The next entry of the current key, an empty TDB_DATA entry if there is no entry.

◆ tdb_open()

_PUBLIC_ struct tdb_context * tdb_open ( const char *  name,
int  hash_size,
int  tdb_flags,
int  open_flags,
mode_t  mode 
)

Open the database and creating it if necessary.

Parameters
[in]nameThe name of the db to open.
[in]hash_sizeThe hash size is advisory, use zero for a default value.
[in]tdb_flagsThe flags to use to open the db:

TDB_CLEAR_IF_FIRST - Clear database if we are the only one with it open
TDB_INTERNAL - Don't use a file, instead store the data in memory. The filename is ignored in this case.
TDB_NOLOCK - Don't do any locking
TDB_NOMMAP - Don't use mmap
TDB_NOSYNC - Don't synchronise transactions to disk
TDB_SEQNUM - Maintain a sequence number
TDB_VOLATILE - activate the per-hashchain freelist, default 5.
TDB_ALLOW_NESTING - Allow transactions to nest.
TDB_DISALLOW_NESTING - Disallow transactions to nest.
TDB_INCOMPATIBLE_HASH - Better hashing: can't be opened by tdb < 1.2.6.
TDB_MUTEX_LOCKING - Optimized locking using robust mutexes if supported, can't be opened by tdb < 1.3.0. Only valid in combination with TDB_CLEAR_IF_FIRST after checking tdb_runtime_check_for_robust_mutexes()
[in]open_flagsFlags for the open(2) function.
[in]modeThe mode for the open(2) function.
Returns
A tdb context structure, NULL on error.

◆ tdb_open_ex()

_PUBLIC_ struct tdb_context * tdb_open_ex ( const char *  name,
int  hash_size,
int  tdb_flags,
int  open_flags,
mode_t  mode,
const struct tdb_logging_context *  log_ctx,
tdb_hash_func  hash_fn 
)

Open the database and creating it if necessary.

This is like tdb_open(), but allows you to pass an initial logging and hash function. Be careful when passing a hash function - all users of the database must use the same hash function or you will get data corruption.

Parameters
[in]nameThe name of the db to open.
[in]hash_sizeThe hash size is advisory, use zero for a default value.
[in]tdb_flagsThe flags to use to open the db:

TDB_CLEAR_IF_FIRST - Clear database if we are the only one with it open
TDB_INTERNAL - Don't use a file, instead store the data in memory. The filename is ignored in this case.
TDB_NOLOCK - Don't do any locking
TDB_NOMMAP - Don't use mmap
TDB_NOSYNC - Don't synchronise transactions to disk
TDB_SEQNUM - Maintain a sequence number
TDB_VOLATILE - activate the per-hashchain freelist, default 5.
TDB_ALLOW_NESTING - Allow transactions to nest.
TDB_DISALLOW_NESTING - Disallow transactions to nest.
TDB_INCOMPATIBLE_HASH - Better hashing: can't be opened by tdb < 1.2.6.
TDB_MUTEX_LOCKING - Optimized locking using robust mutexes if supported, can't be opened by tdb < 1.3.0. Only valid in combination with TDB_CLEAR_IF_FIRST after checking tdb_runtime_check_for_robust_mutexes()
[in]open_flagsFlags for the open(2) function.
[in]modeThe mode for the open(2) function.
[in]log_ctxThe logging function to use.
[in]hash_fnThe hash function you want to use.
Returns
A tdb context structure, NULL on error.
See also
tdb_open()

◆ tdb_parse_record()

_PUBLIC_ int tdb_parse_record ( struct tdb_context *  tdb,
TDB_DATA  key,
int(*)(TDB_DATA key, TDB_DATA data, void *private_data)  parser,
void *  private_data 
)

Hand a record to a parser function without allocating it.

This function is meant as a fast tdb_fetch alternative for large records that are frequently read. The "key" and "data" arguments point directly into the tdb shared memory, they are not aligned at any boundary.

Warning
The parser is called while tdb holds a lock on the record. DO NOT call other tdb routines from within the parser. Also, for good performance you should make the parser fast to allow parallel operations.
Parameters
[in]tdbThe tdb to parse the record.
[in]keyThe key to parse.
[in]parserThe parser to use to parse the data.
[in]private_dataA private data pointer which is passed to the parser function.
Returns
-1 if the record was not found. If the record was found, the return value of "parser" is passed up to the caller.

◆ tdb_remove_flags()

_PUBLIC_ void tdb_remove_flags ( struct tdb_context *  tdb,
unsigned  flag 
)

Remove flags from the database.

Parameters
[in]tdbThe database to remove the flags.
[in]flagThe tdb flags to remove.

◆ tdb_reopen()

_PUBLIC_ int tdb_reopen ( struct tdb_context *  tdb)

Reopen a tdb.

This can be used after a fork to ensure that we have an independent seek pointer from our parent and to re-establish locks.

Parameters
[in]tdbThe database to reopen. It will be free'd on error!
Returns
0 on success, -1 on error.
Note
Don't call tdb_error() after this function cause the tdb context will be freed on error.

◆ tdb_reopen_all()

_PUBLIC_ int tdb_reopen_all ( int  parent_longlived)

Reopen all tdb's.

If the parent is longlived (ie. a parent daemon architecture), we know it will keep it's active lock on a tdb opened with CLEAR_IF_FIRST. Thus for child processes we don't have to add an active lock. This is essential to improve performance on systems that keep POSIX locks as a non-scalable data structure in the kernel.

Parameters
[in]parent_longlivedWhether the parent is longlived or not.
Returns
0 on success, -1 on error.

◆ tdb_rescue()

_PUBLIC_ int tdb_rescue ( struct tdb_context *  tdb,
void(*)(TDB_DATA key, TDB_DATA data, void *private_data)  walk,
void *  private_data 
)

Dump all possible records in a corrupt database.

This is the only way to get data out of a database where tdb_check() fails. It will call walk() with anything which looks like a database record; this may well include invalid, incomplete or duplicate records.

Parameters
[in]tdbThe database to check.
[in]walkThe walk function to use.
[in]private_datathe private data to pass to the walk function.
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_runtime_check_for_robust_mutexes()

_PUBLIC_ bool tdb_runtime_check_for_robust_mutexes ( void  )

Check if support for TDB_MUTEX_LOCKING is available at runtime.

On some systems the API for pthread_mutexattr_setrobust() is not available. On other systems there are some bugs in the interaction between glibc and the linux kernel.

This function provides a runtime check if robust mutexes are really available.

This needs to be called and return true before TDB_MUTEX_LOCKING can be used at runtime.

Note
This calls fork(), but the SIGCHILD handling should be transparent.
Returns
true if supported, false otherwise.
See also
TDB_MUTEX_LOCKING

◆ tdb_set_logging_function()

_PUBLIC_ void tdb_set_logging_function ( struct tdb_context *  tdb,
const struct tdb_logging_context *  log_ctx 
)

Set a different tdb logging function.

Parameters
[in]tdbThe tdb to set the logging function.
[in]log_ctxThe logging function to set.

◆ tdb_set_max_dead()

_PUBLIC_ void tdb_set_max_dead ( struct tdb_context *  tdb,
int  max_dead 
)

Set the maximum number of dead records per hash chain.

Parameters
[in]tdbThe database handle to set the maximum.
[in]max_deadThe maximum number of dead records per hash chain.

◆ tdb_store()

_PUBLIC_ int tdb_store ( struct tdb_context *  tdb,
TDB_DATA  key,
TDB_DATA  dbuf,
int  flag 
)

Store an element in the database.

This replaces any existing element with the same key.

Parameters
[in]tdbThe tdb to store the entry.
[in]keyThe key to use to store the entry.
[in]dbufThe data to store under the key.
[in]flagThe flags to store the key:

TDB_INSERT: Don't overwrite an existing entry.
TDB_MODIFY: Don't create a new entry
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_storev()

_PUBLIC_ int tdb_storev ( struct tdb_context *  tdb,
TDB_DATA  key,
const TDB_DATA dbufs,
int  num_dbufs,
int  flag 
)

Store an element in the database.

This replaces any existing element with the same key.

Parameters
[in]tdbThe tdb to store the entry.
[in]keyThe key to use to store the entry.
[in]dbufsA vector of memory chunks to write
[in]num_dbufsLength of the dbufs vector
[in]flagThe flags to store the key:

TDB_INSERT: Don't overwrite an existing entry.
TDB_MODIFY: Don't create a new entry
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_transaction_active()

_PUBLIC_ bool tdb_transaction_active ( struct tdb_context *  tdb)

Is a transaction active?

It is helpful for the application to know if a transaction is active, rather than needing to maintain an application-level reference count.

Parameters
[in]tdbThe database to start the transaction.
Returns
true if there is a transaction active, false otherwise
See also
tdb_transaction_start()
tdb_transaction_prepare_commit()
tdb_transaction_commit()
tdb_transaction_cancel()

◆ tdb_transaction_cancel()

_PUBLIC_ int tdb_transaction_cancel ( struct tdb_context *  tdb)

Cancel a current transaction.

This discards all write and lock operations that have been made since the transaction started.

Parameters
[in]tdbThe tdb to cancel the transaction on.
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_transaction_commit()

_PUBLIC_ int tdb_transaction_commit ( struct tdb_context *  tdb)

Commit a current transaction.

This updates the database and releases the current transaction locks.

Parameters
[in]tdbThe database to commit the transaction.
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_transaction_prepare_commit()

_PUBLIC_ int tdb_transaction_prepare_commit ( struct tdb_context *  tdb)

Prepare to commit a current transaction, for two-phase commits.

Once prepared for commit, the only allowed calls are tdb_transaction_commit() or tdb_transaction_cancel(). Preparing allocates disk space for the pending updates, so a subsequent commit should succeed (barring any hardware failures).

Parameters
[in]tdbThe database to prepare the commit.
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_transaction_start()

_PUBLIC_ int tdb_transaction_start ( struct tdb_context *  tdb)

Start a transaction.

All operations after the transaction start can either be committed with tdb_transaction_commit() or cancelled with tdb_transaction_cancel().

If (the default) TDB_ALLOW_NESTING was specified or TDB_DISALLOW_NESTING was not specified as a flag via tdb_open() or tdb_open_ex(), you call tdb_transaction_start() again on the same tdb context while a transaction is in progress, then the same transaction buffer is re-used. The number of tdb_transaction_{commit,cancel} operations must match the number of successful tdb_transaction_start() calls.

Note that transactions are by default disk synchronous, and use a recover area in the database to automatically recover the database on the next open if the system crashes during a transaction. You can disable the synchronous transaction recovery setup using the TDB_NOSYNC flag, which will greatly speed up operations at the risk of corrupting your database if the system crashes.

Operations made within a transaction are not visible to other users of the database until a successful commit.

Parameters
[in]tdbThe database to start the transaction.
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()

◆ tdb_transaction_start_nonblock()

_PUBLIC_ int tdb_transaction_start_nonblock ( struct tdb_context *  tdb)

Start a transaction, non-blocking.

Parameters
[in]tdbThe database to start the transaction.
Returns
0 on success, -1 on error with error code set.
See also
tdb_error()
tdb_errorstr()
tdb_transaction_start()

◆ tdb_traverse()

_PUBLIC_ int tdb_traverse ( struct tdb_context *  tdb,
tdb_traverse_func  fn,
void *  private_data 
)

Traverse the entire database.

While traversing the function fn(tdb, key, data, state) is called on each element. If fn is NULL then it is not called. A non-zero return value from fn() indicates that the traversal should stop. Traversal callbacks may not start transactions.

Warning
The data buffer given to the callback fn does NOT meet the alignment restrictions malloc gives you.
Parameters
[in]tdbThe database to traverse.
[in]fnThe function to call on each entry.
[in]private_dataThe private data which should be passed to the traversing function.
Returns
The record count traversed, -1 on error.

◆ tdb_traverse_chain()

_PUBLIC_ int tdb_traverse_chain ( struct tdb_context *  tdb,
unsigned  chain,
tdb_traverse_func  fn,
void *  private_data 
)

Traverse a single hash chain.

Traverse a single hash chain under a single lock operation. No database modification is possible in the callback.

This exists for background cleanup of databases. In normal operations, traversing a complete database can be much too expensive. Databases can have many chains, which will all have to be looked at before tdb_traverse finishes. Also tdb_traverse does a lot of fcntl activity to protect against concurrent record deletes.

With this you can walk a fraction of the whole tdb, collect the entries you want to prune, leave the traverse, and then modify or delete the records in a subsequent step.

To walk the entire database, call this function tdb_hash_size() times, with 0<=chain<tdb_hash_size(tdb).

Parameters
[in]tdbThe database to traverse.
[in]chainThe hash chain number to traverse.
[in]fnThe function to call on each entry.
[in]private_dataThe private data which should be passed to the traversing function.
Returns
The record count traversed, -1 on error.

◆ tdb_traverse_key_chain()

_PUBLIC_ int tdb_traverse_key_chain ( struct tdb_context *  tdb,
TDB_DATA  key,
tdb_traverse_func  fn,
void *  private_data 
)

Traverse a single hash chain.

This is like tdb_traverse_chain(), but for all records that are in the same chain as the record corresponding to the key parameter.

Use it for ongoing database maintenance under a lock. Whenever you are about to modify a database, you know which record you are going to write to. For that tdb_store(), an exclusive chainlock is taken behind the scenes. To utilize this exclusive lock for incremental database cleanup as well, tdb_chainlock() the key you are about to modify, then tdb_traverse_key_chain() to do the incremental maintenance, modify your record and tdb_chainunlock() the key again.

Parameters
[in]tdbThe database to traverse.
[in]keyThe record key to walk the chain for.
[in]fnThe function to call on each entry.
[in]private_dataThe private data which should be passed to the traversing function.
Returns
The record count traversed, -1 on error.

◆ tdb_traverse_read()

_PUBLIC_ int tdb_traverse_read ( struct tdb_context *  tdb,
tdb_traverse_func  fn,
void *  private_data 
)

Traverse the entire database.

While traversing the database the function fn(tdb, key, data, state) is called on each element, but marking the database read only during the traversal, so any write operations will fail. This allows tdb to use read locks, which increases the parallelism possible during the traversal.

Parameters
[in]tdbThe database to traverse.
[in]fnThe function to call on each entry.
[in]private_dataThe private data which should be passed to the traversing function.
Returns
The record count traversed, -1 on error.

◆ tdb_unlockall()

_PUBLIC_ int tdb_unlockall ( struct tdb_context *  tdb)

Unlock entire database with write lock.

Parameters
[in]tdbThe database to unlock.
Returns
0 on success, -1 on error with error code set.
See also
tdb_lockall()
tdb_error()
tdb_errorstr()

◆ tdb_unlockall_read()

_PUBLIC_ int tdb_unlockall_read ( struct tdb_context *  tdb)

Unlock entire database with read lock.

Parameters
[in]tdbThe database to unlock.
Returns
0 on success, -1 on error with error code set.
See also
tdb_lockall_read()
tdb_error()
tdb_errorstr()