Code

store: Added matchers comparing two expressions.
[sysdb.git] / src / core / store-private.h
1 /*
2  * SysDB - src/core/store-private.h
3  * Copyright (C) 2012-2013 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  * private data structures used by the store module
30  */
32 #ifndef SDB_CORE_STORE_PRIVATE_H
33 #define SDB_CORE_STORE_PRIVATE_H 1
35 #include "core/store.h"
36 #include "utils/avltree.h"
38 #include <sys/types.h>
39 #include <regex.h>
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
45 /*
46  * core types
47  */
49 struct sdb_store_obj {
50         sdb_object_t super;
52         /* object type */
53         int type;
55         /* common meta information */
56         sdb_time_t last_update;
57         sdb_time_t interval; /* moving average */
58         char **backends;
59         size_t backends_num;
60         sdb_store_obj_t *parent;
61 };
62 #define STORE_OBJ(obj) ((sdb_store_obj_t *)(obj))
63 #define STORE_CONST_OBJ(obj) ((const sdb_store_obj_t *)(obj))
65 typedef struct {
66         sdb_store_obj_t super;
68         sdb_data_t value;
69 } sdb_attribute_t;
70 #define ATTR(obj) ((sdb_attribute_t *)(obj))
71 #define CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
73 typedef struct {
74         sdb_store_obj_t super;
76         sdb_avltree_t *attributes;
77 } sdb_service_t;
78 #define SVC(obj) ((sdb_service_t *)(obj))
79 #define CONST_SVC(obj) ((const sdb_service_t *)(obj))
81 typedef struct {
82         sdb_store_obj_t super;
84         sdb_avltree_t *attributes;
85         struct {
86                 char *type;
87                 char *id;
88         } store;
89 } sdb_metric_t;
90 #define METRIC(obj) ((sdb_metric_t *)(obj))
92 typedef struct {
93         sdb_store_obj_t super;
95         sdb_avltree_t *services;
96         sdb_avltree_t *metrics;
97         sdb_avltree_t *attributes;
98 } sdb_host_t;
99 #define HOST(obj) ((sdb_host_t *)(obj))
100 #define CONST_HOST(obj) ((const sdb_host_t *)(obj))
102 /* shortcuts for accessing service/host attributes */
103 #define _last_update super.last_update
104 #define _interval super.interval
106 /*
107  * conditionals
108  */
110 /* compares an object using the specified conditional and taking the specified
111  * filter into account */
112 typedef int (*cmp_cb)(sdb_store_obj_t *, sdb_store_cond_t *,
113                 sdb_store_matcher_t *);
115 struct sdb_store_cond {
116         sdb_object_t super;
117         cmp_cb cmp;
118 };
120 typedef struct {
121         sdb_store_cond_t super;
122         char *name;
123         sdb_store_expr_t *expr;
124 } attr_cond_t;
125 #define ATTR_C(obj) ((attr_cond_t *)(obj))
127 typedef struct {
128         sdb_store_cond_t super;
129         int field;
130         sdb_store_expr_t *expr;
131 } obj_cond_t;
132 #define OBJ_C(obj) ((obj_cond_t *)(obj))
134 /*
135  * matchers
136  */
138 /* when adding to this, also update 'matchers' and 'matchers_tostring'
139  * in store_lookup.c */
140 enum {
141         MATCHER_OR,
142         MATCHER_AND,
143         MATCHER_NOT,
144         MATCHER_NAME,
145         MATCHER_ATTR,
146         MATCHER_LT,
147         MATCHER_LE,
148         MATCHER_EQ,
149         MATCHER_GE,
150         MATCHER_GT,
151         MATCHER_CMP_LT,
152         MATCHER_CMP_LE,
153         MATCHER_CMP_EQ,
154         MATCHER_CMP_GE,
155         MATCHER_CMP_GT,
156         MATCHER_ISNULL,
157 };
159 #define MATCHER_SYM(t) \
160         (((t) == MATCHER_OR) ? "OR" \
161                 : ((t) == MATCHER_AND) ? "AND" \
162                 : ((t) == MATCHER_NOT) ? "NOT" \
163                 : ((t) == MATCHER_NAME) ? "NAME" \
164                 : ((t) == MATCHER_ATTR) ? "ATTR" \
165                 : ((t) == MATCHER_LT) ? "<" \
166                 : ((t) == MATCHER_LE) ? "<=" \
167                 : ((t) == MATCHER_EQ) ? "=" \
168                 : ((t) == MATCHER_GE) ? ">=" \
169                 : ((t) == MATCHER_GT) ? ">" \
170                 : ((t) == MATCHER_ISNULL) ? "IS NULL" \
171                 : "UNKNOWN")
173 /* match the name of something */
174 typedef struct {
175         char    *name;
176         regex_t *name_re;
177 } string_matcher_t;
179 /* matcher base type */
180 struct sdb_store_matcher {
181         sdb_object_t super;
182         /* type of the matcher */
183         int type;
184 };
185 #define M(m) ((sdb_store_matcher_t *)(m))
187 /* infix operator matcher */
188 typedef struct {
189         sdb_store_matcher_t super;
191         /* left and right hand operands */
192         sdb_store_matcher_t *left;
193         sdb_store_matcher_t *right;
194 } op_matcher_t;
195 #define OP_M(m) ((op_matcher_t *)(m))
197 /* unary operator matcher */
198 typedef struct {
199         sdb_store_matcher_t super;
201         /* operand */
202         sdb_store_matcher_t *op;
203 } uop_matcher_t;
204 #define UOP_M(m) ((uop_matcher_t *)(m))
206 /* compare operator matcher */
207 typedef struct {
208         sdb_store_matcher_t super;
210         /* left and right hand expressions */
211         sdb_store_expr_t *left;
212         sdb_store_expr_t *right;
213 } cmp_matcher_t;
214 #define CMP_M(m) ((cmp_matcher_t *)(m))
216 /* match any type of object by it's name */
217 typedef struct {
218         sdb_store_matcher_t super;
220         int obj_type;
221         string_matcher_t name;
222 } name_matcher_t;
223 #define NAME_M(m) ((name_matcher_t *)(m))
225 /* match attributes */
226 typedef struct {
227         sdb_store_matcher_t super;
228         char *name;
229         string_matcher_t value;
230 } attr_matcher_t;
231 #define ATTR_M(m) ((attr_matcher_t *)(m))
233 typedef struct {
234         sdb_store_matcher_t super;
235         char *attr_name; /* we only support matching attributes */
236 } isnull_matcher_t;
237 #define ISNULL_M(m) ((isnull_matcher_t *)(m))
239 /* match using conditionals */
240 typedef struct {
241         sdb_store_matcher_t super;
242         sdb_store_cond_t *cond;
243 } cond_matcher_t;
244 #define COND_M(m) ((cond_matcher_t *)(m))
246 #ifdef __cplusplus
247 } /* extern "C" */
248 #endif
250 #endif /* ! SDB_CORE_STORE_H */
252 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */