2023-01-30 21:59:35 +08:00
|
|
|
/*
|
|
|
|
|
**********************************************************************************************
|
2023-05-04 17:10:19 +08:00
|
|
|
* File: maat_virtual.c
|
2023-01-30 21:59:35 +08:00
|
|
|
* Description:
|
|
|
|
|
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
|
|
|
|
* Date: 2022-10-31
|
2023-05-04 17:10:19 +08:00
|
|
|
* Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved.
|
2023-01-30 21:59:35 +08:00
|
|
|
***********************************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
#include "maat_kv.h"
|
|
|
|
|
#include "maat_utils.h"
|
|
|
|
|
#include "log/log.h"
|
|
|
|
|
#include "maat_virtual.h"
|
2023-02-07 11:25:31 +08:00
|
|
|
#include "maat_rule.h"
|
|
|
|
|
#include "maat_table.h"
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
#define MODULE_VIRTUAL module_name_str("maat.virtual")
|
|
|
|
|
|
|
|
|
|
struct virtual_schema {
|
2023-05-31 14:04:35 +08:00
|
|
|
int vtable_id;
|
|
|
|
|
int physical_table_id;
|
2023-02-09 22:13:15 +08:00
|
|
|
struct table_manager *ref_tbl_mgr;
|
2023-01-30 21:59:35 +08:00
|
|
|
};
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
void *virtual_schema_new(cJSON *json, struct table_manager *tbl_mgr,
|
|
|
|
|
const char *table_name, struct log_handle *logger)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
2023-02-20 10:57:40 +08:00
|
|
|
struct virtual_schema *schema = ALLOC(struct virtual_schema, 1);
|
|
|
|
|
schema->ref_tbl_mgr = tbl_mgr;
|
|
|
|
|
|
|
|
|
|
cJSON *item = cJSON_GetObjectItem(json, "table_id");
|
|
|
|
|
if (NULL == item || item->type != cJSON_Number) {
|
|
|
|
|
log_error(logger, MODULE_VIRTUAL,
|
2023-05-30 16:16:18 +08:00
|
|
|
"[%s:%d] virtual table:<%s> schema has no table_id column",
|
2023-03-02 14:52:31 +08:00
|
|
|
__FUNCTION__, __LINE__, table_name);
|
2023-02-20 10:57:40 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2023-05-31 14:04:35 +08:00
|
|
|
schema->vtable_id = item->valueint;
|
2023-02-20 10:57:40 +08:00
|
|
|
|
|
|
|
|
item = cJSON_GetObjectItem(json, "physical_table");
|
2023-03-22 11:10:00 +08:00
|
|
|
if (NULL == item || item->type != cJSON_String) {
|
2023-02-03 17:28:14 +08:00
|
|
|
log_error(logger, MODULE_VIRTUAL,
|
2023-05-30 16:16:18 +08:00
|
|
|
"[%s:%d] virtual table:<%s> schema has no physical_table column",
|
2023-03-02 14:52:31 +08:00
|
|
|
__FUNCTION__, __LINE__, table_name);
|
2023-02-20 10:57:40 +08:00
|
|
|
goto error;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
2023-01-31 20:39:53 +08:00
|
|
|
|
2023-05-31 14:04:35 +08:00
|
|
|
schema->physical_table_id = table_manager_get_table_id(tbl_mgr, item->valuestring);
|
|
|
|
|
if (schema->physical_table_id < 0) {
|
|
|
|
|
log_error(logger, MODULE_VIRTUAL,
|
|
|
|
|
"[%s:%d] virtual table:<%s>'s physical table:<%s> unregistered.",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name, item->valuestring);
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2023-03-22 11:10:00 +08:00
|
|
|
|
2023-02-20 10:57:40 +08:00
|
|
|
return schema;
|
|
|
|
|
error:
|
|
|
|
|
FREE(schema);
|
|
|
|
|
return NULL;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
2023-01-31 20:39:53 +08:00
|
|
|
|
|
|
|
|
void virtual_schema_free(void *virtual_schema)
|
|
|
|
|
{
|
|
|
|
|
FREE(virtual_schema);
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
int virtual_table_get_physical_table_id(void *virtual_schema)
|
2023-02-07 11:25:31 +08:00
|
|
|
{
|
|
|
|
|
if (NULL == virtual_schema) {
|
2023-02-22 15:08:52 +08:00
|
|
|
return 0;
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct virtual_schema *schema = (struct virtual_schema *)virtual_schema;
|
2023-02-20 10:57:40 +08:00
|
|
|
|
2023-05-31 14:04:35 +08:00
|
|
|
return schema->physical_table_id;
|
2023-01-31 20:39:53 +08:00
|
|
|
}
|