From ff53c1c6b145e1a0315ee42d316a858f29f7d0a1 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Tue, 7 Oct 2014 23:28:33 +0200 Subject: [PATCH] =?utf8?q?store:=20Added=20quaryable=20field=20=E2=80=98na?= =?utf8?q?me=E2=80=99.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/core/store.c | 9 +++++++++ src/core/store_lookup.c | 4 +++- src/include/core/store.h | 12 +++++++----- t/unit/core/store_lookup_test.c | 14 +++++++++++++- t/unit/core/store_test.c | 18 ++++++++++++++++++ 5 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/core/store.c b/src/core/store.c index 8cb30ef..f4f2b58 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -894,10 +894,19 @@ sdb_store_fetch_timeseries(const char *hostname, const char *metric, int sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res) { + sdb_data_t tmp; + if ((! obj) || (! res)) 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; + *res = tmp; + break; case SDB_FIELD_LAST_UPDATE: res->type = SDB_TYPE_DATETIME; res->data.datetime = obj->last_update; diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index 3027367..6aa4095 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -880,7 +880,9 @@ sdb_store_parse_object_type_plural(const char *name) int sdb_store_parse_field_name(const char *name) { - if (! strcasecmp(name, "last_update")) + if (! strcasecmp(name, "name")) + return SDB_FIELD_NAME; + else if (! strcasecmp(name, "last_update")) return SDB_FIELD_LAST_UPDATE; else if (! strcasecmp(name, "age")) return SDB_FIELD_AGE; diff --git a/src/include/core/store.h b/src/include/core/store.h index d5a7694..90416a2 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -68,14 +68,16 @@ typedef struct sdb_store_obj sdb_store_obj_t; * Queryable fields of a stored object. */ enum { - SDB_FIELD_LAST_UPDATE = 1, /* datetime */ - SDB_FIELD_AGE, /* datetime */ - SDB_FIELD_INTERVAL, /* datetime */ - SDB_FIELD_BACKEND, /* string */ + SDB_FIELD_NAME = 1, /* string */ + SDB_FIELD_LAST_UPDATE, /* datetime */ + SDB_FIELD_AGE, /* datetime */ + SDB_FIELD_INTERVAL, /* datetime */ + SDB_FIELD_BACKEND, /* string */ }; #define SDB_FIELD_TO_NAME(f) \ - (((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \ + (((f) == SDB_FIELD_NAME) ? "name" \ + : ((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \ : ((f) == SDB_FIELD_AGE) ? "age" \ : ((f) == SDB_FIELD_INTERVAL) ? "interval" \ : ((f) == SDB_FIELD_BACKEND) ? "backend" : "unknown") diff --git a/t/unit/core/store_lookup_test.c b/t/unit/core/store_lookup_test.c index e5a0a74..3b0e956 100644 --- a/t/unit/core/store_lookup_test.c +++ b/t/unit/core/store_lookup_test.c @@ -371,6 +371,12 @@ START_TEST(test_obj_cond) const sdb_data_t value; int expected_lt, expected_le, expected_eq, expected_ge, expected_gt; } golden_data[] = { + { "b", SDB_FIELD_NAME, + { SDB_TYPE_STRING, { .string = "a" } }, 0, 0, 0, 1, 1 }, + { "b", SDB_FIELD_NAME, + { SDB_TYPE_STRING, { .string = "b" } }, 0, 1, 1, 1, 0 }, + { "b", SDB_FIELD_NAME, + { SDB_TYPE_STRING, { .string = "c" } }, 1, 1, 0, 0, 0 }, /* last-update = 1 for all objects */ { "a", SDB_FIELD_LAST_UPDATE, { SDB_TYPE_DATETIME, { .datetime = 1 } }, 0, 1, 1, 1, 0 }, @@ -453,7 +459,7 @@ START_TEST(test_obj_cond) status = sdb_store_matcher_matches(m, obj, /* filter */ NULL); fail_unless(status == tests[j].expected, - "sdb_store_matcher_matches(%s, , NULL) = %d; " + "sdb_store_matcher_matches(%s, , NULL) = %d; " "expected: %d", sdb_store_matcher_tostring(m, m_str, sizeof(m_str)), status, tests[j].expected); @@ -675,6 +681,12 @@ START_TEST(test_parse_field_cmp) const sdb_data_t *value; int expected; } golden_data[] = { + { "name", "<", &string, MATCHER_LT }, + { "name", "<=", &string, MATCHER_LE }, + { "name", "=", &string, MATCHER_EQ }, + { "name", ">=", &string, MATCHER_GE }, + { "name", ">", &string, MATCHER_GT }, + { "name", "!=", &string, MATCHER_NOT }, { "last_update", "<", &datetime, MATCHER_LT }, { "last_update", "<=", &datetime, MATCHER_LE }, { "last_update", "=", &datetime, MATCHER_EQ }, diff --git a/t/unit/core/store_test.c b/t/unit/core/store_test.c index 796c50c..00e3c64 100644 --- a/t/unit/core/store_test.c +++ b/t/unit/core/store_test.c @@ -604,6 +604,13 @@ START_TEST(test_store_tojson) "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": []}" "]" }, + { { sdb_store_eq_matcher, SDB_FIELD_NAME, + { SDB_TYPE_STRING, { .string = "h1" } } }, 0, + "[" + "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": [], " + "\"attributes\": [], \"metrics\": [], \"services\": []}" + "]" }, { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE, { SDB_TYPE_DATETIME, { .datetime = 1 } } }, 0, "[" @@ -725,6 +732,17 @@ START_TEST(test_get_field) "sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, ) = %d; " "expected: <0"); + check = sdb_store_get_field(host, SDB_FIELD_NAME, &value); + fail_unless(check == 0, + "sdb_store_get_field(, SDB_FIELD_NAME, ) = " + "%d; expected: 0"); + fail_unless((value.type == SDB_TYPE_STRING) + && (! strcmp(value.data.string, "host")), + "sdb_store_get_field(, SDB_FIELD_NAME, ) " + "returned value {%d, %s}; expected {%d, host}", + value.type, value.data.string, SDB_TYPE_STRING); + sdb_data_free_datum(&value); + check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, &value); fail_unless(check == 0, "sdb_store_get_field(, SDB_FIELD_LAST_UPDATE, ) = " -- 2.30.2