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.
148 *
149 * Returns:
150 * - 0 on success
151 * - a positive value if the new entry is older than the currently stored
152 * entry (in this case, no update will happen)
153 * - a negative value on error
154 */
155 int
156 sdb_store_service(const char *hostname, const char *name,
157 sdb_time_t last_update);
159 /*
160 * sdb_store_service_attr:
161 * Add/update a service's attribute in the store. If the attribute, identified
162 * by its key, already exists for the specified service, it will be updated to
163 * the specified value. If the references service (for the specified host)
164 * does not exist, an error will be reported. Any memory required for storing
165 * the entry will be allocated and managed by the store itself.
166 *
167 * Returns:
168 * - 0 on success
169 * - a positive value if the new entry is older than the currently stored
170 * entry (in this case, no update will happen)
171 * - a negative value on error
172 */
173 int
174 sdb_store_service_attr(const char *hostname, const char *service,
175 const char *key, const sdb_data_t *value, sdb_time_t last_update);
177 /*
178 * A metric store describes how to access a metric's data.
179 */
180 typedef struct {
181 const char *type;
182 const char *id;
183 } sdb_metric_store_t;
185 /*
186 * sdb_store_metric:
187 * Add/update a metric in the store. If the metric, identified by its name,
188 * already exists for the specified host, it will be updated according to the
189 * specified 'metric' object. If the referenced host does not exist, an error
190 * will be reported. Else, a new entry will be created in the store. Any
191 * memory required for storing the entry will be allocated an managed by the
192 * store itself.
193 *
194 * If specified, the metric store describes where to access the metric's data.
195 *
196 * Returns:
197 * - 0 on success
198 * - a positive value if the new entry is older than the currently stored
199 * entry (in this case, no update will happen)
200 * - a negative value on error
201 */
202 int
203 sdb_store_metric(const char *hostname, const char *name,
204 sdb_metric_store_t *store, sdb_time_t last_update);
206 /*
207 * sdb_store_metric_attr:
208 * Add/update a metric's attribute in the store. If the attribute, identified
209 * by its key, already exists for the specified metric, it will be updated to
210 * the specified value. If the references metric (for the specified host)
211 * does not exist, an error will be reported. Any memory required for storing
212 * the entry will be allocated and managed by the store itself.
213 *
214 * Returns:
215 * - 0 on success
216 * - a positive value if the new entry is older than the currently stored
217 * entry (in this case, no update will happen)
218 * - a negative value on error
219 */
220 int
221 sdb_store_metric_attr(const char *hostname, const char *metric,
222 const char *key, const sdb_data_t *value, sdb_time_t last_update);
224 /*
225 * sdb_store_get_field:
226 * Get the value of a stored object's queryable field. The caller is
227 * responsible for freeing any dynamically allocated memory possibly stored in
228 * the returned value.
229 *
230 * Note: Retrieving the backend this way is not currently supported.
231 *
232 * Returns:
233 * - 0 on success
234 * - a negative value else
235 */
236 int
237 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
239 /*
240 * Expressions specify arithmetic expressions.
241 *
242 * A expression object inherits from sdb_object_t and, thus, may safely be
243 * cast to a generic object.
244 */
245 struct sdb_store_expr;
246 typedef struct sdb_store_expr sdb_store_expr_t;
247 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
249 /*
250 * sdb_store_expr_create:
251 * Creates an arithmetic expression implementing the specified operator on the
252 * specified left and right operand.
253 *
254 * Returns:
255 * - an expression object on success
256 * - NULL else
257 */
258 sdb_store_expr_t *
259 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
261 /*
262 * sdb_store_expr_fieldvalue:
263 * Creates an expression which evaluates to the value of the specified
264 * queryable field of a stored object.
265 *
266 * Returns:
267 * - an expression object on success
268 * - NULL else
269 */
270 sdb_store_expr_t *
271 sdb_store_expr_fieldvalue(int field);
273 /*
274 * sdb_store_expr_constvalue:
275 * Creates an expression which evaluates to the specified constant value.
276 *
277 * Returns:
278 * - an expression object on success
279 * - NULL else
280 */
281 sdb_store_expr_t *
282 sdb_store_expr_constvalue(const sdb_data_t *value);
284 /*
285 * sdb_store_expr_eval:
286 * Evaluate an expression for the specified stored object and stores the
287 * result in 'res'. The result's value will be allocated dynamically if
288 * necessary and, thus, should be free'd by the caller (e.g. using
289 * sdb_data_free_datum). The object may be NULL, in which case the expression
290 * needs to evaluate to a constant value.
291 *
292 * Returns:
293 * - 0 on success
294 * - a negative value else
295 */
296 int
297 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
298 sdb_data_t *res);
300 /*
301 * Conditionals may be used to lookup hosts from the store based on a
302 * conditional expression.
303 *
304 * A conditional object inherits from sdb_object_t and, thus, may safely be
305 * cast to a generic object.
306 */
307 struct sdb_store_cond;
308 typedef struct sdb_store_cond sdb_store_cond_t;
309 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
311 /*
312 * sdb_store_attr_cond:
313 * Creates a conditional based on attribute values. The value of stored
314 * attributes is compared against the value the expression evaluates to. See
315 * sdb_data_cmp for details about the comparison.
316 */
317 sdb_store_cond_t *
318 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr);
320 /*
321 * sdb_store_obj_cond:
322 * Creates a conditional based on queryable object fields. The respective
323 * field of *any* object type is compared against the value the expression
324 * evaluates to.
325 */
326 sdb_store_cond_t *
327 sdb_store_obj_cond(int field, sdb_store_expr_t *expr);
329 /*
330 * Store matchers may be used to lookup hosts from the store based on their
331 * various attributes. Service and attribute matchers are applied to a host's
332 * services and attributes and evaluate to true if *any* service or attribute
333 * matches.
334 *
335 * A store matcher object inherits from sdb_object_t and, thus, may safely be
336 * cast to a generic object.
337 */
338 struct sdb_store_matcher;
339 typedef struct sdb_store_matcher sdb_store_matcher_t;
340 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
342 /*
343 * sdb_store_name_matcher:
344 * Creates a matcher matching by the specified object type's name. If 're' is
345 * true, the specified name is treated as a POSIX extended regular expression.
346 * Else, the exact name has to match (case-insensitive).
347 */
348 sdb_store_matcher_t *
349 sdb_store_name_matcher(int type, const char *name, _Bool re);
351 /*
352 * sdb_store_attr_matcher:
353 * Creates a matcher matching attributes based on their value. If 're' is
354 * true, the specified name is treated as a POSIX extended regular expression.
355 * Else, the exact name has to match (case-insensitive).
356 */
357 sdb_store_matcher_t *
358 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
360 /*
361 * sdb_store_isnull_matcher:
362 * Creates a matcher matching "missing" attributes.
363 */
364 sdb_store_matcher_t *
365 sdb_store_isnull_matcher(const char *attr_name);
367 /*
368 * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
369 * sdb_store_ge_matcher, sdb_store_gt_matcher:
370 * Creates a matcher based on a conditional. The matcher matches objects for
371 * which the conditional evaluates the object to compare less than, less or
372 * equal, equal, greater or equal, or greater than the conditional's value
373 * repsectively.
374 */
375 sdb_store_matcher_t *
376 sdb_store_lt_matcher(sdb_store_cond_t *cond);
377 sdb_store_matcher_t *
378 sdb_store_le_matcher(sdb_store_cond_t *cond);
379 sdb_store_matcher_t *
380 sdb_store_eq_matcher(sdb_store_cond_t *cond);
381 sdb_store_matcher_t *
382 sdb_store_ge_matcher(sdb_store_cond_t *cond);
383 sdb_store_matcher_t *
384 sdb_store_gt_matcher(sdb_store_cond_t *cond);
386 /*
387 * sdb_store_parse_field_name:
388 * Parse the name of a stored object's queryable field.
389 *
390 * Returns:
391 * - the field id on success
392 * - a negative value else
393 */
394 int
395 sdb_store_parse_field_name(const char *name);
397 /*
398 * sdb_store_matcher_parse_cmp:
399 * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
400 *
401 * Returns:
402 * - a matcher object on success
403 * - NULL else
404 */
405 sdb_store_matcher_t *
406 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
407 const char *op, sdb_store_expr_t *expr);
409 /*
410 * sdb_store_matcher_parse_field_cmp:
411 * Parse a simple compare expression for queryable object fields (<field> <op>
412 * <expression>).
413 *
414 * Returns:
415 * - a matcher object on success
416 * - NULL else
417 */
418 sdb_store_matcher_t *
419 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
420 sdb_store_expr_t *expr);
422 /*
423 * sdb_store_dis_matcher:
424 * Creates a matcher matching the disjunction (logical OR) of two matchers.
425 */
426 sdb_store_matcher_t *
427 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
429 /*
430 * sdb_store_con_matcher:
431 * Creates a matcher matching the conjunction (logical AND) of two matchers.
432 */
433 sdb_store_matcher_t *
434 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
436 /*
437 * sdb_store_con_matcher::
438 * Creates a matcher matching the inverse (logical NOT) of a matcher.
439 */
440 sdb_store_matcher_t *
441 sdb_store_inv_matcher(sdb_store_matcher_t *m);
443 /*
444 * sdb_store_matcher_matches:
445 * Check whether the specified matcher matches the specified store object. If
446 * specified, the filter will be used to preselect objects for further
447 * evaluation. It is applied to any object that's used during the evaluation
448 * of the matcher. Only those objects matching the filter will be considered.
449 *
450 * Note that the filter is applied to all object types (hosts, service,
451 * metric, attribute). Thus, any object-specific matchers are mostly unsuited
452 * for this purpose and, if used, may result in unexpected behavior.
453 *
454 * Returns:
455 * - 1 if the object matches
456 * - 0 else
457 */
458 int
459 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
460 sdb_store_matcher_t *filter);
462 /*
463 * sdb_store_matcher_tostring:
464 * Format a matcher object as string. This is meant for logging or debugging
465 * purposes.
466 */
467 char *
468 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
470 /*
471 * sdb_store_lookup_cb:
472 * Lookup callback. It is called for each matching object when looking up data
473 * in the store. The lookup aborts if the callback returns non-zero.
474 */
475 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
477 /*
478 * sdb_store_scan:
479 * Look up objects in the store. The specified callback function is called for
480 * each object in the store matching 'm'. The function performs a full scan of
481 * all hosts stored in the database. If specified, the filter will be used to
482 * preselect objects for further evaluation. See the description of
483 * 'sdb_store_matcher_matches' for details.
484 *
485 * Returns:
486 * - 0 on success
487 * - a negative value else
488 */
489 int
490 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
491 sdb_store_lookup_cb cb, void *user_data);
493 /*
494 * Flags for serialization functions.
495 *
496 * By default, the full object will be included in the serialized output. When
497 * specifying any of the flags, the respective information will be left out.
498 */
499 enum {
500 SDB_SKIP_ATTRIBUTES = 1 << 0,
501 SDB_SKIP_SERVICES = 1 << 1,
502 SDB_SKIP_METRICS = 1 << 2,
503 SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 3,
505 SDB_SKIP_ALL = 0xffff,
506 };
508 /*
509 * sdb_store_tojson:
510 * Serialize the entire store to JSON and append the result to the specified
511 * buffer. If specified, only objects matching the filter will be included in
512 * the result (see sdb_store_host_tojson for details).
513 *
514 * Returns:
515 * - 0 on success
516 * - a negative value on error
517 */
518 int
519 sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags);
521 /*
522 * sdb_store_host_tojson:
523 * Serialize a host object to JSON and append the result to the specified
524 * buffer. If specified, only objects matching the filter will be included in
525 * the result. The filter is applied to each object individually and, thus,
526 * should not be of any object-type specific kind. The filter is never applied
527 * to the specified host object; the caller is responsible for this and for
528 * correctly handling skipped hosts.
529 *
530 * Returns:
531 * - 0 on success
532 * - a negative value on error
533 */
534 int
535 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf,
536 sdb_store_matcher_t *filter, int flags);
538 /*
539 * sdb_store_iter_cb:
540 * Store iterator callback. Iteration stops if the callback returns non-zero.
541 */
542 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
544 /*
545 * sdb_store_iterate:
546 * Iterate the entire store, calling the specified callback for each object.
547 * The user_data pointer is passed on to each call of the callback.
548 *
549 * Returns:
550 * - 0 on success
551 * - a negative value else
552 */
553 int
554 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
556 #ifdef __cplusplus
557 } /* extern "C" */
558 #endif
560 #endif /* ! SDB_CORE_STORE_H */
562 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */