2020-07-08 06:26:17 +00:00
|
|
|
#ifndef ODYSSEY_COUNTER_H
|
|
|
|
#define ODYSSEY_COUNTER_H
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Odyssey.
|
|
|
|
*
|
|
|
|
* Scalable PostgreSQL connection pooler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* llist stands for linked list */
|
|
|
|
typedef struct od_hash_litem od_counter_litem_t;
|
|
|
|
typedef struct od_hash_llist od_counter_llist_t;
|
|
|
|
|
|
|
|
typedef size_t od_hash_table_key_t;
|
|
|
|
/* support only int values for now */
|
|
|
|
/* TODO: change value type to (void *) */
|
|
|
|
typedef size_t od_counter_item_t;
|
|
|
|
typedef size_t od_count_t;
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
struct od_hash_litem {
|
2020-07-08 06:26:17 +00:00
|
|
|
od_counter_item_t value;
|
|
|
|
od_count_t cnt;
|
|
|
|
|
|
|
|
od_counter_litem_t *next;
|
|
|
|
};
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
struct od_hash_llist {
|
2020-07-08 06:26:17 +00:00
|
|
|
size_t count;
|
|
|
|
od_counter_litem_t *list;
|
|
|
|
};
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern od_counter_llist_t *od_counter_llist_create(void);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern void od_counter_llist_add(od_counter_llist_t *llist,
|
|
|
|
const od_counter_item_t *it);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern od_retcode_t od_counter_llist_free(od_counter_llist_t *l);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
|
|
|
#define OD_DEFAULT_HASH_TABLE_SIZE 15
|
2020-12-28 10:43:31 +00:00
|
|
|
typedef struct od_bucket {
|
2020-11-23 09:13:28 +00:00
|
|
|
od_counter_llist_t *l;
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
} od_bucket_t;
|
|
|
|
|
2020-07-08 06:26:17 +00:00
|
|
|
typedef struct od_counter od_counter_t;
|
2020-12-28 10:43:31 +00:00
|
|
|
struct od_counter {
|
2020-07-08 06:26:17 +00:00
|
|
|
size_t size;
|
2020-11-23 09:13:28 +00:00
|
|
|
// ISO C99 flexible array member
|
2020-11-25 10:17:15 +00:00
|
|
|
od_bucket_t *buckets[FLEXIBLE_ARRAY_MEMBER];
|
2020-07-08 06:26:17 +00:00
|
|
|
};
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern od_counter_t *od_counter_create(size_t sz);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern od_counter_t *od_counter_create_default(void);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern od_retcode_t od_counter_free(od_counter_t *t);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern od_count_t od_counter_get_count(od_counter_t *t,
|
|
|
|
od_counter_item_t value);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern od_retcode_t od_counter_reset(od_counter_t *t, od_counter_item_t value);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern od_retcode_t od_counter_reset_all(od_counter_t *t);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern od_retcode_t od_counter_erase(od_counter_t *t, od_hash_table_key_t key,
|
|
|
|
od_counter_llist_t *item);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern void od_counter_inc(od_counter_t *t, od_counter_item_t item);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
static inline od_counter_item_t od_hash_item(od_counter_t *t,
|
|
|
|
od_counter_item_t item)
|
2020-07-08 06:26:17 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* How to get hash of number in range 0 to MAXINT ?
|
|
|
|
* there are many ways, we choose simplest one
|
|
|
|
*/
|
|
|
|
return item % t->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* these are function to perform dynamic counter resize in case of overfit
|
|
|
|
* TODO: implement them
|
|
|
|
* */
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern od_retcode_t od_counter_resize_up(od_counter_t *t);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
extern od_retcode_t od_counter_resize_down(od_counter_t *t);
|
2020-07-08 06:26:17 +00:00
|
|
|
|
|
|
|
#endif // ODYSSEY_COUNTER_H
|