Skip to content

Instantly share code, notes, and snippets.

@ayende
Last active November 28, 2019 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayende/d8ece5d0b24abef55f9e8cfea7b94101 to your computer and use it in GitHub Desktop.
Save ayende/d8ece5d0b24abef55f9e8cfea7b94101 to your computer and use it in GitHub Desktop.
bool hash_table_delete(hash_ctx_t* ctx, uint64_t key, hash_old_value_t* old_value) {
uint32_t bucket_idx = hash_table_bucket_number(ctx, key);
hash_bucket_t* b = ctx->dir->buckets[bucket_idx];
uint32_t piece_idx = key % NUMBER_OF_HASH_BUCKET_PIECES;
ctx->dir->version++;
if (old_value)
old_value->exists = false;
for (size_t i = 0; i < MAX_CHAIN_LENGTH; i++)
{
hash_bucket_piece_t* p = &b->pieces[(piece_idx + i) % NUMBER_OF_HASH_BUCKET_PIECES];
uint8_t* buf = p->data;
uint8_t* end = p->data + p->bytes_used;
while (buf < end)
{
uint64_t k, v;
uint8_t* cur_buf_start = buf;
varint_decode(&buf, &k);
varint_decode(&buf, &v);
if (k == key) {
if (old_value) {
old_value->exists = true;
old_value->value = v;
}
ptrdiff_t diff = buf - cur_buf_start;
memmove(cur_buf_start, buf, end - buf);
p->bytes_used -= (uint8_t)diff;
b->number_of_entries--;
ctx->dir->number_of_entries--;
return true;
}
}
// if we are looking at an overflow page, move to the next one and try to find it there
if (!p->overflowed)
break;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment