Expose modutil_get_canonical in luafilter

Exposing the canonicalization code in the luafilter allows it to be used
on the Lua side of things. This should enable some pretty cool stuff to be
done with it.
This commit is contained in:
Markus Mäkelä 2018-03-21 00:17:47 +02:00
parent db9dc2d2d5
commit 8a397f574f
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
2 changed files with 47 additions and 21 deletions

View File

@ -90,6 +90,12 @@ The luafilter exposes three functions that can be called from the Lua script.
This function can only be called from the `routeQuery` entry point.
- `string lua_get_canonical()`
- Returns the canonical version of a query by replacing all user-defined constant values with question marks.
This function can only be called from the `routeQuery` entry point.
- `number id_gen()`
- This function generates unique integers that can be used to distinct

View File

@ -169,6 +169,25 @@ static int lua_qc_get_operation(lua_State* state)
return 1;
}
static int lua_get_canonical(lua_State* state)
{
int ibuf = lua_upvalueindex(1);
GWBUF *buf = *((GWBUF**)lua_touserdata(state, ibuf));
if (buf)
{
char* canon = modutil_get_canonical(buf);
lua_pushstring(state, canon);
MXS_FREE(canon);
}
else
{
lua_pushliteral(state, "");
}
return 1;
}
/**
* The Lua filter instance.
*/
@ -193,6 +212,26 @@ typedef struct
MXS_UPSTREAM up;
} LUA_SESSION;
void expose_functions(lua_State* state, GWBUF** active_buffer)
{
/** Expose an ID generation function */
lua_pushcfunction(state, id_gen);
lua_setglobal(state, "id_gen");
/** Expose a part of the query classifier API */
lua_pushlightuserdata(state, active_buffer);
lua_pushcclosure(state, lua_qc_get_type_mask, 1);
lua_setglobal(state, "lua_qc_get_type_mask");
lua_pushlightuserdata(state, active_buffer);
lua_pushcclosure(state, lua_qc_get_operation, 1);
lua_setglobal(state, "lua_qc_get_operation");
lua_pushlightuserdata(state, active_buffer);
lua_pushcclosure(state, lua_get_canonical, 1);
lua_setglobal(state, "lua_get_canonical");
}
/**
* Create a new instance of the Lua filter.
*
@ -244,15 +283,7 @@ createInstance(const char *name, char **options, MXS_CONFIG_PARAMETER *params)
lua_pop(my_instance->global_lua_state, -1); // Pop the error off the stack
}
/** Expose a part of the query classifier API */
lua_pushlightuserdata(my_instance->global_lua_state, &current_global_query);
lua_pushcclosure(my_instance->global_lua_state, lua_qc_get_type_mask, 1);
lua_setglobal(my_instance->global_lua_state, "lua_qc_get_type_mask");
lua_pushlightuserdata(my_instance->global_lua_state, &current_global_query);
lua_pushcclosure(my_instance->global_lua_state, lua_qc_get_operation, 1);
lua_setglobal(my_instance->global_lua_state, "lua_qc_get_operation");
expose_functions(my_instance->global_lua_state, &current_global_query);
}
}
else
@ -311,18 +342,7 @@ static MXS_FILTER_SESSION *newSession(MXS_FILTER *instance, MXS_SESSION *session
}
else
{
/** Expose an ID generation function */
lua_pushcfunction(my_session->lua_state, id_gen);
lua_setglobal(my_session->lua_state, "id_gen");
/** Expose a part of the query classifier API */
lua_pushlightuserdata(my_session->lua_state, &my_session->current_query);
lua_pushcclosure(my_session->lua_state, lua_qc_get_type_mask, 1);
lua_setglobal(my_session->lua_state, "lua_qc_get_type_mask");
lua_pushlightuserdata(my_session->lua_state, &my_session->current_query);
lua_pushcclosure(my_session->lua_state, lua_qc_get_operation, 1);
lua_setglobal(my_session->lua_state, "lua_qc_get_operation");
expose_functions(my_session->lua_state, &my_session->current_query);
/** Call the newSession entry point */
lua_getglobal(my_session->lua_state, "newSession");