]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/hash.c
tdb2: rename internal hashfn and logfn to hash_fn and log_fn.
[ccan] / ccan / tdb2 / hash.c
index 6340267c56832f1cc0698b8c17a57be07b7c7526..c0b34f8a42ab576da4981cbf6b224c69f541bc35 100644 (file)
@@ -31,13 +31,12 @@ static uint64_t jenkins_hash(const void *key, size_t length, uint64_t seed,
 
 void tdb_hash_init(struct tdb_context *tdb)
 {
-       tdb->khash = jenkins_hash;
-       tdb->hash_priv = NULL;
+       tdb->hash_fn = jenkins_hash;
 }
 
 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 +47,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 +56,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;
        }
 
@@ -448,8 +445,8 @@ static enum TDB_ERROR COLD add_to_chain(struct tdb_context *tdb,
                if (!next) {
                        next = alloc(tdb, 0, sizeof(struct tdb_chain), 0,
                                     TDB_CHAIN_MAGIC, false);
-                       if (next == TDB_OFF_ERR)
-                               return tdb->ecode;
+                       if (TDB_OFF_IS_ERR(next))
+                               return next;
                        ecode = zero_out(tdb,
                                         next+sizeof(struct tdb_used_record),
                                         sizeof(struct tdb_chain));
@@ -521,8 +518,8 @@ static enum TDB_ERROR expand_group(struct tdb_context *tdb, struct hash_info *h)
        }
 
        subhash = alloc(tdb, 0, subsize, 0, magic, false);
-       if (subhash == TDB_OFF_ERR) {
-               return tdb->ecode;
+       if (TDB_OFF_IS_ERR(subhash)) {
+               return subhash;
        }
 
        ecode = zero_out(tdb, subhash + sizeof(struct tdb_used_record),
@@ -857,22 +854,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 +872,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);
 }