X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcore%2Fstore_lookup.c;h=3d509b0511f026276cfa55194aafad34435164e2;hb=8915b40a414bfdc17f893056b523670d63ca3057;hp=089b99fb125756a0712506f8de9fbe34bc7edab4;hpb=d31d779192a8f55a2d4899c2a78bf007cee887cb;p=sysdb.git diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index 089b99f..3d509b0 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -31,279 +31,660 @@ * simple full table scan is supported only. */ +#if HAVE_CONFIG_H +# include "config.h" +#endif /* HAVE_CONFIG_H */ + #include "sysdb.h" #include "core/store-private.h" +#include "core/object.h" #include #include #include +#include #include -/* - * private data types - */ - -/* match the name of something */ -typedef struct { - const char *name; - regex_t *name_re; -} name_matcher_t; - -/* matcher base type */ -typedef struct { - /* type of the matcher */ - int type; -} matcher_t; -#define M(m) ((matcher_t *)(m)) - -/* logical operator matcher */ -typedef struct { - matcher_t super; - - /* left and right hand operands */ - matcher_t *left; - matcher_t *right; -} op_matcher_t; -#define OP_M(m) ((op_matcher_t *)(m)) - -/* match any type of object by it's base information */ -typedef struct { - matcher_t super; - - /* match by the name of the object */ - name_matcher_t name; -} obj_matcher_t; -#define OBJ_M(m) ((obj_matcher_t *)(m)) - -/* match attributes */ -typedef struct { - obj_matcher_t super; - /* XXX: this needs to be more flexible; - * add support for type-specific operators */ - name_matcher_t value; -} attr_matcher_t; -#define ATTR_M(m) ((attr_matcher_t *)(m)) - -/* match services */ -typedef struct { - obj_matcher_t super; - /* match by attributes assigned to the service */ - attr_matcher_t *attr; -} service_matcher_t; -#define SERVICE_M(m) ((service_matcher_t *)(m)) - -/* match hosts */ -typedef struct { - obj_matcher_t super; - /* match by services assigned to the host */ - service_matcher_t *service; - /* match by attributes assigned to the host */ - attr_matcher_t *attr; -} host_matcher_t; -#define HOST_M(m) ((host_matcher_t *)(m)) +#include /* * 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_logical(matcher_t *m, sdb_store_base_t *obj); -static int -match_obj(matcher_t *m, sdb_store_base_t *obj); +cmp_value(int op, sdb_data_t *v1, sdb_data_t *v2, _Bool strcmp_fallback) +{ + int status; -/* specific matchers */ + 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 (status == INT_MAX) + return 0; + 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_name(name_matcher_t *m, const char *name) +match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj, + sdb_store_matcher_t *filter) { - assert(m); + int status; - if ((! m->name) && (! m->name_re)) - return 0; + assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR)); + assert(OP_M(m)->left && OP_M(m)->right); - if (! name) - name = ""; + status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter); - if (m->name && (! strcasecmp(m->name, name))) - return 0; - if (m->name_re && (! regexec(m->name_re, name, - /* matches */ 0, NULL, /* flags = */ 0))) - return 0; - return -1; -} /* match_name */ + /* 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, filter); +} /* match_logical */ -/* 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_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj, + sdb_store_matcher_t *filter) { - assert(m && obj); + assert(m->type == MATCHER_NOT); + assert(UOP_M(m)->op); - if (obj->type != SDB_ATTRIBUTE) - return -1; + return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter); +} /* match_unary */ - { - sdb_attribute_t *attr = SDB_ATTR(obj); - char buf[sdb_data_strlen(&attr->value) + 1]; +static int +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; + int all = (int)(m->type == MATCHER_ALL); + + assert((m->type == MATCHER_ANY) || (m->type == MATCHER_ALL)); + + 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 (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0) - return -1; - return match_name(&m->value, buf); + 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(ITER_M(m)->m, child, filter)) { + if (! all) { + status = 1; + break; + } + } else if (all) { + status = 0; + break; + } } -} /* match_attr */ + sdb_avltree_iter_destroy(iter); + return status; +} /* match_iter */ -/* match service specific values; - * always call this function through match_obj() */ static int -match_service(service_matcher_t *m, sdb_store_base_t *obj) +match_cmp(sdb_store_matcher_t *m, sdb_store_obj_t *obj, + sdb_store_matcher_t *filter) { - sdb_llist_iter_t *iter; + 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 && obj); + 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 (obj->type != SDB_SERVICE) - return -1; + if (sdb_store_expr_eval(e1, obj, &v1, filter)) + return 0; + if (sdb_store_expr_eval(e2, obj, &v2, filter)) { + sdb_data_free_datum(&v1); + return 0; + } - 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)); + status = cmp_value(m->type, &v1, &v2, + (e1->data_type) < 0 || (e2->data_type < 0)); - /* if any of the attributes matches we found a matching service */ - if (! match_obj(M(m->attr), attr)) { - sdb_llist_iter_destroy(iter); - return 0; - } - } - sdb_llist_iter_destroy(iter); - return -1; -} /* match_service */ + sdb_data_free_datum(&v1); + sdb_data_free_datum(&v2); + return status; +} /* match_cmp */ -/* match host specific values; - * always call this function through match_obj() */ static int -match_host(host_matcher_t *m, sdb_store_base_t *obj) +match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj, + sdb_store_matcher_t *filter) { - sdb_llist_iter_t *iter; - int status; + sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT; + int status = 1; - assert(m && obj); + assert(m->type == MATCHER_IN); - if (obj->type != SDB_HOST) - return -1; + 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); - if (m->service) { - iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children); - status = -1; + sdb_data_free_datum(&value); + sdb_data_free_datum(&array); + return status; +} /* match_in */ + +static int +match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj, + sdb_store_matcher_t *filter) +{ + sdb_data_t v = SDB_DATA_INIT; + int status = 0; + + 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 { - iter = NULL; - status = 0; + 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); } - while (sdb_llist_iter_has_next(iter)) { - sdb_store_base_t *service = STORE_BASE(sdb_llist_iter_get_next(iter)); - /* found a matching service */ - if (! match_obj(M(m->service), service)) { + 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; - break; - } + else if (! regexec(®ex, value, 0, NULL, 0)) + status = 1; } - sdb_llist_iter_destroy(iter); - if (status) - return status; - else if (! m->attr) - return 0; + if (free_regex) + regfree(®ex); + sdb_data_free_datum(&v); + if (m->type == MATCHER_NREGEX) + return !status; + return status; +} /* match_regex */ - 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)); +static int +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; - /* if any attribute matches, we found a matching host */ - if (! match_obj(M(m->attr), attr)) { - sdb_llist_iter_destroy(iter); - return 0; - } - } - sdb_llist_iter_destroy(iter); - return -1; -} /* match_host */ + assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL)); -/* generic matchers */ + /* 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; -enum { - MATCHER_OR, - MATCHER_AND, - MATCHER_ATTR, - MATCHER_SERVICE, - MATCHER_HOST, -}; + sdb_data_free_datum(&v); + if (m->type == MATCHER_ISNNULL) + return !status; + return status; +} /* match_isnull */ -typedef int (*matcher_cb)(matcher_t *, sdb_store_base_t *); +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 */ -static matcher_cb matchers[] = { +/* 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_obj, - match_obj, - match_obj, + match_unary, + match_iter, + match_iter, + match_cmp, + match_cmp, + match_cmp, + match_cmp, + match_cmp, + match_cmp, + match_in, + match_regex, + match_regex, + match_isnull, + match_isnull, }; +/* + * private matcher types + */ + static int -match(matcher_t *m, sdb_store_base_t *obj) +op_matcher_init(sdb_object_t *obj, va_list ap) { - assert(m && obj); - assert((0 <= m->type) - && ((size_t)m->type < SDB_STATIC_ARRAY_LEN(matchers))); + 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)); + + if ((! OP_M(obj)->left) || (! OP_M(obj)->right)) + return -1; + return 0; +} /* op_matcher_init */ - return matchers[m->type](m, obj); -} /* match */ +static void +op_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 */ static int -match_logical(matcher_t *m, sdb_store_base_t *obj) +iter_matcher_init(sdb_object_t *obj, va_list ap) { - int status; + 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 *); - assert(m && obj); - assert(OP_M(m)->left && OP_M(m)->right); + if (! ITER_M(obj)->m) + return -1; - status = match(OP_M(m)->left, obj); - /* lazy evaluation */ - if (status && (m->type == MATCHER_AND)) - return status; - else if ((! status) && (m->type == MATCHER_OR)) - return status; + sdb_object_ref(SDB_OBJ(ITER_M(obj)->m)); + return 0; +} /* iter_matcher_init */ - return match(OP_M(m)->right, obj); -} /* match_logical */ +static void +iter_matcher_destroy(sdb_object_t *obj) +{ + sdb_object_deref(SDB_OBJ(ITER_M(obj)->m)); +} /* iter_matcher_destroy */ static int -match_obj(matcher_t *m, sdb_store_base_t *obj) +cmp_matcher_init(sdb_object_t *obj, va_list ap) { - int status; + M(obj)->type = va_arg(ap, int); - assert(m && obj); + 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)); - status = match_name(&OBJ_M(m)->name, obj->super.name); - if (status) - return status; + if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right)) + return -1; + return 0; +} /* cmp_matcher_init */ - switch (m->type) { - case MATCHER_ATTR: - return match_attr(ATTR_M(m), obj); - break; - case MATCHER_SERVICE: - return match_service(SERVICE_M(m), obj); - break; - case MATCHER_HOST: - return match_host(HOST_M(m), obj); - break; +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) +{ + M(obj)->type = va_arg(ap, int); + if (M(obj)->type != MATCHER_NOT) + return -1; + + UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *); + sdb_object_ref(SDB_OBJ(UOP_M(obj)->op)); + + if (! UOP_M(obj)->op) + return -1; + return 0; +} /* uop_matcher_init */ + +static void +uop_matcher_destroy(sdb_object_t *obj) +{ + if (UOP_M(obj)->op) + sdb_object_deref(SDB_OBJ(UOP_M(obj)->op)); +} /* uop_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; + + 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 op_type = { + /* size = */ sizeof(op_matcher_t), + /* init = */ op_matcher_init, + /* destroy = */ op_matcher_destroy, +}; + +static sdb_type_t uop_type = { + /* size = */ sizeof(uop_matcher_t), + /* init = */ uop_matcher_init, + /* destroy = */ uop_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 = { + /* 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 + */ + +sdb_store_matcher_t * +sdb_store_any_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", iter_type, + MATCHER_ANY, type, m)); +} /* sdb_store_any_matcher */ + +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)) + return NULL; + 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) +{ + 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("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; -} /* match_obj */ +} /* 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) +{ + return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR, + left, right)); +} /* sdb_store_dis_matcher */ + +sdb_store_matcher_t * +sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right) +{ + return M(sdb_object_create("con-matcher", op_type, MATCHER_AND, + left, right)); +} /* sdb_store_con_matcher */ + +sdb_store_matcher_t * +sdb_store_inv_matcher(sdb_store_matcher_t *m) +{ + return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m)); +} /* sdb_store_inv_matcher */ + +int +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; + + if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers))) + return 0; + + return matchers[m->type](m, obj, filter); +} /* sdb_store_matcher_matches */ /* vim: set tw=78 sw=4 ts=4 noexpandtab : */