From 9935ca9e91f79b4f5ee6e9a7f94912b7992e8858 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Mon, 1 Dec 2014 17:34:59 +0100 Subject: [PATCH] store: Added sdb_store_expr_typed. This function creates an expression which evaluates another expression in the context of an object's sibling. The sibling is specified by type and refers to a parent or child node of the specified type. --- src/core/store-private.h | 1 + src/core/store_expr.c | 31 ++++++++++++++++++++++++++++++- src/include/core/store.h | 12 ++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/core/store-private.h b/src/core/store-private.h index d2ea3f9..3ae6c43 100644 --- a/src/core/store-private.h +++ b/src/core/store-private.h @@ -108,6 +108,7 @@ typedef struct { */ enum { + TYPED_EXPR = -3, /* obj type stored in data.data.integer */ ATTR_VALUE = -2, /* attr name stored in data.data.string */ FIELD_VALUE = -1, /* field type stored in data.data.integer */ /* 0: const value (stored in data) */ diff --git a/src/core/store_expr.c b/src/core/store_expr.c index d5b22c7..a0047a1 100644 --- a/src/core/store_expr.c +++ b/src/core/store_expr.c @@ -59,6 +59,8 @@ expr_init(sdb_object_t *obj, va_list ap) if (type <= 0) { if (! value) return -1; + if ((type == TYPED_EXPR) && (! left)) + return -1; } else { if (value) return -1; @@ -127,13 +129,28 @@ sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right) return e; } /* sdb_store_expr_create */ +sdb_store_expr_t * +sdb_store_expr_typed(int typ, sdb_store_expr_t *expr) +{ + sdb_data_t value = { SDB_TYPE_INTEGER, { .integer = typ } }; + sdb_store_expr_t *e; + + if ((typ < SDB_HOST) || (SDB_ATTRIBUTE < typ)) + return NULL; + + e = SDB_STORE_EXPR(sdb_object_create("store-typedexpr", expr_type, + TYPED_EXPR, expr, NULL, &value)); + e->data_type = expr->data_type; + return e; +} /* sdb_store_expr_typed */ + sdb_store_expr_t * sdb_store_expr_fieldvalue(int field) { sdb_data_t value = { SDB_TYPE_INTEGER, { .integer = field } }; sdb_store_expr_t *e; - if ((field < 0) || (SDB_FIELD_BACKEND < field)) + if ((field < SDB_FIELD_NAME) || (SDB_FIELD_BACKEND < field)) return NULL; e = SDB_STORE_EXPR(sdb_object_create("store-fieldvalue", expr_type, FIELD_VALUE, NULL, NULL, &value)); @@ -200,6 +217,18 @@ sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj, } return status; } + else if (expr->type == TYPED_EXPR) { + int typ = (int)expr->data.data.integer; + if (typ != obj->type) { + /* we support self-references and { service, metric } -> host */ + if ((typ != SDB_HOST) + || ((obj->type != SDB_SERVICE) + && (obj->type != SDB_METRIC))) + return -1; + obj = obj->parent; + } + return sdb_store_expr_eval(expr->left, obj, res, filter); + } if (sdb_store_expr_eval(expr->left, obj, &v1, filter)) return -1; diff --git a/src/include/core/store.h b/src/include/core/store.h index d3862b4..5507436 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -332,6 +332,18 @@ sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res, sdb_store_expr_t * sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right); +/* + * sdb_store_expr_typed: + * Creates an expression which evaluates in the context of an object's sibling + * as specified by the given type. + * + * Returns: + * - an expression object on success + * - NULL else + */ +sdb_store_expr_t * +sdb_store_expr_typed(int typ, sdb_store_expr_t *expr); + /* * sdb_store_expr_fieldvalue: * Creates an expression which evaluates to the value of the specified -- 2.30.2