Code

store: Use iterable expressions for iterators.
authorSebastian Harl <sh@tokkee.org>
Sun, 8 Mar 2015 21:57:06 +0000 (22:57 +0100)
committerSebastian Harl <sh@tokkee.org>
Sun, 8 Mar 2015 21:57:06 +0000 (22:57 +0100)
Rather than having a separate iterator implementation in store_lookup.

src/core/store-private.h
src/core/store_lookup.c
src/frontend/analyzer.c

index 2b6b09662f36a91bad7821ed8baacd152ac7e98c..874dc0577a9fe030e64fa7d59b1cfb2c4ac5d709 100644 (file)
@@ -126,7 +126,7 @@ struct sdb_store_expr {
 
        sdb_data_t data;
 };
-#define EXPR_INIT { SDB_OBJECT_INIT, 0, -1, NULL, NULL, SDB_DATA_INIT }
+#define CONST_EXPR(v) { SDB_OBJECT_INIT, 0, (v).type, NULL, NULL, (v) }
 #define EXPR_TO_STRING(e) \
        (((e)->type == TYPED_EXPR) ? "<typed>" \
                : ((e)->type == ATTR_VALUE) ? "attribute" \
index e3b5e8a7331d2953a68e5dfe8ce7f0bcafb0e426..9192618dd069442517da83e1461aae06d49e8031 100644 (file)
@@ -153,14 +153,6 @@ match_regex_value(int op, sdb_data_t *v, sdb_data_t *re)
        return status;
 } /* match_regex_value */
 
-static int
-match_value(int op, sdb_data_t *v1, sdb_data_t *v2, bool strcmp_fallback)
-{
-       if ((op == MATCHER_REGEX) || (op == MATCHER_NREGEX))
-               return match_regex_value(op, v1, v2);
-       return match_cmp_value(op, v1, v2, strcmp_fallback);
-} /* match_value */
-
 static int
 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
                sdb_store_matcher_t *filter)
@@ -191,106 +183,33 @@ match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
        return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
 } /* match_unary */
 
-/* iterate arrays: ANY/ALL <array> <cmp> <value> */
-static int
-match_iter_backends(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
-               sdb_store_matcher_t *filter)
-{
-       sdb_data_t op = SDB_DATA_INIT;
-       int status;
-
-       if ((ITER_M(m)->m->type < MATCHER_LT)
-                       || (MATCHER_NREGEX < ITER_M(m)->m->type))
-               return 0;
-
-       if (sdb_store_expr_eval(CMP_M(ITER_M(m)->m)->right, obj, &op, filter))
-               return 0;
-
-       if (op.type & SDB_TYPE_ARRAY)
-               status = 0;
-       else if (sdb_data_isnull(&op))
-               status = 0;
-       else {
-               size_t i;
-               int all = (int)(m->type == MATCHER_ALL);
-
-               status = all;
-               for (i = 0; i < obj->backends_num; ++i) {
-                       sdb_data_t v = { SDB_TYPE_STRING, { .string = obj->backends[i] } };
-
-                       if (match_value(ITER_M(m)->m->type, &v, &op,
-                                               CMP_M(ITER_M(m)->m)->right->data_type < 0)) {
-                               if (! all) {
-                                       status = 1;
-                                       break;
-                               }
-                       }
-                       else if (all) {
-                               status = 0;
-                               break;
-                       }
-               }
-       }
-
-       sdb_data_free_datum(&op);
-       return status;
-} /* match_iter_backends */
-
+/* iterate: ANY/ALL <iter> <cmp> <value> */
 static int
 match_iter(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
                sdb_store_matcher_t *filter)
 {
-       sdb_avltree_iter_t *iter = NULL;
-       int type, status;
+       sdb_store_expr_iter_t *iter = NULL;
+       int status;
        int all = (int)(m->type == MATCHER_ALL);
 
        assert((m->type == MATCHER_ANY) || (m->type == MATCHER_ALL));
        assert((! CMP_M(ITER_M(m)->m)->left) && CMP_M(ITER_M(m)->m)->right);
 
-       if (ITER_M(m)->iter->type == FIELD_VALUE) {
-               if (ITER_M(m)->iter->data.data.integer != SDB_FIELD_BACKEND)
-                       return 0;
-               return match_iter_backends(m, obj, filter);
-       }
-
-       assert(ITER_M(m)->iter->type == TYPED_EXPR);
-
-       type = (int)ITER_M(m)->iter->data.data.integer;
-       if (obj->type == SDB_HOST) {
-               if (type == SDB_SERVICE)
-                       iter = sdb_avltree_get_iter(HOST(obj)->services);
-               else if (type == SDB_METRIC)
-                       iter = sdb_avltree_get_iter(HOST(obj)->metrics);
-               else if (type == SDB_ATTRIBUTE)
-                       iter = sdb_avltree_get_iter(HOST(obj)->attributes);
-       } else if (obj->type == SDB_SERVICE) {
-               if (type == SDB_ATTRIBUTE)
-                       iter = sdb_avltree_get_iter(SVC(obj)->attributes);
-       } else if (obj->type == SDB_METRIC) {
-               if (type == SDB_ATTRIBUTE)
-                       iter = sdb_avltree_get_iter(METRIC(obj)->attributes);
-       }
+       iter = sdb_store_expr_iter(ITER_M(m)->iter, obj, filter);
+       if (! iter)
+               return 0;
 
        status = all;
-       while (sdb_avltree_iter_has_next(iter)) {
-               sdb_store_obj_t *child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
-               sdb_store_expr_t expr = EXPR_INIT;
-               sdb_data_t v = SDB_DATA_INIT;
+       while (sdb_store_expr_iter_has_next(iter)) {
+               sdb_data_t v = sdb_store_expr_iter_get_next(iter);
+               sdb_store_expr_t expr = CONST_EXPR(v);
                bool matches;
 
-               if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
-                       continue;
-               if (sdb_store_expr_eval(ITER_M(m)->iter, child, &v, filter))
-                       continue;
-
-               expr.type = 0;
-               expr.data_type = v.type;
-               expr.data = v;
-
                CMP_M(ITER_M(m)->m)->left = &expr;
-               matches = sdb_store_matcher_matches(ITER_M(m)->m, child, filter);
+               matches = sdb_store_matcher_matches(ITER_M(m)->m, obj, filter);
                CMP_M(ITER_M(m)->m)->left = NULL;
                sdb_data_free_datum(&v);
+
                if (matches) {
                        if (! all) {
                                status = 1;
@@ -301,7 +220,7 @@ match_iter(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
                        break;
                }
        }
-       sdb_avltree_iter_destroy(iter);
+       sdb_store_expr_iter_destroy(iter);
        return status;
 } /* match_iter */
 
index c71d5ca078b5fbe9a3915013c183bb05ab6bd1f5..c96f81b61fa6cc121f19e9d48ff98f9cffda57c5 100644 (file)
@@ -153,47 +153,31 @@ analyze_matcher(int context, int parent_type,
                case MATCHER_ANY:
                case MATCHER_ALL:
                {
-                       int type = -1;
+                       int child_context = -1;
                        int left_type = -1;
 
                        assert(ITER_M(m)->m);
 
+                       if (! sdb_store_expr_iterable(ITER_M(m)->iter, context)) {
+                               iter_error(errbuf, m->type, ITER_M(m)->iter, context);
+                               return -1;
+                       }
+
                        if (ITER_M(m)->iter->type == TYPED_EXPR) {
-                               type = (int)ITER_M(m)->iter->data.data.integer;
+                               child_context = (int)ITER_M(m)->iter->data.data.integer;
                                left_type = ITER_M(m)->iter->data_type;
                        }
                        else if (ITER_M(m)->iter->type == FIELD_VALUE) {
-                               type = (int)ITER_M(m)->iter->data.data.integer;
+                               child_context = context;
                                /* element type of the field */
                                left_type = ITER_M(m)->iter->data_type & 0xff;
                        }
-                       else {
-                               iter_error(errbuf, m->type, ITER_M(m)->iter, context);
-                               return -1;
-                       }
-
-                       if ((context != SDB_HOST)
-                                       && (context != SDB_SERVICE)
-                                       && (context != SDB_METRIC)) {
-                               iter_error(errbuf, m->type, ITER_M(m)->iter, context);
-                               return -1;
+                       else if (! ITER_M(m)->iter->type) {
+                               child_context = context;
+                               /* elements of the array constant */
+                               left_type = ITER_M(m)->iter->data.type & 0xff;
                        }
-                       if (type == context) {
-                               iter_error(errbuf, m->type, ITER_M(m)->iter, context);
-                               return -1;
-                       }
-                       if ((type != SDB_SERVICE)
-                                       && (type != SDB_METRIC)
-                                       && (type != SDB_ATTRIBUTE)
-                                       && (type != SDB_FIELD_BACKEND)) {
-                               iter_error(errbuf, m->type, ITER_M(m)->iter, context);
-                               return -1;
-                       }
-                       if ((context == SDB_SERVICE) && (type == SDB_METRIC)) {
-                               iter_error(errbuf, m->type, ITER_M(m)->iter, context);
-                               return -1;
-                       }
-                       else if ((context == SDB_METRIC) && (type == SDB_SERVICE)) {
+                       else {
                                iter_error(errbuf, m->type, ITER_M(m)->iter, context);
                                return -1;
                        }
@@ -222,7 +206,7 @@ analyze_matcher(int context, int parent_type,
                                        return -1;
                                }
                        }
-                       if (analyze_matcher(type, m->type, ITER_M(m)->m, errbuf))
+                       if (analyze_matcher(child_context, m->type, ITER_M(m)->m, errbuf))
                                return -1;
                        break;
                }