From: Sebastian Harl Date: Mon, 23 Jun 2014 04:39:19 +0000 (+0200) Subject: store_lookup: Introduce conditional based attribute matchers. X-Git-Tag: sysdb-0.2.0~41 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=fd3e376f4c7dc24c40717c056548c4e26e4e8b97;p=sysdb.git store_lookup: Introduce conditional based attribute matchers. This allows using less-than, less-equal, equal, greater-equal, greater-than operators on attributes. --- diff --git a/src/core/store-private.h b/src/core/store-private.h index 0db46f8..49ddaf5 100644 --- a/src/core/store-private.h +++ b/src/core/store-private.h @@ -77,6 +77,25 @@ typedef struct { #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 */ @@ -89,8 +108,26 @@ enum { 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; @@ -143,6 +180,13 @@ typedef struct { } 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 diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index fa7a8da..100942a 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -47,6 +47,8 @@ #include #include +#include + /* * private data types */ @@ -92,6 +94,23 @@ attr_get(sdb_store_base_t *host, const char *name) 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 */ @@ -190,6 +209,51 @@ match_attr(sdb_store_matcher_t *m, sdb_store_base_t *obj) 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; @@ -200,6 +264,48 @@ static matcher_cb matchers[] = { 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, }; /* @@ -316,6 +422,42 @@ attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen) 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) { @@ -412,6 +554,12 @@ static sdb_type_t attr_type = { /* 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, @@ -434,12 +582,24 @@ static matcher_tostring_cb matchers_tostring[] = { 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) { @@ -472,6 +632,41 @@ sdb_store_attr_matcher(const char *name, const char *value, _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) diff --git a/src/include/core/store.h b/src/include/core/store.h index 81745fa..ecaf21d 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -141,6 +141,26 @@ int 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 @@ -172,6 +192,25 @@ sdb_store_name_matcher(int type, const char *name, _Bool re); 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 (. ). diff --git a/t/unit/core/store_lookup_test.c b/t/unit/core/store_lookup_test.c index 5c3dc48..c125a59 100644 --- a/t/unit/core/store_lookup_test.c +++ b/t/unit/core/store_lookup_test.c @@ -54,6 +54,7 @@ populate(void) sdb_data_t value; } attrs[] = { { "a", "k1", { SDB_TYPE_STRING, { .string = "v1" } } }, + { "a", "k2", { SDB_TYPE_INTEGER, { .integer = 123 } } }, }; size_t i; @@ -117,11 +118,12 @@ START_TEST(test_store_match_name) { 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; @@ -180,8 +182,8 @@ START_TEST(test_store_match_attr) { "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 }, @@ -193,8 +195,8 @@ START_TEST(test_store_match_attr) { "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; @@ -243,6 +245,76 @@ START_TEST(test_store_match_attr) } 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: "); + + 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: ", + 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; @@ -480,6 +552,7 @@ core_store_lookup_suite(void) 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);