plugin table support integer&pointer key type
This commit is contained in:
@@ -60,12 +60,11 @@ TEST(EXDataRuntime, Update) {
|
||||
struct ex_data_runtime *ex_data_rt = ex_data_runtime_new(table_id, ex_container_free, garbage_bin, g_logger);
|
||||
struct ex_data_schema *ex_schema = ex_data_schema_new(ex_data_new_cb, ex_data_free_cb, ex_data_dup_cb,
|
||||
0, &ex_data_counter);
|
||||
ex_data_runtime_set_schema(ex_data_rt, ex_schema);
|
||||
|
||||
const char *row1 = "1\t192.168.0.1\tmahuateng\t1\t0";
|
||||
const char *key1 = "192.168.0.1";
|
||||
size_t key1_len = strlen(key1);
|
||||
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, row1, key1, key1_len);
|
||||
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, row1, key1, key1_len);
|
||||
EXPECT_EQ(ex_data_counter, 1);
|
||||
|
||||
struct ex_container *ex_container = ex_container_new(ex_data, NULL);
|
||||
@@ -75,21 +74,21 @@ TEST(EXDataRuntime, Update) {
|
||||
const char *row2 = "2\t192.168.0.2\tliyanhong\t1\t0";
|
||||
const char *key2 = "192.168.0.2";
|
||||
size_t key2_len = strlen(key2);
|
||||
ex_data = ex_data_runtime_row2ex_data(ex_data_rt, row2, key2, key2_len);
|
||||
ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, row2, key2, key2_len);
|
||||
ex_container = ex_container_new(ex_data, NULL);
|
||||
ret = ex_data_runtime_add_ex_container(ex_data_rt, key2, key2_len, ex_container);
|
||||
EXPECT_EQ(ret, 0);
|
||||
|
||||
ex_data_runtime_commit(ex_data_rt);
|
||||
|
||||
void *res_data1 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, "192.168.0.1", 11);
|
||||
void *res_data1 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, ex_schema, "192.168.0.1", 11);
|
||||
EXPECT_TRUE(res_data1 != NULL);
|
||||
|
||||
struct user_info *info = (struct user_info *)res_data1;
|
||||
EXPECT_EQ(0, strcmp(info->name, "mahuateng"));
|
||||
EXPECT_EQ(info->id, 1);
|
||||
|
||||
void *res_data2 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, "192.168.0.2", 11);
|
||||
void *res_data2 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, ex_schema, "192.168.0.2", 11);
|
||||
EXPECT_TRUE(res_data2 != NULL);
|
||||
|
||||
info = (struct user_info *)res_data2;
|
||||
|
||||
@@ -1414,6 +1414,7 @@ protected:
|
||||
|
||||
struct maat_options *opts = maat_options_new();
|
||||
maat_options_set_redis(opts, redis_ip, redis_port, redis_db);
|
||||
maat_options_set_deferred_load_on(opts);
|
||||
maat_options_set_logger(opts, g_logger);
|
||||
maat_options_set_accept_tags(opts, accept_tags);
|
||||
|
||||
@@ -1449,6 +1450,107 @@ TEST_F(PluginTable, Callback) {
|
||||
EXPECT_EQ(ret, 0);
|
||||
}
|
||||
|
||||
struct plugin_ud {
|
||||
char key[256];
|
||||
char value[256];
|
||||
int id;
|
||||
int ref_cnt;
|
||||
};
|
||||
|
||||
void plugin_EX_new_cb(int table_id, const char *key, const char *table_line,
|
||||
void **ad, long argl, void *argp)
|
||||
{
|
||||
int *counter = (int *)argp;
|
||||
int valid = 0, tag = 0;
|
||||
struct plugin_ud *ud = ALLOC(struct plugin_ud, 1);
|
||||
|
||||
int ret = sscanf(table_line, "%d\t%s\t%s\t%d\t%d", &(ud->id), ud->key, ud->value, &valid, &tag);
|
||||
EXPECT_EQ(ret, 5);
|
||||
ud->ref_cnt = 1;
|
||||
*ad = ud;
|
||||
(*counter)++;
|
||||
}
|
||||
|
||||
void plugin_EX_free_cb(int table_id, void **ad, long argl, void *argp)
|
||||
{
|
||||
struct plugin_ud *ud = (struct plugin_ud *)(*ad);
|
||||
if ((__sync_sub_and_fetch(&ud->ref_cnt, 1) == 0)) {
|
||||
free(ud);
|
||||
*ad = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void plugin_EX_dup_cb(int table_id, void **to, void **from, long argl, void *argp)
|
||||
{
|
||||
struct plugin_ud *ud = (struct plugin_ud *)(*from);
|
||||
__sync_add_and_fetch(&(ud->ref_cnt), 1);
|
||||
*to = ud;
|
||||
}
|
||||
|
||||
TEST_F(PluginTable, EX_DATA) {
|
||||
const char *table_name = "TEST_PLUGIN_EXDATA_TABLE";
|
||||
struct maat *maat_instance = PluginTable::_shared_maat_instance;
|
||||
|
||||
int table_id = maat_get_table_id(maat_instance, table_name);
|
||||
ASSERT_GT(table_id, 0);
|
||||
|
||||
int plugin_ex_data_counter = 0;
|
||||
int ret = maat_plugin_table_ex_schema_register(maat_instance, table_id,
|
||||
plugin_EX_new_cb,
|
||||
plugin_EX_free_cb,
|
||||
plugin_EX_dup_cb,
|
||||
0, &plugin_ex_data_counter);
|
||||
EXPECT_EQ(ret, 0);
|
||||
EXPECT_EQ(plugin_ex_data_counter, 4);
|
||||
|
||||
const char *key1 = "HeBei";
|
||||
struct plugin_ud *ud = NULL;
|
||||
ud = (struct plugin_ud *)maat_plugin_table_get_ex_data(maat_instance, table_id, key1);
|
||||
ASSERT_TRUE(ud != NULL);
|
||||
EXPECT_STREQ(ud->value, "Shijiazhuang");
|
||||
EXPECT_EQ(ud->id, 1);
|
||||
plugin_EX_free_cb(table_id, (void **)&ud, 0, NULL);
|
||||
|
||||
const char *key2 = "ShanDong";
|
||||
ud = (struct plugin_ud *)maat_plugin_table_get_ex_data(maat_instance, table_id, key2);
|
||||
ASSERT_TRUE(ud != NULL);
|
||||
EXPECT_STREQ(ud->value, "Jinan");
|
||||
EXPECT_EQ(ud->id, 3);
|
||||
plugin_EX_free_cb(table_id, (void **)&ud, 0, NULL);
|
||||
}
|
||||
|
||||
TEST_F(PluginTable, KEY_TYPE) {
|
||||
const char *table_name = "TEST_PLUGIN_KEY_TYPE_TABLE";
|
||||
struct maat *maat_instance = PluginTable::_shared_maat_instance;
|
||||
|
||||
int table_id = maat_get_table_id(maat_instance, table_name);
|
||||
ASSERT_GT(table_id, 0);
|
||||
|
||||
int plugin_ex_data_counter = 0;
|
||||
int ret = maat_plugin_table_ex_schema_register(maat_instance, table_id,
|
||||
plugin_EX_new_cb,
|
||||
plugin_EX_free_cb,
|
||||
plugin_EX_dup_cb,
|
||||
0, &plugin_ex_data_counter);
|
||||
EXPECT_EQ(ret, 0);
|
||||
EXPECT_EQ(plugin_ex_data_counter, 4);
|
||||
|
||||
long long key1 = 11111111;
|
||||
struct plugin_ud *ud = NULL;
|
||||
ud = (struct plugin_ud *)maat_plugin_table_get_ex_data(maat_instance, table_id, (char *)&key1);
|
||||
ASSERT_TRUE(ud != NULL);
|
||||
EXPECT_STREQ(ud->value, "Shijiazhuang");
|
||||
EXPECT_EQ(ud->id, 1);
|
||||
plugin_EX_free_cb(table_id, (void **)&ud, 0, NULL);
|
||||
|
||||
long long key2 = 33333333;
|
||||
ud = (struct plugin_ud *)maat_plugin_table_get_ex_data(maat_instance, table_id, (char *)&key2);
|
||||
ASSERT_TRUE(ud != NULL);
|
||||
EXPECT_STREQ(ud->value, "Jinan");
|
||||
EXPECT_EQ(ud->id, 3);
|
||||
plugin_EX_free_cb(table_id, (void **)&ud, 0, NULL);
|
||||
}
|
||||
|
||||
class IPPluginTable : public testing::Test
|
||||
{
|
||||
protected:
|
||||
@@ -2380,6 +2482,7 @@ protected:
|
||||
|
||||
g_logger = log_handle_create("./maat_framework_gtest.log", 0);
|
||||
assert(g_logger != NULL);
|
||||
maat_options_set_deferred_load_on(opts);
|
||||
maat_options_set_logger(opts, g_logger);
|
||||
_shared_maat_instance = maat_new(opts, table_info_path);
|
||||
maat_options_free(opts);
|
||||
|
||||
@@ -2441,6 +2441,24 @@
|
||||
"3\tcccc\t11\t1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"table_name": "TEST_PLUGIN_EXDATA_TABLE",
|
||||
"table_content": [
|
||||
"1\tHeBei\tShijiazhuang\t1\t0",
|
||||
"2\tHeNan\tZhengzhou\t1\t0",
|
||||
"3\tShanDong\tJinan\t1\t0",
|
||||
"4\tShanXi\tTaiyuan\t1\t0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"table_name": "TEST_PLUGIN_KEY_TYPE_TABLE",
|
||||
"table_content": [
|
||||
"1\t11111111\tShijiazhuang\t1\t0",
|
||||
"2\t22222222\tZhengzhou\t1\t0",
|
||||
"3\t33333333\tJinan\t1\t0",
|
||||
"4\t44444444\tTaiyuan\t1\t0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"table_name": "TEST_EFFECTIVE_RANGE_TABLE",
|
||||
"table_content": [
|
||||
|
||||
@@ -148,6 +148,7 @@
|
||||
"valid_column":4,
|
||||
"custom": {
|
||||
"key":1,
|
||||
"key_type":"pointer",
|
||||
"tag":3
|
||||
}
|
||||
},
|
||||
@@ -191,6 +192,7 @@
|
||||
"valid_column":4,
|
||||
"custom": {
|
||||
"key":1,
|
||||
"key_type":"integer",
|
||||
"tag":5
|
||||
}
|
||||
},
|
||||
@@ -201,6 +203,7 @@
|
||||
"valid_column":4,
|
||||
"custom": {
|
||||
"key":2,
|
||||
"key_type":"pointer",
|
||||
"tag":3,
|
||||
"foreign": [6,8]
|
||||
}
|
||||
@@ -212,8 +215,8 @@
|
||||
"valid_column":4,
|
||||
"custom": {
|
||||
"key":2,
|
||||
"tag":5,
|
||||
"estimate_size": 1024
|
||||
"key_type":"pointer",
|
||||
"tag":5
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -223,6 +226,7 @@
|
||||
"valid_column":14,
|
||||
"custom": {
|
||||
"key":2,
|
||||
"key_type":"pointer",
|
||||
"tag":18
|
||||
}
|
||||
},
|
||||
@@ -524,5 +528,16 @@
|
||||
"source_port":"VIRTUAL_PORT_SOURCE",
|
||||
"dest_port":"VIRTUAL_PORT_DESTINATION"
|
||||
}
|
||||
},
|
||||
{
|
||||
"table_id":45,
|
||||
"table_name":"TEST_PLUGIN_KEY_TYPE_TABLE",
|
||||
"table_type":"plugin",
|
||||
"valid_column":4,
|
||||
"custom": {
|
||||
"key":2,
|
||||
"key_type":"integer",
|
||||
"tag":5
|
||||
}
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user