tdb2: tdb_deq: inline helper for comparing two struct tdb_data
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 17 Mar 2011 11:42:21 +0000 (22:12 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 17 Mar 2011 11:42:21 +0000 (22:12 +1030)
Everyone writes their own, so provide a standard one.

ccan/tdb2/doc/TDB1_porting.txt
ccan/tdb2/tdb2.h
ccan/tdb2/test/external-agent.c
ccan/tdb2/test/logging.h
ccan/tdb2/test/run-11-simple-fetch.c
ccan/tdb2/test/run-12-store.c
ccan/tdb2/test/run-13-delete.c
ccan/tdb2/test/run-21-parse_record.c
ccan/tdb2/test/run-open-multiple-times.c

index fee020073b748df6f6d6360abe6e4ebad27e8122..4a6e924411302655db1d9a5facacd717839e9238 100644 (file)
@@ -23,3 +23,5 @@ Interface differences between TDB1 and TDB2.
   already formatted, and it takes an enum tdb_log_level not a tdb_debug_level,
   and which has only three values: TDB_LOG_ERROR, TDB_LOG_USE_ERROR and
   TDB_LOG_WARNING.
+
+- tdb2 provides tdb_deq() for comparing two struct tdb_data.
index 50903a7641bfcc409c2c26d7d74a0a1c40210d60..811d9120458d3b720c670be0179a48db437c5533 100644 (file)
@@ -40,6 +40,8 @@ extern "C" {
 #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>
@@ -208,6 +210,16 @@ enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key);
  */
 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()
index b5dc56f477e169d19008912f61d242dacdc86bf0..79c299a088047ccbbc7d495548d89e4c34fade89 100644 (file)
@@ -56,8 +56,7 @@ static enum agent_return do_operation(enum operation op, const char *name)
                        ret = FAILED;
                } else if (ecode < 0) {
                        ret = OTHER_FAILURE;
-               } else if (data.dsize != k.dsize
-                          || memcmp(data.dptr, k.dptr, k.dsize) != 0) {
+               } else if (!tdb_deq(data, k)) {
                        ret = OTHER_FAILURE;
                        free(data.dptr);
                } else {
index 07d4e10aedee10b112a061212e854a0d4d9bae2d..502506d4253197fbc1079a8e07d80f54f125af2c 100644 (file)
@@ -12,11 +12,4 @@ extern union tdb_attribute tap_log_attr;
 void tap_log_fn(struct tdb_context *tdb,
                enum tdb_log_level level, void *priv,
                const char *message);
-
-static inline bool data_equal(struct tdb_data a, struct tdb_data b)
-{
-       if (a.dsize != b.dsize)
-               return false;
-       return memcmp(a.dptr, b.dptr, a.dsize) == 0;
-}
 #endif /* TDB2_TEST_LOGGING_H */
index c16c28f8ea8c1def7ad0a75394fc264c99055d20..ad1933cd3b0566f3e005b6a428efc85374eb04e2 100644 (file)
@@ -60,7 +60,7 @@ int main(int argc, char *argv[])
                        if (!ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS))
                                goto fail;
                        failtest_suppress = true;
-                       ok1(data_equal(d, data));
+                       ok1(tdb_deq(d, data));
                        free(d.dptr);
                        ok1(tdb_check(tdb, NULL, NULL) == 0);
                        tdb_close(tdb);
index 0b3c29651c44b2cebcc9b4c4fb09754f15afa423..5c21e3a1d0cd0ab8d3658b18d31bfd4979b03843 100644 (file)
@@ -16,13 +16,6 @@ static uint64_t fixedhash(const void *key, size_t len, uint64_t seed, void *p)
                             *(uint64_t *)p);
 }
 
-static bool equal(struct tdb_data a, struct tdb_data b)
-{
-       if (a.dsize != b.dsize)
-               return false;
-       return memcmp(a.dptr, b.dptr, a.dsize) == 0;
-}
-
 int main(int argc, char *argv[])
 {
        unsigned int i, j;
@@ -54,7 +47,7 @@ int main(int argc, char *argv[])
                        struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
                        ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
                        ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
-                       ok1(equal(d, data));
+                       ok1(tdb_deq(d, data));
                        free(d.dptr);
                }
                tdb_close(tdb);
index 3eaeee7a29b1ff8e3f707da522d4c6ed7ca29319..aba6fe29448c3fb3fb53d7acf3bad8ef40fc7ca4 100644 (file)
@@ -33,9 +33,7 @@ static bool store_records(struct tdb_context *tdb)
                if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
                        return false;
                tdb_fetch(tdb, key, &d);
-               if (d.dsize != data.dsize)
-                       return false;
-               if (memcmp(d.dptr, data.dptr, d.dsize) != 0)
+               if (!tdb_deq(d, data))
                        return false;
                free(d.dptr);
        }
index b6601d0108bbff98b7578c1c2888385ae1dfd28f..773cdff4e0cf56c2c438698906adc1faa2b1f77f 100644 (file)
@@ -11,9 +11,7 @@
 
 static enum TDB_ERROR parse(TDB_DATA key, TDB_DATA data, TDB_DATA *expected)
 {
-       if (data.dsize != expected->dsize)
-               return TDB_ERR_EINVAL;
-       if (memcmp(data.dptr, expected->dptr, data.dsize) != 0)
+       if (!tdb_deq(data, *expected))
                return TDB_ERR_EINVAL;
        return TDB_SUCCESS;
 }
index 1f35648bb192c445da0fe49a455d95ea544991c1..240828df16bd3ee0d38713a39e4ccb2a078a43e7 100644 (file)
@@ -34,8 +34,7 @@ int main(int argc, char *argv[])
                /* Store in one, fetch in the other. */
                ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
                ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
-               ok1(d.dptr && d.dsize == data.dsize
-                   && memcmp(d.dptr, data.dptr, d.dsize) == 0);
+               ok1(tdb_deq(d, data));
                free(d.dptr);
 
                /* Vice versa, with delete. */
@@ -47,8 +46,7 @@ int main(int argc, char *argv[])
 
                ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == 0);
                ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
-               ok1(d.dptr && d.dsize == data.dsize
-                   && memcmp(d.dptr, data.dptr, d.dsize) == 0);
+               ok1(tdb_deq(d, data));
                free(d.dptr);
 
                /* Reopen */
@@ -75,8 +73,7 @@ int main(int argc, char *argv[])
                ok1(tdb_close(tdb2) == 0);
 
                ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
-               ok1(d.dptr && d.dsize == data.dsize
-                   && memcmp(d.dptr, data.dptr, d.dsize) == 0);
+               ok1(tdb_deq(d, data));
                free(d.dptr);
                ok1(tdb_close(tdb) == 0);
                ok1(tap_log_messages == 4);