Commit 25ed8785 authored by Rusty Russell's avatar Rusty Russell

tdb2: more fixes and tests for enlarging hash.

- Neaten I/O function
- Don't use fill in zero_out: it's only for low-level ops.
- Don't mangle arg in tdb_write_convert: it broke write_header.
- More use of tdb_access_read, make it optionally converting.
- Rename unlock_range to unlock_lists.
- Lots of fixes to enlarge_hash now it's being tested.
- More expansion cases tested.
parent dbf1ac48
...@@ -71,8 +71,8 @@ static bool check_header(struct tdb_context *tdb) ...@@ -71,8 +71,8 @@ static bool check_header(struct tdb_context *tdb)
< tdb->map_size) { < tdb->map_size) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
"check: %llu zones size %llu don't cover %llu\n", "check: %llu zones size %llu don't cover %llu\n",
(long long)(1ULL << tdb->header.v.zone_bits),
(long long)tdb->header.v.num_zones, (long long)tdb->header.v.num_zones,
(long long)(1ULL << tdb->header.v.zone_bits),
(long long)tdb->map_size); (long long)tdb->map_size);
return false; return false;
} }
......
...@@ -404,20 +404,17 @@ static tdb_off_t huge_alloc(struct tdb_context *tdb, size_t size, ...@@ -404,20 +404,17 @@ static tdb_off_t huge_alloc(struct tdb_context *tdb, size_t size,
{ {
tdb_off_t i, off; tdb_off_t i, off;
do { for (i = 0; i < tdb->header.v.num_zones; i++) {
for (i = 0; i < tdb->header.v.num_zones; i++) { /* Try getting one from list. */
/* Try getting one from list. */ off = lock_and_alloc(tdb, tdb->header.v.free_buckets,
off = lock_and_alloc(tdb, tdb->header.v.free_buckets, size, actual);
size, actual); if (off == TDB_OFF_ERR)
if (off == TDB_OFF_ERR) return TDB_OFF_ERR;
return TDB_OFF_ERR; if (off != 0)
if (off != 0) return off;
return off; /* FIXME: Coalesce! */
/* FIXME: Coalesce! */ }
} return 0;
} while (tdb_expand(tdb, 0, size, false) == 0);
return TDB_OFF_ERR;
} }
static tdb_off_t get_free(struct tdb_context *tdb, size_t size, static tdb_off_t get_free(struct tdb_context *tdb, size_t size,
...@@ -634,14 +631,15 @@ int tdb_expand(struct tdb_context *tdb, tdb_len_t klen, tdb_len_t dlen, ...@@ -634,14 +631,15 @@ int tdb_expand(struct tdb_context *tdb, tdb_len_t klen, tdb_len_t dlen,
/* Increase the zone size. */ /* Increase the zone size. */
new_num_zones = tdb->header.v.num_zones; new_num_zones = tdb->header.v.num_zones;
new_zone_bits = tdb->header.v.zone_bits+1; new_zone_bits = tdb->header.v.zone_bits+1;
while ((new_num_zones << new_zone_bits) - tdb->map_size while ((new_num_zones << new_zone_bits)
< needed) { < tdb->map_size + needed) {
new_zone_bits++; new_zone_bits++;
} }
/* We expand by enough zones to meet the need. */ /* We expand by enough full zones to meet the need. */
add = (needed + (1ULL << new_zone_bits)-1) add = ((tdb->map_size + needed + (1ULL << new_zone_bits)-1)
& ~((1ULL << new_zone_bits)-1); & ~((1ULL << new_zone_bits)-1))
- tdb->map_size;
} }
/* Updates tdb->map_size. */ /* Updates tdb->map_size. */
...@@ -667,15 +665,17 @@ int tdb_expand(struct tdb_context *tdb, tdb_len_t klen, tdb_len_t dlen, ...@@ -667,15 +665,17 @@ int tdb_expand(struct tdb_context *tdb, tdb_len_t klen, tdb_len_t dlen,
old_num_total = tdb->header.v.num_zones*(tdb->header.v.free_buckets+1); old_num_total = tdb->header.v.num_zones*(tdb->header.v.free_buckets+1);
old_free_off = tdb->header.v.free_off; old_free_off = tdb->header.v.free_off;
oldf = tdb_access_read(tdb, old_free_off, oldf = tdb_access_read(tdb, old_free_off,
old_num_total * sizeof(tdb_off_t)); old_num_total * sizeof(tdb_off_t), true);
if (!oldf) if (!oldf)
goto fail; goto fail;
/* Switch to using our new zone. */ /* Switch to using our new zone. */
if (zero_out(tdb, off, new_num_zones * (new_num_buckets + 1)) == -1) if (zero_out(tdb, off, freebucket_size) == -1)
goto fail_release; goto fail_release;
tdb->header.v.free_off = off; tdb->header.v.free_off = off;
tdb->header.v.num_zones = new_num_zones; tdb->header.v.num_zones = new_num_zones;
tdb->header.v.zone_bits = new_zone_bits;
tdb->header.v.free_buckets = new_num_buckets; tdb->header.v.free_buckets = new_num_buckets;
/* FIXME: If zone size hasn't changed, can simply copy pointers. */ /* FIXME: If zone size hasn't changed, can simply copy pointers. */
...@@ -704,7 +704,9 @@ int tdb_expand(struct tdb_context *tdb, tdb_len_t klen, tdb_len_t dlen, ...@@ -704,7 +704,9 @@ int tdb_expand(struct tdb_context *tdb, tdb_len_t klen, tdb_len_t dlen,
if (tdb_read_convert(tdb, old_free_off, &fhdr, sizeof(fhdr)) == -1) if (tdb_read_convert(tdb, old_free_off, &fhdr, sizeof(fhdr)) == -1)
goto fail_release; goto fail_release;
if (add_free_record(tdb, old_free_off, if (add_free_record(tdb, old_free_off,
rec_data_length(&fhdr)+rec_extra_padding(&fhdr))) sizeof(fhdr)
+ rec_data_length(&fhdr)
+ rec_extra_padding(&fhdr)))
goto fail_release; goto fail_release;
/* Add the rest as a new free record. */ /* Add the rest as a new free record. */
......
...@@ -132,19 +132,13 @@ void *tdb_get(struct tdb_context *tdb, tdb_off_t off, void *pad, size_t len) ...@@ -132,19 +132,13 @@ void *tdb_get(struct tdb_context *tdb, tdb_off_t off, void *pad, size_t len)
if (ret) if (ret)
return ret; return ret;
} }
return tdb_read_convert(tdb, off, pad, len) == -1 ? NULL : pad;
if (unlikely(tdb_oob(tdb, off + len, false) == -1))
return NULL;
if (tdb->methods->read(tdb, off, pad, len) == -1)
return NULL;
return tdb_convert(tdb, pad, len);
} }
/* Endian conversion: we only ever deal with 8 byte quantities */ /* Endian conversion: we only ever deal with 8 byte quantities */
void *tdb_convert(const struct tdb_context *tdb, void *buf, tdb_len_t size) void *tdb_convert(const struct tdb_context *tdb, void *buf, tdb_len_t size)
{ {
if (unlikely((tdb->flags & TDB_CONVERT))) { if (unlikely((tdb->flags & TDB_CONVERT)) && buf) {
uint64_t i, *p = (uint64_t *)buf; uint64_t i, *p = (uint64_t *)buf;
for (i = 0; i < size / 8; i++) for (i = 0; i < size / 8; i++)
p[i] = bswap_64(p[i]); p[i] = bswap_64(p[i]);
...@@ -157,23 +151,19 @@ void *tdb_convert(const struct tdb_context *tdb, void *buf, tdb_len_t size) ...@@ -157,23 +151,19 @@ void *tdb_convert(const struct tdb_context *tdb, void *buf, tdb_len_t size)
uint64_t tdb_find_nonzero_off(struct tdb_context *tdb, tdb_off_t off, uint64_t tdb_find_nonzero_off(struct tdb_context *tdb, tdb_off_t off,
uint64_t num) uint64_t num)
{ {
uint64_t i, *val; uint64_t i;
bool alloc = false; const uint64_t *val;
val = tdb_direct(tdb, off, num * sizeof(tdb_off_t)); /* Zero vs non-zero is the same unconverted: minor optimization. */
if (!unlikely(val)) { val = tdb_access_read(tdb, off, num * sizeof(tdb_off_t), false);
val = tdb_alloc_read(tdb, off, num * sizeof(tdb_off_t)); if (!val)
if (!val) return num;
return num;
alloc = true;
}
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
if (val[i]) if (val[i])
break; break;
} }
if (unlikely(alloc)) tdb_access_release(tdb, val);
free(val);
return i; return i;
} }
...@@ -181,55 +171,38 @@ uint64_t tdb_find_nonzero_off(struct tdb_context *tdb, tdb_off_t off, ...@@ -181,55 +171,38 @@ uint64_t tdb_find_nonzero_off(struct tdb_context *tdb, tdb_off_t off,
uint64_t tdb_find_zero_off(struct tdb_context *tdb, tdb_off_t off, uint64_t tdb_find_zero_off(struct tdb_context *tdb, tdb_off_t off,
uint64_t num) uint64_t num)
{ {
uint64_t i, *val; uint64_t i;
bool alloc = false; const uint64_t *val;
val = tdb_direct(tdb, off, num * sizeof(tdb_off_t)); /* Zero vs non-zero is the same unconverted: minor optimization. */
if (!unlikely(val)) { val = tdb_access_read(tdb, off, num * sizeof(tdb_off_t), false);
val = tdb_alloc_read(tdb, off, num * sizeof(tdb_off_t)); if (!val)
if (!val) return num;
return num;
alloc = true;
}
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
if (!val[i]) if (!val[i])
break; break;
} }
if (unlikely(alloc)) tdb_access_release(tdb, val);
free(val);
return i; return i;
} }
static int fill(struct tdb_context *tdb,
const void *buf, size_t size,
tdb_off_t off, tdb_len_t len)
{
while (len) {
size_t n = len > size ? size : len;
if (!tdb_pwrite_all(tdb->fd, buf, n, off)) {
tdb->ecode = TDB_ERR_IO;
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
"fill write failed: giving up!\n");
return -1;
}
len -= n;
off += n;
}
return 0;
}
int zero_out(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len) int zero_out(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len)
{ {
char buf[8192] = { 0 };
void *p = tdb_direct(tdb, off, len); void *p = tdb_direct(tdb, off, len);
if (p) { if (p) {
memset(p, 0, len); memset(p, 0, len);
return 0; return 0;
} else {
char buf[8192] = { 0 };
return fill(tdb, buf, sizeof(buf), off, len);
} }
while (len) {
unsigned todo = len < sizeof(buf) ? len : sizeof(buf);
if (tdb->methods->write(tdb, off, buf, todo) == -1)
return -1;
len -= todo;
off += todo;
}
return 0;
} }
tdb_off_t tdb_read_off(struct tdb_context *tdb, tdb_off_t off) tdb_off_t tdb_read_off(struct tdb_context *tdb, tdb_off_t off)
...@@ -357,9 +330,26 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf, ...@@ -357,9 +330,26 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
} }
int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off, int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off,
void *rec, size_t len) const void *rec, size_t len)
{ {
return tdb->methods->write(tdb, off, tdb_convert(tdb, rec, len), len); int ret;
if (unlikely((tdb->flags & TDB_CONVERT))) {
void *conv = malloc(len);
if (!conv) {
tdb->ecode = TDB_ERR_OOM;
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
"tdb_write: no memory converting %zu bytes\n",
len);
return -1;
}
memcpy(conv, rec, len);
ret = tdb->methods->write(tdb, off,
tdb_convert(tdb, conv, len), len);
free(conv);
} else
ret = tdb->methods->write(tdb, off, rec, len);
return ret;
} }
int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off, int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off,
...@@ -397,7 +387,7 @@ void *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len) ...@@ -397,7 +387,7 @@ void *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len)
uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off) uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off)
{ {
struct tdb_used_record pad, *r; struct tdb_used_record pad, *r;
void *key; const void *key;
uint64_t klen, hash; uint64_t klen, hash;
r = tdb_get(tdb, off, &pad, sizeof(pad)); r = tdb_get(tdb, off, &pad, sizeof(pad));
...@@ -406,41 +396,32 @@ uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off) ...@@ -406,41 +396,32 @@ uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off)
return 0; return 0;
klen = rec_key_length(r); klen = rec_key_length(r);
key = tdb_direct(tdb, off + sizeof(pad), klen); key = tdb_access_read(tdb, off + sizeof(pad), klen, false);
if (likely(key)) if (!key)
return tdb_hash(tdb, key, klen);
key = tdb_alloc_read(tdb, off + sizeof(pad), klen);
if (unlikely(!key))
return 0; return 0;
hash = tdb_hash(tdb, key, klen); hash = tdb_hash(tdb, key, klen);
free(key); tdb_access_release(tdb, key);
return hash; return hash;
} }
/* Give a piece of tdb data to a parser */ static int fill(struct tdb_context *tdb,
int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key, const void *buf, size_t size,
tdb_off_t offset, tdb_len_t len, tdb_off_t off, tdb_len_t len)
int (*parser)(TDB_DATA key, TDB_DATA data,
void *private_data),
void *private_data)
{ {
TDB_DATA data; while (len) {
int result; size_t n = len > size ? size : len;
bool allocated = false;
if (!tdb_pwrite_all(tdb->fd, buf, n, off)) {
data.dsize = len; tdb->ecode = TDB_ERR_IO;
data.dptr = tdb_direct(tdb, offset, len); tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
if (unlikely(!data.dptr)) { "fill write failed: giving up!\n");
if (!(data.dptr = tdb_alloc_read(tdb, offset, len))) {
return -1; return -1;
} }
allocated = true; len -= n;
off += n;
} }
result = parser(key, data, private_data); return 0;
if (unlikely(allocated))
free(data.dptr);
return result;
} }
/* expand a file. we prefer to use ftruncate, as that is what posix /* expand a file. we prefer to use ftruncate, as that is what posix
...@@ -484,12 +465,18 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition) ...@@ -484,12 +465,18 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition)
} }
const void *tdb_access_read(struct tdb_context *tdb, const void *tdb_access_read(struct tdb_context *tdb,
tdb_off_t off, tdb_len_t len) tdb_off_t off, tdb_len_t len, bool convert)
{ {
const void *ret = tdb_direct(tdb, off, len); const void *ret = NULL;
if (!ret) if (likely(!(tdb->flags & TDB_CONVERT)))
ret = tdb_direct(tdb, off, len);
if (!ret) {
ret = tdb_alloc_read(tdb, off, len); ret = tdb_alloc_read(tdb, off, len);
if (convert)
tdb_convert(tdb, (void *)ret, len);
}
return ret; return ret;
} }
......
...@@ -296,20 +296,13 @@ void *tdb_convert(const struct tdb_context *tdb, void *buf, tdb_len_t size); ...@@ -296,20 +296,13 @@ void *tdb_convert(const struct tdb_context *tdb, void *buf, tdb_len_t size);
void tdb_munmap(struct tdb_context *tdb); void tdb_munmap(struct tdb_context *tdb);
void tdb_mmap(struct tdb_context *tdb); void tdb_mmap(struct tdb_context *tdb);
/* Hand data to a function, direct if possible */
int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
tdb_off_t offset, tdb_len_t len,
int (*parser)(TDB_DATA key, TDB_DATA data,
void *private_data),
void *private_data);
/* Either make a copy into pad and return that, or return ptr into mmap. /* Either make a copy into pad and return that, or return ptr into mmap.
* Converts endian (ie. will use pad in that case). */ * Converts endian (ie. will use pad in that case). */
void *tdb_get(struct tdb_context *tdb, tdb_off_t off, void *pad, size_t len); void *tdb_get(struct tdb_context *tdb, tdb_off_t off, void *pad, size_t len);
/* Either alloc a copy, or give direct access. Release frees or noop. */ /* Either alloc a copy, or give direct access. Release frees or noop. */
const void *tdb_access_read(struct tdb_context *tdb, const void *tdb_access_read(struct tdb_context *tdb,
tdb_off_t off, tdb_len_t len); tdb_off_t off, tdb_len_t len, bool convert);
void tdb_access_release(struct tdb_context *tdb, const void *p); void tdb_access_release(struct tdb_context *tdb, const void *p);
/* Convenience routine to get an offset. */ /* Convenience routine to get an offset. */
...@@ -337,9 +330,9 @@ bool tdb_read_all(int fd, void *buf, size_t len); ...@@ -337,9 +330,9 @@ bool tdb_read_all(int fd, void *buf, size_t len);
/* Allocate and make a copy of some offset. */ /* Allocate and make a copy of some offset. */
void *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len); void *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len);
/* Munges record and writes it */ /* Writes a converted copy of a record. */
int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off, int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off,
void *rec, size_t len); const void *rec, size_t len);
/* Reads record and converts it */ /* Reads record and converts it */
int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off, int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off,
......
...@@ -425,7 +425,7 @@ static tdb_off_t entry_matches(struct tdb_context *tdb, ...@@ -425,7 +425,7 @@ static tdb_off_t entry_matches(struct tdb_context *tdb,
if (keylen != key->dsize) if (keylen != key->dsize)
return TDB_OFF_ERR; return TDB_OFF_ERR;
rkey = tdb_access_read(tdb, off + sizeof(*rec), keylen); rkey = tdb_access_read(tdb, off + sizeof(*rec), keylen, false);
if (!rkey) if (!rkey)
return TDB_OFF_ERR; return TDB_OFF_ERR;
if (memcmp(rkey, key->dptr, keylen) != 0) if (memcmp(rkey, key->dptr, keylen) != 0)
...@@ -435,7 +435,7 @@ static tdb_off_t entry_matches(struct tdb_context *tdb, ...@@ -435,7 +435,7 @@ static tdb_off_t entry_matches(struct tdb_context *tdb,
} }
/* FIXME: Optimize? */ /* FIXME: Optimize? */
static void unlock_range(struct tdb_context *tdb, static void unlock_lists(struct tdb_context *tdb,
tdb_off_t list, tdb_len_t num, tdb_off_t list, tdb_len_t num,
int ltype) int ltype)
{ {
...@@ -446,7 +446,7 @@ static void unlock_range(struct tdb_context *tdb, ...@@ -446,7 +446,7 @@ static void unlock_range(struct tdb_context *tdb,
} }
/* FIXME: Optimize? */ /* FIXME: Optimize? */
static int lock_range(struct tdb_context *tdb, static int lock_lists(struct tdb_context *tdb,
tdb_off_t list, tdb_len_t num, tdb_off_t list, tdb_len_t num,
int ltype) int ltype)
{ {
...@@ -454,7 +454,7 @@ static int lock_range(struct tdb_context *tdb, ...@@ -454,7 +454,7 @@ static int lock_range(struct tdb_context *tdb,
for (i = list; i < list + num; i++) { for (i = list; i < list + num; i++) {
if (tdb_lock_list(tdb, i, ltype, TDB_LOCK_WAIT) != 0) { if (tdb_lock_list(tdb, i, ltype, TDB_LOCK_WAIT) != 0) {
unlock_range(tdb, list, i - list, ltype); unlock_lists(tdb, list, i - list, ltype);
return -1; return -1;
} }
} }
...@@ -480,7 +480,7 @@ again: ...@@ -480,7 +480,7 @@ again:
if (tdb_lock_list(tdb, 0, ltype, TDB_LOCK_WAIT)) if (tdb_lock_list(tdb, 0, ltype, TDB_LOCK_WAIT))
return TDB_OFF_ERR; return TDB_OFF_ERR;
len = tdb_find_zero_off(tdb, hash_off(tdb, 0), num); len = tdb_find_zero_off(tdb, hash_off(tdb, 0), num);
if (lock_range(tdb, 1, len, ltype) == -1) { if (lock_lists(tdb, 1, len, ltype) == -1) {
tdb_unlock_list(tdb, 0, ltype); tdb_unlock_list(tdb, 0, ltype);
return TDB_OFF_ERR; return TDB_OFF_ERR;
} }
...@@ -491,9 +491,9 @@ again: ...@@ -491,9 +491,9 @@ again:
start++; start++;
pre_locks = 0; pre_locks = 0;
} }
if (unlikely(lock_range(tdb, start, len, ltype) == -1)) { if (unlikely(lock_lists(tdb, start, len, ltype) == -1)) {
if (pre_locks) if (pre_locks)
unlock_range(tdb, 0, pre_locks, ltype); unlock_lists(tdb, 0, pre_locks, ltype);
else else
tdb_unlock_list(tdb, start, ltype); tdb_unlock_list(tdb, start, ltype);
return TDB_OFF_ERR; return TDB_OFF_ERR;
...@@ -501,9 +501,9 @@ again: ...@@ -501,9 +501,9 @@ again:
/* Now, did we lose the race, and it's not zero any more? */ /* Now, did we lose the race, and it's not zero any more? */
if (unlikely(tdb_read_off(tdb, hash_off(tdb, pre_locks + len)) != 0)) { if (unlikely(tdb_read_off(tdb, hash_off(tdb, pre_locks + len)) != 0)) {
unlock_range(tdb, 0, pre_locks, ltype); unlock_lists(tdb, 0, pre_locks, ltype);
/* Leave the start locked, as expected. */ /* Leave the start locked, as expected. */
unlock_range(tdb, start + 1, len - 1, ltype); unlock_lists(tdb, start + 1, len - 1, ltype);
goto again; goto again;
} }
...@@ -529,7 +529,8 @@ static int update_rec_hdr(struct tdb_context *tdb, ...@@ -529,7 +529,8 @@ static int update_rec_hdr(struct tdb_context *tdb,
/* If we fail, others will try after us. */ /* If we fail, others will try after us. */
static void enlarge_hash(struct tdb_context *tdb) static void enlarge_hash(struct tdb_context *tdb)
{ {
tdb_off_t newoff, i; tdb_off_t newoff, oldoff, i;
tdb_len_t hlen;
uint64_t h, num = 1ULL << tdb->header.v.hash_bits; uint64_t h, num = 1ULL << tdb->header.v.hash_bits;
struct tdb_used_record pad, *r; struct tdb_used_record pad, *r;
...@@ -541,26 +542,32 @@ static void enlarge_hash(struct tdb_context *tdb) ...@@ -541,26 +542,32 @@ static void enlarge_hash(struct tdb_context *tdb)
if ((1ULL << tdb->header.v.hash_bits) != num) if ((1ULL << tdb->header.v.hash_bits) != num)
goto unlock; goto unlock;
newoff = alloc(tdb, 0, num * 2, 0, false); /* Allocate our new array. */
hlen = num * sizeof(tdb_off_t) * 2;
newoff = alloc(tdb, 0, hlen, 0, false);
if (unlikely(newoff == TDB_OFF_ERR)) if (unlikely(newoff == TDB_OFF_ERR))
goto unlock; goto unlock;
if (unlikely(newoff == 0)) { if (unlikely(newoff == 0)) {
if (tdb_expand(tdb, 0, num * 2, false) == -1) if (tdb_expand(tdb, 0, hlen, false) == -1)
goto unlock; goto unlock;
newoff = alloc(tdb, 0, num * 2, 0, false); newoff = alloc(tdb, 0, hlen, 0, false);
if (newoff == TDB_OFF_ERR || newoff == 0) if (newoff == TDB_OFF_ERR || newoff == 0)
goto unlock; goto unlock;
} }
/* Step over record header! */
newoff += sizeof(struct tdb_used_record);
/* Starts all zero. */
if (zero_out(tdb, newoff, hlen) == -1)
goto unlock;
/* FIXME: If the space before is empty, we know this is in its ideal /* FIXME: If the space before is empty, we know this is in its ideal
* location. We can steal a bit from the pointer to avoid rehash. */ * location. Or steal a bit from the pointer to avoid rehash. */
for (i = tdb_find_nonzero_off(tdb, tdb->header.v.hash_off, num); for (i = tdb_find_nonzero_off(tdb, hash_off(tdb, 0), num);
i < num; i < num;
i += tdb_find_nonzero_off(tdb, tdb->header.v.hash_off i += tdb_find_nonzero_off(tdb, hash_off(tdb, i), num - i)) {
+ i*sizeof(tdb_off_t), num - i)) {
tdb_off_t off; tdb_off_t off;
off = tdb_read_off(tdb, tdb->header.v.hash_off off = tdb_read_off(tdb, hash_off(tdb, i));
+ i*sizeof(tdb_off_t));
if (unlikely(off == TDB_OFF_ERR)) if (unlikely(off == TDB_OFF_ERR))
goto unlock; goto unlock;
if (unlikely(!off)) { if (unlikely(!off)) {
...@@ -578,18 +585,17 @@ static void enlarge_hash(struct tdb_context *tdb) ...@@ -578,18 +585,17 @@ static void enlarge_hash(struct tdb_context *tdb)
} }
/* Free up old hash. */ /* Free up old hash. */
r = tdb_get(tdb, tdb->header.v.hash_off, &pad, sizeof(*r)); oldoff = tdb->header.v.hash_off - sizeof(*r);
r = tdb_get(tdb, oldoff, &pad, sizeof(*r));
if (!r) if (!r)
goto unlock; goto unlock;
add_free_record(tdb, tdb->header.v.hash_off, add_free_record(tdb, oldoff,
rec_data_length(r) + rec_extra_padding(r)); sizeof(*r)+rec_data_length(r)+rec_extra_padding(r));
/* Now we write the modified header. */ /* Now we write the modified header. */
tdb->header.v.generation++;
tdb->header.v.hash_bits++; tdb->header.v.hash_bits++;
tdb->header.v.hash_off = newoff; tdb->header.v.hash_off = newoff;
tdb_write_convert(tdb, offsetof(struct tdb_header, v), write_header(tdb);
&tdb->header.v, sizeof(tdb->header.v));
unlock: unlock:
tdb_allrecord_unlock(tdb, F_WRLCK); tdb_allrecord_unlock(tdb, F_WRLCK);
} }
...@@ -667,7 +673,7 @@ int tdb_store(struct tdb_context *tdb, ...@@ -667,7 +673,7 @@ int tdb_store(struct tdb_context *tdb,
/* Allocate a new record. */ /* Allocate a new record. */
new_off = alloc(tdb, key.dsize, dbuf.dsize, h, growing); new_off = alloc(tdb, key.dsize, dbuf.dsize, h, growing);
if (new_off == 0) { if (new_off == 0) {
unlock_range(tdb, start, num_locks, F_WRLCK); unlock_lists(tdb, start, num_locks, F_WRLCK);
/* Expand, then try again... */ /* Expand, then try again... */
if (tdb_expand(tdb, key.dsize, dbuf.dsize, growing) == -1) if (tdb_expand(tdb, key.dsize, dbuf.dsize, growing) == -1)
return -1; return -1;
...@@ -695,7 +701,7 @@ write: ...@@ -695,7 +701,7 @@ write:
goto fail; goto fail;
/* FIXME: tdb_increment_seqnum(tdb); */ /* FIXME: tdb_increment_seqnum(tdb); */
unlock_range(tdb, start, num_locks, F_WRLCK); unlock_lists(tdb, start, num_locks, F_WRLCK);
/* FIXME: by simple simulation, this approximated 60% full. /* FIXME: by simple simulation, this approximated 60% full.
* Check in real case! */ * Check in real case! */
...@@ -705,7 +711,7 @@ write: ...@@ -705,7 +711,7 @@ write:
return 0; return 0;
fail: fail:
unlock_range(tdb, start, num_locks, F_WRLCK); unlock_lists(tdb, start, num_locks, F_WRLCK);
return -1; return -1;
} }
...@@ -745,7 +751,7 @@ struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key) ...@@ -745,7 +751,7 @@ struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
} }
if (!off) { if (!off) {
unlock_range(tdb, start, num_locks, F_RDLCK); unlock_lists(tdb, start, num_locks, F_RDLCK);
tdb->ecode = TDB_ERR_NOEXIST; tdb->ecode = TDB_ERR_NOEXIST;
return tdb_null; return tdb_null;
} }
...@@ -753,7 +759,7 @@ struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key) ...@@ -753,7 +759,7 @@ struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
ret.dsize = rec_data_length(&rec); ret.dsize = rec_data_length(&rec);
ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize, ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
ret.dsize); ret.dsize);
unlock_range(tdb, start, num_locks, F_RDLCK); unlock_lists(tdb, start, num_locks, F_RDLCK);
return ret; return ret;
} }
...@@ -831,7 +837,7 @@ int tdb_delete(struct tdb_context *tdb, struct tdb_data key) ...@@ -831,7 +837,7 @@ int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
} }
if (!off) { if (!off) {
unlock_range(tdb, start, num_locks, F_WRLCK); unlock_lists(tdb, start, num_locks, F_WRLCK);
tdb->ecode = TDB_ERR_NOEXIST; tdb->ecode = TDB_ERR_NOEXIST;
return -1; return -1;
} }
...@@ -875,11 +881,11 @@ delete: ...@@ -875,11 +881,11 @@ delete:
+ rec_extra_padding(&rec)) != 0) + rec_extra_padding(&rec)) != 0)
goto unlock_err; goto unlock_err;
unlock_range(tdb, start, num_locks, F_WRLCK); unlock_lists(tdb, start, num_locks, F_WRLCK);
return 0; return 0;
unlock_err: unlock_err:
unlock_range(tdb, start, num_locks, F_WRLCK); unlock_lists(tdb, start, num_locks, F_WRLCK);
return -1; return -1;
} }
......
#include <ccan/tdb2/tdb.c>
#include <ccan/tdb2/free.c>
#include <ccan/tdb2/lock.c>
#include <ccan/tdb2/io.c>
#include <ccan/tdb2/check.c>
#include <ccan/tap/tap.h>
#include "logging.h"
int main(int argc, char *argv[])
{
unsigned int i;
struct tdb_context *tdb;
int flags[] = { TDB_INTERNAL, TDB_DEFAULT,
TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT };
plan_tests(sizeof(flags) / sizeof(flags[0]) * 2 + 1);
for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
tdb = tdb_open("/tmp/run-new_database.tdb", flags[i],
O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
tdb->log = tap_log_fn;
ok1(tdb);
if (tdb) {
enlarge_hash(tdb);
ok1(tdb_check(tdb, NULL, NULL) == 0);
tdb_close(tdb);
}
}
ok1(tap_log_messages == 0);
return exit_status();
}
...@@ -13,14 +13,24 @@ int main(int argc, char *argv[]) ...@@ -13,14 +13,24 @@ int main(int argc, char *argv[])
int flags[] = { TDB_INTERNAL, TDB_DEFAULT, int flags[] = { TDB_INTERNAL, TDB_DEFAULT,
TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT }; TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT };
plan_tests(sizeof(flags) / sizeof(flags[0]) * 3 + 1); plan_tests(sizeof(flags) / sizeof(flags[0]) * 10 + 1);
for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) { for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
tdb = tdb_open("/tmp/run-new_database.tdb", flags[i], tdb = tdb_open("/tmp/run-new_database.tdb", flags[i],
O_RDWR|O_CREAT|O_TRUNC, 0600, NULL); O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
tdb->log = tap_log_fn; tdb->log = tap_log_fn;
ok1(tdb); ok1(tdb);
if (tdb) { if (tdb) {
/* First expand (expand file to fill zone). */
ok1(tdb_expand(tdb, 1, 1, false) == 0); ok1(tdb_expand(tdb, 1, 1, false) == 0);
ok1(tdb->header.v.num_zones == 1);
ok1(tdb_check(tdb, NULL, NULL) == 0);
/* Little expand (extra zone). */
ok1(tdb_expand(tdb, 1, 1, false) == 0);
ok1(tdb->header.v.num_zones == 2);
ok1(tdb_check(tdb, NULL, NULL) == 0);
/* Big expand (enlarge zones) */
ok1(tdb_expand(tdb, 1, 4096, false) == 0);
ok1(tdb->header.v.num_zones == 2);
ok1(tdb_check(tdb, NULL, NULL) == 0); ok1(tdb_check(tdb, NULL, NULL) == 0);
tdb_close(tdb); tdb_close(tdb);
} }
......
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