]> git.ozlabs.org Git - ccan/commitdiff
tdb2: tdb_foreach()
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 05:37:16 +0000 (15:07 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 05:37:16 +0000 (15:07 +0930)
Create an iterator over every open tdb (not internal TDBs).  This is
useful for re-establishing the tdb1-style active lock for
CLEAR_IF_FIRST.

ccan/tdb2/open.c
ccan/tdb2/tdb2.h
ccan/tdb2/test/run-tdb_foreach.c [new file with mode: 0644]

index 5ee2ed7844105d7c8e753e7e79d5b7885ba1d5e0..555df9512608c1ba0269461e6f5dc823de9ab4f6 100644 (file)
@@ -689,3 +689,13 @@ int tdb_close(struct tdb_context *tdb)
 
        return ret;
 }
+
+void tdb_foreach_(int (*fn)(struct tdb_context *, void *), void *p)
+{
+       struct tdb_context *i;
+
+       for (i = tdbs; i; i = i->next) {
+               if (fn(i, p) != 0)
+                       break;
+       }
+}
index 7a7aaca90fbfa7715749c5329fb447b63831b432..d5ae329a6029d7659886992dd380e845bd9d2fb4 100644 (file)
@@ -670,6 +670,20 @@ const char *tdb_name(const struct tdb_context *tdb);
  */
 int tdb_fd(const struct tdb_context *tdb);
 
+/**
+ * tdb_foreach - iterate through every open TDB.
+ * @fn: the function to call for every TDB
+ * @p: the pointer to hand to @fn
+ *
+ * TDB internally keeps track of all open TDBs; this function allows you to
+ * iterate through them.  If @fn returns non-zero, traversal stops.
+ */
+#define tdb_foreach(fn, p)                                             \
+       tdb_foreach_(typesafe_cb_preargs(int, void *, (fn), (p),        \
+                                        struct tdb_context *), (p))
+
+void tdb_foreach_(int (*fn)(struct tdb_context *, void *), void *p);
+
 /**
  * struct tdb_attribute_base - common fields for all tdb attributes.
  */
diff --git a/ccan/tdb2/test/run-tdb_foreach.c b/ccan/tdb2/test/run-tdb_foreach.c
new file mode 100644 (file)
index 0000000..e34dfb8
--- /dev/null
@@ -0,0 +1,93 @@
+#include <ccan/tdb2/tdb.c>
+#include <ccan/tdb2/open.c>
+#include <ccan/tdb2/free.c>
+#include <ccan/tdb2/lock.c>
+#include <ccan/tdb2/io.c>
+#include <ccan/tdb2/hash.c>
+#include <ccan/tdb2/transaction.c>
+#include <ccan/tdb2/check.c>
+#include <ccan/tap/tap.h>
+#include "logging.h"
+
+static int drop_count(struct tdb_context *tdb, unsigned int *count)
+{
+       if (--(*count) == 0)
+               return 1;
+       return 0;
+}
+
+static int set_found(struct tdb_context *tdb, bool found[3])
+{
+       unsigned int idx;
+
+       if (strcmp(tdb_name(tdb), "run-tdb_foreach0.tdb") == 0)
+               idx = 0;
+       else if (strcmp(tdb_name(tdb), "run-tdb_foreach1.tdb") == 0)
+               idx = 1;
+       else if (strcmp(tdb_name(tdb), "run-tdb_foreach2.tdb") == 0)
+               idx = 2;
+       else
+               abort();
+
+       if (found[idx])
+               abort();
+       found[idx] = true;
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       unsigned int i, count;
+       bool found[3];
+       struct tdb_context *tdb0, *tdb1, *tdb2;
+       int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
+                       TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
+
+       plan_tests(sizeof(flags) / sizeof(flags[0]) * 8);
+       for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
+               tdb0 = tdb_open("run-tdb_foreach0.tdb", flags[i],
+                               O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
+               tdb1 = tdb_open("run-tdb_foreach1.tdb", flags[i],
+                               O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
+               tdb2 = tdb_open("run-tdb_foreach2.tdb", flags[i],
+                               O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
+
+               memset(found, 0, sizeof(found));
+               tdb_foreach(set_found, found);
+               ok1(found[0] && found[1] && found[2]);
+
+               /* Test premature iteration termination */
+               count = 1;
+               tdb_foreach(drop_count, &count);
+               ok1(count == 0);
+
+               tdb_close(tdb1);
+               memset(found, 0, sizeof(found));
+               tdb_foreach(set_found, found);
+               ok1(found[0] && !found[1] && found[2]);
+
+               tdb_close(tdb2);
+               memset(found, 0, sizeof(found));
+               tdb_foreach(set_found, found);
+               ok1(found[0] && !found[1] && !found[2]);
+
+               tdb1 = tdb_open("run-tdb_foreach1.tdb", flags[i],
+                               O_RDWR, 0600, &tap_log_attr);
+               memset(found, 0, sizeof(found));
+               tdb_foreach(set_found, found);
+               ok1(found[0] && found[1] && !found[2]);
+
+               tdb_close(tdb0);
+               memset(found, 0, sizeof(found));
+               tdb_foreach(set_found, found);
+               ok1(!found[0] && found[1] && !found[2]);
+
+               tdb_close(tdb1);
+               memset(found, 0, sizeof(found));
+               tdb_foreach(set_found, found);
+               ok1(!found[0] && !found[1] && !found[2]);
+               ok1(tap_log_messages == 0);
+       }
+
+       return exit_status();
+}