From d64762ef1ee24d605bcd32495daeef29d3dc3b3c Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Wed, 22 Oct 2014 11:09:30 +0200 Subject: [PATCH] store: Removed now unused support for attribute matchers in old code. --- src/core/store_lookup.c | 137 +------------------------------- src/frontend/grammar.y | 2 +- src/include/core/store.h | 13 +-- t/unit/core/store_lookup_test.c | 121 +++++++++++----------------- 4 files changed, 53 insertions(+), 220 deletions(-) diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index 5f23cd7..6730e11 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -99,35 +99,6 @@ attr_get(sdb_host_t *host, const char *name, sdb_store_matcher_t *filter) return attr; } /* attr_get */ -/* - * conditional implementations - */ - -static int -attr_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond, - sdb_store_matcher_t *filter) -{ - sdb_attribute_t *attr; - sdb_data_t value = SDB_DATA_INIT; - int status; - - if (obj->type != SDB_HOST) - return INT_MAX; - - if (sdb_store_expr_eval(ATTR_C(cond)->expr, obj, &value, filter)) - return INT_MAX; - - attr = attr_get(HOST(obj), ATTR_C(cond)->name, filter); - if (! attr) - status = INT_MAX; - else if (attr->value.type != value.type) - status = sdb_data_strcmp(&attr->value, &value); - else - status = sdb_data_cmp(&attr->value, &value); - sdb_data_free_datum(&value); - return status; -} /* attr_cmp */ - /* * matcher implementations */ @@ -561,43 +532,6 @@ matchers[] = { match_isnull, }; -/* - * private conditional types - */ - -static int -attr_cond_init(sdb_object_t *obj, va_list ap) -{ - const char *name = va_arg(ap, const char *); - sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_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; - ATTR_C(obj)->expr = expr; - sdb_object_ref(SDB_OBJ(expr)); - 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_object_deref(SDB_OBJ(ATTR_C(obj)->expr)); -} /* attr_cond_destroy */ - -static sdb_type_t attr_cond_type = { - /* size = */ sizeof(attr_cond_t), - /* init = */ attr_cond_init, - /* destroy = */ attr_cond_destroy, -}; - /* * private matcher types */ @@ -858,13 +792,6 @@ static sdb_type_t isnull_type = { * public API */ -sdb_store_cond_t * -sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr) -{ - return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type, - name, expr)); -} /* sdb_store_attr_cond */ - sdb_store_matcher_t * sdb_store_name_matcher(int type, const char *name, _Bool re) { @@ -1109,58 +1036,8 @@ maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv) return tmp; } /* maybe_inv_matcher */ -static int -parse_cond_op(const char *op, - sdb_store_matcher_t *(**matcher)(sdb_store_cond_t *), _Bool *inv) -{ - *inv = 0; - if (! strcasecmp(op, "<")) - *matcher = sdb_store_lt_matcher; - else if (! strcasecmp(op, "<=")) - *matcher = sdb_store_le_matcher; - else if (! strcasecmp(op, "=")) - *matcher = sdb_store_eq_matcher; - else if (! strcasecmp(op, ">=")) - *matcher = sdb_store_ge_matcher; - else if (! strcasecmp(op, ">")) - *matcher = sdb_store_gt_matcher; - else if (! strcasecmp(op, "!=")) { - *matcher = sdb_store_eq_matcher; - *inv = 1; - } - else - return -1; - return 0; -} /* parse_cond_op */ - -static sdb_store_matcher_t * -parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr) -{ - sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL; - sdb_store_matcher_t *m; - sdb_store_cond_t *cond; - _Bool inv = 0; - - if (! attr) - return NULL; - - if (! expr) - return NULL; - else if (parse_cond_op(op, &matcher, &inv)) - return NULL; - - cond = sdb_store_attr_cond(attr, expr); - if (! cond) - return NULL; - - m = matcher(cond); - /* pass ownership to 'm' or destroy in case of an error */ - sdb_object_deref(SDB_OBJ(cond)); - return maybe_inv_matcher(m, inv); -} /* parse_attr_cmp */ - sdb_store_matcher_t * -sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, +sdb_store_matcher_parse_cmp(const char *obj_type, const char *op, sdb_store_expr_t *expr) { int type = -1; @@ -1195,8 +1072,6 @@ sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, inv = 1; re = 1; } - else if (type == SDB_ATTRIBUTE) - return parse_attr_cmp(attr, op, expr); else return NULL; @@ -1206,16 +1081,10 @@ sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL) || (value.type != SDB_TYPE_STRING)) { sdb_data_free_datum(&value); - if (type != SDB_ATTRIBUTE) - return NULL; - return parse_attr_cmp(attr, op, expr); + return NULL; } - if (! attr) - m = sdb_store_name_matcher(type, value.data.string, re); - else if (type == SDB_ATTRIBUTE) - m = sdb_store_attr_matcher(attr, value.data.string, re); - + m = sdb_store_name_matcher(type, value.data.string, re); sdb_data_free_datum(&value); return maybe_inv_matcher(m, inv); } /* sdb_store_matcher_parse_cmp */ diff --git a/src/frontend/grammar.y b/src/frontend/grammar.y index 376c84a..722796c 100644 --- a/src/frontend/grammar.y +++ b/src/frontend/grammar.y @@ -445,7 +445,7 @@ compare_matcher: | IDENTIFIER cmp expression { - $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, $3); + $$ = sdb_store_matcher_parse_cmp($1, $2, $3); free($1); $1 = NULL; sdb_object_deref(SDB_OBJ($3)); } diff --git a/src/include/core/store.h b/src/include/core/store.h index 4e9e32c..684a3cc 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -369,15 +369,6 @@ 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 value the expression evaluates to. See - * sdb_data_cmp for details about the comparison. - */ -sdb_store_cond_t * -sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr); - /* * sdb_store_name_matcher: * Creates a matcher matching by the specified object type's name. If 're' is @@ -521,14 +512,14 @@ sdb_store_parse_field_name(const char *name); /* * sdb_store_matcher_parse_cmp: - * Parse a simple compare expression (. ). + * Parse a simple compare expression ( ). * * Returns: * - a matcher object on success * - NULL else */ sdb_store_matcher_t * -sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, +sdb_store_matcher_parse_cmp(const char *obj_type, const char *op, sdb_store_expr_t *expr); /* diff --git a/t/unit/core/store_lookup_test.c b/t/unit/core/store_lookup_test.c index 6edc453..38162d2 100644 --- a/t/unit/core/store_lookup_test.c +++ b/t/unit/core/store_lookup_test.c @@ -557,7 +557,6 @@ START_TEST(test_parse_cmp) sdb_data_t metricname = { SDB_TYPE_STRING, { .string = "metricname" } }; sdb_data_t srvname = { SDB_TYPE_STRING, { .string = "srvname" } }; sdb_data_t attrname = { SDB_TYPE_STRING, { .string = "attrname" } }; - sdb_data_t attrvalue = { SDB_TYPE_INTEGER, { .integer = 4711 } }; sdb_store_matcher_t *check; @@ -565,75 +564,50 @@ START_TEST(test_parse_cmp) struct { const char *obj_type; - const char *attr; const char *op; const sdb_data_t *value; int expected; } golden_data[] = { - { "host", NULL, "=", &hostname, MATCHER_NAME }, - { "host", NULL, "!=", &hostname, MATCHER_NOT }, - { "host", NULL, "=~", &hostname, MATCHER_NAME }, - { "host", NULL, "!~", &hostname, MATCHER_NOT }, - { "host", "attr", "=", &hostname, -1 }, - { "host", "attr", "!=", &hostname, -1 }, - { "host", "attr", "!=", &attrvalue, -1 }, - { "host", "attr", "<=", &attrvalue, -1 }, - { "host", NULL, "&^", &hostname, -1 }, - { "host", NULL, "<", &hostname, -1 }, - { "host", NULL, "<=", &hostname, -1 }, - { "host", NULL, ">=", &hostname, -1 }, - { "host", NULL, ">", &hostname, -1 }, - { "host", NULL, "=", NULL, -1 }, - { "metric", NULL, "=", &metricname, MATCHER_NAME }, - { "metric", NULL, "!=", &metricname, MATCHER_NOT }, - { "metric", NULL, "=~", &metricname, MATCHER_NAME }, - { "metric", NULL, "!~", &metricname, MATCHER_NOT }, - { "metric", "attr", "=", &metricname, -1 }, - { "metric", "attr", "!=", &metricname, -1 }, - { "metric", "attr", "!=", &attrvalue, -1 }, - { "metric", "attr", "<=", &attrvalue, -1 }, - { "metric", NULL, "&^", &metricname, -1 }, - { "metric", NULL, "<", &metricname, -1 }, - { "metric", NULL, "<=", &metricname, -1 }, - { "metric", NULL, ">=", &metricname, -1 }, - { "metric", NULL, ">", &metricname, -1 }, - { "metric", NULL, "=", NULL, -1 }, - { "service", NULL, "=", &srvname, MATCHER_NAME }, - { "service", NULL, "!=", &srvname, MATCHER_NOT }, - { "service", NULL, "=~", &srvname, MATCHER_NAME }, - { "service", NULL, "!~", &srvname, MATCHER_NOT }, - { "service", "attr", "=", &srvname, -1 }, - { "service", "attr", "!=", &srvname, -1 }, - { "service", "attr", "!=", &attrvalue, -1 }, - { "service", "attr", "<=", &attrvalue, -1 }, - { "service", NULL, "&^", &srvname, -1 }, - { "service", NULL, "<", &srvname, -1 }, - { "service", NULL, "<=", &srvname, -1 }, - { "service", NULL, ">=", &srvname, -1 }, - { "service", NULL, ">", &srvname, -1 }, - { "service", NULL, "=", NULL, -1 }, - { "attribute", NULL, "=", &attrname, MATCHER_NAME }, - { "attribute", NULL, "!=", &attrname, MATCHER_NOT }, - { "attribute", NULL, "=~", &attrname, MATCHER_NAME }, - { "attribute", NULL, "!~", &attrname, MATCHER_NOT }, - { "attribute", NULL, "<", &attrname, -1 }, - { "attribute", NULL, "<=", &attrname, -1 }, - { "attribute", NULL, ">=", &attrname, -1 }, - { "attribute", NULL, ">", &attrname, -1 }, - { "attribute", NULL, "=", NULL, -1 }, - { "attribute", "attr", "=", &attrname, MATCHER_ATTR }, - { "attribute", "attr", "!=", &attrname, MATCHER_NOT }, - { "attribute", "attr", "=~", &attrname, MATCHER_ATTR }, - { "attribute", "attr", "!~", &attrname, MATCHER_NOT }, - { "attribute", "attr", "&^", &attrname, -1 }, - { "attribute", "attr", "<", NULL, -1 }, - { "attribute", "attr", "<", &attrname, MATCHER_LT }, - { "attribute", "attr", "<=", &attrname, MATCHER_LE }, -/* { "attribute", "attr", "=", &attrname, MATCHER_EQ }, */ - { "attribute", "attr", ">=", &attrname, MATCHER_GE }, - { "attribute", "attr", ">", &attrname, MATCHER_GT }, - { "foo", NULL, "=", &attrname, -1 }, - { "foo", "attr", "=", &attrname, -1 }, + { "host", "=", &hostname, MATCHER_NAME }, + { "host", "!=", &hostname, MATCHER_NOT }, + { "host", "=~", &hostname, MATCHER_NAME }, + { "host", "!~", &hostname, MATCHER_NOT }, + { "host", "&^", &hostname, -1 }, + { "host", "<", &hostname, -1 }, + { "host", "<=", &hostname, -1 }, + { "host", ">=", &hostname, -1 }, + { "host", ">", &hostname, -1 }, + { "host", "=", NULL, -1 }, + { "metric", "=", &metricname, MATCHER_NAME }, + { "metric", "!=", &metricname, MATCHER_NOT }, + { "metric", "=~", &metricname, MATCHER_NAME }, + { "metric", "!~", &metricname, MATCHER_NOT }, + { "metric", "&^", &metricname, -1 }, + { "metric", "<", &metricname, -1 }, + { "metric", "<=", &metricname, -1 }, + { "metric", ">=", &metricname, -1 }, + { "metric", ">", &metricname, -1 }, + { "metric", "=", NULL, -1 }, + { "service", "=", &srvname, MATCHER_NAME }, + { "service", "!=", &srvname, MATCHER_NOT }, + { "service", "=~", &srvname, MATCHER_NAME }, + { "service", "!~", &srvname, MATCHER_NOT }, + { "service", "&^", &srvname, -1 }, + { "service", "<", &srvname, -1 }, + { "service", "<=", &srvname, -1 }, + { "service", ">=", &srvname, -1 }, + { "service", ">", &srvname, -1 }, + { "service", "=", NULL, -1 }, + { "attribute", "=", &attrname, MATCHER_NAME }, + { "attribute", "!=", &attrname, MATCHER_NOT }, + { "attribute", "=~", &attrname, MATCHER_NAME }, + { "attribute", "!~", &attrname, MATCHER_NOT }, + { "attribute", "<", &attrname, -1 }, + { "attribute", "<=", &attrname, -1 }, + { "attribute", ">=", &attrname, -1 }, + { "attribute", ">", &attrname, -1 }, + { "attribute", "=", NULL, -1 }, + { "foo", "=", &attrname, -1 }, }; for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { @@ -650,26 +624,25 @@ START_TEST(test_parse_cmp) buf); check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type, - golden_data[i].attr, golden_data[i].op, expr); + golden_data[i].op, expr); sdb_object_deref(SDB_OBJ(expr)); if (golden_data[i].expected == -1) { fail_unless(check == NULL, - "sdb_store_matcher_parse_cmp(%s, %s, %s, expr{%s}) = %p; " + "sdb_store_matcher_parse_cmp(%s, %s, expr{%s}) = %p; " "expected: NULL", golden_data[i].obj_type, - golden_data[i].attr, golden_data[i].op, buf, check); + golden_data[i].op, buf, check); continue; } fail_unless(check != NULL, - "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; " + "sdb_store_matcher_parse_cmp(%s, %s, %s) = %p; " "expected: ", golden_data[i].obj_type, - golden_data[i].attr, golden_data[i].op, buf, check); + golden_data[i].op, buf, check); fail_unless(M(check)->type == golden_data[i].expected, - "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) returned matcher " + "sdb_store_matcher_parse_cmp(%s, %s, %s) returned matcher " "of type %d; expected: %d", golden_data[i].obj_type, - golden_data[i].attr, golden_data[i].op, buf, - M(check)->type, golden_data[i].expected); + golden_data[i].op, buf, M(check)->type, golden_data[i].expected); sdb_object_deref(SDB_OBJ(check)); } -- 2.30.2