Code

store: Use the JSON writer when querying the in-memory store.
[sysdb.git] / src / core / store_exec.c
index ee1f8d046f47ad49768c4aa2a2b89a0bff9bbd80..6cf3a820bd3a013e770d3f172de2a1728649ff19 100644 (file)
  * private helper functions
  */
 
+typedef struct {
+       sdb_store_obj_t *current_host;
+
+       sdb_store_writer_t *w;
+       sdb_object_t *wd;
+} iter_t;
+
+static int
+maybe_emit_host(iter_t *iter, sdb_store_obj_t *obj)
+{
+       if ((obj->type == SDB_HOST) || (obj->type == SDB_ATTRIBUTE))
+               return 0;
+       if (iter->current_host == obj->parent)
+               return 0;
+       iter->current_host = obj->parent;
+       return sdb_store_emit(obj->parent, iter->w, iter->wd);
+} /* maybe_emit_host */
+
 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);
+       iter_t *iter = user_data;
+       maybe_emit_host(iter, obj);
+       return sdb_store_emit(obj, iter->w, iter->wd);
 } /* list_tojson */
 
 static int
 lookup_tojson(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
                void *user_data)
 {
-       sdb_store_json_formatter_t *f = user_data;
-       return sdb_store_json_emit_full(f, obj, filter);
+       iter_t *iter = user_data;
+       maybe_emit_host(iter, obj);
+       return sdb_store_emit_full(obj, filter, iter->w, iter->wd);
 } /* lookup_tojson */
 
 /*
@@ -74,6 +94,7 @@ exec_fetch(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
        sdb_store_obj_t *obj;
 
        sdb_store_json_formatter_t *f;
+       int status = 0;
 
        if ((! name) || ((type == SDB_HOST) && hostname)
                        || ((type != SDB_HOST) && (! hostname))) {
@@ -127,7 +148,11 @@ exec_fetch(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
        }
 
        sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
-       if (sdb_store_json_emit_full(f, obj, filter)) {
+       if (type != SDB_HOST)
+               status = sdb_store_emit(obj->parent,
+                               &sdb_store_json_writer, SDB_OBJ(f));
+       if (status || sdb_store_emit_full(obj, filter,
+                               &sdb_store_json_writer, SDB_OBJ(f))) {
                sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
                                "%s %s.%s to JSON", SDB_STORE_TYPE_TO_NAME(type),
                                hostname, name);
@@ -149,6 +174,7 @@ exec_list(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
                int type, sdb_store_matcher_t *filter)
 {
        uint32_t res_type = htonl(SDB_CONNECTION_LIST);
+       iter_t iter = { NULL, &sdb_store_json_writer, NULL };
        sdb_store_json_formatter_t *f;
 
        f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
@@ -162,8 +188,9 @@ exec_list(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
                return -1;
        }
 
+       iter.wd = SDB_OBJ(f);
        sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
-       if (sdb_store_scan(store, type, /* m = */ NULL, filter, list_tojson, f)) {
+       if (sdb_store_scan(store, type, /* m = */ NULL, filter, list_tojson, &iter)) {
                sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
                                "store to JSON");
                sdb_strbuf_sprintf(errbuf, "Out of memory");
@@ -182,6 +209,7 @@ exec_lookup(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
                int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter)
 {
        uint32_t res_type = htonl(SDB_CONNECTION_LOOKUP);
+       iter_t iter = { NULL, &sdb_store_json_writer, NULL };
        sdb_store_json_formatter_t *f;
 
        f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
@@ -195,9 +223,9 @@ exec_lookup(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
                return -1;
        }
 
+       iter.wd = SDB_OBJ(f);
        sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
-
-       if (sdb_store_scan(store, type, m, filter, lookup_tojson, f)) {
+       if (sdb_store_scan(store, type, m, filter, lookup_tojson, &iter)) {
                sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup %ss",
                                SDB_STORE_TYPE_TO_NAME(type));
                sdb_strbuf_sprintf(errbuf, "Failed to lookup %ss",