summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 0c519f1)
raw | patch | inline | side by side (parent: 0c519f1)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 22 Oct 2014 09:09:30 +0000 (11:09 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 22 Oct 2014 09:09:30 +0000 (11:09 +0200) |
src/core/store_lookup.c | patch | blob | history | |
src/frontend/grammar.y | patch | blob | history | |
src/include/core/store.h | patch | blob | history | |
t/unit/core/store_lookup_test.c | patch | blob | history |
index 5f23cd753d9b4cda959c7111aae2d76fd8c1a34f..6730e11f59eedc18b51837f35f8b0c19bc77b4ba 100644 (file)
--- a/src/core/store_lookup.c
+++ b/src/core/store_lookup.c
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
*/
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
*/
* 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)
{
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;
inv = 1;
re = 1;
}
- else if (type == SDB_ATTRIBUTE)
- return parse_attr_cmp(attr, op, expr);
else
return NULL;
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 376c84afdb06bac4965e16c27b2bbb90662fd0ee..722796c282726e5bc1f257bb6b6974f032064920 100644 (file)
--- a/src/frontend/grammar.y
+++ b/src/frontend/grammar.y
|
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));
}
index 4e9e32ce1d25ec980a15eb16773f6c52f1773daa..684a3ccf020e5d996acf717e7c06bb3a0925ce3e 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
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
/*
* sdb_store_matcher_parse_cmp:
- * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
+ * Parse a simple compare expression (<obj_type> <op> <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);
/*
index 6edc453fcd6ced7218f2b72d43a6464a480a7911..38162d2dae93f223a5d0c0ee066eea0252c41440 100644 (file)
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;
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) {
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: <expr>", 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));
}