From 6d0d1e3ef653bb23cb76e3330dc58c49257c8573 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sat, 25 Oct 2014 13:58:58 +0200 Subject: [PATCH] store: Renamed child-matcher to any-matcher. It matches if any object of an iterable list matches, so this is a more appropriate name. --- src/core/store-private.h | 15 +++----- src/core/store_lookup.c | 67 +++++++++++++++------------------ src/frontend/grammar.y | 2 +- src/include/core/store.h | 8 ++-- t/unit/core/store_lookup_test.c | 2 +- t/unit/frontend/parser_test.c | 48 +++++++++++------------ 6 files changed, 66 insertions(+), 76 deletions(-) diff --git a/src/core/store-private.h b/src/core/store-private.h index 1e2c3ec..4c38f15 100644 --- a/src/core/store-private.h +++ b/src/core/store-private.h @@ -135,9 +135,7 @@ enum { MATCHER_OR, MATCHER_AND, MATCHER_NOT, - MATCHER_SERVICE, - MATCHER_METRIC, - MATCHER_ATTRIBUTE, + MATCHER_ANY, MATCHER_LT, MATCHER_LE, MATCHER_EQ, @@ -156,9 +154,7 @@ enum { : ((t) == MATCHER_AND) ? "AND" \ : ((t) == MATCHER_NOT) ? "NOT" \ : ((t) == MATCHER_NAME) ? "NAME" \ - : ((t) == MATCHER_SERVICE) ? "SERVICE" \ - : ((t) == MATCHER_METRIC) ? "METRIC" \ - : ((t) == MATCHER_ATTRIBUTE) ? "ATTRIBUTE" \ + : ((t) == MATCHER_ANY) ? "ANY" \ : ((t) == MATCHER_LT) ? "<" \ : ((t) == MATCHER_LE) ? "<=" \ : ((t) == MATCHER_EQ) ? "=" \ @@ -205,12 +201,13 @@ typedef struct { } uop_matcher_t; #define UOP_M(m) ((uop_matcher_t *)(m)) -/* child matcher */ +/* iter matcher */ typedef struct { sdb_store_matcher_t super; + int type; sdb_store_matcher_t *m; -} child_matcher_t; -#define CHILD_M(m) ((child_matcher_t *)(m)) +} iter_matcher_t; +#define ITER_M(m) ((iter_matcher_t *)(m)) /* compare operator matcher */ typedef struct { diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index 93fb347..f95f8ca 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -109,16 +109,14 @@ match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj, } /* match_unary */ static int -match_child(sdb_store_matcher_t *m, sdb_store_obj_t *obj, +match_iter(sdb_store_matcher_t *m, sdb_store_obj_t *obj, sdb_store_matcher_t *filter) { sdb_avltree_iter_t *iter = NULL; int status; int all = 0; - assert((m->type == MATCHER_SERVICE) - || (m->type == MATCHER_METRIC) - || (m->type == MATCHER_ATTRIBUTE)); + assert(m->type == MATCHER_ANY); /* TODO: support all object types */ if (obj->type != SDB_HOST) @@ -126,15 +124,15 @@ match_child(sdb_store_matcher_t *m, sdb_store_obj_t *obj, /* negated matchers should only match if the respective positive matchers * do not match; that is if the negated matcher matchers *all* children */ - if ((CHILD_M(m)->m->type == MATCHER_NE) - || (CHILD_M(m)->m->type == MATCHER_NREGEX)) + if ((ITER_M(m)->m->type == MATCHER_NE) + || (ITER_M(m)->m->type == MATCHER_NREGEX)) all = 1; - if (m->type == MATCHER_SERVICE) + if (ITER_M(m)->type == SDB_SERVICE) iter = sdb_avltree_get_iter(HOST(obj)->services); - else if (m->type == MATCHER_METRIC) + else if (ITER_M(m)->type == SDB_METRIC) iter = sdb_avltree_get_iter(HOST(obj)->metrics); - else if (m->type == MATCHER_ATTRIBUTE) + else if (ITER_M(m)->type == SDB_ATTRIBUTE) iter = sdb_avltree_get_iter(HOST(obj)->attributes); status = all; @@ -143,7 +141,7 @@ match_child(sdb_store_matcher_t *m, sdb_store_obj_t *obj, if (filter && (! sdb_store_matcher_matches(filter, child, NULL))) continue; - if (sdb_store_matcher_matches(CHILD_M(m)->m, child, filter)) { + if (sdb_store_matcher_matches(ITER_M(m)->m, child, filter)) { if (! all) { status = 1; break; @@ -155,7 +153,7 @@ match_child(sdb_store_matcher_t *m, sdb_store_obj_t *obj, } sdb_avltree_iter_destroy(iter); return status; -} /* match_child */ +} /* match_iter */ /* * cmp_expr: @@ -368,9 +366,7 @@ matchers[] = { match_logical, match_logical, match_unary, - match_child, - match_child, - match_child, + match_iter, match_lt, match_le, match_eq, @@ -415,23 +411,24 @@ op_matcher_destroy(sdb_object_t *obj) } /* op_matcher_destroy */ static int -child_matcher_init(sdb_object_t *obj, va_list ap) +iter_matcher_init(sdb_object_t *obj, va_list ap) { M(obj)->type = va_arg(ap, int); - CHILD_M(obj)->m = va_arg(ap, sdb_store_matcher_t *); + ITER_M(obj)->type = va_arg(ap, int); + ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *); - if (! CHILD_M(obj)->m) + if (! ITER_M(obj)->m) return -1; - sdb_object_ref(SDB_OBJ(CHILD_M(obj)->m)); + sdb_object_ref(SDB_OBJ(ITER_M(obj)->m)); return 0; -} /* child_matcher_init */ +} /* iter_matcher_init */ static void -child_matcher_destroy(sdb_object_t *obj) +iter_matcher_destroy(sdb_object_t *obj) { - sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m)); -} /* child_matcher_destroy */ + sdb_object_deref(SDB_OBJ(ITER_M(obj)->m)); +} /* iter_matcher_destroy */ static int cmp_matcher_init(sdb_object_t *obj, va_list ap) @@ -508,10 +505,10 @@ static sdb_type_t uop_type = { /* destroy = */ uop_matcher_destroy, }; -static sdb_type_t child_type = { - /* size = */ sizeof(child_matcher_t), - /* init = */ child_matcher_init, - /* destroy = */ child_matcher_destroy, +static sdb_type_t iter_type = { + /* size = */ sizeof(iter_matcher_t), + /* init = */ iter_matcher_init, + /* destroy = */ iter_matcher_destroy, }; static sdb_type_t cmp_type = { @@ -531,18 +528,14 @@ static sdb_type_t isnull_type = { */ sdb_store_matcher_t * -sdb_store_child_matcher(int type, sdb_store_matcher_t *m) -{ - if (type == SDB_SERVICE) - type = MATCHER_SERVICE; - else if (type == SDB_METRIC) - type = MATCHER_METRIC; - else if (type == SDB_ATTRIBUTE) - type = MATCHER_ATTRIBUTE; - else +sdb_store_any_matcher(int type, sdb_store_matcher_t *m) +{ + if ((type != SDB_SERVICE) && (type != SDB_METRIC) + && (type != SDB_ATTRIBUTE)) return NULL; - return M(sdb_object_create("any-matcher", child_type, type, m)); -} /* sdb_store_child_matcher */ + return M(sdb_object_create("any-matcher", iter_type, + MATCHER_ANY, type, m)); +} /* sdb_store_iter_matcher */ sdb_store_matcher_t * sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right) diff --git a/src/frontend/grammar.y b/src/frontend/grammar.y index 3c813dc..cfffdac 100644 --- a/src/frontend/grammar.y +++ b/src/frontend/grammar.y @@ -458,7 +458,7 @@ compare_matcher: $$ = m; } else { - $$ = sdb_store_child_matcher(type, m); + $$ = sdb_store_any_matcher(type, m); sdb_object_deref(SDB_OBJ(m)); } diff --git a/src/include/core/store.h b/src/include/core/store.h index 97bb6e6..ed445c3 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -381,12 +381,12 @@ sdb_store_matcher_t * sdb_store_isnnull_matcher(sdb_store_expr_t *expr); /* - * sdb_store_child_matcher: - * Creates a matcher matching an object's children of the specified type. It - * matches if *any* of those children match 'm'. + * sdb_store_any_matcher: + * Creates a matcher iterating over objects of the specified type. It matches + * if *any* of those objects match 'm'. */ sdb_store_matcher_t * -sdb_store_child_matcher(int type, sdb_store_matcher_t *m); +sdb_store_any_matcher(int type, sdb_store_matcher_t *m); /* * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher, diff --git a/t/unit/core/store_lookup_test.c b/t/unit/core/store_lookup_test.c index 65cbc62..dbf313a 100644 --- a/t/unit/core/store_lookup_test.c +++ b/t/unit/core/store_lookup_test.c @@ -178,7 +178,7 @@ START_TEST(test_cmp_name) m = sdb_store_eq_matcher(obj, value); if (golden_data[i].type != SDB_HOST) { sdb_store_matcher_t *tmp; - tmp = sdb_store_child_matcher(golden_data[i].type, m); + tmp = sdb_store_any_matcher(golden_data[i].type, m); sdb_object_deref(SDB_OBJ(m)); m = tmp; } diff --git a/t/unit/frontend/parser_test.c b/t/unit/frontend/parser_test.c index b48ca2f..1ca9f2d 100644 --- a/t/unit/frontend/parser_test.c +++ b/t/unit/frontend/parser_test.c @@ -318,33 +318,33 @@ START_TEST(test_parse_matcher) { "host = 'host' ", 13, MATCHER_EQ }, { "host &^ 'localhost'", -1, -1 }, /* match hosts by service */ - { "service < 'name'", -1, MATCHER_SERVICE }, - { "service <= 'name'", -1, MATCHER_SERVICE }, - { "service = 'name'", -1, MATCHER_SERVICE }, - { "service != 'name'", -1, MATCHER_SERVICE }, - { "service >= 'name'", -1, MATCHER_SERVICE }, - { "service > 'name'", -1, MATCHER_SERVICE }, - { "service =~ 'pattern'", -1, MATCHER_SERVICE }, - { "service !~ 'pattern'", -1, MATCHER_SERVICE }, + { "service < 'name'", -1, MATCHER_ANY }, + { "service <= 'name'", -1, MATCHER_ANY }, + { "service = 'name'", -1, MATCHER_ANY }, + { "service != 'name'", -1, MATCHER_ANY }, + { "service >= 'name'", -1, MATCHER_ANY }, + { "service > 'name'", -1, MATCHER_ANY }, + { "service =~ 'pattern'", -1, MATCHER_ANY }, + { "service !~ 'pattern'", -1, MATCHER_ANY }, { "service &^ 'name'", -1, -1 }, /* match hosts by metric */ - { "metric < 'name'", -1, MATCHER_METRIC }, - { "metric <= 'name'", -1, MATCHER_METRIC }, - { "metric = 'name'", -1, MATCHER_METRIC }, - { "metric != 'name'", -1, MATCHER_METRIC }, - { "metric >= 'name'", -1, MATCHER_METRIC }, - { "metric > 'name'", -1, MATCHER_METRIC }, - { "metric =~ 'pattern'", -1, MATCHER_METRIC }, - { "metric !~ 'pattern'", -1, MATCHER_METRIC }, + { "metric < 'name'", -1, MATCHER_ANY }, + { "metric <= 'name'", -1, MATCHER_ANY }, + { "metric = 'name'", -1, MATCHER_ANY }, + { "metric != 'name'", -1, MATCHER_ANY }, + { "metric >= 'name'", -1, MATCHER_ANY }, + { "metric > 'name'", -1, MATCHER_ANY }, + { "metric =~ 'pattern'", -1, MATCHER_ANY }, + { "metric !~ 'pattern'", -1, MATCHER_ANY }, /* match hosts by attribute */ - { "attribute < 'name'", -1, MATCHER_ATTRIBUTE }, - { "attribute <= 'name'", -1, MATCHER_ATTRIBUTE }, - { "attribute = 'name'", -1, MATCHER_ATTRIBUTE }, - { "attribute != 'name'", -1, MATCHER_ATTRIBUTE }, - { "attribute >= 'name'", -1, MATCHER_ATTRIBUTE }, - { "attribute > 'name'", -1, MATCHER_ATTRIBUTE }, - { "attribute =~ 'pattern'", -1, MATCHER_ATTRIBUTE }, - { "attribute !~ 'pattern'", -1, MATCHER_ATTRIBUTE }, + { "attribute < 'name'", -1, MATCHER_ANY }, + { "attribute <= 'name'", -1, MATCHER_ANY }, + { "attribute = 'name'", -1, MATCHER_ANY }, + { "attribute != 'name'", -1, MATCHER_ANY }, + { "attribute >= 'name'", -1, MATCHER_ANY }, + { "attribute > 'name'", -1, MATCHER_ANY }, + { "attribute =~ 'pattern'", -1, MATCHER_ANY }, + { "attribute !~ 'pattern'", -1, MATCHER_ANY }, { "attribute &^ 'pattern'", -1, -1 }, /* composite expressions */ { "host =~ 'pattern' AND " -- 2.30.2