summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 64c09ee)
raw | patch | inline | side by side (parent: 64c09ee)
author | Sebastian Harl <sh@tokkee.org> | |
Tue, 14 Oct 2014 20:45:07 +0000 (22:45 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Tue, 14 Oct 2014 20:45:07 +0000 (22:45 +0200) |
src/core/store.c | patch | blob | history | |
src/core/store_expr.c | patch | blob | history | |
src/core/store_lookup.c | patch | blob | history | |
src/include/core/store.h | patch | blob | history |
diff --git a/src/core/store.c b/src/core/store.c
index db8a9d9c5686f7e7c79059fd383c6014ad5329bf..81aedfb7600a9243accbe4e49cceffcf87f8c54b 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
} /* sdb_store_get_field */
int
-sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res)
+sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
+ sdb_store_matcher_t *filter)
{
sdb_avltree_t *tree = NULL;
sdb_avltree_iter_t *iter = NULL;
if (strcasecmp(SDB_OBJ(attr)->name, name))
continue;
+ if (filter&& (! sdb_store_matcher_matches(filter,
+ STORE_OBJ(attr), NULL)))
+ break; /* found it but it's filtered */
+
assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE);
if (res)
sdb_data_copy(res, &ATTR(attr)->value);
diff --git a/src/core/store_expr.c b/src/core/store_expr.c
index ca7b72b52f6a246de610c73de1d0672736057ee0..4e8bae7cad7c980941145938597be86e1bb770f3 100644 (file)
--- a/src/core/store_expr.c
+++ b/src/core/store_expr.c
int
sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
- sdb_data_t *res)
+ sdb_data_t *res, sdb_store_matcher_t *filter)
{
sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
int status = 0;
if ((! expr) || (! res))
return -1;
+ if (filter && obj && (! sdb_store_matcher_matches(filter, obj, NULL)))
+ obj = NULL; /* this object does not exist */
+
if (! expr->type)
return sdb_data_copy(res, &expr->data);
else if (expr->type == FIELD_VALUE)
return sdb_store_get_field(obj, (int)expr->data.data.integer, res);
else if (expr->type == ATTR_VALUE)
- return sdb_store_get_attr(obj, expr->data.data.string, res);
+ return sdb_store_get_attr(obj, expr->data.data.string, res, filter);
- if (sdb_store_expr_eval(expr->left, obj, &v1))
+ if (sdb_store_expr_eval(expr->left, obj, &v1, filter))
return -1;
- if (sdb_store_expr_eval(expr->right, obj, &v2)) {
+ if (sdb_store_expr_eval(expr->right, obj, &v2, filter)) {
sdb_data_free_datum(&v1);
return -1;
}
index 0c9d36199abca8e6db5fea26e34c0c52c80b852f..dc3e49942efb0cd4db2f0a3d7e53aa5542364df7 100644 (file)
--- a/src/core/store_lookup.c
+++ b/src/core/store_lookup.c
if (obj->type != SDB_HOST)
return INT_MAX;
- if (sdb_store_expr_eval(ATTR_C(cond)->expr, obj, &value))
+ if (sdb_store_expr_eval(ATTR_C(cond)->expr, obj, &value, filter))
return INT_MAX;
attr = attr_get(HOST(obj), ATTR_C(cond)->name, filter);
static int
obj_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond,
- sdb_store_matcher_t __attribute__((unused)) *filter)
+ sdb_store_matcher_t *filter)
{
sdb_data_t obj_value = SDB_DATA_INIT;
sdb_data_t value = SDB_DATA_INIT;
int status;
- if (sdb_store_expr_eval(OBJ_C(cond)->expr, obj, &value))
+ if (sdb_store_expr_eval(OBJ_C(cond)->expr, obj, &value, filter))
return INT_MAX;
if (OBJ_C(cond)->field == SDB_FIELD_BACKEND) {
return buf;
}
- if (sdb_store_expr_eval(expr, NULL, &value))
+ 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)
if (! expr)
return NULL;
- if (sdb_store_expr_eval(expr, NULL, &value) ||
- (value.type != SDB_TYPE_STRING)) {
+ if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
+ || (value.type != SDB_TYPE_STRING)) {
sdb_data_free_datum(&value);
if (type != SDB_ATTRIBUTE)
return NULL;
index b9a8d0896eae6a3cad344464696f0479994a13a4..c0a98492fcbc47812017d19bec466fd4803a0043 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
struct sdb_store_obj;
typedef struct sdb_store_obj sdb_store_obj_t;
+/*
+ * Expressions represent arithmetic expressions based on stored objects and
+ * their various attributes.
+ *
+ * An expression object inherits from sdb_object_t and, thus, may safely be
+ * cast to a generic object.
+ */
+struct sdb_store_expr;
+typedef struct sdb_store_expr sdb_store_expr_t;
+#define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
+
+/*
+ * Store matchers may be used to lookup hosts from the store based on their
+ * various attributes. Service and attribute matchers are applied to a host's
+ * services and attributes and evaluate to true if *any* service or attribute
+ * matches.
+ *
+ * A store matcher object inherits from sdb_object_t and, thus, may safely be
+ * cast to a generic object.
+ */
+struct sdb_store_matcher;
+typedef struct sdb_store_matcher sdb_store_matcher_t;
+#define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
+
/*
* Queryable fields of a stored object.
*/
* sdb_store_get_attr:
* Get the value of a stored object's attribute. The caller is responsible for
* freeing any dynamically allocated memory possibly stored in the returned
- * value.If 'res' is NULL, the function will return whether the attribute
- * exists.
+ * value. If 'res' is NULL, the function will return whether the attribute
+ * exists. If specified, only attributes matching the filter will be
+ * considered.
*
* Returns:
* - 0 if the attribute exists
* - a negative value else
*/
int
-sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res);
-
-/*
- * Expressions specify arithmetic expressions.
- *
- * A expression object inherits from sdb_object_t and, thus, may safely be
- * cast to a generic object.
- */
-struct sdb_store_expr;
-typedef struct sdb_store_expr sdb_store_expr_t;
-#define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
+sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
+ sdb_store_matcher_t *filter);
/*
* sdb_store_expr_create:
* result in 'res'. The result's value will be allocated dynamically if
* necessary and, thus, should be free'd by the caller (e.g. using
* sdb_data_free_datum). The object may be NULL, in which case the expression
- * needs to evaluate to a constant value.
+ * needs to evaluate to a constant value. If specified, only objects matching
+ * the filter will be used during the evaluation.
*
* Returns:
* - 0 on success
*/
int
sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
- sdb_data_t *res);
+ sdb_data_t *res, sdb_store_matcher_t *filter);
/*
* Conditionals may be used to lookup hosts from the store based on a
sdb_store_cond_t *
sdb_store_obj_cond(int field, sdb_store_expr_t *expr);
-/*
- * Store matchers may be used to lookup hosts from the store based on their
- * various attributes. Service and attribute matchers are applied to a host's
- * services and attributes and evaluate to true if *any* service or attribute
- * matches.
- *
- * A store matcher object inherits from sdb_object_t and, thus, may safely be
- * cast to a generic object.
- */
-struct sdb_store_matcher;
-typedef struct sdb_store_matcher sdb_store_matcher_t;
-#define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
-
/*
* sdb_store_name_matcher:
* Creates a matcher matching by the specified object type's name. If 're' is