support same pattern different offset(x-x:pat1 & y-y:pat1)

This commit is contained in:
liuwentan
2023-03-22 11:10:00 +08:00
parent 37447eef7f
commit 23ef2c3797
15 changed files with 970 additions and 906 deletions

View File

@@ -45,10 +45,11 @@ int expr_runtime_commit(void *expr_runtime, const char *table_name);
int expr_runtime_scan(struct expr_runtime *expr_rt, int thread_id, const char *data,
size_t data_len, int vtable_id, struct maat_state *state);
int expr_runtime_stream_open(struct expr_runtime *expr_rt, int thread_id);
int expr_runtime_stream_scan(struct expr_runtime *expr_rt, const char *data, size_t data_len,
struct adapter_hs_stream *expr_runtime_stream_open(struct expr_runtime *expr_rt, int thread_id);
int expr_runtime_stream_scan(struct expr_runtime *expr_rt, struct adapter_hs_stream *s_handle,
const char *data, size_t data_len,
int vtable_id, struct maat_state *state);
void expr_runtime_stream_close(struct expr_runtime *expr_rt);
void expr_runtime_stream_close(struct adapter_hs_stream *s_handle);
void expr_runtime_scan_hit_inc(struct expr_runtime *expr_rt, int thread_id);
long long expr_runtime_scan_hit_sum(struct expr_runtime *expr_rt, int n_thread);

View File

@@ -35,7 +35,6 @@ extern "C"
#define MAX_TABLE_NUM 1024
#define MAX_COMPILE_TABLE_NUM 16
#define MAX_PHYSICAL_TABLE_NUM 16
#define DISTRICT_ANY -1
#define DISTRICT_UNKNOWN -2

View File

@@ -39,7 +39,7 @@ void *virtual_schema_new(cJSON *json, struct table_manager *tbl_mgr,
void virtual_schema_free(void *virtual_schema);
int virtual_table_get_id(void *virtual_schema);
size_t virtual_table_get_physical_table_id(void *virtual_schema, int physical_table_ids[]);
int virtual_table_get_physical_table_id(void *virtual_schema);
#ifdef __cplusplus
}

View File

@@ -48,11 +48,10 @@ enum district_set_flag {
struct maat_stream {
struct maat *ref_maat_instance;
struct adapter_hs_stream *s_handle; //each physical table open one stream
int thread_id;
int table_id;
int vtable_id;
int physical_table_ids[MAX_PHYSICAL_TABLE_NUM];
size_t n_physical_table;
int physical_table_id;
struct log_handle *logger;
};
@@ -987,243 +986,205 @@ size_t hit_group_to_compile(void *compile_runtime, long long *compile_ids, int i
return n_hit_compile;
}
static int vtable_get_physical_table_ids(struct table_manager *tbl_mgr, int table_id,
int *physical_table_ids, size_t n_table_id_array,
int *vtable_id)
static int vtable_get_physical_table_id(struct table_manager *tbl_mgr, int table_id)
{
size_t physical_table_cnt = 0;
enum table_type table_type = table_manager_get_table_type(tbl_mgr, table_id);
if (table_type == TABLE_TYPE_VIRTUAL) {
//find physical table id
*vtable_id = table_id;
void *virtual_schema = table_manager_get_schema(tbl_mgr, table_id);
assert(virtual_schema != NULL);
physical_table_cnt = virtual_table_get_physical_table_id(virtual_schema, physical_table_ids);
return physical_table_cnt;
} else {
*vtable_id = 0;
physical_table_ids[0] = table_id;
physical_table_cnt = 1;
if (table_type != TABLE_TYPE_VIRTUAL) {
return -1;
}
return physical_table_cnt;
// find physical table id
void *virtual_schema = table_manager_get_schema(tbl_mgr, table_id);
assert(virtual_schema != NULL);
return virtual_table_get_physical_table_id(virtual_schema);
}
int flag_scan_hit_group_count(struct table_manager *tbl_mgr, int thread_id, long long flag,
int physical_table_ids[], int physical_table_cnt, int vtable_id,
struct maat_state *mid)
int flag_scan(struct table_manager *tbl_mgr, int thread_id, long long flag,
int physical_table_id, int vtable_id, struct maat_state *mid)
{
int sum_hit_group_cnt = 0;
for (int i = 0; i < physical_table_cnt; i++) {
enum table_type table_type = table_manager_get_table_type(tbl_mgr, physical_table_ids[i]);
if ((table_type == TABLE_TYPE_FLAG_PLUS) &&
(NULL == mid || DISTRICT_FLAG_UNSET == mid->is_set_district)) {
return -1;
}
if (table_type != TABLE_TYPE_FLAG && table_type != TABLE_TYPE_FLAG_PLUS) {
continue;
}
void *flag_rt = table_manager_get_runtime(tbl_mgr, physical_table_ids[i]);
if (NULL == flag_rt) {
return -1;
}
int group_hit_cnt = flag_runtime_scan((struct flag_runtime *)flag_rt, thread_id,
flag, vtable_id, mid);
if (group_hit_cnt < 0) {
return -1;
}
if (group_hit_cnt > 0) {
flag_runtime_scan_hit_inc((struct flag_runtime *)flag_rt, thread_id);
}
sum_hit_group_cnt += group_hit_cnt;
enum table_type table_type = table_manager_get_table_type(tbl_mgr, physical_table_id);
if ((table_type == TABLE_TYPE_FLAG_PLUS) &&
(NULL == mid || DISTRICT_FLAG_UNSET == mid->is_set_district)) {
return -1;
}
return sum_hit_group_cnt;
if (table_type != TABLE_TYPE_FLAG && table_type != TABLE_TYPE_FLAG_PLUS) {
return -1;
}
void *flag_rt = table_manager_get_runtime(tbl_mgr, physical_table_id);
if (NULL == flag_rt) {
return -1;
}
int group_hit_cnt = flag_runtime_scan((struct flag_runtime *)flag_rt, thread_id,
flag, vtable_id, mid);
if (group_hit_cnt < 0) {
return -1;
}
if (group_hit_cnt > 0) {
flag_runtime_scan_hit_inc((struct flag_runtime *)flag_rt, thread_id);
}
return group_hit_cnt;
}
int interval_scan_hit_group_count(struct table_manager *tbl_mgr, int thread_id, long long integer,
int physical_table_ids[], int physical_table_cnt, int vtable_id,
struct maat_state *mid)
int interval_scan(struct table_manager *tbl_mgr, int thread_id, long long integer,
int physical_table_id, int vtable_id, struct maat_state *mid)
{
int sum_hit_group_cnt = 0;
for (size_t i = 0; i < physical_table_cnt; i++) {
enum table_type table_type = table_manager_get_table_type(tbl_mgr, physical_table_ids[i]);
if ((table_type == TABLE_TYPE_INTERVAL_PLUS) &&
(NULL == mid || DISTRICT_FLAG_UNSET == mid->is_set_district)) {
//maat_instance->scan_err_cnt++;
return -1;
}
if (table_type != TABLE_TYPE_INTERVAL && table_type != TABLE_TYPE_INTERVAL_PLUS) {
continue;
}
void *interval_rt = table_manager_get_runtime(tbl_mgr, physical_table_ids[i]);
if (NULL == interval_rt) {
return -1;
}
int group_hit_cnt = interval_runtime_scan((struct interval_runtime *)interval_rt,
thread_id, integer, vtable_id, mid);
if (group_hit_cnt < 0) {
return -1;
}
if (group_hit_cnt > 0) {
interval_runtime_scan_hit_inc((struct interval_runtime *)interval_rt, thread_id);
}
sum_hit_group_cnt += group_hit_cnt;
enum table_type table_type = table_manager_get_table_type(tbl_mgr, physical_table_id);
if ((table_type == TABLE_TYPE_INTERVAL_PLUS) &&
(NULL == mid || DISTRICT_FLAG_UNSET == mid->is_set_district)) {
// maat_instance->scan_err_cnt++;
return -1;
}
return sum_hit_group_cnt;
if (table_type != TABLE_TYPE_INTERVAL && table_type != TABLE_TYPE_INTERVAL_PLUS) {
return -1;
}
void *interval_rt = table_manager_get_runtime(tbl_mgr, physical_table_id);
if (NULL == interval_rt) {
return -1;
}
int group_hit_cnt = interval_runtime_scan((struct interval_runtime *)interval_rt,
thread_id, integer, vtable_id, mid);
if (group_hit_cnt < 0) {
return -1;
}
if (group_hit_cnt > 0) {
interval_runtime_scan_hit_inc((struct interval_runtime *)interval_rt, thread_id);
}
return group_hit_cnt;
}
int ipv4_scan_hit_group_count(struct table_manager *tbl_mgr, int thread_id, uint32_t ip_addr,
int physical_table_ids[], int physical_table_cnt, int vtable_id,
struct maat_state *mid)
int ipv4_scan(struct table_manager *tbl_mgr, int thread_id, uint32_t ip_addr,
int physical_table_id, int vtable_id, struct maat_state *mid)
{
int sum_hit_group_cnt = 0;
for (size_t i = 0; i < physical_table_cnt; i++) {
enum table_type table_type = table_manager_get_table_type(tbl_mgr, physical_table_ids[i]);
if (table_type != TABLE_TYPE_IP_PLUS) {
continue;
}
void *ip_rt = table_manager_get_runtime(tbl_mgr, physical_table_ids[i]);
if (NULL == ip_rt) {
return -1;
}
int group_hit_cnt = ip_runtime_scan((struct ip_runtime *)ip_rt, thread_id, IPv4,
(uint8_t *)&ip_addr, vtable_id, mid);
if (group_hit_cnt < 0) {
return -1;
}
if (group_hit_cnt > 0) {
ip_runtime_scan_hit_inc((struct ip_runtime *)ip_rt, thread_id);
}
sum_hit_group_cnt += group_hit_cnt;
enum table_type table_type = table_manager_get_table_type(tbl_mgr, physical_table_id);
if (table_type != TABLE_TYPE_IP_PLUS) {
return -1;
}
return sum_hit_group_cnt;
void *ip_rt = table_manager_get_runtime(tbl_mgr, physical_table_id);
if (NULL == ip_rt) {
return -1;
}
int group_hit_cnt = ip_runtime_scan((struct ip_runtime *)ip_rt, thread_id, IPv4,
(uint8_t *)&ip_addr, vtable_id, mid);
if (group_hit_cnt < 0) {
return -1;
}
if (group_hit_cnt > 0) {
ip_runtime_scan_hit_inc((struct ip_runtime *)ip_rt, thread_id);
}
return group_hit_cnt;
}
int ipv6_scan_hit_group_count(struct table_manager *tbl_mgr, int thread_id, uint8_t *ip_addr,
int physical_table_ids[], int physical_table_cnt, int vtable_id,
struct maat_state *mid)
int ipv6_scan(struct table_manager *tbl_mgr, int thread_id, uint8_t *ip_addr,
int physical_table_id, int vtable_id, struct maat_state *mid)
{
int sum_hit_group_cnt = 0;
for (size_t i = 0; i < physical_table_cnt; i++) {
enum table_type table_type = table_manager_get_table_type(tbl_mgr, physical_table_ids[i]);
if (table_type != TABLE_TYPE_IP_PLUS) {
continue;
}
void *ip_rt = table_manager_get_runtime(tbl_mgr, physical_table_ids[i]);
if (NULL == ip_rt) {
return -1;
}
int group_hit_cnt = ip_runtime_scan((struct ip_runtime *)ip_rt, thread_id, IPv6,
ip_addr, vtable_id, mid);
if (group_hit_cnt < 0) {
return -1;
}
if (group_hit_cnt > 0) {
ip_runtime_scan_hit_inc((struct ip_runtime *)ip_rt, thread_id);
}
sum_hit_group_cnt += group_hit_cnt;
enum table_type table_type = table_manager_get_table_type(tbl_mgr, physical_table_id);
if (table_type != TABLE_TYPE_IP_PLUS) {
return -1;
}
return sum_hit_group_cnt;
void *ip_rt = table_manager_get_runtime(tbl_mgr, physical_table_id);
if (NULL == ip_rt) {
return -1;
}
int group_hit_cnt = ip_runtime_scan((struct ip_runtime *)ip_rt, thread_id, IPv6,
ip_addr, vtable_id, mid);
if (group_hit_cnt < 0) {
return -1;
}
if (group_hit_cnt > 0) {
ip_runtime_scan_hit_inc((struct ip_runtime *)ip_rt, thread_id);
}
return group_hit_cnt;
}
int string_scan_hit_group_count(struct table_manager *tbl_mgr, int thread_id, const char *data,
size_t data_len, int physical_table_ids[], int physical_table_cnt,
int vtable_id, struct maat_state *mid)
int string_scan(struct table_manager *tbl_mgr, int thread_id, const char *data, size_t data_len,
int physical_table_id, int vtable_id, struct maat_state *mid)
{
int sum_hit_group_cnt = 0;
for (size_t i = 0; i < physical_table_cnt; i++) {
enum table_type table_type = table_manager_get_table_type(tbl_mgr, physical_table_ids[i]);
if ((table_type == TABLE_TYPE_EXPR_PLUS) &&
(NULL == mid || DISTRICT_FLAG_UNSET == mid->is_set_district)) {
//maat_instance->scan_err_cnt++;
return -1;
}
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
continue;
}
void *expr_rt = table_manager_get_runtime(tbl_mgr, physical_table_ids[i]);
if (NULL == expr_rt) {
return -1;
}
int group_hit_cnt = expr_runtime_scan((struct expr_runtime *)expr_rt,
thread_id, data, data_len,
vtable_id, mid);
if (group_hit_cnt < 0) {
return -1;
}
if (group_hit_cnt > 0) {
expr_runtime_scan_hit_inc((struct expr_runtime *)expr_rt, thread_id);
}
sum_hit_group_cnt += group_hit_cnt;
enum table_type table_type = table_manager_get_table_type(tbl_mgr, physical_table_id);
if ((table_type == TABLE_TYPE_EXPR_PLUS) &&
(NULL == mid || DISTRICT_FLAG_UNSET == mid->is_set_district)) {
// maat_instance->scan_err_cnt++;
return -1;
}
return sum_hit_group_cnt;
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
return -1;
}
void *expr_rt = table_manager_get_runtime(tbl_mgr, physical_table_id);
if (NULL == expr_rt) {
return -1;
}
int group_hit_cnt = expr_runtime_scan((struct expr_runtime *)expr_rt,
thread_id, data, data_len,
vtable_id, mid);
if (group_hit_cnt < 0) {
return -1;
}
if (group_hit_cnt > 0) {
expr_runtime_scan_hit_inc((struct expr_runtime *)expr_rt, thread_id);
}
return group_hit_cnt;
}
int stream_scan_hit_group_count(struct table_manager *tbl_mgr, int thread_id, const char *data,
size_t data_len, int physical_table_ids[], int physical_table_cnt,
int vtable_id, struct maat_state *mid)
int expr_stream_scan(struct maat_stream *stream, const char *data, size_t data_len, struct maat_state *state)
{
int sum_hit_group_cnt = 0;
for (size_t i = 0; i < physical_table_cnt; i++) {
enum table_type table_type = table_manager_get_table_type(tbl_mgr, physical_table_ids[i]);
if ((table_type == TABLE_TYPE_EXPR_PLUS) &&
(NULL == mid || DISTRICT_FLAG_UNSET == mid->is_set_district)) {
//maat_instance->scan_err_cnt++;
return -1;
}
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
continue;
}
void *expr_rt = table_manager_get_runtime(tbl_mgr, physical_table_ids[i]);
if (NULL == expr_rt) {
return -1;
}
int group_hit_cnt = expr_runtime_stream_scan((struct expr_runtime *)expr_rt,
data, data_len, vtable_id, mid);
if (group_hit_cnt < 0) {
return -1;
}
if (group_hit_cnt > 0) {
expr_runtime_scan_hit_inc((struct expr_runtime *)expr_rt, thread_id);
}
sum_hit_group_cnt += group_hit_cnt;
if (NULL == stream || NULL == data) {
return 0;
}
return sum_hit_group_cnt;
struct table_manager *tbl_mgr = stream->ref_maat_instance->tbl_mgr;
enum table_type table_type = table_manager_get_table_type(tbl_mgr, stream->physical_table_id);
if ((table_type == TABLE_TYPE_EXPR_PLUS) &&
(NULL == state || DISTRICT_FLAG_UNSET == state->is_set_district)) {
// maat_instance->scan_err_cnt++;
return -1;
}
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
return -1;
}
void *expr_rt = table_manager_get_runtime(tbl_mgr, stream->physical_table_id);
if (NULL == expr_rt) {
return -1;
}
int group_hit_cnt = expr_runtime_stream_scan((struct expr_runtime *)expr_rt, stream->s_handle,
data, data_len, stream->vtable_id, state);
if (group_hit_cnt < 0) {
return -1;
}
if (group_hit_cnt > 0) {
expr_runtime_scan_hit_inc((struct expr_runtime *)expr_rt, stream->thread_id);
}
return group_hit_cnt;
}
size_t group_to_compile(struct maat *maat_instance, long long *results, size_t n_result,
@@ -1279,31 +1240,29 @@ int maat_scan_flag(struct maat *maat_instance, int table_id, int thread_id,
}
int vtable_id = 0;
int physical_table_cnt = 0;
int physical_table_ids[MAX_PHYSICAL_TABLE_NUM];
memset(physical_table_ids, -1, sizeof(physical_table_ids));
int physical_table_id = -1;
enum table_type table_type = table_manager_get_table_type(maat_instance->tbl_mgr, table_id);
if (table_type == TABLE_TYPE_VIRTUAL) {
physical_table_id = vtable_get_physical_table_id(maat_instance->tbl_mgr, table_id);
vtable_id = table_id;
} else {
physical_table_id = table_id;
}
physical_table_cnt = vtable_get_physical_table_ids(maat_instance->tbl_mgr, table_id,
physical_table_ids,
MAX_PHYSICAL_TABLE_NUM, &vtable_id);
if (physical_table_cnt <= 0) {
if (physical_table_id < 0) {
return MAAT_SCAN_ERR;
}
enum table_type table_type = TABLE_TYPE_INVALID;
if (0 == vtable_id) {
table_type = table_manager_get_table_type(maat_instance->tbl_mgr, physical_table_ids[0]);
if (table_type != TABLE_TYPE_FLAG && table_type != TABLE_TYPE_FLAG_PLUS) {
return MAAT_SCAN_ERR;
}
table_type = table_manager_get_table_type(maat_instance->tbl_mgr, physical_table_id);
if (table_type != TABLE_TYPE_FLAG && table_type != TABLE_TYPE_FLAG_PLUS) {
return MAAT_SCAN_ERR;
}
maat_runtime_ref_inc(maat_instance->maat_rt, thread_id);
alignment_int64_array_add(maat_instance->thread_call_cnt, thread_id, 1);
int hit_group_cnt = flag_scan_hit_group_count(maat_instance->tbl_mgr, thread_id, flag,
physical_table_ids,
physical_table_cnt, vtable_id, mid);
int hit_group_cnt = flag_scan(maat_instance->tbl_mgr, thread_id, flag,
physical_table_id, vtable_id, mid);
if (hit_group_cnt < 0) {
return MAAT_SCAN_ERR;
}
@@ -1356,31 +1315,29 @@ int maat_scan_integer(struct maat *maat_instance, int table_id, int thread_id,
}
int vtable_id = 0;
int physical_table_cnt = 0;
int physical_table_ids[MAX_PHYSICAL_TABLE_NUM];
memset(physical_table_ids, -1, sizeof(physical_table_ids));
int physical_table_id = -1;
enum table_type table_type = table_manager_get_table_type(maat_instance->tbl_mgr, table_id);
if (table_type == TABLE_TYPE_VIRTUAL) {
physical_table_id = vtable_get_physical_table_id(maat_instance->tbl_mgr, table_id);
vtable_id = table_id;
} else {
physical_table_id = table_id;
}
physical_table_cnt = vtable_get_physical_table_ids(maat_instance->tbl_mgr, table_id,
physical_table_ids,
MAX_PHYSICAL_TABLE_NUM, &vtable_id);
if (physical_table_cnt <= 0) {
if (physical_table_id < 0) {
return MAAT_SCAN_ERR;
}
enum table_type table_type = TABLE_TYPE_INVALID;
if (0 == vtable_id) {
table_type = table_manager_get_table_type(maat_instance->tbl_mgr, physical_table_ids[0]);
if (table_type != TABLE_TYPE_INTERVAL && table_type != TABLE_TYPE_INTERVAL_PLUS) {
return MAAT_SCAN_ERR;
}
table_type = table_manager_get_table_type(maat_instance->tbl_mgr, physical_table_id);
if (table_type != TABLE_TYPE_INTERVAL && table_type != TABLE_TYPE_INTERVAL_PLUS) {
return MAAT_SCAN_ERR;
}
maat_runtime_ref_inc(maat_instance->maat_rt, thread_id);
alignment_int64_array_add(maat_instance->thread_call_cnt, thread_id, 1);
int hit_group_cnt = interval_scan_hit_group_count(maat_instance->tbl_mgr, thread_id, integer,
physical_table_ids,
physical_table_cnt, vtable_id, mid);
int hit_group_cnt = interval_scan(maat_instance->tbl_mgr, thread_id, integer,
physical_table_id, vtable_id, mid);
if (hit_group_cnt < 0) {
return MAAT_SCAN_ERR;
}
@@ -1433,31 +1390,29 @@ int maat_scan_ipv4(struct maat *maat_instance, int table_id, int thread_id,
}
int vtable_id = 0;
int physical_table_cnt = 0;
int physical_table_ids[MAX_PHYSICAL_TABLE_NUM];
memset(physical_table_ids, -1, sizeof(physical_table_ids));
int physical_table_id = -1;
enum table_type table_type = table_manager_get_table_type(maat_instance->tbl_mgr, table_id);
if (table_type == TABLE_TYPE_VIRTUAL) {
physical_table_id = vtable_get_physical_table_id(maat_instance->tbl_mgr, table_id);
vtable_id = table_id;
} else {
physical_table_id = table_id;
}
physical_table_cnt = vtable_get_physical_table_ids(maat_instance->tbl_mgr, table_id,
physical_table_ids,
MAX_PHYSICAL_TABLE_NUM, &vtable_id);
if (physical_table_cnt <= 0) {
if (physical_table_id < 0) {
return MAAT_SCAN_ERR;
}
enum table_type table_type = TABLE_TYPE_INVALID;
if (0 == vtable_id) {
table_type = table_manager_get_table_type(maat_instance->tbl_mgr, physical_table_ids[0]);
if (table_type != TABLE_TYPE_IP_PLUS) {
return MAAT_SCAN_ERR;
}
table_type = table_manager_get_table_type(maat_instance->tbl_mgr, physical_table_id);
if (table_type != TABLE_TYPE_IP_PLUS) {
return MAAT_SCAN_ERR;
}
maat_runtime_ref_inc(maat_instance->maat_rt, thread_id);
alignment_int64_array_add(maat_instance->thread_call_cnt, thread_id, 1);
int hit_group_cnt = ipv4_scan_hit_group_count(maat_instance->tbl_mgr, thread_id, ip_addr,
physical_table_ids,
physical_table_cnt, vtable_id, mid);
int hit_group_cnt = ipv4_scan(maat_instance->tbl_mgr, thread_id, ip_addr,
physical_table_id, vtable_id, mid);
if (hit_group_cnt < 0) {
return MAAT_SCAN_ERR;
}
@@ -1518,31 +1473,29 @@ int maat_scan_ipv6(struct maat *maat_instance, int table_id, int thread_id,
}
int vtable_id = 0;
int physical_table_cnt = 0;
int physical_table_ids[MAX_PHYSICAL_TABLE_NUM];
memset(physical_table_ids, -1, sizeof(physical_table_ids));
int physical_table_id = -1;
enum table_type table_type = table_manager_get_table_type(maat_instance->tbl_mgr, table_id);
if (table_type == TABLE_TYPE_VIRTUAL) {
physical_table_id = vtable_get_physical_table_id(maat_instance->tbl_mgr, table_id);
vtable_id = table_id;
} else {
physical_table_id = table_id;
}
physical_table_cnt = vtable_get_physical_table_ids(maat_instance->tbl_mgr, table_id,
physical_table_ids,
MAX_PHYSICAL_TABLE_NUM, &vtable_id);
if (physical_table_cnt <= 0) {
if (physical_table_id < 0) {
return MAAT_SCAN_ERR;
}
enum table_type table_type = TABLE_TYPE_INVALID;
if (0 == vtable_id) {
table_type = table_manager_get_table_type(maat_instance->tbl_mgr, physical_table_ids[0]);
if (table_type != TABLE_TYPE_IP_PLUS) {
return MAAT_SCAN_ERR;
}
table_type = table_manager_get_table_type(maat_instance->tbl_mgr, physical_table_id);
if (table_type != TABLE_TYPE_IP_PLUS) {
return MAAT_SCAN_ERR;
}
maat_runtime_ref_inc(maat_instance->maat_rt, thread_id);
alignment_int64_array_add(maat_instance->thread_call_cnt, thread_id, 1);
int hit_group_cnt = ipv6_scan_hit_group_count(maat_instance->tbl_mgr, thread_id, ip_addr,
physical_table_ids,
physical_table_cnt, vtable_id, mid);
int hit_group_cnt = ipv6_scan(maat_instance->tbl_mgr, thread_id, ip_addr,
physical_table_id, vtable_id, mid);
if (hit_group_cnt < 0) {
return MAAT_SCAN_ERR;
}
@@ -1603,31 +1556,29 @@ int maat_scan_string(struct maat *maat_instance, int table_id, int thread_id,
}
int vtable_id = 0;
int physical_table_cnt = 0;
int physical_table_ids[MAX_PHYSICAL_TABLE_NUM];
memset(physical_table_ids, -1, sizeof(physical_table_ids));
int physical_table_id = -1;
enum table_type table_type = table_manager_get_table_type(maat_instance->tbl_mgr, table_id);
if (table_type == TABLE_TYPE_VIRTUAL) {
physical_table_id = vtable_get_physical_table_id(maat_instance->tbl_mgr, table_id);
vtable_id = table_id;
} else {
physical_table_id = table_id;
}
physical_table_cnt = vtable_get_physical_table_ids(maat_instance->tbl_mgr, table_id,
physical_table_ids,
MAX_PHYSICAL_TABLE_NUM, &vtable_id);
if (physical_table_cnt <= 0) {
if (physical_table_id < 0) {
return MAAT_SCAN_ERR;
}
enum table_type table_type = TABLE_TYPE_INVALID;
if (0 == vtable_id) {
table_type = table_manager_get_table_type(maat_instance->tbl_mgr, physical_table_ids[0]);
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
return MAAT_SCAN_ERR;
}
table_type = table_manager_get_table_type(maat_instance->tbl_mgr, physical_table_id);
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
return MAAT_SCAN_ERR;
}
maat_runtime_ref_inc(maat_instance->maat_rt, thread_id);
alignment_int64_array_add(maat_instance->thread_call_cnt, thread_id, 1);
int hit_group_cnt = string_scan_hit_group_count(maat_instance->tbl_mgr, thread_id, data,
data_len, physical_table_ids,
physical_table_cnt, vtable_id, mid);
int hit_group_cnt = string_scan(maat_instance->tbl_mgr, thread_id, data, data_len,
physical_table_id, vtable_id, mid);
if (hit_group_cnt < 0) {
return MAAT_SCAN_ERR;
}
@@ -1666,52 +1617,43 @@ struct maat_stream *maat_scan_stream_open(struct maat *maat_instance, int table_
}
struct maat_stream *stream = ALLOC(struct maat_stream, 1);
memset(stream->physical_table_ids, -1, sizeof(stream->physical_table_ids));
stream->ref_maat_instance = maat_instance;
stream->table_id = table_id;
stream->thread_id = thread_id;
stream->logger = maat_instance->logger;
stream->n_physical_table = vtable_get_physical_table_ids(stream->ref_maat_instance->tbl_mgr,
stream->table_id, stream->physical_table_ids,
MAX_PHYSICAL_TABLE_NUM, &stream->vtable_id);
if (stream->n_physical_table <= 0) {
enum table_type table_type = table_manager_get_table_type(maat_instance->tbl_mgr, table_id);
if (table_type == TABLE_TYPE_VIRTUAL) {
stream->physical_table_id = vtable_get_physical_table_id(maat_instance->tbl_mgr, table_id);
stream->vtable_id = table_id;
} else {
stream->physical_table_id = table_id;
stream->vtable_id = 0;
}
if (stream->physical_table_id < 0) {
return NULL;
}
enum table_type table_type = TABLE_TYPE_INVALID;
if (0 == stream->vtable_id) {
table_type = table_manager_get_table_type(stream->ref_maat_instance->tbl_mgr,
stream->physical_table_ids[0]);
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
log_error(maat_instance->logger, MODULE_MAAT_API,
"[%s:%d] table(table_id:%d) thread_id:%d scan stream's physical table must be expr or expr_plus",
__FUNCTION__, __LINE__, table_id, thread_id);
return NULL;
}
table_type = table_manager_get_table_type(maat_instance->tbl_mgr, stream->physical_table_id);
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
return NULL;
}
for (size_t i = 0; i < stream->n_physical_table; i++) {
enum table_type table_type = table_manager_get_table_type(stream->ref_maat_instance->tbl_mgr,
stream->physical_table_ids[i]);
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
log_error(maat_instance->logger, MODULE_MAAT_API,
"%s:%d] table(table_id:%d) thread_id:%d scan stream's physical table must be expr or expr_plus",
__FUNCTION__, __LINE__, table_id, thread_id);
return NULL;
}
void *expr_rt = table_manager_get_runtime(stream->ref_maat_instance->tbl_mgr,
stream->physical_table_id);
assert(expr_rt != NULL);
void *expr_rt = table_manager_get_runtime(stream->ref_maat_instance->tbl_mgr,
stream->physical_table_ids[i]);
assert(expr_rt != NULL);
int ret = expr_runtime_stream_open((struct expr_runtime *)expr_rt, thread_id);
if (ret < 0) {
return NULL;
}
struct adapter_hs_stream *handle = expr_runtime_stream_open((struct expr_runtime *)expr_rt, thread_id);
if (NULL == handle) {
goto error;
}
stream->s_handle = handle;
return stream;
error:
FREE(stream);
return NULL;
}
int maat_scan_stream(struct maat_stream **maat_stream, const char *data, int data_len,
@@ -1728,20 +1670,23 @@ int maat_scan_stream(struct maat_stream **maat_stream, const char *data, int dat
mid = grab_state(state, stream->ref_maat_instance, stream->thread_id);
mid->scan_cnt++;
int valid_table_id = -1;
if (stream->vtable_id != 0) {
valid_table_id = stream->vtable_id;
} else {
valid_table_id = stream->physical_table_id;
}
if (NULL == stream->ref_maat_instance->maat_rt) {
log_error(stream->logger, MODULE_MAAT_API,
"[%s:%d] table(table_id:%d) thread_id:%d maat_scan_stream error because of maat_runtime is NULL",
__FUNCTION__, __LINE__, stream->table_id, stream->thread_id);
__FUNCTION__, __LINE__, valid_table_id, stream->thread_id);
return MAAT_SCAN_OK;
}
alignment_int64_array_add(stream->ref_maat_instance->thread_call_cnt, stream->thread_id, 1);
int hit_group_cnt = stream_scan_hit_group_count(stream->ref_maat_instance->tbl_mgr,
stream->thread_id, data, data_len,
stream->physical_table_ids,
stream->n_physical_table,
stream->vtable_id, mid);
int hit_group_cnt = expr_stream_scan(stream, data, data_len, mid);
if (hit_group_cnt < 0) {
return MAAT_SCAN_ERR;
}
@@ -1774,13 +1719,7 @@ void maat_scan_stream_close(struct maat_stream **maat_stream)
{
struct maat_stream *stream = *maat_stream;
for (size_t i = 0; i < stream->n_physical_table; i++) {
void *expr_rt = table_manager_get_runtime(stream->ref_maat_instance->tbl_mgr,
stream->physical_table_ids[i]);
assert(expr_rt != NULL);
expr_runtime_stream_close((struct expr_runtime *)expr_rt);
}
expr_runtime_stream_close(stream->s_handle);
FREE(stream);
}

View File

@@ -144,7 +144,6 @@ int bool_plugin_table_set_ex_data_schema(void *bool_plugin_schema,
return 0;
}
static int cmp_ull_p(const void *p1, const void *p2)
{
if(* (unsigned long long*) p1 > * (unsigned long long*) p2) {

View File

@@ -34,7 +34,6 @@ struct expr_schema {
int expr_type_column;
int match_method_column;
int is_hexbin_column;
enum hs_pattern_type pattern_type; /* literal or regex */
int table_id; //ugly
struct table_manager *ref_tbl_mgr;
};
@@ -68,12 +67,9 @@ struct expr_item {
};
struct expr_runtime {
enum hs_pattern_type pattern_type;
struct adapter_hs *hs;
struct adapter_hs_stream *hs_stream;
struct rcu_hash_table *htable; // store hs_expr rule for rebuild adapter_hs instance
struct rcu_hash_table *item_htable; // store this expr table's all maat_item which will be used in expr_runtime_scan
struct group2group_runtime *ref_g2g_rt;
uint32_t rule_num;
int n_worker_thread;
@@ -275,34 +271,26 @@ void expr_item_free(struct expr_item *expr_item)
FREE(expr_item);
}
enum hs_pattern_type pattern_type_str_to_enum(const char *type_str)
{
enum hs_pattern_type pattern_type = HS_PATTERN_TYPE_MAX;
if (strcmp(type_str, "literal") == 0) {
pattern_type = HS_PATTERN_TYPE_STR;
} else if (strcmp(type_str, "regex") == 0) {
pattern_type = HS_PATTERN_TYPE_REG;
} else {
assert(0);
}
return pattern_type;
}
void *expr_schema_new(cJSON *json, struct table_manager *tbl_mgr,
const char *table_name, struct log_handle *logger)
{
int read_cnt = 0;
char table_type[NAME_MAX] = {0};
struct expr_schema *expr_schema = ALLOC(struct expr_schema, 1);
cJSON *custom_item = NULL;
cJSON *item = cJSON_GetObjectItem(json, "table_id");
if (item != NULL && item->type == cJSON_Number) {
expr_schema->table_id = item->valueint;
read_cnt++;
} else {
log_error(logger, MODULE_EXPR,
"[%s:%d] table %s has no table_id column", table_name);
goto error;
}
/* table_type already validate in maat_table_new() */
item = cJSON_GetObjectItem(json, "table_type");
memcpy(table_type, item->valuestring, strlen(item->valuestring));
item = cJSON_GetObjectItem(json, "custom");
if (item == NULL || item->type != cJSON_Object) {
log_error(logger, MODULE_EXPR,
@@ -310,59 +298,73 @@ void *expr_schema_new(cJSON *json, struct table_manager *tbl_mgr,
goto error;
}
custom_item = cJSON_GetObjectItem(item, "pattern_type");
if (custom_item != NULL && custom_item->type == cJSON_String) {
expr_schema->pattern_type = pattern_type_str_to_enum(custom_item->valuestring);
read_cnt++;
}
custom_item = cJSON_GetObjectItem(item, "item_id");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
expr_schema->item_id_column = custom_item->valueint;
read_cnt++;
} else {
log_error(logger, MODULE_EXPR,
"[%s:%d] table %s has no item_id column", table_name);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "group_id");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
expr_schema->group_id_column = custom_item->valueint;
read_cnt++;
} else {
log_error(logger, MODULE_EXPR,
"[%s:%d] table %s has no group_id column", table_name);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "keywords");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
expr_schema->keywords_column = custom_item->valueint;
read_cnt++;
} else {
log_error(logger, MODULE_EXPR,
"[%s:%d] table %s has no keywords column", table_name);
goto error;
}
/* expr_plus has district */
custom_item = cJSON_GetObjectItem(item, "district");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
expr_schema->district_column = custom_item->valueint;
}
if (strcmp(table_type, "expr_plus") == 0) {
custom_item = cJSON_GetObjectItem(item, "district");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
expr_schema->district_column = custom_item->valueint;
} else {
log_error(logger, MODULE_EXPR,
"[%s:%d] expr_plus table %s has no district column", table_name);
goto error;
}
}
custom_item = cJSON_GetObjectItem(item, "expr_type");
custom_item = cJSON_GetObjectItem(item, "expr_type");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
expr_schema->expr_type_column = custom_item->valueint;
read_cnt++;
} else {
log_error(logger, MODULE_EXPR,
"[%s:%d] table %s has no expr_type column", table_name);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "match_method");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
expr_schema->match_method_column = custom_item->valueint;
read_cnt++;
} else {
log_error(logger, MODULE_EXPR,
"[%s:%d] table %s has no match_method column", table_name);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "is_hexbin");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
expr_schema->is_hexbin_column = custom_item->valueint;
read_cnt++;
} else {
log_error(logger, MODULE_EXPR,
"[%s:%d] table %s has no is_hexbin column", table_name);
goto error;
}
expr_schema->ref_tbl_mgr = tbl_mgr;
if (read_cnt < 8) {
goto error;
}
return expr_schema;
error:
@@ -408,12 +410,10 @@ void *expr_runtime_new(void *expr_schema, int max_thread_num,
return NULL;
}
struct expr_schema *schema = (struct expr_schema *)expr_schema;
struct expr_runtime *expr_rt = ALLOC(struct expr_runtime, 1);
expr_rt->htable = rcu_hash_new(expr_ex_data_free);
expr_rt->item_htable = rcu_hash_new(expr_maat_item_free);
expr_rt->pattern_type = schema->pattern_type;
expr_rt->n_worker_thread = max_thread_num;
expr_rt->ref_garbage_bin = garbage_bin;
expr_rt->logger = logger;
@@ -436,10 +436,10 @@ void expr_runtime_free(void *expr_runtime)
expr_rt->hs = NULL;
}
if (expr_rt->hs_stream != NULL) {
adapter_hs_stream_close(expr_rt->hs_stream);
expr_rt->hs_stream = NULL;
}
// if (expr_rt->hs_stream != NULL) {
// adapter_hs_stream_close(expr_rt->hs_stream);
// expr_rt->hs_stream = NULL;
// }
if (expr_rt->htable != NULL) {
rcu_hash_free(expr_rt->htable);
@@ -563,9 +563,9 @@ struct hs_expr *expr_item_to_expr_rule(struct expr_item *expr_item, void *user_d
if (i >= MAAT_MAX_EXPR_ITEM_NUM) {
log_error(logger, MODULE_EXPR,
"[%s:%d] expr item_id:%d too many patterns",
"[%s:%d]abandon config expr_item(item_id:%d) too many patterns",
__FUNCTION__, __LINE__, expr_item->item_id);
return NULL;
goto error;
}
sub_key_array[i] = tmp;
@@ -586,9 +586,9 @@ struct hs_expr *expr_item_to_expr_rule(struct expr_item *expr_item, void *user_d
if (i >= MAAT_MAX_EXPR_ITEM_NUM) {
log_error(logger, MODULE_EXPR,
"[%s:%d] expr item_id:%d too many patterns",
"[%s:%d]abandon config expr_item(item_id:%d) too many patterns",
__FUNCTION__, __LINE__, expr_item->item_id);
return NULL;
goto error;
}
sub_key_array[i] = tmp;
@@ -596,17 +596,17 @@ struct hs_expr *expr_item_to_expr_rule(struct expr_item *expr_item, void *user_d
if (!(key_left_offset[i] >= 0 && key_right_offset[i] > 0
&& key_left_offset[i] <= key_right_offset[i])) {
log_error(logger, MODULE_EXPR,
"[%s:%d] expr item:%d has invalid offset.",
"[%s:%d]abandon config expr_item(item_id:%d) has invalid offset.",
__FUNCTION__, __LINE__, expr_item->item_id);
return NULL;
goto error;
}
sub_key_array[i] = (char *)memchr(sub_key_array[i], ':', strlen(sub_key_array[i]));
if (NULL == sub_key_array[i]) {
log_error(logger, MODULE_EXPR,
"[%s:%d] expr item:%d has invalid offset keyword format.",
"[%s:%d]abandon config expr_item(item_id:%d) has invalid offset keyword format.",
__FUNCTION__, __LINE__, expr_item->item_id);
return NULL;
goto error;
}
sub_key_array[i]++;//jump over ':'
@@ -620,24 +620,26 @@ struct hs_expr *expr_item_to_expr_rule(struct expr_item *expr_item, void *user_d
sub_key_array[0] = str_unescape(sub_key_array[0]);
break;
default:
break;
log_error(logger, MODULE_EXPR,
"[%s:%d]abandon config expr_item(item_id:%lld) has invalid expr type=%d",
__FUNCTION__, __LINE__, expr_item->item_id, expr_item->expr_type);
goto error;
}
size_t region_str_len = 0;
char *region_string = NULL;
size_t sub_key_len = 0;
for (i = 0; i < sub_expr_cnt; i++) {
size_t region_str_len = 0;
char *region_string = NULL;
size_t sub_key_len = 0;
if (FALSE == expr_item->is_case_sensitive) {
// insensitive
expr_rule->patterns[i].case_sensitive = HS_CASE_INSESITIVE;
}
enum hs_pattern_type pattern_type = expr_type2pattern_type(expr_item->expr_type);
if (TRUE == expr_item->is_hexbin && pattern_type != HS_PATTERN_TYPE_REG) {
region_str_len = strlen(sub_key_array[i]) + 1;
expr_rule->patterns[i].pattern_type = expr_type2pattern_type(expr_item->expr_type);
if (TRUE == expr_item->is_hexbin && expr_rule->patterns[i].pattern_type != HS_PATTERN_TYPE_REG) {
region_str_len = strlen(sub_key_array[i]) * 8 + 1;
region_string = ALLOC(char, region_str_len);
region_str_len = hex2bin(sub_key_array[i], strlen(sub_key_array[i]), region_string, region_str_len);
}
@@ -656,8 +658,8 @@ struct hs_expr *expr_item_to_expr_rule(struct expr_item *expr_item, void *user_d
expr_rule->patterns[i].match_mode = expr_item->match_mode;
if (expr_rule->patterns[i].match_mode == HS_MATCH_MODE_SUB) {
expr_rule->patterns[i].l_offset = key_left_offset[i];
expr_rule->patterns[i].r_offset = key_right_offset[i];
expr_rule->patterns[i].start_offset = key_left_offset[i];
expr_rule->patterns[i].end_offset = key_right_offset[i];
}
}
expr_rule->expr_id = expr_item->item_id;
@@ -674,6 +676,9 @@ struct hs_expr *expr_item_to_expr_rule(struct expr_item *expr_item, void *user_d
// printf("expr_rule->patterns[%zu].r_offset:%d\n", i, expr_rule->patterns[i].r_offset);
// }
return expr_rule;
error:
FREE(expr_rule);
return NULL;
}
int expr_runtime_update(void *expr_runtime, void *expr_schema,
@@ -763,18 +768,11 @@ int expr_runtime_commit(void *expr_runtime, const char *table_name)
struct hs_expr *rules = NULL;
void **ex_data_array = NULL;
size_t rule_cnt = rcu_hash_list(expr_rt->htable, &ex_data_array);
//printf("rcu_hash_commit rule_cnt:%zu\n", rule_cnt);
if (rule_cnt > 0) {
rules = ALLOC(struct hs_expr, rule_cnt);
for (size_t i = 0; i < rule_cnt; i++) {
rules[i] = *(struct hs_expr *)ex_data_array[i];
// if (rules[i].expr_id == 13)
// {
// for (size_t j = 0; j < rules[i].n_patterns; j++)
// {
// printf("rules[%zu].patterns[%zu]:%s\n", i, j, rules[i].patterns[j].pat);
// }
// }
}
}
@@ -785,8 +783,7 @@ int expr_runtime_commit(void *expr_runtime, const char *table_name)
int ret = 0;
struct adapter_hs *new_adapter_hs = NULL;
struct adapter_hs *old_adapter_hs = NULL;
new_adapter_hs = adapter_hs_initialize(expr_rt->pattern_type, expr_rt->n_worker_thread,
rules, rule_cnt, expr_rt->logger);
new_adapter_hs = adapter_hs_initialize(expr_rt->n_worker_thread, rules, rule_cnt, expr_rt->logger);
if (NULL == new_adapter_hs) {
log_error(expr_rt->logger, MODULE_EXPR,
"[%s:%d] table[%s] rebuild adapter_hs engine failed when update %zu expr rules",
@@ -867,24 +864,17 @@ int expr_runtime_scan(struct expr_runtime *expr_rt, int thread_id, const char *d
return group_hit_cnt;
}
int expr_runtime_stream_open(struct expr_runtime *expr_rt, int thread_id)
struct adapter_hs_stream *expr_runtime_stream_open(struct expr_runtime *expr_rt, int thread_id)
{
if (NULL == expr_rt || thread_id < 0) {
return -1;
return NULL;
}
struct adapter_hs_stream *hs_stream = adapter_hs_stream_open(expr_rt->hs, thread_id);
if (NULL == hs_stream) {
return -1;
}
expr_rt->hs_stream = hs_stream;
return 0;
return adapter_hs_stream_open(expr_rt->hs, thread_id);
}
int expr_runtime_stream_scan(struct expr_runtime *expr_rt, const char *data, size_t data_len,
int vtable_id, struct maat_state *state)
int expr_runtime_stream_scan(struct expr_runtime *expr_rt, struct adapter_hs_stream *s_handle,
const char *data, size_t data_len, int vtable_id, struct maat_state *state)
{
if (0 == expr_rt->rule_num) {
//empty expr table
@@ -893,9 +883,7 @@ int expr_runtime_stream_scan(struct expr_runtime *expr_rt, const char *data, siz
size_t n_hit_item = 0;
struct hs_scan_result hit_results[MAX_SCANNER_HIT_ITEM_NUM] = {0};
int ret = adapter_hs_scan_stream(expr_rt->hs_stream, data, data_len,
hit_results, MAX_SCANNER_HIT_ITEM_NUM,
&n_hit_item);
int ret = adapter_hs_scan_stream(s_handle, data, data_len, hit_results, MAX_SCANNER_HIT_ITEM_NUM, &n_hit_item);
if (ret < 0) {
return -1;
}
@@ -928,12 +916,9 @@ int expr_runtime_stream_scan(struct expr_runtime *expr_rt, const char *data, siz
return group_hit_cnt;
}
void expr_runtime_stream_close(struct expr_runtime *expr_rt)
void expr_runtime_stream_close(struct adapter_hs_stream *s_handle)
{
if (expr_rt != NULL) {
adapter_hs_stream_close(expr_rt->hs_stream);
expr_rt->hs_stream = NULL;
}
adapter_hs_stream_close(s_handle);
}
void expr_runtime_scan_hit_inc(struct expr_runtime *expr_rt, int thread_id)

View File

@@ -94,60 +94,57 @@ void *plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
}
schema->table_id = item->valueint;
/* custom is optional */
item = cJSON_GetObjectItem(json, "custom");
if (item == NULL || item->type != cJSON_Object) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table %s has no custom column",
__FUNCTION__, __LINE__, table_name);
goto error;
}
if (item != NULL && item->type == cJSON_Object) {
custom_item = cJSON_GetObjectItem(item, "key");
if (NULL == custom_item || custom_item->type != cJSON_Number) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table: %s has no key column",
__FUNCTION__, __LINE__, table_name);
goto error;
}
schema->key_column = custom_item->valueint;
custom_item = cJSON_GetObjectItem(item, "key");
if (NULL == custom_item || custom_item->type != cJSON_Number) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table: %s has no key column",
__FUNCTION__, __LINE__, table_name);
goto error;
}
schema->key_column = custom_item->valueint;
custom_item = cJSON_GetObjectItem(item, "key_type");
if (NULL == custom_item || custom_item->type != cJSON_String) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table: %s has no key_type column",
__FUNCTION__, __LINE__, table_name);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "key_type");
if (NULL == custom_item || custom_item->type != cJSON_String) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table: %s has no key_type column",
__FUNCTION__, __LINE__, table_name);
goto error;
}
if (strcmp(custom_item->valuestring, "pointer") == 0) {
schema->key_type = PLUGIN_KEY_TYPE_POINTER;
} else if (strcmp(custom_item->valuestring, "integer") == 0) {
schema->key_type = PLUGIN_KEY_TYPE_INTEGER;
} else {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table: %s key_type:%s illegal",
__FUNCTION__, __LINE__, table_name,
custom_item->valuestring);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "tag");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
schema->rule_tag_column = custom_item->valueint;
}
if (strcmp(custom_item->valuestring, "pointer") == 0) {
schema->key_type = PLUGIN_KEY_TYPE_POINTER;
} else if (strcmp(custom_item->valuestring, "integer") == 0) {
schema->key_type = PLUGIN_KEY_TYPE_INTEGER;
} else {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table: %s key_type:%s illegal",
__FUNCTION__, __LINE__, table_name,
custom_item->valuestring);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "foreign");
if (custom_item != NULL) {
if (custom_item->type == cJSON_String) {
schema->n_foreign = read_integer_array(custom_item->valuestring,
schema->foreign_columns,
MAX_FOREIGN_CLMN_NUM);
} else if (custom_item->type == cJSON_Array) {
schema->n_foreign = cJSON_GetArraySize(custom_item);
for (int i = 0; i < schema->n_foreign; i++) {
cJSON *foreign_item = cJSON_GetArrayItem(custom_item, i);
assert(foreign_item->type == cJSON_Number);
schema->foreign_columns[i] = foreign_item->valueint;
custom_item = cJSON_GetObjectItem(item, "tag");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
schema->rule_tag_column = custom_item->valueint;
}
custom_item = cJSON_GetObjectItem(item, "foreign");
if (custom_item != NULL) {
if (custom_item->type == cJSON_String) {
schema->n_foreign = read_integer_array(custom_item->valuestring,
schema->foreign_columns,
MAX_FOREIGN_CLMN_NUM);
} else if (custom_item->type == cJSON_Array) {
schema->n_foreign = cJSON_GetArraySize(custom_item);
for (int i = 0; i < schema->n_foreign; i++)
{
cJSON *foreign_item = cJSON_GetArrayItem(custom_item, i);
assert(foreign_item->type == cJSON_Number);
schema->foreign_columns[i] = foreign_item->valueint;
}
}
}
}

View File

@@ -20,8 +20,7 @@
#define MODULE_VIRTUAL module_name_str("maat.virtual")
struct virtual_schema {
char physical_tables[MAX_PHYSICAL_TABLE_NUM][NAME_MAX];
size_t n_physical_table;
char physical_table[NAME_MAX];
int table_id;
struct table_manager *ref_tbl_mgr;
};
@@ -42,23 +41,15 @@ void *virtual_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->table_id = item->valueint;
item = cJSON_GetObjectItem(json, "physical_table");
if (NULL == item || item->type != cJSON_Array) {
if (NULL == item || item->type != cJSON_String) {
log_error(logger, MODULE_VIRTUAL,
"[%s:%d] virtual table %s has no physical_table column",
__FUNCTION__, __LINE__, table_name);
goto error;
}
int cnt = cJSON_GetArraySize(item);
for (int i = 0; i < cnt; i++) {
cJSON *tmp_item = cJSON_GetArrayItem(item, i);
if (tmp_item != NULL && tmp_item->type == cJSON_String) {
memcpy(schema->physical_tables[i], tmp_item->valuestring,
strlen(tmp_item->valuestring));
}
}
schema->n_physical_table = cnt;
memcpy(schema->physical_table, item->valuestring, strlen(item->valuestring));
return schema;
error:
FREE(schema);
@@ -80,7 +71,7 @@ int virtual_table_get_id(void *virtual_schema)
return schema->table_id;
}
size_t virtual_table_get_physical_table_id(void *virtual_schema, int physical_table_ids[])
int virtual_table_get_physical_table_id(void *virtual_schema)
{
if (NULL == virtual_schema) {
return 0;
@@ -88,10 +79,5 @@ size_t virtual_table_get_physical_table_id(void *virtual_schema, int physical_ta
struct virtual_schema *schema = (struct virtual_schema *)virtual_schema;
for (size_t i = 0; i < schema->n_physical_table; i++) {
int table_id = table_manager_get_table_id(schema->ref_tbl_mgr, schema->physical_tables[i]);
physical_table_ids[i] = table_id;
}
return schema->n_physical_table;
return table_manager_get_table_id(schema->ref_tbl_mgr, schema->physical_table);
}