summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ed2c9fc)
raw | patch | inline | side by side (parent: ed2c9fc)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 30 Sep 2015 20:10:25 +0000 (22:10 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 30 Sep 2015 20:10:25 +0000 (22:10 +0200) |
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.
it's writer API. This will allow for more flexible store access.
src/core/store_exec.c | patch | blob | history | |
src/core/store_json.c | patch | blob | history | |
src/include/core/store.h | patch | blob | history | |
t/unit/core/store_json_test.c | patch | blob | history |
diff --git a/src/core/store_exec.c b/src/core/store_exec.c
index c377129b825e8c6e42a2a690e6953f3a21749337..6cf3a820bd3a013e770d3f172de2a1728649ff19 100644 (file)
--- a/src/core/store_exec.c
+++ b/src/core/store_exec.c
*/
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
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
{
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
{
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 */
/*
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);
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",
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 */
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",
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 a069d8543872ea79e20edd8f31c0355f372488dd..4fd309b3ec4ac6fa81f982fd9e71b2d3226d0af7 100644 (file)
--- a/src/core/store_json.c
+++ b/src/core/store_json.c
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) {
(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:
{
(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:
{
(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:
{
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) {
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;
}
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)
index ddd0400e7684027560b0705f291dc006b3477201..f42d217ebe5dba73541d79a0de74a94d8f2081b7 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
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().
* - 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.
* - 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:
index 4173831f824833b821e68b03183c024460e0ad2c..b880f7b99a43c5bf9c5381491b9649d43bd53c5a 100644 (file)
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
}
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,