tdb 1.2.9
|
tdb is a Trivial database. More...
Data Structures | |
struct | TDB_DATA |
The tdb data structure. More... | |
Defines | |
#define | TDB_REPLACE 1 |
Flags to tdb_store() | |
#define | TDB_DEFAULT 0 |
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 | |
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. | |
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. | |
void | tdb_set_max_dead (struct tdb_context *tdb, int max_dead) |
Set the maximum number of dead records per hash chain. | |
int | tdb_reopen (struct tdb_context *tdb) |
Reopen a tdb. | |
int | tdb_reopen_all (int parent_longlived) |
Reopen all tdb's. | |
void | tdb_set_logging_function (struct tdb_context *tdb, const struct tdb_logging_context *log_ctx) |
Set a different tdb logging function. | |
enum TDB_ERROR | tdb_error (struct tdb_context *tdb) |
Get the tdb last error code. | |
const char * | tdb_errorstr (struct tdb_context *tdb) |
Get a error string for the last tdb error. | |
TDB_DATA | tdb_fetch (struct tdb_context *tdb, TDB_DATA key) |
Fetch an entry in the database given a key. | |
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. | |
int | tdb_delete (struct tdb_context *tdb, TDB_DATA key) |
Delete an entry in the database given a key. | |
int | tdb_store (struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag) |
Store an element in the database. | |
int | tdb_append (struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf) |
Append data to an entry. | |
int | tdb_close (struct tdb_context *tdb) |
Close a database. | |
TDB_DATA | tdb_firstkey (struct tdb_context *tdb) |
Find the first entry in the database and return its key. | |
TDB_DATA | tdb_nextkey (struct tdb_context *tdb, TDB_DATA key) |
Find the next entry in the database, returning its key. | |
int | tdb_traverse (struct tdb_context *tdb, tdb_traverse_func fn, void *private_data) |
Traverse the entire database. | |
int | tdb_traverse_read (struct tdb_context *tdb, tdb_traverse_func fn, void *private_data) |
Traverse the entire database. | |
int | tdb_exists (struct tdb_context *tdb, TDB_DATA key) |
Check if an entry in the database exists. | |
int | tdb_lockall (struct tdb_context *tdb) |
Lock entire database with a write lock. | |
int | tdb_lockall_nonblock (struct tdb_context *tdb) |
Lock entire database with a write lock. | |
int | tdb_unlockall (struct tdb_context *tdb) |
Unlock entire database with write lock. | |
int | tdb_lockall_read (struct tdb_context *tdb) |
Lock entire database with a read lock. | |
int | tdb_lockall_read_nonblock (struct tdb_context *tdb) |
Lock entire database with a read lock. | |
int | tdb_unlockall_read (struct tdb_context *tdb) |
Unlock entire database with read lock. | |
int | tdb_lockall_mark (struct tdb_context *tdb) |
Lock entire database with write lock - mark only. | |
int | tdb_lockall_unmark (struct tdb_context *tdb) |
Lock entire database with write lock - unmark only. | |
const char * | tdb_name (struct tdb_context *tdb) |
Get the name of the current tdb file. | |
int | tdb_fd (struct tdb_context *tdb) |
Get the underlying file descriptor being used by tdb. | |
tdb_log_func | tdb_log_fn (struct tdb_context *tdb) |
Get the current logging function. | |
void * | tdb_get_logging_private (struct tdb_context *tdb) |
Get the private data of the logging function. | |
int | tdb_transaction_start (struct tdb_context *tdb) |
Start a transaction. | |
int | tdb_transaction_start_nonblock (struct tdb_context *tdb) |
Start a transaction, non-blocking. | |
int | tdb_transaction_prepare_commit (struct tdb_context *tdb) |
Prepare to commit a current transaction, for two-phase commits. | |
int | tdb_transaction_commit (struct tdb_context *tdb) |
Commit a current transaction. | |
int | tdb_transaction_cancel (struct tdb_context *tdb) |
Cancel a current transaction. | |
int | tdb_get_seqnum (struct tdb_context *tdb) |
Get the tdb sequence number. | |
int | tdb_hash_size (struct tdb_context *tdb) |
Get the hash size. | |
size_t | tdb_map_size (struct tdb_context *tdb) |
Get the map size. | |
int | tdb_get_flags (struct tdb_context *tdb) |
Get the tdb flags set during open. | |
void | tdb_add_flags (struct tdb_context *tdb, unsigned flag) |
Add flags to the database. | |
void | tdb_remove_flags (struct tdb_context *tdb, unsigned flag) |
Remove flags from the database. | |
void | tdb_enable_seqnum (struct tdb_context *tdb) |
Enable sequence number handling on an open tdb. | |
void | tdb_increment_seqnum_nonblock (struct tdb_context *tdb) |
Increment the tdb sequence number. | |
unsigned int | tdb_jenkins_hash (TDB_DATA *key) |
Create a hash of the key. | |
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. |
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.
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.
typedef struct tdb_context TDB_CONTEXT |
This is the context structure that is returned from a db open.
void tdb_add_flags | ( | struct tdb_context * | tdb, |
unsigned | flag | ||
) |
Add flags to the database.
[in] | tdb | The database to add the flags. |
[in] | flag | The tdb flags to add. |
Append data to an entry.
If the entry doesn't exist, it will create a new one.
[in] | tdb | The database to use. |
[in] | key | The key to append the data. |
[in] | new_dbuf | The data to append to the key. |
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.
[in] | tdb | The database to check. |
[in] | check | The check function to use. |
[in] | private_data | the private data to pass to the check function. |
int tdb_close | ( | struct tdb_context * | tdb | ) |
Close a database.
[in] | tdb | The database to close. |
int tdb_delete | ( | struct tdb_context * | tdb, |
TDB_DATA | key | ||
) |
Delete an entry in the database given a key.
[in] | tdb | The tdb to delete the key. |
[in] | key | The key to delete. |
void tdb_enable_seqnum | ( | struct tdb_context * | tdb | ) |
Enable sequence number handling on an open tdb.
[in] | tdb | The database to enable sequence number handling. |
enum TDB_ERROR tdb_error | ( | struct tdb_context * | tdb | ) |
Get the tdb last error code.
[in] | tdb | The tdb to get the error code from. |
const char* tdb_errorstr | ( | struct tdb_context * | tdb | ) |
Get a error string for the last tdb error.
[in] | tdb | The tdb to get the error code from. |
int tdb_exists | ( | struct tdb_context * | tdb, |
TDB_DATA | key | ||
) |
Check if an entry in the database exists.
[in] | tdb | The database to check if the entry exists. |
[in] | key | The key to check if the entry exists. |
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.
[in] | tdb | The database to get the fd from. |
Fetch an entry in the database given a key.
The caller must free the resulting data.
[in] | tdb | The tdb to fetch the key. |
[in] | key | The key to fetch. |
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.
[in] | tdb | The database to use. |
int tdb_get_flags | ( | struct tdb_context * | tdb | ) |
Get the tdb flags set during open.
[in] | tdb | The database to get the flags form. |
void* tdb_get_logging_private | ( | struct tdb_context * | tdb | ) |
Get the private data of the logging function.
[in] | tdb | The database to get the data from. |
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.
[in] | tdb | The database to get the sequence number from. |
int tdb_hash_size | ( | struct tdb_context * | tdb | ) |
Get the hash size.
[in] | tdb | The database to get the hash size from. |
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 useing tdb_enable_seqnum().
[in] | tdb | The database to increment the sequence number. |
unsigned int tdb_jenkins_hash | ( | TDB_DATA * | key | ) |
Create a hash of the key.
[in] | key | The key to hash |
int tdb_lockall | ( | struct tdb_context * | tdb | ) |
Lock entire database with a write lock.
[in] | tdb | The database to lock. |
int tdb_lockall_mark | ( | struct tdb_context * | tdb | ) |
Lock entire database with write lock - mark only.
[in] | tdb | The database to mark. |
int tdb_lockall_nonblock | ( | struct tdb_context * | tdb | ) |
Lock entire database with a write lock.
This is the non-blocking call.
[in] | tdb | The database to lock. |
int tdb_lockall_read | ( | struct tdb_context * | tdb | ) |
Lock entire database with a read lock.
[in] | tdb | The database to lock. |
int tdb_lockall_read_nonblock | ( | struct tdb_context * | tdb | ) |
Lock entire database with a read lock.
This is the non-blocking call.
[in] | tdb | The database to lock. |
int tdb_lockall_unmark | ( | struct tdb_context * | tdb | ) |
Lock entire database with write lock - unmark only.
[in] | tdb | The database to mark. |
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.
[in] | tdb | The database to get the logging function from. |
size_t tdb_map_size | ( | struct tdb_context * | tdb | ) |
Get the map size.
[in] | tdb | The database to get the map size from. |
const char* tdb_name | ( | struct tdb_context * | tdb | ) |
Get the name of the current tdb file.
This is useful for external logging functions.
[in] | tdb | The database to get the name from. |
Find the next entry in the database, returning its key.
The caller must free the returned data.
[in] | tdb | The database to use. |
[in] | key | The key from which you want the next key. |
struct tdb_context* tdb_open | ( | const char * | name, |
int | hash_size, | ||
int | tdb_flags, | ||
int | open_flags, | ||
mode_t | mode | ||
) | [read] |
Open the database and creating it if necessary.
[in] | name | The name of the db to open. |
[in] | hash_size | The hash size is advisory, use zero for a default value. |
[in] | tdb_flags | The 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, instaed 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. |
[in] | open_flags | Flags for the open(2) function. |
[in] | mode | The mode for the open(2) function. |
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 | ||
) | [read] |
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.
[in] | name | The name of the db to open. |
[in] | hash_size | The hash size is advisory, use zero for a default value. |
[in] | tdb_flags | The 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, instaed 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. |
[in] | open_flags | Flags for the open(2) function. |
[in] | mode | The mode for the open(2) function. |
[in] | log_ctx | The logging function to use. |
[in] | hash_fn | The hash function you want to use. |
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.
[in] | tdb | The tdb to parse the record. |
[in] | key | The key to parse. |
[in] | parser | The parser to use to parse the data. |
[in] | private_data | A private data pointer which is passed to the parser function. |
void tdb_remove_flags | ( | struct tdb_context * | tdb, |
unsigned | flag | ||
) |
Remove flags from the database.
[in] | tdb | The database to remove the flags. |
[in] | flag | The tdb flags to remove. |
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.
[in] | tdb | The database to reopen. |
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.
[in] | parent_longlived | Wether the parent is longlived or not. |
void tdb_set_logging_function | ( | struct tdb_context * | tdb, |
const struct tdb_logging_context * | log_ctx | ||
) |
Set a different tdb logging function.
[in] | tdb | The tdb to set the logging function. |
[in] | log_ctx | The logging function to set. |
void tdb_set_max_dead | ( | struct tdb_context * | tdb, |
int | max_dead | ||
) |
Set the maximum number of dead records per hash chain.
[in] | tdb | The database handle to set the maximum. |
[in] | max_dead | The maximum number of dead records per hash chain. |
Store an element in the database.
This replaces any existing element with the same key.
[in] | tdb | The tdb to store the entry. |
[in] | key | The key to use to store the entry. |
[in] | dbuf | The data to store under the key. |
[in] | flag | The flags to store the key: TDB_INSERT: Don't overwrite an existing entry. TDB_MODIFY: Don't create a new entry |
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.
[in] | tdb | The tdb to cancel the transaction on. |
int tdb_transaction_commit | ( | struct tdb_context * | tdb | ) |
Commit a current transaction.
This updates the database and releases the current transaction locks.
[in] | tdb | The database to commit the transaction. |
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).
[in] | tdb | The database to prepare the commit. |
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 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.
[in] | tdb | The database to start the transaction. |
int tdb_transaction_start_nonblock | ( | struct tdb_context * | tdb | ) |
Start a transaction, non-blocking.
[in] | tdb | The database to start the transaction. |
int tdb_traverse | ( | struct tdb_context * | tdb, |
tdb_traverse_func | fn, | ||
void * | private_data | ||
) |
Traverse the entire database.
While travering 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.
[in] | tdb | The database to traverse. |
[in] | fn | The function to call on each entry. |
[in] | private_data | The private data which should be passed to the traversing function. |
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.
[in] | tdb | The database to traverse. |
[in] | fn | The function to call on each entry. |
[in] | private_data | The private data which should be passed to the traversing function. |
int tdb_unlockall | ( | struct tdb_context * | tdb | ) |
Unlock entire database with write lock.
[in] | tdb | The database to unlock. |
int tdb_unlockall_read | ( | struct tdb_context * | tdb | ) |
Unlock entire database with read lock.
[in] | tdb | The database to unlock. |