int main(int argc, char *argv[]) { unsigned int i; struct ntdb_used_record rec; struct ntdb_context ntdb = { .log_fn = tap_log_fn }; plan_tests(64 + 32 + 48*5 + 1); /* We should be able to encode any data value. */ for (i = 0; i < 64; i++) ok1(set_header(&ntdb, &rec, NTDB_USED_MAGIC, 0, 1ULL << i, 1ULL << i) == 0); /* And any key and data with < 64 bits between them. */ for (i = 0; i < 32; i++) { ntdb_len_t dlen = 1ULL >> (63 - i), klen = 1ULL << i; ok1(set_header(&ntdb, &rec, NTDB_USED_MAGIC, klen, dlen, klen + dlen) == 0); } /* We should neatly encode all values. */ for (i = 0; i < 48; i++) { uint64_t klen = 1ULL << (i < 16 ? i : 15); uint64_t dlen = 1ULL << i; uint64_t xlen = 1ULL << (i < 32 ? i : 31); ok1(set_header(&ntdb, &rec, NTDB_USED_MAGIC, klen, dlen, klen+dlen+xlen) == 0); ok1(rec_key_length(&rec) == klen); ok1(rec_data_length(&rec) == dlen); ok1(rec_extra_padding(&rec) == xlen); ok1(rec_magic(&rec) == NTDB_USED_MAGIC); } ok1(tap_log_messages == 0); return exit_status(); }
enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key) { tdb_off_t off; struct tdb_used_record rec; struct hash_info h; enum TDB_ERROR ecode; if (tdb->flags & TDB_VERSION1) { if (tdb1_delete(tdb, key) == -1) return tdb->last_error; return TDB_SUCCESS; } off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL); if (TDB_OFF_IS_ERR(off)) { return tdb->last_error = TDB_OFF_TO_ERR(off); } if (!off) { ecode = TDB_ERR_NOEXIST; goto unlock; } ecode = delete_from_hash(tdb, &h); if (ecode != TDB_SUCCESS) { goto unlock; } /* Free the deleted entry. */ tdb->stats.frees++; ecode = add_free_record(tdb, off, sizeof(struct tdb_used_record) + rec_key_length(&rec) + rec_data_length(&rec) + rec_extra_padding(&rec), TDB_LOCK_WAIT, true); if (tdb->flags & TDB_SEQNUM) tdb_inc_seqnum(tdb); unlock: tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK); return tdb->last_error = ecode; }
_PUBLIC_ enum NTDB_ERROR ntdb_delete(struct ntdb_context *ntdb, NTDB_DATA key) { ntdb_off_t off; struct ntdb_used_record rec; struct hash_info h; enum NTDB_ERROR ecode; off = find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL); if (NTDB_OFF_IS_ERR(off)) { return NTDB_OFF_TO_ERR(off); } if (!off) { ecode = NTDB_ERR_NOEXIST; goto unlock; } ecode = delete_from_hash(ntdb, &h); if (ecode != NTDB_SUCCESS) { goto unlock; } /* Free the deleted entry. */ ntdb->stats.frees++; ecode = add_free_record(ntdb, off, sizeof(struct ntdb_used_record) + rec_key_length(&rec) + rec_data_length(&rec) + rec_extra_padding(&rec), NTDB_LOCK_WAIT, true); if (ntdb->flags & NTDB_SEQNUM) ntdb_inc_seqnum(ntdb); unlock: ntdb_unlock_hash(ntdb, h.h, F_WRLCK); return ecode; }
static tdb_bool_err key_matches(struct tdb_context *tdb, const struct tdb_used_record *rec, tdb_off_t off, const struct tdb_data *key) { tdb_bool_err ret = false; const char *rkey; if (rec_key_length(rec) != key->dsize) { tdb->stats.compare_wrong_keylen++; return ret; } rkey = tdb_access_read(tdb, off + sizeof(*rec), key->dsize, false); if (TDB_PTR_IS_ERR(rkey)) { return TDB_PTR_ERR(rkey); } if (memcmp(rkey, key->dptr, key->dsize) == 0) ret = true; else tdb->stats.compare_wrong_keycmp++; tdb_access_release(tdb, rkey); return ret; }
uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off) { const struct tdb_used_record *r; const void *key; uint64_t klen, hash; r = tdb_access_read(tdb, off, sizeof(*r), true); if (TDB_PTR_IS_ERR(r)) { /* FIXME */ return 0; } klen = rec_key_length(r); tdb_access_release(tdb, r); key = tdb_access_read(tdb, off + sizeof(*r), klen, false); if (TDB_PTR_IS_ERR(key)) { return 0; } hash = tdb_hash(tdb, key, klen); tdb_access_release(tdb, key); return hash; }