From: Sebastian Harl Date: Mon, 28 Jul 2014 19:16:14 +0000 (+0200) Subject: store, frontend: Let lookups support arbitrary expressions for comparison. X-Git-Tag: sysdb-0.3.0~23 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=a077fb06b0cf27ca51477c76d5520d0c814af8df;p=sysdb.git store, frontend: Let lookups support arbitrary expressions for comparison. … rather than simple values. When comparing object names, the expression has to evaluate to a (constant) string but conditional attribute comparison supports arbitrary expressions. Expressions are only evaluated when using them during a comparison even though we only support constant expressions so far. This will be optimized in the future. --- diff --git a/src/core/store-private.h b/src/core/store-private.h index 7479bc6..30091a7 100644 --- a/src/core/store-private.h +++ b/src/core/store-private.h @@ -106,7 +106,7 @@ struct sdb_store_cond { typedef struct { sdb_store_cond_t super; char *name; - sdb_data_t value; + sdb_store_expr_t *expr; } attr_cond_t; #define ATTR_C(obj) ((attr_cond_t *)(obj)) diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index 8aa8ab0..758ddd0 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -102,13 +102,21 @@ static int attr_cmp(sdb_host_t *host, sdb_store_cond_t *cond) { sdb_attribute_t *attr; + sdb_data_t value = SDB_DATA_INIT; + int status; + + if (sdb_store_expr_eval(ATTR_C(cond)->expr, &value)) + return INT_MAX; attr = attr_get(host, 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); + status = INT_MAX; + else if (attr->value.type != value.type) + status = INT_MAX; + else + status = sdb_data_cmp(&attr->value, &value); + sdb_data_free_datum(&value); + return status; } /* attr_cmp */ /* @@ -288,7 +296,7 @@ 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 *); + sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *); if (! name) return -1; @@ -298,8 +306,8 @@ attr_cond_init(sdb_object_t *obj, va_list ap) ATTR_C(obj)->name = strdup(name); if (! ATTR_C(obj)->name) return -1; - if (sdb_data_copy(&ATTR_C(obj)->value, value)) - return -1; + ATTR_C(obj)->expr = expr; + sdb_object_ref(SDB_OBJ(expr)); return 0; } /* attr_cond_init */ @@ -308,7 +316,7 @@ 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); + sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr)); } /* attr_cond_destroy */ static sdb_type_t attr_cond_type = { @@ -457,12 +465,17 @@ 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"); + sdb_data_t value = SDB_DATA_INIT; + char value_str[buflen]; + if (sdb_store_expr_eval(ATTR_C(COND_M(m)->cond)->expr, &value)) + snprintf(value_str, sizeof(value_str), "ERR"); + else if (sdb_data_format(&value, value_str, sizeof(value_str), + SDB_SINGLE_QUOTED) < 0) + snprintf(value_str, sizeof(value_str), "ERR"); snprintf(buf, buflen, "ATTR[%s]{ %s %s }", - ATTR_C(COND_M(m)->cond)->name, MATCHER_SYM(m->type), value); + ATTR_C(COND_M(m)->cond)->name, MATCHER_SYM(m->type), + value_str); + sdb_data_free_datum(&value); } return buf; } /* cond_tostring */ @@ -644,10 +657,10 @@ matchers_tostring[] = { */ sdb_store_cond_t * -sdb_store_attr_cond(const char *name, const sdb_data_t *value) +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, value)); + name, expr)); } /* sdb_store_attr_cond */ sdb_store_matcher_t * @@ -656,11 +669,9 @@ sdb_store_name_matcher(int type, const char *name, _Bool re) sdb_store_matcher_t *m; if (re) - m = M(sdb_object_create("name-matcher", name_type, - NULL, name)); + m = M(sdb_object_create("name-matcher", name_type, NULL, name)); else - m = M(sdb_object_create("name-matcher", name_type, - name, NULL)); + m = M(sdb_object_create("name-matcher", name_type, name, NULL)); if (! m) return NULL; @@ -672,14 +683,18 @@ 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_matcher_t *m; + if (! name) return NULL; if (re) - return M(sdb_object_create("attr-matcher", attr_type, + m = M(sdb_object_create("attr-matcher", attr_type, name, NULL, value)); - return M(sdb_object_create("attr-matcher", attr_type, - name, value, NULL)); + else + m = M(sdb_object_create("attr-matcher", attr_type, + name, value, NULL)); + return m; } /* sdb_store_attr_matcher */ sdb_store_matcher_t * @@ -725,7 +740,7 @@ sdb_store_isnull_matcher(const char *attr_name) } /* sdb_store_isnull_matcher */ static sdb_store_matcher_t * -parse_attr_cmp(const char *attr, const char *op, const sdb_data_t *value) +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; @@ -736,12 +751,12 @@ parse_attr_cmp(const char *attr, const char *op, const sdb_data_t *value) return NULL; if (! strcasecmp(op, "IS")) { - if (! value) + if (! expr) return sdb_store_isnull_matcher(attr); else return NULL; } - else if (! value) + else if (! expr) return NULL; else if (! strcasecmp(op, "<")) matcher = sdb_store_lt_matcher; @@ -760,7 +775,7 @@ parse_attr_cmp(const char *attr, const char *op, const sdb_data_t *value) else return NULL; - cond = sdb_store_attr_cond(attr, value); + cond = sdb_store_attr_cond(attr, expr); if (! cond) return NULL; @@ -782,12 +797,13 @@ parse_attr_cmp(const char *attr, const char *op, const sdb_data_t *value) sdb_store_matcher_t * sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, - const char *op, const sdb_data_t *value) + const char *op, sdb_store_expr_t *expr) { int type = -1; _Bool inv = 0; _Bool re = 0; + sdb_data_t value = SDB_DATA_INIT; sdb_store_matcher_t *m = NULL; if (! strcasecmp(obj_type, "host")) @@ -814,23 +830,26 @@ sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, re = 1; } else if (type == SDB_ATTRIBUTE) - return parse_attr_cmp(attr, op, value); + return parse_attr_cmp(attr, op, expr); else return NULL; - if (! value) + if (! expr) return NULL; - if (value->type != SDB_TYPE_STRING) { - if (type == SDB_ATTRIBUTE) - return parse_attr_cmp(attr, op, value); + if (sdb_store_expr_eval(expr, &value)) return NULL; + if (value.type != SDB_TYPE_STRING) { + sdb_data_free_datum(&value); + return parse_attr_cmp(attr, op, expr); } if (! attr) - m = sdb_store_name_matcher(type, value->data.string, re); + 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_attr_matcher(attr, value.data.string, re); + + sdb_data_free_datum(&value); if (! m) return NULL; diff --git a/src/frontend/grammar.y b/src/frontend/grammar.y index 0afbd9e..ed4de83 100644 --- a/src/frontend/grammar.y +++ b/src/frontend/grammar.y @@ -311,36 +311,17 @@ matcher: compare_matcher: IDENTIFIER op expression { - sdb_data_t value = SDB_DATA_INIT; - if (sdb_store_expr_eval($3, &value)) { - sdb_object_deref(SDB_OBJ($3)); - free($1); $1 = NULL; - sdb_fe_yyerror(&yylloc, scanner, - YY_("syntax error, failed to evaluate expression")); - YYABORT; - } - sdb_object_deref(SDB_OBJ($3)); - $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, &value); + $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, $3); free($1); $1 = NULL; - sdb_data_free_datum(&value); + sdb_object_deref(SDB_OBJ($3)); } | IDENTIFIER '.' IDENTIFIER op expression { - sdb_data_t value = SDB_DATA_INIT; - if (sdb_store_expr_eval($5, &value)) { - sdb_object_deref(SDB_OBJ($5)); - free($1); $1 = NULL; - free($3); $3 = NULL; - sdb_fe_yyerror(&yylloc, scanner, - YY_("syntax error, failed to evaluate expression")); - YYABORT; - } - sdb_object_deref(SDB_OBJ($5)); - $$ = sdb_store_matcher_parse_cmp($1, $3, $4, &value); + $$ = sdb_store_matcher_parse_cmp($1, $3, $4, $5); free($1); $1 = NULL; free($3); $3 = NULL; - sdb_data_free_datum(&value); + sdb_object_deref(SDB_OBJ($5)); } | IDENTIFIER '.' IDENTIFIER IS NULL_T diff --git a/src/include/core/store.h b/src/include/core/store.h index 1b4e738..574b01e 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -218,11 +218,11 @@ typedef struct sdb_store_cond sdb_store_cond_t; /* * 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. + * 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, const sdb_data_t *value); +sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr); /* * Store matchers may be used to lookup hosts from the store based on their @@ -283,7 +283,7 @@ sdb_store_gt_matcher(sdb_store_cond_t *cond); /* * sdb_store_matcher_parse_cmp: - * Parse a simple compare expression (. ). + * Parse a simple compare expression (. ). * * Returns: * - a matcher object on success @@ -291,7 +291,7 @@ sdb_store_gt_matcher(sdb_store_cond_t *cond); */ sdb_store_matcher_t * sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, - const char *op, const sdb_data_t *value); + const char *op, sdb_store_expr_t *expr); /* * sdb_store_dis_matcher: diff --git a/t/unit/core/store_lookup_test.c b/t/unit/core/store_lookup_test.c index 43bb1e5..27c03d7 100644 --- a/t/unit/core/store_lookup_test.c +++ b/t/unit/core/store_lookup_test.c @@ -275,6 +275,7 @@ START_TEST(test_store_cond) "sdb_store_get_host(a) = NULL; expected: "); for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { + sdb_store_expr_t *expr; sdb_store_cond_t *c; char buf[1024]; size_t j; @@ -293,10 +294,15 @@ START_TEST(test_store_cond) 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); + expr = sdb_store_expr_constvalue(&golden_data[i].value); + fail_unless(expr != NULL, + "sdb_store_expr_constvalue(%s) = NULL; expected: ", + buf); + + c = sdb_store_attr_cond(golden_data[i].attr, expr); + sdb_object_deref(SDB_OBJ(expr)); fail_unless(c != NULL, - "sdb_store_attr_cond(%s, %s) = NULL; expected: ", + "sdb_store_attr_cond(%s, expr{%s}) = NULL; expected: ", golden_data[i].attr, buf); for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) { @@ -461,19 +467,25 @@ START_TEST(test_parse_cmp) }; for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { + sdb_store_expr_t *expr; char buf[1024]; - check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type, - golden_data[i].attr, golden_data[i].op, - golden_data[i].value); - 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_cmp(golden_data[i].obj_type, + golden_data[i].attr, 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, %s) = %p; " + "sdb_store_matcher_parse_cmp(%s, %s, %s, expr{%s}) = %p; " "expected: NULL", golden_data[i].obj_type, golden_data[i].attr, golden_data[i].op, buf, check); continue;