]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/hash.c
tdb2: add TDB_ATTRIBUTE_TDB1_HASHSIZE
[ccan] / ccan / tdb2 / hash.c
index 5b44678e14421aa878080cfbbda07b9f3327e527..56c5086e742552480a23e81a8e2a7baafe583d73 100644 (file)
    License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 #include "private.h"
-#include <assert.h>
 #include <ccan/hash/hash.h>
+#include <assert.h>
 
-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)
@@ -48,7 +43,6 @@ uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off)
 
        r = tdb_access_read(tdb, off, sizeof(*r), true);
        if (TDB_PTR_IS_ERR(r)) {
-               tdb->ecode = TDB_PTR_ERR(r);
                /* FIXME */
                return 0;
        }
@@ -58,7 +52,6 @@ uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off)
 
        key = tdb_access_read(tdb, off + sizeof(*r), klen, false);
        if (TDB_PTR_IS_ERR(key)) {
-               tdb->ecode = TDB_PTR_ERR(key);
                return 0;
        }
 
@@ -91,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;
        }
 
@@ -102,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;
 }
@@ -117,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;
        }
 
@@ -128,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;
        }
 
@@ -139,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;
        }
 
@@ -511,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;
        }
@@ -857,22 +850,17 @@ static enum TDB_ERROR chainlock(struct tdb_context *tdb, const TDB_DATA *key,
 
 /* lock/unlock one hash chain. This is meant to be used to reduce
    contention - it cannot guarantee how many records will be locked */
-int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
+enum TDB_ERROR tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
 {
-       tdb->ecode = chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT,
-                              "tdb_chainlock");
-       if (tdb->ecode == TDB_SUCCESS)
-               return 0;
-       return -1;
-       
+       return tdb->last_error = chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT,
+                                          "tdb_chainlock");
 }
 
-int 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;
-       enum TDB_ERROR ecode;
 
        gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
        group = bits_from(h, 64 - gbits, gbits);
@@ -880,10 +868,26 @@ int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
        lockstart = hlock_range(group, &locksize);
 
        tdb_trace_1rec(tdb, "tdb_chainunlock", key);
-       ecode = tdb_unlock_hashes(tdb, lockstart, locksize, F_WRLCK);
-       if (ecode != TDB_SUCCESS) {
-               tdb->ecode = ecode;
-               return -1;
-       }
-       return 0;
+       tdb_unlock_hashes(tdb, lockstart, locksize, F_WRLCK);
+}
+
+enum TDB_ERROR tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
+{
+       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;
+
+       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);
 }