tdb2: unify tdb1_traverse into tdb_traverse
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 06:01:06 +0000 (15:31 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 06:01:06 +0000 (15:31 +0930)
Switch on the TDB_VERSION1 flag; we now only do a tdb1_traverse_read on a
read-only database, as there is no tdb2 equivalent.

ccan/tdb2/private.h
ccan/tdb2/tdb1.h
ccan/tdb2/tdb1_tdb.c
ccan/tdb2/tdb1_traverse.c
ccan/tdb2/test/run-tdb1-3G-file.c
ccan/tdb2/test/run-tdb1-nested-traverse.c
ccan/tdb2/test/run-tdb1-no-lock-during-traverse.c
ccan/tdb2/test/run-tdb1-traverse-in-transaction.c
ccan/tdb2/traverse.c

index fb04656da639cb1d2799ff3d7207853b00c6aa29..98e26c17336ac4ea1b3f94e9f9a74e400f576ca4 100644 (file)
@@ -646,6 +646,11 @@ int tdb1_allrecord_unlock(struct tdb_context *tdb, int ltype);
 int tdb1_transaction_recover(struct tdb_context *tdb);
 int tdb1_transaction_cancel(struct tdb_context *tdb);
 
+/* tdb1_traverse.c: */
+int tdb1_traverse(struct tdb_context *tdb,
+                 int (*)(struct tdb_context *, TDB_DATA, TDB_DATA, void *),
+                 void *private_data);
+
 /* tdb1_tdb.c: */
 int tdb1_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
 enum TDB_ERROR tdb1_fetch(struct tdb_context *tdb, TDB_DATA key,
index da77e27c88b7421394e2950242f34f771d2b9a23..4b0e454d1b7141c90a1481a4581c7a0850155b77 100644 (file)
@@ -36,8 +36,6 @@
 #endif
 
 
-typedef int (*tdb1_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
-
 void tdb1_set_max_dead(struct tdb_context *tdb, int max_dead);
 
 int tdb1_parse_record(struct tdb_context *tdb, TDB_DATA key,
@@ -49,10 +47,6 @@ TDB_DATA tdb1_firstkey(struct tdb_context *tdb);
 
 TDB_DATA tdb1_nextkey(struct tdb_context *tdb, TDB_DATA key);
 
-int tdb1_traverse(struct tdb_context *tdb, tdb1_traverse_func fn, void *private_data);
-
-int tdb1_traverse_read(struct tdb_context *tdb, tdb1_traverse_func fn, void *private_data);
-
 int tdb1_lockall(struct tdb_context *tdb);
 
 int tdb1_unlockall(struct tdb_context *tdb);
index 2c989bb9a4fb16a3a1db501fd76c87d4f43a8461..d7f518dce487c74a492c459b274d04d3fd9db803 100644 (file)
@@ -862,7 +862,7 @@ int tdb1_repack(struct tdb_context *tdb)
        state.error = TDB_SUCCESS;
        state.dest_db = tmp_db;
 
-       if (tdb1_traverse_read(tdb, repack_traverse, &state) == -1) {
+       if (tdb1_traverse(tdb, repack_traverse, &state) == -1) {
                tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
                           __location__ " Failed to traverse copying out");
                tdb1_transaction_cancel(tdb);
@@ -889,7 +889,7 @@ int tdb1_repack(struct tdb_context *tdb)
        state.error = TDB_SUCCESS;
        state.dest_db = tdb;
 
-       if (tdb1_traverse_read(tmp_db, repack_traverse, &state) == -1) {
+       if (tdb1_traverse(tmp_db, repack_traverse, &state) == -1) {
                tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
                           __location__ " Failed to traverse copying back");
                tdb1_transaction_cancel(tdb);
index 87e233b47fe3fa4551b11db6adee922fc242978e..63052854d5428aef78b589667f5c51d3fc3ade3f 100644 (file)
@@ -144,8 +144,10 @@ static tdb1_off_t tdb1_next_lock(struct tdb_context *tdb, struct tdb1_traverse_l
    a non-zero return value from fn() indicates that the traversal should stop
   */
 static int tdb1_traverse_internal(struct tdb_context *tdb,
-                                tdb1_traverse_func fn, void *private_data,
-                                struct tdb1_traverse_lock *tl)
+                                 int (*fn)(struct tdb_context *,
+                                           TDB_DATA, TDB_DATA, void *),
+                                 void *private_data,
+                                 struct tdb1_traverse_lock *tl)
 {
        TDB_DATA key, dbuf;
        struct tdb1_record rec;
@@ -213,10 +215,12 @@ out:
 
 
 /*
-  a write style traverse - temporarily marks the db read only
+  a read style traverse - only if db read only
 */
-int tdb1_traverse_read(struct tdb_context *tdb,
-                     tdb1_traverse_func fn, void *private_data)
+static int tdb1_traverse_read(struct tdb_context *tdb,
+                             int (*fn)(struct tdb_context *,
+                                       TDB_DATA, TDB_DATA, void *),
+                             void *private_data)
 {
        struct tdb1_traverse_lock tl = { NULL, 0, 0, F_RDLCK };
        int ret;
@@ -244,12 +248,14 @@ int tdb1_traverse_read(struct tdb_context *tdb,
   alignment restrictions malloc gives you.
 */
 int tdb1_traverse(struct tdb_context *tdb,
-                tdb1_traverse_func fn, void *private_data)
+                 int (*fn)(struct tdb_context *, TDB_DATA, TDB_DATA, void *),
+                 void *private_data)
 {
        struct tdb1_traverse_lock tl = { NULL, 0, 0, F_WRLCK };
        int ret;
 
-       if ((tdb->flags & TDB_RDONLY) || tdb->tdb1.traverse_read) {
+       /* If we're read-only, we don't have to write-lock whole db. */
+       if (tdb->flags & TDB_RDONLY) {
                return tdb1_traverse_read(tdb, fn, private_data);
        }
 
index 116c655438106fdffed0c74a35ea62b618701b33..074901857faf3d25d03d0dfb8fbe6ef1ff436981 100644 (file)
@@ -101,12 +101,12 @@ int main(int argc, char *argv[])
        tdb1_unlock(tdb, TDB1_BUCKET(rec.full_hash), F_RDLCK);
 
        /* Traverse must work. */
-       ok1(tdb1_traverse(tdb, test_traverse, &orig_data) == 1);
+       ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
 
        /* Delete should work. */
        ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
 
-       ok1(tdb1_traverse(tdb, test_traverse, NULL) == 0);
+       ok1(tdb_traverse(tdb, test_traverse, NULL) == 0);
 
        /* Transactions should work. */
        ok1(tdb1_transaction_start(tdb) == 0);
@@ -118,7 +118,7 @@ int main(int argc, char *argv[])
        free(data.dptr);
        ok1(tdb1_transaction_commit(tdb) == 0);
 
-       ok1(tdb1_traverse(tdb, test_traverse, &orig_data) == 1);
+       ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
        tdb_close(tdb);
 
        return exit_status();
index ef19911d14f058f54c331201cc3851064b0ecdb1..cf5aa4a2f458834bfc2ad0d2ea42ee519e0a1ccc 100644 (file)
@@ -38,7 +38,7 @@ static int traverse1(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
        ok1(correct_data(data));
        ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
            == WOULD_HAVE_BLOCKED);
-       tdb1_traverse(tdb, traverse2, NULL);
+       tdb_traverse(tdb, traverse2, NULL);
 
        /* That should *not* release the transaction lock! */
        ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
@@ -77,8 +77,10 @@ int main(int argc, char *argv[])
        data.dsize = strlen("world");
 
        ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
-       tdb1_traverse(tdb, traverse1, NULL);
-       tdb1_traverse_read(tdb, traverse1, NULL);
+       tdb_traverse(tdb, traverse1, NULL);
+       tdb_add_flag(tdb, TDB_RDONLY);
+       tdb_traverse(tdb, traverse1, NULL);
+       tdb_remove_flag(tdb, TDB_RDONLY);
        tdb_close(tdb);
 
        return exit_status();
index 383b74cb87d59965b5ec194aa6cedac8a6a01c6d..71404c2d7507077fb32d382d27f8e9315a6d2190 100644 (file)
@@ -74,7 +74,7 @@ int main(int argc, char *argv[])
        hsize.base.next = &tap_log_attr;
        hsize.tdb1_hashsize.hsize = 1024;
 
-       plan_tests(41);
+       plan_tests(43);
        tdb = tdb_open("run-no-lock-during-traverse.tdb1",
                       TDB_VERSION1, O_CREAT|O_TRUNC|O_RDWR,
                       0600, &hsize);
@@ -84,7 +84,7 @@ int main(int argc, char *argv[])
        ok1(locking_errors1 == 0);
        ok1(tdb1_lockall(tdb) == 0);
        ok1(locking_errors1 == 0);
-       tdb1_traverse(tdb, delete_other, &errors);
+       ok1(tdb_traverse(tdb, delete_other, &errors) >= 0);
        ok1(errors == 0);
        ok1(locking_errors1 == 0);
        ok1(tdb1_unlockall(tdb) == 0);
@@ -93,7 +93,7 @@ int main(int argc, char *argv[])
        ok1(locking_errors1 == 0);
        ok1(tdb1_lockall(tdb) == 0);
        ok1(locking_errors1 == 0);
-       tdb1_traverse(tdb, delete_self, NULL);
+       ok1(tdb_traverse(tdb, delete_self, NULL) == NUM_ENTRIES);
        ok1(locking_errors1 == 0);
        ok1(tdb1_unlockall(tdb) == 0);
 
index 90783198d91be5bf62bce6d1ce809c28e002b95d..c4fd89dfe4b79b44996063d5dfb6b7d7ec794fcc 100644 (file)
@@ -64,12 +64,12 @@ int main(int argc, char *argv[])
        ok1(tdb1_transaction_start(tdb) == 0);
        ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
            == WOULD_HAVE_BLOCKED);
-       tdb1_traverse(tdb, traverse, NULL);
+       tdb_traverse(tdb, traverse, NULL);
 
        /* That should *not* release the transaction lock! */
        ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
            == WOULD_HAVE_BLOCKED);
-       tdb1_traverse_read(tdb, traverse, NULL);
+       tdb_traverse(tdb, traverse, NULL);
 
        /* That should *not* release the transaction lock! */
        ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
index f8a2504dcd61489eb23bf7b84c8e0a88ced07f43..d93c05abbd76996f66b5a376431648fd30645e3e 100644 (file)
@@ -28,6 +28,13 @@ int64_t tdb_traverse_(struct tdb_context *tdb,
        struct tdb_data k, d;
        int64_t count = 0;
 
+       if (tdb->flags & TDB_VERSION1) {
+               count = tdb1_traverse(tdb, fn, p);
+               if (count == -1)
+                       return tdb->last_error;
+               return count;
+       }
+
        k.dptr = NULL;
        for (ecode = first_in_hash(tdb, &tinfo, &k, &d.dsize);
             ecode == TDB_SUCCESS;