From 33c80b1c2a05192e6b912558f99d19a4d7f9ef06 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Wed, 22 Oct 2014 10:48:13 +0200 Subject: [PATCH] store: Removed sdb_store_matcher_parse_field_cmp() and sdb_store_obj_cond(). Those functions have been superseded by the new expression-based matchers. --- src/core/store-private.h | 7 --- src/core/store_lookup.c | 108 -------------------------------- src/include/core/store.h | 22 ------- t/unit/core/store_lookup_test.c | 91 --------------------------- t/unit/frontend/parser_test.c | 28 ++++++++- 5 files changed, 25 insertions(+), 231 deletions(-) diff --git a/src/core/store-private.h b/src/core/store-private.h index c6350b3..be262ec 100644 --- a/src/core/store-private.h +++ b/src/core/store-private.h @@ -145,13 +145,6 @@ typedef struct { } attr_cond_t; #define ATTR_C(obj) ((attr_cond_t *)(obj)) -typedef struct { - sdb_store_cond_t super; - int field; - sdb_store_expr_t *expr; -} obj_cond_t; -#define OBJ_C(obj) ((obj_cond_t *)(obj)) - /* * matchers */ diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index 5187a37..5f23cd7 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -128,50 +128,6 @@ attr_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond, return status; } /* attr_cmp */ -static int -obj_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond, - sdb_store_matcher_t *filter) -{ - sdb_data_t obj_value = SDB_DATA_INIT; - sdb_data_t value = SDB_DATA_INIT; - int status; - - if (sdb_store_expr_eval(OBJ_C(cond)->expr, obj, &value, filter)) - return INT_MAX; - - if (OBJ_C(cond)->field == SDB_FIELD_BACKEND) { - /* this implementation is not actually a conditional but rather checks - * for equality (or rather, existence) only */ - size_t i; - - if (value.type != SDB_TYPE_STRING) - return INT_MAX; - - status = INT_MAX; - for (i = 0; i < obj->backends_num; ++i) { - if (! strcasecmp(obj->backends[i], value.data.string)) { - status = 0; - break; - } - } - sdb_data_free_datum(&value); - return status; - } - - if (sdb_store_get_field(obj, OBJ_C(cond)->field, &obj_value)) - return INT_MAX; - if (obj_value.type != value.type) { - sdb_data_free_datum(&obj_value); - sdb_data_free_datum(&value); - return INT_MAX; - } - - status = sdb_data_cmp(&obj_value, &value); - sdb_data_free_datum(&obj_value); - sdb_data_free_datum(&value); - return status; -} /* obj_cmp */ - /* * matcher implementations */ @@ -642,32 +598,6 @@ static sdb_type_t attr_cond_type = { /* destroy = */ attr_cond_destroy, }; -static int -obj_cond_init(sdb_object_t *obj, va_list ap) -{ - int field = va_arg(ap, int); - sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *); - - SDB_STORE_COND(obj)->cmp = obj_cmp; - - OBJ_C(obj)->field = field; - OBJ_C(obj)->expr = expr; - sdb_object_ref(SDB_OBJ(expr)); - return 0; -} /* obj_cond_init */ - -static void -obj_cond_destroy(sdb_object_t *obj) -{ - sdb_object_deref(SDB_OBJ(OBJ_C(obj)->expr)); -} /* obj_cond_destroy */ - -static sdb_type_t obj_cond_type = { - /* size = */ sizeof(obj_cond_t), - /* init = */ obj_cond_init, - /* destroy = */ obj_cond_destroy, -}; - /* * private matcher types */ @@ -935,13 +865,6 @@ sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr) name, expr)); } /* sdb_store_attr_cond */ -sdb_store_cond_t * -sdb_store_obj_cond(int field, sdb_store_expr_t *expr) -{ - return SDB_STORE_COND(sdb_object_create("obj-cond", obj_cond_type, - field, expr)); -} /* sdb_store_obj_cond */ - sdb_store_matcher_t * sdb_store_name_matcher(int type, const char *name, _Bool re) { @@ -1297,37 +1220,6 @@ sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, return maybe_inv_matcher(m, inv); } /* sdb_store_matcher_parse_cmp */ -sdb_store_matcher_t * -sdb_store_matcher_parse_field_cmp(const char *name, 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; - - int field; - - if (! expr) - return NULL; - - field = sdb_store_parse_field_name(name); - if (field < 0) - return NULL; - - if (parse_cond_op(op, &matcher, &inv)) - return NULL; - cond = sdb_store_obj_cond(field, expr); - if (! cond) - return NULL; - - assert(matcher); - 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); -} /* sdb_store_matcher_parse_field_cmp */ - sdb_store_matcher_t * sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right) { diff --git a/src/include/core/store.h b/src/include/core/store.h index 2624194..4e9e32c 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -378,15 +378,6 @@ typedef struct sdb_store_cond sdb_store_cond_t; sdb_store_cond_t * sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr); -/* - * sdb_store_obj_cond: - * Creates a conditional based on queryable object fields. The respective - * field of *any* object type is compared against the value the expression - * evaluates to. - */ -sdb_store_cond_t * -sdb_store_obj_cond(int field, sdb_store_expr_t *expr); - /* * sdb_store_name_matcher: * Creates a matcher matching by the specified object type's name. If 're' is @@ -540,19 +531,6 @@ sdb_store_matcher_t * sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, const char *op, sdb_store_expr_t *expr); -/* - * sdb_store_matcher_parse_field_cmp: - * Parse a simple compare expression for queryable object fields ( - * ). - * - * Returns: - * - a matcher object on success - * - NULL else - */ -sdb_store_matcher_t * -sdb_store_matcher_parse_field_cmp(const char *name, const char *op, - sdb_store_expr_t *expr); - /* * sdb_store_dis_matcher: * Creates a matcher matching the disjunction (logical OR) of two matchers. diff --git a/t/unit/core/store_lookup_test.c b/t/unit/core/store_lookup_test.c index ed27312..805de03 100644 --- a/t/unit/core/store_lookup_test.c +++ b/t/unit/core/store_lookup_test.c @@ -675,96 +675,6 @@ START_TEST(test_parse_cmp) } END_TEST -START_TEST(test_parse_field_cmp) -{ - sdb_data_t datetime = { SDB_TYPE_DATETIME, { .datetime = 1 } }; - sdb_data_t string = { SDB_TYPE_STRING, { .string = "s" } }; - - struct { - const char *field; - const char *op; - 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 }, - { "last_update", ">=", &datetime, MATCHER_GE }, - { "last_update", ">", &datetime, MATCHER_GT }, - { "last_update", "!=", &datetime, MATCHER_NOT }, - { "age", "<", &datetime, MATCHER_LT }, - { "age", "<=", &datetime, MATCHER_LE }, - { "age", "=", &datetime, MATCHER_EQ }, - { "age", ">=", &datetime, MATCHER_GE }, - { "age", ">", &datetime, MATCHER_GT }, - { "age", "!=", &datetime, MATCHER_NOT }, - { "interval", "<", &datetime, MATCHER_LT }, - { "interval", "<=", &datetime, MATCHER_LE }, - { "interval", "=", &datetime, MATCHER_EQ }, - { "interval", ">=", &datetime, MATCHER_GE }, - { "interval", ">", &datetime, MATCHER_GT }, - { "interval", "!=", &datetime, MATCHER_NOT }, - { "backend", "=", &string, MATCHER_EQ }, - { "backend", "!=", &string, MATCHER_NOT }, - /* the behavior for other operators on .backend - * is currently unspecified */ - { "last_update", "=", NULL, -1 }, - { "last_update", "IS", NULL, -1 }, - { "age", "=", NULL, -1 }, - { "interval", "=", NULL, -1 }, - { "backend", "=", NULL, -1 }, - { "backend", "=~", &string, -1 }, - }; - - size_t i; - - for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { - sdb_store_matcher_t *check; - sdb_store_expr_t *expr; - char buf[1024]; - - if (sdb_data_format(golden_data[i].value, - buf, sizeof(buf), SDB_UNQUOTED) < 0) - snprintf(buf, sizeof(buf), "ERR"); - - expr = sdb_store_expr_constvalue(golden_data[i].value); - fail_unless(expr != NULL || golden_data[i].value == NULL, - "sdb_store_expr_constvalue(%s) = NULL; expected: ", - buf); - - check = sdb_store_matcher_parse_field_cmp(golden_data[i].field, - 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_field_cmp(%s, %s, expr{%s}) = %p; " - "expected: NULL", golden_data[i].field, - golden_data[i].op, buf, check); - continue; - } - - fail_unless(check != NULL, - "sdb_store_matcher_parse_field_cmp(%s, %s, %s) = %p; " - "expected: NULL", golden_data[i].field, - golden_data[i].op, buf, check); - fail_unless(M(check)->type == golden_data[i].expected, - "sdb_store_matcher_parse_field_cmp(%s, %s, %s) returned " - "matcher of type %d; expected: %d", golden_data[i].field, - golden_data[i].op, buf, M(check)->type, - golden_data[i].expected); - - sdb_object_deref(SDB_OBJ(check)); - } -} -END_TEST - static int scan_cb(sdb_store_obj_t *obj, void *user_data) { @@ -887,7 +797,6 @@ core_store_lookup_suite(void) tcase_add_test(tc, test_cmp_obj); tcase_add_test(tc, test_store_match_op); tcase_add_test(tc, test_parse_cmp); - tcase_add_test(tc, test_parse_field_cmp); tcase_add_test(tc, test_scan); suite_add_tcase(s, tc); diff --git a/t/unit/frontend/parser_test.c b/t/unit/frontend/parser_test.c index bcf00b2..0112da6 100644 --- a/t/unit/frontend/parser_test.c +++ b/t/unit/frontend/parser_test.c @@ -355,13 +355,35 @@ START_TEST(test_parse_matcher) { "attribute['foo'] IS NOT NULL", -1, MATCHER_ISNNULL }, /* object field matchers */ - { ".last_update < 10s", -1, MATCHER_CMP_LT }, - { ".AGE <= 1m", -1, MATCHER_CMP_LE }, - { ".interval = 10h", -1, MATCHER_CMP_EQ }, + { ".name < 'a'", -1, MATCHER_CMP_LT }, + { ".name <= 'a'", -1, MATCHER_CMP_LE }, + { ".name = 'a'", -1, MATCHER_CMP_EQ }, + { ".name != 'a'", -1, MATCHER_CMP_NE }, + { ".name >= 'a'", -1, MATCHER_CMP_GE }, + { ".name > 'a'", -1, MATCHER_CMP_GT }, + { ".last_update < 2014-10-01", -1, MATCHER_CMP_LT }, + { ".last_update <= 2014-10-01", -1, MATCHER_CMP_LE }, + { ".last_update = 2014-10-01", -1, MATCHER_CMP_EQ }, + { ".last_update != 2014-10-01", -1, MATCHER_CMP_NE }, + { ".last_update >= 2014-10-01", -1, MATCHER_CMP_GE }, + { ".last_update > 2014-10-01", -1, MATCHER_CMP_GT }, { ".Last_Update >= 24D", -1, MATCHER_CMP_GE }, + { ".age < 20s", -1, MATCHER_CMP_LT }, + { ".age <= 20s", -1, MATCHER_CMP_LE }, + { ".age = 20s", -1, MATCHER_CMP_EQ }, + { ".age != 20s", -1, MATCHER_CMP_NE }, + { ".age >= 20s", -1, MATCHER_CMP_GE }, + { ".age > 20s", -1, MATCHER_CMP_GT }, + { ".AGE <= 1m", -1, MATCHER_CMP_LE }, { ".age > 1M", -1, MATCHER_CMP_GT }, { ".age != 20Y", -1, MATCHER_CMP_NE }, { ".age <= 2 * .interval", -1, MATCHER_CMP_LE }, + { ".interval < 20s", -1, MATCHER_CMP_LT }, + { ".interval <= 20s", -1, MATCHER_CMP_LE }, + { ".interval = 20s", -1, MATCHER_CMP_EQ }, + { ".interval != 20s", -1, MATCHER_CMP_NE }, + { ".interval >= 20s", -1, MATCHER_CMP_GE }, + { ".interval > 20s", -1, MATCHER_CMP_GT }, { "'be' IN .backend", -1, MATCHER_IN }, /* check operator precedence */ -- 2.30.2