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