533f4080ec17f049051f19a04b97111d226cfc33
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")
56 /*
57 * sdb_store_obj_t represents the super-class of any object stored in the
58 * database. It inherits from sdb_object_t and may safely be cast to a generic
59 * object to access its name.
60 */
61 struct sdb_store_obj;
62 typedef struct sdb_store_obj sdb_store_obj_t;
64 /*
65 * Queryable fields of a stored object.
66 */
67 enum {
68 SDB_FIELD_LAST_UPDATE = 1, /* datetime */
69 SDB_FIELD_AGE, /* datetime */
70 SDB_FIELD_INTERVAL, /* datetime */
71 SDB_FIELD_BACKEND, /* string */
72 };
74 #define SDB_FIELD_TO_NAME(f) \
75 (((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \
76 : ((f) == SDB_FIELD_AGE) ? "age" \
77 : ((f) == SDB_FIELD_INTERVAL) ? "interval" \
78 : ((f) == SDB_FIELD_BACKEND) ? "backend" : "unknown")
80 /*
81 * sdb_store_clear:
82 * Clear the entire store and remove all stored objects.
83 */
84 void
85 sdb_store_clear(void);
87 /*
88 * sdb_store_host:
89 * Add/update a host in the store. If the host, identified by its
90 * canonicalized name, already exists, it will be updated according to the
91 * specified name and timestamp. Else, a new entry will be created in the
92 * store. Any memory required for storing the entry will be allocated an
93 * managed by the store itself.
94 *
95 * Returns:
96 * - 0 on success
97 * - a positive value if the new entry is older than the currently stored
98 * entry (in this case, no update will happen)
99 * - a negative value on error
100 */
101 int
102 sdb_store_host(const char *name, sdb_time_t last_update);
104 /*
105 * sdb_store_has_host:
106 * sdb_store_get_host:
107 * Query the store for a host by its (canonicalized) name.
108 *
109 * sdb_store_get_host increments the ref count of the host object. The caller
110 * needs to deref it when no longer using it.
111 */
112 _Bool
113 sdb_store_has_host(const char *name);
115 sdb_store_obj_t *
116 sdb_store_get_host(const char *name);
118 /*
119 * sdb_store_attribute:
120 * Add/update a host's attribute in the store. If the attribute, identified by
121 * its key, already exists for the specified host, it will be updated to the
122 * specified values. If the referenced host does not exist, an error will be
123 * reported. Else, a new entry will be created in the store. Any memory
124 * required for storing the entry will be allocated and managed by the store
125 * itself.
126 *
127 * Returns:
128 * - 0 on success
129 * - a positive value if the new entry is older than the currently stored
130 * entry (in this case, no update will happen)
131 * - a negative value on error
132 */
133 int
134 sdb_store_attribute(const char *hostname,
135 const char *key, const sdb_data_t *value,
136 sdb_time_t last_update);
138 /*
139 * sdb_store_service:
140 * Add/update a service in the store. If the service, identified by its name,
141 * already exists for the specified host, it will be updated according to the
142 * specified 'service' object. If the referenced host does not exist, an error
143 * will be reported. Else, a new entry will be created in the store. Any
144 * memory required for storing the entry will be allocated an managed by the
145 * store itself. The specified service-object will not be referenced or
146 * further accessed.
147 *
148 * Returns:
149 * - 0 on success
150 * - a positive value if the new entry is older than the currently stored
151 * entry (in this case, no update will happen)
152 * - a negative value on error
153 */
154 int
155 sdb_store_service(const char *hostname, const char *name,
156 sdb_time_t last_update);
158 /*
159 * sdb_store_service_attr:
160 * Add/update a service's attribute in the store. If the attribute, identified
161 * by its key, already exists for the specified service, it will be updated to
162 * the specified value. If the references service (for the specified host)
163 * does not exist, an error will be reported. Any memory required for storing
164 * the entry will be allocated and managed by the store itself.
165 *
166 * Returns:
167 * - 0 on success
168 * - a positive value if the new entry is older than the currently stored
169 * entry (in this case, no update will happen)
170 * - a negative value on error
171 */
172 int
173 sdb_store_service_attr(const char *hostname, const char *service,
174 const char *key, const sdb_data_t *value, sdb_time_t last_update);
176 /*
177 * sdb_store_get_field:
178 * Get the value of a stored object's queryable field. The caller is
179 * responsible for freeing any dynamically allocated memory possibly stored in
180 * the returned value.
181 *
182 * Note: Retrieving the backend this way is not currently supported.
183 *
184 * Returns:
185 * - 0 on success
186 * - a negative value else
187 */
188 int
189 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
191 /*
192 * Expressions specify arithmetic expressions.
193 *
194 * A expression object inherits from sdb_object_t and, thus, may safely be
195 * cast to a generic object.
196 */
197 struct sdb_store_expr;
198 typedef struct sdb_store_expr sdb_store_expr_t;
199 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
201 /*
202 * sdb_store_expr_create:
203 * Creates an arithmetic expression implementing the specified operator on the
204 * specified left and right operand.
205 *
206 * Returns:
207 * - an expression object on success
208 * - NULL else
209 */
210 sdb_store_expr_t *
211 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
213 /*
214 * sdb_store_expr_constvalue:
215 * Creates an expression which evaluates to the specified constant value.
216 *
217 * Returns:
218 * - an expression object on success
219 * - NULL else
220 */
221 sdb_store_expr_t *
222 sdb_store_expr_constvalue(const sdb_data_t *value);
224 /*
225 * sdb_store_expr_eval:
226 * Evaluate an expression and stores the result in 'res'. The result's value
227 * will be allocated dynamically if necessary and, thus, should be free'd by
228 * the caller (e.g. using sdb_data_free_datum);
229 *
230 * Returns:
231 * - 0 on success
232 * - a negative value else
233 */
234 int
235 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_data_t *res);
237 /*
238 * Conditionals may be used to lookup hosts from the store based on a
239 * conditional expression.
240 *
241 * A conditional object inherits from sdb_object_t and, thus, may safely be
242 * cast to a generic object.
243 */
244 struct sdb_store_cond;
245 typedef struct sdb_store_cond sdb_store_cond_t;
246 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
248 /*
249 * sdb_store_attr_cond:
250 * Creates a conditional based on attribute values. The value of stored
251 * attributes is compared against the value the expression evaluates to. See
252 * sdb_data_cmp for details about the comparison.
253 */
254 sdb_store_cond_t *
255 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr);
257 /*
258 * sdb_store_obj_cond:
259 * Creates a conditional based on queryable object fields. The respective
260 * field of *any* object type is compared against the value the expression
261 * evaluates to.
262 */
263 sdb_store_cond_t *
264 sdb_store_obj_cond(int field, sdb_store_expr_t *expr);
266 /*
267 * Store matchers may be used to lookup hosts from the store based on their
268 * various attributes. Service and attribute matchers are applied to a host's
269 * services and attributes and evaluate to true if *any* service or attribute
270 * matches.
271 *
272 * A store matcher object inherits from sdb_object_t and, thus, may safely be
273 * cast to a generic object.
274 */
275 struct sdb_store_matcher;
276 typedef struct sdb_store_matcher sdb_store_matcher_t;
277 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
279 /*
280 * sdb_store_name_matcher:
281 * Creates a matcher matching by the specified object type's name. If 're' is
282 * true, the specified name is treated as a POSIX extended regular expression.
283 * Else, the exact name has to match (case-insensitive).
284 */
285 sdb_store_matcher_t *
286 sdb_store_name_matcher(int type, const char *name, _Bool re);
288 /*
289 * sdb_store_attr_matcher:
290 * Creates a matcher matching attributes based on their value. If 're' is
291 * true, the specified name is treated as a POSIX extended regular expression.
292 * Else, the exact name has to match (case-insensitive).
293 */
294 sdb_store_matcher_t *
295 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
297 /*
298 * sdb_store_isnull_matcher:
299 * Creates a matcher matching "missing" attributes.
300 */
301 sdb_store_matcher_t *
302 sdb_store_isnull_matcher(const char *attr_name);
304 /*
305 * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
306 * sdb_store_ge_matcher, sdb_store_gt_matcher:
307 * Creates a matcher based on a conditional. The matcher matches objects for
308 * which the conditional evaluates the object to compare less than, less or
309 * equal, equal, greater or equal, or greater than the conditional's value
310 * repsectively.
311 */
312 sdb_store_matcher_t *
313 sdb_store_lt_matcher(sdb_store_cond_t *cond);
314 sdb_store_matcher_t *
315 sdb_store_le_matcher(sdb_store_cond_t *cond);
316 sdb_store_matcher_t *
317 sdb_store_eq_matcher(sdb_store_cond_t *cond);
318 sdb_store_matcher_t *
319 sdb_store_ge_matcher(sdb_store_cond_t *cond);
320 sdb_store_matcher_t *
321 sdb_store_gt_matcher(sdb_store_cond_t *cond);
323 /*
324 * sdb_store_matcher_parse_cmp:
325 * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
326 *
327 * Returns:
328 * - a matcher object on success
329 * - NULL else
330 */
331 sdb_store_matcher_t *
332 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
333 const char *op, sdb_store_expr_t *expr);
335 /*
336 * sdb_store_matcher_parse_field_cmp:
337 * Parse a simple compare expression for queryable object fields (<field> <op>
338 * <expression>).
339 *
340 * Returns:
341 * - a matcher object on success
342 * - NULL else
343 */
344 sdb_store_matcher_t *
345 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
346 sdb_store_expr_t *expr);
348 /*
349 * sdb_store_dis_matcher:
350 * Creates a matcher matching the disjunction (logical OR) of two matchers.
351 */
352 sdb_store_matcher_t *
353 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
355 /*
356 * sdb_store_con_matcher:
357 * Creates a matcher matching the conjunction (logical AND) of two matchers.
358 */
359 sdb_store_matcher_t *
360 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
362 /*
363 * sdb_store_con_matcher::
364 * Creates a matcher matching the inverse (logical NOT) of a matcher.
365 */
366 sdb_store_matcher_t *
367 sdb_store_inv_matcher(sdb_store_matcher_t *m);
369 /*
370 * sdb_store_matcher_matches:
371 * Check whether the specified matcher matches the specified store object. If
372 * specified, the filter will be used to preselect objects for further
373 * evaluation. It is applied to any object that's used during the evaluation
374 * of the matcher. Only those objects matching the filter will be considered.
375 *
376 * Note that the filter is applied to all object types (hosts, service,
377 * attribute). Thus, any object-specific matchers are mostly unsuited for this
378 * purpose and, if used, may result in unexpected behavior.
379 *
380 * Returns:
381 * - 1 if the object matches
382 * - 0 else
383 */
384 int
385 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
386 sdb_store_matcher_t *filter);
388 /*
389 * sdb_store_matcher_tostring:
390 * Format a matcher object as string. This is meant for logging or debugging
391 * purposes.
392 */
393 char *
394 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
396 /*
397 * sdb_store_lookup_cb:
398 * Lookup callback. It is called for each matching object when looking up data
399 * in the store. The lookup aborts if the callback returns non-zero.
400 */
401 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
403 /*
404 * sdb_store_scan:
405 * Look up objects in the store. The specified callback function is called for
406 * each object in the store matching 'm'. The function performs a full scan of
407 * all hosts stored in the database. If specified, the filter will be used to
408 * preselect objects for further evaluation. See the description of
409 * 'sdb_store_matcher_matches' for details.
410 *
411 * Returns:
412 * - 0 on success
413 * - a negative value else
414 */
415 int
416 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
417 sdb_store_lookup_cb cb, void *user_data);
419 /*
420 * Flags for serialization functions.
421 *
422 * By default, the full object will be included in the serialized output. When
423 * specifying any of the flags, the respective information will be left out.
424 */
425 enum {
426 SDB_SKIP_ATTRIBUTES = 1 << 0,
427 SDB_SKIP_SERVICES = 1 << 1,
428 SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 2,
430 SDB_SKIP_ALL = 0xffff,
431 };
433 /*
434 * sdb_store_tojson:
435 * Serialize the entire store to JSON and append the result to the specified
436 * buffer. If specified, only objects matching the filter will be included in
437 * the result (see sdb_store_host_tojson for details).
438 *
439 * Returns:
440 * - 0 on success
441 * - a negative value on error
442 */
443 int
444 sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags);
446 /*
447 * sdb_store_host_tojson:
448 * Serialize a host object to JSON and append the result to the specified
449 * buffer. If specified, only objects matching the filter will be included in
450 * the result. The filter is applied to each object individually and, thus,
451 * should not be of any object-type specific kind. The filter is never applied
452 * to the specified host object; the caller is responsible for this and for
453 * correctly handling skipped hosts.
454 *
455 * Returns:
456 * - 0 on success
457 * - a negative value on error
458 */
459 int
460 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf,
461 sdb_store_matcher_t *filter, int flags);
463 /*
464 * sdb_store_iter_cb:
465 * Store iterator callback. Iteration stops if the callback returns non-zero.
466 */
467 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
469 /*
470 * sdb_store_iterate:
471 * Iterate the entire store, calling the specified callback for each object.
472 * The user_data pointer is passed on to each call of the callback.
473 *
474 * Returns:
475 * - 0 on success
476 * - a negative value else
477 */
478 int
479 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
481 #ifdef __cplusplus
482 } /* extern "C" */
483 #endif
485 #endif /* ! SDB_CORE_STORE_H */
487 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */