Code

43fcd41f0464a19c2c7fb559556b736193feb084
[sysdb.git] / src / parser / ast.c
1 /*
2  * SysDB - src/parser/ast.c
3  * Copyright (C) 2013-2015 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 #include "core/store.h"
30 #include "parser/ast.h"
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <string.h>
36 /*
37  * data types
38  */
40 static void
41 op_destroy(sdb_object_t *obj)
42 {
43         sdb_ast_op_t *op = SDB_AST_OP(obj);
44         sdb_object_deref(SDB_OBJ(op->left));
45         sdb_object_deref(SDB_OBJ(op->right));
46         op->left = op->right = NULL;
47 } /* op_destroy */
49 static void
50 iter_destroy(sdb_object_t *obj)
51 {
52         sdb_ast_iter_t *iter = SDB_AST_ITER(obj);
53         sdb_object_deref(SDB_OBJ(iter->iter));
54         sdb_object_deref(SDB_OBJ(iter->expr));
55         iter->iter = iter->expr = NULL;
56 } /* iter_destroy */
58 static void
59 typed_destroy(sdb_object_t *obj)
60 {
61         sdb_ast_typed_t *typed = SDB_AST_TYPED(obj);
62         sdb_object_deref(SDB_OBJ(typed->expr));
63         typed->expr = NULL;
64 } /* typed_destroy */
66 static void
67 const_destroy(sdb_object_t *obj)
68 {
69         sdb_ast_const_t *c = SDB_AST_CONST(obj);
70         sdb_data_free_datum(&c->value);
71 } /* const_destroy */
73 static void
74 value_destroy(sdb_object_t *obj)
75 {
76         sdb_ast_value_t *value = SDB_AST_VALUE(obj);
77         if (value->name)
78                 free(value->name);
79         value->name = NULL;
80 } /* value_destroy */
82 static void
83 fetch_destroy(sdb_object_t *obj)
84 {
85         sdb_ast_fetch_t *fetch = SDB_AST_FETCH(obj);
86         if (fetch->hostname)
87                 free(fetch->hostname);
88         if (fetch->name)
89                 free(fetch->name);
90         fetch->hostname = fetch->name = NULL;
92         sdb_object_deref(SDB_OBJ(fetch->filter));
93         fetch->filter = NULL;
94 } /* fetch_destroy */
96 static void
97 list_destroy(sdb_object_t *obj)
98 {
99         sdb_ast_list_t *list = SDB_AST_LIST(obj);
100         sdb_object_deref(SDB_OBJ(list->filter));
101         list->filter = NULL;
102 } /* list_destroy */
104 static void
105 lookup_destroy(sdb_object_t *obj)
107         sdb_ast_lookup_t *lookup = SDB_AST_LOOKUP(obj);
108         sdb_object_deref(SDB_OBJ(lookup->matcher));
109         sdb_object_deref(SDB_OBJ(lookup->filter));
110         lookup->matcher = lookup->filter = NULL;
111 } /* lookup_destroy */
113 static void
114 store_destroy(sdb_object_t *obj)
116         sdb_ast_store_t *store = SDB_AST_STORE(obj);
117         if (store->hostname)
118                 free(store->hostname);
119         if (store->parent)
120                 free(store->parent);
121         if (store->name)
122                 free(store->name);
123         store->hostname = store->parent = store->name = NULL;
125         if (store->store_type)
126                 free(store->store_type);
127         if (store->store_id)
128                 free(store->store_id);
129         store->store_type = store->store_id = NULL;
131         sdb_data_free_datum(&store->value);
132 } /* store_destroy */
134 static void
135 timeseries_destroy(sdb_object_t *obj)
137         sdb_ast_timeseries_t *timeseries = SDB_AST_TIMESERIES(obj);
138         if (timeseries->hostname)
139                 free(timeseries->hostname);
140         if (timeseries->metric)
141                 free(timeseries->metric);
142         timeseries->hostname = timeseries->metric = NULL;
143 } /* timeseries_destroy */
145 static sdb_type_t op_type = {
146         /* size */ sizeof(sdb_ast_op_t),
147         /* init */ NULL,
148         /* destroy */ op_destroy,
149 };
151 static sdb_type_t iter_type = {
152         /* size */ sizeof(sdb_ast_iter_t),
153         /* init */ NULL,
154         /* destroy */ iter_destroy,
155 };
157 static sdb_type_t typed_type = {
158         /* size */ sizeof(sdb_ast_typed_t),
159         /* init */ NULL,
160         /* destroy */ typed_destroy,
161 };
163 static sdb_type_t const_type = {
164         /* size */ sizeof(sdb_ast_const_t),
165         /* init */ NULL,
166         /* destroy */ const_destroy,
167 };
169 static sdb_type_t value_type = {
170         /* size */ sizeof(sdb_ast_value_t),
171         /* init */ NULL,
172         /* destroy */ value_destroy,
173 };
175 static sdb_type_t fetch_type = {
176         /* size */ sizeof(sdb_ast_fetch_t),
177         /* init */ NULL,
178         /* destroy */ fetch_destroy,
179 };
181 static sdb_type_t list_type = {
182         /* size */ sizeof(sdb_ast_list_t),
183         /* init */ NULL,
184         /* destroy */ list_destroy,
185 };
187 static sdb_type_t lookup_type = {
188         /* size */ sizeof(sdb_ast_lookup_t),
189         /* init */ NULL,
190         /* destroy */ lookup_destroy,
191 };
193 static sdb_type_t st_type = {
194         /* size */ sizeof(sdb_ast_store_t),
195         /* init */ NULL,
196         /* destroy */ store_destroy,
197 };
199 static sdb_type_t ts_type = {
200         /* size */ sizeof(sdb_ast_timeseries_t),
201         /* init */ NULL,
202         /* destroy */ timeseries_destroy,
203 };
205 /*
206  * public API
207  */
209 sdb_ast_node_t *
210 sdb_ast_op_create(int kind, sdb_ast_node_t *left, sdb_ast_node_t *right)
212         sdb_ast_op_t *op;
213         op = SDB_AST_OP(sdb_object_create(SDB_AST_OP_TO_STRING(kind), op_type));
214         if (! op)
215                 return NULL;
217         op->super.type = SDB_AST_TYPE_OPERATOR;
219         op->kind = kind;
220         op->left = left;
221         op->right = right;
222         return SDB_AST_NODE(op);
223 } /* sdb_ast_op_create */
225 sdb_ast_node_t *
226 sdb_ast_iter_create(int kind, sdb_ast_node_t *iter, sdb_ast_node_t *expr)
228         sdb_ast_iter_t *i;
229         i = SDB_AST_ITER(sdb_object_create(SDB_AST_OP_TO_STRING(kind), iter_type));
230         if (! i)
231                 return NULL;
233         i->super.type = SDB_AST_TYPE_ITERATOR;
235         i->kind = kind;
236         i->iter = iter;
237         i->expr = expr;
238         return SDB_AST_NODE(i);
239 } /* sdb_ast_iter_create */
241 sdb_ast_node_t *
242 sdb_ast_typed_create(int type, sdb_ast_node_t *expr)
244         char name[32];
245         sdb_ast_typed_t *typed;
246         size_t i;
248         strncpy(name, SDB_STORE_TYPE_TO_NAME(type), sizeof(name));
249         for (i = 0; i < strlen(name); ++i)
250                 name[i] = (char)toupper((int)name[i]);
251         typed = SDB_AST_TYPED(sdb_object_create(name, typed_type));
252         if (! typed)
253                 return NULL;
255         typed->super.type = SDB_AST_TYPE_TYPED;
257         typed->type = type;
258         typed->expr = expr;
259         return SDB_AST_NODE(typed);
260 } /* sdb_ast_typed_create */
262 sdb_ast_node_t *
263 sdb_ast_const_create(sdb_data_t value)
265         sdb_ast_const_t *c;
266         c = SDB_AST_CONST(sdb_object_create("CONST", const_type));
267         if (! c)
268                 return NULL;
270         c->super.type = SDB_AST_TYPE_CONST;
272         c->value = value;
273         return SDB_AST_NODE(c);
274 } /* sdb_ast_const_create */
276 sdb_ast_node_t *
277 sdb_ast_value_create(int type, char *name)
279         sdb_ast_value_t *value;
280         value = SDB_AST_VALUE(sdb_object_create("VALUE", value_type));
281         if (! value)
282                 return NULL;
284         value->super.type = SDB_AST_TYPE_VALUE;
286         value->type = type;
287         value->name = name;
288         return SDB_AST_NODE(value);
289 } /* sdb_ast_value_create */
291 sdb_ast_node_t *
292 sdb_ast_fetch_create(int obj_type, char *hostname, char *name, bool full,
293                 sdb_ast_node_t *filter)
295         sdb_ast_fetch_t *fetch;
296         fetch = SDB_AST_FETCH(sdb_object_create("FETCH", fetch_type));
297         if (! fetch)
298                 return NULL;
300         fetch->super.type = SDB_AST_TYPE_FETCH;
302         fetch->obj_type = obj_type;
303         fetch->hostname = hostname;
304         fetch->name = name;
305         fetch->full = full;
306         fetch->filter = filter;
307         return SDB_AST_NODE(fetch);
308 } /* sdb_ast_fetch_create */
310 sdb_ast_node_t *
311 sdb_ast_list_create(int obj_type, sdb_ast_node_t *filter)
313         sdb_ast_list_t *list;
314         list = SDB_AST_LIST(sdb_object_create("LIST", list_type));
315         if (! list)
316                 return NULL;
318         list->super.type = SDB_AST_TYPE_LIST;
320         list->obj_type = obj_type;
321         list->filter = filter;
322         return SDB_AST_NODE(list);
323 } /* sdb_ast_list_create */
325 sdb_ast_node_t *
326 sdb_ast_lookup_create(int obj_type, sdb_ast_node_t *matcher,
327                 sdb_ast_node_t *filter)
329         sdb_ast_lookup_t *lookup;
330         lookup = SDB_AST_LOOKUP(sdb_object_create("LOOKUP", lookup_type));
331         if (! lookup)
332                 return NULL;
334         lookup->super.type = SDB_AST_TYPE_LOOKUP;
336         lookup->obj_type = obj_type;
337         lookup->matcher = matcher;
338         lookup->filter = filter;
339         return SDB_AST_NODE(lookup);
340 } /* sdb_ast_lookup_create */
342 sdb_ast_node_t *
343 sdb_ast_store_create(int obj_type, char *hostname,
344                 int parent_type, char *parent, char *name, sdb_time_t last_update,
345                 char *store_type, char *store_id, sdb_data_t value)
347         sdb_ast_store_t *store;
348         store = SDB_AST_STORE(sdb_object_create("STORE", st_type));
349         if (! store)
350                 return NULL;
352         store->super.type = SDB_AST_TYPE_STORE;
354         store->obj_type = obj_type;
355         store->hostname = hostname;
356         store->parent_type = parent_type;
357         store->parent = parent;
358         store->name = name;
359         store->last_update = last_update;
360         store->store_type = store_type;
361         store->store_id = store_id;
362         store->value = value;
363         return SDB_AST_NODE(store);
364 } /* sdb_ast_store_create */
366 sdb_ast_node_t *
367 sdb_ast_timeseries_create(char *hostname, char *metric,
368                 sdb_time_t start, sdb_time_t end)
370         sdb_ast_timeseries_t *timeseries;
371         timeseries = SDB_AST_TIMESERIES(sdb_object_create("TIMESERIES", ts_type));
372         if (! timeseries)
373                 return NULL;
375         timeseries->super.type = SDB_AST_TYPE_TIMESERIES;
377         timeseries->hostname = hostname;
378         timeseries->metric = metric;
379         timeseries->start = start;
380         timeseries->end = end;
381         return SDB_AST_NODE(timeseries);
382 } /* sdb_ast_timeseries_create */
384 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */