]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/tdb2.h
tdb2: remove tdb_null
[ccan] / ccan / tdb2 / tdb2.h
index 47661da245352583f5491b440fec2e58b1ff0e68..d9e2d9ecb520acd4e97468676c45051e109b4dcf 100644 (file)
@@ -38,6 +38,10 @@ extern "C" {
 #include <signal.h>
 /* For uint64_t */
 #include <stdint.h>
+/* For bool */
+#include <stdbool.h>
+/* For memcmp */
+#include <string.h>
 #endif
 #include <ccan/compiler/compiler.h>
 #include <ccan/typesafe_cb/typesafe_cb.h>
@@ -197,6 +201,25 @@ 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_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_transaction_start - start a transaction
  * @tdb: the tdb context returned from tdb_open()
@@ -276,6 +299,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 +375,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()
@@ -544,11 +600,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