X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Fhash.c;h=619d56f8116c49cd954ad0f8864412a5b93ab16e;hp=3db1ac2ebc7c85b9b50006138953ba38aafc7584;hb=614259f13c3e694fcd6b57fc05a329066e43c76d;hpb=f6067e4cbd7b7415571f12438aec00faec5657fb diff --git a/ccan/tdb2/hash.c b/ccan/tdb2/hash.c index 3db1ac2e..619d56f8 100644 --- a/ccan/tdb2/hash.c +++ b/ccan/tdb2/hash.c @@ -16,11 +16,12 @@ License along with this library; if not, see . */ #include "private.h" -#include #include +#include -static uint64_t jenkins_hash(const void *key, size_t length, uint64_t seed, - void *arg) +/* Default hash function. */ +uint64_t tdb_jenkins_hash(const void *key, size_t length, uint64_t seed, + void *unused) { uint64_t ret; /* hash64_stable assumes lower bits are more important; they are a @@ -29,15 +30,9 @@ static uint64_t jenkins_hash(const void *key, size_t length, uint64_t seed, return (ret >> 32) | (ret << 32); } -void tdb_hash_init(struct tdb_context *tdb) -{ - tdb->khash = jenkins_hash; - tdb->hash_priv = NULL; -} - uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len) { - return tdb->khash(ptr, len, tdb->hash_seed, tdb->hash_priv); + return tdb->hash_fn(ptr, len, tdb->hash_seed, tdb->hash_data); } uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off) @@ -89,18 +84,18 @@ static tdb_bool_err key_matches(struct tdb_context *tdb, const char *rkey; if (rec_key_length(rec) != key->dsize) { - add_stat(tdb, compare_wrong_keylen, 1); + tdb->stats.compare_wrong_keylen++; return ret; } rkey = tdb_access_read(tdb, off + sizeof(*rec), key->dsize, false); if (TDB_PTR_IS_ERR(rkey)) { - return TDB_PTR_ERR(rkey); + return (tdb_bool_err)TDB_PTR_ERR(rkey); } if (memcmp(rkey, key->dptr, key->dsize) == 0) ret = true; else - add_stat(tdb, compare_wrong_keycmp, 1); + tdb->stats.compare_wrong_keycmp++; tdb_access_release(tdb, rkey); return ret; } @@ -115,10 +110,10 @@ static tdb_bool_err match(struct tdb_context *tdb, tdb_off_t off; enum TDB_ERROR ecode; - add_stat(tdb, compares, 1); + tdb->stats.compares++; /* Desired bucket must match. */ if (h->home_bucket != (val & TDB_OFF_HASH_GROUP_MASK)) { - add_stat(tdb, compare_wrong_bucket, 1); + tdb->stats.compare_wrong_bucket++; return false; } @@ -126,18 +121,18 @@ static tdb_bool_err match(struct tdb_context *tdb, if (bits_from(val, TDB_OFF_HASH_EXTRA_BIT, TDB_OFF_UPPER_STEAL_EXTRA) != bits_from(h->h, 64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA, TDB_OFF_UPPER_STEAL_EXTRA)) { - add_stat(tdb, compare_wrong_offsetbits, 1); + tdb->stats.compare_wrong_offsetbits++; return false; } off = val & TDB_OFF_MASK; ecode = tdb_read_convert(tdb, off, rec, sizeof(*rec)); if (ecode != TDB_SUCCESS) { - return ecode; + return (tdb_bool_err)ecode; } if ((h->h & ((1 << 11)-1)) != rec_hash(rec)) { - add_stat(tdb, compare_wrong_rechash, 1); + tdb->stats.compare_wrong_rechash++; return false; } @@ -181,7 +176,7 @@ static tdb_off_t COLD find_in_chain(struct tdb_context *tdb, h->group_start = off; ecode = tdb_read_convert(tdb, off, h->group, sizeof(h->group)); if (ecode != TDB_SUCCESS) { - return ecode; + return TDB_ERR_TO_OFF(ecode); } for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) { @@ -198,14 +193,15 @@ static tdb_off_t COLD find_in_chain(struct tdb_context *tdb, ecode = tdb_read_convert(tdb, recoff, rec, sizeof(*rec)); if (ecode != TDB_SUCCESS) { - return ecode; + return TDB_ERR_TO_OFF(ecode); } - ecode = key_matches(tdb, rec, recoff, &key); + ecode = TDB_OFF_TO_ERR(key_matches(tdb, rec, recoff, + &key)); if (ecode < 0) { - return ecode; + return TDB_ERR_TO_OFF(ecode); } - if (ecode == 1) { + if (ecode == (enum TDB_ERROR)1) { h->home_bucket = h->found_bucket = i; if (tinfo) { @@ -257,7 +253,7 @@ tdb_off_t find_and_lock(struct tdb_context *tdb, ecode = tdb_lock_hashes(tdb, h->hlock_start, h->hlock_range, ltype, TDB_LOCK_WAIT); if (ecode != TDB_SUCCESS) { - return ecode; + return TDB_ERR_TO_OFF(ecode); } hashtable = offsetof(struct tdb_header, hashtable); @@ -320,7 +316,7 @@ tdb_off_t find_and_lock(struct tdb_context *tdb, berr = match(tdb, h, &key, h->group[h->found_bucket], rec); if (berr < 0) { - ecode = berr; + ecode = TDB_OFF_TO_ERR(berr); goto fail; } if (berr) { @@ -339,7 +335,7 @@ tdb_off_t find_and_lock(struct tdb_context *tdb, fail: tdb_unlock_hashes(tdb, h->hlock_start, h->hlock_range, ltype); - return ecode; + return TDB_ERR_TO_OFF(ecode); } /* I wrote a simple test, expanding a hash to 2GB, for the following @@ -431,7 +427,7 @@ static enum TDB_ERROR COLD add_to_chain(struct tdb_context *tdb, entry = tdb_find_zero_off(tdb, subhash, 1<group, h->home_bucket); if (h->hash_used == 64) { - add_stat(tdb, alloc_chain, 1); + tdb->stats.alloc_chain++; subsize = sizeof(struct tdb_chain); magic = TDB_CHAIN_MAGIC; } else { - add_stat(tdb, alloc_subhash, 1); + tdb->stats.alloc_subhash++; subsize = (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS); magic = TDB_HTABLE_MAGIC; } subhash = alloc(tdb, 0, subsize, 0, magic, false); if (TDB_OFF_IS_ERR(subhash)) { - return subhash; + return TDB_OFF_TO_ERR(subhash); } ecode = zero_out(tdb, subhash + sizeof(struct tdb_used_record), @@ -765,7 +761,7 @@ enum TDB_ERROR next_in_hash(struct tdb_context *tdb, struct tdb_used_record rec; if (TDB_OFF_IS_ERR(off)) { - ecode = off; + ecode = TDB_OFF_TO_ERR(off); goto fail; } @@ -857,22 +853,61 @@ static enum TDB_ERROR chainlock(struct tdb_context *tdb, const TDB_DATA *key, contention - it cannot guarantee how many records will be locked */ enum TDB_ERROR tdb_chainlock(struct tdb_context *tdb, TDB_DATA key) { + if (tdb->flags & TDB_VERSION1) { + if (tdb1_chainlock(tdb, key) == -1) + return tdb->last_error; + return TDB_SUCCESS; + } return tdb->last_error = chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT, "tdb_chainlock"); } -enum TDB_ERROR tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key) +void tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key) { uint64_t h = tdb_hash(tdb, key.dptr, key.dsize); tdb_off_t lockstart, locksize; unsigned int group, gbits; + if (tdb->flags & TDB_VERSION1) { + tdb1_chainunlock(tdb, key); + return; + } + gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS; group = bits_from(h, 64 - gbits, gbits); lockstart = hlock_range(group, &locksize); tdb_trace_1rec(tdb, "tdb_chainunlock", key); - return tdb->last_error = tdb_unlock_hashes(tdb, lockstart, locksize, - F_WRLCK); + tdb_unlock_hashes(tdb, lockstart, locksize, F_WRLCK); +} + +enum TDB_ERROR tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key) +{ + if (tdb->flags & TDB_VERSION1) { + if (tdb1_chainlock_read(tdb, key) == -1) + return tdb->last_error; + return TDB_SUCCESS; + } + return tdb->last_error = chainlock(tdb, &key, F_RDLCK, TDB_LOCK_WAIT, + "tdb_chainlock_read"); +} + +void tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key) +{ + uint64_t h = tdb_hash(tdb, key.dptr, key.dsize); + tdb_off_t lockstart, locksize; + unsigned int group, gbits; + + if (tdb->flags & TDB_VERSION1) { + tdb1_chainunlock_read(tdb, key); + return; + } + gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS; + group = bits_from(h, 64 - gbits, gbits); + + lockstart = hlock_range(group, &locksize); + + tdb_trace_1rec(tdb, "tdb_chainunlock_read", key); + tdb_unlock_hashes(tdb, lockstart, locksize, F_RDLCK); }