Code

frontend: Use the plugin query interface instead of direct store access.
[sysdb.git] / src / core / store.c
index 401780e054bad02bf160bb807a971f02af0808a7..7671e9800f13a0ac0a81e2b24817d95b0f0a83ef 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
+#include <math.h>
 #include <pthread.h>
 
 /*
  * private variables
  */
 
-static sdb_avltree_t *hosts = NULL;
-static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;
+typedef struct {
+       sdb_object_t super;
+
+       /* hosts are the top-level entries and
+        * reference everything else */
+       sdb_avltree_t *hosts;
+       pthread_rwlock_t host_lock;
+} sdb_store_t;
+#define ST(obj) ((sdb_store_t *)(obj))
+
+sdb_store_t *global_store = NULL;
 
 /*
  * private types
  */
 
-static sdb_type_t sdb_host_type;
-static sdb_type_t sdb_service_type;
-static sdb_type_t sdb_metric_type;
-static sdb_type_t sdb_attribute_type;
+static sdb_type_t host_type;
+static sdb_type_t service_type;
+static sdb_type_t metric_type;
+static sdb_type_t attribute_type;
+
+static int
+store_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
+{
+       int err;
+       if (! (ST(obj)->hosts = sdb_avltree_create()))
+               return -1;
+       if ((err = pthread_rwlock_init(&ST(obj)->host_lock, /* attr = */ NULL))) {
+               char errbuf[128];
+               sdb_log(SDB_LOG_ERR, "store: Failed to initialize lock: %s",
+                               sdb_strerror(err, errbuf, sizeof(errbuf)));
+               return -1;
+       }
+       return 0;
+} /* store_init */
+
+static void
+store_destroy(sdb_object_t *obj)
+{
+       int err;
+       if ((err = pthread_rwlock_destroy(&ST(obj)->host_lock))) {
+               char errbuf[128];
+               sdb_log(SDB_LOG_ERR, "store: Failed to destroy lock: %s",
+                               sdb_strerror(err, errbuf, sizeof(errbuf)));
+               return;
+       }
+       sdb_avltree_destroy(ST(obj)->hosts);
+       ST(obj)->hosts = NULL;
+} /* store_destroy */
 
 static int
 store_obj_init(sdb_object_t *obj, va_list ap)
@@ -88,12 +127,14 @@ store_obj_destroy(sdb_object_t *obj)
        sobj->backends = NULL;
        sobj->backends_num = 0;
 
-       if (sobj->parent)
-               sdb_object_deref(SDB_OBJ(sobj->parent));
+       // We don't currently keep an extra reference for parent objects to
+       // avoid circular self-references which are not handled correctly by
+       // the ref-count base management layer.
+       //sdb_object_deref(SDB_OBJ(sobj->parent));
 } /* store_obj_destroy */
 
 static int
-sdb_host_init(sdb_object_t *obj, va_list ap)
+host_init(sdb_object_t *obj, va_list ap)
 {
        sdb_host_t *sobj = HOST(obj);
        int ret;
@@ -113,10 +154,10 @@ sdb_host_init(sdb_object_t *obj, va_list ap)
        if (! sobj->attributes)
                return -1;
        return 0;
-} /* sdb_host_init */
+} /* host_init */
 
 static void
-sdb_host_destroy(sdb_object_t *obj)
+host_destroy(sdb_object_t *obj)
 {
        sdb_host_t *sobj = HOST(obj);
        assert(obj);
@@ -129,10 +170,10 @@ sdb_host_destroy(sdb_object_t *obj)
                sdb_avltree_destroy(sobj->metrics);
        if (sobj->attributes)
                sdb_avltree_destroy(sobj->attributes);
-} /* sdb_host_destroy */
+} /* host_destroy */
 
 static int
-sdb_service_init(sdb_object_t *obj, va_list ap)
+service_init(sdb_object_t *obj, va_list ap)
 {
        sdb_service_t *sobj = SVC(obj);
        int ret;
@@ -146,10 +187,10 @@ sdb_service_init(sdb_object_t *obj, va_list ap)
        if (! sobj->attributes)
                return -1;
        return 0;
-} /* sdb_service_init */
+} /* service_init */
 
 static void
-sdb_service_destroy(sdb_object_t *obj)
+service_destroy(sdb_object_t *obj)
 {
        sdb_service_t *sobj = SVC(obj);
        assert(obj);
@@ -158,10 +199,10 @@ sdb_service_destroy(sdb_object_t *obj)
 
        if (sobj->attributes)
                sdb_avltree_destroy(sobj->attributes);
-} /* sdb_service_destroy */
+} /* service_destroy */
 
 static int
-sdb_metric_init(sdb_object_t *obj, va_list ap)
+metric_init(sdb_object_t *obj, va_list ap)
 {
        sdb_metric_t *sobj = METRIC(obj);
        int ret;
@@ -177,10 +218,10 @@ sdb_metric_init(sdb_object_t *obj, va_list ap)
 
        sobj->store.type = sobj->store.id = NULL;
        return 0;
-} /* sdb_metric_init */
+} /* metric_init */
 
 static void
-sdb_metric_destroy(sdb_object_t *obj)
+metric_destroy(sdb_object_t *obj)
 {
        sdb_metric_t *sobj = METRIC(obj);
        assert(obj);
@@ -194,10 +235,10 @@ sdb_metric_destroy(sdb_object_t *obj)
                free(sobj->store.type);
        if (sobj->store.id)
                free(sobj->store.id);
-} /* sdb_metric_destroy */
+} /* metric_destroy */
 
 static int
-sdb_attr_init(sdb_object_t *obj, va_list ap)
+attr_init(sdb_object_t *obj, va_list ap)
 {
        const sdb_data_t *value;
        int ret;
@@ -213,39 +254,45 @@ sdb_attr_init(sdb_object_t *obj, va_list ap)
                if (sdb_data_copy(&ATTR(obj)->value, value))
                        return -1;
        return 0;
-} /* sdb_attr_init */
+} /* attr_init */
 
 static void
-sdb_attr_destroy(sdb_object_t *obj)
+attr_destroy(sdb_object_t *obj)
 {
        assert(obj);
 
        store_obj_destroy(obj);
        sdb_data_free_datum(&ATTR(obj)->value);
-} /* sdb_attr_destroy */
+} /* attr_destroy */
+
+static sdb_type_t store_type = {
+       /* size = */ sizeof(sdb_store_t),
+       /* init = */ store_init,
+       /* destroy = */ store_destroy,
+};
 
-static sdb_type_t sdb_host_type = {
-       sizeof(sdb_host_t),
-       sdb_host_init,
-       sdb_host_destroy
+static sdb_type_t host_type = {
+       /* size = */ sizeof(sdb_host_t),
+       /* init = */ host_init,
+       /* destroy = */ host_destroy
 };
 
-static sdb_type_t sdb_service_type = {
-       sizeof(sdb_service_t),
-       sdb_service_init,
-       sdb_service_destroy
+static sdb_type_t service_type = {
+       /* size = */ sizeof(sdb_service_t),
+       /* init = */ service_init,
+       /* destroy = */ service_destroy
 };
 
-static sdb_type_t sdb_metric_type = {
-       sizeof(sdb_metric_t),
-       sdb_metric_init,
-       sdb_metric_destroy
+static sdb_type_t metric_type = {
+       /* size = */ sizeof(sdb_metric_t),
+       /* init = */ metric_init,
+       /* destroy = */ metric_destroy
 };
 
-static sdb_type_t sdb_attribute_type = {
-       sizeof(sdb_attribute_t),
-       sdb_attr_init,
-       sdb_attr_destroy
+static sdb_type_t attribute_type = {
+       /* size = */ sizeof(sdb_attribute_t),
+       /* init = */ attr_init,
+       /* destroy = */ attr_destroy
 };
 
 /*
@@ -253,9 +300,25 @@ static sdb_type_t sdb_attribute_type = {
  */
 
 static sdb_host_t *
-lookup_host(const char *name)
+lookup_host(sdb_store_t *st, const char *name, bool canonicalize)
 {
-       return HOST(sdb_avltree_lookup(hosts, name));
+       sdb_host_t *host;
+       char *cname;
+
+       assert(name);
+       if (! canonicalize)
+               return HOST(sdb_avltree_lookup(st->hosts, name));
+
+       cname = strdup(name);
+       cname = sdb_plugin_cname(cname);
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "store: strdup failed");
+               return NULL;
+       }
+
+       host = HOST(sdb_avltree_lookup(st->hosts, cname));
+       free(cname);
+       return host;
 } /* lookup_host */
 
 static int
@@ -288,8 +351,9 @@ record_backend(sdb_store_obj_t *obj)
 } /* record_backend */
 
 static int
-store_obj(sdb_avltree_t *parent_tree, int type, const char *name,
-               sdb_time_t last_update, sdb_store_obj_t **updated_obj)
+store_obj(sdb_store_obj_t *parent, sdb_avltree_t *parent_tree,
+               int type, const char *name, sdb_time_t last_update,
+               sdb_store_obj_t **updated_obj)
 {
        sdb_store_obj_t *old, *new;
        int status = 0;
@@ -333,16 +397,16 @@ store_obj(sdb_avltree_t *parent_tree, int type, const char *name,
        else {
                if (type == SDB_ATTRIBUTE) {
                        /* the value will be updated by the caller */
-                       new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
+                       new = STORE_OBJ(sdb_object_create(name, attribute_type,
                                                type, last_update, NULL));
                }
                else {
                        sdb_type_t t;
                        t = type == SDB_HOST
-                               ? sdb_host_type
+                               ? host_type
                                : type == SDB_SERVICE
-                                       ? sdb_service_type
-                                       : sdb_metric_type;
+                                       ? service_type
+                                       : metric_type;
                        new = STORE_OBJ(sdb_object_create(name, t, type, last_update));
                }
 
@@ -365,6 +429,14 @@ store_obj(sdb_avltree_t *parent_tree, int type, const char *name,
                return status;
        assert(new);
 
+       if (new->parent != parent) {
+               // Avoid circular self-references which are not handled
+               // correctly by the ref-count based management layer.
+               //sdb_object_deref(SDB_OBJ(new->parent));
+               //sdb_object_ref(SDB_OBJ(parent));
+               new->parent = parent;
+       }
+
        if (updated_obj)
                *updated_obj = new;
 
@@ -374,13 +446,14 @@ store_obj(sdb_avltree_t *parent_tree, int type, const char *name,
 } /* store_obj */
 
 static int
-store_attr(sdb_avltree_t *attributes, const char *key, const sdb_data_t *value,
-               sdb_time_t last_update)
+store_attr(sdb_store_obj_t *parent, sdb_avltree_t *attributes,
+               const char *key, const sdb_data_t *value, sdb_time_t last_update)
 {
        sdb_store_obj_t *attr = NULL;
        int status;
 
-       status = store_obj(attributes, SDB_ATTRIBUTE, key, last_update, &attr);
+       status = store_obj(parent, attributes, SDB_ATTRIBUTE,
+                       key, last_update, &attr);
        if (status)
                return status;
 
@@ -394,32 +467,48 @@ store_attr(sdb_avltree_t *attributes, const char *key, const sdb_data_t *value,
        return status;
 } /* store_attr */
 
-/* The host_lock has to be acquired before calling this function. */
-static sdb_avltree_t *
-get_host_children(const char *hostname, int type)
+static int
+store_metric_store(sdb_metric_t *metric, sdb_metric_store_t *store)
 {
-       char *cname = NULL;
-       sdb_host_t *host;
+       char *type = metric->store.type;
+       char *id = metric->store.id;
 
-       assert(hostname);
-       assert((type == SDB_SERVICE) || (type == SDB_METRIC)
-                       || (type == SDB_ATTRIBUTE));
+       if ((! metric->store.type) || strcasecmp(metric->store.type, store->type)) {
+               if (! (type = strdup(store->type)))
+                       return -1;
+       }
+       if ((! metric->store.id) || strcasecmp(metric->store.id, store->id)) {
+               if (! (id = strdup(store->id))) {
+                       if (type != metric->store.type)
+                               free(type);
+                       return -1;
+               }
+       }
 
-       if (! hosts)
-               return NULL;
+       if (type != metric->store.type) {
+               if (metric->store.type)
+                       free(metric->store.type);
+               metric->store.type = type;
+       }
+       if (id != metric->store.id) {
+               if (metric->store.id)
+                       free(metric->store.id);
+               metric->store.id = id;
+       }
+       return 0;
+} /* store_metric_store */
 
-       cname = sdb_plugin_cname(strdup(hostname));
-       if (! cname) {
-               sdb_log(SDB_LOG_ERR, "store: strdup failed");
+/* The store's host_lock has to be acquired before calling this function. */
+static sdb_avltree_t *
+get_host_children(sdb_host_t *host, int type)
+{
+       if ((type != SDB_SERVICE) && (type != SDB_METRIC)
+                       && (type != SDB_ATTRIBUTE))
                return NULL;
-       }
 
-       host = lookup_host(cname);
-       free(cname);
        if (! host)
                return NULL;
 
-       sdb_object_deref(SDB_OBJ(host));
        if (type == SDB_ATTRIBUTE)
                return host->attributes;
        else if (type == SDB_METRIC)
@@ -428,109 +517,6 @@ get_host_children(const char *hostname, int type)
                return host->services;
 } /* get_host_children */
 
-/*
- * store_common_tojson serializes common object attributes to JSON.
- *
- * The function never returns an error. Rather, an error message will be part
- * of the serialized data.
- */
-static void
-store_common_tojson(sdb_store_obj_t *obj, sdb_strbuf_t *buf)
-{
-       char time_str[64];
-       char interval_str[64];
-       size_t i;
-
-       /* TODO: make time and interval formats configurable */
-       if (! sdb_strftime(time_str, sizeof(time_str),
-                               "%F %T %z", obj->last_update))
-               snprintf(time_str, sizeof(time_str), "<error>");
-       time_str[sizeof(time_str) - 1] = '\0';
-
-       if (! sdb_strfinterval(interval_str, sizeof(interval_str),
-                               obj->interval))
-               snprintf(interval_str, sizeof(interval_str), "<error>");
-       interval_str[sizeof(interval_str) - 1] = '\0';
-
-       sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
-                       "\"update_interval\": \"%s\", \"backends\": [",
-                       time_str, interval_str);
-
-       for (i = 0; i < obj->backends_num; ++i) {
-               sdb_strbuf_append(buf, "\"%s\"", obj->backends[i]);
-               if (i < obj->backends_num - 1)
-                       sdb_strbuf_append(buf, ",");
-       }
-       sdb_strbuf_append(buf, "]");
-} /* store_common_tojson */
-
-/*
- * store_obj_tojson serializes attribute / metric / service objects to JSON.
- *
- * The function never returns an error. Rather, an error message will be part
- * of the serialized data.
- */
-static void
-store_obj_tojson(sdb_avltree_t *tree, int type, sdb_strbuf_t *buf,
-               sdb_store_matcher_t *filter, int flags)
-{
-       sdb_avltree_iter_t *iter;
-
-       assert((type == SDB_ATTRIBUTE)
-                       || (type == SDB_METRIC)
-                       || (type == SDB_SERVICE));
-
-       sdb_strbuf_append(buf, "[");
-       iter = sdb_avltree_get_iter(tree);
-       if (! iter) {
-               char errbuf[1024];
-               sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
-                               SDB_STORE_TYPE_TO_NAME(type),
-                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
-               sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
-                               SDB_STORE_TYPE_TO_NAME(type), errbuf);
-       }
-
-       /* has_next returns false if the iterator is NULL */
-       while (sdb_avltree_iter_has_next(iter)) {
-               sdb_store_obj_t *sobj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
-               assert(sobj);
-               assert(sobj->type == type);
-
-               if (filter && (! sdb_store_matcher_matches(filter, sobj, NULL)))
-                       continue;
-
-               sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
-               if (sobj->type == SDB_ATTRIBUTE) {
-                       char tmp[sdb_data_strlen(&ATTR(sobj)->value) + 1];
-                       sdb_data_format(&ATTR(sobj)->value, tmp, sizeof(tmp),
-                                       SDB_DOUBLE_QUOTED);
-                       sdb_strbuf_append(buf, "\"value\": %s, ", tmp);
-               }
-               store_common_tojson(sobj, buf);
-
-               if ((sobj->type == SDB_SERVICE)
-                               && (! (flags & SDB_SKIP_ATTRIBUTES))) {
-                       sdb_strbuf_append(buf, ", \"attributes\": ");
-                       store_obj_tojson(SVC(sobj)->attributes, SDB_ATTRIBUTE,
-                                       buf, filter, flags);
-               }
-               else if ((sobj->type == SDB_METRIC)
-                               && (! (flags & SDB_SKIP_ATTRIBUTES))) {
-                       sdb_strbuf_append(buf, ", \"attributes\": ");
-                       store_obj_tojson(METRIC(sobj)->attributes, SDB_ATTRIBUTE,
-                                       buf, filter, flags);
-               }
-               sdb_strbuf_append(buf, "}");
-
-               if (sdb_avltree_iter_has_next(iter))
-                       sdb_strbuf_append(buf, ",");
-       }
-
-       sdb_avltree_iter_destroy(iter);
-       sdb_strbuf_append(buf, "]");
-} /* store_obj_tojson */
-
 /*
  * ts_tojson serializes a time-series to JSON.
  *
@@ -546,16 +532,14 @@ ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
        size_t i;
 
        /* TODO: make time format configurable */
-       if (! sdb_strftime(start_str, sizeof(start_str),
-                               "%F %T %z", ts->start))
+       if (! sdb_strftime(start_str, sizeof(start_str), ts->start))
                snprintf(start_str, sizeof(start_str), "<error>");
        start_str[sizeof(start_str) - 1] = '\0';
-       if (! sdb_strftime(end_str, sizeof(end_str),
-                               "%F %T %z", ts->end))
+       if (! sdb_strftime(end_str, sizeof(end_str), ts->end))
                snprintf(end_str, sizeof(end_str), "<error>");
        end_str[sizeof(end_str) - 1] = '\0';
 
-       sdb_strbuf_append(buf, "{\"start\": \"%s\", \"end\": \"%s\", {",
+       sdb_strbuf_append(buf, "{\"start\": \"%s\", \"end\": \"%s\", \"data\": {",
                        start_str, end_str);
 
        for (i = 0; i < ts->data_names_len; ++i) {
@@ -565,13 +549,17 @@ ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
                for (j = 0; j < ts->data_len; ++j) {
                        char time_str[64];
 
-                       if (! sdb_strftime(time_str, sizeof(time_str),
-                                               "%F %T %z", ts->data[i][j].timestamp))
+                       if (! sdb_strftime(time_str, sizeof(time_str), ts->data[i][j].timestamp))
                                snprintf(time_str, sizeof(time_str), "<error>");
                        time_str[sizeof(time_str) - 1] = '\0';
 
-                       sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
-                                       "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
+                       /* Some GNU libc versions may print '-nan' which we dont' want */
+                       if (isnan(ts->data[i][j].value))
+                               sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
+                                               "\"value\": \"nan\"}", time_str);
+                       else
+                               sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
+                                               "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
 
                        if (j < ts->data_len - 1)
                                sdb_strbuf_append(buf, ",");
@@ -586,84 +574,26 @@ ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
 } /* ts_tojson */
 
 /*
- * public API
+ * store writer API
  */
 
-void
-sdb_store_clear(void)
-{
-       sdb_avltree_destroy(hosts);
-       hosts = NULL;
-} /* sdb_store_clear */
-
-int
-sdb_store_host(const char *name, sdb_time_t last_update)
-{
-       char *cname = NULL;
-       int status = 0;
-
-       if (! name)
-               return -1;
-
-       cname = sdb_plugin_cname(strdup(name));
-       if (! cname) {
-               sdb_log(SDB_LOG_ERR, "store: strdup failed");
-               return -1;
-       }
-
-       pthread_rwlock_wrlock(&host_lock);
-       if (! hosts)
-               if (! (hosts = sdb_avltree_create()))
-                       status = -1;
-
-       if (! status)
-               status = store_obj(hosts, SDB_HOST, cname, last_update, NULL);
-       pthread_rwlock_unlock(&host_lock);
-
-       free(cname);
-       return status;
-} /* sdb_store_host */
-
-_Bool
-sdb_store_has_host(const char *name)
+static int
+store_attribute(const char *hostname,
+               const char *key, const sdb_data_t *value,
+               sdb_time_t last_update, sdb_object_t *user_data)
 {
-       sdb_host_t *host;
-
-       if (! name)
-               return NULL;
-
-       host = lookup_host(name);
-       return host != NULL;
-} /* sdb_store_has_host */
+       sdb_store_t *st = ST(user_data);
 
-sdb_store_obj_t *
-sdb_store_get_host(const char *name)
-{
        sdb_host_t *host;
-
-       if (! name)
-               return NULL;
-
-       host = lookup_host(name);
-       if (! host)
-               return NULL;
-
-       return STORE_OBJ(host);
-} /* sdb_store_get_host */
-
-int
-sdb_store_attribute(const char *hostname,
-               const char *key, const sdb_data_t *value,
-               sdb_time_t last_update)
-{
        sdb_avltree_t *attrs;
        int status = 0;
 
        if ((! hostname) || (! key))
                return -1;
 
-       pthread_rwlock_wrlock(&host_lock);
-       attrs = get_host_children(hostname, SDB_ATTRIBUTE);
+       pthread_rwlock_wrlock(&st->host_lock);
+       host = lookup_host(st, hostname, /* canonicalize = */ 1);
+       attrs = get_host_children(host, SDB_ATTRIBUTE);
        if (! attrs) {
                sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
                                "host '%s' not found", key, hostname);
@@ -671,55 +601,64 @@ sdb_store_attribute(const char *hostname,
        }
 
        if (! status)
-               status = store_attr(attrs, key, value, last_update);
+               status = store_attr(STORE_OBJ(host), attrs, key, value, last_update);
+
+       sdb_object_deref(SDB_OBJ(host));
+       pthread_rwlock_unlock(&st->host_lock);
 
-       pthread_rwlock_unlock(&host_lock);
        return status;
-} /* sdb_store_attribute */
+} /* store_attribute */
 
-int
-sdb_store_service(const char *hostname, const char *name,
-               sdb_time_t last_update)
+static int
+store_host(const char *name, sdb_time_t last_update, sdb_object_t *user_data)
 {
-       sdb_avltree_t *services;
+       sdb_store_t *st = ST(user_data);
 
+       char *cname = NULL;
        int status = 0;
 
-       if ((! hostname) || (! name))
+       if (! name)
                return -1;
 
-       pthread_rwlock_wrlock(&host_lock);
-       services = get_host_children(hostname, SDB_SERVICE);
-       if (! services) {
-               sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
-                               "host '%s' not found", name, hostname);
-               status = -1;
+       cname = sdb_plugin_cname(strdup(name));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "store: strdup failed");
+               return -1;
        }
 
-       if (! status)
-               status = store_obj(services, SDB_SERVICE, name, last_update, NULL);
-       pthread_rwlock_unlock(&host_lock);
+       pthread_rwlock_wrlock(&st->host_lock);
+       status = store_obj(NULL, st->hosts,
+                       SDB_HOST, cname, last_update, NULL);
+       pthread_rwlock_unlock(&st->host_lock);
+
+       free(cname);
        return status;
-} /* sdb_store_service */
+} /* store_host */
 
-int
-sdb_store_service_attr(const char *hostname, const char *service,
-               const char *key, const sdb_data_t *value, sdb_time_t last_update)
+static int
+store_service_attr(const char *hostname, const char *service,
+               const char *key, const sdb_data_t *value, sdb_time_t last_update,
+               sdb_object_t *user_data)
 {
-       sdb_avltree_t *services;
+       sdb_store_t *st = ST(user_data);
+
+       sdb_host_t *host;
        sdb_service_t *svc;
+       sdb_avltree_t *services;
        int status = 0;
 
        if ((! hostname) || (! service) || (! key))
                return -1;
 
-       pthread_rwlock_wrlock(&host_lock);
-       services = get_host_children(hostname, SDB_SERVICE);
+       pthread_rwlock_wrlock(&st->host_lock);
+       host = lookup_host(st, hostname, /* canonicalize = */ 1);
+       services = get_host_children(host, SDB_SERVICE);
+       sdb_object_deref(SDB_OBJ(host));
        if (! services) {
                sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
                                "for service '%s' - host '%ss' not found",
                                key, service, hostname);
-               pthread_rwlock_unlock(&host_lock);
+               pthread_rwlock_unlock(&st->host_lock);
                return -1;
        }
 
@@ -731,89 +670,81 @@ sdb_store_service_attr(const char *hostname, const char *service,
        }
 
        if (! status)
-               status = store_attr(svc->attributes, key, value, last_update);
+               status = store_attr(STORE_OBJ(svc), svc->attributes,
+                               key, value, last_update);
 
        sdb_object_deref(SDB_OBJ(svc));
-       pthread_rwlock_unlock(&host_lock);
+       pthread_rwlock_unlock(&st->host_lock);
+
        return status;
-} /* sdb_store_service_attr */
+} /* store_service_attr */
 
-int
-sdb_store_metric(const char *hostname, const char *name,
-               sdb_metric_store_t *store, sdb_time_t last_update)
+static int
+store_service(const char *hostname, const char *name,
+               sdb_time_t last_update, sdb_object_t *user_data)
 {
-       sdb_store_obj_t *obj = NULL;
-       sdb_metric_t *metric;
+       sdb_store_t *st = ST(user_data);
 
-       sdb_avltree_t *metrics;
+       sdb_host_t *host;
+       sdb_avltree_t *services;
+       sdb_data_t d;
 
        int status = 0;
 
        if ((! hostname) || (! name))
                return -1;
-       if (store && ((! store->type) || (! store->id)))
-               return -1;
 
-       pthread_rwlock_wrlock(&host_lock);
-       metrics = get_host_children(hostname, SDB_METRIC);
-       if (! metrics) {
-               sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
+       pthread_rwlock_wrlock(&st->host_lock);
+       host = lookup_host(st, hostname, /* canonicalize = */ 1);
+       services = get_host_children(host, SDB_SERVICE);
+       if (! services) {
+               sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
                                "host '%s' not found", name, hostname);
                status = -1;
        }
 
        if (! status)
-               status = store_obj(metrics, SDB_METRIC, name, last_update, &obj);
-
-       if (status || (! store)) {
-               pthread_rwlock_unlock(&host_lock);
-               return status;
-       }
+               status = store_obj(STORE_OBJ(host), services, SDB_SERVICE,
+                               name, last_update, NULL);
 
-       assert(obj);
-       metric = METRIC(obj);
+       sdb_object_deref(SDB_OBJ(host));
+       pthread_rwlock_unlock(&st->host_lock);
 
-       if ((! metric->store.type) || strcasecmp(metric->store.type, store->type)) {
-               if (metric->store.type)
-                       free(metric->store.type);
-               metric->store.type = strdup(store->type);
-       }
-       if ((! metric->store.id) || strcasecmp(metric->store.id, store->id)) {
-               if (metric->store.id)
-                       free(metric->store.id);
-               metric->store.id = strdup(store->id);
-       }
+       if (status)
+               return status;
 
-       if ((! metric->store.type) || (! metric->store.id)) {
-               if (metric->store.type)
-                       free(metric->store.type);
-               if (metric->store.id)
-                       free(metric->store.id);
-               metric->store.type = metric->store.id = NULL;
+       /* record the hostname as an attribute */
+       d.type = SDB_TYPE_STRING;
+       d.data.string = SDB_OBJ(host)->name;
+       if (store_service_attr(hostname, name, "hostname", &d, last_update, user_data))
                status = -1;
-       }
-       pthread_rwlock_unlock(&host_lock);
        return status;
-} /* sdb_store_metric */
+} /* store_service */
 
-int
-sdb_store_metric_attr(const char *hostname, const char *metric,
-               const char *key, const sdb_data_t *value, sdb_time_t last_update)
+static int
+store_metric_attr(const char *hostname, const char *metric,
+               const char *key, const sdb_data_t *value, sdb_time_t last_update,
+               sdb_object_t *user_data)
 {
+       sdb_store_t *st = ST(user_data);
+
        sdb_avltree_t *metrics;
+       sdb_host_t *host;
        sdb_metric_t *m;
        int status = 0;
 
        if ((! hostname) || (! metric) || (! key))
                return -1;
 
-       pthread_rwlock_wrlock(&host_lock);
-       metrics = get_host_children(hostname, SDB_METRIC);
+       pthread_rwlock_wrlock(&st->host_lock);
+       host = lookup_host(st, hostname, /* canonicalize = */ 1);
+       metrics = get_host_children(host, SDB_METRIC);
+       sdb_object_deref(SDB_OBJ(host));
        if (! metrics) {
                sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
                                "for metric '%s' - host '%s' not found",
                                key, metric, hostname);
-               pthread_rwlock_unlock(&host_lock);
+               pthread_rwlock_unlock(&st->host_lock);
                return -1;
        }
 
@@ -825,205 +756,392 @@ sdb_store_metric_attr(const char *hostname, const char *metric,
        }
 
        if (! status)
-               status = store_attr(m->attributes, key, value, last_update);
+               status = store_attr(STORE_OBJ(m), m->attributes,
+                               key, value, last_update);
 
        sdb_object_deref(SDB_OBJ(m));
-       pthread_rwlock_unlock(&host_lock);
+       pthread_rwlock_unlock(&st->host_lock);
+
        return status;
-} /* sdb_store_metric_attr */
+} /* store_metric_attr */
 
-int
-sdb_store_fetch_timeseries(const char *hostname, const char *metric,
-               sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf)
+static int
+store_metric(const char *hostname, const char *name,
+               sdb_metric_store_t *store, sdb_time_t last_update,
+               sdb_object_t *user_data)
 {
+       sdb_store_t *st = ST(user_data);
+
+       sdb_store_obj_t *obj = NULL;
+       sdb_host_t *host;
+       sdb_metric_t *metric;
+       sdb_data_t d;
+
        sdb_avltree_t *metrics;
-       sdb_metric_t *m;
 
-       sdb_timeseries_t *ts;
+       int status = 0;
 
-       if ((! hostname) || (! metric) || (! opts) || (! buf))
+       if ((! hostname) || (! name))
                return -1;
 
-       pthread_rwlock_rdlock(&host_lock);
-       metrics = get_host_children(hostname, SDB_METRIC);
+       if (store) {
+               if ((store->type != NULL) != (store->id != NULL))
+                       return -1;
+               else if (! store->type)
+                       store = NULL;
+       }
+
+       pthread_rwlock_wrlock(&st->host_lock);
+       host = lookup_host(st, hostname, /* canonicalize = */ 1);
+       metrics = get_host_children(host, SDB_METRIC);
        if (! metrics) {
-               sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
-                               "- host '%s' not found", hostname, metric, hostname);
-               pthread_rwlock_unlock(&host_lock);
-               return -1;
+               sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
+                               "host '%s' not found", name, hostname);
+               status = -1;
        }
 
