X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcore%2Fstore_lookup.c;h=c3029c75078db3c0ee6d35b0fcbe602a799f41f6;hb=dfa8850d084337223b41c03385317bf7359c8643;hp=d43b9e32c764c10e0a372dabd71a79d3d30668f2;hpb=c3cffe0ff4c8d37cbe2df874743a59e1ec514c71;p=sysdb.git diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index d43b9e3..c3029c7 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -47,48 +47,130 @@ #include #include +#include + /* * 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 */ + +static sdb_attribute_t * +attr_get(sdb_host_t *host, const char *name, sdb_store_matcher_t *filter) +{ + sdb_avltree_iter_t *iter = NULL; + sdb_attribute_t *attr = NULL; + + iter = sdb_avltree_get_iter(host->attributes); + while (sdb_avltree_iter_has_next(iter)) { + sdb_attribute_t *a = ATTR(sdb_avltree_iter_get_next(iter)); + + if (strcasecmp(name, SDB_OBJ(a)->name)) + continue; + + assert(STORE_OBJ(a)->type == SDB_ATTRIBUTE); + attr = a; + break; + } + sdb_avltree_iter_destroy(iter); + + if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(attr), + NULL))) + return NULL; + return attr; +} /* attr_get */ /* - * matcher implementations + * conditional 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); +attr_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond, + sdb_store_matcher_t *filter) +{ + sdb_attribute_t *attr; + sdb_data_t value = SDB_DATA_INIT; + int status; -/* specific matchers */ + if (obj->type != SDB_HOST) + return INT_MAX; + + if (sdb_store_expr_eval(ATTR_C(cond)->expr, obj, &value)) + return INT_MAX; + + attr = attr_get(HOST(obj), ATTR_C(cond)->name, filter); + if (! attr) + status = INT_MAX; + else if (attr->value.type != value.type) + status = INT_MAX; + else + status = sdb_data_cmp(&attr->value, &value); + sdb_data_free_datum(&value); + return status; +} /* attr_cmp */ static int -match_name(name_matcher_t *m, const char *name) +obj_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond, + sdb_store_matcher_t __attribute__((unused)) *filter) { - assert(m); + sdb_data_t obj_value = SDB_DATA_INIT; + sdb_data_t value = SDB_DATA_INIT; + int status; + + if (sdb_store_get_field(obj, OBJ_C(cond)->field, &obj_value)) + return INT_MAX; + if (sdb_store_expr_eval(OBJ_C(cond)->expr, obj, &value)) + return INT_MAX; + + if (obj_value.type != value.type) { + sdb_data_free_datum(&value); + return INT_MAX; + } + else if (OBJ_C(cond)->field == SDB_FIELD_BACKEND) { + /* this implementation is not actually a conditional but rather checks + * for equality (or rather, existence) only */ + size_t i; + status = INT_MAX; + for (i = 0; i < obj->backends_num; ++i) { + if (! strcasecmp(obj->backends[i], value.data.string)) { + status = 0; + break; + } + } + } + else { + status = sdb_data_cmp(&obj_value, &value); + } + sdb_data_free_datum(&value); + return status; +} /* obj_cmp */ +/* + * matcher implementations + */ + +static int +match_string(string_matcher_t *m, const char *name) +{ if ((! m->name) && (! m->name_re)) return 1; @@ -101,182 +183,251 @@ match_name(name_matcher_t *m, const char *name) /* matches */ 0, NULL, /* flags = */ 0)) return 0; return 1; -} /* match_name */ +} /* match_string */ -/* 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 0; + 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 0; - 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 && obj); - - if (obj->type != SDB_SERVICE) - return 0; - - if (! m->attr) - return 1; - - 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_NOT); + assert(UOP_M(m)->op); - /* if any of the attributes matches we found a matching service */ - if (match_obj(M(m->attr), attr)) { - sdb_llist_iter_destroy(iter); - return 1; - } - } - sdb_llist_iter_destroy(iter); - return 0; -} /* match_service */ + return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter); +} /* match_unary */ -/* 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_name(sdb_store_matcher_t *m, sdb_store_obj_t *obj, + sdb_store_matcher_t *filter) { - sdb_llist_iter_t *iter; - int status; + 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; - if (m->service) { - iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children); - status = 0; - } - else { - iter = NULL; - status = 1; + switch (NAME_M(m)->obj_type) { + case SDB_SERVICE: + 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_avltree_get_iter(HOST(obj)->attributes); + break; } - 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)) { + 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 */ - if (! status) - return status; - else if (! m->attr) - return 1; +static int +match_attr(sdb_store_matcher_t *m, sdb_store_obj_t *obj, + sdb_store_matcher_t *filter) +{ + sdb_attribute_t *attr; - 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_ATTR); + assert(ATTR_M(m)->name); - /* if any attribute matches, we found a matching host */ - if (match_obj(M(m->attr), attr)) { - sdb_llist_iter_destroy(iter); + if (obj->type != SDB_HOST) + return 0; + + attr = attr_get(HOST(obj), ATTR_M(m)->name, filter); + if (attr) { + char buf[sdb_data_strlen(&attr->value) + 1]; + if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0) + return 0; + if (match_string(&ATTR_M(m)->value, buf)) return 1; - } } - sdb_llist_iter_destroy(iter); return 0; -} /* match_host */ +} /* match_attr */ + +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 = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter); + return (status != INT_MAX) && (status < 0); +} /* match_lt */ + +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 = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter); + return (status != INT_MAX) && (status <= 0); +} /* match_le */ + +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 = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter); + return (status != INT_MAX) && (! status); +} /* match_eq */ -/* generic matchers */ +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 = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter); + return (status != INT_MAX) && (status >= 0); +} /* match_ge */ -typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *); +static int +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 = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter); + return (status != INT_MAX) && (status > 0); +} /* match_gt */ + +static int +match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj, + sdb_store_matcher_t *filter) +{ + assert(m->type == MATCHER_ISNULL); + if (obj->type != SDB_HOST) + return 0; + return attr_get(HOST(obj), ISNULL_M(m)->attr_name, filter) == NULL; +} /* 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[] = { +static matcher_cb +matchers[] = { match_logical, match_logical, match_unary, - match_obj, - match_obj, - match_obj, + match_name, + match_attr, + match_lt, + match_le, + match_eq, + match_ge, + match_gt, + match_isnull, }; +/* + * private conditional types + */ + static int -match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj) +attr_cond_init(sdb_object_t *obj, va_list ap) { - int status; + const char *name = va_arg(ap, const char *); + sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *); - assert(m && obj); - assert(OP_M(m)->left && OP_M(m)->right); + if (! name) + return -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; + SDB_STORE_COND(obj)->cmp = attr_cmp; - return sdb_store_matcher_matches(OP_M(m)->right, obj); -} /* match_logical */ + ATTR_C(obj)->name = strdup(name); + if (! ATTR_C(obj)->name) + return -1; + ATTR_C(obj)->expr = expr; + sdb_object_ref(SDB_OBJ(expr)); + return 0; +} /* attr_cond_init */ -static int -match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj) +static void +attr_cond_destroy(sdb_object_t *obj) { - assert(m && obj); - assert(UOP_M(m)->op); - - return !sdb_store_matcher_matches(UOP_M(m)->op, obj); -} /* match_unary */ + if (ATTR_C(obj)->name) + free(ATTR_C(obj)->name); + sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr)); +} /* attr_cond_destroy */ + +static sdb_type_t attr_cond_type = { + /* size = */ sizeof(attr_cond_t), + /* init = */ attr_cond_init, + /* destroy = */ attr_cond_destroy, +}; static int -match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj) +obj_cond_init(sdb_object_t *obj, va_list ap) { - int status; + int field = va_arg(ap, int); + sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *); - assert(m && obj); - - status = match_name(&OBJ_M(m)->name, obj->super.name); - if (! status) - return status; + SDB_STORE_COND(obj)->cmp = obj_cmp; - 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; - } + OBJ_C(obj)->field = field; + OBJ_C(obj)->expr = expr; + sdb_object_ref(SDB_OBJ(expr)); return 0; -} /* match_obj */ +} /* obj_cond_init */ + +static void +obj_cond_destroy(sdb_object_t *obj) +{ + sdb_object_deref(SDB_OBJ(OBJ_C(obj)->expr)); +} /* obj_cond_destroy */ + +static sdb_type_t obj_cond_type = { + /* size = */ sizeof(obj_cond_t), + /* init = */ obj_cond_init, + /* destroy = */ obj_cond_destroy, +}; /* * 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 *); @@ -294,10 +445,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); @@ -305,102 +456,140 @@ 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 */ +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 */ + +/* 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); - return name_matcher_init(&m->name, ap); -} /* obj_matcher_init */ + name_matcher_t *m = NAME_M(obj); + M(obj)->type = MATCHER_NAME; + 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 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 */ static int attr_matcher_init(sdb_object_t *obj, va_list ap) { attr_matcher_t *attr = ATTR_M(obj); - int status; + const char *name = va_arg(ap, const char *); M(obj)->type = MATCHER_ATTR; - - status = obj_matcher_init(obj, ap); - if (! status) - status = name_matcher_init(&attr->value, ap); - return status; + if (name) { + attr->name = strdup(name); + if (! attr->name) + return -1; + } + return string_matcher_init(&attr->value, ap); } /* attr_matcher_init */ static void attr_matcher_destroy(sdb_object_t *obj) { attr_matcher_t *attr = ATTR_M(obj); - - obj_matcher_destroy(obj); - name_matcher_destroy(&attr->value); + if (attr->name) + free(attr->name); + attr->name = NULL; + string_matcher_destroy(&attr->value); } /* attr_matcher_destroy */ -static int -service_matcher_init(sdb_object_t *obj, va_list ap) +static char * +attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen) { - attr_matcher_t *attr; - int status; - - M(obj)->type = MATCHER_SERVICE; + char value[buflen + 1]; - status = obj_matcher_init(obj, ap); - if (status) - return status; + if (! m) { + snprintf(buf, buflen, "ATTR{}"); + return buf; + } - attr = va_arg(ap, attr_matcher_t *); - - sdb_object_ref(SDB_OBJ(attr)); - SERVICE_M(obj)->attr = attr; - return 0; -} /* service_matcher_init */ - -static void -service_matcher_destroy(sdb_object_t *obj) -{ - obj_matcher_destroy(obj); - sdb_object_deref(SDB_OBJ(SERVICE_M(obj)->attr)); -} /* service_matcher_destroy */ + 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 */ static int -host_matcher_init(sdb_object_t *obj, va_list ap) +cond_matcher_init(sdb_object_t *obj, va_list ap) { - service_matcher_t *service; - attr_matcher_t *attr; - int status; - - M(obj)->type = MATCHER_HOST; + int type = va_arg(ap, int); + sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *); - status = obj_matcher_init(obj, ap); - if (status) - return status; + if (! cond) + return -1; - service = va_arg(ap, service_matcher_t *); - attr = va_arg(ap, attr_matcher_t *); + sdb_object_ref(SDB_OBJ(cond)); - sdb_object_ref(SDB_OBJ(service)); - HOST_M(obj)->service = service; - sdb_object_ref(SDB_OBJ(attr)); - HOST_M(obj)->attr = attr; + M(obj)->type = type; + COND_M(obj)->cond = cond; return 0; -} /* host_matcher_init */ +} /* cond_matcher_init */ static void -host_matcher_destroy(sdb_object_t *obj) +cond_matcher_destroy(sdb_object_t *obj) { - obj_matcher_destroy(obj); - sdb_object_deref(SDB_OBJ(HOST_M(obj)->service)); - sdb_object_deref(SDB_OBJ(HOST_M(obj)->attr)); -} /* host_matcher_destroy */ + sdb_object_deref(SDB_OBJ(COND_M(obj)->cond)); +} /* cond_matcher_destroy */ + +static char * +cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen) +{ + const char *type, *id; + sdb_data_t value = SDB_DATA_INIT; + char value_str[buflen]; + sdb_store_expr_t *expr; + + if (COND_M(m)->cond->cmp == attr_cmp) { + type = "ATTR"; + id = ATTR_C(COND_M(m)->cond)->name; + expr = ATTR_C(COND_M(m)->cond)->expr; + } + else if (COND_M(m)->cond->cmp == obj_cmp) { + type = "OBJ"; + id = SDB_FIELD_TO_NAME(OBJ_C(COND_M(m)->cond)->field); + expr = OBJ_C(COND_M(m)->cond)->expr; + } + else { + snprintf(buf, buflen, ""); + return buf; + } + + if (sdb_store_expr_eval(expr, NULL, &value)) + snprintf(value_str, sizeof(value_str), "ERR"); + else if (sdb_data_format(&value, value_str, sizeof(value_str), + SDB_SINGLE_QUOTED) < 0) + snprintf(value_str, sizeof(value_str), "ERR"); + snprintf(buf, buflen, "%s[%s]{ %s %s }", type, id, + MATCHER_SYM(m->type), value_str); + sdb_data_free_datum(&value); + return buf; +} /* cond_tostring */ static int op_matcher_init(sdb_object_t *obj, va_list ap) @@ -428,6 +617,25 @@ op_matcher_destroy(sdb_object_t *obj) 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) +{ + 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; +} /* op_tostring */ + static int uop_matcher_init(sdb_object_t *obj, va_list ap) { @@ -450,22 +658,72 @@ uop_matcher_destroy(sdb_object_t *obj) 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) +{ + 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; +} /* uop_tostring */ + +static int +isnull_matcher_init(sdb_object_t *obj, va_list ap) +{ + const char *name; + + M(obj)->type = va_arg(ap, int); + if (M(obj)->type != MATCHER_ISNULL) + return -1; + + name = va_arg(ap, const char *); + if (! name) + return -1; + ISNULL_M(obj)->attr_name = strdup(name); + if (! ISNULL_M(obj)->attr_name) + return -1; + return 0; +} /* isnull_matcher_init */ + +static void +isnull_matcher_destroy(sdb_object_t *obj) +{ + if (ISNULL_M(obj)->attr_name) + free(ISNULL_M(obj)->attr_name); + ISNULL_M(obj)->attr_name = NULL; +} /* isnull_matcher_destroy */ + +static char * +isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen) +{ + snprintf(buf, buflen, "(IS NULL, ATTR[%s])", ISNULL_M(m)->attr_name); + return buf; +} /* isnull_tostring */ + +static sdb_type_t name_type = { + /* size = */ sizeof(name_matcher_t), + /* init = */ name_matcher_init, + /* destroy = */ name_matcher_destroy, +}; + static sdb_type_t attr_type = { /* size = */ sizeof(attr_matcher_t), /* init = */ attr_matcher_init, /* destroy = */ attr_matcher_destroy, }; -static sdb_type_t service_type = { - /* size = */ sizeof(service_matcher_t), - /* init = */ service_matcher_init, - /* destroy = */ service_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 cond_type = { + /* size = */ sizeof(cond_matcher_t), + /* init = */ cond_matcher_init, + /* destroy = */ cond_matcher_destroy, }; static sdb_type_t op_type = { @@ -480,76 +738,300 @@ static sdb_type_t uop_type = { /* destroy = */ uop_matcher_destroy, }; +static sdb_type_t isnull_type = { + /* size = */ sizeof(isnull_matcher_t), + /* init = */ isnull_matcher_init, + /* destroy = */ isnull_matcher_destroy, +}; + +typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t); + +/* 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, + cond_tostring, + cond_tostring, + cond_tostring, + cond_tostring, + cond_tostring, + isnull_tostring, +}; + /* * public API */ +sdb_store_cond_t * +sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr) +{ + return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type, + name, expr)); +} /* sdb_store_attr_cond */ + +sdb_store_cond_t * +sdb_store_obj_cond(int field, sdb_store_expr_t *expr) +{ + return SDB_STORE_COND(sdb_object_create("obj-cond", obj_cond_type, + field, expr)); +} /* sdb_store_obj_cond */ + +sdb_store_matcher_t * +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)); + else + m = M(sdb_object_create("name-matcher", name_type, name, NULL)); + + if (! m) + return NULL; + + 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_attr_matcher(const char *name, const char *value, _Bool re) { - return M(sdb_object_create("attr-matcher", attr_type, - attr_name, attr_name_re, attr_value, attr_value_re)); + sdb_store_matcher_t *m; + + if (! name) + return NULL; + + if (re) + m = M(sdb_object_create("attr-matcher", attr_type, + name, NULL, value)); + else + m = M(sdb_object_create("attr-matcher", attr_type, + name, value, NULL)); + return m; } /* sdb_store_attr_matcher */ sdb_store_matcher_t * -sdb_store_service_matcher(const char *service_name, const char *service_name_re, - sdb_store_matcher_t *attr_matcher) +sdb_store_lt_matcher(sdb_store_cond_t *cond) { - return M(sdb_object_create("service-matcher", service_type, - service_name, service_name_re, attr_matcher)); -} /* sdb_store_service_matcher */ + return M(sdb_object_create("lt-matcher", cond_type, + MATCHER_LT, cond)); +} /* sdb_store_lt_matcher */ sdb_store_matcher_t * -sdb_store_host_matcher(const char *host_name, const char *host_name_re, - sdb_store_matcher_t *service_matcher, - sdb_store_matcher_t *attr_matcher) +sdb_store_le_matcher(sdb_store_cond_t *cond) { - return M(sdb_object_create("host-matcher", host_type, - host_name, host_name_re, service_matcher, attr_matcher)); -} /* sdb_store_host_matcher */ + return M(sdb_object_create("le-matcher", cond_type, + MATCHER_LE, cond)); +} /* sdb_store_le_matcher */ + +sdb_store_matcher_t * +sdb_store_eq_matcher(sdb_store_cond_t *cond) +{ + return M(sdb_object_create("eq-matcher", cond_type, + MATCHER_EQ, cond)); +} /* sdb_store_eq_matcher */ + +sdb_store_matcher_t * +sdb_store_ge_matcher(sdb_store_cond_t *cond) +{ + return M(sdb_object_create("ge-matcher", cond_type, + MATCHER_GE, cond)); +} /* sdb_store_ge_matcher */ + +sdb_store_matcher_t * +sdb_store_gt_matcher(sdb_store_cond_t *cond) +{ + return M(sdb_object_create("gt-matcher", cond_type, + MATCHER_GT, cond)); +} /* sdb_store_gt_matcher */ + +sdb_store_matcher_t * +sdb_store_isnull_matcher(const char *attr_name) +{ + return M(sdb_object_create("isnull-matcher", isnull_type, + MATCHER_ISNULL, attr_name)); +} /* sdb_store_isnull_matcher */ + +int +sdb_store_parse_field_name(const char *name) +{ + 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 */ + +static int +parse_cond_op(const char *op, + sdb_store_matcher_t *(**matcher)(sdb_store_cond_t *), _Bool *inv) +{ + *inv = 0; + if (! strcasecmp(op, "<")) + *matcher = sdb_store_lt_matcher; + else if (! strcasecmp(op, "<=")) + *matcher = sdb_store_le_matcher; + else if (! strcasecmp(op, "=")) + *matcher = sdb_store_eq_matcher; + else if (! strcasecmp(op, ">=")) + *matcher = sdb_store_ge_matcher; + else if (! strcasecmp(op, ">")) + *matcher = sdb_store_gt_matcher; + else if (! strcasecmp(op, "!=")) { + *matcher = sdb_store_eq_matcher; + *inv = 1; + } + else + return -1; + return 0; +} /* parse_cond_op */ + +static sdb_store_matcher_t * +parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr) +{ + sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL; + sdb_store_matcher_t *m; + sdb_store_cond_t *cond; + _Bool inv = 0; + + if (! attr) + return NULL; + + if (! strcasecmp(op, "IS")) { + if (! expr) + return sdb_store_isnull_matcher(attr); + else + return NULL; + } + else if (! expr) + return NULL; + else if (parse_cond_op(op, &matcher, &inv)) + return NULL; + + cond = sdb_store_attr_cond(attr, expr); + if (! cond) + return NULL; + + m = matcher(cond); + /* pass ownership to 'm' or destroy in case of an error */ + sdb_object_deref(SDB_OBJ(cond)); + return maybe_inv_matcher(m, inv); +} /* parse_attr_cmp */ sdb_store_matcher_t * sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, - const char *op, const char *value) + const char *op, sdb_store_expr_t *expr) { - int typ = -1; + int type = -1; + _Bool inv = 0; + _Bool re = 0; - const char *matcher = NULL; - const char *matcher_re = NULL; + sdb_data_t value = SDB_DATA_INIT; + sdb_store_matcher_t *m = NULL; if (! strcasecmp(obj_type, "host")) - typ = SDB_HOST; + type = SDB_HOST; else if (! strcasecmp(obj_type, "service")) - typ = SDB_SERVICE; + type = SDB_SERVICE; + else if (! strcasecmp(obj_type, "metric")) + type = SDB_METRIC; else if (! strcasecmp(obj_type, "attribute")) - typ = SDB_ATTRIBUTE; - - /* TODO: support other operators */ - if (! strcasecmp(op, "=")) - matcher = value; - else if (! strcasecmp(op, "=~")) - matcher_re = value; + type = SDB_ATTRIBUTE; else return NULL; - if (! strcasecmp(attr, "name")) { - /* accept */ + /* XXX: this code sucks! */ + if (! strcasecmp(op, "=")) { + /* nothing to do */ + } + else if (! strcasecmp(op, "!=")) { + inv = 1; + } + else if (! strcasecmp(op, "=~")) { + re = 1; } - else if (typ == SDB_ATTRIBUTE) - return sdb_store_attr_matcher(attr, NULL, matcher, matcher_re); + else if (! strcasecmp(op, "!~")) { + inv = 1; + re = 1; + } + else if (type == SDB_ATTRIBUTE) + return parse_attr_cmp(attr, op, expr); else return NULL; - if (typ == SDB_HOST) - return sdb_store_host_matcher(matcher, matcher_re, NULL, NULL); - else if (typ == SDB_SERVICE) - return sdb_store_service_matcher(matcher, matcher_re, NULL); - else if (typ == SDB_ATTRIBUTE) - return sdb_store_attr_matcher(matcher, matcher_re, NULL, NULL); - return NULL; + if (! expr) + return NULL; + + if (sdb_store_expr_eval(expr, NULL, &value)) + return NULL; + if (value.type != SDB_TYPE_STRING) { + sdb_data_free_datum(&value); + return parse_attr_cmp(attr, op, expr); + } + + if (! attr) + m = sdb_store_name_matcher(type, value.data.string, re); + else if (type == SDB_ATTRIBUTE) + m = sdb_store_attr_matcher(attr, value.data.string, re); + + sdb_data_free_datum(&value); + return maybe_inv_matcher(m, inv); } /* sdb_store_matcher_parse_cmp */ +sdb_store_matcher_t * +sdb_store_matcher_parse_field_cmp(const char *name, const char *op, + sdb_store_expr_t *expr) +{ + sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL; + sdb_store_matcher_t *m; + sdb_store_cond_t *cond; + _Bool inv = 0; + + int field; + + if (! expr) + return NULL; + + field = sdb_store_parse_field_name(name); + if (field < 0) + return NULL; + + if (parse_cond_op(op, &matcher, &inv)) + return NULL; + cond = sdb_store_obj_cond(field, expr); + if (! cond) + return NULL; + + assert(matcher); + m = matcher(cond); + /* pass ownership to 'm' or destroy in case of an error */ + sdb_object_deref(SDB_OBJ(cond)); + return maybe_inv_matcher(m, inv); +} /* sdb_store_matcher_parse_field_cmp */ + sdb_store_matcher_t * sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right) { @@ -571,8 +1053,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; @@ -580,19 +1066,31 @@ 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 : */