static ntdb_bool_err key_matches(struct ntdb_context *ntdb,
const struct ntdb_used_record *rec,
ntdb_off_t off,
- const NTDB_DATA *key)
+ const NTDB_DATA *key,
+ const char **rptr)
{
ntdb_bool_err ret = false;
const char *rkey;
return ret;
}
- rkey = ntdb_access_read(ntdb, off + sizeof(*rec), key->dsize, false);
+ rkey = ntdb_access_read(ntdb, off + sizeof(*rec),
+ key->dsize + rec_data_length(rec), false);
if (NTDB_PTR_IS_ERR(rkey)) {
return (ntdb_bool_err)NTDB_PTR_ERR(rkey);
}
- if (memcmp(rkey, key->dptr, key->dsize) == 0)
- ret = true;
- else
- ntdb->stats.compare_wrong_keycmp++;
+ if (memcmp(rkey, key->dptr, key->dsize) == 0) {
+ if (rptr) {
+ *rptr = rkey;
+ } else {
+ ntdb_access_release(ntdb, rkey);
+ }
+ return true;
+ }
+ ntdb->stats.compare_wrong_keycmp++;
ntdb_access_release(ntdb, rkey);
return ret;
}
const NTDB_DATA *key,
ntdb_off_t val,
struct ntdb_used_record *rec,
+ const char **rptr,
const ntdb_off_t **mapped)
{
ntdb_off_t off;
return (ntdb_bool_err)ecode;
}
- return key_matches(ntdb, rec, off, key);
+ return key_matches(ntdb, rec, off, key, rptr);
}
static bool is_chain(ntdb_off_t val)
* If not found, the return value is 0.
* If found, the return value is the offset, and *rec is the record. */
ntdb_off_t find_and_lock(struct ntdb_context *ntdb,
- NTDB_DATA key,
- int ltype,
- struct hash_info *h,
- struct ntdb_used_record *rec)
+ NTDB_DATA key,
+ int ltype,
+ struct hash_info *h,
+ struct ntdb_used_record *rec,
+ const char **rptr)
{
ntdb_off_t off, val;
const ntdb_off_t *arr = NULL;
}
/* Directly in hash table? */
- if (!is_chain(val)) {
+ if (!likely(is_chain(val))) {
if (val) {
- berr = match(ntdb, h->h, &key, val, rec, NULL);
+ berr = match(ntdb, h->h, &key, val, rec, rptr, NULL);
if (berr < 0) {
ecode = NTDB_OFF_TO_ERR(berr);
goto fail;
found_empty = true;
}
} else {
- berr = match(ntdb, h->h, &key, val, rec, &arr);
+ berr = match(ntdb, h->h, &key, val, rec, rptr, &arr);
if (berr < 0) {
ecode = NTDB_OFF_TO_ERR(berr);
if (arr) {
struct ntdb_used_record rec;
enum NTDB_ERROR ecode;
- off = find_and_lock(ntdb, key, F_WRLCK, &h, &rec);
+ off = find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL);
if (NTDB_OFF_IS_ERR(off)) {
return NTDB_OFF_TO_ERR(off);
}
NTDB_DATA new_dbuf;
enum NTDB_ERROR ecode;
- off = find_and_lock(ntdb, key, F_WRLCK, &h, &rec);
+ off = find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL);
if (NTDB_OFF_IS_ERR(off)) {
return NTDB_OFF_TO_ERR(off);
}
struct ntdb_used_record rec;
struct hash_info h;
enum NTDB_ERROR ecode;
+ const char *keyp;
- off = find_and_lock(ntdb, key, F_RDLCK, &h, &rec);
+ off = find_and_lock(ntdb, key, F_RDLCK, &h, &rec, &keyp);
if (NTDB_OFF_IS_ERR(off)) {
return NTDB_OFF_TO_ERR(off);
}
ecode = NTDB_ERR_NOEXIST;
} else {
data->dsize = rec_data_length(&rec);
- data->dptr = ntdb_alloc_read(ntdb, off + sizeof(rec) + key.dsize,
- data->dsize);
- if (NTDB_PTR_IS_ERR(data->dptr)) {
- ecode = NTDB_PTR_ERR(data->dptr);
- } else
+ data->dptr = ntdb->alloc_fn(ntdb, data->dsize, ntdb->alloc_data);
+ if (unlikely(!data->dptr)) {
+ ecode = NTDB_ERR_OOM;
+ } else {
+ memcpy(data->dptr, keyp + key.dsize, data->dsize);
ecode = NTDB_SUCCESS;
+ }
+ ntdb_access_release(ntdb, keyp);
}
ntdb_unlock_hash(ntdb, h.h, F_RDLCK);
struct ntdb_used_record rec;
struct hash_info h;
- off = find_and_lock(ntdb, key, F_RDLCK, &h, &rec);
+ off = find_and_lock(ntdb, key, F_RDLCK, &h, &rec, NULL);
if (NTDB_OFF_IS_ERR(off)) {
return false;
}
struct hash_info h;
enum NTDB_ERROR ecode;
- off = find_and_lock(ntdb, key, F_WRLCK, &h, &rec);
+ off = find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL);
if (NTDB_OFF_IS_ERR(off)) {
return NTDB_OFF_TO_ERR(off);
}
struct ntdb_used_record rec;
struct hash_info h;
enum NTDB_ERROR ecode;
+ const char *keyp;
- off = find_and_lock(ntdb, key, F_RDLCK, &h, &rec);
+ off = find_and_lock(ntdb, key, F_RDLCK, &h, &rec, &keyp);
if (NTDB_OFF_IS_ERR(off)) {
return NTDB_OFF_TO_ERR(off);
}
if (!off) {
ecode = NTDB_ERR_NOEXIST;
} else {
- const void *dptr;
- dptr = ntdb_access_read(ntdb, off + sizeof(rec) + key.dsize,
- rec_data_length(&rec), false);
- if (NTDB_PTR_IS_ERR(dptr)) {
- ecode = NTDB_PTR_ERR(dptr);
- } else {
- NTDB_DATA d = ntdb_mkdata(dptr, rec_data_length(&rec));
+ NTDB_DATA d = ntdb_mkdata(keyp + key.dsize,
+ rec_data_length(&rec));
- ecode = parse(key, d, data);
- ntdb_access_release(ntdb, dptr);
- }
+ ecode = parse(key, d, data);
+ ntdb_access_release(ntdb, keyp);
}
ntdb_unlock_hash(ntdb, h.h, F_RDLCK);
NTDB_DATA key,
int ltype,
struct hash_info *h,
- struct ntdb_used_record *rec);
+ struct ntdb_used_record *rec,
+ const char **rkey);
enum NTDB_ERROR replace_in_hash(struct ntdb_context *ntdb,
const struct hash_info *h,
v = 0;
/* Should not find it. */
- ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec) == 0);
+ ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == 0);
/* Should have created correct hash. */
ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
/* Should have located space in top table, bucket 0. */
ok1(ntdb_check(ntdb, NULL, NULL) == 0);
/* Now, this should give a successful lookup. */
- ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec) == new_off);
+ ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == new_off);
/* Should have created correct hash. */
ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
/* Should have located it in top table, bucket 0. */
/* Test expansion. */
v = 1;
- ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec) == 0);
+ ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == 0);
/* Should have created correct hash. */
ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
/* Should have located clash in toplevel bucket 0. */
/* Should be able to find both. */
v = 1;
- ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec) == new_off2);
+ ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == new_off2);
/* Should have created correct hash. */
ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
/* Should have located space in chain. */
ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);
v = 0;
- ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec) == new_off);
+ ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == new_off);
/* Should have created correct hash. */
ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
/* Should have located space in chain. */
/* Should still be able to find other record. */
v = 1;
- ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec) == new_off2);
+ ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == new_off2);
/* Should have created correct hash. */
ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
/* Should have located space in chain. */
/* Now should find empty space. */
v = 0;
- ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec) == 0);
+ ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == 0);
/* Should have created correct hash. */
ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
/* Should have located space in chain, bucket 0. */
/* Adding another record should work. */
v = 2;
- ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec) == 0);
+ ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == 0);
/* Should have created correct hash. */
ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
/* Should have located space in chain, bucket 0. */
/* Adding another record should cause expansion. */
v = 3;
- ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec) == 0);
+ ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == 0);
/* Should have created correct hash. */
ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
/* Should not have located space in chain. */
ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);
/* Retrieve it and check. */
- ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec) == new_off);
+ ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == new_off);
/* Should have created correct hash. */
ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
/* Should have appended to chain, bucket 2. */
/* YA record: relocation. */
v = 4;
- ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec) == 0);
+ ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == 0);
/* Should have created correct hash. */
ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
/* Should not have located space in chain. */
ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);
/* Retrieve it and check. */
- ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec) == new_off);
+ ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == new_off);
/* Should have created correct hash. */
ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
/* Should have appended to chain, bucket 2. */
struct ntdb_used_record urec;
struct hash_info h;
- off = find_and_lock(ntdb, key, F_RDLCK, &h, &urec);
+ off = find_and_lock(ntdb, key, F_RDLCK, &h, &urec, NULL);
if (NTDB_OFF_IS_ERR(off))
return 0;
ntdb_unlock_hash(ntdb, h.h, F_RDLCK);
ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
/* Make sure it put it at end as we expected. */
- off = find_and_lock(ntdb, k, F_RDLCK, &h, &rec);
+ off = find_and_lock(ntdb, k, F_RDLCK, &h, &rec, NULL);
ok1(off >= ALMOST_4G);
ntdb_unlock_hash(ntdb, h.h, F_RDLCK);
struct ntdb_used_record rec;
ntdb_off_t off;
- off = find_and_lock(ntdb, *key, F_RDLCK, &h, &rec);
+ off = find_and_lock(ntdb, *key, F_RDLCK, &h, &rec, NULL);
ntdb->free_fn(key->dptr, ntdb->alloc_data);
if (NTDB_OFF_IS_ERR(off)) {
return NTDB_OFF_TO_ERR(off);