Commit d117f992 authored by Rusty Russell's avatar Rusty Russell

tdb: add Bob Jenkins lookup3 hash as helper hash.

This is a better hash than the default: shipping it with tdb makes it easy
for callers to use it as the hash by passing it to tdb_open_ex().
parent 9cbb97f3
......@@ -73,7 +73,6 @@ int main(int argc, char *argv[])
return 1;
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/hash\n");
return 0;
}
......
This diff is collapsed.
......@@ -30,20 +30,6 @@
/* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
static struct tdb_context *tdbs = NULL;
/* This is based on the hash algorithm from gdbm */
static unsigned int default_tdb_hash(TDB_DATA *key)
{
uint32_t value; /* Used to compute the hash value. */
uint32_t i; /* Used to cycle through random values. */
/* 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)));
return (1103515243 * value + 12345);
}
/* We use two hashes to double-check they're using the right hash function. */
void tdb_header_hash(struct tdb_context *tdb,
uint32_t *magic1_hash, uint32_t *magic2_hash)
......@@ -206,7 +192,7 @@ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
tdb->hash_fn = hash_fn;
hash_alg = "user defined";
} else {
tdb->hash_fn = default_tdb_hash;
tdb->hash_fn = tdb_old_hash;
hash_alg = "default";
}
......
......@@ -149,6 +149,7 @@ void tdb_add_flags(struct tdb_context *tdb, unsigned flag);
void tdb_remove_flags(struct tdb_context *tdb, unsigned flag);
void tdb_enable_seqnum(struct tdb_context *tdb);
void tdb_increment_seqnum_nonblock(struct tdb_context *tdb);
unsigned int tdb_jenkins_hash(TDB_DATA *key);
int tdb_check(struct tdb_context *tdb,
int (*check)(TDB_DATA key, TDB_DATA data, void *private),
void *private);
......
......@@ -302,5 +302,5 @@ int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off,
struct tdb_record *rec);
void tdb_header_hash(struct tdb_context *tdb,
uint32_t *magic1_hash, uint32_t *magic2_hash);
unsigned int tdb_old_hash(TDB_DATA *key);
#endif
......@@ -11,6 +11,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <err.h>
......
......@@ -9,6 +9,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <err.h>
......
......@@ -9,6 +9,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <err.h>
......
......@@ -9,6 +9,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <err.h>
......
......@@ -20,6 +20,7 @@ static int ftruncate_check(int fd, off_t length);
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <stdbool.h>
......
......@@ -9,6 +9,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <err.h>
......
......@@ -9,6 +9,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <stdbool.h>
......
......@@ -11,6 +11,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#undef fcntl
#include <stdlib.h>
......
......@@ -14,6 +14,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <err.h>
......
......@@ -9,17 +9,12 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/hash/hash.h>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <err.h>
#include "logging.h"
static unsigned int jenkins_hash(TDB_DATA *key)
{
return hash_stable(key->dptr, key->dsize, 0);
}
int main(int argc, char *argv[])
{
struct tdb_context *tdb;
......@@ -41,13 +36,13 @@ int main(int argc, char *argv[])
tdb_close(tdb);
tdb = tdb_open_ex("test/old-nohash-le.tdb", 0, 0, O_RDWR, 0,
&taplogctx, jenkins_hash);
&taplogctx, tdb_jenkins_hash);
ok1(tdb);
ok1(tdb_check(tdb, NULL, NULL) == 0);
tdb_close(tdb);
tdb = tdb_open_ex("test/old-nohash-be.tdb", 0, 0, O_RDWR, 0,
&taplogctx, jenkins_hash);
&taplogctx, tdb_jenkins_hash);
ok1(tdb);
ok1(tdb_check(tdb, NULL, NULL) == 0);
tdb_close(tdb);
......
......@@ -21,6 +21,7 @@ static int ftruncate_check(int fd, off_t length);
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <stdbool.h>
......
......@@ -11,6 +11,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <err.h>
......
......@@ -9,7 +9,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/hash/hash.h>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <err.h>
......
......@@ -11,6 +11,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#undef fcntl_with_lockcheck
#include <stdlib.h>
......
......@@ -9,16 +9,11 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/hash/hash.h>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <err.h>
static unsigned int jenkins_hash(TDB_DATA *key)
{
return hash_stable(key->dptr, key->dsize, 0);
}
static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
{
unsigned int *count = tdb_get_logging_private(tdb);
......@@ -44,7 +39,7 @@ int main(int argc, char *argv[])
/* Fail to open with different hash. */
tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
&log_ctx, jenkins_hash);
&log_ctx, tdb_jenkins_hash);
ok1(!tdb);
ok1(log_count == 1);
......@@ -52,7 +47,7 @@ int main(int argc, char *argv[])
log_count = 0;
tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0,
O_CREAT|O_RDWR|O_TRUNC,
0600, &log_ctx, jenkins_hash);
0600, &log_ctx, tdb_jenkins_hash);
ok1(tdb);
ok1(log_count == 0);
tdb_close(tdb);
......@@ -71,7 +66,7 @@ int main(int argc, char *argv[])
ok1(log_count == 1);
log_count = 0;
/* Fail to open with defailt hash. */
/* Fail to open with default hash. */
tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
&log_ctx, NULL);
ok1(!tdb);
......@@ -79,7 +74,7 @@ int main(int argc, char *argv[])
log_count = 0;
tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY,
0, &log_ctx, jenkins_hash);
0, &log_ctx, tdb_jenkins_hash);
ok1(tdb);
ok1(log_count == 0);
ok1(tdb_check(tdb, NULL, NULL) == 0);
......@@ -87,7 +82,7 @@ int main(int argc, char *argv[])
log_count = 0;
tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY,
0, &log_ctx, jenkins_hash);
0, &log_ctx, tdb_jenkins_hash);
ok1(tdb);
ok1(log_count == 0);
ok1(tdb_check(tdb, NULL, NULL) == 0);
......
......@@ -9,6 +9,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <err.h>
......
......@@ -9,6 +9,7 @@
#include <ccan/tdb/error.c>
#include <ccan/tdb/open.c>
#include <ccan/tdb/check.c>
#include <ccan/tdb/hash.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <err.h>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment