]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/hash.c
tdb2: unify tdb1_parse_record into tdb_parse_record
[ccan] / ccan / tdb2 / hash.c
index 1359cfecd66dc6280e3314e78fd2e7e5ea91c8bd..5eee41a88df0dcaf820ab00cc3cb01df87c4cdf4 100644 (file)
    License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 #include "private.h"
+#include <ccan/hash/hash.h>
 #include <assert.h>
 
+/* 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);
@@ -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);