X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Ffrontend%2Fquery.c;h=6e94f646f2f9d1c20dc2f12dbd30236283dda21a;hb=4065234b3744f0472ce33e4c7fda5bb8cdd1d2e3;hp=dddf8424ad0d89fc5c9a4b10be757c353b669aa0;hpb=a58f4c5ace18595f3cdaf731078028d747d5d9bd;p=sysdb.git diff --git a/src/frontend/query.c b/src/frontend/query.c index dddf842..6e94f64 100644 --- a/src/frontend/query.c +++ b/src/frontend/query.c @@ -1,6 +1,6 @@ /* * SysDB - src/frontend/query.c - * Copyright (C) 2013 Sebastian 'tokkee' Harl + * Copyright (C) 2013-2014 Sebastian 'tokkee' Harl * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -41,6 +41,15 @@ * private helper functions */ +static int +list_tojson(sdb_store_obj_t *obj, + sdb_store_matcher_t __attribute__((unused)) *filter, + void *user_data) +{ + sdb_store_json_formatter_t *f = user_data; + return sdb_store_json_emit(f, obj); +} /* list_tojson */ + static int lookup_tojson(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, void *user_data) @@ -64,13 +73,13 @@ sdb_fe_query(sdb_conn_t *conn) return -1; parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf), - (int)conn->cmd_len); + (int)conn->cmd_len, conn->errbuf); 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); + sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s': %s", + query, sdb_strbuf_string(conn->errbuf)); return -1; } @@ -98,16 +107,7 @@ sdb_fe_query(sdb_conn_t *conn) } if (node) { - if (sdb_fe_analyze(node)) { - 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 verify query '%s'", - query); - status = -1; - } - else - status = sdb_fe_exec(conn, node); + status = sdb_fe_exec(conn, node); sdb_object_deref(SDB_OBJ(node)); } @@ -124,7 +124,7 @@ sdb_fe_fetch(sdb_conn_t *conn) if ((! conn) || (conn->cmd != CONNECTION_FETCH)) return -1; - if (conn->cmd_len < sizeof(type)) { + if (conn->cmd_len < sizeof(uint32_t)) { sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for " "FETCH command", conn->cmd_len); sdb_strbuf_sprintf(conn->errbuf, "FETCH: Invalid command length %d", @@ -133,10 +133,11 @@ sdb_fe_fetch(sdb_conn_t *conn) } type = sdb_proto_get_int(conn->buf, 0); - strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(type), - conn->cmd_len - sizeof(type)); + strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t), + conn->cmd_len - sizeof(uint32_t)); name[sizeof(name) - 1] = '\0'; - return sdb_fe_exec_fetch(conn, type, name, /* filter = */ NULL); + /* TODO: support other types besides hosts */ + return sdb_fe_exec_fetch(conn, type, name, NULL, /* filter = */ NULL); } /* sdb_fe_fetch */ int @@ -166,7 +167,7 @@ sdb_fe_lookup(sdb_conn_t *conn) const char *matcher; size_t matcher_len; - uint32_t type; + int type; int status; conn_matcher_t m_node = { @@ -180,7 +181,7 @@ sdb_fe_lookup(sdb_conn_t *conn) if ((! conn) || (conn->cmd != CONNECTION_LOOKUP)) return -1; - if (conn->cmd_len < sizeof(type)) { + if (conn->cmd_len < sizeof(uint32_t)) { sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for " "LOOKUP command", conn->cmd_len); sdb_strbuf_sprintf(conn->errbuf, "LOOKUP: Invalid command length %d", @@ -189,27 +190,33 @@ sdb_fe_lookup(sdb_conn_t *conn) } type = sdb_proto_get_int(conn->buf, 0); - matcher = sdb_strbuf_string(conn->buf) + sizeof(type); - matcher_len = conn->cmd_len - sizeof(type); - m = sdb_fe_parse_matcher(matcher, (int)matcher_len); + matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t); + matcher_len = conn->cmd_len - sizeof(uint32_t); + m = sdb_fe_parse_matcher(matcher, (int)matcher_len, conn->errbuf); if (! m) { char expr[matcher_len + 1]; strncpy(expr, matcher, sizeof(expr)); expr[sizeof(expr) - 1] = '\0'; sdb_log(SDB_LOG_ERR, "frontend: Failed to parse " - "lookup condition '%s'", expr); + "lookup condition '%s': %s", expr, + sdb_strbuf_string(conn->errbuf)); return -1; } node.type = type; m_node.matcher = m; - if (sdb_fe_analyze(SDB_CONN_NODE(&node))) { + /* run analyzer separately; parse_matcher is missing + * the right context to do so */ + if (sdb_fe_analyze(SDB_CONN_NODE(&node), conn->errbuf)) { char expr[matcher_len + 1]; + char err[sdb_strbuf_len(conn->errbuf) + sizeof(expr) + 64]; strncpy(expr, matcher, sizeof(expr)); expr[sizeof(expr) - 1] = '\0'; - sdb_log(SDB_LOG_ERR, "frontend: Failed to verify " - "lookup condition '%s'", expr); + snprintf(err, sizeof(err), "Failed to parse " + "lookup condition '%s': %s", expr, + sdb_strbuf_string(conn->errbuf)); + sdb_strbuf_sprintf(conn->errbuf, "%s", err); status = -1; } else @@ -231,7 +238,7 @@ sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node) if (CONN_FETCH(node)->filter) filter = CONN_FETCH(node)->filter->matcher; return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->type, - CONN_FETCH(node)->name, filter); + CONN_FETCH(node)->host, CONN_FETCH(node)->name, filter); case CONNECTION_LIST: if (CONN_LIST(node)->filter) filter = CONN_LIST(node)->filter->matcher; @@ -256,30 +263,48 @@ sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node) } /* sdb_fe_exec */ int -sdb_fe_exec_fetch(sdb_conn_t *conn, int type, const char *name, - sdb_store_matcher_t *filter) +sdb_fe_exec_fetch(sdb_conn_t *conn, int type, + const char *hostname, const char *name, sdb_store_matcher_t *filter) { - sdb_strbuf_t *buf; - sdb_store_obj_t *host; uint32_t res_type = htonl(CONNECTION_FETCH); - /* XXX: support other types */ - if (type != SDB_HOST) { - sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d " - "in FETCH command", type); - sdb_strbuf_sprintf(conn->errbuf, - "FETCH: Invalid object type %d", type); + sdb_store_obj_t *host; + sdb_store_obj_t *obj; + + sdb_store_json_formatter_t *f; + sdb_strbuf_t *buf; + + if ((! hostname) || ((type == SDB_HOST) && name) + || ((type != SDB_HOST) && (! name))) { + /* This is a programming error, not something the client did wrong */ + sdb_strbuf_sprintf(conn->errbuf, "INTERNAL ERROR: invalid " + "arguments to sdb_fe_exec_fetch(%s, %s, %s)", + SDB_STORE_TYPE_TO_NAME(type), hostname, name); return -1; } - host = sdb_store_get_host(name); - if (! host) { - sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': " - "not found", name); - - sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name); + host = sdb_store_get_host(hostname); + if ((! host) || (filter + && (! sdb_store_matcher_matches(filter, host, NULL)))) { + sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s: " + "host %s not found", SDB_STORE_TYPE_TO_NAME(type), + name, hostname); return -1; } + if (type == SDB_HOST) { + obj = host; + } + else { + obj = sdb_store_get_child(host, type, name); + if ((! obj) || (filter + && (! sdb_store_matcher_matches(filter, obj, NULL)))) { + sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s.%s: " + "%s not found", SDB_STORE_TYPE_TO_NAME(type), + hostname, name, name); + return -1; + } + sdb_object_deref(SDB_OBJ(host)); + } buf = sdb_strbuf_create(1024); if (! buf) { @@ -290,50 +315,50 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, int type, const char *name, sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); sdb_strbuf_destroy(buf); - sdb_object_deref(SDB_OBJ(host)); + sdb_object_deref(SDB_OBJ(obj)); + return -1; + } + f = sdb_store_json_formatter(buf, type, /* flags = */ 0); + if (! f) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to create " + "JSON formatter to handle FETCH command: %s", + sdb_strerror(errno, errbuf, sizeof(errbuf))); + + sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); + sdb_strbuf_destroy(buf); + sdb_object_deref(SDB_OBJ(obj)); return -1; } sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t)); - if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) { + if (sdb_store_json_emit_full(f, obj, filter)) { sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize " - "host '%s' to JSON", name); + "%s %s.%s to JSON", SDB_STORE_TYPE_TO_NAME(type), + hostname, name); sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); sdb_strbuf_destroy(buf); - sdb_object_deref(SDB_OBJ(host)); + free(f); + sdb_object_deref(SDB_OBJ(obj)); return -1; } + sdb_store_json_finish(f); 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)); + free(f); + sdb_object_deref(SDB_OBJ(obj)); return 0; } /* sdb_fe_exec_fetch */ int 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; - } + sdb_store_json_formatter_t *f; + sdb_strbuf_t *buf; buf = sdb_strbuf_create(1024); if (! buf) { @@ -346,19 +371,33 @@ sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter) sdb_strbuf_destroy(buf); return -1; } + f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY); + if (! f) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to create " + "JSON formatter to handle LIST command: %s", + sdb_strerror(errno, errbuf, sizeof(errbuf))); + + sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); + sdb_strbuf_destroy(buf); + return -1; + } sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t)); - if (sdb_store_tojson(buf, filter, flags)) { + if (sdb_store_scan(type, /* m = */ NULL, filter, list_tojson, f)) { sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize " "store to JSON"); sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); sdb_strbuf_destroy(buf); + free(f); return -1; } + sdb_store_json_finish(f); sdb_connection_send(conn, CONNECTION_DATA, (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf)); sdb_strbuf_destroy(buf); + free(f); return 0; } /* sdb_fe_exec_list */ @@ -371,15 +410,6 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, int type, sdb_store_json_formatter_t *f; sdb_strbuf_t *buf; - /* XXX: support other types */ - if (type != SDB_HOST) { - sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d " - "in LOOKUP command", type); - sdb_strbuf_sprintf(conn->errbuf, - "LOOKUP: Invalid object type %d", type); - return -1; - } - buf = sdb_strbuf_create(1024); if (! buf) { char errbuf[1024]; @@ -390,7 +420,7 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, int type, sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); return -1; } - f = sdb_store_json_formatter(buf); + f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY); if (! f) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "frontend: Failed to create " @@ -403,19 +433,18 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, int type, } sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t)); - sdb_strbuf_append(buf, "["); - if (sdb_store_scan(SDB_HOST, m, filter, lookup_tojson, f)) { - sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts"); - sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts"); + if (sdb_store_scan(type, m, filter, lookup_tojson, f)) { + sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup %ss", + SDB_STORE_TYPE_TO_NAME(type)); + sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup %ss", + SDB_STORE_TYPE_TO_NAME(type)); sdb_strbuf_destroy(buf); free(f); return -1; } sdb_store_json_finish(f); - sdb_strbuf_append(buf, "]"); - sdb_connection_send(conn, CONNECTION_DATA, (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf)); sdb_strbuf_destroy(buf);