/* Our statistics. */
struct tdb_attribute_stats stats;
- /* Are we accessing directly? (debugging check). */
- int direct_access;
-
/* Hash function. */
uint64_t (*hash_fn)(const void *key, size_t len, uint64_t seed, void *);
void *hash_data;
uint64_t hash_seed;
+ /* Are we accessing directly? (debugging check). */
+ int direct_access;
+
/* Set if we are in a transaction. */
struct tdb_transaction *transaction;
typedef int (*tdb1_traverse_func)(struct tdb1_context *, TDB_DATA, TDB_DATA, void *);
typedef void (*tdb1_log_func)(struct tdb1_context *, enum tdb_log_level, enum TDB_ERROR,
const char *, void *);
-typedef unsigned int (*tdb1_hash_func)(TDB_DATA *key);
+typedef uint64_t (*tdb1_hash_func)(const void *key, size_t len, uint64_t seed,
+ void *data);
struct tdb1_logging_context {
tdb1_log_func log_fn;
void tdb1_increment_seqnum_nonblock(struct tdb1_context *tdb);
-unsigned int tdb1_incompatible_hash(TDB_DATA *key);
+uint64_t tdb1_incompatible_hash(const void *key, size_t len, uint64_t seed, void *);
int tdb1_check(struct tdb1_context *tdb,
int (*check) (TDB_DATA key, TDB_DATA data, void *private_data),
if (!key.dptr)
return false;
- if (tdb->hash_fn(&key) != rec->full_hash) {
+ if ((uint32_t)tdb_hash(tdb, key.dptr, key.dsize) != rec->full_hash) {
tdb->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
"Record offset %d has incorrect hash\n", off);
goto fail_put_key;
#include "tdb1_private.h"
/* This is based on the hash algorithm from gdbm */
-unsigned int tdb1_old_hash(TDB_DATA *key)
+uint64_t tdb1_old_hash(const void *key, size_t len, uint64_t seed, void *unused)
{
uint32_t value; /* Used to compute the hash value. */
uint32_t i; /* Used to cycle through random values. */
+ const unsigned char *dptr = key;
/* Set the initial value from the key size. */
- for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
- value = (value + (key->dptr[i] << (i*5 % 24)));
+ for (value = 0x238F13AF * len, i=0; i < len; i++)
+ value = (value + (dptr[i] << (i*5 % 24)));
return (1103515243 * value + 12345);
}
return c;
}
-unsigned int tdb1_incompatible_hash(TDB_DATA *key)
+uint64_t tdb1_incompatible_hash(const void *key, size_t len, uint64_t seed,
+ void *unused)
{
- return hashlittle(key->dptr, key->dsize);
+ return hashlittle(key, len);
}
contention - it cannot guarantee how many records will be locked */
int tdb1_chainlock(struct tdb1_context *tdb, TDB_DATA key)
{
- int ret = tdb1_lock(tdb, TDB1_BUCKET(tdb->hash_fn(&key)), F_WRLCK);
+ int ret = tdb1_lock(tdb,
+ TDB1_BUCKET(tdb_hash(tdb, key.dptr, key.dsize)),
+ F_WRLCK);
return ret;
}
int tdb1_chainunlock(struct tdb1_context *tdb, TDB_DATA key)
{
- return tdb1_unlock(tdb, TDB1_BUCKET(tdb->hash_fn(&key)), F_WRLCK);
+ return tdb1_unlock(tdb, TDB1_BUCKET(tdb_hash(tdb, key.dptr, key.dsize)),
+ F_WRLCK);
}
int tdb1_chainlock_read(struct tdb1_context *tdb, TDB_DATA key)
{
int ret;
- ret = tdb1_lock(tdb, TDB1_BUCKET(tdb->hash_fn(&key)), F_RDLCK);
+ ret = tdb1_lock(tdb, TDB1_BUCKET(tdb_hash(tdb, key.dptr, key.dsize)),
+ F_RDLCK);
return ret;
}
int tdb1_chainunlock_read(struct tdb1_context *tdb, TDB_DATA key)
{
- return tdb1_unlock(tdb, TDB1_BUCKET(tdb->hash_fn(&key)), F_RDLCK);
+ return tdb1_unlock(tdb, TDB1_BUCKET(tdb_hash(tdb, key.dptr, key.dsize)),
+ F_RDLCK);
}
/* record lock stops delete underneath */
void tdb1_header_hash(struct tdb1_context *tdb,
uint32_t *magic1_hash, uint32_t *magic2_hash)
{
- TDB_DATA hash_key;
uint32_t tdb1_magic = TDB1_MAGIC;
- hash_key.dptr = (unsigned char *)TDB_MAGIC_FOOD;
- hash_key.dsize = sizeof(TDB_MAGIC_FOOD);
- *magic1_hash = tdb->hash_fn(&hash_key);
-
- hash_key.dptr = (unsigned char *)TDB1_CONV(tdb1_magic);
- hash_key.dsize = sizeof(tdb1_magic);
- *magic2_hash = tdb->hash_fn(&hash_key);
+ *magic1_hash = tdb_hash(tdb, TDB_MAGIC_FOOD, sizeof(TDB_MAGIC_FOOD));
+ *magic2_hash = tdb_hash(tdb, TDB1_CONV(tdb1_magic), sizeof(tdb1_magic));
/* Make sure at least one hash is non-zero! */
if (*magic1_hash == 0 && *magic2_hash == 0)
errno = ENOMEM;
goto fail;
}
+ tdb->hash_seed = 0;
if (hash_fn) {
tdb->hash_fn = hash_fn;
#define tdb_allrecord_upgrade(tdb1, start) \
tdb_allrecord_upgrade((struct tdb_context *)(tdb1), (start))
+#define tdb_hash(tdb1, ptr, len) \
+ tdb_hash((struct tdb_context *)(tdb1), (ptr), (len))
+
#define tdb_lock_gradual(tdb1, ltype, flags, off, len) \
tdb_lock_gradual((struct tdb_context *)(tdb1), \
(ltype), (flags), (off), (len))
/* Our statistics. */
struct tdb_attribute_stats stats;
+ /* Hash function. */
+ uint64_t (*hash_fn)(const void *key, size_t len, uint64_t seed, void *);
+ void *hash_data;
+ uint64_t hash_seed;
+
bool read_only; /* opened read-only */
int traverse_read; /* read-only traversal */
int traverse_write; /* read-write traversal */
struct tdb1_header header; /* a cached copy of the header */
struct tdb1_traverse_lock travlocks; /* current traversal locks */
- unsigned int (*hash_fn)(TDB_DATA *key);
const struct tdb1_methods *methods;
struct tdb1_transaction *transaction;
int page_size;
int tdb1_transaction_recover(struct tdb1_context *tdb);
void tdb1_header_hash(struct tdb1_context *tdb,
uint32_t *magic1_hash, uint32_t *magic2_hash);
-unsigned int tdb1_old_hash(TDB_DATA *key);
+uint64_t tdb1_old_hash(const void *key, size_t len, uint64_t seed, void *);
size_t tdb1_dead_space(struct tdb1_context *tdb, tdb1_off_t off);
#endif /* CCAN_TDB2_TDB1_PRIVATE_H */
uint32_t hash;
/* find which hash bucket it is in */
- hash = tdb->hash_fn(&key);
+ hash = tdb_hash(tdb, key.dptr, key.dsize);
if (!(rec_ptr = tdb1_find_lock_hash(tdb,key,hash,F_RDLCK,&rec)))
return tdb1_null;
uint32_t hash;
/* find which hash bucket it is in */
- hash = tdb->hash_fn(&key);
+ hash = tdb_hash(tdb, key.dptr, key.dsize);
if (!(rec_ptr = tdb1_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
/* record not found */
int tdb1_exists(struct tdb1_context *tdb, TDB_DATA key)
{
- uint32_t hash = tdb->hash_fn(&key);
+ uint32_t hash = tdb_hash(tdb, key.dptr, key.dsize);
int ret;
ret = tdb1_exists_hash(tdb, key, hash);
int tdb1_delete(struct tdb1_context *tdb, TDB_DATA key)
{
- uint32_t hash = tdb->hash_fn(&key);
+ uint32_t hash = tdb_hash(tdb, key.dptr, key.dsize);
int ret;
ret = tdb1_delete_hash(tdb, key, hash);
}
/* find which hash bucket it is in */
- hash = tdb->hash_fn(&key);
+ hash = tdb_hash(tdb, key.dptr, key.dsize);
if (tdb1_lock(tdb, TDB1_BUCKET(hash), F_WRLCK) == -1)
return -1;
int ret = -1;
/* find which hash bucket it is in */
- hash = tdb->hash_fn(&key);
+ hash = tdb_hash(tdb, key.dptr, key.dsize);
if (tdb1_lock(tdb, TDB1_BUCKET(hash), F_WRLCK) == -1)
return -1;
if (!tdb->travlocks.off) {
/* No previous element: do normal find, and lock record */
- tdb->travlocks.off = tdb1_find_lock_hash(tdb, oldkey, tdb->hash_fn(&oldkey), tdb->travlocks.lock_rw, &rec);
+ tdb->travlocks.off = tdb1_find_lock_hash(tdb, oldkey, tdb_hash(tdb, oldkey.dptr, oldkey.dsize), tdb->travlocks.lock_rw, &rec);
if (!tdb->travlocks.off) {
return tdb1_null;
}
free(data.dptr);
/* That currently fills at the end, make sure that's true. */
- hash = tdb->hash_fn(&key);
+ hash = tdb_hash(tdb, key.dptr, key.dsize);
rec_ptr = tdb1_find_lock_hash(tdb, key, hash, F_RDLCK, &rec);
ok1(rec_ptr);
ok1(rec_ptr > 2U*1024*1024*1024);
#include <stdlib.h>
#include <err.h>
-static unsigned int tdb1_dumb_hash(TDB_DATA *key)
+static uint64_t tdb1_dumb_hash(const void *key, size_t len, uint64_t seed,
+ void *unused)
{
- return key->dsize;
+ return len;
}
static void log_fn(struct tdb1_context *tdb, enum tdb_log_level level,
return hdr.rwlocks;
}
-static unsigned int jenkins_hashfn(TDB_DATA *key)
+static uint64_t jenkins_hashfn(const void *key, size_t len, uint64_t seed,
+ void *unused)
{
- return hashlittle(key->dptr, key->dsize);
+ return hashlittle(key, len);
}
-static unsigned int old_hash(TDB_DATA *key)
+static uint64_t old_hash(const void *key, size_t len, uint64_t seed,
+ void *unused)
{
- return tdb1_old_hash(key);
+ return tdb1_old_hash(key, len, seed, unused);
}
int main(int argc, char *argv[])
(*count)++;
}
-static unsigned int jenkins_hashfn(TDB_DATA *key)
+static uint64_t jenkins_hashfn(const void *key, size_t len, uint64_t seed,
+ void *unused)
{
- return hashlittle(key->dptr, key->dsize);
+ return hashlittle(key, len);
}
/* the tdb1_old_hash function is "magic" as it automatically makes us test the
* tdb1_incompatible_hash as well, so use this wrapper. */
-static unsigned int old_hash(TDB_DATA *key)
+static uint64_t old_hash(const void *key, size_t len, uint64_t seed,
+ void *unused)
{
- return tdb1_old_hash(key);
+ return tdb1_old_hash(key, len, seed, unused);
}
int main(int argc, char *argv[])