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:
@ -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.
|
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()`
|
- `number id_gen()`
|
||||||
|
|
||||||
- This function generates unique integers that can be used to distinct
|
- This function generates unique integers that can be used to distinct
|
||||||
|
|||||||
@ -169,6 +169,25 @@ static int lua_qc_get_operation(lua_State* state)
|
|||||||
return 1;
|
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.
|
* The Lua filter instance.
|
||||||
*/
|
*/
|
||||||
@ -193,6 +212,26 @@ typedef struct
|
|||||||
MXS_UPSTREAM up;
|
MXS_UPSTREAM up;
|
||||||
} LUA_SESSION;
|
} 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.
|
* 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
|
lua_pop(my_instance->global_lua_state, -1); // Pop the error off the stack
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Expose a part of the query classifier API */
|
expose_functions(my_instance->global_lua_state, ¤t_global_query);
|
||||||
lua_pushlightuserdata(my_instance->global_lua_state, ¤t_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, ¤t_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");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -311,18 +342,7 @@ static MXS_FILTER_SESSION *newSession(MXS_FILTER *instance, MXS_SESSION *session
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/** Expose an ID generation function */
|
expose_functions(my_session->lua_state, &my_session->current_query);
|
||||||
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");
|
|
||||||
|
|
||||||
/** Call the newSession entry point */
|
/** Call the newSession entry point */
|
||||||
lua_getglobal(my_session->lua_state, "newSession");
|
lua_getglobal(my_session->lua_state, "newSession");
|
||||||
|
|||||||
Reference in New Issue
Block a user