Skip to content

Commit

Permalink
implemented all the basic funtions
Browse files Browse the repository at this point in the history
  • Loading branch information
SudeepRed committed Aug 9, 2023
1 parent 131f988 commit 499a08f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 52 deletions.
61 changes: 30 additions & 31 deletions src/include/pgagroal.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ extern "C" {
#define unlikely(x) __builtin_expect (!!(x), 0)

#define MAX(a, b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })

#define MIN(a, b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })

/*
* Common piece of code to perform a sleeping.
Expand All @@ -195,13 +195,13 @@ extern "C" {
*
*/
#define SLEEP(zzz) \
do \
{ \
struct timespec ts_private; \
ts_private.tv_sec = 0; \
ts_private.tv_nsec = zzz; \
nanosleep(&ts_private, NULL); \
} while (0);
do \
{ \
struct timespec ts_private; \
ts_private.tv_sec = 0; \
ts_private.tv_nsec = zzz; \
nanosleep(&ts_private, NULL); \
} while (0);

/*
* Commonly used block of code to sleep
Expand All @@ -218,14 +218,14 @@ extern "C" {
SLEEP_AND_GOTO(100000L, retry)
*/
#define SLEEP_AND_GOTO(zzz, goto_to) \
do \
{ \
struct timespec ts_private; \
ts_private.tv_sec = 0; \
ts_private.tv_nsec = zzz; \
nanosleep(&ts_private, NULL); \
goto goto_to; \
} while (0);
do \
{ \
struct timespec ts_private; \
ts_private.tv_sec = 0; \
ts_private.tv_nsec = zzz; \
nanosleep(&ts_private, NULL); \
goto goto_to; \
} while (0);

/**
* The shared memory segment
Expand Down Expand Up @@ -407,7 +407,6 @@ struct prometheus

} __attribute__ ((aligned (64)));


/**
* A structure to handle the query response
* so that it is possible to serve the very same
Expand All @@ -423,16 +422,16 @@ struct prometheus
struct query_cache
{
atomic_schar lock; /**< lock to protect the cache */
size_t size; /**< size of the cache */
struct hashTable *table;
size_t size; /**< size of the cache */
struct hashTable* table;
} __attribute__ ((aligned (64)));
struct hashTable
{
char key;
time_t valid_until;
char data;
UT_hash_handle hh;
} __attribute__ ((aligned (64)));
struct hashTable {
char key;
time_t valid_until;
char data;
UT_hash_handle hh;
}__attribute__ ((aligned (64)));


/** @struct
* Defines the configuration and state of pgagroal
Expand Down
14 changes: 8 additions & 6 deletions src/include/query_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
get method: (impletent on ur own)
*/
#include <pgagroal.h>
#include <stdlib.h>

/**
Expand All @@ -66,17 +67,18 @@
int
pgagroal_query_cache_init(size_t* p_size, void** p_shmem);

int //get the query cache
pgagroal_query_cache_get(void* p_shmem, void* p_query, size_t p_query_size, void** p_result, size_t* p_result_size);
//get the query cache
struct hashTable*
pgagroal_query_cache_get(struct hashTable* Table, char key);

int //invalidate the query cache
pgagroal_query_cache_invalidate(void* p_shmem, void* p_query, size_t p_query_size);
pgagroal_query_cache_invalidate(struct hashTable* Table, char key);

int //update the query cache
pgagroal_query_cache_update(void* p_shmem, void* p_query, size_t p_query_size, void* p_result, size_t p_result_size);
pgagroal_query_cache_update(struct hashTable* Table, char key, char data);

int //add a new query to the cache
pgagroal_query_cache_add(void* p_shmem, char data, char key);
pgagroal_query_cache_add(struct hashTable* Table, char data, char key);

int //define a cache invalidator function
pgagroal_query_cache_clear(void* p_shmem, void* p_query, size_t p_query_size);
pgagroal_query_cache_clear(struct hashTable* Table);
76 changes: 61 additions & 15 deletions src/libpgagroal/query_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <query_cache.h>
#include <utils.h>
#include <shmem.h>
#include <uthash.h>

/* system */
#include <ev.h>
Expand All @@ -60,6 +61,7 @@ int
pgagroal_query_cache_init(size_t* p_size, void** p_shmem)
{
struct query_cache* cache;
// initalizing the hash table
struct configuration* config;

size_t cache_size = 0;
Expand All @@ -79,6 +81,8 @@ pgagroal_query_cache_init(size_t* p_size, void** p_shmem)

memset(cache, 0, struct_size + cache_size);
cache->size = cache_size;
struct hashTable* Table = NULL;
cache->table = Table;
atomic_init(&cache->lock, STATE_FREE);

// success! do the memory swap
Expand All @@ -95,23 +99,65 @@ pgagroal_query_cache_init(size_t* p_size, void** p_shmem)

return 1;
}
//get the query cache
struct hashTable*
pgagroal_query_cache_get(struct hashTable* Table, char key)
{
struct hashTable* s;
HASH_FIND_INT(Table, &key, s);
return s;
}

int //get the query cache
pgagroal_query_cache_get(void* p_shmem, void* p_query, size_t p_query_size, void** p_result, size_t* p_result_size);

int //invalidate the query cache
pgagroal_query_cache_invalidate(void* p_shmem, void* p_query, size_t p_query_size);

int //update the query cache
pgagroal_query_cache_update(void* p_shmem, void* p_query, size_t p_query_size, void* p_result, size_t p_result_size);
//invalidate the query cache
int
pgagroal_query_cache_invalidate(struct hashTable* Table, char key)
{
struct hashTable* s;
HASH_FIND_INT(Table, &key, s);
if (s == NULL)
{
return 0;
}
HASH_DEL(Table, s);
free(s);
return 1;
}

int //add a new query to the cache
pgagroal_query_cache_add(void** p_shmem, char data, char key);
// struct query_cache* cache = *p_shmem;
// cache->table.
// return 1;
//update the query cache
int
pgagroal_query_cache_update(struct hashTable* Table, char key, char data)
{
struct hashTable* s;
HASH_FIND_INT(Table, &key, s);
if (s == NULL)
{
return 0;
}
s->data = data;
return 1;
}

int
//add a new query to the cache
pgagroal_query_cache_add(struct hashTable* Table, char data, char key)
{
struct hashTable* obj = (struct hashTable*)malloc(sizeof(struct hashTable));
obj->key = key;
obj->data = data;

HASH_ADD_INT(Table, key, obj);
return 1;
}

int //define a cache invalidator function
pgagroal_query_cache_clear(void* p_shmem, void* p_query, size_t p_query_size);
//Clear the entire hashtable and free the memeory
int
pgagroal_query_cache_clear(struct hashTable* Table)
{
struct hashTable* current_item, * tmp;
HASH_ITER(hh, Table, current_item, tmp)
{
HASH_DEL(Table, current_item);
free(current_item);
}
return 1;
}

0 comments on commit 499a08f

Please sign in to comment.