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 "parser/ast.h"
37 #include "utils/strbuf.h"
39 #include <stdbool.h>
40 #include <stdio.h>
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
46 /*
47 * Store object types.
48 */
49 enum {
50 SDB_HOST = 1,
51 SDB_SERVICE,
52 SDB_METRIC,
54 SDB_ATTRIBUTE = 1 << 4,
56 /*
57 * Queryable fields of a stored object.
58 */
59 SDB_FIELD_NAME = 1 << 8, /* type: string */
60 SDB_FIELD_LAST_UPDATE, /* type: datetime */
61 SDB_FIELD_AGE, /* type: datetime */
62 SDB_FIELD_INTERVAL, /* type: datetime */
63 SDB_FIELD_BACKEND, /* type: array of strings */
64 SDB_FIELD_VALUE, /* attributes only; type: type of the value */
65 SDB_FIELD_TIMESERIES, /* metrics only; type: boolean */
66 };
67 #define SDB_STORE_TYPE_TO_NAME(t) \
68 (((t) == SDB_HOST) ? "host" \
69 : ((t) == SDB_SERVICE) ? "service" \
70 : ((t) == SDB_METRIC) ? "metric" \
71 : ((t) == SDB_ATTRIBUTE) ? "attribute" \
72 : ((t) == (SDB_ATTRIBUTE | SDB_HOST)) ? "host attribute" \
73 : ((t) == (SDB_ATTRIBUTE | SDB_SERVICE)) ? "service attribute" \
74 : ((t) == (SDB_ATTRIBUTE | SDB_METRIC)) ? "metric attribute" \
75 : "unknown")
77 #define SDB_FIELD_TO_NAME(f) \
78 (((f) == SDB_FIELD_NAME) ? "name" \
79 : ((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \
80 : ((f) == SDB_FIELD_AGE) ? "age" \
81 : ((f) == SDB_FIELD_INTERVAL) ? "interval" \
82 : ((f) == SDB_FIELD_BACKEND) ? "backend" \
83 : ((f) == SDB_FIELD_VALUE) ? "value" \
84 : ((f) == SDB_FIELD_TIMESERIES) ? "timeseries" \
85 : "unknown")
87 #define SDB_FIELD_TYPE(f) \
88 (((f) == SDB_FIELD_NAME) ? SDB_TYPE_STRING \
89 : ((f) == SDB_FIELD_LAST_UPDATE) ? SDB_TYPE_DATETIME \
90 : ((f) == SDB_FIELD_AGE) ? SDB_TYPE_DATETIME \
91 : ((f) == SDB_FIELD_INTERVAL) ? SDB_TYPE_DATETIME \
92 : ((f) == SDB_FIELD_BACKEND) ? (SDB_TYPE_ARRAY | SDB_TYPE_STRING) \
93 : ((f) == SDB_FIELD_VALUE) ? -1 /* unknown */ \
94 : ((f) == SDB_FIELD_TIMESERIES) ? SDB_TYPE_BOOLEAN \
95 : -1)
97 /*
98 * sdb_store_t represents an in-memory store. It inherits from sdb_object_t
99 * and may safely be case to a generic object.
100 */
101 struct sdb_store;
102 typedef struct sdb_store sdb_store_t;
103 #define SDB_STORE(obj) ((sdb_store_t *)(obj))
105 /*
106 * sdb_store_obj_t represents the super-class of any object stored in the
107 * database. It inherits from sdb_object_t and may safely be cast to a generic
108 * object to access its name.
109 */
110 struct sdb_store_obj;
111 typedef struct sdb_store_obj sdb_store_obj_t;
113 /*
114 * A metric store describes how to access a metric's data.
115 */
116 typedef struct {
117 const char *type;
118 const char *id;
119 } sdb_metric_store_t;
121 /*
122 * Expressions represent arithmetic expressions based on stored objects and
123 * their various attributes.
124 *
125 * An expression object inherits from sdb_object_t and, thus, may safely be
126 * cast to a generic object.
127 */
128 struct sdb_store_expr;
129 typedef struct sdb_store_expr sdb_store_expr_t;
130 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
132 /*
133 * An expression iterator iterates over the values of an iterable expression
134 * (see sdb_store_expr_iterable).
135 */
136 struct sdb_store_expr_iter;
137 typedef struct sdb_store_expr_iter sdb_store_expr_iter_t;
139 /*
140 * Store matchers may be used to lookup hosts from the store based on their
141 * various attributes. Service and attribute matchers are applied to a host's
142 * services and attributes and evaluate to true if *any* service or attribute
143 * matches.
144 *
145 * A store matcher object inherits from sdb_object_t and, thus, may safely be
146 * cast to a generic object.
147 */
148 struct sdb_store_matcher;
149 typedef struct sdb_store_matcher sdb_store_matcher_t;
150 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
152 /*
153 * A JSON formatter converts stored objects into the JSON format.
154 * See http://www.ietf.org/rfc/rfc4627.txt
155 *
156 * A JSON formatter object inherits from sdb_object_t and, thus, may safely be
157 * cast to a generic object.
158 */
159 struct sdb_store_json_formatter;
160 typedef struct sdb_store_json_formatter sdb_store_json_formatter_t;
162 /*
163 * A store writer describes the interface for plugins implementing a store.
164 *
165 * Any of the call-back functions shall return:
166 * - 0 on success
167 * - a positive value if the new entry is older than the currently stored
168 * entry (in this case, no update will happen)
169 * - a negative value on error
170 */
171 typedef struct {
172 /*
173 * store_host:
174 * Add/update a host in the store. If the host, identified by its
175 * canonicalized name, already exists, it will be updated according to the
176 * specified name and timestamp. Else, a new entry will be created in the
177 * store.
178 */
179 int (*store_host)(const char *name, sdb_time_t last_update,
180 sdb_object_t *user_data);
182 /*
183 * store_service:
184 * Add/update a service in the store. If the service, identified by its
185 * name, already exists for the specified host, it will be updated
186 * according to the specified name and timestamp. If the referenced host
187 * does not exist, an error will be reported. Else, a new entry will be
188 * created in the store.
189 */
190 int (*store_service)(const char *hostname, const char *name,
191 sdb_time_t last_update, sdb_object_t *user_data);
193 /*
194 * store_metric:
195 * Add/update a metric in the store. If the metric, identified by its
196 * name, already exists for the specified host, it will be updated
197 * according to the specified attributes. If the referenced host does not
198 * exist, an error will be reported. Else, a new entry will be created in
199 * the store.
200 */
201 int (*store_metric)(const char *hostname, const char *name,
202 sdb_metric_store_t *store, sdb_time_t last_update,
203 sdb_object_t *user_data);
205 /*
206 * store_attribute:
207 * Add/update a host's attribute in the store. If the attribute,
208 * identified by its key, already exists for the specified host, it will
209 * be updated to the specified values. If the referenced host does not
210 * exist, an error will be reported. Else, a new entry will be created in
211 * the store.
212 */
213 int (*store_attribute)(const char *hostname,
214 const char *key, const sdb_data_t *value, sdb_time_t last_update,
215 sdb_object_t *user_data);
217 /*
218 * store_service_attr:
219 * Add/update a service's attribute in the store. If the attribute,
220 * identified by its key, already exists for the specified service, it
221 * will be updated to the specified value. If the references service (for
222 * the specified host) does not exist, an error will be reported.
223 */
224 int (*store_service_attr)(const char *hostname, const char *service,
225 const char *key, const sdb_data_t *value, sdb_time_t last_update,
226 sdb_object_t *user_data);
228 /*
229 * store_metric_attr:
230 * Add/update a metric's attribute in the store. If the attribute,
231 * identified by its key, already exists for the specified metric, it will
232 * be updated to the specified value. If the references metric (for the
233 * specified host) does not exist, an error will be reported.
234 */
235 int (*store_metric_attr)(const char *hostname, const char *metric,
236 const char *key, const sdb_data_t *value, sdb_time_t last_update,
237 sdb_object_t *user_data);
238 } sdb_store_writer_t;
240 /*
241 * sdb_store_writer:
242 * A store writer implementation that provides an in-memory object store. It
243 * expects a store object as its user-data argument.
244 */
245 extern sdb_store_writer_t sdb_store_writer;
247 /*
248 * A store reader describes the interface to query a store implementation.
249 */
250 typedef struct {
251 /*
252 * prepare_query:
253 * Prepare the query described by 'ast' for execution.
254 */
255 sdb_object_t *(*prepare_query)(sdb_ast_node_t *ast,
256 sdb_strbuf_t *errbuf, sdb_object_t *user_data);
258 /*
259 * execute_query:
260 * Execute a previously prepared query. The callback may expect that only
261 * queries prepared by its respective prepare callback will be passed to
262 * this function.
263 *
264 * TODO: Instead of letting the executor write directly to a string buffer
265 * (which cannot easily be merged with other results), let it hand
266 * all objects to a store-writer.
267 */
268 int (*execute_query)(sdb_object_t *q,
269 sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
270 sdb_object_t *user_data);
271 } sdb_store_reader_t;
273 /*
274 * sdb_store_reader:
275 * A store reader implementation that uses an in-memory object store. It
276 * expects a store object as its user-data argument.
277 */
278 extern sdb_store_reader_t sdb_store_reader;
280 /*
281 * sdb_store_create:
282 * Allocate a new in-memory store.
283 */
284 sdb_store_t *
285 sdb_store_create(void);
287 /*
288 * sdb_store_init:
289 * Initialize the store sub-system. This function has to be called before
290 * doing any other store operations.
291 *
292 * Returns:
293 * - 0 on success
294 * - a negative value else
295 */
296 int
297 sdb_store_init(void);
299 /*
300 * sdb_store_clear:
301 * Clear the entire store and remove all stored objects.
302 */
303 void
304 sdb_store_clear(void);
306 /*
307 * sdb_store_host, sdb_store_service, sdb_store_metric, sdb_store_attribute,
308 * sdb_store_metric_attr:
309 * Store an object in the specified store.
310 */
311 int
312 sdb_store_host(sdb_store_t *store, const char *name, sdb_time_t last_update);
313 int
314 sdb_store_service(sdb_store_t *store, const char *hostname, const char *name,
315 sdb_time_t last_update);
316 int
317 sdb_store_metric(sdb_store_t *store, const char *hostname, const char *name,
318 sdb_metric_store_t *metric_store, sdb_time_t last_update);
319 int
320 sdb_store_attribute(sdb_store_t *store, const char *hostname,
321 const char *key, const sdb_data_t *value, sdb_time_t last_update);
322 int
323 sdb_store_service_attr(sdb_store_t *store, const char *hostname,
324 const char *service, const char *key, const sdb_data_t *value,
325 sdb_time_t last_update);
326 int
327 sdb_store_metric_attr(sdb_store_t *store, const char *hostname,
328 const char *metric, const char *key, const sdb_data_t *value,
329 sdb_time_t last_update);
331 /*
332 * sdb_store_get_host:
333 * Query the specified store for a host by its (canonicalized) name.
334 *
335 * The function increments the ref count of the host object. The caller needs
336 * to deref it when no longer using it.
337 */
338 sdb_store_obj_t *
339 sdb_store_get_host(sdb_store_t *store, const char *name);
341 /*
342 * sdb_store_fetch_timeseries:
343 * Fetch the time-series described by the specified host's metric and
344 * serialize it as JSON into the provided string buffer. The host data is
345 * retrieved from the specified store.
346 *
347 * Returns:
348 * - 0 on success
349 * - a negative value else
350 */
351 int
352 sdb_store_fetch_timeseries(sdb_store_t *store,
353 const char *hostname, const char *metric,
354 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf);
356 /*
357 * sdb_store_get_child:
358 * Retrieve a host's child object of the specified type and name. The
359 * reference count of the child object will be incremented before returning
360 * it. The caller is responsible for releasing the object once it's no longer
361 * used.
362 *
363 * Returns:
364 * - the child object on success
365 * - NULL else
366 */
367 sdb_store_obj_t *
368 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name);
370 /*
371 * sdb_store_get_field:
372 * Get the value of a stored object's queryable field. The caller is
373 * responsible for freeing any dynamically allocated memory possibly stored in
374 * the returned value. If 'res' is NULL, the function will return whether the
375 * field exists.
376 *
377 * Returns:
378 * - 0 on success
379 * - a negative value else
380 */
381 int
382 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
384 /*
385 * sdb_store_get_attr:
386 * Get the value of a stored object's attribute. The caller is responsible for
387 * freeing any dynamically allocated memory possibly stored in the returned
388 * value. If 'res' is NULL, the function will return whether the attribute
389 * exists. If specified, only attributes matching the filter will be
390 * considered.
391 *
392 * Returns:
393 * - 0 if the attribute exists
394 * - a negative value else
395 */
396 int
397 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
398 sdb_store_matcher_t *filter);
400 /*
401 * Querying a store:
402 *
403 * - Query interface: A query is a formal description of an interaction with
404 * the store. It can be used, both, for read and write access. The query is
405 * described by its abstract syntax tree (AST). The parser package provides
406 * means to parse a string (SysQL) representation of the query into an AST.
407 *
408 * - Matcher / expression interface: This low-level interface provides direct
409 * control over how to access the store. It is used by the query
410 * implementation internally and can only be used for read access.
411 */
413 /*
414 * sdb_store_query_t:
415 * A parsed query readily prepared for execution.
416 */
417 struct sdb_store_query;
418 typedef struct sdb_store_query sdb_store_query_t;
420 /*
421 * sdb_store_query_prepare:
422 * Prepare the query described by 'ast' for execution in a store.
423 *
424 * Returns:
425 * - a store query on success
426 * - NULL else
427 */
428 sdb_store_query_t *
429 sdb_store_query_prepare(sdb_ast_node_t *ast);
431 /*
432 * sdb_store_query_prepare_matcher:
433 * Prepare the logical expression described by 'ast' for execution as a store
434 * matcher.
435 *
436 * Returns:
437 * - a matcher on success
438 * - NULL else
439 */
440 sdb_store_matcher_t *
441 sdb_store_query_prepare_matcher(sdb_ast_node_t *ast);
443 /*
444 * sdb_store_query_execute:
445 * Execute a previously prepared query in the specified store. The query
446 * result will be written to 'buf' and any errors to 'errbuf'.
447 *
448 * Returns:
449 * - the result type (to be used by the server reply)
450 * - a negative value on error
451 */
452 int
453 sdb_store_query_execute(sdb_store_t *store, sdb_store_query_t *m,
454 sdb_strbuf_t *buf, sdb_strbuf_t *errbuf);
456 /*
457 * sdb_store_expr_create:
458 * Creates an arithmetic expression implementing the specified operator on the
459 * specified left and right operand.
460 *
461 * Returns:
462 * - an expression object on success
463 * - NULL else
464 */
465 sdb_store_expr_t *
466 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
468 /*
469 * sdb_store_expr_typed:
470 * Creates an expression which evaluates in the context of an object's sibling
471 * as specified by the given type.
472 *
473 * Returns:
474 * - an expression object on success
475 * - NULL else
476 */
477 sdb_store_expr_t *
478 sdb_store_expr_typed(int typ, sdb_store_expr_t *expr);
480 /*
481 * sdb_store_expr_fieldvalue:
482 * Creates an expression which evaluates to the value of the specified
483 * queryable field of a stored object.
484 *
485 * Returns:
486 * - an expression object on success
487 * - NULL else
488 */
489 sdb_store_expr_t *
490 sdb_store_expr_fieldvalue(int field);
492 /*
493 * sdb_store_expr_attrvalue:
494 * Creates an expression which evaluates to the value of the specified
495 * attribute of a stored object. Evaluates to a NULL value if the attribute
496 * does not exist.
497 *
498 * Returns:
499 * - an expression object on success
500 * - NULL else
501 */
502 sdb_store_expr_t *
503 sdb_store_expr_attrvalue(const char *name);
505 /*
506 * sdb_store_expr_constvalue:
507 * Creates an expression which evaluates to the specified constant value.
508 *
509 * Returns:
510 * - an expression object on success
511 * - NULL else
512 */
513 sdb_store_expr_t *
514 sdb_store_expr_constvalue(const sdb_data_t *value);
516 /*
517 * sdb_store_expr_eval:
518 * Evaluate an expression for the specified stored object and stores the
519 * result in 'res'. The result's value will be allocated dynamically if
520 * necessary and, thus, should be free'd by the caller (e.g. using
521 * sdb_data_free_datum). The object may be NULL, in which case the expression
522 * needs to evaluate to a constant value. If specified, only objects matching
523 * the filter will be used during the evaluation.
524 *
525 * Returns:
526 * - 0 on success
527 * - a negative value else
528 */
529 int
530 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
531 sdb_data_t *res, sdb_store_matcher_t *filter);
533 /*
534 * sdb_store_expr_iterable:
535 * Check whether an expression, evaluated in the specified context (HOST,
536 * SERVICE, METRIC) is iterable, that is, if it may evaluate to multiple
537 * values.
538 */
539 bool
540 sdb_store_expr_iterable(sdb_store_expr_t *expr, int context);
542 /*
543 * sdb_store_expr_iter:
544 * Iterate over the elements of an iterable expression. sdb_store_expr_iter
545 * returns NULL if the expression is not iterable (for the specified object).
546 * See also sdb_store_expr_iterable.
547 *
548 * sdb_store_expr_iter_get_next returns NULL if there is no next element.
549 */
550 sdb_store_expr_iter_t *
551 sdb_store_expr_iter(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
552 sdb_store_matcher_t *filter);
553 void
554 sdb_store_expr_iter_destroy(sdb_store_expr_iter_t *iter);
556 bool
557 sdb_store_expr_iter_has_next(sdb_store_expr_iter_t *iter);
558 sdb_data_t
559 sdb_store_expr_iter_get_next(sdb_store_expr_iter_t *iter);
561 /*
562 * sdb_store_dis_matcher:
563 * Creates a matcher matching the disjunction (logical OR) of two matchers.
564 */
565 sdb_store_matcher_t *
566 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
568 /*
569 * sdb_store_con_matcher:
570 * Creates a matcher matching the conjunction (logical AND) of two matchers.
571 */
572 sdb_store_matcher_t *
573 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
575 /*
576 * sdb_store_inv_matcher::
577 * Creates a matcher matching the inverse (logical NOT) of a matcher.
578 */
579 sdb_store_matcher_t *
580 sdb_store_inv_matcher(sdb_store_matcher_t *m);
582 /*
583 * sdb_store_any_matcher:
584 * Creates a matcher iterating over values of the first expression (which has
585 * to be iterable). It matches if *any* of those elements match 'm'. 'm' has
586 * to be an ary operation with the left operand unset.
587 */
588 sdb_store_matcher_t *
589 sdb_store_any_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m);
591 /*
592 * sdb_store_all_matcher:
593 * Creates a matcher iterating over values of the first expression (which has
594 * to be iterable). It matches if *all* of those elements match 'm'. 'm' has
595 * to be an ary operation with the left operand unset.
596 */
597 sdb_store_matcher_t *
598 sdb_store_all_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m);
600 /*
601 * sdb_store_in_matcher:
602 * Creates a matcher which matches if the right value evaluates to an array
603 * value and the left value is included in that array. See sdb_data_inarray
604 * for more details.
605 */
606 sdb_store_matcher_t *
607 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
609 /*
610 * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
611 * sdb_store_ge_matcher, sdb_store_gt_matcher:
612 * Create conditional matchers comparing the values of two expressions. The
613 * matcher matches if the left expression compres less than, less or equal
614 * than, equal to, not equal to, greater or equal than, or greater than the
615 * right expression.
616 */
617 sdb_store_matcher_t *
618 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
619 sdb_store_matcher_t *
620 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
621 sdb_store_matcher_t *
622 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
623 sdb_store_matcher_t *
624 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
625 sdb_store_matcher_t *
626 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
627 sdb_store_matcher_t *
628 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
630 /*
631 * sdb_store_regex_matcher:
632 * Creates a matcher which matches the string value the left expression
633 * evaluates to against the regular expression the right expression evaluates
634 * to. The right expression may either be a constant value regular expression
635 * or string or a dynamic value evaluating to a string. In the latter case,
636 * the string is compiled to a regex every time the matcher is executed.
637 */
638 sdb_store_matcher_t *
639 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
641 /*
642 * sdb_store_nregex_matcher:
643 * Creates a regex matcher just like sdb_store_regex_matcher except that it
644 * matches in case the regular expression does not match.
645 */
646 sdb_store_matcher_t *
647 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
649 /*
650 * sdb_store_isnull_matcher:
651 * Creates a matcher matching NULL values.
652 */
653 sdb_store_matcher_t *
654 sdb_store_isnull_matcher(sdb_store_expr_t *expr);
656 /*
657 * sdb_store_istrue_matcher, sdb_store_isfalse_matcher:
658 * Creates a matcher matching boolean values.
659 */
660 sdb_store_matcher_t *
661 sdb_store_istrue_matcher(sdb_store_expr_t *expr);
662 sdb_store_matcher_t *
663 sdb_store_isfalse_matcher(sdb_store_expr_t *expr);
665 /*
666 * sdb_store_matcher_matches:
667 * Check whether the specified matcher matches the specified store object. If
668 * specified, the filter will be used to preselect objects for further
669 * evaluation. It is applied to any object that's used during the evaluation
670 * of the matcher. Only those objects matching the filter will be considered.
671 *
672 * Note that the filter is applied to all object types (hosts, service,
673 * metric, attribute). Thus, any object-specific matchers are mostly unsuited
674 * for this purpose and, if used, may result in unexpected behavior.
675 *
676 * Returns:
677 * - 1 if the object matches
678 * - 0 else
679 */
680 int
681 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
682 sdb_store_matcher_t *filter);
684 /*
685 * sdb_store_matcher_op_cb:
686 * Callback constructing a matcher operator.
687 */
688 typedef sdb_store_matcher_t *(*sdb_store_matcher_op_cb)
689 (sdb_store_expr_t *, sdb_store_expr_t *);
691 /*
692 * sdb_store_lookup_cb:
693 * Lookup callback. It is called for each matching object when looking up data
694 * in the store passing on the lookup filter and the specified user-data. The
695 * lookup aborts early if the callback returns non-zero.
696 */
697 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj,
698 sdb_store_matcher_t *filter, void *user_data);
700 /*
701 * sdb_store_scan:
702 * Look up objects of the specified type in the specified store. The specified
703 * callback function is called for each object in the store matching 'm'. The
704 * function performs a full scan of all objects stored in the database. If
705 * specified, the filter will be used to preselect objects for further
706 * evaluation. See the description of 'sdb_store_matcher_matches' for details.
707 *
708 * Returns:
709 * - 0 on success
710 * - a negative value else
711 */
712 int
713 sdb_store_scan(sdb_store_t *store, int type,
714 sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
715 sdb_store_lookup_cb cb, void *user_data);
717 /*
718 * Flags for JSON formatting.
719 */
720 enum {
721 SDB_WANT_ARRAY = 1 << 0,
722 };
724 /*
725 * sdb_store_json_formatter:
726 * Create a JSON formatter for the specified object types writing to the
727 * specified buffer.
728 */
729 sdb_store_json_formatter_t *
730 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags);
732 /*
733 * sdb_store_json_emit:
734 * Serialize a single object to JSON adding it to the string buffer associated
735 * with the formatter object. The serialized object will not include
736 * attributes or any child objects. Instead, call the function again for each
737 * of those objects. All attributes have to be emitted before any other
738 * children types. Use sdb_store_json_emit_full() to emit a full (filtered)
739 * object.
740 *
741 * Note that the output might not be valid JSON before calling
742 * sdb_store_json_finish().
743 *
744 * Returns:
745 * - 0 on success
746 * - a negative value else
747 */
748 int
749 sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj);
751 /*
752 * sdb_store_json_emit_full:
753 * Serialize a single object including it's attributes and all children to
754 * JSON, adding it to the string buffer associated with the formatter object.
755 * The filter, if specified, is applied to each attribute and child object.
756 * Only matching objects will be included in the output.
757 *
758 * Note that the output might not be valid JSON before calling
759 * sdb_store_json_finish().
760 *
761 * Returns:
762 * - 0 on success
763 * - a negative value else
764 */
765 int
766 sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
767 sdb_store_matcher_t *filter);
769 /*
770 * sdb_store_json_finish:
771 * Finish the JSON output. This function has to be called once after emiting
772 * all objects.
773 */
774 int
775 sdb_store_json_finish(sdb_store_json_formatter_t *f);
777 #ifdef __cplusplus
778 } /* extern "C" */
779 #endif
781 #endif /* ! SDB_CORE_STORE_H */
783 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */