X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Ftraverse.c;h=f8a2504dcd61489eb23bf7b84c8e0a88ced07f43;hp=d30c24cdde5cc837dd849a4eec2662dc2ef75dee;hb=dc9da1e34fe6a9d113fd57e116ebbc6d5bd54819;hpb=ee6f11b307599200a574208372bc962eff81a9b1 diff --git a/ccan/tdb2/traverse.c b/ccan/tdb2/traverse.c index d30c24cd..f8a2504d 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,108 +18,82 @@ #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, + int (*fn)(struct tdb_context *, + TDB_DATA, TDB_DATA, void *), + void *p) { - uint64_t i, num, count = 0; - tdb_off_t off, prev_bucket; - struct tdb_used_record rec; + enum TDB_ERROR ecode; + struct traverse_info tinfo; struct tdb_data k, d; - bool finish = false; - - /* FIXME: Do we need to start at 0? */ - prev_bucket = tdb_lock_list(tdb, 0, ltype, TDB_LOCK_WAIT); - if (prev_bucket != 0) - return -1; - - num = (1ULL << tdb->header.v.hash_bits); + int64_t count = 0; - for (i = tdb_find_nonzero_off(tdb, hash_off(tdb, 0), num); - i != num && !finish; - i += tdb_find_nonzero_off(tdb, hash_off(tdb, i), num - i)) { - if (tdb_lock_list(tdb, i, ltype, TDB_LOCK_WAIT) != i) - goto fail; - - off = tdb_read_off(tdb, hash_off(tdb, i)); - if (off == TDB_OFF_ERR) { - tdb_unlock_list(tdb, i, ltype); - goto fail; - } - - /* This race can happen, but look again. */ - if (off == 0) { - tdb_unlock_list(tdb, i, ltype); - continue; - } - - /* Drop previous lock. */ - tdb_unlock_list(tdb, prev_bucket, ltype); - prev_bucket = i; - - if (tdb_read_convert(tdb, off, &rec, sizeof(rec)) != 0) - goto fail; - - k.dsize = rec_key_length(&rec); - d.dsize = rec_data_length(&rec); - if (ltype == F_RDLCK) { - /* Read traverses can keep the lock. */ - k.dptr = (void *)tdb_access_read(tdb, - off + sizeof(rec), - k.dsize + d.dsize, - false); - } else { - k.dptr = tdb_alloc_read(tdb, off + sizeof(rec), - k.dsize + d.dsize); - } - if (!k.dptr) - goto fail; + 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 (ltype == F_WRLCK) { - /* Drop lock before calling out. */ - tdb_unlock_list(tdb, i, ltype); - } - - if (fn && fn(tdb, k, d, p)) - finish = true; - - if (ltype == F_WRLCK) { + if (fn && fn(tdb, k, d, p)) { free(k.dptr); - /* Regain lock. FIXME: Is this necessary? */ - if (tdb_lock_list(tdb, i, ltype, TDB_LOCK_WAIT) != i) - return -1; - - /* This makes deleting under ourselves a bit nicer. */ - if (tdb_read_off(tdb, hash_off(tdb, i)) == off) - i++; - } else { - tdb_access_release(tdb, k.dptr); - i++; + tdb->last_error = TDB_SUCCESS; + return count; } + free(k.dptr); } - /* Drop final lock. */ - tdb_unlock_list(tdb, prev_bucket, ltype); + if (ecode != TDB_ERR_NOEXIST) { + return tdb->last_error = ecode; + } + tdb->last_error = TDB_SUCCESS; return count; +} + +enum TDB_ERROR tdb_firstkey(struct tdb_context *tdb, struct tdb_data *key) +{ + struct traverse_info tinfo; -fail: - tdb_unlock_list(tdb, prev_bucket, ltype); - return -1; + return tdb->last_error = first_in_hash(tdb, &tinfo, key, NULL); } -int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p) +/* We lock twice, not very efficient. We could keep last key & tinfo cached. */ +enum TDB_ERROR tdb_nextkey(struct tdb_context *tdb, struct tdb_data *key) { - return traverse(tdb, F_WRLCK, fn, p); + struct traverse_info tinfo; + struct hash_info h; + struct tdb_used_record rec; + + tinfo.prev = find_and_lock(tdb, *key, F_RDLCK, &h, &rec, &tinfo); + free(key->dptr); + if (TDB_OFF_IS_ERR(tinfo.prev)) { + return tdb->last_error = tinfo.prev; + } + tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK); + + return tdb->last_error = next_in_hash(tdb, &tinfo, key, NULL); } - -int64_t tdb_traverse_read(struct tdb_context *tdb, - tdb_traverse_func fn, void *p) + +static int wipe_one(struct tdb_context *tdb, + TDB_DATA key, TDB_DATA data, enum TDB_ERROR *ecode) +{ + *ecode = tdb_delete(tdb, key); + return (*ecode != TDB_SUCCESS); +} + +enum TDB_ERROR tdb_wipe_all(struct tdb_context *tdb) { - 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; + enum TDB_ERROR ecode; + int64_t count; + + ecode = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false); + if (ecode != TDB_SUCCESS) + return tdb->last_error = ecode; + + /* FIXME: Be smarter. */ + count = tdb_traverse(tdb, wipe_one, &ecode); + if (count < 0) + ecode = count; + tdb_allrecord_unlock(tdb, F_WRLCK); + return tdb->last_error = ecode; }