index 1630d3a2487c956292abf0522561d6d9848e241c..a95f5877d38011dff4788e4655e8616bc27d420a 100644 (file)
--- a/src/core/store_lookup.c
+++ b/src/core/store_lookup.c
#include <stdlib.h>
#include <string.h>
-/*
- * private data types
- */
+#include <limits.h>
-typedef struct {
- sdb_store_matcher_t *m;
- sdb_store_lookup_cb cb;
- void *user_data;
-} lookup_iter_data_t;
+static int
+expr_eval2(sdb_store_expr_t *e1, sdb_data_t *v1,
+ sdb_store_expr_t *e2, sdb_data_t *v2,
+ sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
+{
+ if (e1->type) {
+ if (sdb_store_expr_eval(e1, obj, v1, filter))
+ return -1;
+ }
+ else
+ *v1 = e1->data;
+ if (e2->type) {
+ if (sdb_store_expr_eval(e2, obj, v2, filter)) {
+ if (e1->type)
+ sdb_data_free_datum(v1);
+ return -1;
+ }
+ }
+ else
+ *v2 = e2->data;
+ return 0;
+} /* expr_eval2 */
+
+static void
+expr_free_datum2(sdb_store_expr_t *e1, sdb_data_t *v1,
+ sdb_store_expr_t *e2, sdb_data_t *v2)
+{
+ if (e1->type)
+ sdb_data_free_datum(v1);
+ if (e2->type)
+ sdb_data_free_datum(v2);
+} /* expr_free_datum2 */
/*
- * private helper functions
+ * 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
-lookup_iter(sdb_store_base_t *obj, void *user_data)
+match_cmp_value(int op, sdb_data_t *v1, sdb_data_t *v2, bool strcmp_fallback)
{
- lookup_iter_data_t *d = user_data;
+ int status;
- if (sdb_store_matcher_matches(d->m, obj))
- return d->cb(obj, d->user_data);
- return 0;
-} /* lookup_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 if (! strcmp_fallback)
+ status = INT_MAX;
+ else
+ status = sdb_data_strcmp(v1, v2);
-/*
- * matcher implementations
- */
+ if (status == INT_MAX)
+ return 0;
+ switch (op) {
+ case MATCHER_LT: return status < 0;
+ case MATCHER_LE: return status <= 0;
+ case MATCHER_EQ: return status == 0;
+ case MATCHER_NE: return status != 0;
+ case MATCHER_GE: return status >= 0;
+ case MATCHER_GT: return status > 0;
+ }
+ return 0;
+} /* match_cmp_value */
static int
-match_string(string_matcher_t *m, const char *name)
+match_regex_value(int op, sdb_data_t *v, sdb_data_t *re)
{
- if ((! m->name) && (! m->name_re))
- return 1;
+ char value[sdb_data_strlen(v) + 1];
+ int status = 0;
- if (! name)
- name = "";
+ assert((op == MATCHER_REGEX)
+ || (op == MATCHER_NREGEX));
- if (m->name && strcasecmp(m->name, name))
+ if (sdb_data_isnull(v) || sdb_data_isnull(re))
return 0;
- if (m->name_re && regexec(m->name_re, name,
- /* matches */ 0, NULL, /* flags = */ 0))
+
+ if (re->type == SDB_TYPE_STRING) {
+ sdb_data_t tmp = SDB_DATA_INIT;
+
+ if (sdb_data_parse(re->data.string, SDB_TYPE_REGEX, &tmp))
+ return 0;
+
+ sdb_data_free_datum(re);
+ *re = tmp;
+ }
+ else if (re->type != SDB_TYPE_REGEX)
return 0;
- return 1;
-} /* match_string */
+
+ if (sdb_data_format(v, value, sizeof(value), SDB_UNQUOTED) < 0)
+ status = 0;
+ else if (! regexec(&re->data.re.regex, value, 0, NULL, 0))
+ status = 1;
+
+ if (op == MATCHER_NREGEX)
+ return !status;
+ 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_base_t *obj)
+match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+ sdb_store_matcher_t *filter)
{
int status;
assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
assert(OP_M(m)->left && OP_M(m)->right);
- status = sdb_store_matcher_matches(OP_M(m)->left, obj);
+ status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
+
/* lazy evaluation */
if ((! status) && (m->type == MATCHER_AND))
return status;
else if (status && (m->type == MATCHER_OR))
return status;
- return sdb_store_matcher_matches(OP_M(m)->right, obj);
+ return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
} /* match_logical */
static int
-match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+ sdb_store_matcher_t *filter)
{
assert(m->type == MATCHER_NOT);
assert(UOP_M(m)->op);
- return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
+ return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
} /* match_unary */
+/* iterate arrays: ANY/ALL <array> <cmp> <value> */
static int
-match_name(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+match_iter_array(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+ sdb_store_matcher_t *filter)
{
- sdb_llist_iter_t *iter = NULL;
- int status = 0;
+ sdb_store_expr_t *e1, *e2;
+ sdb_data_t v1 = SDB_DATA_INIT;
+ sdb_data_t v2 = SDB_DATA_INIT;
- assert(m->type == MATCHER_NAME);
+ int status;
- switch (NAME_M(m)->obj_type) {
- case SDB_HOST:
- return match_string(&NAME_M(m)->name, obj->super.name);
- break;
- case SDB_SERVICE:
- iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
- break;
- case SDB_ATTRIBUTE:
- iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
- break;
- }
+ if ((ITER_M(m)->m->type < MATCHER_LT)
+ || (MATCHER_NREGEX < ITER_M(m)->m->type))
+ return 0;
- while (sdb_llist_iter_has_next(iter)) {
- sdb_store_base_t *child = STORE_BASE(sdb_llist_iter_get_next(iter));
- if (match_string(&NAME_M(m)->name, child->super.name)) {
- status = 1;
- break;
+ e1 = CMP_M(ITER_M(m)->m)->left;
+ e2 = CMP_M(ITER_M(m)->m)->right;
+
+ if (expr_eval2(e1, &v1, e2, &v2, obj, filter))
+ return 0;
+
+ if ((! (v1.type & SDB_TYPE_ARRAY)) || (v2.type & SDB_TYPE_ARRAY))
+ status = 0;
+ else if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
+ status = 0;
+ else {
+ size_t i;
+ int all = (int)(m->type == MATCHER_ALL);
+
+ status = all;
+ for (i = 0; i < v1.data.array.length; ++i) {
+ sdb_data_t v = SDB_DATA_INIT;
+ if (sdb_data_array_get(&v1, i, &v)) {
+ status = 0;
+ break;
+ }
+
+ if (match_value(ITER_M(m)->m->type, &v, &v2,
+ (e1->data_type) < 0 || (e2->data_type < 0))) {
+ if (! all) {
+ status = 1;
+ break;
+ }
+ }
+ else if (all) {
+ status = 0;
+ break;
+ }
}
}
- sdb_llist_iter_destroy(iter);
+
+ expr_free_datum2(e1, &v1, e2, &v2);
return status;
-} /* match_name */
+} /* match_iter_array */
static int
-match_attr(sdb_store_matcher_t *m, sdb_store_base_t *obj)
+match_iter(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+ sdb_store_matcher_t *filter)
{
- sdb_llist_iter_t *iter = NULL;
- int status = 0;
-
- assert(m->type == MATCHER_ATTR);
- assert(ATTR_M(m)->name);
-
- iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
- while (sdb_llist_iter_has_next(iter)) {
- sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(iter));
- char buf[sdb_data_strlen(&attr->value) + 1];
+ sdb_avltree_iter_t *iter = NULL;
+ int status;
+ int all = (int)(m->type == MATCHER_ALL);
+
+ assert((m->type == MATCHER_ANY) || (m->type == MATCHER_ALL));
+
+ if (ITER_M(m)->type == SDB_FIELD_BACKEND)
+ return match_iter_array(m, obj, filter);
+
+ if (obj->type == SDB_HOST) {
+ if (ITER_M(m)->type == SDB_SERVICE)
+ iter = sdb_avltree_get_iter(HOST(obj)->services);
+ else if (ITER_M(m)->type == SDB_METRIC)
+ iter = sdb_avltree_get_iter(HOST(obj)->metrics);
+ else if (ITER_M(m)->type == SDB_ATTRIBUTE)
+ iter = sdb_avltree_get_iter(HOST(obj)->attributes);
+ } 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);
+ }
- if (strcasecmp(ATTR_M(m)->name, SDB_OBJ(attr)->name))
+ 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_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
- return 0;
- if (match_string(&ATTR_M(m)->value, buf)) {
- status = 1;
+ if (sdb_store_matcher_matches(ITER_M(m)->m, child, filter)) {
+ if (! all) {
+ status = 1;
+ break;
+ }
+ } else if (all) {
+ status = 0;
break;
}
}
- sdb_llist_iter_destroy(iter);
+ sdb_avltree_iter_destroy(iter);
return status;
-} /* match_attr */
+} /* match_iter */
-typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
+static int
+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;
-/* 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_attr,
-};
+ 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));
-/*
- * private matcher types
- */
+ if (expr_eval2(e1, &v1, e2, &v2, obj, filter))
+ return 0;
+
+ status = match_cmp_value(m->type, &v1, &v2,
+ (e1->data_type) < 0 || (e2->data_type < 0));
+
+ expr_free_datum2(e1, &v1, e2, &v2);
+ return status;
+} /* match_cmp */
-/* initializes a string matcher consuming two elements from ap */
static int
-string_matcher_init(string_matcher_t *m, va_list ap)
+match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+ sdb_store_matcher_t *filter)
{
- const char *name = va_arg(ap, const char *);
- const char *name_re = va_arg(ap, const char *);
+ sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
+ int status = 1;
- 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 */
+ assert(m->type == MATCHER_IN);
-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 */
+ if (expr_eval2(CMP_M(m)->left, &value,
+ CMP_M(m)->right, &array, obj, filter))
+ status = 0;
-static char *
-string_tostring(string_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;
-} /* string_tostring */
+ if (status)
+ status = sdb_data_inarray(&value, &array);
+
+ expr_free_datum2(CMP_M(m)->left, &value, CMP_M(m)->right, &array);
+ return status;
+} /* match_in */
-/* initializes a name matcher */
static int
-name_matcher_init(sdb_object_t *obj, va_list ap)
+match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+ sdb_store_matcher_t *filter)
{
- name_matcher_t *m = NAME_M(obj);
- M(obj)->type = MATCHER_NAME;
- return string_matcher_init(&m->name, ap);
-} /* name_matcher_init */
+ sdb_data_t regex = SDB_DATA_INIT, v = SDB_DATA_INIT;
+ int status = 0;
-static void
-name_matcher_destroy(sdb_object_t *obj)
-{
- name_matcher_t *m = NAME_M(obj);
- string_matcher_destroy(&m->name);
-} /* name_matcher_destroy */
+ assert((m->type == MATCHER_REGEX)
+ || (m->type == MATCHER_NREGEX));
-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(NAME_M(m)->obj_type),
- string_tostring(&NAME_M(m)->name, name, sizeof(name)));
- return buf;
-} /* name_tostring */
+ if (expr_eval2(CMP_M(m)->left, &v, CMP_M(m)->right, ®ex, obj, filter))
+ return 0;
+
+ status = match_regex_value(m->type, &v, ®ex);
+
+ expr_free_datum2(CMP_M(m)->left, &v, CMP_M(m)->right, ®ex);
+ return status;
+} /* match_regex */
static int
-attr_matcher_init(sdb_object_t *obj, va_list ap)
+match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+ sdb_store_matcher_t *filter)
{
- attr_matcher_t *attr = ATTR_M(obj);
- const char *name = va_arg(ap, const char *);
+ sdb_data_t v = SDB_DATA_INIT;
+ int status;
- M(obj)->type = MATCHER_ATTR;
- if (name) {
- attr->name = strdup(name);
- if (! attr->name)
- return -1;
+ assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
+
+ if (ISNULL_M(m)->expr->type) {
+ /* TODO: this might hide real errors;
+ * improve error reporting and propagation */
+ if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter))
+ return 1;
}
- return string_matcher_init(&attr->value, ap);
-} /* attr_matcher_init */
+ else
+ v = ISNULL_M(m)->expr->data;
-static void
-attr_matcher_destroy(sdb_object_t *obj)
-{
- attr_matcher_t *attr = ATTR_M(obj);
- if (attr->name)
- free(attr->name);
- attr->name = NULL;
- string_matcher_destroy(&attr->value);
-} /* attr_matcher_destroy */
+ if (sdb_data_isnull(&v))
+ status = 1;
+ else
+ status = 0;
-static char *
-attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
- char value[buflen + 1];
+ if (ISNULL_M(m)->expr->type)
+ sdb_data_free_datum(&v);
+ if (m->type == MATCHER_ISNNULL)
+ return !status;
+ return status;
+} /* match_isnull */
- if (! m) {
- snprintf(buf, buflen, "ATTR{}");
- return buf;
- }
+typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
+ sdb_store_matcher_t *);
- assert(m->type == MATCHER_ATTR);
- snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
- string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
- return buf;
-} /* attr_tostring */
+/* 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_iter,
+ match_iter,
+ match_in,
+
+ /* unary operators */
+ match_isnull,
+ match_isnull,
+
+ /* ary operators */
+ match_cmp,
+ match_cmp,
+ match_cmp,
+ match_cmp,
+ match_cmp,
+ match_cmp,
+ match_regex,
+ match_regex,
+};
+
+/*
+ * private matcher types
+ */
static int
op_matcher_init(sdb_object_t *obj, va_list ap)
sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
} /* op_matcher_destroy */
-static char *
-op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+static int
+iter_matcher_init(sdb_object_t *obj, va_list ap)
{
- char left[buflen + 1], right[buflen + 1];
+ M(obj)->type = va_arg(ap, int);
+ ITER_M(obj)->type = va_arg(ap, int);
+ ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
- if (! m) {
- /* this should not happen */
- snprintf(buf, buflen, "()");
- return buf;
- }
+ if (! ITER_M(obj)->m)
+ return -1;
- 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;
-} /* op_tostring */
+ sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
+ return 0;
+} /* iter_matcher_init */
+
+static void
+iter_matcher_destroy(sdb_object_t *obj)
+{
+ sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
+} /* iter_matcher_destroy */
+
+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 int
uop_matcher_init(sdb_object_t *obj, va_list ap)
sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
} /* uop_matcher_destroy */
-static char *
-uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+static int
+isnull_matcher_init(sdb_object_t *obj, va_list ap)
{
- char op[buflen + 1];
-
- if (! m) {
- /* this should not happen */
- snprintf(buf, buflen, "()");
- return buf;
- }
+ M(obj)->type = va_arg(ap, int);
+ if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
+ return -1;
- assert(m->type == MATCHER_NOT);
- snprintf(buf, buflen, "(NOT, %s)",
- sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
- return buf;
-} /* uop_tostring */
-
-static sdb_type_t name_type = {
- /* size = */ sizeof(name_matcher_t),
- /* init = */ name_matcher_init,
- /* destroy = */ name_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 sdb_type_t attr_type = {
- /* size = */ sizeof(attr_matcher_t),
- /* init = */ attr_matcher_init,
- /* destroy = */ attr_matcher_destroy,
-};
+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 op_type = {
/* size = */ sizeof(op_matcher_t),
/* destroy = */ uop_matcher_destroy,
};
-typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
+static sdb_type_t iter_type = {
+ /* size = */ sizeof(iter_matcher_t),
+ /* init = */ iter_matcher_init,
+ /* destroy = */ iter_matcher_destroy,
+};
-/* this array needs to be indexable by the matcher types;
- * -> update the enum in store-private.h when updating this */
-static matcher_tostring_cb matchers_tostring[] = {
- op_tostring,
- op_tostring,
- uop_tostring,
- name_tostring,
- attr_tostring,
+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,
};
/*
*/
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 ((type != SDB_SERVICE) && (type != SDB_METRIC)
+ && (type != SDB_ATTRIBUTE) && (type != SDB_FIELD_BACKEND))
+ return NULL;
+ return M(sdb_object_create("any-matcher", iter_type,
+ MATCHER_ANY, type, m));
+} /* sdb_store_any_matcher */
- if (! m)
+sdb_store_matcher_t *
+sdb_store_all_matcher(int type, sdb_store_matcher_t *m)
+{
+ if ((type != SDB_SERVICE) && (type != SDB_METRIC)
+ && (type != SDB_ATTRIBUTE) && (type != SDB_FIELD_BACKEND))
return NULL;
+ return M(sdb_object_create("all-matcher", iter_type,
+ MATCHER_ALL, type, m));
+} /* sdb_store_all_matcher */
- NAME_M(m)->obj_type = type;
- return m;
-} /* sdb_store_name_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_attr_matcher(const char *name, const char *value, _Bool re)
+sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
{
- if (! name)
- return NULL;
+ return M(sdb_object_create("le-matcher", cmp_type,
+ MATCHER_LE, left, right));
+} /* sdb_store_le_matcher */
- if (re)
- return M(sdb_object_create("attr-matcher", attr_type,
- name, NULL, value));
- return M(sdb_object_create("attr-matcher", attr_type,
- name, value, NULL));
-} /* sdb_store_attr_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_matcher_parse_cmp(const char *obj_type, const char *attr,
- const char *op, const char *value)
+sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
{
- int type = -1;
- _Bool inv = 0;
- _Bool re = 0;
+ return M(sdb_object_create("ne-matcher", cmp_type,
+ MATCHER_NE, left, right));
+} /* sdb_store_ne_matcher */
- sdb_store_matcher_t *m = NULL;
+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 */
- if (! strcasecmp(obj_type, "host"))
- type = SDB_HOST;
- else if (! strcasecmp(obj_type, "service"))
- type = SDB_SERVICE;
- else if (! strcasecmp(obj_type, "attribute"))
- type = SDB_ATTRIBUTE;
+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 */
- /* TODO: support other operators */
- 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;
+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 */
- if (! strcasecmp(attr, "name"))
- m = sdb_store_name_matcher(type, value, re);
- else if (type == SDB_ATTRIBUTE)
- m = sdb_store_attr_matcher(attr, value, re);
+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;
-
- 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;
- }
+ m->type = MATCHER_NREGEX;
return m;
-} /* sdb_store_matcher_parse_cmp */
+} /* sdb_store_nregex_matcher */
+
+sdb_store_matcher_t *
+sdb_store_isnull_matcher(sdb_store_expr_t *expr)
+{
+ return M(sdb_object_create("isnull-matcher", isnull_type,
+ MATCHER_ISNULL, expr));
+} /* sdb_store_isnull_matcher */
+
+sdb_store_matcher_t *
+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(const char *name)
+{
+ if (! strcasecmp(name, "host"))
+ return SDB_HOST;
+ else if (! strcasecmp(name, "service"))
+ return SDB_SERVICE;
+ else if (! strcasecmp(name, "metric"))
+ return SDB_METRIC;
+ else if (! strcasecmp(name, "attribute"))
+ return SDB_ATTRIBUTE;
+ return -1;
+} /* sdb_store_parse_object_type */
+
+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;
+ else if (! strcasecmp(name, "attributes"))
+ return SDB_ATTRIBUTE;
+ 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 */
sdb_store_matcher_t *
sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
} /* 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 (obj->type != SDB_HOST)
+ if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
return 0;
/* "NULL" always matches */
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)
-{
- lookup_iter_data_t data = { m, cb, user_data };
-
- if (! cb)
- return -1;
- return sdb_store_iterate(lookup_iter, &data);
-} /* sdb_store_lookup */
-
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */