index 9968aba468eab75c8dfc8b4da86c915504c1c3e9..f1baea644a4c2f2b2ee16e18ecc3d531f0e4f723 100644 (file)
--- a/src/core/store_lookup.c
+++ b/src/core/store_lookup.c
* specified stored object and filter. Returns a value less than, equal to, or
* greater than zero if the value of the first expression compares less than,
* equal to, or greater than the value of the second expression. Returns
- * INT_MAX if any of the expressions could not be evaluated.
+ * INT_MAX if any of the expressions could not be evaluated or if any of them
+ * evaluated to NULL.
*/
static int
cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
return INT_MAX;
}
- if (v1.type == v2.type)
+ if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
+ status = INT_MAX;
+ else if (v1.type == v2.type)
status = sdb_data_cmp(&v1, &v2);
else
status = sdb_data_strcmp(&v1, &v2);
return (status != INT_MAX) && (! status);
} /* match_cmp_eq */
+static int
+match_cmp_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+ sdb_store_matcher_t *filter)
+{
+ int status;
+ assert(m->type == MATCHER_CMP_NE);
+ status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
+ return (status != INT_MAX) && status;
+} /* match_cmp_ne */
+
static int
match_cmp_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
sdb_store_matcher_t *filter)
return (status != INT_MAX) && (status > 0);
} /* match_cmp_gt */
+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);
+
+ if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
+ || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
+ status = 0;
+
+ if (status)
+ status = sdb_data_inarray(&value, &array);
+
+ sdb_data_free_datum(&value);
+ sdb_data_free_datum(&array);
+ return status;
+} /* match_in */
+
+static int
+match_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 {
+ sdb_data_t tmp = SDB_DATA_INIT;
+ char *raw;
+
+ if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
+ return 0;
+
+ if (tmp.type != SDB_TYPE_STRING) {
+ sdb_data_free_datum(&tmp);
+ return 0;
+ }
+
+ raw = tmp.data.string;
+ if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
+ free(raw);
+ return 0;
+ }
+
+ regex = tmp.data.re.regex;
+ free_regex = 1;
+ free(tmp.data.re.raw);
+ free(raw);
+ }
+
+ if (sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
+ status = 0;
+ else {
+ char value[sdb_data_strlen(&v) + 1];
+ if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
+ status = 0;
+ else if (! regexec(®ex, value, 0, NULL, 0))
+ status = 1;
+ }
+
+ if (free_regex)
+ regfree(®ex);
+ sdb_data_free_datum(&v);
+ if (m->type == MATCHER_NREGEX)
+ return !status;
+ return status;
+} /* match_regex */
+
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;
+ sdb_data_t v = SDB_DATA_INIT;
+ int status;
+
+ assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
+
+ /* 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;
+
+ sdb_data_free_datum(&v);
+ if (m->type == MATCHER_ISNNULL)
+ return !status;
+ return status;
} /* match_isnull */
typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
match_cmp_lt,
match_cmp_le,
match_cmp_eq,
+ match_cmp_ne,
match_cmp_ge,
match_cmp_gt,
+ match_in,
+ match_regex,
+ match_regex,
+ match_isnull,
match_isnull,
};
}
} /* string_matcher_destroy */
-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
name_matcher_init(sdb_object_t *obj, va_list ap)
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)
{
string_matcher_destroy(&attr->value);
} /* attr_matcher_destroy */
-static char *
-attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
- char value[buflen + 1];
-
- if (! m) {
- snprintf(buf, buflen, "ATTR{}");
- return buf;
- }
-
- 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
cond_matcher_init(sdb_object_t *obj, va_list ap)
{
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, "<unknown>");
- return buf;
- }
-
- if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL))
- 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)
{
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
child_matcher_init(sdb_object_t *obj, va_list ap)
{
sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m));
} /* child_matcher_destroy */
-static char *
-child_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
- snprintf(buf, buflen, "%s:", MATCHER_SYM(m->type));
- buf[buflen - 1] = '\0';
- sdb_store_matcher_tostring(CHILD_M(m)->m,
- buf + strlen(buf), buflen - strlen(buf));
- return buf;
-} /* child_tostring */
-
static int
cmp_matcher_init(sdb_object_t *obj, va_list ap)
{
sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
} /* cmp_matcher_destroy */
-static char *
-cmp_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
- if (! m) {
- /* this should not happen */
- snprintf(buf, buflen, "()");
- return buf;
- }
-
- /* TODO */
- snprintf(buf, buflen, "CMP_MATCHER(%d)", m->type);
- return buf;
-} /* cmp_tostring */
-
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)
-{
- 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)
+ if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
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;
+ 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)
{
- if (ISNULL_M(obj)->attr_name)
- free(ISNULL_M(obj)->attr_name);
- ISNULL_M(obj)->attr_name = NULL;
+ sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
+ ISNULL_M(obj)->expr = 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 = */ 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,
- child_tostring,
- child_tostring,
- child_tostring,
- cond_tostring,
- cond_tostring,
- cond_tostring,
- cond_tostring,
- cond_tostring,
- cmp_tostring,
- cmp_tostring,
- cmp_tostring,
- cmp_tostring,
- cmp_tostring,
- isnull_tostring,
-};
-
/*
* public API
*/
MATCHER_CMP_EQ, left, right));
} /* sdb_store_cmp_eq */
+sdb_store_matcher_t *
+sdb_store_cmp_ne(sdb_store_expr_t *left, sdb_store_expr_t *right)
+{
+ return M(sdb_object_create("ne-matcher", cmp_type,
+ MATCHER_CMP_NE, left, right));
+} /* sdb_store_cmp_ne */
+
sdb_store_matcher_t *
sdb_store_cmp_ge(sdb_store_expr_t *left, sdb_store_expr_t *right)
{
} /* sdb_store_cmp_gt */
sdb_store_matcher_t *
-sdb_store_isnull_matcher(const char *attr_name)
+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, attr_name));
+ 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_cmp_lt;
+ else if (! strcasecmp(op, "<="))
+ return sdb_store_cmp_le;
+ else if (! strcasecmp(op, "="))
+ return sdb_store_cmp_eq;
+ else if (! strcasecmp(op, "!="))
+ return sdb_store_cmp_ne;
+ else if (! strcasecmp(op, ">="))
+ return sdb_store_cmp_ge;
+ else if (! strcasecmp(op, ">"))
+ return sdb_store_cmp_gt;
+ else if (! strcasecmp(op, "=~"))
+ return sdb_store_regex_matcher;
+ else if (! strcasecmp(op, "!~"))
+ return sdb_store_nregex_matcher;
+ return NULL;
+} /* sdb_store_parse_matcher_op */
+
int
sdb_store_parse_object_type_plural(const char *name)
{
if (! attr)
return NULL;
- if (! strcasecmp(op, "IS")) {
- if (! expr)
- return sdb_store_isnull_matcher(attr);
- else
- return NULL;
- }
- else if (! expr)
+ if (! expr)
return NULL;
else if (parse_cond_op(op, &matcher, &inv))
return NULL;
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_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
sdb_store_lookup_cb cb, void *user_data)