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 struct sdb_store_obj {
46 sdb_object_t super;
48 /* object type */
49 int type;
51 /* common meta information */
52 sdb_time_t last_update;
53 sdb_time_t interval; /* moving average */
54 char **backends;
55 size_t backends_num;
56 sdb_store_obj_t *parent;
57 };
58 #define STORE_OBJ(obj) ((sdb_store_obj_t *)(obj))
59 #define STORE_CONST_OBJ(obj) ((const sdb_store_obj_t *)(obj))
61 typedef struct {
62 sdb_store_obj_t super;
64 sdb_data_t value;
65 } sdb_attribute_t;
66 #define ATTR(obj) ((sdb_attribute_t *)(obj))
67 #define CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
69 typedef struct {
70 sdb_store_obj_t super;
72 sdb_avltree_t *attributes;
73 } sdb_service_t;
74 #define SVC(obj) ((sdb_service_t *)(obj))
75 #define CONST_SVC(obj) ((const sdb_service_t *)(obj))
77 typedef struct {
78 sdb_store_obj_t super;
80 sdb_avltree_t *services;
81 sdb_avltree_t *attributes;
82 } sdb_host_t;
83 #define HOST(obj) ((sdb_host_t *)(obj))
84 #define CONST_HOST(obj) ((const sdb_host_t *)(obj))
86 /* shortcuts for accessing service/host attributes */
87 #define _last_update super.last_update
88 #define _interval super.interval
90 /*
91 * conditionals
92 */
94 /* compares a host using the specified conditional */
95 typedef int (*cmp_cb)(sdb_host_t *, sdb_store_cond_t *);
97 struct sdb_store_cond {
98 sdb_object_t super;
99 cmp_cb cmp;
100 };
102 typedef struct {
103 sdb_store_cond_t super;
104 char *name;
105 sdb_data_t value;
106 } attr_cond_t;
107 #define ATTR_C(obj) ((attr_cond_t *)(obj))
109 /*
110 * matchers
111 */
113 /* when adding to this, also update 'matchers' and 'matchers_tostring'
114 * in store_lookup.c */
115 enum {
116 MATCHER_OR,
117 MATCHER_AND,
118 MATCHER_NOT,
119 MATCHER_NAME,
120 MATCHER_ATTR,
121 MATCHER_LT,
122 MATCHER_LE,
123 MATCHER_EQ,
124 MATCHER_GE,
125 MATCHER_GT,
126 MATCHER_ISNULL,
127 };
129 #define MATCHER_SYM(t) \
130 (((t) == MATCHER_OR) ? "OR" \
131 : ((t) == MATCHER_AND) ? "AND" \
132 : ((t) == MATCHER_NOT) ? "NOT" \
133 : ((t) == MATCHER_NAME) ? "NAME" \
134 : ((t) == MATCHER_ATTR) ? "ATTR" \
135 : ((t) == MATCHER_LT) ? "<" \
136 : ((t) == MATCHER_LE) ? "<=" \
137 : ((t) == MATCHER_EQ) ? "=" \
138 : ((t) == MATCHER_GE) ? ">=" \
139 : ((t) == MATCHER_GT) ? ">" \
140 : ((t) == MATCHER_ISNULL) ? "IS NULL" \
141 : "UNKNOWN")
143 /* match the name of something */
144 typedef struct {
145 char *name;
146 regex_t *name_re;
147 } string_matcher_t;
149 /* matcher base type */
150 struct sdb_store_matcher {
151 sdb_object_t super;
152 /* type of the matcher */
153 int type;
154 };
155 #define M(m) ((sdb_store_matcher_t *)(m))
157 /* infix operator matcher */
158 typedef struct {
159 sdb_store_matcher_t super;
161 /* left and right hand operands */
162 sdb_store_matcher_t *left;
163 sdb_store_matcher_t *right;
164 } op_matcher_t;
165 #define OP_M(m) ((op_matcher_t *)(m))
167 /* unary operator matcher */
168 typedef struct {
169 sdb_store_matcher_t super;
171 /* operand */
172 sdb_store_matcher_t *op;
173 } uop_matcher_t;
174 #define UOP_M(m) ((uop_matcher_t *)(m))
176 /* match any type of object by it's name */
177 typedef struct {
178 sdb_store_matcher_t super;
180 int obj_type;
181 string_matcher_t name;
182 } name_matcher_t;
183 #define NAME_M(m) ((name_matcher_t *)(m))
185 /* match attributes */
186 typedef struct {
187 sdb_store_matcher_t super;
188 char *name;
189 string_matcher_t value;
190 } attr_matcher_t;
191 #define ATTR_M(m) ((attr_matcher_t *)(m))
193 typedef struct {
194 sdb_store_matcher_t super;
195 char *attr_name; /* we only support matching attributes */
196 } isnull_matcher_t;
197 #define ISNULL_M(m) ((isnull_matcher_t *)(m))
199 /* match using conditionals */
200 typedef struct {
201 sdb_store_matcher_t super;
202 sdb_store_cond_t *cond;
203 } cond_matcher_t;
204 #define COND_M(m) ((cond_matcher_t *)(m))
206 #ifdef __cplusplus
207 } /* extern "C" */
208 #endif
210 #endif /* ! SDB_CORE_STORE_H */
212 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */