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 *services;
85 sdb_avltree_t *attributes;
86 } sdb_host_t;
87 #define HOST(obj) ((sdb_host_t *)(obj))
88 #define CONST_HOST(obj) ((const sdb_host_t *)(obj))
90 /* shortcuts for accessing service/host attributes */
91 #define _last_update super.last_update
92 #define _interval super.interval
94 /*
95 * conditionals
96 */
98 /* compares a host using the specified conditional and taking the specified
99 * filter into account */
100 typedef int (*cmp_cb)(sdb_host_t *, sdb_store_cond_t *,
101 sdb_store_matcher_t *);
103 struct sdb_store_cond {
104 sdb_object_t super;
105 cmp_cb cmp;
106 };
108 typedef struct {
109 sdb_store_cond_t super;
110 char *name;
111 sdb_store_expr_t *expr;
112 } attr_cond_t;
113 #define ATTR_C(obj) ((attr_cond_t *)(obj))
115 /*
116 * matchers
117 */
119 /* when adding to this, also update 'matchers' and 'matchers_tostring'
120 * in store_lookup.c */
121 enum {
122 MATCHER_OR,
123 MATCHER_AND,
124 MATCHER_NOT,
125 MATCHER_NAME,
126 MATCHER_ATTR,
127 MATCHER_LT,
128 MATCHER_LE,
129 MATCHER_EQ,
130 MATCHER_GE,
131 MATCHER_GT,
132 MATCHER_ISNULL,
133 };
135 #define MATCHER_SYM(t) \
136 (((t) == MATCHER_OR) ? "OR" \
137 : ((t) == MATCHER_AND) ? "AND" \
138 : ((t) == MATCHER_NOT) ? "NOT" \
139 : ((t) == MATCHER_NAME) ? "NAME" \
140 : ((t) == MATCHER_ATTR) ? "ATTR" \
141 : ((t) == MATCHER_LT) ? "<" \
142 : ((t) == MATCHER_LE) ? "<=" \
143 : ((t) == MATCHER_EQ) ? "=" \
144 : ((t) == MATCHER_GE) ? ">=" \
145 : ((t) == MATCHER_GT) ? ">" \
146 : ((t) == MATCHER_ISNULL) ? "IS NULL" \
147 : "UNKNOWN")
149 /* match the name of something */
150 typedef struct {
151 char *name;
152 regex_t *name_re;
153 } string_matcher_t;
155 /* matcher base type */
156 struct sdb_store_matcher {
157 sdb_object_t super;
158 /* type of the matcher */
159 int type;
160 };
161 #define M(m) ((sdb_store_matcher_t *)(m))
163 /* infix operator matcher */
164 typedef struct {
165 sdb_store_matcher_t super;
167 /* left and right hand operands */
168 sdb_store_matcher_t *left;
169 sdb_store_matcher_t *right;
170 } op_matcher_t;
171 #define OP_M(m) ((op_matcher_t *)(m))
173 /* unary operator matcher */
174 typedef struct {
175 sdb_store_matcher_t super;
177 /* operand */
178 sdb_store_matcher_t *op;
179 } uop_matcher_t;
180 #define UOP_M(m) ((uop_matcher_t *)(m))
182 /* match any type of object by it's name */
183 typedef struct {
184 sdb_store_matcher_t super;
186 int obj_type;
187 string_matcher_t name;
188 } name_matcher_t;
189 #define NAME_M(m) ((name_matcher_t *)(m))
191 /* match attributes */
192 typedef struct {
193 sdb_store_matcher_t super;
194 char *name;
195 string_matcher_t value;
196 } attr_matcher_t;
197 #define ATTR_M(m) ((attr_matcher_t *)(m))
199 typedef struct {
200 sdb_store_matcher_t super;
201 char *attr_name; /* we only support matching attributes */
202 } isnull_matcher_t;
203 #define ISNULL_M(m) ((isnull_matcher_t *)(m))
205 /* match using conditionals */
206 typedef struct {
207 sdb_store_matcher_t super;
208 sdb_store_cond_t *cond;
209 } cond_matcher_t;
210 #define COND_M(m) ((cond_matcher_t *)(m))
212 #ifdef __cplusplus
213 } /* extern "C" */
214 #endif
216 #endif /* ! SDB_CORE_STORE_H */
218 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */