X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Ftdb2.h;h=0731503b874ce130649f1bb64964460b1c7ad9f1;hp=3374e0cac6ba310883be85c06e600ef822bd0f25;hb=6ce40d6ab4dbd63bd76979b8491351708c3df892;hpb=39b9d9ef8c05224b5f2685003160f4092fbd7045 diff --git a/ccan/tdb2/tdb2.h b/ccan/tdb2/tdb2.h index 3374e0ca..0731503b 100644 --- a/ccan/tdb2/tdb2.h +++ b/ccan/tdb2/tdb2.h @@ -38,8 +38,14 @@ extern "C" { #include /* For uint64_t */ #include +/* For bool */ +#include +/* For memcmp */ +#include #endif #include +#include +#include union tdb_attribute; struct tdb_context; @@ -73,6 +79,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, #define TDB_NOMMAP 8 /* don't use mmap */ #define TDB_CONVERT 16 /* convert endian */ #define TDB_NOSYNC 64 /* don't use synchronous transactions */ +#define TDB_SEQNUM 128 /* maintain a sequence number */ /** * tdb_close - close and free a tdb. @@ -96,6 +103,25 @@ typedef struct tdb_data { size_t dsize; } TDB_DATA; +/** + * enum TDB_ERROR - error returns for TDB + * + * See Also: + * tdb_errorstr() + */ +enum TDB_ERROR { + TDB_SUCCESS = 0, /* No error. */ + TDB_ERR_CORRUPT = -1, /* We read the db, and it was bogus. */ + TDB_ERR_IO = -2, /* We couldn't read/write the db. */ + TDB_ERR_LOCK = -3, /* Locking failed. */ + TDB_ERR_OOM = -4, /* Out of Memory. */ + TDB_ERR_EXISTS = -5, /* The key already exists. */ + TDB_ERR_NOEXIST = -6, /* The key does not exist. */ + TDB_ERR_EINVAL = -7, /* You're using it wrong. */ + TDB_ERR_RDONLY = -8, /* The database is read-only. */ + TDB_ERR_LAST = TDB_ERR_RDONLY +}; + /** * tdb_store - store a key/value pair in a tdb. * @tdb: the tdb context returned from tdb_open() @@ -105,18 +131,18 @@ typedef struct tdb_data { * * This inserts (or overwrites) a key/value pair in the TDB. If flag * is TDB_REPLACE, it doesn't matter whether the key exists or not; - * TDB_INSERT means it must not exist (TDB_ERR_EXISTS otherwise), - * and TDB_MODIFY means it must exist (TDB_ERR_NOEXIST otherwise). + * TDB_INSERT means it must not exist (returns TDB_ERR_EXISTS otherwise), + * and TDB_MODIFY means it must exist (returns TDB_ERR_NOEXIST otherwise). * - * On success, this returns 0, on failure -1, and sets tdb_error(). + * On success, this returns TDB_SUCCESS. * * See also: * tdb_fetch, tdb_transaction_start, tdb_append, tdb_delete. */ -int tdb_store(struct tdb_context *tdb, - struct tdb_data key, - struct tdb_data dbuf, - int flag); +enum TDB_ERROR tdb_store(struct tdb_context *tdb, + struct tdb_data key, + struct tdb_data dbuf, + int flag); /* flags to tdb_store() */ #define TDB_REPLACE 1 /* A readability place holder */ @@ -127,55 +153,26 @@ int tdb_store(struct tdb_context *tdb, * tdb_fetch - fetch a value from a tdb. * @tdb: the tdb context returned from tdb_open() * @key: the key + * @data: pointer to data. * - * This looks up a key in the database and returns it, or returns tdb_null - * and sets tdb_error() if there's a problem (usually, TDB_ERR_NOEXIST). - * - * It is your responsibility to call free() on the returned structure's - * dptr. - */ -struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key); - -/** - * enum TDB_ERROR - error codes for TDB - * - * See Also: - * tdb_error(), tdb_errorstr() - */ -enum TDB_ERROR { - TDB_SUCCESS=0, /* No error. */ - TDB_ERR_CORRUPT, /* We read the db, and it was bogus. */ - TDB_ERR_IO, /* We couldn't read/write the db. */ - TDB_ERR_LOCK, /* Locking failed. */ - TDB_ERR_OOM, /* Out of Memory. */ - TDB_ERR_EXISTS, /* The key already exists. */ - TDB_ERR_NOEXIST, /* The key does not exist. */ - TDB_ERR_EINVAL, /* You're using it wrong. */ - TDB_ERR_RDONLY /* The database is read-only. */ -}; - -/** - * tdb_error - fetch the last error value from the tdb. - * @tdb: the tdb context returned from tdb_open() + * This looks up a key in the database and sets it in @data. * - * This returns the last error, or TDB_SUCCESS. It always returns TDB_SUCCESS - * immediately after tdb_open() returns the (non-NULL) tdb context. + * If it returns TDB_SUCCESS, the key was found: it is your + * responsibility to call free() on @data->dptr. * - * See Also: - * tdb_errorstr() + * Otherwise, it returns an error (usually, TDB_ERR_NOEXIST) and @data is + * undefined. */ -enum TDB_ERROR tdb_error(const struct tdb_context *tdb); +enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key, + struct tdb_data *data); /** * tdb_errorstr - map the tdb error onto a constant readable string - * @tdb: the tdb context returned from tdb_open() - * - * This is more useful for displaying errors to users than tdb_error. + * @ecode: the enum TDB_ERROR to map. * - * See Also: - * tdb_error() + * This is useful for displaying errors to users. */ -const char *tdb_errorstr(const struct tdb_context *tdb); +const char *tdb_errorstr(enum TDB_ERROR ecode); /** * tdb_append - append a value to a key/value pair in a tdb. @@ -188,27 +185,60 @@ const char *tdb_errorstr(const struct tdb_context *tdb); * doesn't exist, it's equivalent to tdb_store (with an additional hint that * you expect to expand the record in future). * - * Returns 0 on success, -1 on failure (and sets tdb_error()). - * * See Also: * tdb_fetch(), tdb_store() */ -int tdb_append(struct tdb_context *tdb, - struct tdb_data key, - struct tdb_data dbuf); +enum TDB_ERROR tdb_append(struct tdb_context *tdb, + struct tdb_data key, struct tdb_data dbuf); /** * tdb_delete - delete a key from a tdb. * @tdb: the tdb context returned from tdb_open() * @key: the key to delete. * - * Returns 0 on success, or -1 on error (usually tdb_error() would be - * TDB_ERR_NOEXIST in that case). + * Returns TDB_SUCCESS on success, or an error (usually TDB_ERR_NOEXIST). * * See Also: * tdb_fetch(), tdb_store() */ -int tdb_delete(struct tdb_context *tdb, struct tdb_data key); +enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key); + +/** + * tdb_exists - does a key exist in the database? + * @tdb: the tdb context returned from tdb_open() + * @key: the key to search for. + * + * Returns true if it exists, or false if it doesn't or any other error. + */ +bool tdb_exists(struct tdb_context *tdb, TDB_DATA key); + +/** + * tdb_deq - are struct tdb_data equal? + * @a: one struct tdb_data + * @b: another struct tdb_data + */ +static inline bool tdb_deq(struct tdb_data a, struct tdb_data b) +{ + return a.dsize == b.dsize && memcmp(a.dptr, b.dptr, a.dsize) == 0; +} + +/** + * tdb_mkdata - make a struct tdb_data from const data + * @p: the constant pointer + * @len: the length + * + * As the dptr member of struct tdb_data is not constant, you need to + * cast it. This function keeps thost casts in one place, as well as + * suppressing the warning some compilers give when casting away a + * qualifier (eg. gcc with -Wcast-qual) + */ +static inline struct tdb_data tdb_mkdata(const void *p, size_t len) +{ + struct tdb_data d; + d.dptr = cast_const(void *, p); + d.dsize = len; + return d; +} /** * tdb_transaction_start - start a transaction @@ -218,12 +248,10 @@ int tdb_delete(struct tdb_context *tdb, struct tdb_data key); * to read the tdb, but not alter it (they will block), nor will they see * any changes until tdb_transaction_commit() is called. * - * On failure, returns -1 and sets tdb_error(). - * * See Also: * tdb_transaction_cancel, tdb_transaction_commit. */ -int tdb_transaction_start(struct tdb_context *tdb); +enum TDB_ERROR tdb_transaction_start(struct tdb_context *tdb); /** * tdb_transaction_cancel - abandon a transaction @@ -244,14 +272,13 @@ void tdb_transaction_cancel(struct tdb_context *tdb); * making it robust against machine crashes, but very slow compared to * other TDB operations. * - * Returns 0 on success, or -1 on failure: this can only be caused by - * unexpected errors (eg. I/O or memory); this is no point looping on - * transaction failure. + * A failure can only be caused by unexpected errors (eg. I/O or + * memory); this is no point looping on transaction failure. * * See Also: * tdb_transaction_prepare_commit() */ -int tdb_transaction_commit(struct tdb_context *tdb); +enum TDB_ERROR tdb_transaction_commit(struct tdb_context *tdb); /** * tdb_transaction_prepare_commit - prepare to commit a transaction @@ -261,15 +288,10 @@ int tdb_transaction_commit(struct tdb_context *tdb); * tdb_transaction_commit): if this succeeds then a transaction will only * fail if the write() or fsync() calls fail. * - * Returns 0 on success, or -1 on failure. - * * See Also: * tdb_transaction_commit() */ -int tdb_transaction_prepare_commit(struct tdb_context *tdb); - -/* FIXME: Make typesafe */ -typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *); +enum TDB_ERROR tdb_transaction_prepare_commit(struct tdb_context *tdb); /** * tdb_traverse - traverse a TDB @@ -286,38 +308,94 @@ typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void * current key does not undermine the reliability of the traversal. * * On success, returns the number of keys iterated. On error returns - * -1 and sets tdb_error(). + * a negative enum TDB_ERROR value. */ -int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p); +#define tdb_traverse(tdb, fn, p) \ + tdb_traverse_(tdb, typesafe_cb_preargs(int, void *, (fn), (p), \ + struct tdb_context *, \ + TDB_DATA, TDB_DATA), (p)) + +int64_t tdb_traverse_(struct tdb_context *tdb, + int (*fn)(struct tdb_context *, + TDB_DATA, TDB_DATA, void *), void *p); + +/** + * tdb_parse_record - operate directly on data in the database. + * @tdb: the tdb context returned from tdb_open() + * @key: the key whose record we should hand to @parse + * @parse: the function to call for the data + * @data: the private pointer to hand to @parse (types must match). + * + * This avoids a copy for many cases, by handing you a pointer into + * the memory-mapped database. It also locks the record to prevent + * other accesses at the same time. + * + * Do not alter the data handed to parse()! + */ +#define tdb_parse_record(tdb, key, parse, data) \ + tdb_parse_record_((tdb), (key), \ + typesafe_cb_preargs(enum TDB_ERROR, void *, \ + (parse), (data), \ + TDB_DATA, TDB_DATA), (data)) + +enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb, + TDB_DATA key, + enum TDB_ERROR (*parse)(TDB_DATA k, + TDB_DATA d, + void *data), + void *data); + +/** + * tdb_get_seqnum - get a database sequence number + * @tdb: the tdb context returned from tdb_open() + * + * This returns a sequence number: any change to the database from a + * tdb context opened with the TDB_SEQNUM flag will cause that number + * to increment. Note that the incrementing is unreliable (it is done + * without locking), so this is only useful as an optimization. + * + * For example, you may have a regular database backup routine which + * does not operate if the sequence number is unchanged. In the + * unlikely event of a failed increment, it will be backed up next + * time any way. + * + * Returns an enum TDB_ERROR (ie. negative) on error. + */ +int64_t tdb_get_seqnum(struct tdb_context *tdb); /** * tdb_firstkey - get the "first" key in a TDB * @tdb: the tdb context returned from tdb_open() + * @key: pointer to key. * * This returns an arbitrary key in the database; with tdb_nextkey() it allows - * open-coded traversal of the database. + * open-coded traversal of the database, though it is slightly less efficient + * than tdb_traverse. + * + * It is your responsibility to free @key->dptr on success. * - * On failure, returns tdb_null and sets tdb_error(). On success, returns - * a key, or tdb_null and set tdb_error() to TDB_SUCCESS for an empty database. + * Returns TDB_ERR_NOEXIST if the database is empty. */ -TDB_DATA tdb_firstkey(struct tdb_context *tdb); +enum TDB_ERROR tdb_firstkey(struct tdb_context *tdb, struct tdb_data *key); /** * tdb_nextkey - get the "next" key in a TDB * @tdb: the tdb context returned from tdb_open() * @key: a key returned by tdb_firstkey() or tdb_nextkey(). * - * This returns another key in the database. On failure or the last key - * it returns tdb_null: tdb_error() will be TDB_SUCCESS if it was the last key. + * This returns another key in the database; it will free @key.dptr for + * your convenience. + * + * Returns TDB_ERR_NOEXIST if there are no more keys. */ -TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key); +enum TDB_ERROR tdb_nextkey(struct tdb_context *tdb, struct tdb_data *key); /** * tdb_chainlock - lock a record in the TDB * @tdb: the tdb context returned from tdb_open() * @key: the key to lock. * - * This prevents any changes from occurring to a group of keys including @key, + * This prevents any access occurring to a group of keys including @key, * even if @key does not exist. This allows primitive atomic updates of * records without using transactions. * @@ -328,30 +406,122 @@ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key); * See Also: * tdb_chainunlock() */ -int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key); +enum TDB_ERROR tdb_chainlock(struct tdb_context *tdb, TDB_DATA key); /** * tdb_chainunlock - unlock a record in the TDB * @tdb: the tdb context returned from tdb_open() * @key: the key to unlock. + * + * The key must have previously been locked by tdb_chainlock(). + */ +void tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key); + +/** + * tdb_chainlock_read - lock a record in the TDB, for reading + * @tdb: the tdb context returned from tdb_open() + * @key: the key to lock. + * + * This prevents any changes from occurring to a group of keys including @key, + * even if @key does not exist. This allows primitive atomic updates of + * records without using transactions. + * + * You cannot begin a transaction while holding a tdb_chainlock_read(), nor can + * you do any operations on any other keys in the database. This also means + * that you cannot hold more than one tdb_chainlock()/read() at a time. + * + * See Also: + * tdb_chainlock() + */ +enum TDB_ERROR tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key); + +/** + * tdb_chainunlock_read - unlock a record in the TDB for reading + * @tdb: the tdb context returned from tdb_open() + * @key: the key to unlock. + * + * The key must have previously been locked by tdb_chainlock_read(). + */ +void tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key); + +/** + * tdb_lockall - lock the entire TDB + * @tdb: the tdb context returned from tdb_open() + * + * You cannot hold a tdb_chainlock while calling this. It nests, so you + * must call tdb_unlockall as many times as you call tdb_lockall. */ -int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key); +enum TDB_ERROR tdb_lockall(struct tdb_context *tdb); + +/** + * tdb_unlockall - unlock the entire TDB + * @tdb: the tdb context returned from tdb_open() + */ +void tdb_unlockall(struct tdb_context *tdb); + +/** + * tdb_lockall_read - lock the entire TDB for reading + * @tdb: the tdb context returned from tdb_open() + * + * This prevents others writing to the database, eg. tdb_delete, tdb_store, + * tdb_append, but not tdb_fetch. + * + * You cannot hold a tdb_chainlock while calling this. It nests, so you + * must call tdb_unlockall_read as many times as you call tdb_lockall_read. + */ +enum TDB_ERROR tdb_lockall_read(struct tdb_context *tdb); + +/** + * tdb_unlockall_read - unlock the entire TDB for reading + * @tdb: the tdb context returned from tdb_open() + */ +void tdb_unlockall_read(struct tdb_context *tdb); + +/** + * tdb_wipe_all - wipe the database clean + * @tdb: the tdb context returned from tdb_open() + * + * Completely erase the database. This is faster than iterating through + * each key and doing tdb_delete. + */ +enum TDB_ERROR tdb_wipe_all(struct tdb_context *tdb); /** * tdb_check - check a TDB for consistency * @tdb: the tdb context returned from tdb_open() * @check: function to check each key/data pair (or NULL) - * @private_data: pointer for @check + * @data: argument for @check, must match type. * * This performs a consistency check of the open database, optionally calling * a check() function on each record so you can do your own data consistency - * checks as well. If check() returns non-zero, it is considered a failure. + * checks as well. If check() returns an error, that is returned from + * tdb_check(). * - * Returns 0 on success, or -1 on failure and sets tdb_error(). + * Returns TDB_SUCCESS or an error. */ -int tdb_check(struct tdb_context *tdb, - int (*check)(TDB_DATA key, TDB_DATA data, void *private_data), - void *private_data); +#define tdb_check(tdb, check, data) \ + tdb_check_((tdb), typesafe_cb_preargs(enum TDB_ERROR, void *, \ + (check), (data), \ + struct tdb_data, \ + struct tdb_data), \ + (data)) + +enum TDB_ERROR tdb_check_(struct tdb_context *tdb, + enum TDB_ERROR (*check)(struct tdb_data k, + struct tdb_data d, + void *data), + void *data); + +/** + * tdb_error - get the last error (not threadsafe) + * @tdb: the tdb context returned from tdb_open() + * + * Returns the last error returned by a TDB function. + * + * This makes porting from TDB1 easier, but note that the last error is not + * reliable in threaded programs. + */ +enum TDB_ERROR tdb_error(struct tdb_context *tdb); /** * enum tdb_summary_flags - flags for tdb_summary. @@ -364,22 +534,70 @@ enum tdb_summary_flags { * tdb_summary - return a string describing the TDB state * @tdb: the tdb context returned from tdb_open() * @flags: flags to control the summary output. + * @summary: pointer to string to allocate. * * This returns a developer-readable string describing the overall * state of the tdb, such as the percentage used and sizes of records. * It is designed to provide information about the tdb at a glance * without displaying any keys or data in the database. * - * On success, returns a nul-terminated multi-line string. On failure, - * returns NULL and sets tdb_error(). + * On success, sets @summary to point to a malloc()'ed nul-terminated + * multi-line string. It is your responsibility to free() it. */ -char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags); +enum TDB_ERROR tdb_summary(struct tdb_context *tdb, + enum tdb_summary_flags flags, + char **summary); + +/** + * tdb_get_flags - return the flags for a tdb + * @tdb: the tdb context returned from tdb_open() + * + * This returns the flags on the current tdb. Some of these are caused by + * the flags argument to tdb_open(), others (such as TDB_CONVERT) are + * intuited. + */ +unsigned int tdb_get_flags(struct tdb_context *tdb); +/** + * tdb_add_flag - set a flag for a tdb + * @tdb: the tdb context returned from tdb_open() + * @flag: one of TDB_NOLOCK, TDB_NOMMAP or TDB_NOSYNC. + * + * You can use this to set a flag on the TDB. You cannot set these flags + * on a TDB_INTERNAL tdb. + */ +void tdb_add_flag(struct tdb_context *tdb, unsigned flag); -int64_t tdb_traverse_read(struct tdb_context *tdb, - tdb_traverse_func fn, void *p); +/** + * tdb_remove_flag - unset a flag for a tdb + * @tdb: the tdb context returned from tdb_open() + * @flag: one of TDB_NOLOCK, TDB_NOMMAP or TDB_NOSYNC. + * + * You can use this to clear a flag on the TDB. You cannot clear flags + * on a TDB_INTERNAL tdb. + */ +void tdb_remove_flag(struct tdb_context *tdb, unsigned flag); + +/** + * tdb_name - get the name of a tdb + * @tdb: the tdb context returned from tdb_open() + * + * This returns a copy of the name string, made at tdb_open() time. If that + * argument was NULL (possible for a TDB_INTERNAL db) this will return NULL. + * + * This is mostly useful for logging. + */ +const char *tdb_name(const struct tdb_context *tdb); +/** + * tdb_fd - get the file descriptor of a tdb + * @tdb: the tdb context returned from tdb_open() + * + * This returns the file descriptor for the underlying database file, or -1 + * for TDB_INTERNAL. + */ +int tdb_fd(const struct tdb_context *tdb); /** * enum tdb_attribute_type - descriminator for union tdb_attribute. @@ -422,11 +640,11 @@ enum tdb_log_level { */ struct tdb_attribute_log { struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */ - void (*log_fn)(struct tdb_context *tdb, - enum tdb_log_level level, - void *log_private, - const char *message); - void *log_private; + void (*fn)(struct tdb_context *tdb, + enum tdb_log_level level, + const char *message, + void *data); + void *data; }; /** @@ -443,9 +661,9 @@ struct tdb_attribute_log { */ struct tdb_attribute_hash { struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_HASH */ - uint64_t (*hash_fn)(const void *key, size_t len, uint64_t seed, - void *priv); - void *hash_private; + uint64_t (*fn)(const void *key, size_t len, uint64_t seed, + void *data); + void *data; }; /** @@ -515,11 +733,6 @@ union tdb_attribute { struct tdb_attribute_stats stats; }; -/** - * tdb_null - a convenient value for errors. - */ -extern struct tdb_data tdb_null; - #ifdef __cplusplus } #endif