X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcore%2Fstore_lookup.c;h=b0b7bf8f9345644e037264a2abd50b6d14b697f8;hb=d8d03b18f0e0a4c39e2adec3cc2cd093d52c1f93;hp=26e466617dc29244da13a770dcc465554b5b68fd;hpb=5e371c0cd627b5628453813c180fa8d08a523af8;p=sysdb.git diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index 26e4666..f72e26d 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -31,279 +31,715 @@ * 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 "utils/error.h" #include #include #include +#include #include -/* - * private data types - */ +#include -/* 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)) +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 */ /* * 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); +match_cmp_value(int op, sdb_data_t *v1, sdb_data_t *v2, bool strcmp_fallback) +{ + int status; + + 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); -/* specific matchers */ + 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_name(name_matcher_t *m, const char *name) +match_regex_value(int op, sdb_data_t *v, sdb_data_t *re) { - assert(m); + char value[sdb_data_strlen(v) + 1]; + int status = 0; - if ((! m->name) && (! m->name_re)) + assert((op == MATCHER_REGEX) + || (op == MATCHER_NREGEX)); + + if (sdb_data_isnull(v) || sdb_data_isnull(re)) return 0; - if (! name) - name = ""; + if (re->type == SDB_TYPE_STRING) { + sdb_data_t tmp = SDB_DATA_INIT; - if (m->name && strcasecmp(m->name, name)) - return -1; - if (m->name_re && regexec(m->name_re, name, - /* matches */ 0, NULL, /* flags = */ 0)) - return -1; - return 0; -} /* match_name */ + 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; + + if (! sdb_data_format(v, value, sizeof(value), SDB_UNQUOTED)) + 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 */ -/* 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_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj, + sdb_store_matcher_t *filter) { - assert(m && obj); + int status; - if (obj->type != SDB_ATTRIBUTE) - return -1; + assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR)); + assert(OP_M(m)->left && OP_M(m)->right); - { - sdb_attribute_t *attr = SDB_ATTR(obj); - char buf[sdb_data_strlen(&attr->value) + 1]; + status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter); - if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0) - return -1; - return match_name(&m->value, buf); - } -} /* match_attr */ + /* 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 service specific values; - * always call this function through match_obj() */ static int -match_service(service_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) { - sdb_llist_iter_t *iter; + assert(m->type == MATCHER_NOT); + assert(UOP_M(m)->op); - assert(m && obj); + return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter); +} /* match_unary */ - if (obj->type != SDB_SERVICE) - return -1; +/* iterate: ANY/ALL */ +static int +match_iter(sdb_store_matcher_t *m, sdb_store_obj_t *obj, + sdb_store_matcher_t *filter) +{ + sdb_store_expr_iter_t *iter = NULL; + int status; + int all = (int)(m->type == MATCHER_ALL); - 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)); + assert((m->type == MATCHER_ANY) || (m->type == MATCHER_ALL)); + assert((! CMP_M(ITER_M(m)->m)->left) && CMP_M(ITER_M(m)->m)->right); - /* 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; + iter = sdb_store_expr_iter(ITER_M(m)->iter, obj, filter); + if (! iter) + return 0; + + status = all; + while (sdb_store_expr_iter_has_next(iter)) { + sdb_data_t v = sdb_store_expr_iter_get_next(iter); + sdb_store_expr_t expr = CONST_EXPR(v); + bool matches; + + CMP_M(ITER_M(m)->m)->left = &expr; + matches = sdb_store_matcher_matches(ITER_M(m)->m, obj, filter); + CMP_M(ITER_M(m)->m)->left = NULL; + sdb_data_free_datum(&v); + + if (matches) { + if (! all) { + status = 1; + break; + } + } else if (all) { + status = 0; + break; } } - sdb_llist_iter_destroy(iter); - return -1; -} /* match_service */ + sdb_store_expr_iter_destroy(iter); + return status; +} /* match_iter */ -/* 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_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)); + assert(e1 && e2); - if (obj->type != SDB_HOST) - return -1; + if (expr_eval2(e1, &v1, e2, &v2, obj, filter)) + return 0; - if (m->service) { - iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children); - status = -1; - } - else { - iter = NULL; - status = 0; - } - while (sdb_llist_iter_has_next(iter)) { - sdb_store_base_t *service = STORE_BASE(sdb_llist_iter_get_next(iter)); + status = match_cmp_value(m->type, &v1, &v2, + (e1->data_type) < 0 || (e2->data_type < 0)); - /* found a matching service */ - if (! match_obj(M(m->service), service)) { - status = 0; - break; - } - } - sdb_llist_iter_destroy(iter); + expr_free_datum2(e1, &v1, e2, &v2); + return status; +} /* match_cmp */ + +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; + + assert((m->type == MATCHER_IN) || (m->type == MATCHER_NIN)); + assert(CMP_M(m)->left && CMP_M(m)->right); + + if (expr_eval2(CMP_M(m)->left, &value, + CMP_M(m)->right, &array, obj, filter)) + status = 0; if (status) - return status; - else if (! m->attr) + status = sdb_data_inarray(&value, &array); + + expr_free_datum2(CMP_M(m)->left, &value, CMP_M(m)->right, &array); + if (m->type == MATCHER_NIN) + return !status; + 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 regex = SDB_DATA_INIT, v = SDB_DATA_INIT; + int status = 0; + + assert((m->type == MATCHER_REGEX) + || (m->type == MATCHER_NREGEX)); + assert(CMP_M(m)->left && CMP_M(m)->right); + + if (expr_eval2(CMP_M(m)->left, &v, CMP_M(m)->right, ®ex, obj, filter)) 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 = match_regex_value(m->type, &v, ®ex); - /* if any attribute matches, we found a matching host */ - if (! match_obj(M(m->attr), attr)) { - sdb_llist_iter_destroy(iter); - return 0; - } + expr_free_datum2(CMP_M(m)->left, &v, CMP_M(m)->right, ®ex); + return status; +} /* match_regex */ + +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; + + assert(m->type == MATCHER_ISNULL); + + 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; } - sdb_llist_iter_destroy(iter); - return -1; -} /* match_host */ + else + v = ISNULL_M(m)->expr->data; -/* generic matchers */ + if (sdb_data_isnull(&v)) + status = 1; + else + status = 0; -enum { - MATCHER_OR, - MATCHER_AND, - MATCHER_ATTR, - MATCHER_SERVICE, - MATCHER_HOST, -}; + if (ISNULL_M(m)->expr->type) + sdb_data_free_datum(&v); + 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_in, + match_in, + + /* unary operators */ + match_isnull, + + /* ary operators */ + match_cmp, + match_cmp, + match_cmp, + match_cmp, + match_cmp, + match_cmp, + match_regex, + match_regex, + + NULL, /* QUERY */ }; +/* + * 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)->iter = va_arg(ap, sdb_store_expr_t *); + ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *); - assert(m && obj); - assert(OP_M(m)->left && OP_M(m)->right); + sdb_object_ref(SDB_OBJ(ITER_M(obj)->iter)); + sdb_object_ref(SDB_OBJ(ITER_M(obj)->m)); - 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; + if ((! ITER_M(obj)->iter) || (! ITER_M(obj)->m)) + return -1; + 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)->iter)); + 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)->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) + 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(sdb_store_expr_t *iter, sdb_store_matcher_t *m) +{ + if ((m->type < MATCHER_LT) || (MATCHER_NREGEX < m->type)) { + sdb_log(SDB_LOG_ERR, "store: Invalid ANY -> %s matcher " + "(invalid operator)", MATCHER_SYM(m->type)); + return NULL; + } + if (CMP_M(m)->left) { + sdb_log(SDB_LOG_ERR, "store: Invalid ANY %s %s %s matcher " + "(invalid left operand)", + SDB_TYPE_TO_STRING(CMP_M(m)->left->data_type), + MATCHER_SYM(m->type), + SDB_TYPE_TO_STRING(CMP_M(m)->right->data_type)); + return NULL; + } + return M(sdb_object_create("any-matcher", iter_type, + MATCHER_ANY, iter, m)); +} /* sdb_store_any_matcher */ + +sdb_store_matcher_t * +sdb_store_all_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m) +{ + if ((m->type < MATCHER_LT) || (MATCHER_NREGEX < m->type)) { + sdb_log(SDB_LOG_ERR, "store: Invalid ALL -> %s matcher " + "(invalid operator)", MATCHER_SYM(m->type)); + return NULL; } + if (CMP_M(m)->left) { + sdb_log(SDB_LOG_ERR, "store: Invalid ALL %s %s %s matcher " + "(invalid left operand)", + SDB_TYPE_TO_STRING(CMP_M(m)->left->data_type), + MATCHER_SYM(m->type), + SDB_TYPE_TO_STRING(CMP_M(m)->right->data_type)); + return NULL; + } + return M(sdb_object_create("all-matcher", iter_type, + MATCHER_ALL, iter, 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_nin_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right) +{ + return M(sdb_object_create("not-in-matcher", cmp_type, + MATCHER_NIN, 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_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; + else if (! strcasecmp(name, "value")) + return SDB_FIELD_VALUE; return -1; -} /* match_obj */ +} /* 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; + + if (! matchers[m->type]) + return 0; + return matchers[m->type](m, obj, filter); +} /* sdb_store_matcher_matches */ /* vim: set tw=78 sw=4 ts=4 noexpandtab : */