X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Ftdb2.h;h=50903a7641bfcc409c2c26d7d74a0a1c40210d60;hp=684bc0492a7260d9f284f8e5d59b3c3c0a097cf3;hb=a97da100b00206544c7a68593b64a49f2b854f7e;hpb=a0e0927e1f34d80fd4d4ee2d68fa2acb94ae9eb2 diff --git a/ccan/tdb2/tdb2.h b/ccan/tdb2/tdb2.h index 684bc049..50903a76 100644 --- a/ccan/tdb2/tdb2.h +++ b/ccan/tdb2/tdb2.h @@ -38,6 +38,8 @@ extern "C" { #include /* For uint64_t */ #include +/* For bool */ +#include #endif #include #include @@ -197,6 +199,15 @@ enum TDB_ERROR tdb_append(struct tdb_context *tdb, */ 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_transaction_start - start a transaction * @tdb: the tdb context returned from tdb_open() @@ -276,6 +287,30 @@ 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 + * @p: 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, p) \ + tdb_parse_record_((tdb), (key), \ + typesafe_cb_preargs(enum TDB_ERROR, (parse), (p), \ + TDB_DATA, TDB_DATA), (p)) + +enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb, + TDB_DATA key, + enum TDB_ERROR (*parse)(TDB_DATA key, + TDB_DATA data, + void *p), + void *p); /** * tdb_firstkey - get the "first" key in a TDB * @tdb: the tdb context returned from tdb_open() @@ -328,6 +363,15 @@ enum TDB_ERROR tdb_chainlock(struct tdb_context *tdb, TDB_DATA key); */ enum TDB_ERROR tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key); +/** + * 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() @@ -379,6 +423,37 @@ 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); + +/** + * 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); + /** * enum tdb_attribute_type - descriminator for union tdb_attribute. */