-       m = METRIC(sdb_avltree_lookup(metrics, metric));
-       if (! m) {
-               sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
-                               "- metric '%s' not found", hostname, metric, metric);
-               pthread_rwlock_unlock(&host_lock);
-               return -1;
+       if (! status)
+               status = store_obj(STORE_OBJ(host), metrics, SDB_METRIC,
+                               name, last_update, &obj);
+       sdb_object_deref(SDB_OBJ(host));
+
+       if (status) {
+               pthread_rwlock_unlock(&st->host_lock);
+               return status;
        }
 
-       if ((! m->store.type) || (! m->store.id)) {
-               sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
-                               "- no data-store configured for the stored metric",
-                               hostname, metric);
-               pthread_rwlock_unlock(&host_lock);
+       assert(obj);
+       metric = METRIC(obj);
+
+       if (store)
+               if (store_metric_store(metric, store))
+                       status = -1;
+       pthread_rwlock_unlock(&st->host_lock);
+
+       /* record the hostname as an attribute */
+       d.type = SDB_TYPE_STRING;
+       d.data.string = SDB_OBJ(host)->name;
+       if (store_metric_attr(hostname, name, "hostname", &d, last_update, user_data))
+               status = -1;
+       return status;
+} /* store_metric */
+
+static sdb_store_writer_t store_writer = {
+       store_host, store_service, store_metric,
+       store_attribute, store_service_attr, store_metric_attr,
+};
+
+/*
+ * TODO: let prepare and execute accept a store object as their user_data
+ * object
+ */
+
+static sdb_object_t *
+prepare_query(sdb_ast_node_t *ast,
+               sdb_strbuf_t __attribute__((unused)) *errbuf,
+               sdb_object_t __attribute__((unused)) *user_data)
+{
+       return SDB_OBJ(sdb_store_query_prepare(ast));
+} /* prepare_query */
+
+static int
+execute_query(sdb_object_t *q,
+               sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
+               sdb_object_t __attribute__((unused)) *user_data)
+{
+       return sdb_store_query_execute(QUERY(q), buf, errbuf);
+} /* execute_query */
+
+static sdb_store_reader_t store_reader = {
+       prepare_query, execute_query,
+};
+
+/*
+ * public API
+ */
+
+int
+sdb_store_init(void)
+{
+       if (global_store)
+               return 0;
+
+       global_store = ST(sdb_object_create("store", store_type));
+       if (! global_store) {
+               sdb_log(SDB_LOG_ERR, "store: Failed to allocate store");
                return -1;
        }
+       if (sdb_plugin_register_writer("memstore",
+                               &store_writer, SDB_OBJ(global_store)))
+               return -1;
+       return sdb_plugin_register_reader("memstore",
+                       &store_reader, SDB_OBJ(global_store));
+} /* sdb_store_init */
 
-       {
-               char type[strlen(m->store.type) + 1];
-               char id[strlen(m->store.id) + 1];
+void
+sdb_store_clear(void)
+{
+       if (! global_store)
+               return;
+       sdb_avltree_clear(global_store->hosts);
+} /* sdb_store_clear */
 
-               strncpy(type, m->store.type, sizeof(type));
-               strncpy(id, m->store.id, sizeof(id));
-               pthread_rwlock_unlock(&host_lock);
+sdb_store_obj_t *
+sdb_store_get_host(const char *name)
+{
+       sdb_host_t *host;
 
-               ts = sdb_plugin_fetch_timeseries(type, id, opts);
-               if (! ts) {
-                       sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
-                                       "- %s fetcher callback returned no data for '%s'",
-                                       hostname, metric, type, id);
-                       return -1;
-               }
-       }
+       if ((! global_store) || (! name))
+               return NULL;
 
-       ts_tojson(ts, buf);
-       sdb_timeseries_destroy(ts);
-       return 0;
-} /* sdb_store_fetch_timeseries */
+       host = lookup_host(global_store, name, /* canonicalize = */ 0);
+       if (! host)
+               return NULL;
+
+       return STORE_OBJ(host);
+} /* sdb_store_get_host */
+
+sdb_store_obj_t *
+sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name)
+{
+       sdb_avltree_t *children;
+
+       if ((! host) || (host->type != SDB_HOST) || (! name))
+               return NULL;
+
+       children = get_host_children(HOST(host), type);
+       if (! children)
+               return NULL;
+       return STORE_OBJ(sdb_avltree_lookup(children, name));
+} /* sdb_store_get_child */
 
 int
 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
 {
-       if ((! obj) || (! res))
+       sdb_data_t tmp;
+
+       if (! obj)
                return -1;
 
        switch (field) {
+               case SDB_FIELD_NAME:
+                       tmp.type = SDB_TYPE_STRING;
+                       tmp.data.string = strdup(SDB_OBJ(obj)->name);
+                       if (! tmp.data.string)
+                               return -1;
+                       break;
                case SDB_FIELD_LAST_UPDATE:
-                       res->type = SDB_TYPE_DATETIME;
-                       res->data.datetime = obj->last_update;
+                       tmp.type = SDB_TYPE_DATETIME;
+                       tmp.data.datetime = obj->last_update;
                        break;
                case SDB_FIELD_AGE:
-                       res->type = SDB_TYPE_DATETIME;
-                       res->data.datetime = sdb_gettime() - obj->last_update;
+                       tmp.type = SDB_TYPE_DATETIME;
+                       tmp.data.datetime = sdb_gettime() - obj->last_update;
                        break;
                case SDB_FIELD_INTERVAL:
-                       res->type = SDB_TYPE_DATETIME;
-                       res->data.datetime = obj->interval;
+                       tmp.type = SDB_TYPE_DATETIME;
+                       tmp.data.datetime = obj->interval;
                        break;
                case SDB_FIELD_BACKEND:
-                       /* TODO: add support for storing array values in a data object
-                        * for now, fall thru to the error case */
+                       if (! res)
+                               return 0;
+                       tmp.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
+                       tmp.data.array.length = obj->backends_num;
+                       tmp.data.array.values = obj->backends;
+                       return sdb_data_copy(res, &tmp);
+               case SDB_FIELD_VALUE:
+                       if (obj->type != SDB_ATTRIBUTE)
+                               return -1;
+                       if (! res)
+                               return 0;
+                       return sdb_data_copy(res, &ATTR(obj)->value);
+               case SDB_FIELD_TIMESERIES:
+                       if (obj->type != SDB_METRIC)
+                               return -1;
+                       tmp.type = SDB_TYPE_BOOLEAN;
+                       tmp.data.boolean = METRIC(obj)->store.type != NULL;
                default:
                        return -1;
        }
+       if (res)
+               *res = tmp;
+       else
+               sdb_data_free_datum(&tmp);
        return 0;
 } /* sdb_store_get_field */
 
 int
-sdb_store_host_tojson(sdb_store_obj_t *h, sdb_strbuf_t *buf,
-               sdb_store_matcher_t *filter, int flags)
+sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
+               sdb_store_matcher_t *filter)
 {
-       sdb_host_t *host = HOST(h);
+       sdb_avltree_t *tree = NULL;
+       sdb_store_obj_t *attr;
 
-       if ((! h) || (h->type != SDB_HOST) || (! buf))
+       if ((! obj) || (! name))
                return -1;
 
-       sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(host)->name);
-       store_common_tojson(h, buf);
-
-       if (! (flags & SDB_SKIP_ATTRIBUTES)) {
-               sdb_strbuf_append(buf, ", \"attributes\": ");
-               store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf, filter, flags);
-       }
+       if (obj->type == SDB_HOST)
+               tree = HOST(obj)->attributes;
+       else if (obj->type == SDB_SERVICE)
+               tree = SVC(obj)->attributes;
+       else if (obj->type == SDB_METRIC)
+               tree = METRIC(obj)->attributes;
 
-       if (! (flags & SDB_SKIP_METRICS)) {
-               sdb_strbuf_append(buf, ", \"metrics\": ");
-               store_obj_tojson(host->metrics, SDB_METRIC, buf, filter, flags);
-       }
+       if (! tree)
+               return -1;
 
-       if (! (flags & SDB_SKIP_SERVICES)) {
-               sdb_strbuf_append(buf, ", \"services\": ");
-               store_obj_tojson(host->services, SDB_SERVICE, buf, filter, flags);
+       attr = STORE_OBJ(sdb_avltree_lookup(tree, name));
+       if (! attr)
+               return -1;
+       if (filter && (! sdb_store_matcher_matches(filter, attr, NULL))) {
+               sdb_object_deref(SDB_OBJ(attr));
+               return -1;
        }
 
-       sdb_strbuf_append(buf, "}");
+       assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE);
+       if (res)
+               sdb_data_copy(res, &ATTR(attr)->value);
+       sdb_object_deref(SDB_OBJ(attr));
        return 0;
-} /* sdb_store_host_tojson */
+} /* sdb_store_get_attr */
+
+/* TODO: sdb_store_fetch_timeseries should move into the plugin module */
 
 int
-sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags)
+sdb_store_fetch_timeseries(const char *hostname, const char *metric,
+               sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf)
 {
-       sdb_avltree_iter_t *host_iter;
-       size_t len;
+       sdb_avltree_t *metrics;
+       sdb_host_t *host;
+       sdb_metric_t *m;
 
-       if (! buf)
-               return -1;
+       sdb_timeseries_t *ts;
 
-       pthread_rwlock_rdlock(&host_lock);
+       int status = 0;
 
-       host_iter = sdb_avltree_get_iter(hosts);
-       if (! host_iter) {
-               pthread_rwlock_unlock(&host_lock);
+       if ((! global_store) || (! hostname) || (! metric) || (! opts) || (! buf))
                return -1;
-       }
 
-       sdb_strbuf_append(buf, "[");
+       pthread_rwlock_rdlock(&global_store->host_lock);
+       host = lookup_host(global_store, hostname, /* canonicalize = */ 1);
+       metrics = get_host_children(host, SDB_METRIC);
+       sdb_object_deref(SDB_OBJ(host));
+       if (! metrics) {
+               sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
+                               "- host '%s' not found", hostname, metric, hostname);
+               pthread_rwlock_unlock(&global_store->host_lock);
+               return -1;
+       }
 
-       len = sdb_strbuf_len(buf);
-       while (sdb_avltree_iter_has_next(host_iter)) {
-               sdb_store_obj_t *host;
+       m = METRIC(sdb_avltree_lookup(metrics, metric));
+       if (! m) {
+               sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
+                               "- metric '%s' not found", hostname, metric, metric);
+               pthread_rwlock_unlock(&global_store->host_lock);
+               return -1;
+       }
 
-               host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
-               assert(host);
+       if ((! m->store.type) || (! m->store.id)) {
+               sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
+                               "- no data-store configured for the stored metric",
+                               hostname, metric);
+               sdb_object_deref(SDB_OBJ(m));
+               pthread_rwlock_unlock(&global_store->host_lock);
+               return -1;
+       }
 
-               if (filter && (! sdb_store_matcher_matches(filter, host, NULL)))
-                       continue;
+       {
+               char type[strlen(m->store.type) + 1];
+               char id[strlen(m->store.id) + 1];
 
-               if (sdb_strbuf_len(buf) > len)
-                       sdb_strbuf_append(buf, ",");
-               len = sdb_strbuf_len(buf);
+               strncpy(type, m->store.type, sizeof(type));
+               strncpy(id, m->store.id, sizeof(id));
+               pthread_rwlock_unlock(&global_store->host_lock);
 
-               if (sdb_store_host_tojson(host, buf, filter, flags))
-                       return -1;
+               ts = sdb_plugin_fetch_timeseries(type, id, opts);
+               if (! ts) {
+                       sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
+                                       "- %s fetcher callback returned no data for '%s'",
+                                       hostname, metric, type, id);
+                       status = -1;
+               }
        }
 
-       sdb_strbuf_append(buf, "]");
-
-       sdb_avltree_iter_destroy(host_iter);
-       pthread_rwlock_unlock(&host_lock);
-       return 0;
-} /* sdb_store_tojson */
+       ts_tojson(ts, buf);
+       sdb_object_deref(SDB_OBJ(m));
+       sdb_timeseries_destroy(ts);
+       return status;
+} /* sdb_store_fetch_timeseries */
 
-/* TODO: actually support hierarchical data */
 int
-sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
+sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
+               sdb_store_lookup_cb cb, void *user_data)
 {
-       sdb_avltree_iter_t *host_iter;
+       sdb_avltree_iter_t *host_iter = NULL;
        int status = 0;
 
-       pthread_rwlock_rdlock(&host_lock);
+       if ((! global_store) || (! cb))
+               return -1;
 
-       host_iter = sdb_avltree_get_iter(hosts);
+       if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) {
+               sdb_log(SDB_LOG_ERR, "store: Cannot scan objects of type %d", type);
+               return -1;
+       }
+
+       pthread_rwlock_rdlock(&global_store->host_lock);
+       host_iter = sdb_avltree_get_iter(global_store->hosts);
        if (! host_iter)
                status = -1;
 
        /* has_next returns false if the iterator is NULL */
        while (sdb_avltree_iter_has_next(host_iter)) {
                sdb_store_obj_t *host;
+               sdb_avltree_iter_t *iter = NULL;
 
                host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
                assert(host);
 
-               if (cb(host, user_data)) {
-                       status = -1;
-                       break;
+               if (! sdb_store_matcher_matches(filter, host, NULL))
+                       continue;
+
+               if (type == SDB_SERVICE)
+                       iter = sdb_avltree_get_iter(HOST(host)->services);
+               else if (type == SDB_METRIC)
+                       iter = sdb_avltree_get_iter(HOST(host)->metrics);
+
+               if (iter) {
+                       while (sdb_avltree_iter_has_next(iter)) {
+                               sdb_store_obj_t *obj;
+                               obj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
+                               assert(obj);
+
+                               if (sdb_store_matcher_matches(m, obj, filter)) {
+                                       if (cb(obj, filter, user_data)) {
+                                               sdb_log(SDB_LOG_ERR, "store: Callback returned "
+                                                               "an error while scanning");
+                                               status = -1;
+                                               break;
+                                       }
+                               }
+                       }
+               }
+               else if (sdb_store_matcher_matches(m, host, filter)) {
+                       if (cb(host, filter, user_data)) {
+                               sdb_log(SDB_LOG_ERR, "store: Callback returned "
+                                               "an error while scanning");
+                               status = -1;
+                       }
                }
+
+               sdb_avltree_iter_destroy(iter);
+               if (status)
+                       break;
        }
 
        sdb_avltree_iter_destroy(host_iter);
-       pthread_rwlock_unlock(&host_lock);
+       pthread_rwlock_unlock(&global_store->host_lock);
        return status;
-} /* sdb_store_iterate */
+} /* sdb_store_scan */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */