struct new_database {
struct tdb_header hdr;
- /* Initial free zone. */
- struct free_zone_header zhdr;
- tdb_off_t free[BUCKETS_FOR_ZONE(INITIAL_ZONE_BITS) + 1];
+ struct tdb_freelist flist;
};
/* initialise a new database */
sizeof(newdb.hdr.hash_test),
newdb.hdr.hash_seed,
tdb->hash_priv);
+ newdb.hdr.recovery = 0;
memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
/* Initial hashes are empty. */
memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
/* Free is empty. */
- newdb.zhdr.zone_bits = INITIAL_ZONE_BITS;
- memset(newdb.free, 0, sizeof(newdb.free));
+ newdb.hdr.free_list = offsetof(struct new_database, flist);
+ memset(&newdb.flist, 0, sizeof(newdb.flist));
+ set_used_header(NULL, &newdb.flist.hdr, 0,
+ sizeof(newdb.flist) - sizeof(newdb.flist.hdr),
+ sizeof(newdb.flist) - sizeof(newdb.flist.hdr), 1);
/* Magic food */
memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
tdb->log = null_log_fn;
tdb->log_priv = NULL;
tdb->transaction = NULL;
+ tdb->stats = NULL;
tdb_hash_init(tdb);
- /* last_zone will be set below. */
tdb_io_init(tdb);
tdb_lock_init(tdb);
case TDB_ATTRIBUTE_SEED:
seed = &attr->seed;
break;
+ case TDB_ATTRIBUTE_STATS:
+ tdb->stats = &attr->stats;
+ /* They have stats we don't know about? Tell them. */
+ if (tdb->stats->size > sizeof(attr->stats))
+ tdb->stats->size = sizeof(attr->stats);
+ break;
default:
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
"tdb_open: unknown attribute type %u\n",
}
tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
tdb->hash_seed = hdr.hash_seed;
- tdb_zone_init(tdb);
+ tdb_flist_init(tdb);
return tdb;
}
fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
/* ensure there is only one process initialising at once */
- if (tdb_lock_open(tdb) == -1) {
+ if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
"tdb_open: failed to get open lock on %s: %s\n",
name, strerror(errno));
/* This make sure we have current map_size and mmap. */
tdb->methods->oob(tdb, tdb->map_size + 1, true);
- /* Now we can pick a random free zone to start from. */
- if (tdb_zone_init(tdb) == -1)
+ /* Now it's fully formed, recover if necessary. */
+ if (tdb_needs_recovery(tdb) && tdb_lock_and_recover(tdb) == -1) {
+ errno = EIO;
+ goto fail;
+ }
+
+ if (tdb_flist_init(tdb) == -1)
goto fail;
tdb->next = tdbs;
{
uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
- if (set_header(tdb, rec, keylen, datalen, keylen + dataroom, h,
- rec_zone_bits(rec)))
+ if (set_used_header(tdb, rec, keylen, datalen, keylen + dataroom, h))
return -1;
return tdb_write_convert(tdb, off, rec, sizeof(*rec));
struct hash_info *h,
struct tdb_data key, struct tdb_data dbuf,
tdb_off_t old_off, tdb_len_t old_room,
- unsigned old_zone,
bool growing)
{
tdb_off_t new_off;
/* We didn't like the existing one: remove it. */
if (old_off) {
- add_free_record(tdb, old_zone, old_off,
+ add_stat(tdb, frees, 1);
+ add_free_record(tdb, old_off,
sizeof(struct tdb_used_record)
+ key.dsize + old_room);
if (replace_in_hash(tdb, h, new_off) == -1)
}
/* If we didn't use the old record, this implies we're growing. */
- ret = replace_data(tdb, &h, key, dbuf, off, old_room,
- rec_zone_bits(&rec), off != 0);
+ ret = replace_data(tdb, &h, key, dbuf, off, old_room, off != 0);
tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
return ret;
}
/* If they're using tdb_append(), it implies they're growing record. */
- ret = replace_data(tdb, &h, key, new_dbuf, off,
- old_room, rec_zone_bits(&rec), true);
+ ret = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
free(newdata);
goto unlock_err;
/* Free the deleted entry. */
- if (add_free_record(tdb, rec_zone_bits(&rec), off,
+ add_stat(tdb, frees, 1);
+ if (add_free_record(tdb, off,
sizeof(struct tdb_used_record)
+ rec_key_length(&rec)
+ rec_data_length(&rec)
struct tdb_context **i;
int ret = 0;
- /* FIXME:
+ tdb_trace(tdb, "tdb_close");
+
if (tdb->transaction) {
tdb_transaction_cancel(tdb);
}
- */
- tdb_trace(tdb, "tdb_close");
if (tdb->map_ptr) {
if (tdb->flags & TDB_INTERNAL)