Code

store: Use the JSON writer when querying the in-memory store.
[sysdb.git] / src / core / store_exec.c
index c377129b825e8c6e42a2a690e6953f3a21749337..6cf3a820bd3a013e770d3f172de2a1728649ff19 100644 (file)
  */
 
 typedef struct {
-       sdb_store_json_formatter_t *f;
        sdb_store_obj_t *current_host;
+
+       sdb_store_writer_t *w;
+       sdb_object_t *wd;
 } iter_t;
 
 static int
@@ -55,7 +57,7 @@ maybe_emit_host(iter_t *iter, sdb_store_obj_t *obj)
        if (iter->current_host == obj->parent)
                return 0;
        iter->current_host = obj->parent;
-       return sdb_store_json_emit(iter->f, obj->parent);
+       return sdb_store_emit(obj->parent, iter->w, iter->wd);
 } /* maybe_emit_host */
 
 static int
@@ -65,7 +67,7 @@ list_tojson(sdb_store_obj_t *obj,
 {
        iter_t *iter = user_data;
        maybe_emit_host(iter, obj);
-       return sdb_store_json_emit(iter->f, obj);
+       return sdb_store_emit(obj, iter->w, iter->wd);
 } /* list_tojson */
 
 static int
@@ -74,7 +76,7 @@ lookup_tojson(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
 {
        iter_t *iter = user_data;
        maybe_emit_host(iter, obj);
-       return sdb_store_json_emit_full(iter->f, obj, filter);
+       return sdb_store_emit_full(obj, filter, iter->w, iter->wd);
 } /* lookup_tojson */
 
 /*
@@ -147,8 +149,10 @@ exec_fetch(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
 
        sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
        if (type != SDB_HOST)
-               status = sdb_store_json_emit(f, obj->parent);
-       if (status || sdb_store_json_emit_full(f, obj, filter)) {
+               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);
@@ -170,10 +174,11 @@ 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, NULL };
+       iter_t iter = { NULL, &sdb_store_json_writer, NULL };
+       sdb_store_json_formatter_t *f;
 
-       iter.f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
-       if (! iter.f) {
+       f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
+       if (! f) {
                char err[1024];
                sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
                                "JSON formatter to handle LIST command: %s",
@@ -183,17 +188,18 @@ 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, &iter)) {
                sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
                                "store to JSON");
                sdb_strbuf_sprintf(errbuf, "Out of memory");
-               sdb_object_deref(SDB_OBJ(iter.f));
+               sdb_object_deref(SDB_OBJ(f));
                return -1;
        }
 
-       sdb_store_json_finish(iter.f);
-       sdb_object_deref(SDB_OBJ(iter.f));
+       sdb_store_json_finish(f);
+       sdb_object_deref(SDB_OBJ(f));
 
        return SDB_CONNECTION_DATA;
 } /* exec_list */
@@ -203,10 +209,11 @@ 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, NULL };
+       iter_t iter = { NULL, &sdb_store_json_writer, NULL };
+       sdb_store_json_formatter_t *f;
 
-       iter.f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
-       if (! iter.f) {
+       f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
+       if (! f) {
                char err[1024];
                sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
                                "JSON formatter to handle LOOKUP command: %s",
@@ -216,19 +223,19 @@ 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, &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",
                                SDB_STORE_TYPE_TO_NAME(type));
-               sdb_object_deref(SDB_OBJ(iter.f));
+               sdb_object_deref(SDB_OBJ(f));
                return -1;
        }
 
-       sdb_store_json_finish(iter.f);
-       sdb_object_deref(SDB_OBJ(iter.f));
+       sdb_store_json_finish(f);
+       sdb_object_deref(SDB_OBJ(f));
 
        return SDB_CONNECTION_DATA;
 } /* exec_lookup */