X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Fhash.c;h=5eee41a88df0dcaf820ab00cc3cb01df87c4cdf4;hp=745f04c87e5574f50719b3018b4a78f7167fbbd6;hb=3352e4e947777d4a90a2dd4f3037e1e494231b25;hpb=703cea0c78a896c658272af06306218fc0bb23cc diff --git a/ccan/tdb2/hash.c b/ccan/tdb2/hash.c index 745f04c8..5eee41a8 100644 --- a/ccan/tdb2/hash.c +++ b/ccan/tdb2/hash.c @@ -16,8 +16,20 @@ License along with this library; if not, see . */ #include "private.h" +#include #include +/* 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 + * slightly better hash. We use the upper bits first, so swap them. */ + ret = hash64_stable((const unsigned char *)key, length, seed); + return (ret >> 32) | (ret << 32); +} + uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len) { return tdb->hash_fn(ptr, len, tdb->hash_seed, tdb->hash_data); @@ -72,7 +84,7 @@ 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; } @@ -83,7 +95,7 @@ static tdb_bool_err key_matches(struct tdb_context *tdb, 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; } @@ -98,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; } @@ -109,7 +121,7 @@ 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; } @@ -120,7 +132,7 @@ static tdb_bool_err match(struct tdb_context *tdb, } if ((h->h & ((1 << 11)-1)) != rec_hash(rec)) { - add_stat(tdb, compare_wrong_rechash, 1); + tdb->stats.compare_wrong_rechash++; return false; } @@ -492,11 +504,11 @@ static enum TDB_ERROR expand_group(struct tdb_context *tdb, struct hash_info *h) bucket = fullest_bucket(tdb, h->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; } @@ -840,6 +852,11 @@ 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"); } @@ -850,6 +867,11 @@ void tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key) 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); @@ -861,6 +883,11 @@ void tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key) 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"); } @@ -871,6 +898,10 @@ void tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key) 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);