summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 31c79a0)
raw | patch | inline | side by side (parent: 31c79a0)
author | Sebastian Harl <sh@tokkee.org> | |
Mon, 23 Jun 2014 04:39:19 +0000 (06:39 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Mon, 23 Jun 2014 04:39:19 +0000 (06:39 +0200) |
This allows using less-than, less-equal, equal, greater-equal, greater-than
operators on attributes.
operators on attributes.
src/core/store-private.h | patch | blob | history | |
src/core/store_lookup.c | patch | blob | history | |
src/include/core/store.h | patch | blob | history | |
t/unit/core/store_lookup_test.c | patch | blob | history |
index 0db46f84b5d6c28393d8a7d49b8e8dd453471335..49ddaf547465f349b8ba57aede974e0388628652 100644 (file)
--- a/src/core/store-private.h
+++ b/src/core/store-private.h
#define _last_update super.last_update
#define _interval super.interval
+/*
+ * conditionals
+ */
+
+/* compares a store object using the specified conditional */
+typedef int (*cmp_cb)(sdb_store_base_t *, sdb_store_cond_t *);
+
+struct sdb_store_cond {
+ sdb_object_t super;
+ cmp_cb cmp;
+};
+
+typedef struct {
+ sdb_store_cond_t super;
+ char *name;
+ sdb_data_t value;
+} attr_cond_t;
+#define ATTR_C(obj) ((attr_cond_t *)(obj))
+
/*
* matchers
*/
MATCHER_NOT,
MATCHER_NAME,
MATCHER_ATTR,
+ MATCHER_LT,
+ MATCHER_LE,
+ MATCHER_EQ,
+ MATCHER_GE,
+ MATCHER_GT,
};
+#define MATCHER_SYM(t) \
+ (((t) == MATCHER_OR) ? "OR" \
+ : ((t) == MATCHER_AND) ? "AND" \
+ : ((t) == MATCHER_NOT) ? "NOT" \
+ : ((t) == MATCHER_NAME) ? "NAME" \
+ : ((t) == MATCHER_ATTR) ? "ATTR" \
+ : ((t) == MATCHER_LT) ? "<" \
+ : ((t) == MATCHER_LE) ? "<=" \
+ : ((t) == MATCHER_EQ) ? "=" \
+ : ((t) == MATCHER_GE) ? ">=" \
+ : ((t) == MATCHER_GT) ? ">" \
+ : "UNKNOWN")
+
/* match the name of something */
typedef struct {
char *name;
} attr_matcher_t;
#define ATTR_M(m) ((attr_matcher_t *)(m))
+/* match using conditionals */
+typedef struct {
+ sdb_store_matcher_t super;
+ sdb_store_cond_t *cond;
+} cond_matcher_t;
+#define COND_M(m) ((cond_matcher_t *)(m))
+
#ifdef __cplusplus
} /* extern "C" */
#endif
index fa7a8dad7d98a1bb35ca28fb1b896131ee1f13a7..100942ac8ab652fc21c9d2691e9517ca28564c81 100644 (file)
--- a/src/core/store_lookup.c
+++ b/src/core/store_lookup.c
#include <stdlib.h>
#include <string.h>
+#include <limits.h>
+
/*
* private data types
*/
return attr;
} /* attr_get */
+/*
+ * conditional implementations
+ */
+
+static int
+attr_cmp(sdb_store_base_t *obj, sdb_store_cond_t *cond)
+{
+ sdb_attribute_t *attr;
+
+ attr = SDB_ATTR(attr_get(obj, ATTR_C(cond)->name));
+ if (! attr)
+ return INT_MAX;
+ if (attr->value.type != ATTR_C(cond)->value.type)
+ return INT_MAX;
+ return sdb_data_cmp(&attr->value, &ATTR_C(cond)->value);
+} /* attr_cmp */
+
/*
* matcher implementations
*/
return 0;
} /* match_attr */
+static int
+match_lt(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+{
+ int status;
+ assert(m->type == MATCHER_LT);
+ status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond);
+ return (status != INT_MAX) && (status < 0);
+} /* match_lt */
+
+static int
+match_le(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+{
+ int status;
+ assert(m->type == MATCHER_LE);
+ status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond);
+ return (status != INT_MAX) && (status <= 0);
+} /* match_le */
+
+static int
+match_eq(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+{
+ int status;
+ assert(m->type == MATCHER_EQ);
+ status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond);
+ return (status != INT_MAX) && (! status);
+} /* match_eq */
+
+static int
+match_ge(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+{
+ int status;
+ assert(m->type == MATCHER_GE);
+ status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond);
+ return (status != INT_MAX) && (status >= 0);
+} /* match_ge */
+
+static int
+match_gt(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+{
+ int status;
+ assert(m->type == MATCHER_GT);
+ status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond);
+ return (status != INT_MAX) && (status > 0);
+} /* match_gt */
+
typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
/* this array needs to be indexable by the matcher types;
match_unary,
match_name,
match_attr,
+ match_lt,
+ match_le,
+ match_eq,
+ match_ge,
+ match_gt,
+};
+
+/*
+ * private conditional types
+ */
+
+static int
+attr_cond_init(sdb_object_t *obj, va_list ap)
+{
+ const char *name = va_arg(ap, const char *);
+ const sdb_data_t *value = va_arg(ap, const sdb_data_t *);
+
+ if (! name)
+ return -1;
+
+ SDB_STORE_COND(obj)->cmp = attr_cmp;
+
+ ATTR_C(obj)->name = strdup(name);
+ if (! ATTR_C(obj)->name)
+ return -1;
+ if (sdb_data_copy(&ATTR_C(obj)->value, value))
+ return -1;
+ return 0;
+} /* attr_cond_init */
+
+static void
+attr_cond_destroy(sdb_object_t *obj)
+{
+ if (ATTR_C(obj)->name)
+ free(ATTR_C(obj)->name);
+ sdb_data_free_datum(&ATTR_C(obj)->value);
+} /* attr_cond_destroy */
+
+static sdb_type_t attr_cond_type = {
+ /* size = */ sizeof(attr_cond_t),
+ /* init = */ attr_cond_init,
+ /* destroy = */ attr_cond_destroy,
};
/*
return buf;
} /* attr_tostring */
+static int
+cond_matcher_init(sdb_object_t *obj, va_list ap)
+{
+ int type = va_arg(ap, int);
+ sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
+
+ if (! cond)
+ return -1;
+
+ sdb_object_ref(SDB_OBJ(cond));
+
+ M(obj)->type = type;
+ COND_M(obj)->cond = cond;
+ return 0;
+} /* cond_matcher_init */
+
+static void
+cond_matcher_destroy(sdb_object_t *obj)
+{
+ sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
+} /* cond_matcher_destroy */
+
+static char *
+cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+{
+ if (COND_M(m)->cond->cmp == attr_cmp) {
+ char value[buflen];
+ if (sdb_data_format(&ATTR_C(COND_M(m)->cond)->value,
+ value, sizeof(value), SDB_SINGLE_QUOTED) < 0)
+ snprintf(value, sizeof(value), "ERR");
+ snprintf(buf, buflen, "ATTR[%s]{ %s %s }",
+ ATTR_C(COND_M(m)->cond)->name, MATCHER_SYM(m->type), value);
+ }
+ return buf;
+} /* cond_tostring */
+
static int
op_matcher_init(sdb_object_t *obj, va_list ap)
{
/* destroy = */ attr_matcher_destroy,
};
+static sdb_type_t cond_type = {
+ /* size = */ sizeof(cond_matcher_t),
+ /* init = */ cond_matcher_init,
+ /* destroy = */ cond_matcher_destroy,
+};
+
static sdb_type_t op_type = {
/* size = */ sizeof(op_matcher_t),
/* init = */ op_matcher_init,
uop_tostring,
name_tostring,
attr_tostring,
+ cond_tostring,
+ cond_tostring,
+ cond_tostring,
+ cond_tostring,
+ cond_tostring,
};
/*
* public API
*/
+sdb_store_cond_t *
+sdb_store_attr_cond(const char *name, const sdb_data_t *value)
+{
+ return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
+ name, value));
+} /* sdb_store_attr_cond */
+
sdb_store_matcher_t *
sdb_store_name_matcher(int type, const char *name, _Bool re)
{
name, value, NULL));
} /* sdb_store_attr_matcher */
+sdb_store_matcher_t *
+sdb_store_lt_matcher(sdb_store_cond_t *cond)
+{
+ return M(sdb_object_create("lt-matcher", cond_type,
+ MATCHER_LT, cond));
+} /* sdb_store_lt_matcher */
+
+sdb_store_matcher_t *
+sdb_store_le_matcher(sdb_store_cond_t *cond)
+{
+ return M(sdb_object_create("le-matcher", cond_type,
+ MATCHER_LE, cond));
+} /* sdb_store_le_matcher */
+
+sdb_store_matcher_t *
+sdb_store_eq_matcher(sdb_store_cond_t *cond)
+{
+ return M(sdb_object_create("eq-matcher", cond_type,
+ MATCHER_EQ, cond));
+} /* sdb_store_eq_matcher */
+
+sdb_store_matcher_t *
+sdb_store_ge_matcher(sdb_store_cond_t *cond)
+{
+ return M(sdb_object_create("ge-matcher", cond_type,
+ MATCHER_GE, cond));
+} /* sdb_store_ge_matcher */
+
+sdb_store_matcher_t *
+sdb_store_gt_matcher(sdb_store_cond_t *cond)
+{
+ return M(sdb_object_create("gt-matcher", cond_type,
+ MATCHER_GT, cond));
+} /* sdb_store_gt_matcher */
+
sdb_store_matcher_t *
sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
const char *op, const char *value)
index 81745fa7269df5e7a218a3e9488bd32aea004857..ecaf21d0bbd6b974483290a94dce38916dd285c4 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
sdb_store_service(const char *hostname, const char *name,
sdb_time_t last_update);
+/*
+ * Conditionals may be used to lookup hosts from the store based on a
+ * conditional expression.
+ *
+ * A conditional object inherits from sdb_object_t and, thus, may safely be
+ * cast to a generic object.
+ */
+struct sdb_store_cond;
+typedef struct sdb_store_cond sdb_store_cond_t;
+#define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
+
+/*
+ * sdb_store_attr_cond:
+ * Creates a conditional based on attribute values. The value of stored
+ * attributes is compared against the specified value. See sdb_data_cmp for
+ * details about the comparison.
+ */
+sdb_store_cond_t *
+sdb_store_attr_cond(const char *name, const sdb_data_t *value);
+
/*
* Store matchers may be used to lookup hosts from the store based on their
* various attributes. Service and attribute matchers are applied to a host's
sdb_store_matcher_t *
sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
+/*
+ * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
+ * sdb_store_ge_matcher, sdb_store_gt_matcher:
+ * Creates a matcher based on a conditional. The matcher matches objects for
+ * which the conditional evaluates the object to compare less than, less or
+ * equal, equal, greater or equal, or greater than the conditional's value
+ * repsectively.
+ */
+sdb_store_matcher_t *
+sdb_store_lt_matcher(sdb_store_cond_t *cond);
+sdb_store_matcher_t *
+sdb_store_le_matcher(sdb_store_cond_t *cond);
+sdb_store_matcher_t *
+sdb_store_eq_matcher(sdb_store_cond_t *cond);
+sdb_store_matcher_t *
+sdb_store_ge_matcher(sdb_store_cond_t *cond);
+sdb_store_matcher_t *
+sdb_store_gt_matcher(sdb_store_cond_t *cond);
+
/*
* sdb_store_matcher_parse_cmp:
* Parse a simple compare expression (<obj_type>.<attr> <op> <value>).
index 5c3dc4866493e2caa0bfe2fc9a2254163aebae5b..c125a5913a407c1e177998864d740d1427933d1d 100644 (file)
sdb_data_t value;
} attrs[] = {
{ "a", "k1", { SDB_TYPE_STRING, { .string = "v1" } } },
+ { "a", "k2", { SDB_TYPE_INTEGER, { .integer = 123 } } },
};
size_t i;
{ SDB_ATTRIBUTE, NULL, 0, 1 },
{ SDB_ATTRIBUTE, NULL, 1, 1 },
{ SDB_ATTRIBUTE, "k1", 0, 1 },
- { SDB_ATTRIBUTE, "k2", 0, 0 },
+ { SDB_ATTRIBUTE, "k2", 0, 1 },
{ SDB_ATTRIBUTE, "k3", 0, 0 },
+ { SDB_ATTRIBUTE, "k4", 0, 0 },
{ SDB_ATTRIBUTE, "k", 1, 1 },
{ SDB_ATTRIBUTE, "1", 1, 1 },
- { SDB_ATTRIBUTE, "2", 1, 0 },
+ { SDB_ATTRIBUTE, "3", 1, 0 },
};
size_t i;
{ "k1", NULL, 0, 1 },
{ "k", NULL, 1, 0 },
{ "1", NULL, 1, 0 },
- { "k2", NULL, 0, 0 },
- { "k2", NULL, 1, 0 },
+ { "k3", NULL, 0, 0 },
+ { "k3", NULL, 1, 0 },
{ "k3", NULL, 0, 0 },
{ "k3", NULL, 1, 0 },
{ "k1", "v1", 0, 1 },
{ "k1", "v2", 1, 0 },
{ "k", "v1", 0, 0 },
{ "k", "v1", 1, 0 },
- { "k2", "1", 0, 0 },
- { "k2", "1", 1, 0 },
+ { "k3", "1", 0, 0 },
+ { "k3", "1", 1, 0 },
};
size_t i;
}
END_TEST
+START_TEST(test_store_cond)
+{
+ sdb_store_base_t *obj;
+
+ struct {
+ const char *attr;
+ const sdb_data_t value;
+ int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
+ } golden_data[] = {
+ { "k1", { SDB_TYPE_STRING, { .string = "v1" } }, 0, 1, 1, 1, 0 },
+ { "k1", { SDB_TYPE_STRING, { .string = "v2" } }, 1, 1, 0, 0, 0 },
+ { "k1", { SDB_TYPE_STRING, { .string = "v0" } }, 0, 0, 0, 1, 1 },
+ { "k2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 0, 1, 1, 1, 0 },
+ { "k2", { SDB_TYPE_INTEGER, { .integer = 124 } }, 1, 1, 0, 0, 0 },
+ { "k2", { SDB_TYPE_INTEGER, { .integer = 122 } }, 0, 0, 0, 1, 1 },
+ /* key does not exist */
+ { "k3", { SDB_TYPE_STRING, { .string = "v1" } }, 0, 0, 0, 0, 0 },
+ { "k3", { SDB_TYPE_STRING, { .string = "123" } }, 0, 0, 0, 0, 0 },
+ { "k3", { SDB_TYPE_INTEGER, { .integer = 123 } }, 0, 0, 0, 0, 0 },
+ /* type mismatch */
+ { "k1", { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
+ { "k2", { SDB_TYPE_STRING, { .string = "123" } }, 0, 0, 0, 0, 0 },
+ };
+
+ int status;
+ size_t i;
+
+ obj = sdb_store_get_host("a");
+ fail_unless(obj != NULL,
+ "sdb_store_get_host(a) = NULL; expected: <host>");
+
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ sdb_store_cond_t *c;
+ char buf[1024];
+ size_t j;
+
+ struct {
+ sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *);
+ int *expected;
+ } tests[] = {
+ { sdb_store_lt_matcher, &golden_data[i].expected_lt },
+ { sdb_store_le_matcher, &golden_data[i].expected_le },
+ { sdb_store_eq_matcher, &golden_data[i].expected_eq },
+ { sdb_store_ge_matcher, &golden_data[i].expected_ge },
+ { sdb_store_gt_matcher, &golden_data[i].expected_gt },
+ };
+
+ sdb_data_format(&golden_data[i].value,
+ buf, sizeof(buf), SDB_UNQUOTED);
+
+ c = sdb_store_attr_cond(golden_data[i].attr,
+ &golden_data[i].value);
+ fail_unless(c != NULL,
+ "sdb_store_attr_cond(%s, %s) = NULL; expected: <cond>",
+ golden_data[i].attr, buf);
+
+ for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
+ sdb_store_matcher_t *m = tests[j].matcher(c);
+ char m_str[1024];
+ sdb_object_deref(SDB_OBJ(c));
+ status = sdb_store_matcher_matches(m, obj);
+ fail_unless(status == *tests[j].expected,
+ "sdb_store_matcher_matches(%s) = %d; expected: %d",
+ sdb_store_matcher_tostring(m, m_str, sizeof(m_str)),
+ status, *tests[j].expected);
+ }
+ }
+}
+END_TEST
+
START_TEST(test_store_match_op)
{
sdb_store_base_t *obj;
tcase_add_checked_fixture(tc, populate, sdb_store_clear);
tcase_add_test(tc, test_store_match_name);
tcase_add_test(tc, test_store_match_attr);
+ tcase_add_test(tc, test_store_cond);
tcase_add_test(tc, test_store_match_op);
tcase_add_test(tc, test_parse_cmp);
tcase_add_test(tc, test_lookup);