Code

FETCH: Add parameter for fetching the main object without children.
authorSebastian Harl <sh@tokkee.org>
Fri, 9 Oct 2015 16:56:12 +0000 (18:56 +0200)
committerSebastian Harl <sh@tokkee.org>
Fri, 9 Oct 2015 16:56:12 +0000 (18:56 +0200)
That is, exclude any attributes or child objects.

Use this to simplify the TIMESERIES fetcher which no longer requires an
attribute callback.

src/core/memstore_exec.c
src/frontend/query.c
src/include/parser/ast.h
src/parser/ast.c
src/parser/grammar.y

index 802e70c..ce8f825 100644 (file)
@@ -86,7 +86,7 @@ lookup_tojson(sdb_memstore_obj_t *obj, sdb_memstore_matcher_t *filter,
 static int
 exec_fetch(sdb_memstore_t *store,
                sdb_store_writer_t *w, sdb_object_t *wd, sdb_strbuf_t *errbuf,
-               int type, const char *hostname, const char *name,
+               int type, const char *hostname, const char *name, bool full,
                sdb_memstore_matcher_t *filter)
 {
        sdb_memstore_obj_t *host;
@@ -135,7 +135,13 @@ exec_fetch(sdb_memstore_t *store,
 
        if (type != SDB_HOST)
                status = sdb_memstore_emit(obj->parent, w, wd);
-       if (status || sdb_memstore_emit_full(obj, filter, w, wd)) {
+       if (! status) {
+               if (full)
+                       status = sdb_memstore_emit_full(obj, filter, w, wd);
+               else
+                       status = sdb_memstore_emit(obj, w, wd);
+       }
+       if (status) {
                sdb_log(SDB_LOG_ERR, "memstore: Failed to serialize "
                                "%s %s.%s to JSON", SDB_STORE_TYPE_TO_NAME(type),
                                hostname, name);
@@ -205,7 +211,7 @@ sdb_memstore_query_execute(sdb_memstore_t *store, sdb_memstore_query_t *q,
        case SDB_AST_TYPE_FETCH:
                return exec_fetch(store, w, wd, errbuf, SDB_AST_FETCH(ast)->obj_type,
                                SDB_AST_FETCH(ast)->hostname, SDB_AST_FETCH(ast)->name,
-                               q->filter);
+                               SDB_AST_FETCH(ast)->full, q->filter);
 
        case SDB_AST_TYPE_LIST:
                return exec_list(store, w, wd, errbuf, SDB_AST_LIST(ast)->obj_type,
index 9a157fb..28e1e31 100644 (file)
@@ -46,8 +46,6 @@
 /*
  * metric fetcher:
  * Implements the callbacks necessary to read a metric object.
- * TODO: FETCH should allow to ignore child elements (attributes); then, we'd
- * only need store_host/store_metric.
  */
 
 typedef struct {
@@ -77,15 +75,8 @@ metric_fetcher_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
        return 0;
 } /* metric_fetcher_metric */
 
-static int
-metric_fetcher_attr(sdb_store_attribute_t __attribute__((unused)) *attr,
-               sdb_object_t __attribute__((unused)) *user_data)
-{
-       return 0;
-} /* metric_fetcher_attr */
-
 static sdb_store_writer_t metric_fetcher = {
-       metric_fetcher_host, NULL, metric_fetcher_metric, metric_fetcher_attr,
+       metric_fetcher_host, NULL, metric_fetcher_metric, NULL,
 };
 
 /*
@@ -404,7 +395,7 @@ sdb_conn_fetch(sdb_conn_t *conn)
        ast = sdb_ast_fetch_create((int)type,
                        hostname[0] ? strdup(hostname) : NULL,
                        name[0] ? strdup(name) : NULL,
-                       /* filter = */ NULL);
+                       /* full */ 1, /* filter = */ NULL);
        status = exec_cmd(conn, ast);
        sdb_object_deref(SDB_OBJ(ast));
        return status;
index 8533fc7..0736876 100644 (file)
@@ -258,11 +258,14 @@ typedef struct {
        int obj_type;
        char *hostname; /* optional */
        char *name;
+       /* whether to include the full object, that is,
+        * including all attributes and all children */
+       bool full;
        sdb_ast_node_t *filter; /* optional */
 } sdb_ast_fetch_t;
 #define SDB_AST_FETCH(obj) ((sdb_ast_fetch_t *)(obj))
 #define SDB_AST_FETCH_INIT \
-       { { SDB_OBJECT_INIT, SDB_AST_TYPE_FETCH, -1 }, -1, NULL, NULL, NULL }
+       { { SDB_OBJECT_INIT, SDB_AST_TYPE_FETCH, -1 }, -1, NULL, NULL, 0, NULL }
 
 /*
  * sdb_ast_list_t represents a LIST command.
@@ -384,7 +387,7 @@ sdb_ast_value_create(int type, char *name);
  * takes ownership of the strings and the filter node.
  */
 sdb_ast_node_t *
-sdb_ast_fetch_create(int obj_type, char *hostname, char *name,
+sdb_ast_fetch_create(int obj_type, char *hostname, char *name, bool full,
                sdb_ast_node_t *filter);
 
 /*
index 81a4c34..43fcd41 100644 (file)
@@ -289,7 +289,7 @@ sdb_ast_value_create(int type, char *name)
 } /* sdb_ast_value_create */
 
 sdb_ast_node_t *
-sdb_ast_fetch_create(int obj_type, char *hostname, char *name,
+sdb_ast_fetch_create(int obj_type, char *hostname, char *name, bool full,
                sdb_ast_node_t *filter)
 {
        sdb_ast_fetch_t *fetch;
@@ -302,6 +302,7 @@ sdb_ast_fetch_create(int obj_type, char *hostname, char *name,
        fetch->obj_type = obj_type;
        fetch->hostname = hostname;
        fetch->name = name;
+       fetch->full = full;
        fetch->filter = filter;
        return SDB_AST_NODE(fetch);
 } /* sdb_ast_fetch_create */
index 1b6d208..b94300a 100644 (file)
@@ -281,13 +281,13 @@ statement:
 fetch_statement:
        FETCH object_type STRING filter_clause
                {
-                       $$ = sdb_ast_fetch_create($2, NULL, $3, $4);
+                       $$ = sdb_ast_fetch_create($2, NULL, $3, 1, $4);
                        CK_OOM($$);
                }
        |
        FETCH object_type STRING '.' STRING filter_clause
                {
-                       $$ = sdb_ast_fetch_create($2, $3, $5, $6);
+                       $$ = sdb_ast_fetch_create($2, $3, $5, 1, $6);
                        CK_OOM($$);
                }
        ;