Skip to content

Commit

Permalink
[WORKING]: uthash implmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
SudeepRed committed Nov 6, 2023
1 parent 7a8438a commit 4170940
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 106 deletions.
3 changes: 1 addition & 2 deletions src/include/pgagroal.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,7 @@ struct query_cache

struct hashTable
{
struct hashEntry* key;
time_t valid_until;
char key[1024];
struct hashEntry* data;
UT_hash_handle hh;
} __attribute__ ((aligned (64)));
Expand Down
4 changes: 2 additions & 2 deletions src/include/query_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pgagroal_query_cache_init(size_t* p_size, void** p_shmem);
* @warning The caller should ensure the validity of the 'Table' pointer and the 'key' pointer.
*/
struct hashEntry*
pgagroal_query_cache_get(struct query_cache* cache, struct hashTable** Table, struct hashEntry* key);
pgagroal_query_cache_get(struct query_cache* cache, struct hashTable** Table, char* key);

/**
* Invalidate a cache entry and remove it from the hash table.
Expand Down Expand Up @@ -142,7 +142,7 @@ pgagroal_query_cache_update(struct hashTable** Table, struct hashEntry* key, str
* The caller should manage memory to avoid leaks.
*/
int
pgagroal_query_cache_add(struct query_cache* cache, struct hashTable** Table, struct hashEntry* data, struct hashEntry* key, int flag);
pgagroal_query_cache_add(struct query_cache* cache, struct hashTable** Table, struct hashEntry* data, char* key);

/**
* Clear all cache entries from the hash table and free associated memory.
Expand Down
74 changes: 9 additions & 65 deletions src/libpgagroal/pipeline_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,27 +339,13 @@ session_client(struct ev_loop* loop, struct ev_io* watcher, int revents)

client_query->key_length = key_length;
client_query->kind = msg->kind;
char key[1024];
strcpy(key, msg->data + 5);


struct hashEntry* key = NULL;
key = (struct hashEntry*)malloc(sizeof(struct hashEntry) + client_query->key_length + 1);

if (key == NULL)
{

client_inactive(wi->slot);
ev_break(loop, EVBREAK_ONE);
return;
}

memset(key->key, 0, client_query->key_length + 1);

memcpy(key->key, client_query->key, key_length);
key->key[client_query->key_length + 1] = '\0';

key->length = client_query->key_length;
struct hashEntry* s = pgagroal_query_cache_get(cache, &(cache->table), key);

if (s != NULL && s->value != NULL)
if (s != NULL)
{
// log cache hit
struct message* result = NULL;
Expand Down Expand Up @@ -552,60 +538,18 @@ session_server(struct ev_loop* loop, struct ev_io* watcher, int revents)
if (!pgagroal_extract_message('Z', msg, &tmp))
{

struct hashEntry* key, * data;
key = (struct hashEntry*)malloc(sizeof(struct hashEntry) + client_query->key_length + 1);
data = (struct hashEntry*)malloc(sizeof(struct hashEntry) + msg->length);

if (key == NULL || data == NULL)
{

if (key != NULL)
{
free(key);
key = NULL;
}
if (data != NULL)
{
free(data);
data = NULL;
}
client_inactive(wi->slot);
ev_break(loop, EVBREAK_ONE);

return;
}
char key[1024];
struct hashEntry* data = NULL;

strcpy(key, client_query->key);
data = (struct hashEntry*)malloc(sizeof(struct hashEntry) + msg->length);
data->value = malloc(msg->length);
memset(key->key, 0, client_query->key_length + 1);

if (data->value == NULL)
{

if (data->value != NULL)
{
free(data->value);
data->value = NULL;
}
if (data != NULL)
{
free(data);
data = NULL;
}
client_inactive(wi->slot);
ev_break(loop, EVBREAK_ONE);

return;
}

memcpy(key->key, client_query->key, client_query->key_length);
key->key[client_query->key_length + 1] = '\0';
key->length = client_query->key_length;

memset(data->value, 0, msg->length);
memcpy(data->value, msg->data, msg->length);
data->length = msg->length;

pgagroal_query_cache_add(cache, &(cache->table), data, key, 1);
pgagroal_query_cache_add(cache, &(cache->table), data, key);

}

Expand Down
54 changes: 17 additions & 37 deletions src/libpgagroal/query_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,14 @@ pgagroal_query_cache_init(size_t* p_size, void** p_shmem)
return 1;
}
struct hashEntry*
pgagroal_query_cache_get(struct query_cache* cache, struct hashTable** Table, struct hashEntry* key)
pgagroal_query_cache_get(struct query_cache* cache, struct hashTable** Table, char* key)
{
for (int i = 0; i < cache->max_elements; i++)
{

int x = strncmp(cache->cache[i].key->key, key->key, key->length);

if (x == 0)
{

return cache->cache[i].data;
}
struct hashTable* s = NULL;
HASH_FIND_STR(*Table, key, s);
if (s)
{
return s->data;
}
return NULL;

Expand All @@ -127,41 +123,25 @@ int
pgagroal_query_cache_update(struct hashTable** Table, struct hashEntry* key, struct hashEntry* data)
{

struct hashTable* s = NULL;
void* qkey = malloc(strlen(key->value) + 1);
strcpy(qkey, key->value);
HASH_FIND(hh, *Table, qkey, strlen(qkey), s);
HASH_DEL(*Table, s);
if (s == NULL)
{
return 0;
}
if (data->length > HASH_ENTRY_DATA_SIZE)
{
free(s);
return 0;
}

s->data = data;
HASH_ADD_KEYPTR(hh, *Table, s->key->value, strlen(s->key->value), s);
return 1;
}

int
pgagroal_query_cache_add(struct query_cache* cache, struct hashTable** Table, struct hashEntry* data, struct hashEntry* key, int flag)
pgagroal_query_cache_add(struct query_cache* cache, struct hashTable** Table, struct hashEntry* data, char* key)
{
if (cache->max_elements >= QUERY_CACHE_MAX_ENTRIES)
{
pgagroal_log_warn("Cache is full %d", cache->max_elements);

return -1;
struct hashTable* s = NULL;
HASH_FIND_STR(*Table, key, s);
if (s)
{
return 0;
}
s = (struct hashTable*)malloc(sizeof(struct hashTable));

int idx = cache->max_elements;

cache->cache[idx].key = key;
cache->cache[idx].data = data;
cache->max_elements = idx + 1;
memset(s->key, 0, 1024);
strcpy(s->key, key);
s->data = data;
HASH_ADD_STR(*Table, key, s);

return 1;

Expand Down

0 comments on commit 4170940

Please sign in to comment.