b9a8d0896eae6a3cad344464696f0479994a13a4
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 "core/timeseries.h"
36 #include "utils/strbuf.h"
38 #include <stdio.h>
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
44 /*
45 * Store object types.
46 */
47 enum {
48 SDB_HOST = 1,
49 SDB_SERVICE,
50 SDB_METRIC,
51 SDB_ATTRIBUTE,
52 };
53 #define SDB_STORE_TYPE_TO_NAME(t) \
54 (((t) == SDB_HOST) ? "host" \
55 : ((t) == SDB_SERVICE) ? "service" \
56 : ((t) == SDB_METRIC) ? "metric" \
57 : ((t) == SDB_ATTRIBUTE) ? "attribute" : "unknown")
59 /*
60 * sdb_store_obj_t represents the super-class of any object stored in the
61 * database. It inherits from sdb_object_t and may safely be cast to a generic
62 * object to access its name.
63 */
64 struct sdb_store_obj;
65 typedef struct sdb_store_obj sdb_store_obj_t;
67 /*
68 * Queryable fields of a stored object.
69 */
70 enum {
71 SDB_FIELD_NAME = 1, /* string */
72 SDB_FIELD_LAST_UPDATE, /* datetime */
73 SDB_FIELD_AGE, /* datetime */
74 SDB_FIELD_INTERVAL, /* datetime */
75 SDB_FIELD_BACKEND, /* string */
76 };
78 #define SDB_FIELD_TO_NAME(f) \
79 (((f) == SDB_FIELD_NAME) ? "name" \
80 : ((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \
81 : ((f) == SDB_FIELD_AGE) ? "age" \
82 : ((f) == SDB_FIELD_INTERVAL) ? "interval" \
83 : ((f) == SDB_FIELD_BACKEND) ? "backend" : "unknown")
85 /*
86 * sdb_store_clear:
87 * Clear the entire store and remove all stored objects.
88 */
89 void
90 sdb_store_clear(void);
92 /*
93 * sdb_store_host:
94 * Add/update a host in the store. If the host, identified by its
95 * canonicalized name, already exists, it will be updated according to the
96 * specified name and timestamp. Else, a new entry will be created in the
97 * store. Any memory required for storing the entry will be allocated an
98 * managed by the store itself.
99 *
100 * Returns:
101 * - 0 on success
102 * - a positive value if the new entry is older than the currently stored
103 * entry (in this case, no update will happen)
104 * - a negative value on error
105 */
106 int
107 sdb_store_host(const char *name, sdb_time_t last_update);
109 /*
110 * sdb_store_has_host:
111 * sdb_store_get_host:
112 * Query the store for a host by its (canonicalized) name.
113 *
114 * sdb_store_get_host increments the ref count of the host object. The caller
115 * needs to deref it when no longer using it.
116 */
117 _Bool
118 sdb_store_has_host(const char *name);
120 sdb_store_obj_t *
121 sdb_store_get_host(const char *name);
123 /*
124 * sdb_store_attribute:
125 * Add/update a host's attribute in the store. If the attribute, identified by
126 * its key, already exists for the specified host, it will be updated to the
127 * specified values. If the referenced host does not exist, an error will be
128 * reported. Else, a new entry will be created in the store. Any memory
129 * required for storing the entry will be allocated and managed by the store
130 * itself.
131 *
132 * Returns:
133 * - 0 on success
134 * - a positive value if the new entry is older than the currently stored
135 * entry (in this case, no update will happen)
136 * - a negative value on error
137 */
138 int
139 sdb_store_attribute(const char *hostname,
140 const char *key, const sdb_data_t *value,
141 sdb_time_t last_update);
143 /*
144 * sdb_store_service:
145 * Add/update a service in the store. If the service, identified by its name,
146 * already exists for the specified host, it will be updated according to the
147 * specified 'service' object. If the referenced host does not exist, an error
148 * will be reported. Else, a new entry will be created in the store. Any
149 * memory required for storing the entry will be allocated an managed by the
150 * store itself.
151 *
152 * Returns:
153 * - 0 on success
154 * - a positive value if the new entry is older than the currently stored
155 * entry (in this case, no update will happen)
156 * - a negative value on error
157 */
158 int
159 sdb_store_service(const char *hostname, const char *name,
160 sdb_time_t last_update);
162 /*
163 * sdb_store_service_attr:
164 * Add/update a service's attribute in the store. If the attribute, identified
165 * by its key, already exists for the specified service, it will be updated to
166 * the specified value. If the references service (for the specified host)
167 * does not exist, an error will be reported. Any memory required for storing
168 * the entry will be allocated and managed by the store itself.
169 *
170 * Returns:
171 * - 0 on success
172 * - a positive value if the new entry is older than the currently stored
173 * entry (in this case, no update will happen)
174 * - a negative value on error
175 */
176 int
177 sdb_store_service_attr(const char *hostname, const char *service,
178 const char *key, const sdb_data_t *value, sdb_time_t last_update);
180 /*
181 * A metric store describes how to access a metric's data.
182 */
183 typedef struct {
184 const char *type;
185 const char *id;
186 } sdb_metric_store_t;
188 /*
189 * sdb_store_metric:
190 * Add/update a metric in the store. If the metric, identified by its name,
191 * already exists for the specified host, it will be updated according to the
192 * specified 'metric' object. If the referenced host does not exist, an error
193 * will be reported. Else, a new entry will be created in the store. Any
194 * memory required for storing the entry will be allocated an managed by the
195 * store itself.
196 *
197 * If specified, the metric store describes where to access the metric's data.
198 *
199 * Returns:
200 * - 0 on success
201 * - a positive value if the new entry is older than the currently stored
202 * entry (in this case, no update will happen)
203 * - a negative value on error
204 */
205 int
206 sdb_store_metric(const char *hostname, const char *name,
207 sdb_metric_store_t *store, sdb_time_t last_update);
209 /*
210 * sdb_store_metric_attr:
211 * Add/update a metric's attribute in the store. If the attribute, identified
212 * by its key, already exists for the specified metric, it will be updated to
213 * the specified value. If the references metric (for the specified host)
214 * does not exist, an error will be reported. Any memory required for storing
215 * the entry will be allocated and managed by the store itself.
216 *
217 * Returns:
218 * - 0 on success
219 * - a positive value if the new entry is older than the currently stored
220 * entry (in this case, no update will happen)
221 * - a negative value on error
222 */
223 int
224 sdb_store_metric_attr(const char *hostname, const char *metric,
225 const char *key, const sdb_data_t *value, sdb_time_t last_update);
227 /*
228 * sdb_store_fetch_timeseries:
229 * Fetch the time-series described by the specified host's metric and
230 * serialize it as JSON into the provided string buffer.
231 *
232 * Returns:
233 * - 0 on success
234 * - a negative value else
235 */
236 int
237 sdb_store_fetch_timeseries(const char *hostname, const char *metric,
238 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf);
240 /*
241 * sdb_store_get_field:
242 * Get the value of a stored object's queryable field. The caller is
243 * responsible for freeing any dynamically allocated memory possibly stored in
244 * the returned value. If 'res' is NULL, the function will return whether the
245 * field exists.
246 *
247 * Note: Retrieving the backend this way is not currently supported.
248 *
249 * Returns:
250 * - 0 on success
251 * - a negative value else
252 */
253 int
254 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
256 /*
257 * sdb_store_get_attr:
258 * Get the value of a stored object's attribute. The caller is responsible for
259 * freeing any dynamically allocated memory possibly stored in the returned
260 * value.If 'res' is NULL, the function will return whether the attribute
261 * exists.
262 *
263 * Returns:
264 * - 0 if the attribute exists
265 * - a negative value else
266 */
267 int
268 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res);
270 /*
271 * Expressions specify arithmetic expressions.
272 *
273 * A expression object inherits from sdb_object_t and, thus, may safely be
274 * cast to a generic object.
275 */
276 struct sdb_store_expr;
277 typedef struct sdb_store_expr sdb_store_expr_t;
278 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
280 /*
281 * sdb_store_expr_create:
282 * Creates an arithmetic expression implementing the specified operator on the
283 * specified left and right operand.
284 *
285 * Returns:
286 * - an expression object on success
287 * - NULL else
288 */
289 sdb_store_expr_t *
290 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
292 /*
293 * sdb_store_expr_fieldvalue:
294 * Creates an expression which evaluates to the value of the specified
295 * queryable field of a stored object.
296 *
297 * Returns:
298 * - an expression object on success
299 * - NULL else
300 */
301 sdb_store_expr_t *
302 sdb_store_expr_fieldvalue(int field);
304 /*
305 * sdb_store_expr_attrvalue:
306 * Creates an expression which evaluates to the value of the specified
307 * attribute of a stored object.
308 *
309 * Returns:
310 * - an expression object on success
311 * - NULL else
312 */
313 sdb_store_expr_t *
314 sdb_store_expr_attrvalue(const char *name);
316 /*
317 * sdb_store_expr_constvalue:
318 * Creates an expression which evaluates to the specified constant value.
319 *
320 * Returns:
321 * - an expression object on success
322 * - NULL else
323 */
324 sdb_store_expr_t *
325 sdb_store_expr_constvalue(const sdb_data_t *value);
327 /*
328 * sdb_store_expr_eval:
329 * Evaluate an expression for the specified stored object and stores the
330 * result in 'res'. The result's value will be allocated dynamically if
331 * necessary and, thus, should be free'd by the caller (e.g. using
332 * sdb_data_free_datum). The object may be NULL, in which case the expression
333 * needs to evaluate to a constant value.
334 *
335 * Returns:
336 * - 0 on success
337 * - a negative value else
338 */
339 int
340 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
341 sdb_data_t *res);
343 /*
344 * Conditionals may be used to lookup hosts from the store based on a
345 * conditional expression.
346 *
347 * A conditional object inherits from sdb_object_t and, thus, may safely be
348 * cast to a generic object.
349 */
350 struct sdb_store_cond;
351 typedef struct sdb_store_cond sdb_store_cond_t;
352 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
354 /*
355 * sdb_store_attr_cond:
356 * Creates a conditional based on attribute values. The value of stored
357 * attributes is compared against the value the expression evaluates to. See
358 * sdb_data_cmp for details about the comparison.
359 */
360 sdb_store_cond_t *
361 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr);
363 /*
364 * sdb_store_obj_cond:
365 * Creates a conditional based on queryable object fields. The respective
366 * field of *any* object type is compared against the value the expression
367 * evaluates to.
368 */
369 sdb_store_cond_t *
370 sdb_store_obj_cond(int field, sdb_store_expr_t *expr);
372 /*
373 * Store matchers may be used to lookup hosts from the store based on their
374 * various attributes. Service and attribute matchers are applied to a host's
375 * services and attributes and evaluate to true if *any* service or attribute
376 * matches.
377 *
378 * A store matcher object inherits from sdb_object_t and, thus, may safely be
379 * cast to a generic object.
380 */
381 struct sdb_store_matcher;
382 typedef struct sdb_store_matcher sdb_store_matcher_t;
383 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
385 /*
386 * sdb_store_name_matcher:
387 * Creates a matcher matching by the specified object type's name. If 're' is
388 * true, the specified name is treated as a POSIX extended regular expression.
389 * Else, the exact name has to match (case-insensitive).
390 */
391 sdb_store_matcher_t *
392 sdb_store_name_matcher(int type, const char *name, _Bool re);
394 /*
395 * sdb_store_attr_matcher:
396 * Creates a matcher matching attributes based on their value. If 're' is
397 * true, the specified name is treated as a POSIX extended regular expression.
398 * Else, the exact name has to match (case-insensitive).
399 */
400 sdb_store_matcher_t *
401 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
403 /*
404 * sdb_store_isnull_matcher:
405 * Creates a matcher matching "missing" attributes.
406 */
407 sdb_store_matcher_t *
408 sdb_store_isnull_matcher(const char *attr_name);
410 /*
411 * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
412 * sdb_store_ge_matcher, sdb_store_gt_matcher:
413 * Creates a matcher based on a conditional. The matcher matches objects for
414 * which the conditional evaluates the object to compare less than, less or
415 * equal, equal, greater or equal, or greater than the conditional's value
416 * repsectively.
417 */
418 sdb_store_matcher_t *
419 sdb_store_lt_matcher(sdb_store_cond_t *cond);
420 sdb_store_matcher_t *
421 sdb_store_le_matcher(sdb_store_cond_t *cond);
422 sdb_store_matcher_t *
423 sdb_store_eq_matcher(sdb_store_cond_t *cond);
424 sdb_store_matcher_t *
425 sdb_store_ge_matcher(sdb_store_cond_t *cond);
426 sdb_store_matcher_t *
427 sdb_store_gt_matcher(sdb_store_cond_t *cond);
429 /*
430 * sdb_store_parse_object_type_plural:
431 * Parse the type name (plural) of a stored object.
432 *
433 * Returns:
434 * - the object type on success
435 * - a negative value else
436 */
437 int
438 sdb_store_parse_object_type_plural(const char *name);
440 /*
441 * sdb_store_parse_field_name:
442 * Parse the name of a stored object's queryable field.
443 *
444 * Returns:
445 * - the field id on success
446 * - a negative value else
447 */
448 int
449 sdb_store_parse_field_name(const char *name);
451 /*
452 * sdb_store_matcher_parse_cmp:
453 * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
454 *
455 * Returns:
456 * - a matcher object on success
457 * - NULL else
458 */
459 sdb_store_matcher_t *
460 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
461 const char *op, sdb_store_expr_t *expr);
463 /*
464 * sdb_store_matcher_parse_field_cmp:
465 * Parse a simple compare expression for queryable object fields (<field> <op>
466 * <expression>).
467 *
468 * Returns:
469 * - a matcher object on success
470 * - NULL else
471 */
472 sdb_store_matcher_t *
473 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
474 sdb_store_expr_t *expr);
476 /*
477 * sdb_store_dis_matcher:
478 * Creates a matcher matching the disjunction (logical OR) of two matchers.
479 */
480 sdb_store_matcher_t *
481 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
483 /*
484 * sdb_store_con_matcher:
485 * Creates a matcher matching the conjunction (logical AND) of two matchers.
486 */
487 sdb_store_matcher_t *
488 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
490 /*
491 * sdb_store_con_matcher::
492 * Creates a matcher matching the inverse (logical NOT) of a matcher.
493 */
494 sdb_store_matcher_t *
495 sdb_store_inv_matcher(sdb_store_matcher_t *m);
497 /*
498 * sdb_store_matcher_matches:
499 * Check whether the specified matcher matches the specified store object. If
500 * specified, the filter will be used to preselect objects for further
501 * evaluation. It is applied to any object that's used during the evaluation
502 * of the matcher. Only those objects matching the filter will be considered.
503 *
504 * Note that the filter is applied to all object types (hosts, service,
505 * metric, attribute). Thus, any object-specific matchers are mostly unsuited
506 * for this purpose and, if used, may result in unexpected behavior.
507 *
508 * Returns:
509 * - 1 if the object matches
510 * - 0 else
511 */
512 int
513 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
514 sdb_store_matcher_t *filter);
516 /*
517 * sdb_store_matcher_tostring:
518 * Format a matcher object as string. This is meant for logging or debugging
519 * purposes.
520 */
521 char *
522 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
524 /*
525 * sdb_store_lookup_cb:
526 * Lookup callback. It is called for each matching object when looking up data
527 * in the store. The lookup aborts if the callback returns non-zero.
528 */
529 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
531 /*
532 * sdb_store_scan:
533 * Look up objects in the store. The specified callback function is called for
534 * each object in the store matching 'm'. The function performs a full scan of
535 * all hosts stored in the database. If specified, the filter will be used to
536 * preselect objects for further evaluation. See the description of
537 * 'sdb_store_matcher_matches' for details.
538 *
539 * Returns:
540 * - 0 on success
541 * - a negative value else
542 */
543 int
544 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
545 sdb_store_lookup_cb cb, void *user_data);
547 /*
548 * Flags for serialization functions.
549 *
550 * By default, the full host object will be included in the serialized output.
551 * When specifying any of the flags, the respective information will be left
552 * out. The SKIP_EMPTY flags may be used to skip host objects entirely.
553 */
554 enum {
555 SDB_SKIP_ATTRIBUTES = 1 << 0,
556 SDB_SKIP_SERVICES = 1 << 1,
557 SDB_SKIP_METRICS = 1 << 2,
558 SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 3,
560 SDB_SKIP_ALL = (1 << 8) - 1,
562 /* skip hosts if they do not reference any services/metrics */
563 SDB_SKIP_EMPTY_SERVICES = 1 << 8,
564 SDB_SKIP_EMPTY_METRICS = 1 << 9,
565 };
567 /*
568 * sdb_store_tojson:
569 * Serialize the entire store to JSON and append the result to the specified
570 * buffer. If specified, only objects matching the filter will be included in
571 * the result (see sdb_store_host_tojson for details).
572 *
573 * Returns:
574 * - 0 on success
575 * - a negative value on error
576 */
577 int
578 sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags);
580 /*
581 * sdb_store_host_tojson:
582 * Serialize a host object to JSON and append the result to the specified
583 * buffer. If specified, only objects matching the filter will be included in
584 * the result. The filter is applied to each object individually and, thus,
585 * should not be of any object-type specific kind. The filter is never applied
586 * to the specified host object; the caller is responsible for this and for
587 * correctly handling skipped hosts.
588 *
589 * Returns:
590 * - 0 on success
591 * - a negative value on error
592 */
593 int
594 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf,
595 sdb_store_matcher_t *filter, int flags);
597 /*
598 * sdb_store_iter_cb:
599 * Store iterator callback. Iteration stops if the callback returns non-zero.
600 */
601 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
603 /*
604 * sdb_store_iterate:
605 * Iterate the entire store, calling the specified callback for each object.
606 * The user_data pointer is passed on to each call of the callback.
607 *
608 * Returns:
609 * - 0 on success
610 * - a negative value else
611 */
612 int
613 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
615 #ifdef __cplusplus
616 } /* extern "C" */
617 #endif
619 #endif /* ! SDB_CORE_STORE_H */
621 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */