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 * querying
108 */
110 struct sdb_store_query {
111 sdb_object_t super;
112 sdb_ast_node_t *ast;
113 sdb_store_matcher_t *matcher;
114 sdb_store_matcher_t *filter;
115 };
116 #define QUERY(m) ((sdb_store_query_t *)(m))
118 /*
119 * expressions
120 */
122 enum {
123 TYPED_EXPR = -3, /* obj type stored in data.data.integer */
124 ATTR_VALUE = -2, /* attr name stored in data.data.string */
125 FIELD_VALUE = -1, /* field type stored in data.data.integer */
126 /* 0: const value (stored in data) */
127 /* >0: operator id */
128 };
130 struct sdb_store_expr {
131 sdb_object_t super;
133 int type; /* see above */
134 int data_type;
136 sdb_store_expr_t *left;
137 sdb_store_expr_t *right;
139 sdb_data_t data;
140 };
141 #define CONST_EXPR(v) { SDB_OBJECT_INIT, 0, (v).type, NULL, NULL, (v) }
142 #define EXPR_TO_STRING(e) \
143 (((e)->type == TYPED_EXPR) ? "<typed>" \
144 : ((e)->type == ATTR_VALUE) ? "attribute" \
145 : ((e)->type == FIELD_VALUE) ? SDB_FIELD_TO_NAME((e)->data.data.integer) \
146 : ((e)->type == 0) ? "<constant>" \
147 : ((e)->type > 0) ? SDB_DATA_OP_TO_STRING((e)->type) \
148 : "<unknown>")
150 /*
151 * matchers
152 */
154 /* when adding to this, also update 'MATCHER_SYM' below and 'matchers' in
155 * store_lookup.c */
156 enum {
157 MATCHER_OR,
158 MATCHER_AND,
159 MATCHER_NOT,
160 MATCHER_ANY,
161 MATCHER_ALL,
162 MATCHER_IN,
163 MATCHER_NIN,
165 /* unary operators */
166 MATCHER_ISNULL,
168 /* ary operators */
169 MATCHER_LT,
170 MATCHER_LE,
171 MATCHER_EQ,
172 MATCHER_NE,
173 MATCHER_GE,
174 MATCHER_GT,
175 MATCHER_REGEX,
176 MATCHER_NREGEX,
178 /* a generic query */
179 MATCHER_QUERY,
180 };
182 #define MATCHER_SYM(t) \
183 (((t) == MATCHER_OR) ? "OR" \
184 : ((t) == MATCHER_AND) ? "AND" \
185 : ((t) == MATCHER_NOT) ? "NOT" \
186 : ((t) == MATCHER_ANY) ? "ANY" \
187 : ((t) == MATCHER_ALL) ? "ALL" \
188 : ((t) == MATCHER_IN) ? "IN" \
189 : ((t) == MATCHER_NIN) ? "NOT IN" \
190 : ((t) == MATCHER_ISNULL) ? "IS NULL" \
191 : ((t) == MATCHER_LT) ? "<" \
192 : ((t) == MATCHER_LE) ? "<=" \
193 : ((t) == MATCHER_EQ) ? "=" \
194 : ((t) == MATCHER_NE) ? "!=" \
195 : ((t) == MATCHER_GE) ? ">=" \
196 : ((t) == MATCHER_GT) ? ">" \
197 : ((t) == MATCHER_REGEX) ? "=~" \
198 : ((t) == MATCHER_NREGEX) ? "!~" \
199 : ((t) == MATCHER_QUERY) ? "QUERY" \
200 : "UNKNOWN")
202 /* matcher base type */
203 struct sdb_store_matcher {
204 sdb_object_t super;
205 /* type of the matcher */
206 int type;
207 };
208 #define M(m) ((sdb_store_matcher_t *)(m))
210 /* infix operator matcher */
211 typedef struct {
212 sdb_store_matcher_t super;
214 /* left and right hand operands */
215 sdb_store_matcher_t *left;
216 sdb_store_matcher_t *right;
217 } op_matcher_t;
218 #define OP_M(m) ((op_matcher_t *)(m))
220 /* unary operator matcher */
221 typedef struct {
222 sdb_store_matcher_t super;
224 /* operand */
225 sdb_store_matcher_t *op;
226 } uop_matcher_t;
227 #define UOP_M(m) ((uop_matcher_t *)(m))
229 /* iter matcher */
230 typedef struct {
231 sdb_store_matcher_t super;
232 sdb_store_expr_t *iter;
233 sdb_store_matcher_t *m;
234 } iter_matcher_t;
235 #define ITER_M(m) ((iter_matcher_t *)(m))
237 /* compare operator matcher */
238 typedef struct {
239 sdb_store_matcher_t super;
241 /* left and right hand expressions */
242 sdb_store_expr_t *left;
243 sdb_store_expr_t *right;
244 } cmp_matcher_t;
245 #define CMP_M(m) ((cmp_matcher_t *)(m))
247 typedef struct {
248 sdb_store_matcher_t super;
249 sdb_store_expr_t *expr;
250 } isnull_matcher_t;
251 #define ISNULL_M(m) ((isnull_matcher_t *)(m))
253 #ifdef __cplusplus
254 } /* extern "C" */
255 #endif
257 #endif /* ! SDB_CORE_STORE_H */
259 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */