Code

store: Added child matchers.
[sysdb.git] / src / core / store_lookup.c
index c3029c75078db3c0ee6d35b0fcbe602a799f41f6..9968aba468eab75c8dfc8b4da86c915504c1c3e9 100644 (file)
@@ -114,14 +114,14 @@ attr_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond,
        if (obj->type != SDB_HOST)
                return INT_MAX;
 
-       if (sdb_store_expr_eval(ATTR_C(cond)->expr, obj, &value))
+       if (sdb_store_expr_eval(ATTR_C(cond)->expr, obj, &value, filter))
                return INT_MAX;
 
        attr = attr_get(HOST(obj), ATTR_C(cond)->name, filter);
        if (! attr)
                status = INT_MAX;
        else if (attr->value.type != value.type)
-               status = INT_MAX;
+               status = sdb_data_strcmp(&attr->value, &value);
        else
                status = sdb_data_cmp(&attr->value, &value);
        sdb_data_free_datum(&value);
@@ -130,25 +130,23 @@ attr_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond,
 
 static int
 obj_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond,
-               sdb_store_matcher_t __attribute__((unused)) *filter)
+               sdb_store_matcher_t *filter)
 {
        sdb_data_t obj_value = SDB_DATA_INIT;
        sdb_data_t value = SDB_DATA_INIT;
        int status;
 
-       if (sdb_store_get_field(obj, OBJ_C(cond)->field, &obj_value))
-               return INT_MAX;
-       if (sdb_store_expr_eval(OBJ_C(cond)->expr, obj, &value))
+       if (sdb_store_expr_eval(OBJ_C(cond)->expr, obj, &value, filter))
                return INT_MAX;
 
-       if (obj_value.type != value.type) {
-               sdb_data_free_datum(&value);
-               return INT_MAX;
-       }
-       else if (OBJ_C(cond)->field == SDB_FIELD_BACKEND) {
+       if (OBJ_C(cond)->field == SDB_FIELD_BACKEND) {
                /* this implementation is not actually a conditional but rather checks
                 * for equality (or rather, existence) only */
                size_t i;
+
+               if (value.type != SDB_TYPE_STRING)
+                       return INT_MAX;
+
                status = INT_MAX;
                for (i = 0; i < obj->backends_num; ++i) {
                        if (! strcasecmp(obj->backends[i], value.data.string)) {
@@ -156,10 +154,20 @@ obj_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond,
                                break;
                        }
                }
+               sdb_data_free_datum(&value);
+               return status;
        }
-       else {
-               status = sdb_data_cmp(&obj_value, &value);
+
+       if (sdb_store_get_field(obj, OBJ_C(cond)->field, &obj_value))
+               return INT_MAX;
+       if (obj_value.type != value.type) {
+               sdb_data_free_datum(&obj_value);
+               sdb_data_free_datum(&value);
+               return INT_MAX;
        }
+
+       status = sdb_data_cmp(&obj_value, &value);
+       sdb_data_free_datum(&obj_value);
        sdb_data_free_datum(&value);
        return status;
 } /* obj_cmp */
@@ -278,6 +286,43 @@ match_attr(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
        return 0;
 } /* match_attr */
 
+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 = 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;
+
+       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 == SDB_ATTRIBUTE)
+               iter = sdb_avltree_get_iter(HOST(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 (sdb_store_matcher_matches(CHILD_M(m)->m, obj, filter)) {
+                       status = 1;
+                       break;
+               }
+       }
+       sdb_avltree_iter_destroy(iter);
+       return status;
+} /* match_child */
+
 static int
 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
                sdb_store_matcher_t *filter)
@@ -328,6 +373,88 @@ match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
        return (status != INT_MAX) && (status > 0);
 } /* match_gt */
 
+/*
+ * 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.
+ */
+static int
+cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
+               sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
+{
+       sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
+       int status;
+
+       if (sdb_store_expr_eval(e1, obj, &v1, filter))
+               return INT_MAX;
+       if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
+               sdb_data_free_datum(&v1);
+               return INT_MAX;
+       }
+
+       if (v1.type == v2.type)
+               status = sdb_data_cmp(&v1, &v2);
+       else
+               status = sdb_data_strcmp(&v1, &v2);
+
+       sdb_data_free_datum(&v1);
+       sdb_data_free_datum(&v2);
+       return status;
+} /* cmp_expr */
+
+static int
+match_cmp_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
+{
+       int status;
+       assert(m->type == MATCHER_CMP_LT);
+       status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
+       return (status != INT_MAX) && (status < 0);
+} /* match_cmp_lt */
+
+static int
+match_cmp_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
+{
+       int status;
+       assert(m->type == MATCHER_CMP_LE);
+       status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
+       return (status != INT_MAX) && (status <= 0);
+} /* match_cmp_le */
+
+static int
+match_cmp_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
+{
+       int status;
+       assert(m->type == MATCHER_CMP_EQ);
+       status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
+       return (status != INT_MAX) && (! status);
+} /* match_cmp_eq */
+
+static int
+match_cmp_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
+{
+       int status;
+       assert(m->type == MATCHER_CMP_GE);
+       status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
+       return (status != INT_MAX) && (status >= 0);
+} /* match_cmp_ge */
+
+static int
+match_cmp_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
+{
+       int status;
+       assert(m->type == MATCHER_CMP_GT);
+       status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
+       return (status != INT_MAX) && (status > 0);
+} /* match_cmp_gt */
+
 static int
 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
                sdb_store_matcher_t *filter)
@@ -350,11 +477,19 @@ matchers[] = {
        match_unary,
        match_name,
        match_attr,
+       match_child,
+       match_child,
+       match_child,
        match_lt,
        match_le,
        match_eq,
        match_ge,
        match_gt,
+       match_cmp_lt,
+       match_cmp_le,
+       match_cmp_eq,
+       match_cmp_ge,
+       match_cmp_gt,
        match_isnull,
 };
 
@@ -580,7 +715,7 @@ cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
                return buf;
        }
 
-       if (sdb_store_expr_eval(expr, NULL, &value))
+       if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL))
                snprintf(value_str, sizeof(value_str), "ERR");
        else if (sdb_data_format(&value, value_str, sizeof(value_str),
                                SDB_SINGLE_QUOTED) < 0)
@@ -636,6 +771,71 @@ op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
        return buf;
 } /* op_tostring */
 
+static int
+child_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 *);
+
+       if (! CHILD_M(obj)->m)
+               return -1;
+
+       sdb_object_ref(SDB_OBJ(CHILD_M(obj)->m));
+       return 0;
+} /* child_matcher_init */
+
+static void
+child_matcher_destroy(sdb_object_t *obj)
+{
+       sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m));
+} /* child_matcher_destroy */
+
+static char *
+child_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+{
+       snprintf(buf, buflen, "%s:", MATCHER_SYM(m->type));
+       buf[buflen - 1] = '\0';
+       sdb_store_matcher_tostring(CHILD_M(m)->m,
+                       buf + strlen(buf), buflen - strlen(buf));
+       return buf;
+} /* child_tostring */
+
+static int
+cmp_matcher_init(sdb_object_t *obj, va_list ap)
+{
+       M(obj)->type = va_arg(ap, int);
+
+       CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
+       sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
+       CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
+       sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
+
+       if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
+               return -1;
+       return 0;
+} /* cmp_matcher_init */
+
+static void
+cmp_matcher_destroy(sdb_object_t *obj)
+{
+       sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
+       sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
+} /* cmp_matcher_destroy */
+
+static char *
+cmp_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+{
+       if (! m) {
+               /* this should not happen */
+               snprintf(buf, buflen, "()");
+               return buf;
+       }
+
+       /* TODO */
+       snprintf(buf, buflen, "CMP_MATCHER(%d)", m->type);
+       return buf;
+} /* cmp_tostring */
+
 static int
 uop_matcher_init(sdb_object_t *obj, va_list ap)
 {
@@ -738,6 +938,18 @@ 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 cmp_type = {
+       /* size = */ sizeof(cmp_matcher_t),
+       /* init = */ cmp_matcher_init,
+       /* destroy = */ cmp_matcher_destroy,
+};
+
 static sdb_type_t isnull_type = {
        /* size = */ sizeof(isnull_matcher_t),
        /* init = */ isnull_matcher_init,
@@ -755,11 +967,19 @@ matchers_tostring[] = {
        uop_tostring,
        name_tostring,
        attr_tostring,
+       child_tostring,
+       child_tostring,
+       child_tostring,
        cond_tostring,
        cond_tostring,
        cond_tostring,
        cond_tostring,
        cond_tostring,
+       cmp_tostring,
+       cmp_tostring,
+       cmp_tostring,
+       cmp_tostring,
+       cmp_tostring,
        isnull_tostring,
 };
 
@@ -815,6 +1035,20 @@ sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
        return m;
 } /* sdb_store_attr_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
+               return NULL;
+       return M(sdb_object_create("any-matcher", child_type, type, m));
+} /* sdb_store_child_matcher */
+
 sdb_store_matcher_t *
 sdb_store_lt_matcher(sdb_store_cond_t *cond)
 {
@@ -850,6 +1084,46 @@ sdb_store_gt_matcher(sdb_store_cond_t *cond)
                                MATCHER_GT, cond));
 } /* sdb_store_gt_matcher */
 
+/*
+ * TODO: Rename sdb_store_cmp_* to sdb_store_* once the old code is unused and
+ * has been removed.
+ */
+
+sdb_store_matcher_t *
+sdb_store_cmp_lt(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       return M(sdb_object_create("lt-matcher", cmp_type,
+                               MATCHER_CMP_LT, left, right));
+} /* sdb_store_cmp_lt */
+
+sdb_store_matcher_t *
+sdb_store_cmp_le(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       return M(sdb_object_create("le-matcher", cmp_type,
+                               MATCHER_CMP_LE, left, right));
+} /* sdb_store_cmp_le */
+
+sdb_store_matcher_t *
+sdb_store_cmp_eq(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       return M(sdb_object_create("eq-matcher", cmp_type,
+                               MATCHER_CMP_EQ, left, right));
+} /* sdb_store_cmp_eq */
+
+sdb_store_matcher_t *
+sdb_store_cmp_ge(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       return M(sdb_object_create("ge-matcher", cmp_type,
+                               MATCHER_CMP_GE, left, right));
+} /* sdb_store_cmp_ge */
+
+sdb_store_matcher_t *
+sdb_store_cmp_gt(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       return M(sdb_object_create("gt-matcher", cmp_type,
+                               MATCHER_CMP_GT, left, right));
+} /* sdb_store_cmp_gt */
+
 sdb_store_matcher_t *
 sdb_store_isnull_matcher(const char *attr_name)
 {
@@ -857,10 +1131,24 @@ sdb_store_isnull_matcher(const char *attr_name)
                                MATCHER_ISNULL, attr_name));
 } /* sdb_store_isnull_matcher */
 
+int
+sdb_store_parse_object_type_plural(const char *name)
+{
+       if (! strcasecmp(name, "hosts"))
+               return SDB_HOST;
+       else if (! strcasecmp(name, "services"))
+               return SDB_SERVICE;
+       else if (! strcasecmp(name, "metrics"))
+               return SDB_METRIC;
+       return -1;
+} /* sdb_store_parse_object_type_plural */
+
 int
 sdb_store_parse_field_name(const char *name)
 {
-       if (! strcasecmp(name, "last_update"))
+       if (! strcasecmp(name, "name"))
+               return SDB_FIELD_NAME;
+       else if (! strcasecmp(name, "last_update"))
                return SDB_FIELD_LAST_UPDATE;
        else if (! strcasecmp(name, "age"))
                return SDB_FIELD_AGE;
@@ -985,10 +1273,11 @@ sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
        if (! expr)
                return NULL;
 
-       if (sdb_store_expr_eval(expr, NULL, &value))
-               return NULL;
-       if (value.type != SDB_TYPE_STRING) {
+       if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
+                       || (value.type != SDB_TYPE_STRING)) {
                sdb_data_free_datum(&value);
+               if (type != SDB_ATTRIBUTE)
+                       return NULL;
                return parse_attr_cmp(attr, op, expr);
        }