Code

store: Merged implementations of compare matchers.
[sysdb.git] / src / core / store_lookup.c
index 180d17931d2c603fb0e295b5b06f4c9637d5cb8a..3d509b0511f026276cfa55194aafad34435164e2 100644 (file)
 #include <limits.h>
 
 /*
- * private data types
- */
-
-typedef struct {
-       sdb_store_matcher_t *m;
-       sdb_store_matcher_t *filter;
-       sdb_store_lookup_cb  cb;
-       void *user_data;
-} scan_iter_data_t;
-
-/*
- * private helper functions
+ * matcher implementations
  */
 
-static int
-scan_iter(sdb_store_obj_t *obj, void *user_data)
-{
-       scan_iter_data_t *d = user_data;
-
-       if (sdb_store_matcher_matches(d->m, obj, d->filter))
-               return d->cb(obj, d->user_data);
-       return 0;
-} /* scan_iter */
-
 /*
- * matcher implementations
+ * cmp_expr:
+ * Compare two values using the specified matcher operator. If strcmp_fallback
+ * is enabled, compare the string values in case of a type mismatch.
  */
-
 static int
-match_string(string_matcher_t *m, const char *name)
+cmp_value(int op, sdb_data_t *v1, sdb_data_t *v2, _Bool strcmp_fallback)
 {
-       if ((! m->name) && (! m->name_re))
-               return 1;
+       int status;
 
-       if (! name)
-               name = "";
+       if (sdb_data_isnull(v1) || (sdb_data_isnull(v2)))
+               status = INT_MAX;
+       else if (v1->type == v2->type)
+               status = sdb_data_cmp(v1, v2);
+       else if (! strcmp_fallback)
+               status = INT_MAX;
+       else
+               status = sdb_data_strcmp(v1, v2);
 
-       if (m->name && strcasecmp(m->name, name))
+       if (status == INT_MAX)
                return 0;
-       if (m->name_re && regexec(m->name_re, name,
-                                       /* matches */ 0, NULL, /* flags = */ 0))
-               return 0;
-       return 1;
-} /* match_string */
+       else if (op == MATCHER_LT)
+               return status < 0;
+       else if (op == MATCHER_LE)
+               return status <= 0;
+       else if (op == MATCHER_EQ)
+               return status == 0;
+       else if (op == MATCHER_NE)
+               return status != 0;
+       else if (op == MATCHER_GE)
+               return status >= 0;
+       else if (op == MATCHER_GT)
+               return status > 0;
+       return 0;
+} /* cmp_value */
 
 static int
 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
@@ -126,81 +120,37 @@ match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
 } /* match_unary */
 
 static int
-match_name(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 = 0;
+       int status;
+       int all = (int)(m->type == MATCHER_ALL);
 
-       assert(m->type == MATCHER_NAME);
+       assert((m->type == MATCHER_ANY) || (m->type == MATCHER_ALL));
 
-       if (obj->type == NAME_M(m)->obj_type)
-               return match_string(&NAME_M(m)->name, SDB_OBJ(obj)->name);
-       else if (obj->type != SDB_HOST)
-               return 0;
-
-       switch (NAME_M(m)->obj_type) {
-               case SDB_SERVICE:
+       if (obj->type == SDB_HOST) {
+               if (ITER_M(m)->type == SDB_SERVICE)
                        iter = sdb_avltree_get_iter(HOST(obj)->services);
-                       break;
-               case SDB_METRIC:
+               else if (ITER_M(m)->type == SDB_METRIC)
                        iter = sdb_avltree_get_iter(HOST(obj)->metrics);
-                       break;
-               case SDB_ATTRIBUTE:
+               else if (ITER_M(m)->type == SDB_ATTRIBUTE)
                        iter = sdb_avltree_get_iter(HOST(obj)->attributes);
-                       break;
+       } else if (obj->type == SDB_SERVICE) {
+               if (ITER_M(m)->type == SDB_ATTRIBUTE)
+                       iter = sdb_avltree_get_iter(SVC(obj)->attributes);
+       } else if (obj->type == SDB_METRIC) {
+               if (ITER_M(m)->type == SDB_ATTRIBUTE)
+                       iter = sdb_avltree_get_iter(METRIC(obj)->attributes);
        }
 
-       while (sdb_avltree_iter_has_next(iter)) {
-               sdb_object_t *child = sdb_avltree_iter_get_next(iter);
-               if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(child),
-                                               NULL)))
-                       continue;
-               if (match_string(&NAME_M(m)->name, child->name)) {
-                       status = 1;
-                       break;
-               }
-       }
-       sdb_avltree_iter_destroy(iter);
-       return status;
-} /* match_name */
-
-static int
-match_child(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));
-
-       /* TODO: support all object types */
-       if (obj->type != SDB_HOST)
-               return 0;
-
-       /* 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))
-               all = 1;
-
-       if (m->type == MATCHER_SERVICE)
-               iter = sdb_avltree_get_iter(HOST(obj)->services);
-       else if (m->type == MATCHER_METRIC)
-               iter = sdb_avltree_get_iter(HOST(obj)->metrics);
-       else if (m->type == MATCHER_ATTRIBUTE)
-               iter = sdb_avltree_get_iter(HOST(obj)->attributes);
-
        status = all;
        while (sdb_avltree_iter_has_next(iter)) {
                sdb_store_obj_t *child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
                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;
@@ -212,102 +162,38 @@ 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:
- * Compare the values of two expressions when evaluating them using the
- * specified stored object and filter. Returns a value less than, equal to, or
- * greater than zero if the value of the first expression compares less than,
- * equal to, or greater than the value of the second expression. Returns
- * INT_MAX if any of the expressions could not be evaluated or if any of them
- * evaluated to NULL.
- */
 static int
-cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
-               sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
+match_cmp(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
 {
+       sdb_store_expr_t *e1 = CMP_M(m)->left;
+       sdb_store_expr_t *e2 = CMP_M(m)->right;
        sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
        int status;
 
+       assert((m->type == MATCHER_LT)
+                       || (m->type == MATCHER_LE)
+                       || (m->type == MATCHER_EQ)
+                       || (m->type == MATCHER_NE)
+                       || (m->type == MATCHER_GE)
+                       || (m->type == MATCHER_GT));
+
        if (sdb_store_expr_eval(e1, obj, &v1, filter))
-               return INT_MAX;
+               return 0;
        if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
                sdb_data_free_datum(&v1);
-               return INT_MAX;
+               return 0;
        }
 
-       if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
-               status = INT_MAX;
-       else if (v1.type == v2.type)
-               status = sdb_data_cmp(&v1, &v2);
-       else
-               status = sdb_data_strcmp(&v1, &v2);
+       status = cmp_value(m->type, &v1, &v2,
+                       (e1->data_type) < 0 || (e2->data_type < 0));
 
        sdb_data_free_datum(&v1);
        sdb_data_free_datum(&v2);
        return status;
-} /* cmp_expr */
-
-static int
-match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
-               sdb_store_matcher_t *filter)
-{
-       int status;
-       assert(m->type == MATCHER_LT);
-       status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
-       return (status != INT_MAX) && (status < 0);
-} /* match_lt */
-
-static int
-match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
-               sdb_store_matcher_t *filter)
-{
-       int status;
-       assert(m->type == MATCHER_LE);
-       status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
-       return (status != INT_MAX) && (status <= 0);
-} /* match_le */
-
-static int
-match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
-               sdb_store_matcher_t *filter)
-{
-       int status;
-       assert(m->type == MATCHER_EQ);
-       status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
-       return (status != INT_MAX) && (! status);
-} /* match_eq */
-
-static int
-match_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
-               sdb_store_matcher_t *filter)
-{
-       int status;
-       assert(m->type == MATCHER_NE);
-       status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
-       return (status != INT_MAX) && status;
-} /* match_ne */
-
-static int
-match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
-               sdb_store_matcher_t *filter)
-{
-       int status;
-       assert(m->type == MATCHER_GE);
-       status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
-       return (status != INT_MAX) && (status >= 0);
-} /* match_ge */
-
-static int
-match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
-               sdb_store_matcher_t *filter)
-{
-       int status;
-       assert(m->type == MATCHER_GT);
-       status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
-       return (status != INT_MAX) && (status > 0);
-} /* match_gt */
+} /* match_cmp */
 
 static int
 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
@@ -423,16 +309,14 @@ matchers[] = {
        match_logical,
        match_logical,
        match_unary,
-       match_name,
-       match_child,
-       match_child,
-       match_child,
-       match_lt,
-       match_le,
-       match_eq,
-       match_ne,
-       match_ge,
-       match_gt,
+       match_iter,
+       match_iter,
+       match_cmp,
+       match_cmp,
+       match_cmp,
+       match_cmp,
+       match_cmp,
+       match_cmp,
        match_in,
        match_regex,
        match_regex,
@@ -444,55 +328,6 @@ matchers[] = {
  * private matcher types
  */
 
-/* initializes a string matcher consuming two elements from ap */
-static int
-string_matcher_init(string_matcher_t *m, va_list ap)
-{
-       const char *name = va_arg(ap, const char *);
-       const char *name_re = va_arg(ap, const char *);
-
-       if (name) {
-               m->name = strdup(name);
-               if (! m->name)
-                       return -1;
-       }
-       if (name_re) {
-               m->name_re = malloc(sizeof(*m->name_re));
-               if (! m->name_re)
-                       return -1;
-               if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
-                       return -1;
-       }
-       return 0;
-} /* string_matcher_init */
-
-static void
-string_matcher_destroy(string_matcher_t *m)
-{
-       if (m->name)
-               free(m->name);
-       if (m->name_re) {
-               regfree(m->name_re);
-               free(m->name_re);
-       }
-} /* string_matcher_destroy */
-
-/* initializes a name matcher */
-static int
-name_matcher_init(sdb_object_t *obj, va_list ap)
-{
-       name_matcher_t *m = NAME_M(obj);
-       M(obj)->type = MATCHER_NAME;
-       return string_matcher_init(&m->name, ap);
-} /* name_matcher_init */
-
-static void
-name_matcher_destroy(sdb_object_t *obj)
-{
-       name_matcher_t *m = NAME_M(obj);
-       string_matcher_destroy(&m->name);
-} /* name_matcher_destroy */
-
 static int
 op_matcher_init(sdb_object_t *obj, va_list ap)
 {
@@ -520,23 +355,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)
@@ -601,12 +437,6 @@ isnull_matcher_destroy(sdb_object_t *obj)
        ISNULL_M(obj)->expr = NULL;
 } /* isnull_matcher_destroy */
 
-static sdb_type_t name_type = {
-       /* size = */ sizeof(name_matcher_t),
-       /* init = */ name_matcher_init,
-       /* destroy = */ name_matcher_destroy,
-};
-
 static sdb_type_t op_type = {
        /* size = */ sizeof(op_matcher_t),
        /* init = */ op_matcher_init,
@@ -619,10 +449,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 = {
@@ -642,35 +472,24 @@ static sdb_type_t isnull_type = {
  */
 
 sdb_store_matcher_t *
-sdb_store_name_matcher(int type, const char *name, _Bool re)
+sdb_store_any_matcher(int type, sdb_store_matcher_t *m)
 {
-       sdb_store_matcher_t *m;
-
-       if (re)
-               m = M(sdb_object_create("name-matcher", name_type, NULL, name));
-       else
-               m = M(sdb_object_create("name-matcher", name_type, name, NULL));
-
-       if (! m)
+       if ((type != SDB_SERVICE) && (type != SDB_METRIC)
+                       && (type != SDB_ATTRIBUTE))
                return NULL;
-
-       NAME_M(m)->obj_type = type;
-       return m;
-} /* sdb_store_name_matcher */
+       return M(sdb_object_create("any-matcher", iter_type,
+                               MATCHER_ANY, type, m));
+} /* sdb_store_any_matcher */
 
 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_all_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("all-matcher", iter_type,
+                               MATCHER_ALL, type, m));
+} /* sdb_store_all_matcher */
 
 sdb_store_matcher_t *
 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
@@ -830,73 +649,6 @@ sdb_store_parse_field_name(const char *name)
        return -1;
 } /* sdb_store_parse_field_name */
 
-static sdb_store_matcher_t *
-maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
-{
-       sdb_store_matcher_t *tmp;
-
-       if ((! m) || (! inv))
-               return m;
-
-       tmp = sdb_store_inv_matcher(m);
-       /* pass ownership to the inverse matcher */
-       sdb_object_deref(SDB_OBJ(m));
-       return tmp;
-} /* maybe_inv_matcher */
-
-sdb_store_matcher_t *
-sdb_store_matcher_parse_cmp(const char *obj_type,
-               const char *op, sdb_store_expr_t *expr)
-{
-       int type = -1;
-       _Bool inv = 0;
-       _Bool re = 0;
-
-       sdb_data_t value = SDB_DATA_INIT;
-       sdb_store_matcher_t *m = NULL;
-
-       if (! strcasecmp(obj_type, "host"))
-               type = SDB_HOST;
-       else if (! strcasecmp(obj_type, "service"))
-               type = SDB_SERVICE;
-       else if (! strcasecmp(obj_type, "metric"))
-               type = SDB_METRIC;
-       else if (! strcasecmp(obj_type, "attribute"))
-               type = SDB_ATTRIBUTE;
-       else
-               return NULL;
-
-       /* XXX: this code sucks! */
-       if (! strcasecmp(op, "=")) {
-               /* nothing to do */
-       }
-       else if (! strcasecmp(op, "!=")) {
-               inv = 1;
-       }
-       else if (! strcasecmp(op, "=~")) {
-               re = 1;
-       }
-       else if (! strcasecmp(op, "!~")) {
-               inv = 1;
-               re = 1;
-       }
-       else
-               return NULL;
-
-       if (! expr)
-               return NULL;
-
-       if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
-                       || (value.type != SDB_TYPE_STRING)) {
-               sdb_data_free_datum(&value);
-               return NULL;
-       }
-
-       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 */
-
 sdb_store_matcher_t *
 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
 {
@@ -934,16 +686,5 @@ sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
        return matchers[m->type](m, obj, filter);
 } /* sdb_store_matcher_matches */
 
-int
-sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
-               sdb_store_lookup_cb cb, void *user_data)
-{
-       scan_iter_data_t data = { m, filter, cb, user_data };
-
-       if (! cb)
-               return -1;
-       return sdb_store_iterate(scan_iter, &data);
-} /* sdb_store_scan */
-
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */