Code

store: Fixed a typo in the type check of the child matcher.
[sysdb.git] / src / core / store_lookup.c
index 1fe45dfe11f58510cd5ec6a884dc48135130cc5b..6613c21f503b375456661af679a9218485d5ddff 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
+#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;
-} lookup_iter_data_t;
+} scan_iter_data_t;
 
 /*
  * private helper functions
  */
 
 static int
-lookup_iter(sdb_store_base_t *obj, void *user_data)
+scan_iter(sdb_store_obj_t *obj, void *user_data)
 {
-       lookup_iter_data_t *d = user_data;
+       scan_iter_data_t *d = user_data;
 
-       if (sdb_store_matcher_matches(d->m, obj))
+       if (sdb_store_matcher_matches(d->m, obj, d->filter))
                return d->cb(obj, d->user_data);
        return 0;
-} /* lookup_iter */
+} /* scan_iter */
 
 /*
  * matcher implementations
  */
 
 static int
-match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj);
-static int
-match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj);
-static int
-match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj);
-
-/* specific matchers */
-
-static int
-match_obj_name(name_matcher_t *m, const char *name)
+match_string(string_matcher_t *m, const char *name)
 {
-       assert(m);
-
        if ((! m->name) && (! m->name_re))
                return 1;
 
@@ -101,267 +93,348 @@ match_obj_name(name_matcher_t *m, const char *name)
                                        /* matches */ 0, NULL, /* flags = */ 0))
                return 0;
        return 1;
-} /* match_obj_name */
-
-static char *
-obj_name_tostring(name_matcher_t *m, char *buf, size_t buflen)
-{
-       snprintf(buf, buflen, "{ %s%s%s, %p }",
-                       m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
-                       m->name_re);
-       return buf;
-} /* obj_name_tostring */
+} /* match_string */
 
-static char *
-logical_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
-       char left[buflen + 1], right[buflen + 1];
-
-       if (! m) {
-               /* this should not happen */
-               snprintf(buf, buflen, "()");
-               return buf;
-       }
-
-       assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
-       snprintf(buf, buflen, "(%s, %s, %s)",
-                       m->type == MATCHER_OR ? "OR" : "AND",
-                       sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
-                       sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
-       return buf;
-} /* logical_tostring */
-
-static char *
-unary_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+static int
+match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
 {
-       char op[buflen + 1];
-
-       if (! m) {
-               /* this should not happen */
-               snprintf(buf, buflen, "()");
-               return buf;
-       }
-
-       assert(m->type == MATCHER_NOT);
-       snprintf(buf, buflen, "(NOT, %s)",
-                       sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
-       return buf;
-} /* unary_tostring */
+       int status;
 
-static char *
-name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
-       char name[buflen + 1];
-       assert(m->type == MATCHER_NAME);
-       snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
-                       SDB_STORE_TYPE_TO_NAME(OBJ_M(m)->obj_type),
-                       obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)));
-       return buf;
-} /* name_tostring */
+       assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
+       assert(OP_M(m)->left && OP_M(m)->right);
 
-static char *
-attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
-       char name[buflen + 1], value[buflen + 1];
+       status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
 
-       if (! m) {
-               snprintf(buf, buflen, "ATTR{}");
-               return buf;
-       }
+       /* lazy evaluation */
+       if ((! status) && (m->type == MATCHER_AND))
+               return status;
+       else if (status && (m->type == MATCHER_OR))
+               return status;
 
-       assert(m->type == MATCHER_ATTR);
-       snprintf(buf, buflen, "ATTR{ NAME%s, VALUE%s }",
-                       obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
-                       obj_name_tostring(&ATTR_M(m)->value, value, sizeof(value)));
-       return buf;
-} /* attr_tostring */
+       return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
+} /* match_logical */
 
-static char *
-host_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+static int
+match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
 {
-       char name[buflen + 1], attr[buflen + 1];
-
-       if (! m) {
-               snprintf(buf, buflen, "HOST{}");
-               return buf;
-       }
+       assert(m->type == MATCHER_NOT);
+       assert(UOP_M(m)->op);
 
-       assert(m->type == MATCHER_HOST);
-       snprintf(buf, buflen, "HOST{ NAME%s, %s }",
-                       obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
-                       attr_tostring(SDB_STORE_MATCHER(HOST_M(m)->attr),
-                               attr, sizeof(attr)));
-       return buf;
-} /* host_tostring */
+       return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
+} /* match_unary */
 
 static int
