1 /*
2 * SysDB - src/include/core/store.h
3 * Copyright (C) 2012 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 #ifndef SDB_CORE_STORE_H
29 #define SDB_CORE_STORE_H 1
31 #include "sysdb.h"
32 #include "core/object.h"
33 #include "core/data.h"
34 #include "core/time.h"
35 #include "utils/strbuf.h"
37 #include <stdio.h>
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
43 /*
44 * Store object types.
45 */
46 enum {
47 SDB_HOST = 1,
48 SDB_SERVICE,
49 SDB_ATTRIBUTE,
50 };
51 #define SDB_STORE_TYPE_TO_NAME(t) \
52 (((t) == SDB_HOST) ? "host" \
53 : ((t) == SDB_SERVICE) ? "service" \
54 : ((t) == SDB_ATTRIBUTE) ? "attribute" : "unknown")
57 /*
58 * sdb_store_obj_t represents the super-class of any object stored in the
59 * database. It inherits from sdb_object_t and may safely be cast to a generic
60 * object to access its name.
61 */
62 struct sdb_store_obj;
63 typedef struct sdb_store_obj sdb_store_obj_t;
65 /*
66 * sdb_store_clear:
67 * Clear the entire store and remove all stored objects.
68 */
69 void
70 sdb_store_clear(void);
72 /*
73 * sdb_store_host:
74 * Add/update a host in the store. If the host, identified by its
75 * canonicalized name, already exists, it will be updated according to the
76 * specified name and timestamp. Else, a new entry will be created in the
77 * store. Any memory required for storing the entry will be allocated an
78 * managed by the store itself.
79 *
80 * Returns:
81 * - 0 on success
82 * - a positive value if the new entry is older than the currently stored
83 * entry (in this case, no update will happen)
84 * - a negative value on error
85 */
86 int
87 sdb_store_host(const char *name, sdb_time_t last_update);
89 /*
90 * sdb_store_has_host:
91 * sdb_store_get_host:
92 * Query the store for a host by its (canonicalized) name.
93 *
94 * sdb_store_get_host increments the ref count of the host object. The caller
95 * needs to deref it when no longer using it.
96 */
97 _Bool
98 sdb_store_has_host(const char *name);
100 sdb_store_obj_t *
101 sdb_store_get_host(const char *name);
103 /*
104 * sdb_store_attribute:
105 * Add/update a host's attribute in the store. If the attribute, identified by
106 * its key, already exists for the specified host, it will be updated to the
107 * specified values. If the referenced host does not exist, an error will be
108 * reported. Else, a new entry will be created in the store. Any memory
109 * required for storing the entry will be allocated and managed by the store
110 * itself.
111 *
112 * Returns:
113 * - 0 on success
114 * - a positive value if the new entry is older than the currently stored
115 * entry (in this case, no update will happen)
116 * - a negative value on error
117 */
118 int
119 sdb_store_attribute(const char *hostname,
120 const char *key, const sdb_data_t *value,
121 sdb_time_t last_update);
123 /*
124 * sdb_store_service:
125 * Add/update a service in the store. If the service, identified by its name,
126 * already exists for the specified host, it will be updated according to the
127 * specified 'service' object. If the referenced host does not exist, an error
128 * will be reported. Else, a new entry will be created in the store. Any
129 * memory required for storing the entry will be allocated an managed by the
130 * store itself. The specified service-object will not be referenced or
131 * further accessed.
132 *
133 * Returns:
134 * - 0 on success
135 * - a positive value if the new entry is older than the currently stored
136 * entry (in this case, no update will happen)
137 * - a negative value on error
138 */
139 int
140 sdb_store_service(const char *hostname, const char *name,
141 sdb_time_t last_update);
143 /*
144 * sdb_store_service_attr:
145 * Add/update a service's attribute in the store. If the attribute, identified
146 * by its key, already exists for the specified service, it will be updated to
147 * the specified value. If the references service (for the specified host)
148 * does not exist, an error will be reported. Any memory required for storing
149 * the entry will be allocated and managed by the store itself.
150 *
151 * Returns:
152 * - 0 on success
153 * - a positive value if the new entry is older than the currently stored
154 * entry (in this case, no update will happen)
155 * - a negative value on error
156 */
157 int
158 sdb_store_service_attr(const char *hostname, const char *service,
159 const char *key, const sdb_data_t *value, sdb_time_t last_update);
161 /*
162 * Conditionals may be used to lookup hosts from the store based on a
163 * conditional expression.
164 *
165 * A conditional object inherits from sdb_object_t and, thus, may safely be
166 * cast to a generic object.
167 */
168 struct sdb_store_cond;
169 typedef struct sdb_store_cond sdb_store_cond_t;
170 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
172 /*
173 * sdb_store_attr_cond:
174 * Creates a conditional based on attribute values. The value of stored
175 * attributes is compared against the specified value. See sdb_data_cmp for
176 * details about the comparison.
177 */
178 sdb_store_cond_t *
179 sdb_store_attr_cond(const char *name, const sdb_data_t *value);
181 /*
182 * Store matchers may be used to lookup hosts from the store based on their
183 * various attributes. Service and attribute matchers are applied to a host's
184 * services and attributes and evaluate to true if *any* service or attribute
185 * matches.
186 *
187 * A store matcher object inherits from sdb_object_t and, thus, may safely be
188 * cast to a generic object.
189 */
190 struct sdb_store_matcher;
191 typedef struct sdb_store_matcher sdb_store_matcher_t;
192 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
194 /*
195 * sdb_store_name_matcher:
196 * Creates a matcher matching by the specified object type's name. If 're' is
197 * true, the specified name is treated as a POSIX extended regular expression.
198 * Else, the exact name has to match (case-insensitive).
199 */
200 sdb_store_matcher_t *
201 sdb_store_name_matcher(int type, const char *name, _Bool re);
203 /*
204 * sdb_store_attr_matcher:
205 * Creates a matcher matching attributes based on their value. If 're' is
206 * true, the specified name is treated as a POSIX extended regular expression.
207 * Else, the exact name has to match (case-insensitive).
208 */
209 sdb_store_matcher_t *
210 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
212 /*
213 * sdb_store_isnull_matcher:
214 * Creates a matcher matching "missing" attributes.
215 */
216 sdb_store_matcher_t *
217 sdb_store_isnull_matcher(const char *attr_name);
219 /*
220 * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
221 * sdb_store_ge_matcher, sdb_store_gt_matcher:
222 * Creates a matcher based on a conditional. The matcher matches objects for
223 * which the conditional evaluates the object to compare less than, less or
224 * equal, equal, greater or equal, or greater than the conditional's value
225 * repsectively.
226 */
227 sdb_store_matcher_t *
228 sdb_store_lt_matcher(sdb_store_cond_t *cond);
229 sdb_store_matcher_t *
230 sdb_store_le_matcher(sdb_store_cond_t *cond);
231 sdb_store_matcher_t *
232 sdb_store_eq_matcher(sdb_store_cond_t *cond);
233 sdb_store_matcher_t *
234 sdb_store_ge_matcher(sdb_store_cond_t *cond);
235 sdb_store_matcher_t *
236 sdb_store_gt_matcher(sdb_store_cond_t *cond);
238 /*
239 * sdb_store_matcher_parse_cmp:
240 * Parse a simple compare expression (<obj_type>.<attr> <op> <value>).
241 *
242 * Returns:
243 * - a matcher object on success
244 * - NULL else
245 */
246 sdb_store_matcher_t *
247 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
248 const char *op, const sdb_data_t *value);
250 /*
251 * sdb_store_dis_matcher:
252 * Creates a matcher matching the disjunction (logical OR) of two matchers.
253 */
254 sdb_store_matcher_t *
255 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
257 /*
258 * sdb_store_con_matcher:
259 * Creates a matcher matching the conjunction (logical AND) of two matchers.
260 */
261 sdb_store_matcher_t *
262 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
264 /*
265 * sdb_store_con_matcher::
266 * Creates a matcher matching the inverse (logical NOT) of a matcher.
267 */
268 sdb_store_matcher_t *
269 sdb_store_inv_matcher(sdb_store_matcher_t *m);
271 /*
272 * sdb_store_matcher_matches:
273 * Check whether the specified matcher matches the specified store object.
274 *
275 * Returns:
276 * - 1 if the object matches
277 * - 0 else
278 */
279 int
280 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj);
282 /*
283 * sdb_store_matcher_tostring:
284 * Format a matcher object as string. This is meant for logging or debugging
285 * purposes.
286 */
287 char *
288 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
290 /*
291 * sdb_store_lookup_cb:
292 * Lookup callback. It is called for each matching object when looking up data
293 * in the store. The lookup aborts if the callback returns non-zero.
294 */
295 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
297 /*
298 * sdb_store_lookup:
299 * Look up objects in the store. The specified callback function is called for
300 * each object in the store matching 'm'.
301 *
302 * Returns:
303 * - 0 on success
304 * - a negative value else
305 */
306 int
307 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
308 void *user_data);
310 /*
311 * Flags for serialization functions.
312 *
313 * By default, the full object will be included in the serialized output. When
314 * specifying any of the flags, the respective information will be left out.
315 */
316 enum {
317 SDB_SKIP_ATTRIBUTES = 1 << 0,
318 SDB_SKIP_SERVICES = 1 << 1,
319 SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 2,
321 SDB_SKIP_ALL = 0xffff,
322 };
324 /*
325 * sdb_store_tojson:
326 * Serialize the entire store to JSON and append the result to the specified
327 * buffer.
328 *
329 * Returns:
330 * - 0 on success
331 * - a negative value on error
332 */
333 int
334 sdb_store_tojson(sdb_strbuf_t *buf, int flags);
336 /*
337 * sdb_store_host_tojson:
338 * Serialize a host object to JSON and append the result to the specified
339 * buffer.
340 *
341 * Returns:
342 * - 0 on success
343 * - a negative value on error
344 */
345 int
346 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf, int flags);
348 /*
349 * sdb_store_iter_cb:
350 * Store iterator callback. Iteration stops if the callback returns non-zero.
351 */
352 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
354 /*
355 * sdb_store_iterate:
356 * Iterate the entire store, calling the specified callback for each object.
357 * The user_data pointer is passed on to each call of the callback.
358 *
359 * Returns:
360 * - 0 on success
361 * - a negative value else
362 */
363 int
364 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
366 #ifdef __cplusplus
367 } /* extern "C" */
368 #endif
370 #endif /* ! SDB_CORE_STORE_H */
372 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */