MXS-2302: Do hint processing in a member function

Also fixed a unit test failure.
This commit is contained in:
Markus Mäkelä
2019-02-18 11:12:27 +02:00
parent f106864659
commit 1d49e45036
4 changed files with 30 additions and 28 deletions

View File

@ -179,7 +179,7 @@ static int routeQuery(MXS_FILTER* instance, MXS_FILTER_SESSION* session, GWBUF*
if (modutil_is_SQL(queue) && gwbuf_length(queue) > 5) if (modutil_is_SQL(queue) && gwbuf_length(queue) > 5)
{ {
process_hints(my_session, queue); my_session->process_hints(queue);
} }
/* Now process the request */ /* Now process the request */

View File

@ -325,7 +325,7 @@ HINT* process_definition(InputIter it, InputIter end)
} }
template<class InputIter> template<class InputIter>
HINT* process_comment(HINT_SESSION* session, InputIter it, InputIter end) HINT* HINT_SESSION::process_comment(InputIter it, InputIter end)
{ {
HINT* rval = nullptr; HINT* rval = nullptr;
@ -339,15 +339,15 @@ HINT* process_comment(HINT_SESSION* session, InputIter it, InputIter end)
{ {
if ((rval = process_definition(it, end))) if ((rval = process_definition(it, end)))
{ {
session->stack.push_back(hint_dup(rval)); stack.push_back(hint_dup(rval));
} }
} }
else if (t.type == TOK_STOP) else if (t.type == TOK_STOP)
{ {
if (!session->stack.empty()) if (!stack.empty())
{ {
hint_free(session->stack.back()); hint_free(stack.back());
session->stack.pop_back(); stack.pop_back();
} }
} }
else if (t.type == TOK_STRING) else if (t.type == TOK_STRING)
@ -373,28 +373,28 @@ HINT* process_comment(HINT_SESSION* session, InputIter it, InputIter end)
if (hint) if (hint)
{ {
// Preparation of a named hint // Preparation of a named hint
session->named_hints[key] = hint_dup(hint); named_hints[key] = hint_dup(hint);
} }
} }
else if (t.type == TOK_START) else if (t.type == TOK_START)
{ {
if ((rval = process_definition(it, end))) if ((rval = process_definition(it, end)))
{ {
if (session->named_hints.count(key) == 0) if (named_hints.count(key) == 0)
{ {
// New hint defined, push it on to the stack // New hint defined, push it on to the stack
session->named_hints[key] = hint_dup(rval); named_hints[key] = hint_dup(rval);
session->stack.push_back(hint_dup(rval)); stack.push_back(hint_dup(rval));
} }
} }
else if (next_token(&it, end).type == TOK_END) else if (next_token(&it, end).type == TOK_END)
{ {
auto it = session->named_hints.find(key); auto it = named_hints.find(key);
if (it != session->named_hints.end()) if (it != named_hints.end())
{ {
// We're starting an already define named hint // We're starting an already define named hint
session->stack.push_back(hint_dup(it->second)); stack.push_back(hint_dup(it->second));
rval = hint_dup(it->second); rval = hint_dup(it->second);
} }
} }
@ -410,13 +410,13 @@ HINT* process_comment(HINT_SESSION* session, InputIter it, InputIter end)
return rval; return rval;
} }
void process_hints(HINT_SESSION* session, GWBUF* buffer) void HINT_SESSION::process_hints(GWBUF* buffer)
{ {
mxs::Buffer buf(buffer); mxs::Buffer buf(buffer);
for (auto comment : get_all_comments(std::next(buf.begin(), 5), buf.end())) for (auto comment : get_all_comments(std::next(buf.begin(), 5), buf.end()))
{ {
HINT* hint = process_comment(session, comment.first, comment.second); HINT* hint = process_comment(comment.first, comment.second);
if (hint) if (hint)
{ {
@ -424,9 +424,9 @@ void process_hints(HINT_SESSION* session, GWBUF* buffer)
} }
} }
if (!buffer->hint && !session->stack.empty()) if (!buffer->hint && !stack.empty())
{ {
buffer->hint = hint_dup(session->stack.back()); buffer->hint = hint_dup(stack.back());
} }
buf.release(); buf.release();

View File

@ -31,6 +31,8 @@ struct HINT_SESSION: public MXS_FILTER_SESSION
MXS_DOWNSTREAM down; MXS_DOWNSTREAM down;
std::vector<HINT*> stack; std::vector<HINT*> stack;
std::unordered_map<std::string, HINT*> named_hints; std::unordered_map<std::string, HINT*> named_hints;
};
void process_hints(HINT_SESSION* session, GWBUF* buffer); template<class InputIter>
HINT* process_comment(InputIter it, InputIter end);
void process_hints(GWBUF* buffer);
};

View File

@ -78,7 +78,7 @@ void test_parse(const std::string& input, int expected_type)
for (auto comment : get_all_comments(input.begin(), input.end())) for (auto comment : get_all_comments(input.begin(), input.end()))
{ {
std::string comment_str(comment.first, comment.second); std::string comment_str(comment.first, comment.second);
HINT* hint = process_comment(&session, comment.first, comment.second); HINT* hint = session.process_comment(comment.first, comment.second);
if (!hint && expected_type != 0) if (!hint && expected_type != 0)
{ {
@ -102,7 +102,7 @@ void count_hints(const std::string& input, int num_expected)
for (auto comment : get_all_comments(input.begin(), input.end())) for (auto comment : get_all_comments(input.begin(), input.end()))
{ {
if (process_comment(&session, comment.first, comment.second)) if (session.process_comment(comment.first, comment.second))
{ {
++n; ++n;
} }
@ -188,7 +188,7 @@ int main(int argc, char** argv)
test_parse("SELECT 1 /* maxscale route to slave */", HINT_ROUTE_TO_SLAVE); test_parse("SELECT 1 /* maxscale route to slave */", HINT_ROUTE_TO_SLAVE);
test_parse("SELECT 1 /* maxscale route to last*/", HINT_ROUTE_TO_LAST_USED); test_parse("SELECT 1 /* maxscale route to last*/", HINT_ROUTE_TO_LAST_USED);
test_parse("SELECT 1 /* maxscale route to server server1 */", HINT_ROUTE_TO_NAMED_SERVER); test_parse("SELECT 1 /* maxscale route to server server1 */", HINT_ROUTE_TO_NAMED_SERVER);
test_parse("SELECT 1 /* maxscale test1 prepare route to server server1 */", HINT_ROUTE_TO_NAMED_SERVER); test_parse("SELECT 1 /* maxscale test1 prepare route to server server1 */", 0);
test_parse("SELECT 1 /* maxscale test1 start route to server server1 */", HINT_ROUTE_TO_NAMED_SERVER); test_parse("SELECT 1 /* maxscale test1 start route to server server1 */", HINT_ROUTE_TO_NAMED_SERVER);
test_parse("SELECT 1 /* maxscale start route to server server1 */", HINT_ROUTE_TO_NAMED_SERVER); test_parse("SELECT 1 /* maxscale start route to server server1 */", HINT_ROUTE_TO_NAMED_SERVER);
test_parse("SELECT 1 /* maxscale end*/", 0); test_parse("SELECT 1 /* maxscale end*/", 0);