-match_name(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+match_name(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
 {
-       sdb_llist_iter_t *iter = NULL;
+       sdb_avltree_iter_t *iter = NULL;
        int status = 0;
 
-       assert(m && obj);
+       assert(m->type == MATCHER_NAME);
 
-       if (obj->type != SDB_HOST)
+       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 (OBJ_M(m)->obj_type) {
-               case SDB_HOST:
-                       return match_obj_name(&OBJ_M(m)->name, obj->super.name);
-                       break;
+       switch (NAME_M(m)->obj_type) {
                case SDB_SERVICE:
-                       iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
+                       iter = sdb_avltree_get_iter(HOST(obj)->services);
+                       break;
+               case SDB_METRIC:
+                       iter = sdb_avltree_get_iter(HOST(obj)->metrics);
                        break;
                case SDB_ATTRIBUTE:
-                       iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
+                       iter = sdb_avltree_get_iter(HOST(obj)->attributes);
                        break;
        }
 
-       while (sdb_llist_iter_has_next(iter)) {
-               sdb_store_base_t *child = STORE_BASE(sdb_llist_iter_get_next(iter));
-               if (match_obj_name(&OBJ_M(m)->name, child->super.name)) {
+       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_llist_iter_destroy(iter);
+       sdb_avltree_iter_destroy(iter);
        return status;
 } /* match_name */
 
-/* match attribute specific values;
- * always call this function through match_obj() */
 static int
-match_attr(attr_matcher_t *m, sdb_store_base_t *obj)
+match_child(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
 {
-       assert(m && obj);
+       sdb_avltree_iter_t *iter = NULL;
+       int status = 0;
+
+       assert((m->type == MATCHER_SERVICE)
+                       || (m->type == MATCHER_METRIC)
+                       || (m->type == MATCHER_ATTRIBUTE));
 
-       if (obj->type != SDB_ATTRIBUTE)
+       /* TODO: support all object types */
+       if (obj->type != SDB_HOST)
                return 0;
 
-       {
-               sdb_attribute_t *attr = SDB_ATTR(obj);
-               char buf[sdb_data_strlen(&attr->value) + 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);
 
-               if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
-                       return 0;
-               return match_obj_name(&m->value, buf);
+       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;
+               }
        }
-} /* match_attr */
+       sdb_avltree_iter_destroy(iter);
+       return status;
+} /* match_child */
 
-/* match host specific values;
- * always call this function through match_obj() */
+/*
+ * 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
-match_host(host_matcher_t *m, sdb_store_base_t *obj)
+cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
+               sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
 {
-       sdb_llist_iter_t *iter;
-
-       assert(m && obj);
-
-       if (obj->type != SDB_HOST)
-               return 0;
+       sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
+       int status;
 
-       if (! m->attr)
-               return 1;
+       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;
+       }
 
-       iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
-       while (sdb_llist_iter_has_next(iter)) {
-               sdb_store_base_t *attr = STORE_BASE(sdb_llist_iter_get_next(iter));
+       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);
 
-               /* if any attribute matches, we found a matching host */
-               if (match_obj(M(m->attr), attr)) {
-                       sdb_llist_iter_destroy(iter);
-                       return 1;
-               }
-       }
-       sdb_llist_iter_destroy(iter);
-       return 0;
-} /* match_host */
+       sdb_data_free_datum(&v1);
+       sdb_data_free_datum(&v2);
+       return status;
+} /* cmp_expr */
 
-/* generic matchers */
+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 */
 
-typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
+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 */
 
-/* this array needs to be indexable by the matcher types;
- * -> update the enum in store-private.h when updating this */
-static matcher_cb matchers[] = {
-       match_logical,
-       match_logical,
-       match_unary,
-       match_name,
-       match_obj,
-       match_obj,
-};
+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 */
 
-typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
+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 matcher_tostring_cb matchers_tostring[] = {
-       logical_tostring,
-       logical_tostring,
-       unary_tostring,
-       name_tostring,
-       attr_tostring,
-       host_tostring,
-};
+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_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+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 */
 
-       assert(m && obj);
-       assert(OP_M(m)->left && OP_M(m)->right);
+static int
+match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
+{
+       sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
+       int status = 1;
 
-       status = sdb_store_matcher_matches(OP_M(m)->left, obj);
-       /* lazy evaluation */
-       if ((! status) && (m->type == MATCHER_AND))
-               return status;
-       else if (status && (m->type == MATCHER_OR))
-               return status;
+       assert(m->type == MATCHER_IN);
 
-       return sdb_store_matcher_matches(OP_M(m)->right, obj);
-} /* match_logical */
+       if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
+                       || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
+               status = 0;
+
+       if (status)
+               status = sdb_data_inarray(&value, &array);
+
+       sdb_data_free_datum(&value);
+       sdb_data_free_datum(&array);
+       return status;
+} /* match_in */
 
 static int
-match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
 {
-       assert(m && obj);
-       assert(UOP_M(m)->op);
+       sdb_data_t v = SDB_DATA_INIT;
+       int status = 0;
 
-       return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
-} /* match_unary */
+       regex_t regex;
+       _Bool free_regex = 0;
+
+       assert((m->type == MATCHER_REGEX)
+                       || (m->type == MATCHER_NREGEX));
+
+       if (! CMP_M(m)->right->type) {
+               assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
+               regex = CMP_M(m)->right->data.data.re.regex;
+       }
+       else {
+               sdb_data_t tmp = SDB_DATA_INIT;
+               char *raw;
+
+               if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
+                       return 0;
+
+               if (tmp.type != SDB_TYPE_STRING) {
+                       sdb_data_free_datum(&tmp);
+                       return 0;
+               }
+
+               raw = tmp.data.string;
+               if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
+                       free(raw);
+                       return 0;
+               }
+
+               regex = tmp.data.re.regex;
+               free_regex = 1;
+               free(tmp.data.re.raw);
+               free(raw);
+       }
+
+       if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
+                       || (sdb_data_isnull(&v)))
+               status = 0;
+       else {
+               char value[sdb_data_strlen(&v) + 1];
+               if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
+                       status = 0;
+               else if (! regexec(&regex, value, 0, NULL, 0))
+                       status = 1;
+       }
+
+       if (free_regex)
+               regfree(&regex);
+       sdb_data_free_datum(&v);
+       if (m->type == MATCHER_NREGEX)
+               return !status;
+       return status;
+} /* match_regex */
 
 static int
-match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
 {
+       sdb_data_t v = SDB_DATA_INIT;
        int status;
 
-       assert(m && obj);
+       assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
 
-       status = match_obj_name(&OBJ_M(m)->name, obj->super.name);
-       if (! status)
-               return status;
+       /* TODO: this might hide real errors;
+        * improve error reporting and propagation */
+       if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
+                       || sdb_data_isnull(&v))
+               status = 1;
+       else
+               status = 0;
 
-       switch (m->type) {
-               case MATCHER_ATTR:
-                       return match_attr(ATTR_M(m), obj);
-                       break;
-               case MATCHER_HOST:
-                       return match_host(HOST_M(m), obj);
-                       break;
-               default:
-                       assert(m->type != m->type);
-                       break;
-       }
-       return 0;
-} /* match_obj */
+       sdb_data_free_datum(&v);
+       if (m->type == MATCHER_ISNNULL)
+               return !status;
+       return status;
+} /* match_isnull */
+
+typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
+               sdb_store_matcher_t *);
+
+/* this array needs to be indexable by the matcher types;
+ * -> update the enum in store-private.h when updating this */
+static matcher_cb
+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_in,
+       match_regex,
+       match_regex,
+       match_isnull,
+       match_isnull,
+};
 
 /*
  * private matcher types
  */
 
-/* initializes a name matcher consuming two elements from ap */
+/* initializes a string matcher consuming two elements from ap */
 static int
-name_matcher_init(name_matcher_t *m, va_list ap)
+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 *);
@@ -379,10 +452,10 @@ name_matcher_init(name_matcher_t *m, va_list ap)
                        return -1;
        }
        return 0;
-} /* name_matcher_init */
+} /* string_matcher_init */
 
 static void
-name_matcher_destroy(name_matcher_t *m)
+string_matcher_destroy(string_matcher_t *m)
 {
        if (m->name)
                free(m->name);
@@ -390,97 +463,90 @@ name_matcher_destroy(name_matcher_t *m)
                regfree(m->name_re);
                free(m->name_re);
        }
-} /* name_matcher_destroy */
+} /* string_matcher_destroy */
 
-/* initializes an object matcher consuming two elements from ap */
+/* initializes a name matcher */
 static int
-obj_matcher_init(sdb_object_t *obj, va_list ap)
+name_matcher_init(sdb_object_t *obj, va_list ap)
 {
-       obj_matcher_t *m = OBJ_M(obj);
+       name_matcher_t *m = NAME_M(obj);
        M(obj)->type = MATCHER_NAME;
-       return name_matcher_init(&m->name, ap);
-} /* obj_matcher_init */
+       return string_matcher_init(&m->name, ap);
+} /* name_matcher_init */
 
 static void
