From: Sebastian Harl Date: Sat, 2 Aug 2014 10:57:16 +0000 (+0200) Subject: store_expr: Evaluate constant expressions early. X-Git-Tag: sysdb-0.4.0~53 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=6ef9d05304ef34454ab41b4d98a05f2b5ef0f547;p=sysdb.git store_expr: Evaluate constant expressions early. That is, when creating a new expression objects, evaluate it right away if its left and right operands are constant values. --- diff --git a/src/core/store_expr.c b/src/core/store_expr.c index 7d674bc..9aa1ce9 100644 --- a/src/core/store_expr.c +++ b/src/core/store_expr.c @@ -86,8 +86,7 @@ expr_init(sdb_object_t *obj, va_list ap) } if (value) - if (sdb_data_copy(&expr->data, value)) - return -1; + expr->data = *value; sdb_object_ref(SDB_OBJ(left)); sdb_object_ref(SDB_OBJ(right)); @@ -122,10 +121,20 @@ static sdb_type_t expr_type = { sdb_store_expr_t * sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right) { - if ((op < 0) || (SDB_DATA_CONCAT < op)) + sdb_data_t value = SDB_DATA_INIT; + + if ((op < 0) || (SDB_DATA_CONCAT < op) || (! left) || (! right)) return NULL; - return SDB_STORE_EXPR(sdb_object_create("store-expr", expr_type, - op, left, right, NULL)); + + if (left->type || right->type) + return SDB_STORE_EXPR(sdb_object_create("store-expr", expr_type, + op, left, right, NULL)); + /* else: both expressions are constant values; evaluate now */ + + if (sdb_data_expr_eval(op, &left->data, &right->data, &value)) + return NULL; + return SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type, + 0, NULL, NULL, &value)); } /* sdb_store_expr_create */ sdb_store_expr_t * @@ -141,8 +150,11 @@ sdb_store_expr_fieldvalue(int field) sdb_store_expr_t * sdb_store_expr_constvalue(const sdb_data_t *value) { + sdb_data_t data = SDB_DATA_INIT; + if (sdb_data_copy(&data, value)) + return NULL; return SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type, - 0, NULL, NULL, value)); + 0, NULL, NULL, &data)); } /* sdb_store_expr_constvalue */ int