X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftdb2%2Ftraverse.c;h=06f0bc68ee4016b5a90330025f1c2ee34d1ad108;hb=3d917ba6dffe2029608a3d4c870dfdb4033ca4c9;hp=410054d7947a704407dbf3c559a569d22ea27a87;hpb=cd1c68c0d989e6aed401260f92c63db8e8132db5;p=ccan diff --git a/ccan/tdb2/traverse.c b/ccan/tdb2/traverse.c index 410054d7..06f0bc68 100644 --- a/ccan/tdb2/traverse.c +++ b/ccan/tdb2/traverse.c @@ -1,7 +1,7 @@ - /* + /* Trivial Database 2: traverse function. Copyright (C) Rusty Russell 2010 - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -18,59 +18,49 @@ #include "private.h" #include -static int64_t traverse(struct tdb_context *tdb, int ltype, - tdb_traverse_func fn, void *p) +int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p) { - int ret; + enum TDB_ERROR ecode; struct traverse_info tinfo; struct tdb_data k, d; int64_t count = 0; - for (ret = first_in_hash(tdb, ltype, &tinfo, &k, &d.dsize); - ret == 1; - ret = next_in_hash(tdb, ltype, &tinfo, &k, &d.dsize)) { + k.dptr = NULL; + for (ecode = first_in_hash(tdb, &tinfo, &k, &d.dsize); + ecode == TDB_SUCCESS; + ecode = next_in_hash(tdb, &tinfo, &k, &d.dsize)) { d.dptr = k.dptr + k.dsize; count++; - if (fn && fn(tdb, k, d, p)) + if (fn && fn(tdb, k, d, p)) { + free(k.dptr); break; + } + free(k.dptr); } - if (ret < 0) + if (ecode != TDB_ERR_NOEXIST) { + tdb->ecode = ecode; return -1; + } return count; } - -int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p) -{ - return traverse(tdb, F_WRLCK, fn, p); -} -int64_t tdb_traverse_read(struct tdb_context *tdb, - tdb_traverse_func fn, void *p) -{ - int64_t ret; - bool was_ro = tdb->read_only; - tdb->read_only = true; - ret = traverse(tdb, F_RDLCK, fn, p); - tdb->read_only = was_ro; - return ret; -} - TDB_DATA tdb_firstkey(struct tdb_context *tdb) { struct traverse_info tinfo; struct tdb_data k; - switch (first_in_hash(tdb, F_RDLCK, &tinfo, &k, NULL)) { - case 1: + enum TDB_ERROR ecode; + + ecode = first_in_hash(tdb, &tinfo, &k, NULL); + if (ecode == TDB_SUCCESS) { return k; - case 0: - tdb->ecode = TDB_SUCCESS; - /* Fall thru... */ - default: - return tdb_null; } -} + if (ecode == TDB_ERR_NOEXIST) + ecode = TDB_SUCCESS; + tdb->ecode = ecode; + return tdb_null; +} /* We lock twice, not very efficient. We could keep last key & tinfo cached. */ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key) @@ -78,19 +68,21 @@ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key) struct traverse_info tinfo; struct hash_info h; struct tdb_used_record rec; + enum TDB_ERROR ecode; tinfo.prev = find_and_lock(tdb, key, F_RDLCK, &h, &rec, &tinfo); - if (unlikely(tinfo.prev == TDB_OFF_ERR)) + if (TDB_OFF_IS_ERR(tinfo.prev)) { + tdb->ecode = tinfo.prev; return tdb_null; + } tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK); - switch (next_in_hash(tdb, F_RDLCK, &tinfo, &key, NULL)) { - case 1: + ecode = next_in_hash(tdb, &tinfo, &key, NULL); + if (ecode == TDB_SUCCESS) { return key; - case 0: - tdb->ecode = TDB_SUCCESS; - /* Fall thru... */ - default: - return tdb_null; } -} + if (ecode == TDB_ERR_NOEXIST) + ecode = TDB_SUCCESS; + tdb->ecode = ecode; + return tdb_null; +}