2018-08-28 14:43:46 +00:00
|
|
|
#ifndef ODYSSEY_ID_H
|
|
|
|
#define ODYSSEY_ID_H
|
2017-06-20 13:33:20 +00:00
|
|
|
|
|
|
|
/*
|
2018-03-12 14:03:15 +00:00
|
|
|
* Odyssey.
|
2017-06-20 13:33:20 +00:00
|
|
|
*
|
2018-04-04 13:19:58 +00:00
|
|
|
* Scalable PostgreSQL connection pooler.
|
2020-04-02 11:00:56 +00:00
|
|
|
*/
|
2017-06-20 13:33:20 +00:00
|
|
|
|
2019-01-30 09:30:31 +00:00
|
|
|
#include "machinarium.h"
|
|
|
|
|
2020-04-02 11:00:56 +00:00
|
|
|
typedef struct od_id od_id_t;
|
2018-08-28 14:43:46 +00:00
|
|
|
typedef struct od_id_mgr od_id_mgr_t;
|
2017-06-20 13:33:20 +00:00
|
|
|
|
2017-06-20 15:33:32 +00:00
|
|
|
#define OD_ID_SEEDMAX 6
|
|
|
|
|
2017-06-20 13:33:20 +00:00
|
|
|
struct od_id
|
|
|
|
{
|
2020-04-02 11:00:56 +00:00
|
|
|
char *id_prefix;
|
|
|
|
char id[OD_ID_SEEDMAX * 2];
|
2017-11-24 12:19:14 +00:00
|
|
|
uint64_t id_a;
|
|
|
|
uint64_t id_b;
|
2017-06-20 13:33:20 +00:00
|
|
|
};
|
|
|
|
|
2019-01-30 09:30:31 +00:00
|
|
|
static inline int
|
|
|
|
od_id_cmp(od_id_t *a, od_id_t *b)
|
2017-06-20 13:33:20 +00:00
|
|
|
{
|
2019-01-30 09:30:31 +00:00
|
|
|
return memcmp(a->id, b->id, sizeof(a->id)) == 0;
|
|
|
|
}
|
2017-06-20 13:33:20 +00:00
|
|
|
|
2019-01-30 09:30:31 +00:00
|
|
|
static inline void
|
|
|
|
od_id_generate(od_id_t *id, char *prefix)
|
2017-06-20 15:33:32 +00:00
|
|
|
{
|
2019-01-30 09:30:31 +00:00
|
|
|
long int a = machine_lrand48();
|
|
|
|
long int b = machine_lrand48();
|
|
|
|
|
|
|
|
char seed[OD_ID_SEEDMAX];
|
|
|
|
memcpy(seed + 0, &a, 4);
|
|
|
|
memcpy(seed + 4, &b, 2);
|
|
|
|
|
|
|
|
id->id_prefix = prefix;
|
2020-04-02 11:00:56 +00:00
|
|
|
id->id_a = a;
|
|
|
|
id->id_b = b;
|
2019-01-30 09:30:31 +00:00
|
|
|
|
|
|
|
static const char *hex = "0123456789abcdef";
|
|
|
|
int q, w;
|
|
|
|
for (q = 0, w = 0; q < OD_ID_SEEDMAX; q++) {
|
|
|
|
id->id[w++] = hex[(seed[q] >> 4) & 0x0F];
|
2020-04-02 11:00:56 +00:00
|
|
|
id->id[w++] = hex[(seed[q]) & 0x0F];
|
2019-01-30 09:30:31 +00:00
|
|
|
}
|
|
|
|
assert(w == (OD_ID_SEEDMAX * 2));
|
2017-06-20 15:33:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 09:30:31 +00:00
|
|
|
void
|
2020-06-14 17:02:07 +00:00
|
|
|
od_id_generator_seed(void);
|
2019-01-30 09:30:31 +00:00
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
#endif /* ODYSSEY_ID_H */
|