more comments for hashindex_lookup

This commit is contained in:
Thomas Waldmann 2023-02-08 15:33:58 +01:00
parent 9bf352d00c
commit 0098ac9e63
No known key found for this signature in database
GPG Key ID: 243ACFA951F78E01
1 changed files with 12 additions and 4 deletions

View File

@ -160,22 +160,24 @@ static int
hashindex_lookup(HashIndex *index, const unsigned char *key, int *start_idx) hashindex_lookup(HashIndex *index, const unsigned char *key, int *start_idx)
{ {
int didx = -1; int didx = -1;
int start = hashindex_index(index, key); int start = hashindex_index(index, key); /* perfect index for this key, if there is no collision. */
int idx = start; int idx = start;
for(;;) { for(;;) {
if(BUCKET_IS_EMPTY(index, idx)) if(BUCKET_IS_EMPTY(index, idx))
{ {
break; break; /* if we encounter an empty bucket, we do not need to look any further. */
} }
if(BUCKET_IS_DELETED(index, idx)) { if(BUCKET_IS_DELETED(index, idx)) {
if(didx == -1) { if(didx == -1) {
didx = idx; didx = idx; /* remember the index of the first deleted bucket. */
} }
} }
else if(BUCKET_MATCHES_KEY(index, idx, key)) { else if(BUCKET_MATCHES_KEY(index, idx, key)) {
/* we found the bucket with the key we are looking for! */
if (didx != -1) { if (didx != -1) {
// note: although lookup is logically a read-only operation, // note: although lookup is logically a read-only operation,
// we optimize (change) the hashindex here "on the fly". // we optimize (change) the hashindex here "on the fly":
// swap this full bucket with a previous deleted/tombstone bucket.
memcpy(BUCKET_ADDR(index, didx), BUCKET_ADDR(index, idx), index->bucket_size); memcpy(BUCKET_ADDR(index, didx), BUCKET_ADDR(index, idx), index->bucket_size);
BUCKET_MARK_DELETED(index, idx); BUCKET_MARK_DELETED(index, idx);
idx = didx; idx = didx;
@ -187,10 +189,16 @@ hashindex_lookup(HashIndex *index, const unsigned char *key, int *start_idx)
idx -= index->num_buckets; idx -= index->num_buckets;
} }
if(idx == start) { if(idx == start) {
/* we have done a full pass over all buckets. */
break; break;
} }
} }
/* we get here if we did not find a bucket with the key we searched for. */
if (start_idx != NULL) { if (start_idx != NULL) {
/* by giving a non-NULL pointer in start_idx, caller can request to
* get the index of the first empty or deleted bucket we encountered,
* e.g. to add a new entry for that key into that bucket.
*/
(*start_idx) = (didx == -1) ? idx : didx; (*start_idx) = (didx == -1) ? idx : didx;
} }
return -1; return -1;