Code

When querying services/metrics skip hosts without such children.
[sysdb.git] / src / frontend / query.c
index bbf98530afedcb106abc34174e9c61e9dc8a55ae..1751ba57e7a9a241b148cbc4f4dc391787cf0c44 100644 (file)
@@ -31,6 +31,7 @@
 #include "frontend/connection-private.h"
 #include "frontend/parser.h"
 #include "utils/error.h"
+#include "utils/proto.h"
 #include "utils/strbuf.h"
 
 #include <errno.h>
@@ -91,7 +92,8 @@ sdb_fe_query(sdb_conn_t *conn)
 
        switch (sdb_llist_len(parsetree)) {
                case 0:
-                       /* skipping empty command */
+                       /* skipping empty command; send back an empty reply */
+                       sdb_connection_send(conn, CONNECTION_DATA, 0, NULL);
                        break;
                case 1:
                        node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
@@ -134,9 +136,21 @@ sdb_fe_fetch(sdb_conn_t *conn)
 int
 sdb_fe_list(sdb_conn_t *conn)
 {
+       int type = SDB_HOST;
+
        if ((! conn) || (conn->cmd != CONNECTION_LIST))
                return -1;
-       return sdb_fe_exec_list(conn, /* filter = */ NULL);
+
+       if (conn->cmd_len == sizeof(uint32_t))
+               type = sdb_proto_get_int(conn->buf, 0);
+       else if (conn->cmd_len) {
+               sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
+                               "LIST command", conn->cmd_len);
+               sdb_strbuf_sprintf(conn->errbuf, "LIST: Invalid command length %d",
+                               conn->cmd_len);
+               return -1;
+       }
+       return sdb_fe_exec_list(conn, type, /* filter = */ NULL);
 } /* sdb_fe_list */
 
 int
@@ -180,7 +194,7 @@ sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
                case CONNECTION_LIST:
                        if (CONN_LIST(node)->filter)
                                filter = CONN_LIST(node)->filter->matcher;
-                       return sdb_fe_exec_list(conn, filter);
+                       return sdb_fe_exec_list(conn, CONN_LIST(node)->type, filter);
                case CONNECTION_LOOKUP:
                        if (CONN_LOOKUP(node)->matcher)
                                m = CONN_LOOKUP(node)->matcher->matcher;
@@ -205,6 +219,7 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name,
 {
        sdb_strbuf_t *buf;
        sdb_store_obj_t *host;
+       uint32_t res_type = htonl(CONNECTION_FETCH);
 
        host = sdb_store_get_host(name);
        if (! host) {
@@ -228,6 +243,7 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name,
                return -1;
        }
 
+       sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
        if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) {
                sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
                                "host '%s' to JSON", name);
@@ -237,7 +253,7 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name,
                return -1;
        }
 
-       sdb_connection_send(conn, CONNECTION_OK,
+       sdb_connection_send(conn, CONNECTION_DATA,
                        (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
        sdb_strbuf_destroy(buf);
        sdb_object_deref(SDB_OBJ(host));
@@ -245,9 +261,28 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name,
 } /* sdb_fe_exec_fetch */
 
 int
-sdb_fe_exec_list(sdb_conn_t *conn, sdb_store_matcher_t *filter)
+sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter)
 {
        sdb_strbuf_t *buf;
+       uint32_t res_type = htonl(CONNECTION_LIST);
+
+       int flags;
+
+       if (type == SDB_HOST)
+               flags = SDB_SKIP_ALL;
+       else if (type == SDB_SERVICE)
+               flags = (SDB_SKIP_ALL & (~SDB_SKIP_SERVICES))
+                       | SDB_SKIP_EMPTY_SERVICES;
+       else if (type == SDB_METRIC)
+               flags = (SDB_SKIP_ALL & (~SDB_SKIP_METRICS))
+                       | SDB_SKIP_EMPTY_METRICS;
+       else {
+               sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d "
+                               "for LIST command", type);
+               sdb_strbuf_sprintf(conn->errbuf,
+                               "LIST: Invalid object type %d", type);
+               return -1;
+       }
 
        buf = sdb_strbuf_create(1024);
        if (! buf) {
@@ -261,7 +296,8 @@ sdb_fe_exec_list(sdb_conn_t *conn, sdb_store_matcher_t *filter)
                return -1;
        }
 
-       if (sdb_store_tojson(buf, filter, /* flags = */ SDB_SKIP_ALL)) {
+       sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
+       if (sdb_store_tojson(buf, filter, flags)) {
                sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
                                "store to JSON");
                sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
@@ -269,7 +305,7 @@ sdb_fe_exec_list(sdb_conn_t *conn, sdb_store_matcher_t *filter)
                return -1;
        }
 
-       sdb_connection_send(conn, CONNECTION_OK,
+       sdb_connection_send(conn, CONNECTION_DATA,
                        (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
        sdb_strbuf_destroy(buf);
        return 0;
@@ -280,6 +316,7 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
                sdb_store_matcher_t *filter)
 {
        tojson_data_t data = { NULL, filter, 0 };
+       uint32_t res_type = htonl(CONNECTION_LOOKUP);
 
        data.buf = sdb_strbuf_create(1024);
        if (! data.buf) {
@@ -293,6 +330,7 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
                return -1;
        }
 
+       sdb_strbuf_memcpy(data.buf, &res_type, sizeof(uint32_t));
        sdb_strbuf_append(data.buf, "[");
 
        /* Let the JSON serializer handle the filter instead of the scanner. Else,
@@ -308,7 +346,7 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
 
        sdb_strbuf_append(data.buf, "]");
 
-       sdb_connection_send(conn, CONNECTION_OK,
+       sdb_connection_send(conn, CONNECTION_DATA,
                        (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf));
        sdb_strbuf_destroy(data.buf);
        return 0;
@@ -320,6 +358,7 @@ sdb_fe_exec_timeseries(sdb_conn_t *conn,
                sdb_timeseries_opts_t *opts)
 {
        sdb_strbuf_t *buf;
+       uint32_t res_type = htonl(CONNECTION_TIMESERIES);
 
        buf = sdb_strbuf_create(1024);
        if (! buf) {
@@ -332,6 +371,7 @@ sdb_fe_exec_timeseries(sdb_conn_t *conn,
                return -1;
        }
 
+       sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
        if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) {
                sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series");
                sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch time-series");
@@ -339,7 +379,7 @@ sdb_fe_exec_timeseries(sdb_conn_t *conn,
                return -1;
        }
 
-       sdb_connection_send(conn, CONNECTION_OK,
+       sdb_connection_send(conn, CONNECTION_DATA,
                        (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
        sdb_strbuf_destroy(buf);
        return 0;