Code

store: Added quaryable field ‘name’.
authorSebastian Harl <sh@tokkee.org>
Tue, 7 Oct 2014 21:28:33 +0000 (23:28 +0200)
committerSebastian Harl <sh@tokkee.org>
Tue, 7 Oct 2014 21:28:33 +0000 (23:28 +0200)
src/core/store.c
src/core/store_lookup.c
src/include/core/store.h
t/unit/core/store_lookup_test.c
t/unit/core/store_test.c

index 8cb30efaea88a250284cb569db2ae07b05d7fa12..f4f2b58f0d700c0df8967aafddd6429261732af8 100644 (file)
@@ -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;
index 302736754ed8434421596b0801d08cc5ad33f27e..6aa4095bc5de2370f965e88500bc01f8c817cc8d 100644 (file)
@@ -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;
index d5a76941c06255085215a77215c2cd17b0b89f3c..90416a2cc0699cc3df67d50ac516a1d415a9f058 100644 (file)
@@ -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")
index e5a0a74c4becffff53d479a174d60daf97bf2b4d..3b0e956e327404f11c42e96d8391dce60bb08b3b 100644 (file)
@@ -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, <obj>, NULL) = %d; "
+                                       "sdb_store_matcher_matches(%s, <host '%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 },
index 796c50cb1eddda2c6b030ccfc50ed73b23e3c83c..00e3c64389d3843321bb2fcbb396f8174fb0ed68 100644 (file)
@@ -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, <value>) = %d; "
                        "expected: <0");
 
+       check = sdb_store_get_field(host, SDB_FIELD_NAME, &value);
+       fail_unless(check == 0,
+                       "sdb_store_get_field(<host>, SDB_FIELD_NAME, <value>) = "
+                       "%d; expected: 0");
+       fail_unless((value.type == SDB_TYPE_STRING)
+                       && (! strcmp(value.data.string, "host")),
+                       "sdb_store_get_field(<host>, SDB_FIELD_NAME, <value>) "
+                       "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(<host>, SDB_FIELD_LAST_UPDATE, <value>) = "