[FEATURE]Compile table must register plugin table to get compile ex_data & maat_state_get_compile_table_ids API
This commit is contained in:
109
src/maat_kv.c
109
src/maat_kv.c
@@ -12,14 +12,21 @@
|
||||
|
||||
#include "uthash/uthash.h"
|
||||
#include "maat_utils.h"
|
||||
#include "maat_kv.h"
|
||||
|
||||
#define MAAT_KV_MAX_KEY_LEN 512
|
||||
#define MAX_ENTITY_ID_CNT 4
|
||||
|
||||
struct kv_entity {
|
||||
long long id[MAX_ENTITY_ID_CNT];
|
||||
int id_cnt; //0~4
|
||||
};
|
||||
|
||||
struct maat_kv_pair
|
||||
{
|
||||
char* key; //must be lower case.
|
||||
size_t keylen;
|
||||
long long val;
|
||||
size_t key_len;
|
||||
struct kv_entity *val;
|
||||
UT_hash_handle hh;
|
||||
};
|
||||
|
||||
@@ -41,10 +48,27 @@ maat_kv_pair_new(const char* key, size_t keylen, long long value)
|
||||
struct maat_kv_pair *kv = ALLOC(struct maat_kv_pair, 1);
|
||||
|
||||
kv->key = ALLOC(char, keylen);
|
||||
kv->keylen = keylen;
|
||||
kv->val = value;
|
||||
kv->key_len = keylen;
|
||||
kv->val = ALLOC(struct kv_entity, 1);
|
||||
|
||||
strlowercase(key, keylen, kv->key, kv->keylen);
|
||||
strlowercase(key, keylen, kv->key, kv->key_len);
|
||||
kv->val->id[0] = value;
|
||||
kv->val->id_cnt = 1;
|
||||
|
||||
return kv;
|
||||
}
|
||||
|
||||
static struct maat_kv_pair *
|
||||
maat_kv_pair_clone(const char *key, size_t key_len, struct kv_entity *entity)
|
||||
{
|
||||
struct maat_kv_pair *kv = ALLOC(struct maat_kv_pair, 1);
|
||||
|
||||
kv->key = ALLOC(char, key_len);
|
||||
kv->key_len = key_len;
|
||||
kv->val = ALLOC(struct kv_entity, 1);
|
||||
|
||||
strlowercase(key, key_len, kv->key, kv->key_len);
|
||||
*(kv->val) = *entity;
|
||||
|
||||
return kv;
|
||||
}
|
||||
@@ -55,10 +79,14 @@ static void maat_kv_pair_free(struct maat_kv_pair *kv)
|
||||
return;
|
||||
}
|
||||
|
||||
if (kv->key != NULL) {
|
||||
if (kv->key != NULL) {
|
||||
FREE(kv->key);
|
||||
}
|
||||
|
||||
if (kv->val != NULL) {
|
||||
FREE(kv->val);
|
||||
}
|
||||
|
||||
FREE(kv);
|
||||
}
|
||||
|
||||
@@ -114,8 +142,8 @@ int maat_kv_register(struct maat_kv_store* store, const char *key, long long val
|
||||
return ret;
|
||||
}
|
||||
|
||||
int maat_kv_read_unNull(struct maat_kv_store *store, const char* key,
|
||||
size_t keylen, long long *value)
|
||||
int maat_kv_read_unNull(struct maat_kv_store *store, const char *key, size_t keylen,
|
||||
long long *value_array, size_t n_array)
|
||||
{
|
||||
struct maat_kv_pair *kv = NULL;
|
||||
char key_lowercase[MAAT_KV_MAX_KEY_LEN] = {0};
|
||||
@@ -126,36 +154,42 @@ int maat_kv_read_unNull(struct maat_kv_store *store, const char* key,
|
||||
|
||||
strlowercase(key, keylen, key_lowercase, sizeof(key_lowercase));
|
||||
HASH_FIND(hh, store->hash, key_lowercase, keylen, kv);
|
||||
|
||||
int i = 0;
|
||||
if (kv) {
|
||||
*value = kv->val;
|
||||
return 1;
|
||||
for (i = 0; i < kv->val->id_cnt && i < (int)n_array; i++) {
|
||||
value_array[i] = kv->val->id[i];
|
||||
}
|
||||
return i;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int maat_kv_read(struct maat_kv_store *store, const char * key, long long *value)
|
||||
int maat_kv_read(struct maat_kv_store *store, const char * key,
|
||||
long long *value_array, size_t n_array)
|
||||
{
|
||||
return maat_kv_read_unNull(store, key, strlen(key), value);
|
||||
return maat_kv_read_unNull(store, key, strlen(key), value_array, n_array);
|
||||
}
|
||||
|
||||
int maat_kv_write_unNull(struct maat_kv_store* store, const char* key,
|
||||
size_t keylen, long long value)
|
||||
size_t key_len, long long value)
|
||||
{
|
||||
struct maat_kv_pair *kv = NULL;
|
||||
char key_lowercase[MAAT_KV_MAX_KEY_LEN] = {0};
|
||||
|
||||
if (keylen > MAAT_KV_MAX_KEY_LEN) {
|
||||
if (key_len > MAAT_KV_MAX_KEY_LEN) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
strlowercase(key, keylen, key_lowercase, sizeof(key_lowercase));
|
||||
HASH_FIND(hh, store->hash, key_lowercase, keylen, kv);
|
||||
strlowercase(key, key_len, key_lowercase, sizeof(key_lowercase));
|
||||
HASH_FIND(hh, store->hash, key_lowercase, key_len, kv);
|
||||
if (kv) {
|
||||
kv->val = value;
|
||||
kv->val->id[0] = value;
|
||||
kv->val->id_cnt = 1;
|
||||
} else {
|
||||
kv = maat_kv_pair_new(key, keylen, value);
|
||||
HASH_ADD_KEYPTR(hh, store->hash, kv->key, keylen, kv);
|
||||
kv = maat_kv_pair_new(key, key_len, value);
|
||||
HASH_ADD_KEYPTR(hh, store->hash, kv->key, key_len, kv);
|
||||
}
|
||||
|
||||
return 1;
|
||||
@@ -166,14 +200,47 @@ int maat_kv_write(struct maat_kv_store *store, const char *key, long long value)
|
||||
return maat_kv_write_unNull(store, key, strlen(key), value);
|
||||
}
|
||||
|
||||
int maat_kv_append_unNull(struct maat_kv_store* store, const char* key,
|
||||
size_t key_len, long long value)
|
||||
{
|
||||
struct maat_kv_pair *kv = NULL;
|
||||
char key_lowercase[MAAT_KV_MAX_KEY_LEN] = {0};
|
||||
|
||||
if (key_len > MAAT_KV_MAX_KEY_LEN) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
strlowercase(key, key_len, key_lowercase, sizeof(key_lowercase));
|
||||
HASH_FIND(hh, store->hash, key_lowercase, key_len, kv);
|
||||
if (kv) {
|
||||
size_t id_cnt = kv->val->id_cnt;
|
||||
if (id_cnt >= MAX_ENTITY_ID_CNT) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
kv->val->id[id_cnt] = value;
|
||||
kv->val->id_cnt++;
|
||||
} else {
|
||||
kv = maat_kv_pair_new(key, key_len, value);
|
||||
HASH_ADD_KEYPTR(hh, store->hash, kv->key, key_len, kv);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int maat_kv_append(struct maat_kv_store *store, const char *key, long long value)
|
||||
{
|
||||
return maat_kv_append_unNull(store, key, strlen(key), value);
|
||||
}
|
||||
|
||||
struct maat_kv_store *maat_kv_store_duplicate(struct maat_kv_store *origin_map)
|
||||
{
|
||||
struct maat_kv_store *target = maat_kv_store_new();
|
||||
struct maat_kv_pair *kv = NULL, *tmp_kv = NULL, *copy_kv = NULL;
|
||||
|
||||
HASH_ITER (hh, origin_map->hash, kv, tmp_kv) {
|
||||
copy_kv = maat_kv_pair_new(kv->key, kv->keylen, kv->val);
|
||||
HASH_ADD_KEYPTR(hh, target->hash, copy_kv->key, copy_kv->keylen, copy_kv);
|
||||
copy_kv = maat_kv_pair_clone(kv->key, kv->key_len, kv->val);
|
||||
HASH_ADD_KEYPTR(hh, target->hash, copy_kv->key, copy_kv->key_len, copy_kv);
|
||||
}
|
||||
|
||||
return target;
|
||||
|
||||
Reference in New Issue
Block a user