X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Ftdb1_tdb.c;h=6f6f080da2298497127fd7ae602472f30ed8908d;hp=cdcf76d3689f4a02f4ea11178d91556bb27d68fe;hb=1be090a2d749713cfd0c4584cafb97bffd716189;hpb=c8c3b3568677e8b0105f84e4ab068c580faf4591 diff --git a/ccan/tdb2/tdb1_tdb.c b/ccan/tdb2/tdb1_tdb.c index cdcf76d3..6f6f080d 100644 --- a/ccan/tdb2/tdb1_tdb.c +++ b/ccan/tdb2/tdb1_tdb.c @@ -26,8 +26,7 @@ */ #include "tdb1_private.h" - -TDB_DATA tdb1_null; +#include /* non-blocking increment of the tdb sequence number if the tdb has been opened using @@ -69,13 +68,17 @@ static void tdb1_increment_seqnum(struct tdb_context *tdb) tdb1_nest_unlock(tdb, TDB1_SEQNUM_OFS, F_WRLCK); } -static int tdb1_key_compare(TDB_DATA key, TDB_DATA data, void *private_data) +static enum TDB_ERROR tdb1_key_compare(TDB_DATA key, TDB_DATA data, + void *matches_) { - return memcmp(data.dptr, key.dptr, data.dsize); + bool *matches = matches_; + *matches = (memcmp(data.dptr, key.dptr, data.dsize) == 0); + return TDB_SUCCESS; } -/* Returns 0 on fail. On success, return offset of record, and fills - in rec */ +/* Returns 0 on fail; last_error will be TDB_ERR_NOEXIST if it simply + * wasn't there, otherwise a real error. + * On success, return offset of record, and fills in rec */ static tdb1_off_t tdb1_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, struct tdb1_record *r) { @@ -90,12 +93,30 @@ static tdb1_off_t tdb1_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash if (tdb1_rec_read(tdb, rec_ptr, r) == -1) return 0; - if (!TDB1_DEAD(r) && hash==r->full_hash - && key.dsize==r->key_len - && tdb1_parse_data(tdb, key, rec_ptr + sizeof(*r), - r->key_len, tdb1_key_compare, - NULL) == 0) { - return rec_ptr; + tdb->stats.compares++; + if (TDB1_DEAD(r)) { + tdb->stats.compare_wrong_bucket++; + } else if (key.dsize != r->key_len) { + tdb->stats.compare_wrong_keylen++; + } else if (hash != r->full_hash) { + tdb->stats.compare_wrong_rechash++; + } else { + enum TDB_ERROR ecode; + bool matches; + ecode = tdb1_parse_data(tdb, key, rec_ptr + sizeof(*r), + r->key_len, tdb1_key_compare, + &matches); + + if (ecode != TDB_SUCCESS) { + tdb->last_error = ecode; + return 0; + } + + if (!matches) { + tdb->stats.compare_wrong_keycmp++; + } else { + return rec_ptr; + } } /* detect tight infinite loop */ if (rec_ptr == r->next) { @@ -190,8 +211,11 @@ static TDB_DATA _tdb1_fetch(struct tdb_context *tdb, TDB_DATA key) /* find which hash bucket it is in */ hash = tdb_hash(tdb, key.dptr, key.dsize); - if (!(rec_ptr = tdb1_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) - return tdb1_null; + if (!(rec_ptr = tdb1_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) { + ret.dptr = NULL; + ret.dsize = 0; + return ret; + } ret.dptr = tdb1_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len, rec.data_len); @@ -200,48 +224,30 @@ static TDB_DATA _tdb1_fetch(struct tdb_context *tdb, TDB_DATA key) return ret; } -TDB_DATA tdb1_fetch(struct tdb_context *tdb, TDB_DATA key) +enum TDB_ERROR tdb1_fetch(struct tdb_context *tdb, TDB_DATA key, TDB_DATA *data) { - TDB_DATA ret = _tdb1_fetch(tdb, key); - - return ret; + *data = _tdb1_fetch(tdb, key); + if (data->dptr == NULL) + return tdb->last_error; + return TDB_SUCCESS; } -/* - * Find an entry in the database and hand the record's data to a parsing - * function. The parsing function is executed under the chain read lock, so it - * should be fast and should not block on other syscalls. - * - * DON'T CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS. - * - * For mmapped tdb's that do not have a transaction open it points the parsing - * function directly at the mmap area, it avoids the malloc/memcpy in this - * case. If a transaction is open or no mmap is available, it has to do - * malloc/read/parse/free. - * - * This is interesting for all readers of potentially large data structures in - * the tdb records, ldb indexes being one example. - * - * Return -1 if the record was not found. - */ - -int tdb1_parse_record(struct tdb_context *tdb, TDB_DATA key, - int (*parser)(TDB_DATA key, TDB_DATA data, - void *private_data), - void *private_data) +enum TDB_ERROR tdb1_parse_record(struct tdb_context *tdb, TDB_DATA key, + enum TDB_ERROR (*parser)(TDB_DATA key, + TDB_DATA data, + void *private_data), + void *private_data) { tdb1_off_t rec_ptr; struct tdb1_record rec; - int ret; + enum TDB_ERROR ret; uint32_t hash; /* find which hash bucket it is in */ hash = tdb_hash(tdb, key.dptr, key.dsize); if (!(rec_ptr = tdb1_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) { - /* record not found */ - tdb->last_error = TDB_ERR_NOEXIST; - return -1; + return tdb->last_error; } ret = tdb1_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len, @@ -273,6 +279,7 @@ int tdb1_exists(struct tdb_context *tdb, TDB_DATA key) uint32_t hash = tdb_hash(tdb, key.dptr, key.dsize); int ret; + assert(tdb->flags & TDB_VERSION1); ret = tdb1_exists_hash(tdb, key, hash); return ret; } @@ -432,6 +439,7 @@ int tdb1_delete(struct tdb_context *tdb, TDB_DATA key) uint32_t hash = tdb_hash(tdb, key.dptr, key.dsize); int ret; + assert(tdb->flags & TDB_VERSION1); ret = tdb1_delete_hash(tdb, key, hash); return ret; } @@ -479,16 +487,23 @@ static int _tdb1_store(struct tdb_context *tdb, TDB_DATA key, tdb->last_error = TDB_ERR_EXISTS; goto fail; } + if (tdb->last_error != TDB_ERR_NOEXIST) { + goto fail; + } } else { /* first try in-place update, on modify or replace. */ if (tdb1_update_hash(tdb, key, hash, dbuf) == 0) { goto done; } - if (tdb->last_error == TDB_ERR_NOEXIST && - flag == TDB_MODIFY) { - /* if the record doesn't exist and we are in TDB1_MODIFY mode then - we should fail the store */ - goto fail; + if (tdb->last_error != TDB_SUCCESS) { + if (tdb->last_error != TDB_ERR_NOEXIST) { + goto fail; + } + if (flag == TDB_MODIFY) { + /* if the record doesn't exist and we are in TDB1_MODIFY mode then + we should fail the store */ + goto fail; + } } } /* reset the error code potentially set by the tdb1_update() */ @@ -504,7 +519,9 @@ static int _tdb1_store(struct tdb_context *tdb, TDB_DATA key, fails and we are left with a dead spot in the tdb. */ if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) { - tdb->last_error = TDB_ERR_OOM; + tdb->last_error = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR, + "tdb1_store: out of memory" + " allocating copy"); goto fail; } @@ -601,8 +618,12 @@ int tdb1_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag) uint32_t hash; int ret; + assert(tdb->flags & TDB_VERSION1); + if ((tdb->flags & TDB_RDONLY) || tdb->tdb1.traverse_read) { - tdb->last_error = TDB_ERR_RDONLY; + tdb->last_error = tdb_logerr(tdb, TDB_ERR_RDONLY, + TDB_LOG_USE_ERROR, + "tdb_store: read-only tdb"); return -1; } @@ -623,6 +644,8 @@ int tdb1_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf) TDB_DATA dbuf; int ret = -1; + assert(tdb->flags & TDB_VERSION1); + /* find which hash bucket it is in */ hash = tdb_hash(tdb, key.dptr, key.dsize); if (tdb1_lock(tdb, TDB1_BUCKET(hash), F_WRLCK) == -1) @@ -724,7 +747,7 @@ int tdb1_wipe_all(struct tdb_context *tdb) tdb1_off_t recovery_head; tdb1_len_t recovery_size = 0; - if (tdb1_lockall(tdb) != 0) { + if (tdb_lockall(tdb) != TDB_SUCCESS) { return -1; } @@ -793,121 +816,15 @@ int tdb1_wipe_all(struct tdb_context *tdb) } } - if (tdb1_unlockall(tdb) != 0) { - tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, - "tdb1_wipe_all: failed to unlock"); - goto failed; - } - + tdb1_increment_seqnum_nonblock(tdb); + tdb_unlockall(tdb); return 0; failed: - tdb1_unlockall(tdb); + tdb_unlockall(tdb); return -1; } -struct traverse_state { - enum TDB_ERROR error; - struct tdb_context *dest_db; -}; - -/* - traverse function for repacking - */ -static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data) -{ - struct traverse_state *state = (struct traverse_state *)private_data; - if (tdb1_store(state->dest_db, key, data, TDB_INSERT) != 0) { - state->error = state->dest_db->last_error; - return -1; - } - return 0; -} - -/* - repack a tdb - */ -int tdb1_repack(struct tdb_context *tdb) -{ - struct tdb_context *tmp_db; - struct traverse_state state; - union tdb_attribute hsize; - - hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE; - hsize.base.next = NULL; - hsize.tdb1_hashsize.hsize = tdb->tdb1.header.hash_size; - - if (tdb1_transaction_start(tdb) != 0) { - tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, - __location__ " Failed to start transaction"); - return -1; - } - - tmp_db = tdb_open("tmpdb", TDB_INTERNAL, O_RDWR|O_CREAT, 0, &hsize); - if (tmp_db == NULL) { - tdb->last_error = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR, - __location__ " Failed to create tmp_db"); - tdb1_transaction_cancel(tdb); - return -1; - } - - state.error = TDB_SUCCESS; - state.dest_db = tmp_db; - - if (tdb1_traverse_read(tdb, repack_traverse, &state) == -1) { - tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, - __location__ " Failed to traverse copying out"); - tdb1_transaction_cancel(tdb); - tdb_close(tmp_db); - return -1; - } - - if (state.error != TDB_SUCCESS) { - tdb->last_error = tdb_logerr(tdb, state.error, TDB_LOG_ERROR, - __location__ " Error during traversal"); - tdb1_transaction_cancel(tdb); - tdb_close(tmp_db); - return -1; - } - - if (tdb1_wipe_all(tdb) != 0) { - tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, - __location__ " Failed to wipe database\n"); - tdb1_transaction_cancel(tdb); - tdb_close(tmp_db); - return -1; - } - - state.error = TDB_SUCCESS; - state.dest_db = tdb; - - if (tdb1_traverse_read(tmp_db, repack_traverse, &state) == -1) { - tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, - __location__ " Failed to traverse copying back"); - tdb1_transaction_cancel(tdb); - tdb_close(tmp_db); - return -1; - } - - if (state.error) { - tdb->last_error = tdb_logerr(tdb, state.error, TDB_LOG_ERROR, - __location__ " Error during second traversal"); - tdb1_transaction_cancel(tdb); - tdb_close(tmp_db); - return -1; - } - - tdb_close(tmp_db); - - if (tdb1_transaction_commit(tdb) != 0) { - tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, - __location__ " Failed to commit"); - return -1; - } - - return 0; -} - /* Even on files, we can get partial writes due to signals. */ bool tdb1_write_all(int fd, const void *buf, size_t count) {