Insert new buckets into linked list for for linkmap

This commit is contained in:
Nathan Fisher 2023-08-18 10:42:29 -04:00
parent 6a58dd2a63
commit e266346bf2

View file

@ -48,6 +48,7 @@ haggis_bucket* haggis_bucket_init(ino_t inode, uint64_t hash, char * path) {
bucket->key.val = inode; bucket->key.val = inode;
bucket->hash = hash; bucket->hash = hash;
bucket->path = path; bucket->path = path;
bucket->next = NULL;
return bucket; return bucket;
} }
@ -110,7 +111,7 @@ char* haggis_linkmap_get_or_add(haggis_linkmap *map, ino_t inode, char * path) {
} key; } key;
char * target = NULL; char * target = NULL;
size_t idx, hash; size_t idx, hash;
int i; haggis_bucket *b;
pthread_mutex_lock(&map->mutex); pthread_mutex_lock(&map->mutex);
if (map->len >= map->capacity) if (map->len >= map->capacity)
@ -118,23 +119,19 @@ char* haggis_linkmap_get_or_add(haggis_linkmap *map, ino_t inode, char * path) {
key.val = inode; key.val = inode;
hash = hash_fnv1a_64(key.bytes, sizeof(ino_t)); hash = hash_fnv1a_64(key.bytes, sizeof(ino_t));
idx = map->capacity % hash; idx = map->capacity % hash;
for (i = 0; i < map->capacity; i++) { if (map->buckets[idx].key.val == inode) {
if (map->buckets[idx].key.val == 0) { target = strndup(target, PATH_MAX - 1);
pthread_mutex_unlock(&map->mutex);
} else if (map->buckets[idx].key.val == 0) {
map->buckets[idx].key.val = inode; map->buckets[idx].key.val = inode;
map->buckets[idx].hash = hash; map->buckets[idx].hash = hash;
map->buckets[idx].path = path; map->buckets[idx].path = path;
map->len++; map->len++;
pthread_mutex_unlock(&map->mutex); pthread_mutex_unlock(&map->mutex);
break; } else {
} else if (map->buckets[idx].key.val == inode) { b = haggis_bucket_init(key.val, hash, path);
target = strndup(target, PATH_MAX - 1); if (b == NULL) return NULL;
pthread_mutex_unlock(&map->mutex); haggis_bucket_append(&map->buckets[idx], b);
break;
}
if (idx == map->capacity - 1)
idx = 0;
else
idx++;
} }
return target; return target;
} }