diff --git a/src/frontend/query.c b/src/frontend/query.c
index 35ff1026d1d84686d10468b9fe5046d5a14d7a41..6015d7dd17275f27d01ef1f312321ca22d4975b9 100644 (file)
--- a/src/frontend/query.c
+++ b/src/frontend/query.c
#include "core/store.h"
#include "frontend/connection-private.h"
#include "core/store.h"
#include "frontend/connection-private.h"
-#include "frontend/parser.h"
+#include "parser/ast.h"
+#include "parser/parser.h"
#include "utils/error.h"
#include "utils/proto.h"
#include "utils/strbuf.h"
#include "utils/error.h"
#include "utils/proto.h"
#include "utils/strbuf.h"
return sdb_store_json_emit_full(f, obj, filter);
} /* lookup_tojson */
return sdb_store_json_emit_full(f, obj, filter);
} /* lookup_tojson */
+static int
+query_exec(sdb_conn_t *conn, sdb_ast_node_t *ast)
+{
+ sdb_store_query_t *q;
+ sdb_strbuf_t *buf;
+ int status;
+
+ q = sdb_store_query_prepare(ast);
+ if (! q) {
+ /* this shouldn't happen */
+ sdb_strbuf_sprintf(conn->errbuf, "failed to compile AST");
+ sdb_log(SDB_LOG_ERR, "frontend: failed to compile AST");
+ return -1;
+ }
+
+ buf = sdb_strbuf_create(1024);
+ if (! buf) {
+ sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
+ sdb_object_deref(SDB_OBJ(q));
+ return -1;
+ }
+ status = sdb_store_query_execute(q, buf, conn->errbuf);
+ if (status < 0) {
+ 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 execute query '%s'", query);
+ }
+ else
+ sdb_connection_send(conn, status,
+ (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
+
+ sdb_strbuf_destroy(buf);
+ sdb_object_deref(SDB_OBJ(q));
+ return status < 0 ? status : 0;
+} /* query_exec */
+
/*
* public API
*/
/*
* public API
*/
sdb_fe_query(sdb_conn_t *conn)
{
sdb_llist_t *parsetree;
sdb_fe_query(sdb_conn_t *conn)
{
sdb_llist_t *parsetree;
- sdb_conn_node_t *node = NULL;
+ sdb_ast_node_t *ast = NULL;
int status = 0;
if ((! conn) || (conn->cmd != SDB_CONNECTION_QUERY))
return -1;
int status = 0;
if ((! conn) || (conn->cmd != SDB_CONNECTION_QUERY))
return -1;
- parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf),
+ parsetree = sdb_parser_parse(sdb_strbuf_string(conn->buf),
(int)conn->cmd_len, conn->errbuf);
if (! parsetree) {
char query[conn->cmd_len + 1];
(int)conn->cmd_len, conn->errbuf);
if (! parsetree) {
char query[conn->cmd_len + 1];
sdb_connection_send(conn, SDB_CONNECTION_DATA, 0, NULL);
break;
case 1:
sdb_connection_send(conn, SDB_CONNECTION_DATA, 0, NULL);
break;
case 1:
- node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
+ ast = SDB_AST_NODE(sdb_llist_get(parsetree, 0));
break;
default:
break;
default:
sdb_llist_len(parsetree) - 1,
sdb_llist_len(parsetree) == 2 ? "" : "s",
query);
sdb_llist_len(parsetree) - 1,
sdb_llist_len(parsetree) == 2 ? "" : "s",
query);
- node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
+ ast = SDB_AST_NODE(sdb_llist_get(parsetree, 0));
}
}
}
}
- if (node) {
- status = sdb_fe_exec(conn, node);
- sdb_object_deref(SDB_OBJ(node));
+ if (ast) {
+ status = query_exec(conn, ast);
+ sdb_object_deref(SDB_OBJ(ast));
}
}
-
sdb_llist_destroy(parsetree);
return status;
} /* sdb_fe_query */
sdb_llist_destroy(parsetree);
return status;
} /* sdb_fe_query */
sdb_fe_fetch(sdb_conn_t *conn)
{
char name[conn->cmd_len + 1];
sdb_fe_fetch(sdb_conn_t *conn)
{
char name[conn->cmd_len + 1];
- int type;
+ uint32_t type;
if ((! conn) || (conn->cmd != SDB_CONNECTION_FETCH))
return -1;
if ((! conn) || (conn->cmd != SDB_CONNECTION_FETCH))
return -1;
return -1;
}
return -1;
}
- type = sdb_proto_get_int(conn->buf, 0);
+ sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t),
conn->cmd_len - sizeof(uint32_t));
name[sizeof(name) - 1] = '\0';
/* TODO: support other types besides hosts */
strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t),
conn->cmd_len - sizeof(uint32_t));
name[sizeof(name) - 1] = '\0';
/* TODO: support other types besides hosts */
- return sdb_fe_exec_fetch(conn, type, name, NULL, /* filter = */ NULL);
+ return sdb_fe_exec_fetch(conn, (int)type, name, NULL, /* filter = */ NULL);
} /* sdb_fe_fetch */
int
sdb_fe_list(sdb_conn_t *conn)
{
} /* sdb_fe_fetch */
int
sdb_fe_list(sdb_conn_t *conn)
{
- int type = SDB_HOST;
+ uint32_t type = SDB_HOST;
if ((! conn) || (conn->cmd != SDB_CONNECTION_LIST))
return -1;
if (conn->cmd_len == sizeof(uint32_t))
if ((! conn) || (conn->cmd != SDB_CONNECTION_LIST))
return -1;
if (conn->cmd_len == sizeof(uint32_t))
- type = sdb_proto_get_int(conn->buf, 0);
+ sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
else if (conn->cmd_len) {
sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
"LIST command", conn->cmd_len);
else if (conn->cmd_len) {
sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
"LIST command", conn->cmd_len);
conn->cmd_len);
return -1;
}
conn->cmd_len);
return -1;
}
- return sdb_fe_exec_list(conn, type, /* filter = */ NULL);
+ return sdb_fe_exec_list(conn, (int)type, /* filter = */ NULL);
} /* sdb_fe_list */
int
} /* sdb_fe_list */
int
const char *matcher;
size_t matcher_len;
const char *matcher;
size_t matcher_len;
- int type;
+ uint32_t type;
int status;
conn_matcher_t m_node = {
int status;
conn_matcher_t m_node = {
conn->cmd_len);
return -1;
}
conn->cmd_len);
return -1;
}
- type = sdb_proto_get_int(conn->buf, 0);
+ sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t);
matcher_len = conn->cmd_len - sizeof(uint32_t);
matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t);
matcher_len = conn->cmd_len - sizeof(uint32_t);
return -1;
}
return -1;
}
- node.type = type;
+ node.type = (int)type;
m_node.matcher = m;
/* run analyzer separately; parse_matcher is missing
m_node.matcher = m;
/* run analyzer separately; parse_matcher is missing
status = -1;
}
else
status = -1;
}
else
- status = sdb_fe_exec_lookup(conn, type, m, /* filter = */ NULL);
+ status = sdb_fe_exec_lookup(conn, (int)type, m, /* filter = */ NULL);
sdb_object_deref(SDB_OBJ(m));
return status;
} /* sdb_fe_lookup */
sdb_object_deref(SDB_OBJ(m));
return status;
} /* sdb_fe_lookup */
-int
-sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
-{
- sdb_store_matcher_t *m = NULL, *filter = NULL;
-
- if (! node)
- return -1;
-
- switch (node->cmd) {
- case SDB_CONNECTION_FETCH:
- if (CONN_FETCH(node)->filter)
- filter = CONN_FETCH(node)->filter->matcher;
- return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->type,
- CONN_FETCH(node)->host, CONN_FETCH(node)->name, filter);
- case SDB_CONNECTION_LIST:
- if (CONN_LIST(node)->filter)
- filter = CONN_LIST(node)->filter->matcher;
- return sdb_fe_exec_list(conn, CONN_LIST(node)->type, filter);
- case SDB_CONNECTION_LOOKUP:
- if (CONN_LOOKUP(node)->matcher)
- m = CONN_LOOKUP(node)->matcher->matcher;
- if (CONN_LOOKUP(node)->filter)
- filter = CONN_LOOKUP(node)->filter->matcher;
- return sdb_fe_exec_lookup(conn,
- CONN_LOOKUP(node)->type, m, filter);
- case SDB_CONNECTION_TIMESERIES:
- return sdb_fe_exec_timeseries(conn,
- CONN_TS(node)->hostname, CONN_TS(node)->metric,
- &CONN_TS(node)->opts);
-
- default:
- sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
- return -1;
- }
- return -1;
-} /* sdb_fe_exec */
+/*
+ * TODO: let the functions above build an AST; then drop sdb_fe_exec_*.
+ */
int
sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
int
sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
SDB_STORE_TYPE_TO_NAME(type), hostname, name);
return -1;
}
SDB_STORE_TYPE_TO_NAME(type), hostname, name);
return -1;
}
+ if (type == SDB_HOST)
+ name = hostname;
host = sdb_store_get_host(hostname);
if ((! host) || (filter
host = sdb_store_get_host(hostname);
if ((! host) || (filter
sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s: "
"host %s not found", SDB_STORE_TYPE_TO_NAME(type),
name, hostname);
sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s: "
"host %s not found", SDB_STORE_TYPE_TO_NAME(type),
name, hostname);
+ sdb_object_deref(SDB_OBJ(host));
return -1;
}
if (type == SDB_HOST) {
return -1;
}
if (type == SDB_HOST) {
sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s.%s: "
"%s not found", SDB_STORE_TYPE_TO_NAME(type),
hostname, name, name);
sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s.%s: "
"%s not found", SDB_STORE_TYPE_TO_NAME(type),
hostname, name, name);
+ if (obj)
+ sdb_object_deref(SDB_OBJ(obj));
+ sdb_object_deref(SDB_OBJ(host));
return -1;
}
sdb_object_deref(SDB_OBJ(host));
}
return -1;
}
sdb_object_deref(SDB_OBJ(host));
}
+ host = NULL;
buf = sdb_strbuf_create(1024);
if (! buf) {
buf = sdb_strbuf_create(1024);
if (! buf) {
return 0;
} /* sdb_fe_exec_lookup */
return 0;
} /* sdb_fe_exec_lookup */
-int
-sdb_fe_exec_timeseries(sdb_conn_t *conn,
- const char *hostname, const char *metric,
- sdb_timeseries_opts_t *opts)
-{
- sdb_strbuf_t *buf;
- uint32_t res_type = htonl(SDB_CONNECTION_TIMESERIES);
-
- buf = sdb_strbuf_create(1024);
- if (! buf) {
- char errbuf[1024];
- sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
- "buffer to handle TIMESERIES command: %s",
- sdb_strerror(errno, errbuf, sizeof(errbuf)));
-
- sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
- 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");
- sdb_strbuf_destroy(buf);
- return -1;
- }
-
- sdb_connection_send(conn, SDB_CONNECTION_DATA,
- (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
- sdb_strbuf_destroy(buf);
- return 0;
-} /* sdb_fe_exec_timeseries */
-
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */