From: Sebastian Harl Date: Sat, 2 Aug 2014 18:14:50 +0000 (+0200) Subject: frontend: Split up command_handle() into several sdb_fe_ functions. X-Git-Tag: sysdb-0.4.0~48 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=d48884911904c87e0143b70a1a102d4cf25d638b;p=sysdb.git frontend: Split up command_handle() into several sdb_fe_ functions. One big function is not very nice to handle. --- diff --git a/src/frontend/connection.c b/src/frontend/connection.c index 2fdc5e9..6888e39 100644 --- a/src/frontend/connection.c +++ b/src/frontend/connection.c @@ -29,7 +29,6 @@ #include "core/object.h" #include "core/plugin.h" #include "frontend/connection-private.h" -#include "frontend/parser.h" #include "utils/error.h" #include "utils/strbuf.h" #include "utils/proto.h" @@ -265,102 +264,23 @@ command_handle(sdb_conn_t *conn) sdb_log(SDB_LOG_DEBUG, "frontend: Handling command %u (len: %u)", conn->cmd, conn->cmd_len); - switch (conn->cmd) { - case CONNECTION_PING: - status = sdb_connection_ping(conn); - break; - case CONNECTION_STARTUP: - status = sdb_fe_session_start(conn); - break; - - case CONNECTION_QUERY: - { - sdb_llist_t *parsetree; - sdb_conn_node_t *node = NULL; - - parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf), - (int)conn->cmd_len); - if (! parsetree) { - char query[conn->cmd_len + 1]; - strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len); - query[sizeof(query) - 1] = '\0'; - sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s'", - query); - status = -1; - break; - } - - switch (sdb_llist_len(parsetree)) { - case 0: - /* skipping empty command */ - break; - case 1: - node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0)); - break; - - default: - { - char query[conn->cmd_len + 1]; - strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len); - query[sizeof(query) - 1] = '\0'; - sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %d command%s " - "in multi-statement query '%s'", - sdb_llist_len(parsetree) - 1, - sdb_llist_len(parsetree) == 2 ? "" : "s", - query); - node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0)); - } - } - - if (node) { - status = sdb_fe_exec(conn, node); - sdb_object_deref(SDB_OBJ(node)); - } - - sdb_llist_destroy(parsetree); - break; - } - - case CONNECTION_FETCH: - { - char hostname[conn->cmd_len + 1]; - strncpy(hostname, sdb_strbuf_string(conn->buf), conn->cmd_len); - hostname[sizeof(hostname) - 1] = '\0'; - status = sdb_fe_exec_fetch(conn, hostname, /* filter = */ NULL); - break; - } - case CONNECTION_LIST: - status = sdb_fe_exec_list(conn, /* filter = */ NULL); - break; - case CONNECTION_LOOKUP: - { - sdb_store_matcher_t *m; - - m = sdb_fe_parse_matcher(sdb_strbuf_string(conn->buf), - (int)conn->cmd_len); - if (! m) { - char expr[conn->cmd_len + 1]; - strncpy(expr, sdb_strbuf_string(conn->buf), conn->cmd_len); - expr[sizeof(expr) - 1] = '\0'; - sdb_log(SDB_LOG_ERR, "frontend: Failed to parse " - "lookup condition '%s'", expr); - status = -1; - break; - } - - status = sdb_fe_exec_lookup(conn, m, /* filter = */ NULL); - sdb_object_deref(SDB_OBJ(m)); - break; - } - - default: - { - sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command %#x", - conn->cmd); - sdb_strbuf_sprintf(conn->errbuf, "Invalid command %#x", conn->cmd); - status = -1; - break; - } + if (conn->cmd == CONNECTION_PING) + status = sdb_connection_ping(conn); + else if (conn->cmd == CONNECTION_STARTUP) + status = sdb_fe_session_start(conn); + else if (conn->cmd == CONNECTION_QUERY) + status = sdb_fe_query(conn); + else if (conn->cmd == CONNECTION_FETCH) + status = sdb_fe_fetch(conn); + else if (conn->cmd == CONNECTION_LIST) + status = sdb_fe_list(conn); + else if (conn->cmd == CONNECTION_LOOKUP) + status = sdb_fe_lookup(conn); + else { + sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command %#x", + conn->cmd); + sdb_strbuf_sprintf(conn->errbuf, "Invalid command %#x", conn->cmd); + status = -1; } if (status) diff --git a/src/frontend/query.c b/src/frontend/query.c index fda93f6..585222d 100644 --- a/src/frontend/query.c +++ b/src/frontend/query.c @@ -29,10 +29,12 @@ #include "core/store.h" #include "frontend/connection-private.h" +#include "frontend/parser.h" #include "utils/error.h" #include "utils/strbuf.h" #include +#include /* * private helper functions @@ -66,6 +68,92 @@ lookup_tojson(sdb_store_obj_t *obj, void *user_data) * public API */ +int +sdb_fe_query(sdb_conn_t *conn) +{ + sdb_llist_t *parsetree; + sdb_conn_node_t *node = NULL; + int status = 0; + + parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf), + (int)conn->cmd_len); + if (! parsetree) { + char query[conn->cmd_len + 1]; + strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len); + query[sizeof(query) - 1] = '\0'; + sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s'", + query); + return -1; + } + + switch (sdb_llist_len(parsetree)) { + case 0: + /* skipping empty command */ + break; + case 1: + node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0)); + break; + + default: + { + char query[conn->cmd_len + 1]; + strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len); + query[sizeof(query) - 1] = '\0'; + sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %d command%s " + "in multi-statement query '%s'", + sdb_llist_len(parsetree) - 1, + sdb_llist_len(parsetree) == 2 ? "" : "s", + query); + node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0)); + } + } + + if (node) { + status = sdb_fe_exec(conn, node); + sdb_object_deref(SDB_OBJ(node)); + } + + sdb_llist_destroy(parsetree); + return status; +} /* sdb_fe_query */ + +int +sdb_fe_fetch(sdb_conn_t *conn) +{ + char hostname[conn->cmd_len + 1]; + strncpy(hostname, sdb_strbuf_string(conn->buf), conn->cmd_len); + hostname[sizeof(hostname) - 1] = '\0'; + return sdb_fe_exec_fetch(conn, hostname, /* filter = */ NULL); +} /* sdb_fe_fetch */ + +int +sdb_fe_list(sdb_conn_t *conn) +{ + return sdb_fe_exec_list(conn, /* filter = */ NULL); +} /* sdb_fe_list */ + +int +sdb_fe_lookup(sdb_conn_t *conn) +{ + sdb_store_matcher_t *m; + int status; + + m = sdb_fe_parse_matcher(sdb_strbuf_string(conn->buf), + (int)conn->cmd_len); + if (! m) { + char expr[conn->cmd_len + 1]; + strncpy(expr, sdb_strbuf_string(conn->buf), conn->cmd_len); + expr[sizeof(expr) - 1] = '\0'; + sdb_log(SDB_LOG_ERR, "frontend: Failed to parse " + "lookup condition '%s'", expr); + return -1; + } + + status = sdb_fe_exec_lookup(conn, m, /* filter = */ NULL); + sdb_object_deref(SDB_OBJ(m)); + return status; +} /* sdb_fe_lookup */ + int sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node) { diff --git a/src/include/frontend/connection.h b/src/include/frontend/connection.h index bff3f3b..16ad39a 100644 --- a/src/include/frontend/connection.h +++ b/src/include/frontend/connection.h @@ -165,6 +165,25 @@ sdb_fe_session_start(sdb_conn_t *conn); * store access */ +/* + * sdb_fe_query, sdb_fe_fetch, sdb_fe_list, sdb_fe_lookup: + * Handle the CONNECTION_QUERY, CONNECTION_FETCH, CONNECTION_LIST, and + * CONNECTION_LOOKUP commands respectively. It is expected that the current + * command has been initialized already. + * + * Returns: + * - 0 on success + * - a negative value else + */ +int +sdb_fe_query(sdb_conn_t *conn); +int +sdb_fe_fetch(sdb_conn_t *conn); +int +sdb_fe_list(sdb_conn_t *conn); +int +sdb_fe_lookup(sdb_conn_t *conn); + /* * sdb_fe_exec_fetch: * Execute the 'FETCH' command. Send the named host, serialized as JSON, to