summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: a077fb0)
raw | patch | inline | side by side (parent: a077fb0)
author | Sebastian Harl <sh@tokkee.org> | |
Mon, 28 Jul 2014 20:25:04 +0000 (22:25 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Mon, 28 Jul 2014 20:25:04 +0000 (22:25 +0200) |
A filter may be used to preselect objects when evaluating a query. Any object
that's used during the evaluation of a matcher will be compared against the
filter. Only matching objects will be considered further.
that's used during the evaluation of a matcher will be compared against the
filter. Only matching objects will be considered further.
index 30091a75a466e28351383be46a3e61883999f70a..703bc55e476c0b3ef2663ec28ac63098a0d2399b 100644 (file)
--- a/src/core/store-private.h
+++ b/src/core/store-private.h
* conditionals
*/
-/* compares a host using the specified conditional */
-typedef int (*cmp_cb)(sdb_host_t *, sdb_store_cond_t *);
+/* compares a host using the specified conditional and taking the specified
+ * filter into account */
+typedef int (*cmp_cb)(sdb_host_t *, sdb_store_cond_t *,
+ sdb_store_matcher_t *);
struct sdb_store_cond {
sdb_object_t super;
index 758ddd053c7a742e61ac85fbf15e9f51819c86df..ac0f2e8c794c61c36a9d0aa34cff47deb2a9c939 100644 (file)
--- a/src/core/store_lookup.c
+++ b/src/core/store_lookup.c
typedef struct {
sdb_store_matcher_t *m;
+ sdb_store_matcher_t *filter;
sdb_store_lookup_cb cb;
void *user_data;
} scan_iter_data_t;
{
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;
} /* scan_iter */
static sdb_attribute_t *
-attr_get(sdb_host_t *host, const char *name)
+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;
break;
}
sdb_avltree_iter_destroy(iter);
+
+ if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(attr),
+ NULL)))
+ return NULL;
return attr;
} /* attr_get */
*/
static int
-attr_cmp(sdb_host_t *host, sdb_store_cond_t *cond)
+attr_cmp(sdb_host_t *host, sdb_store_cond_t *cond,
+ sdb_store_matcher_t *filter)
{
sdb_attribute_t *attr;
sdb_data_t value = SDB_DATA_INIT;
if (sdb_store_expr_eval(ATTR_C(cond)->expr, &value))
return INT_MAX;
- attr = attr_get(host, ATTR_C(cond)->name);
+ attr = attr_get(host, ATTR_C(cond)->name, filter);
if (! attr)
status = INT_MAX;
else if (attr->value.type != value.type)
} /* match_string */
static int
-match_logical(sdb_store_matcher_t *m, sdb_host_t *host)
+match_logical(sdb_store_matcher_t *m, sdb_host_t *host,
+ 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, STORE_OBJ(host));
+ status = sdb_store_matcher_matches(OP_M(m)->left, STORE_OBJ(host),
+ 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, STORE_OBJ(host));
+ return sdb_store_matcher_matches(OP_M(m)->right, STORE_OBJ(host), filter);
} /* match_logical */
static int
-match_unary(sdb_store_matcher_t *m, sdb_host_t *host)
+match_unary(sdb_store_matcher_t *m, sdb_host_t *host,
+ sdb_store_matcher_t *filter)
{
assert(m->type == MATCHER_NOT);
assert(UOP_M(m)->op);
- return !sdb_store_matcher_matches(UOP_M(m)->op, STORE_OBJ(host));
+ return !sdb_store_matcher_matches(UOP_M(m)->op, STORE_OBJ(host), filter);
} /* match_unary */
static int
-match_name(sdb_store_matcher_t *m, sdb_host_t *host)
+match_name(sdb_store_matcher_t *m, sdb_host_t *host,
+ sdb_store_matcher_t *filter)
{
sdb_avltree_iter_t *iter = NULL;
int status = 0;
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;
} /* match_name */
static int
-match_attr(sdb_store_matcher_t *m, sdb_host_t *host)
+match_attr(sdb_store_matcher_t *m, sdb_host_t *host,
+ sdb_store_matcher_t *filter)
{
sdb_attribute_t *attr;
assert(m->type == MATCHER_ATTR);
assert(ATTR_M(m)->name);
- attr = attr_get(host, ATTR_M(m)->name);
+ attr = attr_get(host, 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)
} /* match_attr */
static int
-match_lt(sdb_store_matcher_t *m, sdb_host_t *host)
+match_lt(sdb_store_matcher_t *m, sdb_host_t *host,
+ sdb_store_matcher_t *filter)
{
int status;
assert(m->type == MATCHER_LT);
- status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
+ status = COND_M(m)->cond->cmp(host, COND_M(m)->cond, filter);
return (status != INT_MAX) && (status < 0);
} /* match_lt */
static int
-match_le(sdb_store_matcher_t *m, sdb_host_t *host)
+match_le(sdb_store_matcher_t *m, sdb_host_t *host,
+ sdb_store_matcher_t *filter)
{
int status;
assert(m->type == MATCHER_LE);
- status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
+ status = COND_M(m)->cond->cmp(host, COND_M(m)->cond, filter);
return (status != INT_MAX) && (status <= 0);
} /* match_le */
static int
-match_eq(sdb_store_matcher_t *m, sdb_host_t *host)
+match_eq(sdb_store_matcher_t *m, sdb_host_t *host,
+ sdb_store_matcher_t *filter)
{
int status;
assert(m->type == MATCHER_EQ);
- status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
+ status = COND_M(m)->cond->cmp(host, COND_M(m)->cond, filter);
return (status != INT_MAX) && (! status);
} /* match_eq */
static int
-match_ge(sdb_store_matcher_t *m, sdb_host_t *host)
+match_ge(sdb_store_matcher_t *m, sdb_host_t *host,
+ sdb_store_matcher_t *filter)
{
int status;
assert(m->type == MATCHER_GE);
- status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
+ status = COND_M(m)->cond->cmp(host, COND_M(m)->cond, filter);
return (status != INT_MAX) && (status >= 0);
} /* match_ge */
static int
-match_gt(sdb_store_matcher_t *m, sdb_host_t *host)
+match_gt(sdb_store_matcher_t *m, sdb_host_t *host,
+ sdb_store_matcher_t *filter)
{
int status;
assert(m->type == MATCHER_GT);
- status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
+ status = COND_M(m)->cond->cmp(host, COND_M(m)->cond, filter);
return (status != INT_MAX) && (status > 0);
} /* match_gt */
static int
-match_isnull(sdb_store_matcher_t *m, sdb_host_t *host)
+match_isnull(sdb_store_matcher_t *m, sdb_host_t *host,
+ sdb_store_matcher_t *filter)
{
assert(m->type == MATCHER_ISNULL);
- return attr_get(host, ISNULL_M(m)->attr_name) == NULL;
+ return attr_get(host, ISNULL_M(m)->attr_name, filter) == NULL;
} /* match_isnull */
-typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_host_t *);
+typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_host_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 */
} /* sdb_store_inv_matcher */
int
-sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_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)
return 0;
+ 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, HOST(obj));
+ return matchers[m->type](m, HOST(obj), filter);
} /* sdb_store_matcher_matches */
char *
} /* sdb_store_matcher_tostring */
int
-sdb_store_scan(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)
{
- scan_iter_data_t data = { m, cb, user_data };
+ scan_iter_data_t data = { m, filter, cb, user_data };
if (! cb)
return -1;
diff --git a/src/frontend/query.c b/src/frontend/query.c
index 2a6aecff6b1513856f22c9929646bb64c34367c7..675b3960a48c7680a75a1a0ef1c34d808635144d 100644 (file)
--- a/src/frontend/query.c
+++ b/src/frontend/query.c
sdb_strbuf_append(buf, "[");
- if (sdb_store_scan(m, lookup_tojson, buf)) {
+ if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, buf)) {
sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
sdb_strbuf_destroy(buf);
index 574b01ed262adcfd88d2a6be63afdff7285300a9..578fb51e7087e72b634cb60f4b44cc32738a5b9d 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
/*
* sdb_store_matcher_matches:
- * Check whether the specified matcher matches the specified store object.
+ * Check whether the specified matcher matches the specified store object. If
+ * specified, the filter will be used to preselect objects for further
+ * evaluation. It is applied to any object that's used during the evaluation
+ * of the matcher. Only those objects matching the filter will be considered.
+ *
+ * Note that the filter is applied to all object types (hosts, service,
+ * attribute). Thus, any object-specific matchers are mostly unsuited for this
+ * purpose and, if used, may result in unexpected behavior.
*
* Returns:
* - 1 if the object matches
* - 0 else
*/
int
-sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj);
+sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
+ sdb_store_matcher_t *filter);
/*
* sdb_store_matcher_tostring:
* sdb_store_scan:
* Look up objects in the store. The specified callback function is called for
* each object in the store matching 'm'. The function performs a full scan of
- * all hosts stored in the database.
+ * all hosts stored in the database. If specified, the filter will be used to
+ * preselect objects for further evaluation. See the description of
+ * 'sdb_store_matcher_matches' for details.
*
* Returns:
* - 0 on success
* - a negative value else
*/
int
-sdb_store_scan(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);
/*
* Flags for serialization functions.
index 27c03d7b71dcffd6ee3afe1249985de3893bf348..c6eeb6bc3ae4d83b2cc6c323063795c5fe04083d 100644 (file)
m = sdb_store_name_matcher(golden_data[i].type,
golden_data[i].name, golden_data[i].re);
fail_unless(m != NULL,
- "sdb_store_service_matcher(%d, %s, %d) = NULL; expected: <matcher>",
- golden_data[i].type, golden_data[i].name, golden_data[i].re);
+ "sdb_store_service_matcher(%d, %s, %d) = NULL; "
+ "expected: <matcher>", golden_data[i].type,
+ golden_data[i].name, golden_data[i].re);
- status = sdb_store_matcher_matches(m, obj);
+ status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
fail_unless(status == golden_data[i].expected,
- "sdb_store_matcher_matches(%s, <host a>) = %d; expected: %d",
+ "sdb_store_matcher_matches(%s, <host a>, NULL) = %d; "
+ "expected: %d",
sdb_store_matcher_tostring(m, buf, sizeof(buf)),
status, golden_data[i].expected);
sdb_object_deref(SDB_OBJ(m));
/* now match the inverted set of objects */
- status = sdb_store_matcher_matches(n, obj);
+ status = sdb_store_matcher_matches(n, obj, /* filter */ NULL);
fail_unless(status == !golden_data[i].expected,
- "sdb_store_matcher_matches(%s, <host a>) = %d; expected: %d",
+ "sdb_store_matcher_matches(%s, <host a>, NULL) = %d; "
+ "expected: %d",
sdb_store_matcher_tostring(n, buf, sizeof(buf)),
status, !golden_data[i].expected);
fail_unless(m != NULL,
"sdb_store_attr_matcher() = NULL; expected: <matcher>");
- status = sdb_store_matcher_matches(m, obj);
+ status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
fail_unless(status == golden_data[i].expected,
- "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
+ "sdb_store_matcher_matches({%s, <host a>, NULL) = %d; "
+ "expected: %d",
sdb_store_matcher_tostring(m, buf, sizeof(buf)),
status, golden_data[i].expected);
sdb_object_deref(SDB_OBJ(m));
/* now match the inverted set of objects */
- status = sdb_store_matcher_matches(n, obj);
+ status = sdb_store_matcher_matches(n, obj, /* filter */ NULL);
fail_unless(status == !golden_data[i].expected,
- "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
+ "sdb_store_matcher_matches({%s, <host a>, NULL) = %d; "
+ "expected: %d",
sdb_store_matcher_tostring(n, buf, sizeof(buf)),
status, !golden_data[i].expected);
fail_unless(m != NULL,
"sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
- status = sdb_store_matcher_matches(m, obj);
+ status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
fail_unless(status == *tests[j].expected,
- "sdb_store_matcher_matches(%s) = %d; expected: %d",
+ "sdb_store_matcher_matches(%s, <obj>, NULL) = %d; "
+ "expected: %d",
sdb_store_matcher_tostring(m, m_str, sizeof(m_str)),
status, *tests[j].expected);
obj = sdb_store_get_host("a");
- status = sdb_store_matcher_matches(always, obj);
+ status = sdb_store_matcher_matches(always, obj, /* filter */ NULL);
fail_unless(status == 1,
"INTERNAL ERROR: 'always' did not match host");
- status = sdb_store_matcher_matches(never, obj);
+ status = sdb_store_matcher_matches(never, obj, /* filter */ NULL);
fail_unless(status == 0,
"INTERNAL ERROR: 'never' matches host");
#define TO_NAME(v) (((v) == always) ? "always" \
: ((v) == never) ? "never" : "<unknown>")
- status = sdb_store_matcher_matches(m, obj);
+ status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
fail_unless(status == golden_data[i].expected,
- "%s(%s, %s) = %d; expected: %d", golden_data[i].op,
+ "%s(%s, %s, NULL) = %d; expected: %d", golden_data[i].op,
TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
status, golden_data[i].expected);
#define PTR_RE "0x[0-9a-f]+"
struct {
const char *query;
+ const char *filter;
int expected;
const char *tostring_re;
} golden_data[] = {
- { "host = 'a'", 1,
+ { "host = 'a'", NULL, 1,
+ "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
+ { "host = 'a'", "host = 'b'", 0,
"OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
- { "host =~ 'a|b'", 2,
+ { "host = 'a'",
+ "attribute.x IS NULL", 1,
+ "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
+ { "host =~ 'a|b'", NULL, 2,
"OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
- { "host =~ 'host'", 0,
+ { "host =~ 'host'", NULL, 0,
"OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
- { "host =~ '.'", 3,
+ { "host =~ '.'", NULL, 3,
"OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
- { "service = 's1'", 2,
+ { "service = 's1'", NULL, 2,
"OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
- { "service =~ 's'", 2,
+ { "service =~ 's'", NULL, 2,
"OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
- { "service !~ 's'", 1,
+ { "service !~ 's'", NULL, 1,
"\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
- { "attribute = 'k1'", 1,
+ { "attribute = 'k1'", NULL, 1,
"OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
- { "attribute = 'x'", 0,
+ { "attribute = 'x'", NULL, 0,
"OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
- { "attribute.k1 = 'v1'", 1,
+ { "attribute.k1 = 'v1'", NULL, 1,
"ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
- { "attribute.k1 IS NULL", 2,
+ { "attribute.k1 IS NULL", NULL, 2,
"\\(IS NULL, ATTR\\[k1\\]\\)" },
- { "attribute.x1 IS NULL", 3,
+ { "attribute.x1 IS NULL", NULL, 3,
"\\(IS NULL, ATTR\\[x1\\]\\)" },
- { "attribute.k1 IS NOT NULL", 1,
+ { "attribute.k1 IS NOT NULL", NULL, 1,
"\\(NOT, \\(IS NULL, ATTR\\[k1\\]\\)\\)" },
- { "attribute.x1 IS NOT NULL", 0,
+ { "attribute.x1 IS NOT NULL", NULL, 0,
"\\(NOT, \\(IS NULL, ATTR\\[x1\\]\\)\\)" },
- { "attribute.k2 < 123", 0,
+ { "attribute.k2 < 123", NULL, 0,
"ATTR\\[k2\\]\\{ < 123 \\}" },
- { "attribute.k2 <= 123", 1,
+ { "attribute.k2 <= 123", NULL, 1,
"ATTR\\[k2\\]\\{ <= 123 \\}" },
- { "attribute.k2 >= 123", 1,
+ { "attribute.k2 >= 123", NULL, 1,
"ATTR\\[k2\\]\\{ >= 123 \\}" },
- { "attribute.k2 > 123", 0,
+ { "attribute.k2 > 123", NULL, 0,
"ATTR\\[k2\\]\\{ > 123 \\}" },
- { "attribute.k2 = 123", 1,
+ { "attribute.k2 = 123", NULL, 1,
"ATTR\\[k2\\]\\{ = 123 \\}" },
- { "attribute.k2 != 123", 2,
+ { "attribute.k2 != 123", NULL, 2,
"\\(NOT, ATTR\\[k2\\]\\{ = 123 \\}\\)" },
- { "attribute.k1 != 'v1'", 2,
+ { "attribute.k1 != 'v1'", NULL, 2,
"\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
- { "attribute.k1 != 'v2'", 3,
+ { "attribute.k1 != 'v2'", NULL, 3,
"\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
{ "attribute != 'x' "
- "AND attribute.y !~ 'x'", 3,
+ "AND attribute.y !~ 'x'", NULL, 3,
"\\(AND, "
"\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
"\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },
size_t i;
n = 0;
- check = sdb_store_scan(NULL, scan_cb, &n);
+ check = sdb_store_scan(/* matcher */ NULL, /* filter */ NULL,
+ scan_cb, &n);
fail_unless(check == 0,
"sdb_store_scan() = %d; expected: 0", check);
fail_unless(n == 3,
"sdb_store_scan called callback %d times; expected: 3", (int)n);
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
- sdb_store_matcher_t *m;
+ sdb_store_matcher_t *m, *filter = NULL;
char buf[4096];
m = sdb_fe_parse_matcher(golden_data[i].query, -1);
fail_unless(m != NULL,
"sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
golden_data[i].query);
+
+ if (golden_data[i].filter) {
+ filter = sdb_fe_parse_matcher(golden_data[i].filter, -1);
+ fail_unless(filter != NULL,
+ "sdb_fe_parse_matcher(%s, -1) = NULL; "
+ "expected: <matcher>", golden_data[i].filter);
+ }
+
fail_unless(sdb_regmatches(golden_data[i].tostring_re,
sdb_store_matcher_tostring(m, buf, sizeof(buf))) == 0,
"sdb_fe_parse_matcher(%s, -1) = %s; expected: %s",
golden_data[i].tostring_re);
n = 0;
- sdb_store_scan(m, scan_cb, &n);
+ sdb_store_scan(m, filter, scan_cb, &n);
fail_unless(n == golden_data[i].expected,
- "sdb_store_scan(matcher{%s}) found %d hosts; expected: %d",
- golden_data[i].query, n, golden_data[i].expected);
+ "sdb_store_scan(matcher{%s}, filter{NULL}) found %d hosts; "
+ "expected: %d", golden_data[i].query, n,
+ golden_data[i].expected);
+ sdb_object_deref(SDB_OBJ(filter));
sdb_object_deref(SDB_OBJ(m));
}
}