Code

store: Add support for iterable expressions.
authorSebastian Harl <sh@tokkee.org>
Thu, 5 Mar 2015 20:25:40 +0000 (21:25 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 5 Mar 2015 20:25:40 +0000 (21:25 +0100)
It can be used to iterate through either the list of child nodes or through
arbitrary arrays. An expression is iterable either if it stores / evaluates to
an array or if it's a typed expression evaluated in the context of an object
where the specified type represents child nodes.

src/core/store_expr.c
src/include/core/store.h

index a0047a1f12e1fa8498ef0ed5255373478f98a070..230c95795f5046944ee665ce3c86f69efd19e5a7 100644 (file)
  * private data types
  */
 
+/* iterate through either a list of child nodes or arrays */
+struct sdb_store_expr_iter {
+       sdb_store_obj_t *obj;
+       sdb_store_expr_t *expr;
+
+       sdb_avltree_iter_t *tree;
+
+       sdb_data_t array;
+       size_t array_idx;
+
+       sdb_store_matcher_t *filter;
+};
+
+/*
+ * private types
+ */
+
 static int
 expr_init(sdb_object_t *obj, va_list ap)
 {
@@ -244,5 +261,176 @@ sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
        return status;
 } /* sdb_store_expr_eval */
 
+bool
+sdb_store_expr_iterable(sdb_store_expr_t *expr, int context)
+{
+       if (expr->type == TYPED_EXPR) {
+               if ((context != SDB_HOST) && (context != SDB_SERVICE)
+                               && (context != SDB_METRIC))
+                       return 0;
+               if (context == expr->data.data.integer)
+                       return 0;
+               if ((expr->data.data.integer != SDB_SERVICE)
+                               && (expr->data.data.integer != SDB_METRIC)
+                               && (expr->data.data.integer != SDB_ATTRIBUTE))
+                       return 0;
+               if ((context == SDB_SERVICE)
+                               && (expr->data.data.integer == SDB_METRIC))
+                       return 0;
+               else if ((context == SDB_METRIC)
+                               && (expr->data.data.integer == SDB_SERVICE))
+                       return 0;
+               return 1;
+       }
+       else if (expr->type == FIELD_VALUE) {
+               if ((context != SDB_HOST) && (context != SDB_SERVICE)
+                               && (context != SDB_METRIC) && (context != SDB_ATTRIBUTE))
+                       return 0;
+               return expr->data.data.integer == SDB_FIELD_BACKEND;
+       }
+       else if (! expr->type) {
+               return !!(expr->data.type & SDB_TYPE_ARRAY);
+       }
+       return 0;
+} /* sdb_store_expr_iterable */
+
+sdb_store_expr_iter_t *
+sdb_store_expr_iter(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
+{
+       sdb_store_expr_iter_t *iter;
+       sdb_avltree_iter_t *tree = NULL;
+       sdb_data_t array = SDB_DATA_INIT;
+
+       if ((! expr) || (! obj))
+               return NULL;
+
+       if (expr->type == TYPED_EXPR) {
+               if (obj->type == SDB_HOST) {
+                       if (expr->data.data.integer == SDB_SERVICE)
+                               tree = sdb_avltree_get_iter(HOST(obj)->services);
+                       else if (expr->data.data.integer == SDB_METRIC)
+                               tree = sdb_avltree_get_iter(HOST(obj)->metrics);
+                       else if (expr->data.data.integer == SDB_ATTRIBUTE)
+                               tree = sdb_avltree_get_iter(HOST(obj)->attributes);
+               }
+               else if (obj->type == SDB_SERVICE) {
+                       if (expr->data.data.integer == SDB_ATTRIBUTE)
+                               tree = sdb_avltree_get_iter(SVC(obj)->attributes);
+               }
+               else if (obj->type == SDB_METRIC) {
+                       if (expr->data.data.integer == SDB_ATTRIBUTE)
+                               tree = sdb_avltree_get_iter(METRIC(obj)->attributes);
+               }
+       }
+       else if (expr->type == FIELD_VALUE) {
+               if (expr->data.data.integer == SDB_FIELD_BACKEND) {
+                       /* while scanning the store, we hold a read lock, so it's safe to
+                        * access the data without copying */
+                       array.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
+                       array.data.array.length = obj->backends_num;
+                       array.data.array.values = obj->backends;
+               }
+       }
+       else if (! expr->type) {
+               if (expr->data.type & SDB_TYPE_ARRAY)
+                       array = expr->data;
+       }
+       else
+               return NULL;
+
+       if ((! tree) && (array.type == SDB_TYPE_NULL))
+               return NULL;
+
+       iter = calloc(1, sizeof(*iter));
+       if (! iter)
+               return NULL;
+
+       sdb_object_ref(SDB_OBJ(obj));
+       sdb_object_ref(SDB_OBJ(expr));
+       sdb_object_ref(SDB_OBJ(filter));
+
+       iter->obj = obj;
+       iter->expr = expr;
+       iter->tree = tree;
+       iter->array = array;
+       iter->filter = filter;
+       return iter;
+} /* sdb_store_expr_iter */
+
+void
+sdb_store_expr_iter_destroy(sdb_store_expr_iter_t *iter)
+{
+       sdb_data_t null = SDB_DATA_INIT;
+
+       if (! iter)
+               return;
+
+       if (iter->tree)
+               sdb_avltree_iter_destroy(iter->tree);
+       iter->tree = NULL;
+
+       iter->array = null;
+       iter->array_idx = 0;
+
+       sdb_object_deref(SDB_OBJ(iter->obj));
+       sdb_object_deref(SDB_OBJ(iter->filter));
+       free(iter);
+} /* sdb_store_expr_iter_destroy */
+
+bool
+sdb_store_expr_iter_has_next(sdb_store_expr_iter_t *iter)
+{
+       if (! iter)
+               return 0;
+
+       if (iter->tree)
+               return sdb_avltree_iter_has_next(iter->tree);
+       return iter->array_idx < iter->array.data.array.length;
+} /* sdb_store_expr_iter_has_next */
+
+sdb_data_t
+sdb_store_expr_iter_get_next(sdb_store_expr_iter_t *iter)
+{
+       sdb_data_t null = SDB_DATA_INIT;
+       sdb_data_t ret = SDB_DATA_INIT;
+
+       if (! iter)
+               return null;
+
+       if (iter->tree) {
+               sdb_store_obj_t *child;
+
+               while (42) {
+                       child = STORE_OBJ(sdb_avltree_iter_get_next(iter->tree));
+                       if (! child)
+                               break;
+                       if (iter->filter
+                                       && (! sdb_store_matcher_matches(iter->filter, child, NULL)))
+                               continue;
+
+                       if (sdb_store_expr_eval(iter->expr, iter->obj, &ret, iter->filter))
+                               return null;
+                       break;
+               }
+
+               /* Skip over any filtered objects */
+               if (iter->filter)
+                       while ((child = STORE_OBJ(sdb_avltree_iter_peek_next(iter->tree))))
+                               if (sdb_store_matcher_matches(iter->filter, child, NULL))
+                                       break;
+
+               return ret;
+       }
+
+       if (iter->array_idx >= iter->array.data.array.length)
+               return null;
+       if (sdb_data_array_get(&iter->array, iter->array_idx, &ret))
+               return null;
+
+       ++iter->array_idx;
+       return ret;
+} /* sdb_store_expr_iter_get_next */
+
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
 
index 68f094b4a9d996a85fb40292656e9fcad4bd8cff..e83ff8486ebcf2a8d21a49a9c8da87ddeceba9ec 100644 (file)
@@ -113,6 +113,13 @@ struct sdb_store_expr;
 typedef struct sdb_store_expr sdb_store_expr_t;
 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
 
+/*
+ * An expression iterator iterates over the values of an iterable expression
+ * (see sdb_store_expr_iterable).
+ */
+struct sdb_store_expr_iter;
+typedef struct sdb_store_expr_iter sdb_store_expr_iter_t;
+
 /*
  * Store matchers may be used to lookup hosts from the store based on their
  * various attributes. Service and attribute matchers are applied to a host's
@@ -425,6 +432,34 @@ int
 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
                sdb_data_t *res, sdb_store_matcher_t *filter);
 
+/*
+ * sdb_store_expr_iterable:
+ * Check whether an expression, evaluated in the specified context (HOST,
+ * SERVICE, METRIC) is iterable, that is, if it may evaluate to multiple
+ * values.
+ */
+bool
+sdb_store_expr_iterable(sdb_store_expr_t *expr, int context);
+
+/*
+ * sdb_store_expr_iter:
+ * Iterate over the elements of an iterable expression. sdb_store_expr_iter
+ * returns NULL if the expression is not iterable (for the specified object).
+ * See also sdb_store_expr_iterable.
+ *
+ * sdb_store_expr_iter_get_next returns NULL if there is no next element.
+ */
+sdb_store_expr_iter_t *
+sdb_store_expr_iter(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter);
+void
+sdb_store_expr_iter_destroy(sdb_store_expr_iter_t *iter);
+
+bool
+sdb_store_expr_iter_has_next(sdb_store_expr_iter_t *iter);
+sdb_data_t
+sdb_store_expr_iter_get_next(sdb_store_expr_iter_t *iter);
+
 /*
  * sdb_store_dis_matcher:
  * Creates a matcher matching the disjunction (logical OR) of two matchers.