summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 73b11c8)
raw | patch | inline | side by side (parent: 73b11c8)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 23 Oct 2014 05:27:30 +0000 (07:27 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 23 Oct 2014 05:27:30 +0000 (07:27 +0200) |
src/core/store-private.h | patch | blob | history | |
src/core/store_lookup.c | patch | blob | history | |
src/include/core/store.h | patch | blob | history | |
t/unit/core/store_lookup_test.c | patch | blob | history |
index 717f89392e4e325478b36c39e7d74a728efc0db3..550668db2ebd1259818411c8a7fe7e769e8012db 100644 (file)
--- a/src/core/store-private.h
+++ b/src/core/store-private.h
MATCHER_AND,
MATCHER_NOT,
MATCHER_NAME,
- MATCHER_ATTR,
MATCHER_SERVICE,
MATCHER_METRIC,
MATCHER_ATTRIBUTE,
: ((t) == MATCHER_AND) ? "AND" \
: ((t) == MATCHER_NOT) ? "NOT" \
: ((t) == MATCHER_NAME) ? "NAME" \
- : ((t) == MATCHER_ATTR) ? "ATTR" \
: ((t) == MATCHER_SERVICE) ? "SERVICE" \
: ((t) == MATCHER_METRIC) ? "METRIC" \
: ((t) == MATCHER_ATTRIBUTE) ? "ATTRIBUTE" \
} name_matcher_t;
#define NAME_M(m) ((name_matcher_t *)(m))
-/* match attributes */
-typedef struct {
- sdb_store_matcher_t super;
- char *name;
- string_matcher_t value;
-} attr_matcher_t;
-#define ATTR_M(m) ((attr_matcher_t *)(m))
-
typedef struct {
sdb_store_matcher_t super;
sdb_store_expr_t *expr;
index 7f9605707576f4c45efbcd71cefab5abb43a584c..e2ae0669f3cdc64300d5a8d49ecc3b032deea49a 100644 (file)
--- a/src/core/store_lookup.c
+++ b/src/core/store_lookup.c
return 0;
} /* scan_iter */
-static sdb_attribute_t *
-attr_get(sdb_host_t *host, const char *name, sdb_store_matcher_t *filter)
-{
- sdb_avltree_iter_t *iter = NULL;
- sdb_attribute_t *attr = NULL;
-
- iter = sdb_avltree_get_iter(host->attributes);
- while (sdb_avltree_iter_has_next(iter)) {
- sdb_attribute_t *a = ATTR(sdb_avltree_iter_get_next(iter));
-
- if (strcasecmp(name, SDB_OBJ(a)->name))
- continue;
-
- assert(STORE_OBJ(a)->type == SDB_ATTRIBUTE);
- attr = a;
- break;
- }
- sdb_avltree_iter_destroy(iter);
-
- if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(attr),
- NULL)))
- return NULL;
- return attr;
-} /* attr_get */
-
/*
* matcher implementations
*/
return status;
} /* match_name */
-static int
-match_attr(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
- sdb_store_matcher_t *filter)
-{
- sdb_attribute_t *attr;
-
- assert(m->type == MATCHER_ATTR);
- assert(ATTR_M(m)->name);
-
- if (obj->type != SDB_HOST)
- return 0;
-
- attr = attr_get(HOST(obj), ATTR_M(m)->name, filter);
- if (attr) {
- char buf[sdb_data_strlen(&attr->value) + 1];
- if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
- return 0;
- if (match_string(&ATTR_M(m)->value, buf))
- return 1;
- }
- return 0;
-} /* match_attr */
-
static int
match_child(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
sdb_store_matcher_t *filter)
match_logical,
match_unary,
match_name,
- match_attr,
match_child,
match_child,
match_child,
string_matcher_destroy(&m->name);
} /* name_matcher_destroy */
-static int
-attr_matcher_init(sdb_object_t *obj, va_list ap)
-{
- attr_matcher_t *attr = ATTR_M(obj);
- const char *name = va_arg(ap, const char *);
-
- M(obj)->type = MATCHER_ATTR;
- if (name) {
- attr->name = strdup(name);
- if (! attr->name)
- return -1;
- }
- return string_matcher_init(&attr->value, ap);
-} /* attr_matcher_init */
-
-static void
-attr_matcher_destroy(sdb_object_t *obj)
-{
- attr_matcher_t *attr = ATTR_M(obj);
- if (attr->name)
- free(attr->name);
- attr->name = NULL;
- string_matcher_destroy(&attr->value);
-} /* attr_matcher_destroy */
-
static int
op_matcher_init(sdb_object_t *obj, va_list ap)
{
/* destroy = */ name_matcher_destroy,
};
-static sdb_type_t attr_type = {
- /* size = */ sizeof(attr_matcher_t),
- /* init = */ attr_matcher_init,
- /* destroy = */ attr_matcher_destroy,
-};
-
static sdb_type_t op_type = {
/* size = */ sizeof(op_matcher_t),
/* init = */ op_matcher_init,
return m;
} /* sdb_store_name_matcher */
-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)
- m = M(sdb_object_create("attr-matcher", attr_type,
- name, NULL, value));
- else
- m = M(sdb_object_create("attr-matcher", attr_type,
- name, value, NULL));
- return m;
-} /* sdb_store_attr_matcher */
-
sdb_store_matcher_t *
sdb_store_child_matcher(int type, sdb_store_matcher_t *m)
{
index a3bb2b7941cc640a6bf755794c377366d3d7e291..ab12162115b6d9d56d7011ae225871c488fc322f 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
sdb_store_matcher_t *
sdb_store_name_matcher(int type, const char *name, _Bool re);
-/*
- * sdb_store_attr_matcher:
- * Creates a matcher matching attributes based on their value. If 're' is
- * true, the specified name is treated as a POSIX extended regular expression.
- * Else, the exact name has to match (case-insensitive).
- */
-sdb_store_matcher_t *
-sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
-
/*
* sdb_store_isnull_matcher:
* Creates a matcher matching NULL values.
index d80a5990b4dbd4a52efb2248419e1af58c0ac63c..007fd05c042083cc14997b8df6967d1688d9bef6 100644 (file)
}
END_TEST
-START_TEST(test_store_match_attr)
-{
- sdb_store_obj_t *obj;
-
- struct {
- const char *attr_name;
- const char *attr_value;
- _Bool re;
-
- int expected;
- } golden_data[] = {
- { "k1", NULL, 0, 1 },
- { "k", NULL, 1, 0 },
- { "1", NULL, 1, 0 },
- { "k3", NULL, 0, 0 },
- { "k3", NULL, 1, 0 },
- { "k1", "v1", 0, 1 },
- { "k1", "v1", 1, 1 },
- { "k1", "^v1$", 1, 1 },
- { "k1", "v", 1, 1 },
- { "k1", "1", 1, 1 },
- { "k1", "v2", 0, 0 },
- { "k1", "v2", 1, 0 },
- { "k", "v1", 0, 0 },
- { "k", "v1", 1, 0 },
- { "k3", "1", 0, 0 },
- { "k3", "1", 1, 0 },
- };
-
- size_t i;
-
- obj = sdb_store_get_host("a");
- fail_unless(obj != NULL,
- "sdb_store_get_host(a) = NULL; expected: <host>");
-
- fail_unless(sdb_store_attr_matcher(NULL, "re", 0) == NULL,
- "sdb_store_attr_matcher(NULL, \"re\", 0) = <m>; expected: NULL");
- fail_unless(sdb_store_attr_matcher(NULL, "re", 1) == NULL,
- "sdb_store_attr_matcher(NULL, \"re\", 1) = <m>; expected: NULL");
-
- for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
- sdb_store_matcher_t *m, *n;
- int status;
-
- m = sdb_store_attr_matcher(golden_data[i].attr_name,
- golden_data[i].attr_value, golden_data[i].re);
- fail_unless(m != NULL,
- "sdb_store_attr_matcher() = NULL; expected: <matcher>");
-
- status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
- fail_unless(status == golden_data[i].expected,
- "sdb_store_matcher_matches(attribute[%s] = %s, "
- "<host a>, NULL) = %d; expected: %d", golden_data[i].attr_name,
- golden_data[i].attr_value, status, golden_data[i].expected);
-
- n = sdb_store_inv_matcher(m);
- fail_unless(n != NULL,
- "sdb_store_inv_matcher() = NULL; expected: <matcher>");
- sdb_object_deref(SDB_OBJ(m));
-
- /* now match the inverted set of objects */
- status = sdb_store_matcher_matches(n, obj, /* filter */ NULL);
- fail_unless(status == !golden_data[i].expected,
- "sdb_store_matcher_matches(attribute[%s] = %s, "
- "<host a>, NULL) = %d; expected: %d",
- golden_data[i].attr_name, golden_data[i].attr_value,
- status, !golden_data[i].expected);
-
- sdb_object_deref(SDB_OBJ(n));
- }
-
- sdb_object_deref(SDB_OBJ(obj));
-}
-END_TEST
-
START_TEST(test_cmp_attr)
{
sdb_store_obj_t *host;
const char *filter;
int expected;
} golden_data[] = {
+ /* TODO: check the name of the expected hosts */
{ "host = 'a'", NULL, 1 },
{ "host = 'a'", "host = 'x'", 0 }, /* filter never matches */
{ "host = 'a'",
{ "attribute = 'k1'", "host = 'x'", 0 }, /* filter never matches */
{ "attribute = 'k1'",
"NOT attribute['x'] = ''", 2 }, /* filter always matches */
+ { "attribute =~ 'k'", NULL, 2 },
+ { "attribute =~ '1'", NULL, 2 },
+ { "attribute =~ '2'", NULL, 1 },
{ "attribute = 'x'", NULL, 0 },
+ { "attribute =~ 'x'", NULL, 0 },
{ "attribute['k1'] = 'v1'", NULL, 1 },
+ { "attribute['k1'] =~ 'v1'", NULL, 1 },
+ { "attribute['k1'] =~ '^v1$'", NULL, 1 },
{ "attribute['k1'] =~ 'v'", NULL, 2 },
+ { "attribute['k1'] =~ '1'", NULL, 1 },
{ "attribute['k1'] !~ 'v'", NULL, 1 },
+ { "attribute['k1'] = 'v2'", NULL, 1 },
+ { "attribute['k1'] =~ 'v2'", NULL, 1 },
{ "attribute['x1'] =~ 'v'", NULL, 0 },
{ "attribute['x1'] =~ 'NULL'", NULL, 0 },
{ "attribute['x1'] !~ 'v'", NULL, 3 },
tc = tcase_create("core");
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_cmp_attr);
tcase_add_test(tc, test_cmp_obj);
tcase_add_test(tc, test_store_match_op);