/** * Tests basic functionality for cuckoo_insert and cuckoo_get with small key/val. Checks that the * commands succeed and that the item returned is well-formed. */ void test_insert_basic(uint32_t policy, bool cas) { #define KEY "key" #define VAL "value" struct bstring key, testval; struct val val; rstatus_i status; struct item *it; test_reset(policy, cas); key.data = KEY; key.len = sizeof(KEY) - 1; val.type = VAL_TYPE_STR; val.vstr.data = VAL; val.vstr.len = sizeof(VAL) - 1; time_update(); status = cuckoo_insert(&key, &val, UINT32_MAX - 1); ck_assert_msg(status == CC_OK, "cuckoo_insert not OK - return status %d", status); it = cuckoo_get(&key); ck_assert_msg(it != NULL, "cuckoo_get returned NULL"); ck_assert_int_eq(it->vlen, sizeof(VAL) - 1); ck_assert_int_eq(it->klen, sizeof(KEY) - 1); item_value_str(&testval, it); ck_assert_int_eq(it->vlen, testval.len); ck_assert_int_eq(cc_memcmp(testval.data, VAL, testval.len), 0); #undef KEY #undef VAL }
void hashtable_delete(const char *key, uint32_t klen, struct hash_table *ht) { struct item_slh *bucket; struct item *it, *prev; ASSERT(hashtable_get(key, klen, ht) != NULL); bucket = _get_bucket(key, klen, ht); for (prev = NULL, it = SLIST_FIRST(bucket); it != NULL; prev = it, it = SLIST_NEXT(it, i_sle)) { /* iterate through bucket to find item to be removed */ if ((klen == it->klen) && cc_memcmp(key, item_key(it), klen) == 0) { /* found item */ break; } } if (prev == NULL) { SLIST_REMOVE_HEAD(bucket, i_sle); } else { SLIST_REMOVE_AFTER(prev, i_sle); } --(ht->nhash_item); }
static void test_assert_entry_exists(struct bstring *key, struct val *val) { struct item *it = cuckoo_get(key); ck_assert_msg(it != NULL, "cuckoo_get returned NULL"); ck_assert_int_eq(it->vlen, val->vstr.len); ck_assert_int_eq(it->klen, key->len); ck_assert_int_eq(it->vlen, val->vstr.len); struct bstring testval; item_value_str(&testval, it); ck_assert_int_eq(it->vlen, testval.len); ck_assert_int_eq(cc_memcmp(testval.data, val->vstr.data, testval.len), 0); }
struct item * hashtable_get(const char *key, uint32_t klen, struct hash_table *ht) { struct item_slh *bucket; struct item *it; ASSERT(key != NULL); ASSERT(klen != 0); bucket = _get_bucket(key, klen, ht); /* iterate through bucket looking for item */ for (it = SLIST_FIRST(bucket); it != NULL; it = SLIST_NEXT(it, i_sle)) { if ((klen == it->klen) && cc_memcmp(key, item_key(it), klen) == 0) { /* found item */ return it; } } return NULL; }