forked from lda/telodendria
Add HashMapGetKey() so we can free bucket keys before deleting them.
This commit is contained in:
parent
feb11de6b0
commit
46fe667988
3 changed files with 39 additions and 0 deletions
2
src/Db.c
2
src/Db.c
|
@ -293,6 +293,7 @@ DbCacheEvict(Db * db)
|
||||||
pthread_mutex_destroy(&ref->lock);
|
pthread_mutex_destroy(&ref->lock);
|
||||||
|
|
||||||
hash = DbHashKey(ref->name);
|
hash = DbHashKey(ref->name);
|
||||||
|
Free(HashMapGetKey(db->cache, hash));
|
||||||
HashMapDelete(db->cache, hash);
|
HashMapDelete(db->cache, hash);
|
||||||
Free(hash);
|
Free(hash);
|
||||||
|
|
||||||
|
@ -621,6 +622,7 @@ DbDelete(Db * db, size_t nArgs,...)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&ref->lock);
|
pthread_mutex_lock(&ref->lock);
|
||||||
|
|
||||||
|
Free(HashMapGetKey(db->cache, hash));
|
||||||
HashMapDelete(db->cache, hash);
|
HashMapDelete(db->cache, hash);
|
||||||
JsonFree(ref->json);
|
JsonFree(ref->json);
|
||||||
StringArrayFree(ref->name);
|
StringArrayFree(ref->name);
|
||||||
|
|
|
@ -244,6 +244,40 @@ HashMapGet(HashMap * map, const char *key)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
HashMapGetKey(HashMap * map, const char *key)
|
||||||
|
{
|
||||||
|
unsigned long hash;
|
||||||
|
size_t index;
|
||||||
|
|
||||||
|
if (!map || !key)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
hash = map->hashFunc(key);
|
||||||
|
index = hash % map->capacity;
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
HashMapBucket *bucket = map->entries[index];
|
||||||
|
|
||||||
|
if (!bucket)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bucket->hash == hash)
|
||||||
|
{
|
||||||
|
return bucket->key;
|
||||||
|
}
|
||||||
|
|
||||||
|
index = (index + 1) % map->capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
HashMapIterate(HashMap * map, char **key, void **value)
|
HashMapIterate(HashMap * map, char **key, void **value)
|
||||||
{
|
{
|
||||||
|
|
|
@ -42,6 +42,9 @@ extern void *
|
||||||
extern void *
|
extern void *
|
||||||
HashMapGet(HashMap *, const char *);
|
HashMapGet(HashMap *, const char *);
|
||||||
|
|
||||||
|
extern void *
|
||||||
|
HashMapGetKey(HashMap *, const char *);
|
||||||
|
|
||||||
extern void *
|
extern void *
|
||||||
HashMapDelete(HashMap *, const char *);
|
HashMapDelete(HashMap *, const char *);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue