summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 11824e1)
raw | patch | inline | side by side (parent: 11824e1)
author | Sebastian Harl <sh@tokkee.org> | |
Fri, 7 Aug 2015 16:26:51 +0000 (18:26 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Fri, 7 Aug 2015 16:26:51 +0000 (18:26 +0200) |
That is, instead of operating on a global, shared instance.
diff --git a/src/core/store.c b/src/core/store.c
index 0ab57d97be4e5ee884f6a472188fd857eb0bd6b2..e24d2ef0a5356bf0a2badc8aa5fe468f94983a4f 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
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 = {
} /* 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;
- if ((! global_store) || (! name))
+ if ((! store) || (! name))
return NULL;
- host = lookup_host(global_store, name, /* canonicalize = */ 0);
+ host = lookup_host(store, name, /* canonicalize = */ 0);
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;
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 = lookup_host(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);
+ 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 */
diff --git a/src/core/store_exec.c b/src/core/store_exec.c
index 705fbe163afa4468fbe937101ca218c24248acc3..5daaa143105558c133f6649d8d44e7c24e0f9307 100644 (file)
--- a/src/core/store_exec.c
+++ b/src/core/store_exec.c
*/
static int
-exec_fetch(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type,
- const char *hostname, const char *name, sdb_store_matcher_t *filter)
+exec_fetch(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
+ int type, const char *hostname, const char *name,
+ sdb_store_matcher_t *filter)
{
uint32_t res_type = htonl(SDB_CONNECTION_FETCH);
if (type == SDB_HOST)
hostname = name;
- host = sdb_store_get_host(hostname);
+ host = sdb_store_get_host(store, hostname);
if ((! host)
|| (filter && (! sdb_store_matcher_matches(filter, host, NULL)))) {
sdb_strbuf_sprintf(errbuf, "Failed to fetch %s %s: "
} /* exec_fetch */
static int
-exec_list(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type,
- sdb_store_matcher_t *filter)
+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);
sdb_store_json_formatter_t *f;
}
sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
- if (sdb_store_scan(type, /* m = */ NULL, filter, list_tojson, f)) {
+ if (sdb_store_scan(store, type, /* m = */ NULL, filter, list_tojson, f)) {
sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
"store to JSON");
sdb_strbuf_sprintf(errbuf, "Out of memory");
} /* exec_list */
static int
-exec_lookup(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type,
- sdb_store_matcher_t *m, sdb_store_matcher_t *filter)
+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);
sdb_store_json_formatter_t *f;
sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
- if (sdb_store_scan(type, m, filter, lookup_tojson, f)) {
+ if (sdb_store_scan(store, type, m, filter, lookup_tojson, f)) {
sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup %ss",
SDB_STORE_TYPE_TO_NAME(type));
sdb_strbuf_sprintf(errbuf, "Failed to lookup %ss",
} /* exec_store */
static int
-exec_timeseries(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
+exec_timeseries(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
const char *hostname, const char *metric,
sdb_timeseries_opts_t *opts)
{
uint32_t res_type = htonl(SDB_CONNECTION_TIMESERIES);
sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
- if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) {
+ if (sdb_store_fetch_timeseries(store, hostname, metric, opts, buf)) {
sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series");
sdb_strbuf_sprintf(errbuf, "Failed to fetch time-series");
return -1;
*/
int
-sdb_store_query_execute(sdb_store_query_t *q,
+sdb_store_query_execute(sdb_store_t *store, sdb_store_query_t *q,
sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
{
sdb_timeseries_opts_t ts_opts;
ast = q->ast;
switch (ast->type) {
case SDB_AST_TYPE_FETCH:
- return exec_fetch(buf, errbuf, SDB_AST_FETCH(ast)->obj_type,
+ return exec_fetch(store, buf, errbuf, SDB_AST_FETCH(ast)->obj_type,
SDB_AST_FETCH(ast)->hostname, SDB_AST_FETCH(ast)->name,
q->filter);
case SDB_AST_TYPE_LIST:
- return exec_list(buf, errbuf, SDB_AST_LIST(ast)->obj_type,
+ return exec_list(store, buf, errbuf, SDB_AST_LIST(ast)->obj_type,
q->filter);
case SDB_AST_TYPE_LOOKUP:
- return exec_lookup(buf, errbuf, SDB_AST_LOOKUP(ast)->obj_type,
+ return exec_lookup(store, buf, errbuf, SDB_AST_LOOKUP(ast)->obj_type,
q->matcher, q->filter);
case SDB_AST_TYPE_STORE:
case SDB_AST_TYPE_TIMESERIES:
ts_opts.start = SDB_AST_TIMESERIES(ast)->start;
ts_opts.end = SDB_AST_TIMESERIES(ast)->end;
- return exec_timeseries(buf, errbuf, SDB_AST_TIMESERIES(ast)->hostname,
+ return exec_timeseries(store, buf, errbuf,
+ SDB_AST_TIMESERIES(ast)->hostname,
SDB_AST_TIMESERIES(ast)->metric, &ts_opts);
default:
index 1cb0e5af1a6f386a5b2ad8af508edd1ce2e8b098..aa53e8eadec81721c19d0c84adefe7634a146bb3 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
/*
* sdb_store_get_host:
- * Query the store for a host by its (canonicalized) name.
+ * Query the specified store for a host by its (canonicalized) name.
*
* The function increments the ref count of the host object. The caller needs
* to deref it when no longer using it.
*/
sdb_store_obj_t *
-sdb_store_get_host(const char *name);
+sdb_store_get_host(sdb_store_t *store, const char *name);
/*
* sdb_store_fetch_timeseries:
* Fetch the time-series described by the specified host's metric and
- * serialize it as JSON into the provided string buffer.
+ * serialize it as JSON into the provided string buffer. The host data is
+ * retrieved from the specified store.
*
* Returns:
* - 0 on success
* - a negative value else
*/
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_store_query_execute:
- * Execute a previously prepared query. The query result will be written to
- * 'buf' and any errors to 'errbuf'.
+ * Execute a previously prepared query in the specified store. The query
+ * result will be written to 'buf' and any errors to 'errbuf'.
*
* Returns:
* - the result type (to be used by the server reply)
* - a negative value on error
*/
int
-sdb_store_query_execute(sdb_store_query_t *m,
+sdb_store_query_execute(sdb_store_t *store, sdb_store_query_t *m,
sdb_strbuf_t *buf, sdb_strbuf_t *errbuf);
/*
/*
* sdb_store_scan:
- * Look up objects of the specified type in the store. The specified callback
- * function is called for each object in the store matching 'm'. The function
- * performs a full scan of all objects stored in the database. If specified,
- * the filter will be used to preselect objects for further evaluation. See
- * the description of 'sdb_store_matcher_matches' for details.
+ * Look up objects of the specified type in the specified store. The specified
+ * callback function is called for each object in the store matching 'm'. The
+ * function performs a full scan of all objects stored in the database. If
+ * specified, the filter will be used to preselect objects for further
+ * evaluation. See the description of 'sdb_store_matcher_matches' for details.
*
* Returns:
* - 0 on success
* - a negative value else
*/
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);
/*
index 153e52f1c84bb393bcd560541485ee71e5af8044..7a918ce98347635222aa246642d7886d63f63ab6 100644 (file)
#include <check.h>
+static sdb_store_t *store;
+
static void
populate(void)
{
size_t i;
- sdb_store_init();
+ store = sdb_store_create();
+ ck_assert(store != NULL);
for (i = 0; i < SDB_STATIC_ARRAY_LEN(hosts); ++i) {
- int status = sdb_plugin_store_host(hosts[i], 1);
+ int status = sdb_store_host(store, hosts[i], 1);
ck_assert(status == 0);
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(metrics); ++i) {
- int status = sdb_plugin_store_metric(metrics[i].host,
+ int status = sdb_store_metric(store, metrics[i].host,
metrics[i].metric, /* store */ NULL, 1);
ck_assert(status == 0);
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) {
- int status = sdb_plugin_store_service(services[i].host,
+ int status = sdb_store_service(store, services[i].host,
services[i].service, 1);
ck_assert(status == 0);
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(attrs); ++i) {
- int status = sdb_plugin_store_attribute(attrs[i].host,
+ int status = sdb_store_attribute(store, attrs[i].host,
attrs[i].name, &attrs[i].value, 1);
ck_assert(status == 0);
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(svc_attrs); ++i) {
- int status = sdb_plugin_store_service_attribute(svc_attrs[i].host,
+ int status = sdb_store_service_attr(store, svc_attrs[i].host,
svc_attrs[i].service, svc_attrs[i].name,
&svc_attrs[i].value, 1);
ck_assert(status == 0);
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(metric_attrs); ++i) {
- int status = sdb_plugin_store_metric_attribute(metric_attrs[i].host,
+ int status = sdb_store_metric_attr(store, metric_attrs[i].host,
metric_attrs[i].metric, metric_attrs[i].name,
&metric_attrs[i].value, 1);
ck_assert(status == 0);
}
} /* populate */
+static void
+turndown(void)
+{
+ sdb_object_deref(SDB_OBJ(store));
+ store = NULL;
+} /* turndown */
+
#define NAME { SDB_TYPE_INTEGER, { .integer = SDB_FIELD_NAME } }
#define LAST_UPDATE { SDB_TYPE_INTEGER, { .integer = SDB_FIELD_LAST_UPDATE } }
#define AGE { SDB_TYPE_INTEGER, { .integer = SDB_FIELD_AGE } }
size_t i;
if (expr_iter_data[_i].host) {
- obj = sdb_store_get_host(expr_iter_data[_i].host);
+ obj = sdb_store_get_host(store, expr_iter_data[_i].host);
ck_assert(obj != NULL);
if (expr_iter_data[_i].child) {
TEST_MAIN("core::store_expr")
{
TCase *tc = tcase_create("core");
- tcase_add_checked_fixture(tc, populate, sdb_store_clear);
+ tcase_add_checked_fixture(tc, populate, turndown);
TC_ADD_LOOP_TEST(tc, expr_iter);
ADD_TCASE(tc);
}
index 6be3c144e37f3cc2466f1ffa840a408c4e12a0ce..d887c439f0bddbb4c21572402d27e9d490245d24 100644 (file)
#undef SDB_INTERVAL_SECOND
#define SDB_INTERVAL_SECOND 1000000000L
+static sdb_store_t *store;
+
static void
populate(void)
{
sdb_data_t datum;
- sdb_store_init();
+ store = sdb_store_create();
+ ck_assert(store != NULL);
- sdb_plugin_store_host("h1", 1 * SDB_INTERVAL_SECOND);
- sdb_plugin_store_host("h2", 3 * SDB_INTERVAL_SECOND);
+ sdb_store_host(store, "h1", 1 * SDB_INTERVAL_SECOND);
+ sdb_store_host(store, "h2", 3 * SDB_INTERVAL_SECOND);
datum.type = SDB_TYPE_STRING;
datum.data.string = "v1";
- sdb_plugin_store_attribute("h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND);
+ sdb_store_attribute(store, "h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND);
datum.data.string = "v2";
- sdb_plugin_store_attribute("h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND);
+ sdb_store_attribute(store, "h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND);
datum.data.string = "v3";
- sdb_plugin_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
+ sdb_store_attribute(store, "h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
/* make sure that older updates don't overwrite existing values */
datum.data.string = "fail";
- sdb_plugin_store_attribute("h1", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
- sdb_plugin_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
+ sdb_store_attribute(store, "h1", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
+ sdb_store_attribute(store, "h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
- sdb_plugin_store_metric("h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND);
- sdb_plugin_store_metric("h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
- sdb_plugin_store_metric("h2", "m1", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
+ sdb_store_metric(store, "h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND);
+ sdb_store_metric(store, "h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
+ sdb_store_metric(store, "h2", "m1", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
- sdb_plugin_store_service("h2", "s1", 1 * SDB_INTERVAL_SECOND);
- sdb_plugin_store_service("h2", "s2", 2 * SDB_INTERVAL_SECOND);
+ sdb_store_service(store, "h2", "s1", 1 * SDB_INTERVAL_SECOND);
+ sdb_store_service(store, "h2", "s2", 2 * SDB_INTERVAL_SECOND);
datum.type = SDB_TYPE_INTEGER;
datum.data.integer = 42;
- sdb_plugin_store_metric_attribute("h1", "m1", "k3",
+ sdb_store_metric_attr(store, "h1", "m1", "k3",
&datum, 2 * SDB_INTERVAL_SECOND);
datum.data.integer = 123;
- sdb_plugin_store_service_attribute("h2", "s2", "k1",
+ sdb_store_service_attr(store, "h2", "s2", "k1",
&datum, 2 * SDB_INTERVAL_SECOND);
datum.data.integer = 4711;
- sdb_plugin_store_service_attribute("h2", "s2", "k2",
+ sdb_store_service_attr(store, "h2", "s2", "k2",
&datum, 1 * SDB_INTERVAL_SECOND);
/* don't overwrite k1 */
datum.data.integer = 666;
- sdb_plugin_store_service_attribute("h2", "s2", "k1",
+ sdb_store_service_attr(store, "h2", "s2", "k1",
&datum, 2 * SDB_INTERVAL_SECOND);
} /* populate */
+static void
+turndown(void)
+{
+ sdb_object_deref(SDB_OBJ(store));
+ store = NULL;
+} /* turndown */
+
static int
scan_tojson(sdb_store_obj_t *obj,
sdb_store_matcher_t __attribute__((unused)) *filter,
store_tojson_data[_i].type, SDB_WANT_ARRAY);
ck_assert(f != NULL);
- status = sdb_store_scan(store_tojson_data[_i].type,
+ status = sdb_store_scan(store, store_tojson_data[_i].type,
/* m = */ NULL, filter, store_tojson_data[_i].f, f);
fail_unless(status == 0,
"sdb_store_scan(HOST, ..., tojson) = %d; expected: 0",
TEST_MAIN("core::store_json")
{
TCase *tc = tcase_create("core");
+ tcase_add_unchecked_fixture(tc, populate, turndown);
TC_ADD_LOOP_TEST(tc, store_tojson);
- tcase_add_unchecked_fixture(tc, populate, sdb_store_clear);
ADD_TCASE(tc);
}
TEST_MAIN_END
index 49f2268a24d0758dc5165934bb5c75d7908a9947..9f486836aaec3daf85818ed07c7d17c67c327c63 100644 (file)
#include <check.h>
#include <string.h>
+static sdb_store_t *store;
+
static void
populate(void)
{
size_t i;
- sdb_store_init();
+ store = sdb_store_create();
+ ck_assert(store != NULL);
for (i = 0; i < SDB_STATIC_ARRAY_LEN(hosts); ++i) {
- int status = sdb_plugin_store_host(hosts[i], 1);
+ int status = sdb_store_host(store, hosts[i], 1);
fail_unless(status == 0,
- "sdb_plugin_store_host(%s, 1) = %d; expected: 0",
+ "sdb_store_host(%s, 1) = %d; expected: 0",
hosts[i], status);
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(metrics); ++i) {
- int status = sdb_plugin_store_metric(metrics[i].host,
+ int status = sdb_store_metric(store, metrics[i].host,
metrics[i].metric, /* store */ NULL, 1);
fail_unless(status == 0,
- "sdb_plugin_store_metric(%s, %s, NULL, 1) = %d; expected: 0",
+ "sdb_store_metric(%s, %s, NULL, 1) = %d; expected: 0",
metrics[i].host, metrics[i].metric, status);
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) {
- int status = sdb_plugin_store_service(services[i].host,
+ int status = sdb_store_service(store, services[i].host,
services[i].service, 1);
fail_unless(status == 0,
- "sdb_plugin_store_service(%s, %s, 1) = %d; expected: 0",
+ "sdb_store_service(%s, %s, 1) = %d; expected: 0",
services[i].host, services[i].service, status);
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(attrs); ++i) {
- int status = sdb_plugin_store_attribute(attrs[i].host,
+ int status = sdb_store_attribute(store, attrs[i].host,
attrs[i].name, &attrs[i].value, 1);
fail_unless(status == 0,
- "sdb_plugin_store_attribute(%s, %s, <val>, 1) = %d; expected: 0",
+ "sdb_store_attribute(%s, %s, <val>, 1) = %d; expected: 0",
attrs[i].host, attrs[i].name, status);
}
} /* populate */
+static void
+turndown(void)
+{
+ sdb_object_deref(SDB_OBJ(store));
+ store = NULL;
+} /* turndown */
+
struct {
int type;
char *name;
sdb_store_matcher_t *m, *n;
int status;
- host = sdb_store_get_host("a");
+ host = sdb_store_get_host(store, "a");
fail_unless(host != NULL,
"sdb_store_get_host(a) = NULL; expected: <host>");
const char *op_str[] = { "<", "<=", "=", ">=", ">" };
ck_assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
- host = sdb_store_get_host("a");
+ host = sdb_store_get_host(store, "a");
fail_unless(host != NULL,
"sdb_store_get_host(a) = NULL; expected: <host>");
ck_assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
- host = sdb_store_get_host(cmp_obj_data[_i].host);
+ host = sdb_store_get_host(store, cmp_obj_data[_i].host);
fail_unless(host != NULL,
"sdb_store_get_host(%s) = NULL; expected: <host>",
cmp_obj_data[_i].host);
int status;
size_t i;
- obj = sdb_store_get_host("a");
+ obj = sdb_store_get_host(store, "a");
status = sdb_store_matcher_matches(always, obj, /* filter */ NULL);
fail_unless(status == 1,
int check, n;
n = 0;
- check = sdb_store_scan(SDB_HOST, /* matcher */ NULL, /* filter */ NULL,
+ check = sdb_store_scan(store, SDB_HOST,
+ /* matcher */ NULL, /* filter */ NULL,
scan_cb, &n);
fail_unless(check == 0,
"sdb_store_scan() = %d; expected: 0", check);
}
n = 0;
- sdb_store_scan(SDB_HOST, m, filter, scan_cb, &n);
+ sdb_store_scan(store, SDB_HOST, m, filter, scan_cb, &n);
fail_unless(n == scan_data[_i].expected,
"sdb_store_scan(HOST, matcher{%s}, filter{%s}) "
"found %d hosts; expected: %d", scan_data[_i].query,
TEST_MAIN("core::store_lookup")
{
TCase *tc = tcase_create("core");
- tcase_add_checked_fixture(tc, populate, sdb_store_clear);
+ tcase_add_checked_fixture(tc, populate, turndown);
TC_ADD_LOOP_TEST(tc, cmp_name);
TC_ADD_LOOP_TEST(tc, cmp_attr);
TC_ADD_LOOP_TEST(tc, cmp_obj);
index 6d528dede366fe9bce639e9e6eb10eba4cb7a268..1d524b6070bb966ca8c7ae74b3963fd3bd00281b 100644 (file)
--- a/t/unit/core/store_test.c
+++ b/t/unit/core/store_test.c
#include <string.h>
#include <strings.h>
+static sdb_store_t *store;
+
static void
init(void)
{
- sdb_store_init();
+ store = sdb_store_create();
+ ck_assert(store != NULL);
}
static void
{
sdb_data_t datum;
- sdb_plugin_store_host("h1", 1);
- sdb_plugin_store_host("h2", 3);
+ sdb_store_host(store, "h1", 1);
+ sdb_store_host(store, "h2", 3);
datum.type = SDB_TYPE_STRING;
datum.data.string = "v1";
- sdb_plugin_store_attribute("h1", "k1", &datum, 1);
+ sdb_store_attribute(store, "h1", "k1", &datum, 1);
datum.data.string = "v2";
- sdb_plugin_store_attribute("h1", "k2", &datum, 2);
+ sdb_store_attribute(store, "h1", "k2", &datum, 2);
datum.data.string = "v3";
- sdb_plugin_store_attribute("h1", "k3", &datum, 2);
+ sdb_store_attribute(store, "h1", "k3", &datum, 2);
/* make sure that older updates don't overwrite existing values */
datum.data.string = "fail";
- sdb_plugin_store_attribute("h1", "k2", &datum, 1);
- sdb_plugin_store_attribute("h1", "k3", &datum, 2);
+ sdb_store_attribute(store, "h1", "k2", &datum, 1);
+ sdb_store_attribute(store, "h1", "k3", &datum, 2);
- sdb_plugin_store_metric("h1", "m1", /* store */ NULL, 2);
- sdb_plugin_store_metric("h1", "m2", /* store */ NULL, 1);
- sdb_plugin_store_metric("h2", "m1", /* store */ NULL, 1);
+ sdb_store_metric(store, "h1", "m1", /* store */ NULL, 2);
+ sdb_store_metric(store, "h1", "m2", /* store */ NULL, 1);
+ sdb_store_metric(store, "h2", "m1", /* store */ NULL, 1);
- sdb_plugin_store_service("h2", "s1", 1);
- sdb_plugin_store_service("h2", "s2", 2);
+ sdb_store_service(store, "h2", "s1", 1);
+ sdb_store_service(store, "h2", "s2", 2);
datum.type = SDB_TYPE_INTEGER;
datum.data.integer = 42;
- sdb_plugin_store_metric_attribute("h1", "m1", "k3", &datum, 2);
+ sdb_store_metric_attr(store, "h1", "m1", "k3", &datum, 2);
datum.data.integer = 123;
- sdb_plugin_store_service_attribute("h2", "s2", "k1", &datum, 2);
+ sdb_store_service_attr(store, "h2", "s2", "k1", &datum, 2);
datum.data.integer = 4711;
- sdb_plugin_store_service_attribute("h2", "s2", "k2", &datum, 1);
+ sdb_store_service_attr(store, "h2", "s2", "k2", &datum, 1);
/* don't overwrite k1 */
datum.data.integer = 666;
- sdb_plugin_store_service_attribute("h2", "s2", "k1", &datum, 2);
+ sdb_store_service_attr(store, "h2", "s2", "k1", &datum, 2);
} /* populate */
+static void
+turndown(void)
+{
+ sdb_object_deref(SDB_OBJ(store));
+ store = NULL;
+} /* turndown */
+
START_TEST(test_store_host)
{
struct {
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
int status;
- status = sdb_plugin_store_host(golden_data[i].name,
+ status = sdb_store_host(store, golden_data[i].name,
golden_data[i].last_update);
fail_unless(status == golden_data[i].expected,
- "sdb_plugin_store_host(%s, %d) = %d; expected: %d",
+ "sdb_store_host(%s, %d) = %d; expected: %d",
golden_data[i].name, (int)golden_data[i].last_update,
status, golden_data[i].expected);
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) {
sdb_store_obj_t *have;
- have = sdb_store_get_host(golden_hosts[i].name);
+ have = sdb_store_get_host(store, golden_hosts[i].name);
fail_unless((have != NULL) == golden_hosts[i].have,
"sdb_store_get_host(%s) = %p; expected: %s",
golden_hosts[i].name, have,
size_t i;
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) {
- int status = sdb_plugin_store_host(golden_hosts[i], 1);
+ int status = sdb_store_host(store, golden_hosts[i], 1);
fail_unless(status >= 0,
- "sdb_plugin_store_host(%s) = %d; expected: >=0",
+ "sdb_store_host(%s) = %d; expected: >=0",
golden_hosts[i], status);
}
sdb_store_obj_t *sobj1, *sobj2;
int ref_cnt;
- sobj1 = sdb_store_get_host(golden_hosts[i]);
+ sobj1 = sdb_store_get_host(store, golden_hosts[i]);
fail_unless(sobj1 != NULL,
"sdb_store_get_host(%s) = NULL; expected: <host>",
golden_hosts[i]);
"sdb_store_get_host(%s) did not increment ref count: "
"got: %d; expected: >1", golden_hosts[i], ref_cnt);
- sobj2 = sdb_store_get_host(golden_hosts[i]);
+ sobj2 = sdb_store_get_host(store, golden_hosts[i]);
fail_unless(sobj2 != NULL,
"sdb_store_get_host(%s) = NULL; expected: <host>",
golden_hosts[i]);
for (i = 0; i < SDB_STATIC_ARRAY_LEN(unknown_hosts); ++i) {
sdb_store_obj_t *sobj;
- sobj = sdb_store_get_host(unknown_hosts[i]);
+ sobj = sdb_store_get_host(store, unknown_hosts[i]);
fail_unless(!sobj, "sdb_store_get_host(%s) = <host:%s>; expected: NULL",
unknown_hosts[i], sobj ? SDB_OBJ(sobj)->name : "NULL");
}
size_t i;
- sdb_plugin_store_host("l", 1);
- sdb_plugin_store_host("m", 1);
+ sdb_store_host(store, "l", 1);
+ sdb_store_host(store, "m", 1);
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
sdb_data_t datum;
int status;
datum.type = SDB_TYPE_STRING;
datum.data.string = golden_data[i].value;
- status = sdb_plugin_store_attribute(golden_data[i].host,
+ status = sdb_store_attribute(store, golden_data[i].host,
golden_data[i].key, &datum,
golden_data[i].last_update);
fail_unless(status == golden_data[i].expected,
- "sdb_plugin_store_attribute(%s, %s, %s, %d) = %d; expected: %d",
+ "sdb_store_attribute(%s, %s, %s, %d) = %d; expected: %d",
golden_data[i].host, golden_data[i].key, golden_data[i].value,
golden_data[i].last_update, status, golden_data[i].expected);
}
size_t i;
- sdb_plugin_store_host("m", 1);
- sdb_plugin_store_host("l", 1);
+ sdb_store_host(store, "m", 1);
+ sdb_store_host(store, "l", 1);
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
int status;
- status = sdb_plugin_store_metric(golden_data[i].host,
+ status = sdb_store_metric(store, golden_data[i].host,
golden_data[i].metric, golden_data[i].store,
golden_data[i].last_update);
fail_unless(status == golden_data[i].expected,
- "sdb_plugin_store_metric(%s, %s, %p, %d) = %d; expected: %d",
+ "sdb_store_metric(%s, %s, %p, %d) = %d; expected: %d",
golden_data[i].host, golden_data[i].metric,
golden_data[i].store, golden_data[i].last_update,
status, golden_data[i].expected);
size_t i;
- sdb_plugin_store_host("m", 1);
- sdb_plugin_store_host("l", 1);
- sdb_plugin_store_metric("m", "m1", NULL, 1);
- sdb_plugin_store_metric("l", "m1", NULL, 1);
- sdb_plugin_store_metric("l", "m2", NULL, 1);
+ sdb_store_host(store, "m", 1);
+ sdb_store_host(store, "l", 1);
+ sdb_store_metric(store, "m", "m1", NULL, 1);
+ sdb_store_metric(store, "l", "m1", NULL, 1);
+ sdb_store_metric(store, "l", "m2", NULL, 1);
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
int status;
- status = sdb_plugin_store_metric_attribute(golden_data[i].host,
+ status = sdb_store_metric_attr(store, golden_data[i].host,
golden_data[i].metric, golden_data[i].attr,
&golden_data[i].value, golden_data[i].last_update);
fail_unless(status == golden_data[i].expected,
- "sdb_plugin_store_metric_attribute(%s, %s, %s, %d, %d) = %d; "
+ "sdb_store_metric_attr(%s, %s, %s, %d, %d) = %d; "
"expected: %d", golden_data[i].host, golden_data[i].metric,
golden_data[i].attr, golden_data[i].value.data.integer,
golden_data[i].last_update, status, golden_data[i].expected);
size_t i;
- sdb_plugin_store_host("m", 1);
- sdb_plugin_store_host("l", 1);
+ sdb_store_host(store, "m", 1);
+ sdb_store_host(store, "l", 1);
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
int status;
- status = sdb_plugin_store_service(golden_data[i].host,
+ status = sdb_store_service(store, golden_data[i].host,
golden_data[i].svc, golden_data[i].last_update);
fail_unless(status == golden_data[i].expected,
- "sdb_plugin_store_service(%s, %s, %d) = %d; expected: %d",
+ "sdb_store_service(%s, %s, %d) = %d; expected: %d",
golden_data[i].host, golden_data[i].svc,
golden_data[i].last_update, status, golden_data[i].expected);
}
size_t i;
- sdb_plugin_store_host("m", 1);
- sdb_plugin_store_host("l", 1);
- sdb_plugin_store_service("m", "s1", 1);
- sdb_plugin_store_service("l", "s1", 1);
- sdb_plugin_store_service("l", "s2", 1);
+ sdb_store_host(store, "m", 1);
+ sdb_store_host(store, "l", 1);
+ sdb_store_service(store, "m", "s1", 1);
+ sdb_store_service(store, "l", "s1", 1);
+ sdb_store_service(store, "l", "s2", 1);
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
int status;
- status = sdb_plugin_store_service_attribute(golden_data[i].host,
+ status = sdb_store_service_attr(store, golden_data[i].host,
golden_data[i].svc, golden_data[i].attr,
&golden_data[i].value, golden_data[i].last_update);
fail_unless(status == golden_data[i].expected,
- "sdb_plugin_store_service_attribute(%s, %s, %s, %d, %d) = %d; "
+ "sdb_store_service_attr(%s, %s, %s, %d, %d) = %d; "
"expected: %d", golden_data[i].host, golden_data[i].svc,
golden_data[i].attr, golden_data[i].value.data.integer,
golden_data[i].last_update, status, golden_data[i].expected);
sdb_time_t now = sdb_gettime();
int check;
- sdb_plugin_store_host("host", 10);
- sdb_plugin_store_host("host", 20);
- sdb_plugin_store_attribute("host", "attr", &get_field_data[_i].value, 10);
- sdb_plugin_store_attribute("host", "attr", &get_field_data[_i].value, 20);
+ sdb_store_host(store, "host", 10);
+ sdb_store_host(store, "host", 20);
+ sdb_store_attribute(store, "host", "attr", &get_field_data[_i].value, 10);
+ sdb_store_attribute(store, "host", "attr", &get_field_data[_i].value, 20);
if (get_field_data[_i].hostname) {
- obj = sdb_store_get_host(get_field_data[_i].hostname);
+ obj = sdb_store_get_host(store, get_field_data[_i].hostname);
ck_assert(obj != NULL);
if (get_field_data[_i].attr) {
sdb_store_obj_t *obj;
const char *expected_name = golden_data[i].host;
- obj = sdb_store_get_host(golden_data[i].host);
+ obj = sdb_store_get_host(store, golden_data[i].host);
if (golden_data[i].expected && (golden_data[i].type == SDB_HOST))
fail_unless(obj == NULL,
"sdb_store_get_host(%s) = %p; expected: NULL",
sdb_store_obj_t *host;
/* 10 us interval */
- sdb_plugin_store_host("host", 10);
- sdb_plugin_store_host("host", 20);
- sdb_plugin_store_host("host", 30);
- sdb_plugin_store_host("host", 40);
+ sdb_store_host(store, "host", 10);
+ sdb_store_host(store, "host", 20);
+ sdb_store_host(store, "host", 30);
+ sdb_store_host(store, "host", 40);
- host = sdb_store_get_host("host");
+ host = sdb_store_get_host(store, "host");
fail_unless(host != NULL,
"INTERNAL ERROR: store doesn't have host after adding it");
fail_unless(host->interval == 10,
- "sdb_plugin_store_host() did not calculate interval correctly: "
+ "sdb_store_host() did not calculate interval correctly: "
"got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 10);
/* multiple updates for the same timestamp don't modify the interval */
- sdb_plugin_store_host("host", 40);
- sdb_plugin_store_host("host", 40);
- sdb_plugin_store_host("host", 40);
- sdb_plugin_store_host("host", 40);
+ sdb_store_host(store, "host", 40);
+ sdb_store_host(store, "host", 40);
+ sdb_store_host(store, "host", 40);
+ sdb_store_host(store, "host", 40);
fail_unless(host->interval == 10,
- "sdb_plugin_store_host() changed interval when doing multiple updates "
+ "sdb_store_host() changed interval when doing multiple updates "
"using the same timestamp; got: %"PRIsdbTIME"; "
"expected: %"PRIsdbTIME, host->interval, 10);
/* multiple updates using an timestamp don't modify the interval */
- sdb_plugin_store_host("host", 20);
- sdb_plugin_store_host("host", 20);
- sdb_plugin_store_host("host", 20);
- sdb_plugin_store_host("host", 20);
+ sdb_store_host(store, "host", 20);
+ sdb_store_host(store, "host", 20);
+ sdb_store_host(store, "host", 20);
+ sdb_store_host(store, "host", 20);
fail_unless(host->interval == 10,
- "sdb_plugin_store_host() changed interval when doing multiple updates "
+ "sdb_store_host() changed interval when doing multiple updates "
"using an old timestamp; got: %"PRIsdbTIME"; expected: %"PRIsdbTIME,
host->interval, 10);
/* new interval: 20 us */
- sdb_plugin_store_host("host", 60);
+ sdb_store_host(store, "host", 60);
fail_unless(host->interval == 11,
- "sdb_plugin_store_host() did not calculate interval correctly: "
+ "sdb_store_host() did not calculate interval correctly: "
"got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 11);
/* new interval: 40 us */
- sdb_plugin_store_host("host", 100);
+ sdb_store_host(store, "host", 100);
fail_unless(host->interval == 13,
- "sdb_plugin_store_host() did not calculate interval correctly: "
+ "sdb_store_host() did not calculate interval correctly: "
"got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 11);
sdb_object_deref(SDB_OBJ(host));
int check;
/* empty store */
- check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL,
+ check = sdb_store_scan(store, SDB_HOST, /* m, filter = */ NULL, NULL,
scan_count, &i);
fail_unless(check == 0,
"sdb_store_scan(HOST), empty store = %d; expected: 0", check);
populate();
- check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL,
+ check = sdb_store_scan(store, SDB_HOST, /* m, filter = */ NULL, NULL,
scan_count, &i);
fail_unless(check == 0,
"sdb_store_scan(HOST) = %d; expected: 0", check);
"expected: 1", (int)i);
i = 0;
- check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL,
+ check = sdb_store_scan(store, SDB_HOST, /* m, filter = */ NULL, NULL,
scan_error, &i);
fail_unless(check == -1,
"sdb_store_scan(HOST), error callback = %d; expected: -1", check);
"(callback returned error); expected: 1", (int)i);
i = 0;
- check = sdb_store_scan(SDB_SERVICE, /* m, filter = */ NULL, NULL,
+ check = sdb_store_scan(store, SDB_SERVICE, /* m, filter = */ NULL, NULL,
scan_count, &i);
fail_unless(check == 0,
"sdb_store_scan(SERVICE) = %d; expected: 0", check);
"expected: 2", (int)i);
i = 0;
- check = sdb_store_scan(SDB_METRIC, /* m, filter = */ NULL, NULL,
+ check = sdb_store_scan(store, SDB_METRIC, /* m, filter = */ NULL, NULL,
scan_count, &i);
fail_unless(check == 0,
"sdb_store_scan(METRIC) = %d; expected: 0", check);
TEST_MAIN("core::store")
{
TCase *tc = tcase_create("core");
+ tcase_add_unchecked_fixture(tc, init, turndown);
tcase_add_test(tc, test_store_host);
tcase_add_test(tc, test_store_get_host);
tcase_add_test(tc, test_store_attr);
tcase_add_test(tc, test_get_child);
tcase_add_test(tc, test_interval);
tcase_add_test(tc, test_scan);
- tcase_add_unchecked_fixture(tc, init, sdb_store_clear);
ADD_TCASE(tc);
}
TEST_MAIN_END