1 /*
2 * SysDB - src/core/store_expr.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 /*
29 * This module implements expressions which may be executed in the store.
30 */
32 #if HAVE_CONFIG_H
33 # include "config.h"
34 #endif /* HAVE_CONFIG_H */
36 #include "sysdb.h"
37 #include "core/store-private.h"
38 #include "core/data.h"
39 #include "core/object.h"
41 #include <assert.h>
42 #include <stdlib.h>
43 #include <string.h>
45 /*
46 * private data types
47 */
49 static int
50 expr_init(sdb_object_t *obj, va_list ap)
51 {
52 int type = va_arg(ap, int);
53 sdb_store_expr_t *left = va_arg(ap, sdb_store_expr_t *);
54 sdb_store_expr_t *right = va_arg(ap, sdb_store_expr_t *);
55 const sdb_data_t *value = va_arg(ap, const sdb_data_t *);
57 sdb_store_expr_t *expr = SDB_STORE_EXPR(obj);
59 if (type <= 0) {
60 if (! value)
61 return -1;
62 if ((type == TYPED_EXPR) && (! left))
63 return -1;
64 } else {
65 if (value)
66 return -1;
67 if ((! left) || (! right))
68 return -1;
69 }
71 if (value)
72 expr->data = *value;
74 sdb_object_ref(SDB_OBJ(left));
75 sdb_object_ref(SDB_OBJ(right));
77 expr->type = type;
78 expr->left = left;
79 expr->right = right;
81 /* unknown for now */
82 expr->data_type = -1;
83 return 0;
84 } /* expr_init */
86 static void
87 expr_destroy(sdb_object_t *obj)
88 {
89 sdb_store_expr_t *expr = SDB_STORE_EXPR(obj);
90 sdb_object_deref(SDB_OBJ(expr->left));
91 sdb_object_deref(SDB_OBJ(expr->right));
93 if (expr->data.type)
94 sdb_data_free_datum(&expr->data);
95 } /* expr_destroy */
97 static sdb_type_t expr_type = {
98 /* size = */ sizeof(sdb_store_expr_t),
99 /* init = */ expr_init,
100 /* destroy = */ expr_destroy,
101 };
103 /*
104 * public API
105 */
107 sdb_store_expr_t *
108 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right)
109 {
110 sdb_data_t value = SDB_DATA_INIT;
111 sdb_store_expr_t *e;
113 if ((op < 0) || (SDB_DATA_CONCAT < op) || (! left) || (! right))
114 return NULL;
116 if (left->type || right->type) {
117 e = SDB_STORE_EXPR(sdb_object_create("store-expr", expr_type,
118 op, left, right, NULL));
119 e->data_type = sdb_data_expr_type(op, left->type, right->type);
120 return e;
121 }
122 /* else: both expressions are constant values; evaluate now */
124 if (sdb_data_expr_eval(op, &left->data, &right->data, &value))
125 return NULL;
126 e = SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
127 0, NULL, NULL, &value));
128 e->data_type = value.type;
129 return e;
130 } /* sdb_store_expr_create */
132 sdb_store_expr_t *
133 sdb_store_expr_typed(int typ, sdb_store_expr_t *expr)
134 {
135 sdb_data_t value = { SDB_TYPE_INTEGER, { .integer = typ } };
136 sdb_store_expr_t *e;
138 if ((typ < SDB_HOST) || (SDB_ATTRIBUTE < typ))
139 return NULL;
141 e = SDB_STORE_EXPR(sdb_object_create("store-typedexpr", expr_type,
142 TYPED_EXPR, expr, NULL, &value));
143 e->data_type = expr->data_type;
144 return e;
145 } /* sdb_store_expr_typed */
147 sdb_store_expr_t *
148 sdb_store_expr_fieldvalue(int field)
149 {
150 sdb_data_t value = { SDB_TYPE_INTEGER, { .integer = field } };
151 sdb_store_expr_t *e;
153 if ((field < SDB_FIELD_NAME) || (SDB_FIELD_BACKEND < field))
154 return NULL;
155 e = SDB_STORE_EXPR(sdb_object_create("store-fieldvalue", expr_type,
156 FIELD_VALUE, NULL, NULL, &value));
157 e->data_type = SDB_FIELD_TYPE(field);
158 return e;
159 } /* sdb_store_expr_fieldvalue */
161 sdb_store_expr_t *
162 sdb_store_expr_attrvalue(const char *name)
163 {
164 sdb_data_t value = { SDB_TYPE_STRING, { .string = NULL} };
165 sdb_store_expr_t *expr;
167 value.data.string = strdup(name);
168 if (! value.data.string)
169 return NULL;
171 expr = SDB_STORE_EXPR(sdb_object_create("store-attrvalue", expr_type,
172 ATTR_VALUE, NULL, NULL, &value));
173 if (! expr)
174 free(value.data.string);
175 expr->data_type = -1;
176 return expr;
177 } /* sdb_store_expr_attrvalue */
179 sdb_store_expr_t *
180 sdb_store_expr_constvalue(const sdb_data_t *value)
181 {
182 sdb_data_t data = SDB_DATA_INIT;
183 sdb_store_expr_t *e;
185 if (sdb_data_copy(&data, value))
186 return NULL;
187 e = SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
188 0, NULL, NULL, &data));
189 e->data_type = data.type;
190 return e;
191 } /* sdb_store_expr_constvalue */
193 int
194 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
195 sdb_data_t *res, sdb_store_matcher_t *filter)
196 {
197 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
198 int status = 0;
200 if ((! expr) || (! res))
201 return -1;
203 if (filter && obj && (! sdb_store_matcher_matches(filter, obj, NULL)))
204 obj = NULL; /* this object does not exist */
206 if (! expr->type)
207 return sdb_data_copy(res, &expr->data);
208 else if (expr->type == FIELD_VALUE)
209 return sdb_store_get_field(obj, (int)expr->data.data.integer, res);
210 else if (expr->type == ATTR_VALUE) {
211 status = sdb_store_get_attr(obj, expr->data.data.string, res, filter);
212 if ((status < 0) && obj) {
213 /* attribute does not exist => NULL */
214 status = 0;
215 res->type = SDB_TYPE_STRING;
216 res->data.string = NULL;
217 }
218 return status;
219 }
220 else if (expr->type == TYPED_EXPR) {
221 int typ = (int)expr->data.data.integer;
222 if (typ != obj->type) {
223 /* we support self-references and { service, metric } -> host */
224 if ((typ != SDB_HOST)
225 || ((obj->type != SDB_SERVICE)
226 && (obj->type != SDB_METRIC)))
227 return -1;
228 obj = obj->parent;
229 }
230 return sdb_store_expr_eval(expr->left, obj, res, filter);
231 }
233 if (sdb_store_expr_eval(expr->left, obj, &v1, filter))
234 return -1;
235 if (sdb_store_expr_eval(expr->right, obj, &v2, filter)) {
236 sdb_data_free_datum(&v1);
237 return -1;
238 }
240 if (sdb_data_expr_eval(expr->type, &v1, &v2, res))
241 status = -1;
242 sdb_data_free_datum(&v1);
243 sdb_data_free_datum(&v2);
244 return status;
245 } /* sdb_store_expr_eval */
247 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */