diff --git a/server/modules/filter/hintfilter/hintfilter.cc b/server/modules/filter/hintfilter/hintfilter.cc index 253a1e736..26a2871bc 100644 --- a/server/modules/filter/hintfilter/hintfilter.cc +++ b/server/modules/filter/hintfilter/hintfilter.cc @@ -101,7 +101,7 @@ extern "C" */ static MXS_FILTER* createInstance(const char* name, MXS_CONFIG_PARAMETER* params) { - return static_cast(new (std::nothrow)HINT_INSTANCE); + return static_cast(new(std::nothrow) HINT_INSTANCE); } /** @@ -113,7 +113,7 @@ static MXS_FILTER* createInstance(const char* name, MXS_CONFIG_PARAMETER* params */ static MXS_FILTER_SESSION* newSession(MXS_FILTER* instance, MXS_SESSION* session) { - return static_cast(new (std::nothrow)HINT_SESSION); + return static_cast(new(std::nothrow) HINT_SESSION); } /** @@ -179,7 +179,7 @@ static int routeQuery(MXS_FILTER* instance, MXS_FILTER_SESSION* session, GWBUF* if (modutil_is_SQL(queue) && gwbuf_length(queue) > 5) { - process_hints(my_session, queue); + my_session->process_hints(queue); } /* Now process the request */ diff --git a/server/modules/filter/hintfilter/hintparser.cc b/server/modules/filter/hintfilter/hintparser.cc index d7d0533a2..9b0c31367 100644 --- a/server/modules/filter/hintfilter/hintparser.cc +++ b/server/modules/filter/hintfilter/hintparser.cc @@ -325,7 +325,7 @@ HINT* process_definition(InputIter it, InputIter end) } template -HINT* process_comment(HINT_SESSION* session, InputIter it, InputIter end) +HINT* HINT_SESSION::process_comment(InputIter it, InputIter end) { HINT* rval = nullptr; @@ -339,15 +339,15 @@ HINT* process_comment(HINT_SESSION* session, InputIter it, InputIter 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) { - if (!session->stack.empty()) + if (!stack.empty()) { - hint_free(session->stack.back()); - session->stack.pop_back(); + hint_free(stack.back()); + stack.pop_back(); } } else if (t.type == TOK_STRING) @@ -373,28 +373,28 @@ HINT* process_comment(HINT_SESSION* session, InputIter it, InputIter end) if (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) { 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 - session->named_hints[key] = hint_dup(rval); - session->stack.push_back(hint_dup(rval)); + named_hints[key] = hint_dup(rval); + stack.push_back(hint_dup(rval)); } } 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 - session->stack.push_back(hint_dup(it->second)); + stack.push_back(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; } -void process_hints(HINT_SESSION* session, GWBUF* buffer) +void HINT_SESSION::process_hints(GWBUF* buffer) { mxs::Buffer buf(buffer); 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) { @@ -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(); diff --git a/server/modules/filter/hintfilter/mysqlhint.hh b/server/modules/filter/hintfilter/mysqlhint.hh index 41950c3d9..210041f52 100644 --- a/server/modules/filter/hintfilter/mysqlhint.hh +++ b/server/modules/filter/hintfilter/mysqlhint.hh @@ -18,7 +18,7 @@ /** * The hint instance structure */ -struct HINT_INSTANCE: public MXS_FILTER +struct HINT_INSTANCE : public MXS_FILTER { int sessions = 0; }; @@ -26,11 +26,13 @@ struct HINT_INSTANCE: public MXS_FILTER /** * A hint parser session structure */ -struct HINT_SESSION: public MXS_FILTER_SESSION +struct HINT_SESSION : public MXS_FILTER_SESSION { - MXS_DOWNSTREAM down; - std::vector stack; + MXS_DOWNSTREAM down; + std::vector stack; std::unordered_map named_hints; -}; -void process_hints(HINT_SESSION* session, GWBUF* buffer); + template + HINT* process_comment(InputIter it, InputIter end); + void process_hints(GWBUF* buffer); +}; diff --git a/server/modules/filter/hintfilter/test/test_hintparser.cc b/server/modules/filter/hintfilter/test/test_hintparser.cc index 7775aca74..2dad9da7a 100644 --- a/server/modules/filter/hintfilter/test/test_hintparser.cc +++ b/server/modules/filter/hintfilter/test/test_hintparser.cc @@ -78,7 +78,7 @@ void test_parse(const std::string& input, int expected_type) for (auto comment : get_all_comments(input.begin(), input.end())) { 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) { @@ -102,7 +102,7 @@ void count_hints(const std::string& input, int num_expected) 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; } @@ -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 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 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 start route to server server1 */", HINT_ROUTE_TO_NAMED_SERVER); test_parse("SELECT 1 /* maxscale end*/", 0);