From: Sebastian Harl Date: Wed, 30 Sep 2015 20:10:25 +0000 (+0200) Subject: store: Use the JSON writer when querying the in-memory store. X-Git-Tag: sysdb-0.8.0~17 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=3e4817902cb7285e5ab7a4f546861194f6e4ea1c;p=sysdb.git store: Use the JSON writer when querying the in-memory store. That is, instead of writing to a JSON formatter directly, access it through it's writer API. This will allow for more flexible store access. --- diff --git a/src/core/store_exec.c b/src/core/store_exec.c index c377129..6cf3a82 100644 --- a/src/core/store_exec.c +++ b/src/core/store_exec.c @@ -43,8 +43,10 @@ */ 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 */ diff --git a/src/core/store_json.c b/src/core/store_json.c index a069d85..4fd309b 100644 --- a/src/core/store_json.c +++ b/src/core/store_json.c @@ -379,10 +379,14 @@ sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags) buf, type, flags)); } /* sdb_store_json_formatter */ +/* TODO: Move sdb_store_emit* somewhere else. */ + int -sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) +sdb_store_emit(sdb_store_obj_t *obj, sdb_store_writer_t *w, sdb_object_t *wd) { - if ((! f) || (! obj)) + if ((! obj) || (! w) + || (! w->store_host) || (! w->store_service) + || (! w->store_metric) || (! w->store_attribute)) return -1; switch (obj->type) { @@ -395,7 +399,7 @@ sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) (const char * const *)obj->backends, obj->backends_num, }; - return sdb_store_json_writer.store_host(&host, SDB_OBJ(f)); + return w->store_host(&host, wd); } case SDB_SERVICE: { @@ -407,7 +411,7 @@ sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) (const char * const *)obj->backends, obj->backends_num, }; - return sdb_store_json_writer.store_service(&service, SDB_OBJ(f)); + return w->store_service(&service, wd); } case SDB_METRIC: { @@ -423,7 +427,7 @@ sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) (const char * const *)obj->backends, obj->backends_num, }; - return sdb_store_json_writer.store_metric(&metric, SDB_OBJ(f)); + return w->store_metric(&metric, wd); } case SDB_ATTRIBUTE: { @@ -441,21 +445,21 @@ sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) if (obj->parent && (obj->parent->type != SDB_HOST) && obj->parent->parent) attr.hostname = obj->parent->parent->_name; - return sdb_store_json_writer.store_attribute(&attr, SDB_OBJ(f)); + return w->store_attribute(&attr, wd); } } return -1; -} /* sdb_store_json_emit */ +} /* sdb_store_emit */ int -sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj, - sdb_store_matcher_t *filter) +sdb_store_emit_full(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, + sdb_store_writer_t *w, sdb_object_t *wd) { sdb_avltree_t *trees[] = { NULL, NULL, NULL }; size_t i; - if (sdb_store_json_emit(f, obj)) + if (sdb_store_emit(obj, w, wd)) return -1; if (obj->type == SDB_HOST) { @@ -486,7 +490,7 @@ sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj, if (filter && (! sdb_store_matcher_matches(filter, child, NULL))) continue; - if (sdb_store_json_emit_full(f, child, filter)) { + if (sdb_store_emit_full(child, filter, w, wd)) { sdb_avltree_iter_destroy(iter); return -1; } @@ -494,7 +498,7 @@ sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj, sdb_avltree_iter_destroy(iter); } return 0; -} /* sdb_store_json_emit_full */ +} /* sdb_store_emit_full */ int sdb_store_json_finish(sdb_store_json_formatter_t *f) diff --git a/src/include/core/store.h b/src/include/core/store.h index ddd0400..f42d217 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -731,13 +731,12 @@ sdb_store_json_formatter_t * sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags); /* - * sdb_store_json_emit: + * sdb_store_emit: * Serialize a single object to JSON adding it to the string buffer associated * with the formatter object. The serialized object will not include * attributes or any child objects. Instead, call the function again for each * of those objects. All attributes have to be emitted before any other - * children types. Use sdb_store_json_emit_full() to emit a full (filtered) - * object. + * children types. Use sdb_store_emit_full() to emit a full (filtered) object. * * Note that the output might not be valid JSON before calling * sdb_store_json_finish(). @@ -747,10 +746,10 @@ sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags); * - a negative value else */ int -sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj); +sdb_store_emit(sdb_store_obj_t *obj, sdb_store_writer_t *w, sdb_object_t *wd); /* - * sdb_store_json_emit_full: + * sdb_store_emit_full: * Serialize a single object including it's attributes and all children to * JSON, adding it to the string buffer associated with the formatter object. * The filter, if specified, is applied to each attribute and child object. @@ -764,8 +763,8 @@ sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj); * - a negative value else */ int -sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj, - sdb_store_matcher_t *filter); +sdb_store_emit_full(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, + sdb_store_writer_t *w, sdb_object_t *wd); /* * sdb_store_json_finish: diff --git a/t/unit/core/store_json_test.c b/t/unit/core/store_json_test.c index 4173831..b880f7b 100644 --- a/t/unit/core/store_json_test.c +++ b/t/unit/core/store_json_test.c @@ -102,16 +102,14 @@ scan_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); + return sdb_store_emit(obj, &sdb_store_json_writer, user_data); } /* scan_tojson */ static int scan_tojson_full(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); + return sdb_store_emit_full(obj, filter, &sdb_store_json_writer, user_data); } /* scan_tojson_full */ static void @@ -392,8 +390,7 @@ START_TEST(test_store_tojson) } sdb_strbuf_clear(buf); - f = sdb_store_json_formatter(buf, - store_tojson_data[_i].type, SDB_WANT_ARRAY); + f = sdb_store_json_formatter(buf, store_tojson_data[_i].type, SDB_WANT_ARRAY); ck_assert(f != NULL); status = sdb_store_scan(store, store_tojson_data[_i].type,