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 * expressions
108 */
109 enum {
110 ATTR_VALUE = -2, /* attr name stored in data.data.string */
111 FIELD_VALUE = -1, /* field type stored in data.data.integer */
112 /* 0: const value (stored in data) */
113 /* >0: operator id */
114 };
116 struct sdb_store_expr {
117 sdb_object_t super;
119 int type; /* see above */
121 sdb_store_expr_t *left;
122 sdb_store_expr_t *right;
124 sdb_data_t data;
125 };
127 /*
128 * conditionals
129 */
131 /* compares an object using the specified conditional and taking the specified
132 * filter into account */
133 typedef int (*cmp_cb)(sdb_store_obj_t *, sdb_store_cond_t *,
134 sdb_store_matcher_t *);
136 struct sdb_store_cond {
137 sdb_object_t super;
138 cmp_cb cmp;
139 };
141 typedef struct {
142 sdb_store_cond_t super;
143 char *name;
144 sdb_store_expr_t *expr;
145 } attr_cond_t;
146 #define ATTR_C(obj) ((attr_cond_t *)(obj))
148 typedef struct {
149 sdb_store_cond_t super;
150 int field;
151 sdb_store_expr_t *expr;
152 } obj_cond_t;
153 #define OBJ_C(obj) ((obj_cond_t *)(obj))
155 /*
156 * matchers
157 */
159 /* when adding to this, also update 'MATCHER_SYM' below as well as 'matchers'
160 * and 'matchers_tostring' in store_lookup.c */
161 enum {
162 MATCHER_OR,
163 MATCHER_AND,
164 MATCHER_NOT,
165 MATCHER_NAME,
166 MATCHER_ATTR,
167 MATCHER_SERVICE,
168 MATCHER_METRIC,
169 MATCHER_ATTRIBUTE,
170 MATCHER_LT,
171 MATCHER_LE,
172 MATCHER_EQ,
173 MATCHER_GE,
174 MATCHER_GT,
175 MATCHER_CMP_LT,
176 MATCHER_CMP_LE,
177 MATCHER_CMP_EQ,
178 MATCHER_CMP_NE,
179 MATCHER_CMP_GE,
180 MATCHER_CMP_GT,
181 MATCHER_REGEX,
182 MATCHER_NREGEX,
183 MATCHER_ISNULL,
184 };
186 #define MATCHER_SYM(t) \
187 (((t) == MATCHER_OR) ? "OR" \
188 : ((t) == MATCHER_AND) ? "AND" \
189 : ((t) == MATCHER_NOT) ? "NOT" \
190 : ((t) == MATCHER_NAME) ? "NAME" \
191 : ((t) == MATCHER_ATTR) ? "ATTR" \
192 : ((t) == MATCHER_SERVICE) ? "SERVICE" \
193 : ((t) == MATCHER_METRIC) ? "METRIC" \
194 : ((t) == MATCHER_ATTRIBUTE) ? "ATTRIBUTE" \
195 : ((t) == MATCHER_LT) ? "<" \
196 : ((t) == MATCHER_LE) ? "<=" \
197 : ((t) == MATCHER_EQ) ? "=" \
198 : ((t) == MATCHER_CMP_NE) ? "!=" \
199 : ((t) == MATCHER_GE) ? ">=" \
200 : ((t) == MATCHER_GT) ? ">" \
201 : ((t) == MATCHER_REGEX) ? "=~" \
202 : ((t) == MATCHER_NREGEX) ? "!~" \
203 : ((t) == MATCHER_ISNULL) ? "IS NULL" \
204 : "UNKNOWN")
206 /* match the name of something */
207 typedef struct {
208 char *name;
209 regex_t *name_re;
210 } string_matcher_t;
212 /* matcher base type */
213 struct sdb_store_matcher {
214 sdb_object_t super;
215 /* type of the matcher */
216 int type;
217 };
218 #define M(m) ((sdb_store_matcher_t *)(m))
220 /* infix operator matcher */
221 typedef struct {
222 sdb_store_matcher_t super;
224 /* left and right hand operands */
225 sdb_store_matcher_t *left;
226 sdb_store_matcher_t *right;
227 } op_matcher_t;
228 #define OP_M(m) ((op_matcher_t *)(m))
230 /* unary operator matcher */
231 typedef struct {
232 sdb_store_matcher_t super;
234 /* operand */
235 sdb_store_matcher_t *op;
236 } uop_matcher_t;
237 #define UOP_M(m) ((uop_matcher_t *)(m))
239 /* child matcher */
240 typedef struct {
241 sdb_store_matcher_t super;
242 sdb_store_matcher_t *m;
243 } child_matcher_t;
244 #define CHILD_M(m) ((child_matcher_t *)(m))
246 /* compare operator matcher */
247 typedef struct {
248 sdb_store_matcher_t super;
250 /* left and right hand expressions */
251 sdb_store_expr_t *left;
252 sdb_store_expr_t *right;
253 } cmp_matcher_t;
254 #define CMP_M(m) ((cmp_matcher_t *)(m))
256 /* match any type of object by it's name */
257 typedef struct {
258 sdb_store_matcher_t super;
260 int obj_type;
261 string_matcher_t name;
262 } name_matcher_t;
263 #define NAME_M(m) ((name_matcher_t *)(m))
265 /* match attributes */
266 typedef struct {
267 sdb_store_matcher_t super;
268 char *name;
269 string_matcher_t value;
270 } attr_matcher_t;
271 #define ATTR_M(m) ((attr_matcher_t *)(m))
273 typedef struct {
274 sdb_store_matcher_t super;
275 char *attr_name; /* we only support matching attributes */
276 } isnull_matcher_t;
277 #define ISNULL_M(m) ((isnull_matcher_t *)(m))
279 /* match using conditionals */
280 typedef struct {
281 sdb_store_matcher_t super;
282 sdb_store_cond_t *cond;
283 } cond_matcher_t;
284 #define COND_M(m) ((cond_matcher_t *)(m))
286 #ifdef __cplusplus
287 } /* extern "C" */
288 #endif
290 #endif /* ! SDB_CORE_STORE_H */
292 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */