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)
{
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) {
hash = tdb_hash(tdb, key.dptr, key.dsize);
if (!(rec_ptr = tdb1_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
- /* record not found */
- return TDB_ERR_NOEXIST;
+ return tdb->last_error;
}
ret = tdb1_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
{
struct tdb1_record rec;
tdb1_off_t rec_ptr;
- char *p = NULL;
int ret = -1;
/* check for it existing, on insert. */
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() */
if (flag != TDB_INSERT)
tdb1_delete_hash(tdb, key, hash);
- /* Copy key+value *before* allocating free space in case malloc
- 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;
- goto fail;
- }
-
- memcpy(p, key.dptr, key.dsize);
- if (dbuf.dsize)
- memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
-
if (tdb->tdb1.max_dead_records != 0) {
/*
* Allow for some dead records per hash chain, look if we can
if (tdb1_rec_write(tdb, rec_ptr, &rec) == -1
|| tdb->tdb1.io->tdb1_write(
tdb, rec_ptr + sizeof(rec),
- p, key.dsize + dbuf.dsize) == -1) {
+ key.dptr, key.dsize) == -1
+ || tdb->tdb1.io->tdb1_write(
+ tdb, rec_ptr + sizeof(rec) + key.dsize,
+ dbuf.dptr, dbuf.dsize) == -1) {
goto fail;
}
goto done;
/* write out and point the top of the hash chain at it */
if (tdb1_rec_write(tdb, rec_ptr, &rec) == -1
- || tdb->tdb1.io->tdb1_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
+ || tdb->tdb1.io->tdb1_write(tdb, rec_ptr + sizeof(rec),
+ key.dptr, key.dsize) == -1
+ || tdb->tdb1.io->tdb1_write(tdb, rec_ptr + sizeof(rec) + key.dsize,
+ dbuf.dptr, dbuf.dsize) == -1
|| tdb1_ofs_write(tdb, TDB1_HASH_TOP(hash), &rec_ptr) == -1) {
/* Need to tdb1_unallocate() here */
goto fail;
if (ret == 0) {
tdb1_increment_seqnum(tdb);
}
-
- SAFE_FREE(p);
return ret;
}
}
}
+ tdb1_increment_seqnum_nonblock(tdb);
tdb_unlockall(tdb);
return 0;