-obj_matcher_destroy(sdb_object_t *obj)
+name_matcher_destroy(sdb_object_t *obj)
 {
-       obj_matcher_t *m = OBJ_M(obj);
-       name_matcher_destroy(&m->name);
-} /* obj_matcher_destroy */
+       name_matcher_t *m = NAME_M(obj);
+       string_matcher_destroy(&m->name);
+} /* name_matcher_destroy */
 
 static int
-attr_matcher_init(sdb_object_t *obj, va_list ap)
+op_matcher_init(sdb_object_t *obj, va_list ap)
 {
-       attr_matcher_t *attr = ATTR_M(obj);
-       int status;
+       M(obj)->type = va_arg(ap, int);
+       if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
+               return -1;
 
-       status = obj_matcher_init(obj, ap);
-       if (! status)
-               status = name_matcher_init(&attr->value, ap);
+       OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
+       sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
+       OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
+       sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
 
-       M(obj)->type = MATCHER_ATTR;
-       return status;
-} /* attr_matcher_init */
+       if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
+               return -1;
+       return 0;
+} /* op_matcher_init */
 
 static void
-attr_matcher_destroy(sdb_object_t *obj)
+op_matcher_destroy(sdb_object_t *obj)
 {
-       attr_matcher_t *attr = ATTR_M(obj);
-
-       obj_matcher_destroy(obj);
-       name_matcher_destroy(&attr->value);
-} /* attr_matcher_destroy */
+       if (OP_M(obj)->left)
+               sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
+       if (OP_M(obj)->right)
+               sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
+} /* op_matcher_destroy */
 
 static int
-host_matcher_init(sdb_object_t *obj, va_list ap)
+child_matcher_init(sdb_object_t *obj, va_list ap)
 {
-       attr_matcher_t *attr;
-       int status;
-
-       status = obj_matcher_init(obj, ap);
-       if (status)
-               return status;
+       M(obj)->type = va_arg(ap, int);
+       CHILD_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
 
-       attr = va_arg(ap, attr_matcher_t *);
-       sdb_object_ref(SDB_OBJ(attr));
-       HOST_M(obj)->attr = attr;
+       if (! CHILD_M(obj)->m)
+               return -1;
 
-       M(obj)->type = MATCHER_HOST;
+       sdb_object_ref(SDB_OBJ(CHILD_M(obj)->m));
        return 0;
-} /* host_matcher_init */
+} /* child_matcher_init */
 
 static void
-host_matcher_destroy(sdb_object_t *obj)
+child_matcher_destroy(sdb_object_t *obj)
 {
-       obj_matcher_destroy(obj);
-       sdb_object_deref(SDB_OBJ(HOST_M(obj)->attr));
-} /* host_matcher_destroy */
+       sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m));
+} /* child_matcher_destroy */
 
 static int
-op_matcher_init(sdb_object_t *obj, va_list ap)
+cmp_matcher_init(sdb_object_t *obj, va_list ap)
 {
        M(obj)->type = va_arg(ap, int);
-       if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
-               return -1;
 
-       OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
-       sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
-       OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
-       sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
+       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 ((! OP_M(obj)->left) || (! OP_M(obj)->right))
+       if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
                return -1;
        return 0;
-} /* op_matcher_init */
+} /* cmp_matcher_init */
 
 static void
-op_matcher_destroy(sdb_object_t *obj)
+cmp_matcher_destroy(sdb_object_t *obj)
 {
-       if (OP_M(obj)->left)
-               sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
-       if (OP_M(obj)->right)
-               sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
-} /* op_matcher_destroy */
+       sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
+       sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
+} /* cmp_matcher_destroy */
 
 static int
 uop_matcher_init(sdb_object_t *obj, va_list ap)
@@ -504,22 +570,29 @@ uop_matcher_destroy(sdb_object_t *obj)
                sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
 } /* uop_matcher_destroy */
 
-static sdb_type_t name_type = {
-       /* size = */ sizeof(obj_matcher_t),
-       /* init = */ obj_matcher_init,
-       /* destroy = */ obj_matcher_destroy,
-};
+static int
+isnull_matcher_init(sdb_object_t *obj, va_list ap)
+{
+       M(obj)->type = va_arg(ap, int);
+       if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
+               return -1;
 
-static sdb_type_t attr_type = {
-       /* size = */ sizeof(attr_matcher_t),
-       /* init = */ attr_matcher_init,
-       /* destroy = */ attr_matcher_destroy,
-};
+       ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
+       sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
+       return 0;
+} /* isnull_matcher_init */
+
+static void
+isnull_matcher_destroy(sdb_object_t *obj)
+{
+       sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
+       ISNULL_M(obj)->expr = NULL;
+} /* isnull_matcher_destroy */
 
-static sdb_type_t host_type = {
-       /* size = */ sizeof(host_matcher_t),
-       /* init = */ host_matcher_init,
-       /* destroy = */ host_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 = {
@@ -534,6 +607,24 @@ 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,
+       /* destroy = */ isnull_matcher_destroy,
+};
+
 /*
  * public API
  */
@@ -544,97 +635,238 @@ sdb_store_name_matcher(int type, const char *name, _Bool re)
        sdb_store_matcher_t *m;
 
        if (re)
-               m = M(sdb_object_create("name-matcher", name_type,
-                                       NULL, name));
+               m = M(sdb_object_create("name-matcher", name_type, NULL, name));
        else
-               m = M(sdb_object_create("name-matcher", name_type,
-                                       name, NULL));
+               m = M(sdb_object_create("name-matcher", name_type, name, NULL));
 
        if (! m)
                return NULL;
 
-       OBJ_M(m)->obj_type = type;
+       NAME_M(m)->obj_type = type;
        return m;
 } /* sdb_store_name_matcher */
 
 sdb_store_matcher_t *
-sdb_store_attr_matcher(const char *attr_name, const char *attr_name_re,
-               const char *attr_value, const char *attr_value_re)
+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_expr_t *left, sdb_store_expr_t *right)
+{
+       return M(sdb_object_create("lt-matcher", cmp_type,
+                               MATCHER_LT, left, right));
+} /* sdb_store_lt_matcher */
+
+sdb_store_matcher_t *
+sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       return M(sdb_object_create("le-matcher", cmp_type,
+                               MATCHER_LE, left, right));
+} /* sdb_store_le_matcher */
+
+sdb_store_matcher_t *
+sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       return M(sdb_object_create("eq-matcher", cmp_type,
+                               MATCHER_EQ, left, right));
+} /* sdb_store_eq_matcher */
+
+sdb_store_matcher_t *
+sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       return M(sdb_object_create("ne-matcher", cmp_type,
+                               MATCHER_NE, left, right));
+} /* sdb_store_ne_matcher */
+
+sdb_store_matcher_t *
+sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       return M(sdb_object_create("ge-matcher", cmp_type,
+                               MATCHER_GE, left, right));
+} /* sdb_store_ge_matcher */
+
+sdb_store_matcher_t *
+sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       return M(sdb_object_create("gt-matcher", cmp_type,
+                               MATCHER_GT, left, right));
+} /* sdb_store_gt_matcher */
+
+sdb_store_matcher_t *
+sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       return M(sdb_object_create("in-matcher", cmp_type,
+                               MATCHER_IN, left, right));
+} /* sdb_store_in_matcher */
+
+sdb_store_matcher_t *
+sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       if (! right->type) {
+               if ((right->data.type != SDB_TYPE_STRING)
+                               && (right->data.type != SDB_TYPE_REGEX))
+                       return NULL;
+
+               if (right->data.type == SDB_TYPE_STRING) {
+                       char *raw = right->data.data.string;
+                       if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
+                               return NULL;
+                       free(raw);
+               }
+       }
+       return M(sdb_object_create("regex-matcher", cmp_type,
+                               MATCHER_REGEX, left, right));
+} /* sdb_store_regex_matcher */
+
+sdb_store_matcher_t *
+sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+       sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
+       if (! m)
+               return NULL;
+       m->type = MATCHER_NREGEX;
+       return m;
+} /* sdb_store_nregex_matcher */
+
+sdb_store_matcher_t *
+sdb_store_isnull_matcher(sdb_store_expr_t *expr)
 {
-       return M(sdb_object_create("attr-matcher", attr_type,
-                               attr_name, attr_name_re, attr_value, attr_value_re));
-} /* sdb_store_attr_matcher */
+       return M(sdb_object_create("isnull-matcher", isnull_type,
+                               MATCHER_ISNULL, expr));
+} /* sdb_store_isnull_matcher */
 
 sdb_store_matcher_t *
-sdb_store_host_matcher(const char *host_name, const char *host_name_re,
-               sdb_store_matcher_t *attr_matcher)
+sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
+{
+       return M(sdb_object_create("isnull-matcher", isnull_type,
+                               MATCHER_ISNNULL, expr));
+} /* sdb_store_isnnull_matcher */
+
+sdb_store_matcher_op_cb
+sdb_store_parse_matcher_op(const char *op)
+{
+       if (! strcasecmp(op, "<"))
+               return sdb_store_lt_matcher;
+       else if (! strcasecmp(op, "<="))
+               return sdb_store_le_matcher;
+       else if (! strcasecmp(op, "="))
+               return sdb_store_eq_matcher;
+       else if (! strcasecmp(op, "!="))
+               return sdb_store_ne_matcher;
+       else if (! strcasecmp(op, ">="))
+               return sdb_store_ge_matcher;
+       else if (! strcasecmp(op, ">"))
+               return sdb_store_gt_matcher;
+       else if (! strcasecmp(op, "=~"))
+               return sdb_store_regex_matcher;
+       else if (! strcasecmp(op, "!~"))
+               return sdb_store_nregex_matcher;
+       return NULL;
+} /* sdb_store_parse_matcher_op */
+
+int
+sdb_store_parse_object_type_plural(const char *name)
 {
-       return M(sdb_object_create("host-matcher", host_type,
-                               host_name, host_name_re, attr_matcher));
-} /* sdb_store_host_matcher */
+       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, "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;
+       else if (! strcasecmp(name, "interval"))
+               return SDB_FIELD_INTERVAL;
+       else if (! strcasecmp(name, "backend"))
+               return SDB_FIELD_BACKEND;
+       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 *attr,
-               const char *op, const char *value)
+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;
 
-       const char *matcher = NULL;
-       const char *matcher_re = 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;
 
-       /* TODO: support other operators */
+       /* XXX: this code sucks! */
        if (! strcasecmp(op, "=")) {
-               matcher = value;
+               /* nothing to do */
        }
        else if (! strcasecmp(op, "!=")) {
-               matcher = value;
                inv = 1;
        }
        else if (! strcasecmp(op, "=~")) {
-               matcher_re = value;
                re = 1;
        }
        else if (! strcasecmp(op, "!~")) {
-               matcher_re = value;
                inv = 1;
                re = 1;
        }
        else
                return NULL;
 
-       if (! strcasecmp(attr, "name"))
-               m = sdb_store_name_matcher(type, value, re);
-       else if (type == SDB_ATTRIBUTE) {
-               m = sdb_store_host_matcher(/* name = */ NULL, NULL,
-                               sdb_store_attr_matcher(attr, NULL, matcher, matcher_re));
-
-               /* pass ownership to the host matcher */
-               if (m)
-                       sdb_object_deref(SDB_OBJ(HOST_M(m)->attr));
-       }
-
-       if (! m)
+       if (! expr)
                return NULL;
 
-       if (inv) {
-               sdb_store_matcher_t *tmp;
-               tmp = sdb_store_inv_matcher(m);
-               /* pass ownership to the inverse matcher */
-               sdb_object_deref(SDB_OBJ(m));
-               m = tmp;
+       if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
+                       || (value.type != SDB_TYPE_STRING)) {
+               sdb_data_free_datum(&value);
+               return NULL;
        }
-       return m;
+
+       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 *
@@ -658,8 +890,12 @@ sdb_store_inv_matcher(sdb_store_matcher_t *m)
 } /* sdb_store_inv_matcher */
 
 int
-sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter)
 {
+       if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
+               return 0;
+
        /* "NULL" always matches */
        if ((! m) || (! obj))
                return 1;
@@ -667,31 +903,19 @@ sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
        if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
                return 0;
 
-       return matchers[m->type](m, obj);
+       return matchers[m->type](m, obj, filter);
 } /* sdb_store_matcher_matches */
 
-char *
-sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
-       if (! m)
-               return NULL;
-
-       if ((m->type < 0)
-                       || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
-               return NULL;
-       return matchers_tostring[m->type](m, buf, buflen);
-} /* sdb_store_matcher_tostring */
-
 int
-sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
-               void *user_data)
+sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
+               sdb_store_lookup_cb cb, void *user_data)
 {
-       lookup_iter_data_t data = { m, cb, user_data };
+       scan_iter_data_t data = { m, filter, cb, user_data };
 
        if (! cb)
                return -1;
-       return sdb_store_iterate(lookup_iter, &data);
-} /* sdb_store_lookup */
+       return sdb_store_iterate(scan_iter, &data);
+} /* sdb_store_scan */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */