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_fieldvalue:
215 * Creates an expression which evaluates to the value of the specified
216 * queryable field of a stored object.
217 *
218 * Returns:
219 * - an expression object on success
220 * - NULL else
221 */
222 sdb_store_expr_t *
223 sdb_store_expr_fieldvalue(int field);
225 /*
226 * sdb_store_expr_constvalue:
227 * Creates an expression which evaluates to the specified constant value.
228 *
229 * Returns:
230 * - an expression object on success
231 * - NULL else
232 */
233 sdb_store_expr_t *
234 sdb_store_expr_constvalue(const sdb_data_t *value);
236 /*
237 * sdb_store_expr_eval:
238 * Evaluate an expression for the specified stored object and stores the
239 * result in 'res'. The result's value will be allocated dynamically if
240 * necessary and, thus, should be free'd by the caller (e.g. using
241 * sdb_data_free_datum). The object may be NULL, in which case the expression
242 * needs to evaluate to a constant value.
243 *
244 * Returns:
245 * - 0 on success
246 * - a negative value else
247 */
248 int
249 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
250 sdb_data_t *res);
252 /*
253 * Conditionals may be used to lookup hosts from the store based on a
254 * conditional expression.
255 *
256 * A conditional object inherits from sdb_object_t and, thus, may safely be
257 * cast to a generic object.
258 */
259 struct sdb_store_cond;
260 typedef struct sdb_store_cond sdb_store_cond_t;
261 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
263 /*
264 * sdb_store_attr_cond:
265 * Creates a conditional based on attribute values. The value of stored
266 * attributes is compared against the value the expression evaluates to. See
267 * sdb_data_cmp for details about the comparison.
268 */
269 sdb_store_cond_t *
270 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr);
272 /*
273 * sdb_store_obj_cond:
274 * Creates a conditional based on queryable object fields. The respective
275 * field of *any* object type is compared against the value the expression
276 * evaluates to.
277 */
278 sdb_store_cond_t *
279 sdb_store_obj_cond(int field, sdb_store_expr_t *expr);
281 /*
282 * Store matchers may be used to lookup hosts from the store based on their
283 * various attributes. Service and attribute matchers are applied to a host's
284 * services and attributes and evaluate to true if *any* service or attribute
285 * matches.
286 *
287 * A store matcher object inherits from sdb_object_t and, thus, may safely be
288 * cast to a generic object.
289 */
290 struct sdb_store_matcher;
291 typedef struct sdb_store_matcher sdb_store_matcher_t;
292 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
294 /*
295 * sdb_store_name_matcher:
296 * Creates a matcher matching by the specified object type's name. If 're' is
297 * true, the specified name is treated as a POSIX extended regular expression.
298 * Else, the exact name has to match (case-insensitive).
299 */
300 sdb_store_matcher_t *
301 sdb_store_name_matcher(int type, const char *name, _Bool re);
303 /*
304 * sdb_store_attr_matcher:
305 * Creates a matcher matching attributes based on their value. If 're' is
306 * true, the specified name is treated as a POSIX extended regular expression.
307 * Else, the exact name has to match (case-insensitive).
308 */
309 sdb_store_matcher_t *
310 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
312 /*
313 * sdb_store_isnull_matcher:
314 * Creates a matcher matching "missing" attributes.
315 */
316 sdb_store_matcher_t *
317 sdb_store_isnull_matcher(const char *attr_name);
319 /*
320 * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
321 * sdb_store_ge_matcher, sdb_store_gt_matcher:
322 * Creates a matcher based on a conditional. The matcher matches objects for
323 * which the conditional evaluates the object to compare less than, less or
324 * equal, equal, greater or equal, or greater than the conditional's value
325 * repsectively.
326 */
327 sdb_store_matcher_t *
328 sdb_store_lt_matcher(sdb_store_cond_t *cond);
329 sdb_store_matcher_t *
330 sdb_store_le_matcher(sdb_store_cond_t *cond);
331 sdb_store_matcher_t *
332 sdb_store_eq_matcher(sdb_store_cond_t *cond);
333 sdb_store_matcher_t *
334 sdb_store_ge_matcher(sdb_store_cond_t *cond);
335 sdb_store_matcher_t *
336 sdb_store_gt_matcher(sdb_store_cond_t *cond);
338 /*
339 * sdb_store_parse_field_name:
340 * Parse the name of a stored object's queryable field.
341 *
342 * Returns:
343 * - the field id on success
344 * - a negative value else
345 */
346 int
347 sdb_store_parse_field_name(const char *name);
349 /*
350 * sdb_store_matcher_parse_cmp:
351 * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
352 *
353 * Returns:
354 * - a matcher object on success
355 * - NULL else
356 */
357 sdb_store_matcher_t *
358 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
359 const char *op, sdb_store_expr_t *expr);
361 /*
362 * sdb_store_matcher_parse_field_cmp:
363 * Parse a simple compare expression for queryable object fields (<field> <op>
364 * <expression>).
365 *
366 * Returns:
367 * - a matcher object on success
368 * - NULL else
369 */
370 sdb_store_matcher_t *
371 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
372 sdb_store_expr_t *expr);
374 /*
375 * sdb_store_dis_matcher:
376 * Creates a matcher matching the disjunction (logical OR) of two matchers.
377 */
378 sdb_store_matcher_t *
379 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
381 /*
382 * sdb_store_con_matcher:
383 * Creates a matcher matching the conjunction (logical AND) of two matchers.
384 */
385 sdb_store_matcher_t *
386 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
388 /*
389 * sdb_store_con_matcher::
390 * Creates a matcher matching the inverse (logical NOT) of a matcher.
391 */
392 sdb_store_matcher_t *
393 sdb_store_inv_matcher(sdb_store_matcher_t *m);
395 /*
396 * sdb_store_matcher_matches:
397 * Check whether the specified matcher matches the specified store object. If
398 * specified, the filter will be used to preselect objects for further
399 * evaluation. It is applied to any object that's used during the evaluation
400 * of the matcher. Only those objects matching the filter will be considered.
401 *
402 * Note that the filter is applied to all object types (hosts, service,
403 * attribute). Thus, any object-specific matchers are mostly unsuited for this
404 * purpose and, if used, may result in unexpected behavior.
405 *
406 * Returns:
407 * - 1 if the object matches
408 * - 0 else
409 */
410 int
411 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
412 sdb_store_matcher_t *filter);
414 /*
415 * sdb_store_matcher_tostring:
416 * Format a matcher object as string. This is meant for logging or debugging
417 * purposes.
418 */
419 char *
420 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
422 /*
423 * sdb_store_lookup_cb:
424 * Lookup callback. It is called for each matching object when looking up data
425 * in the store. The lookup aborts if the callback returns non-zero.
426 */
427 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
429 /*
430 * sdb_store_scan:
431 * Look up objects in the store. The specified callback function is called for
432 * each object in the store matching 'm'. The function performs a full scan of
433 * all hosts stored in the database. If specified, the filter will be used to
434 * preselect objects for further evaluation. See the description of
435 * 'sdb_store_matcher_matches' for details.
436 *
437 * Returns:
438 * - 0 on success
439 * - a negative value else
440 */
441 int
442 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
443 sdb_store_lookup_cb cb, void *user_data);
445 /*
446 * Flags for serialization functions.
447 *
448 * By default, the full object will be included in the serialized output. When
449 * specifying any of the flags, the respective information will be left out.
450 */
451 enum {
452 SDB_SKIP_ATTRIBUTES = 1 << 0,
453 SDB_SKIP_SERVICES = 1 << 1,
454 SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 2,
456 SDB_SKIP_ALL = 0xffff,
457 };
459 /*
460 * sdb_store_tojson:
461 * Serialize the entire store to JSON and append the result to the specified
462 * buffer. If specified, only objects matching the filter will be included in
463 * the result (see sdb_store_host_tojson for details).
464 *
465 * Returns:
466 * - 0 on success
467 * - a negative value on error
468 */
469 int
470 sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags);
472 /*
473 * sdb_store_host_tojson:
474 * Serialize a host object to JSON and append the result to the specified
475 * buffer. If specified, only objects matching the filter will be included in
476 * the result. The filter is applied to each object individually and, thus,
477 * should not be of any object-type specific kind. The filter is never applied
478 * to the specified host object; the caller is responsible for this and for
479 * correctly handling skipped hosts.
480 *
481 * Returns:
482 * - 0 on success
483 * - a negative value on error
484 */
485 int
486 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf,
487 sdb_store_matcher_t *filter, int flags);
489 /*
490 * sdb_store_iter_cb:
491 * Store iterator callback. Iteration stops if the callback returns non-zero.
492 */
493 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
495 /*
496 * sdb_store_iterate:
497 * Iterate the entire store, calling the specified callback for each object.
498 * The user_data pointer is passed on to each call of the callback.
499 *
500 * Returns:
501 * - 0 on success
502 * - a negative value else
503 */
504 int
505 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
507 #ifdef __cplusplus
508 } /* extern "C" */
509 #endif
511 #endif /* ! SDB_CORE_STORE_H */
513 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */