2022-11-17 05:05:35 +08:00
|
|
|
/*
|
|
|
|
|
**********************************************************************************************
|
|
|
|
|
* File: maat_ex_data.cpp
|
|
|
|
|
* Description: ex data
|
|
|
|
|
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
|
|
|
|
* Date: 2022-10-31
|
|
|
|
|
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
|
|
|
|
***********************************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2022-11-25 16:32:29 +08:00
|
|
|
#include <stdio.h>
|
2022-11-17 05:05:35 +08:00
|
|
|
|
|
|
|
|
#include "uthash/uthash.h"
|
|
|
|
|
#include "uthash/utarray.h"
|
2022-12-09 17:12:18 +08:00
|
|
|
#include "log/log.h"
|
2022-11-17 05:05:35 +08:00
|
|
|
#include "maat_utils.h"
|
2022-11-25 16:32:29 +08:00
|
|
|
#include "maat_ex_data.h"
|
2022-11-17 05:05:35 +08:00
|
|
|
|
2022-12-09 17:12:18 +08:00
|
|
|
#define MODULE_EX_DATA module_name_str("maat.ex_data")
|
|
|
|
|
|
2022-11-17 05:05:35 +08:00
|
|
|
struct ex_data_runtime {
|
|
|
|
|
UT_array *cache_rows;
|
|
|
|
|
size_t cache_row_num;
|
|
|
|
|
size_t cache_size;
|
|
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
struct rcu_hash_table *htable;
|
2023-03-15 11:36:54 +08:00
|
|
|
struct maat_garbage_bin *ref_garbage_bin;
|
2022-11-17 05:05:35 +08:00
|
|
|
int table_id;
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
struct log_handle *logger;
|
2022-11-17 05:05:35 +08:00
|
|
|
};
|
|
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
void cache_row_free(void *p)
|
2022-11-17 05:05:35 +08:00
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
free(*(char **)p);
|
2022-11-17 05:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UT_icd ut_cache_row_icd = {sizeof(char*), NULL, NULL, cache_row_free};
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
struct ex_data_runtime *
|
|
|
|
|
ex_data_runtime_new(int table_id, rcu_hash_data_free_fn *data_free_fn,
|
2023-03-15 11:36:54 +08:00
|
|
|
struct maat_garbage_bin *garbage_bin, struct log_handle *logger)
|
2022-11-17 05:05:35 +08:00
|
|
|
{
|
2023-03-15 13:30:39 +08:00
|
|
|
if (NULL == data_free_fn || NULL == garbage_bin ||
|
|
|
|
|
NULL == logger) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-17 05:05:35 +08:00
|
|
|
struct ex_data_runtime *ex_data_rt = ALLOC(struct ex_data_runtime, 1);
|
|
|
|
|
|
|
|
|
|
utarray_new(ex_data_rt->cache_rows, &ut_cache_row_icd);
|
2022-11-25 16:32:29 +08:00
|
|
|
ex_data_rt->htable = rcu_hash_new(data_free_fn);
|
|
|
|
|
ex_data_rt->table_id = table_id;
|
2023-03-15 11:36:54 +08:00
|
|
|
ex_data_rt->ref_garbage_bin = garbage_bin;
|
2023-01-30 21:59:35 +08:00
|
|
|
ex_data_rt->logger = logger;
|
2022-11-25 16:32:29 +08:00
|
|
|
|
|
|
|
|
return ex_data_rt;
|
2022-11-17 05:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ex_data_runtime_free(struct ex_data_runtime *ex_data_rt)
|
|
|
|
|
{
|
2022-11-25 16:32:29 +08:00
|
|
|
if (NULL == ex_data_rt) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-17 05:05:35 +08:00
|
|
|
if (ex_data_rt->cache_rows != NULL) {
|
|
|
|
|
utarray_free(ex_data_rt->cache_rows);
|
2023-02-15 11:53:46 +08:00
|
|
|
ex_data_rt->cache_rows = NULL;
|
|
|
|
|
ex_data_rt->cache_row_num = 0;
|
2022-11-17 05:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ex_data_rt->htable != NULL) {
|
2022-11-25 16:32:29 +08:00
|
|
|
rcu_hash_free(ex_data_rt->htable);
|
2022-11-17 05:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
2022-12-05 23:21:18 +08:00
|
|
|
FREE(ex_data_rt);
|
2022-11-25 16:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ex_data_runtime_commit(struct ex_data_runtime *ex_data_rt)
|
2023-02-23 19:08:26 +08:00
|
|
|
{
|
2023-03-15 13:30:39 +08:00
|
|
|
if (NULL == ex_data_rt) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
rcu_hash_commit(ex_data_rt->htable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ex_data_runtime_cache_row_put(struct ex_data_runtime *ex_data_rt, const char *row)
|
|
|
|
|
{
|
2023-03-15 13:30:39 +08:00
|
|
|
if (NULL == ex_data_rt || NULL == row) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
size_t len = strlen(row) + 1;
|
|
|
|
|
char* row_copy = ALLOC(char, len);
|
|
|
|
|
|
|
|
|
|
memcpy(row_copy, row, len);
|
|
|
|
|
ex_data_rt->cache_size += len;
|
|
|
|
|
utarray_push_back(ex_data_rt->cache_rows, &row_copy);
|
|
|
|
|
ex_data_rt->cache_row_num++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *ex_data_runtime_cached_row_get(struct ex_data_runtime *ex_data_rt, size_t index)
|
|
|
|
|
{
|
2023-03-15 13:30:39 +08:00
|
|
|
if (NULL == ex_data_rt) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char **row = NULL;
|
2022-11-25 16:32:29 +08:00
|
|
|
row = (const char **)utarray_eltptr(ex_data_rt->cache_rows, index);
|
|
|
|
|
return *row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t ex_data_runtime_cached_row_count(struct ex_data_runtime *ex_data_rt)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == ex_data_rt) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ex_data_rt->cache_row_num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ex_data_runtime_clear_row_cache(struct ex_data_runtime *ex_data_rt)
|
|
|
|
|
{
|
2023-03-15 13:30:39 +08:00
|
|
|
if (NULL == ex_data_rt) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
utarray_free(ex_data_rt->cache_rows);
|
|
|
|
|
ex_data_rt->cache_rows = NULL;
|
|
|
|
|
ex_data_rt->cache_row_num = 0;
|
|
|
|
|
ex_data_rt->cache_size = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 11:37:02 +08:00
|
|
|
struct ex_data_schema *ex_data_schema_new(maat_ex_new_func_t *new_func,
|
|
|
|
|
maat_ex_free_func_t *free_func,
|
|
|
|
|
maat_ex_dup_func_t *dup_func,
|
2023-01-30 21:59:35 +08:00
|
|
|
long argl, void *argp)
|
|
|
|
|
{
|
|
|
|
|
struct ex_data_schema *ex_schema = ALLOC(struct ex_data_schema, 1);
|
|
|
|
|
|
|
|
|
|
ex_schema->new_func = new_func;
|
|
|
|
|
ex_schema->free_func = free_func;
|
|
|
|
|
ex_schema->dup_func = dup_func;
|
|
|
|
|
ex_schema->argl = argl;
|
|
|
|
|
ex_schema->argp = argp;
|
|
|
|
|
|
|
|
|
|
return ex_schema;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ex_data_schema_free(struct ex_data_schema *ex_schema)
|
|
|
|
|
{
|
2023-03-15 13:30:39 +08:00
|
|
|
if (ex_schema != NULL) {
|
|
|
|
|
FREE(ex_schema);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
void ex_data_runtime_set_ex_container_schema(struct ex_data_runtime *ex_data_rt,
|
|
|
|
|
struct ex_container_schema *container_schema)
|
2022-12-09 17:12:18 +08:00
|
|
|
{
|
2023-03-15 13:30:39 +08:00
|
|
|
if (NULL == ex_data_rt) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
rcu_hash_set_user_ctx(ex_data_rt->htable, container_schema);
|
2022-12-09 17:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
struct ex_container_schema *
|
|
|
|
|
ex_data_runtime_get_ex_container_schema(struct ex_data_runtime *ex_data_rt)
|
2022-11-25 16:32:29 +08:00
|
|
|
{
|
2023-03-15 13:30:39 +08:00
|
|
|
if (NULL == ex_data_rt) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
return (struct ex_container_schema *)rcu_hash_get_user_ctx(ex_data_rt->htable);
|
2022-11-25 16:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-16 09:55:35 +08:00
|
|
|
void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt,
|
|
|
|
|
struct ex_data_schema *ex_schema,
|
2023-03-29 22:25:14 +08:00
|
|
|
const char *table_name,
|
2023-03-16 09:55:35 +08:00
|
|
|
const char *row, const char *key,
|
|
|
|
|
size_t key_len)
|
2022-11-25 16:32:29 +08:00
|
|
|
{
|
|
|
|
|
void *ex_data = NULL;
|
2023-03-16 09:55:35 +08:00
|
|
|
|
2023-03-29 22:25:14 +08:00
|
|
|
ex_schema->new_func(table_name, ex_data_rt->table_id, key, row, &ex_data,
|
2023-02-03 17:28:14 +08:00
|
|
|
ex_schema->argl, ex_schema->argp);
|
2022-11-25 16:32:29 +08:00
|
|
|
return ex_data;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
struct ex_container *ex_container_new(void *ex_data, void *custom_data)
|
2022-11-25 16:32:29 +08:00
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
struct ex_container *ex_container = ALLOC(struct ex_container, 1);
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
ex_container->ex_data = ex_data;
|
|
|
|
|
ex_container->custom_data = custom_data;
|
|
|
|
|
|
|
|
|
|
return ex_container;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
void ex_container_free(void *schema, void *data)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
/* schema is NULL if not call ex_data_runtime_set_ex_container_schema */
|
2023-03-17 17:28:52 +08:00
|
|
|
if (NULL == schema || NULL == data) {
|
2022-11-25 16:32:29 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2023-03-16 15:16:42 +08:00
|
|
|
|
|
|
|
|
struct ex_container *ex_container = (struct ex_container *)data;
|
2023-03-15 11:36:54 +08:00
|
|
|
struct ex_container_schema *container_schema = (struct ex_container_schema *)schema;
|
|
|
|
|
long argl = container_schema->ex_schema->argl;
|
|
|
|
|
void *argp = container_schema->ex_schema->argp;
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
if (ex_container->ex_data != NULL
|
2023-03-15 11:36:54 +08:00
|
|
|
&& container_schema->ex_schema->free_func != NULL) {
|
|
|
|
|
container_schema->ex_schema->free_func(container_schema->table_id,
|
2023-02-03 17:28:14 +08:00
|
|
|
&(ex_container->ex_data), argl, argp);
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
if (ex_container->custom_data != NULL
|
2023-03-15 11:36:54 +08:00
|
|
|
&& container_schema->custom_data_free != NULL) {
|
|
|
|
|
container_schema->custom_data_free(ex_container->custom_data);
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FREE(ex_container);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ex_data_runtime_add_ex_container(struct ex_data_runtime *ex_data_rt,
|
|
|
|
|
const char *key, size_t key_len,
|
2023-03-15 11:36:54 +08:00
|
|
|
struct ex_container *ex_container)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
2023-03-15 13:30:39 +08:00
|
|
|
return rcu_hash_add(ex_data_rt->htable, key, key_len, ex_container);
|
2022-11-25 16:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
int ex_data_runtime_del_ex_container(struct ex_data_runtime *ex_data_rt,
|
|
|
|
|
const char *key, size_t key_len)
|
2022-11-25 16:32:29 +08:00
|
|
|
{
|
2023-03-15 13:30:39 +08:00
|
|
|
return rcu_hash_del(ex_data_rt->htable, key, key_len);
|
2022-11-25 16:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-16 09:55:35 +08:00
|
|
|
void *ex_data_runtime_get_ex_data_by_key(struct ex_data_runtime *ex_data_rt,
|
|
|
|
|
struct ex_data_schema *ex_schema,
|
2023-02-20 10:57:40 +08:00
|
|
|
const char *key, size_t key_len)
|
2022-11-25 16:32:29 +08:00
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
struct ex_container *ex_container = NULL;
|
2023-02-23 19:08:26 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
ex_container = (struct ex_container *)rcu_hash_find(ex_data_rt->htable,
|
2023-02-03 17:28:14 +08:00
|
|
|
key, key_len);
|
2022-12-09 17:12:18 +08:00
|
|
|
if (NULL == ex_container) {
|
2022-11-25 16:32:29 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 10:57:40 +08:00
|
|
|
void *dup_ex_data = NULL;
|
2023-03-16 09:55:35 +08:00
|
|
|
ex_schema->dup_func(ex_data_rt->table_id, &dup_ex_data, &(ex_container->ex_data),
|
|
|
|
|
ex_schema->argl, ex_schema->argp);
|
2023-02-20 10:57:40 +08:00
|
|
|
return dup_ex_data;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 09:55:35 +08:00
|
|
|
void *ex_data_runtime_get_ex_data_by_container(struct ex_data_runtime *ex_data_rt,
|
|
|
|
|
struct ex_data_schema *ex_schema,
|
2023-03-15 11:36:54 +08:00
|
|
|
struct ex_container *ex_container)
|
2023-02-20 10:57:40 +08:00
|
|
|
{
|
2022-11-25 16:32:29 +08:00
|
|
|
void *dup_ex_data = NULL;
|
2023-03-16 09:55:35 +08:00
|
|
|
ex_schema->dup_func(ex_data_rt->table_id, &dup_ex_data, &(ex_container->ex_data),
|
|
|
|
|
ex_schema->argl, ex_schema->argp);
|
2022-11-25 16:32:29 +08:00
|
|
|
return dup_ex_data;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
void *ex_data_runtime_get_custom_data(struct ex_data_runtime *ex_data_rt,
|
|
|
|
|
const char *key, size_t key_len)
|
2022-11-25 16:32:29 +08:00
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
struct ex_container *ex_container = NULL;
|
|
|
|
|
ex_container = (struct ex_container *)rcu_hash_find(ex_data_rt->htable,
|
2023-02-03 17:28:14 +08:00
|
|
|
key, key_len);
|
2022-12-09 17:12:18 +08:00
|
|
|
if (NULL == ex_container) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ex_container->custom_data;
|
2022-11-25 16:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
2022-12-09 17:12:18 +08:00
|
|
|
size_t ex_data_runtime_ex_container_count(struct ex_data_runtime *ex_data_rt)
|
2022-11-25 16:32:29 +08:00
|
|
|
{
|
2022-12-09 17:12:18 +08:00
|
|
|
return rcu_hash_count(ex_data_rt->htable);
|
|
|
|
|
}
|
2022-11-25 16:32:29 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
int ex_data_runtime_is_updating(struct ex_data_runtime *ex_data_rt)
|
2022-12-09 17:12:18 +08:00
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
return rcu_hash_is_updating(ex_data_rt->htable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t ex_data_runtime_list_ex_container(struct ex_data_runtime *ex_data_rt,
|
|
|
|
|
struct ex_container ***ex_container)
|
|
|
|
|
{
|
|
|
|
|
return rcu_hash_list(ex_data_rt->htable, (void ***)ex_container);
|
|
|
|
|
}
|