diff --git a/src/frontend/query.c b/src/frontend/query.c
index 39b5d53004da6774ad6395ade472e22cc33971b7..dddf8424ad0d89fc5c9a4b10be757c353b669aa0 100644 (file)
--- a/src/frontend/query.c
+++ b/src/frontend/query.c
* private helper functions
*/
-typedef struct {
- sdb_strbuf_t *buf;
- sdb_store_matcher_t *filter;
-
- size_t last_len;
-} tojson_data_t;
-
static int
-lookup_tojson(sdb_store_obj_t *obj, void *user_data)
+lookup_tojson(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
+ void *user_data)
{
- tojson_data_t *data = user_data;
- int status;
-
- if (data->filter && (! sdb_store_matcher_matches(data->filter, obj, NULL)))
- return 0;
-
- if (sdb_strbuf_len(data->buf) > data->last_len)
- sdb_strbuf_append(data->buf, ",");
- data->last_len = sdb_strbuf_len(data->buf);
- status = sdb_store_host_tojson(obj, data->buf,
- data->filter, /* flags = */ 0);
- return status;
+ sdb_store_json_formatter_t *f = user_data;
+ return sdb_store_json_emit_full(f, obj, filter);
} /* lookup_tojson */
/*
}
if (node) {
- status = sdb_fe_exec(conn, 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);
sdb_object_deref(SDB_OBJ(node));
}
uint32_t type;
int status;
+ conn_matcher_t m_node = {
+ { SDB_OBJECT_INIT, CONNECTION_MATCHER }, NULL
+ };
+ conn_lookup_t node = {
+ { SDB_OBJECT_INIT, CONNECTION_LOOKUP },
+ -1, &m_node, NULL
+ };
+
if ((! conn) || (conn->cmd != CONNECTION_LOOKUP))
return -1;
return -1;
}
- status = sdb_fe_exec_lookup(conn, type, m, /* filter = */ NULL);
+ node.type = type;
+ m_node.matcher = m;
+
+ if (sdb_fe_analyze(SDB_CONN_NODE(&node))) {
+ char expr[matcher_len + 1];
+ strncpy(expr, matcher, sizeof(expr));
+ expr[sizeof(expr) - 1] = '\0';
+ sdb_log(SDB_LOG_ERR, "frontend: Failed to verify "
+ "lookup condition '%s'", expr);
+ status = -1;
+ }
+ else
+ status = sdb_fe_exec_lookup(conn, type, m, /* filter = */ NULL);
sdb_object_deref(SDB_OBJ(m));
return status;
} /* sdb_fe_lookup */
sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
sdb_store_matcher_t *m, sdb_store_matcher_t *filter)
{
- tojson_data_t data = { NULL, filter, 0 };
uint32_t res_type = htonl(CONNECTION_LOOKUP);
+ 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 "
return -1;
}
- data.buf = sdb_strbuf_create(1024);
- if (! data.buf) {
+ buf = sdb_strbuf_create(1024);
+ if (! buf) {
char errbuf[1024];
sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
"buffer to handle LOOKUP command: %s",
sdb_strerror(errno, errbuf, sizeof(errbuf)));
sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
- sdb_strbuf_destroy(data.buf);
return -1;
}
+ f = sdb_store_json_formatter(buf);
+ if (! f) {
+ char errbuf[1024];
+ sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
+ "JSON formatter to handle LOOKUP command: %s",
+ sdb_strerror(errno, errbuf, sizeof(errbuf)));
- sdb_strbuf_memcpy(data.buf, &res_type, sizeof(uint32_t));
- sdb_strbuf_append(data.buf, "[");
+ sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
+ sdb_strbuf_destroy(buf);
+ return -1;
+ }
+
+ sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
+ sdb_strbuf_append(buf, "[");
- /* Let the JSON serializer handle the filter instead of the scanner. Else,
- * we'd have to filter twice -- once in the scanner and then again in the
- * serializer. */
- data.last_len = sdb_strbuf_len(data.buf);
- if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, &data)) {
+ 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");
- sdb_strbuf_destroy(data.buf);
+ sdb_strbuf_destroy(buf);
+ free(f);
return -1;
}
+ sdb_store_json_finish(f);
- sdb_strbuf_append(data.buf, "]");
+ sdb_strbuf_append(buf, "]");
sdb_connection_send(conn, CONNECTION_DATA,
- (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf));
- sdb_strbuf_destroy(data.buf);
+ (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
+ sdb_strbuf_destroy(buf);
+ free(f);
return 0;
} /* sdb_fe_exec_lookup */