diff --git a/src/core/store.c b/src/core/store.c
index 0ab57d97be4e5ee884f6a472188fd857eb0bd6b2..3936020d54ccd04b6cc3a9b47d6ed0f25b9627b0 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
pthread_rwlock_t host_lock;
};
-sdb_store_t *global_store = NULL;
-
/*
* private types
*/
static int
host_init(sdb_object_t *obj, va_list ap)
{
- sdb_host_t *sobj = HOST(obj);
+ host_t *sobj = HOST(obj);
int ret;
/* this will consume the first argument (type) of ap */
static void
host_destroy(sdb_object_t *obj)
{
- sdb_host_t *sobj = HOST(obj);
+ host_t *sobj = HOST(obj);
assert(obj);
store_obj_destroy(obj);
static int
service_init(sdb_object_t *obj, va_list ap)
{
- sdb_service_t *sobj = SVC(obj);
+ service_t *sobj = SVC(obj);
int ret;
/* this will consume the first argument (type) of ap */
static void
service_destroy(sdb_object_t *obj)
{
- sdb_service_t *sobj = SVC(obj);
+ service_t *sobj = SVC(obj);
assert(obj);
store_obj_destroy(obj);
};
static sdb_type_t host_type = {
- /* size = */ sizeof(sdb_host_t),
+ /* size = */ sizeof(host_t),
/* init = */ host_init,
/* destroy = */ host_destroy
};
static sdb_type_t service_type = {
- /* size = */ sizeof(sdb_service_t),
+ /* size = */ sizeof(service_t),
/* init = */ service_init,
/* destroy = */ service_destroy
};
};
static sdb_type_t attribute_type = {
- /* size = */ sizeof(sdb_attribute_t),
+ /* size = */ sizeof(attr_t),
/* init = */ attr_init,
/* destroy = */ attr_destroy
};
* private helper functions
*/
-static sdb_host_t *
-lookup_host(sdb_store_t *st, const char *name, bool canonicalize)
-{
- 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
record_backend(sdb_store_obj_t *obj)
{
/* 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)
+get_host_children(host_t *host, int type)
{
if ((type != SDB_SERVICE) && (type != SDB_METRIC)
&& (type != SDB_ATTRIBUTE))
{
sdb_store_t *st = SDB_STORE(user_data);
- sdb_host_t *host;
+ host_t *host;
sdb_avltree_t *attrs;
int status = 0;
return -1;
pthread_rwlock_wrlock(&st->host_lock);
- host = lookup_host(st, hostname, /* canonicalize = */ 1);
+ host = HOST(sdb_avltree_lookup(st->hosts, hostname));
attrs = get_host_children(host, SDB_ATTRIBUTE);
if (! attrs) {
sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
store_host(const char *name, sdb_time_t last_update, sdb_object_t *user_data)
{
sdb_store_t *st = SDB_STORE(user_data);
-
- 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(&st->host_lock);
status = store_obj(NULL, st->hosts,
- SDB_HOST, cname, last_update, NULL);
+ SDB_HOST, name, last_update, NULL);
pthread_rwlock_unlock(&st->host_lock);
- free(cname);
return status;
} /* store_host */
{
sdb_store_t *st = SDB_STORE(user_data);
- sdb_host_t *host;
- sdb_service_t *svc;
+ host_t *host;
+ service_t *svc;
sdb_avltree_t *services;
int status = 0;
return -1;
pthread_rwlock_wrlock(&st->host_lock);
- host = lookup_host(st, hostname, /* canonicalize = */ 1);
+ host = HOST(sdb_avltree_lookup(st->hosts, hostname));
services = get_host_children(host, SDB_SERVICE);
sdb_object_deref(SDB_OBJ(host));
if (! services) {
{
sdb_store_t *st = SDB_STORE(user_data);
- sdb_host_t *host;
+ host_t *host;
sdb_avltree_t *services;
- sdb_data_t d;
int status = 0;
return -1;
pthread_rwlock_wrlock(&st->host_lock);
- host = lookup_host(st, hostname, /* canonicalize = */ 1);
+ host = HOST(sdb_avltree_lookup(st->hosts, hostname));
services = get_host_children(host, SDB_SERVICE);
if (! services) {
sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
sdb_object_deref(SDB_OBJ(host));
pthread_rwlock_unlock(&st->host_lock);
-
- if (status)
- return status;
-
- /* 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;
return status;
} /* store_service */
sdb_store_t *st = SDB_STORE(user_data);
sdb_avltree_t *metrics;
- sdb_host_t *host;
+ host_t *host;
sdb_metric_t *m;
int status = 0;
return -1;
pthread_rwlock_wrlock(&st->host_lock);
- host = lookup_host(st, hostname, /* canonicalize = */ 1);
+ host = HOST(sdb_avltree_lookup(st->hosts, hostname));
metrics = get_host_children(host, SDB_METRIC);
sdb_object_deref(SDB_OBJ(host));
if (! metrics) {
sdb_store_t *st = SDB_STORE(user_data);
sdb_store_obj_t *obj = NULL;
- sdb_host_t *host;
+ host_t *host;
sdb_metric_t *metric;
- sdb_data_t d;
sdb_avltree_t *metrics;
}
pthread_rwlock_wrlock(&st->host_lock);
- host = lookup_host(st, hostname, /* canonicalize = */ 1);
+ host = HOST(sdb_avltree_lookup(st->hosts, hostname));
metrics = get_host_children(host, SDB_METRIC);
if (! metrics) {
sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
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 */
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,
static int
execute_query(sdb_object_t *q,
sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
- sdb_object_t __attribute__((unused)) *user_data)
+ sdb_object_t *user_data)
{
- return sdb_store_query_execute(QUERY(q), buf, errbuf);
+ return sdb_store_query_execute(SDB_STORE(user_data),
+ QUERY(q), buf, errbuf);
} /* execute_query */
sdb_store_reader_t sdb_store_reader = {
return SDB_STORE(sdb_object_create("store", store_type));
} /* sdb_store_create */
-int
-sdb_store_init(void)
-{
- if (global_store)
- return 0;
-
- global_store = SDB_STORE(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",
- &sdb_store_writer, SDB_OBJ(global_store)))
- return -1;
- return sdb_plugin_register_reader("memstore",
- &sdb_store_reader, SDB_OBJ(global_store));
-} /* sdb_store_init */
-
-void
-sdb_store_clear(void)
-{
- if (! global_store)
- return;
- sdb_avltree_clear(global_store->hosts);
-} /* sdb_store_clear */
-
int
sdb_store_host(sdb_store_t *store, const char *name, sdb_time_t last_update)
{
} /* sdb_store_metric_attr */
sdb_store_obj_t *
-sdb_store_get_host(const char *name)
+sdb_store_get_host(sdb_store_t *store, const char *name)
{
- sdb_host_t *host;
+ host_t *host;
- if ((! global_store) || (! name))
+ if ((! store) || (! name))
return NULL;
- host = lookup_host(global_store, name, /* canonicalize = */ 0);
+ host = HOST(sdb_avltree_lookup(store->hosts, name));
if (! host)
return NULL;
/* TODO: sdb_store_fetch_timeseries should move into the plugin module */
int
-sdb_store_fetch_timeseries(const char *hostname, const char *metric,
+sdb_store_fetch_timeseries(sdb_store_t *store,
+ const char *hostname, const char *metric,
sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf)
{
sdb_avltree_t *metrics;
- sdb_host_t *host;
+ host_t *host;
sdb_metric_t *m;
sdb_timeseries_t *ts;
int status = 0;
- if ((! global_store) || (! hostname) || (! metric) || (! opts) || (! buf))
+ if ((! store) || (! hostname) || (! metric) || (! opts) || (! buf))
return -1;
- pthread_rwlock_rdlock(&global_store->host_lock);
- host = lookup_host(global_store, hostname, /* canonicalize = */ 1);
+ pthread_rwlock_rdlock(&store->host_lock);
+ host = HOST(sdb_avltree_lookup(store->hosts, hostname));
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);
+ pthread_rwlock_unlock(&store->host_lock);
return -1;
}
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);
+ pthread_rwlock_unlock(&store->host_lock);
return -1;
}
"- no data-store configured for the stored metric",
hostname, metric);
sdb_object_deref(SDB_OBJ(m));
- pthread_rwlock_unlock(&global_store->host_lock);
+ pthread_rwlock_unlock(&store->host_lock);
return -1;
}
strncpy(type, m->store.type, sizeof(type));
strncpy(id, m->store.id, sizeof(id));
- pthread_rwlock_unlock(&global_store->host_lock);
+ pthread_rwlock_unlock(&store->host_lock);
ts = sdb_plugin_fetch_timeseries(type, id, opts);
if (! ts) {
} /* sdb_store_fetch_timeseries */
int
-sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
+sdb_store_scan(sdb_store_t *store, 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 = NULL;
int status = 0;
- if ((! global_store) || (! cb))
+ if ((! store) || (! cb))
return -1;
if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) {
return -1;
}
- pthread_rwlock_rdlock(&global_store->host_lock);
- host_iter = sdb_avltree_get_iter(global_store->hosts);
+ pthread_rwlock_rdlock(&store->host_lock);
+ host_iter = sdb_avltree_get_iter(store->hosts);
if (! host_iter)
status = -1;
}
sdb_avltree_iter_destroy(host_iter);
- pthread_rwlock_unlock(&global_store->host_lock);
+ pthread_rwlock_unlock(&store->host_lock);
return status;
} /* sdb_store_scan */