Code

Move the timeseries serialize to the timeseries module.
[sysdb.git] / src / include / core / store.h
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 stored object. It
107  * inherits from sdb_object_t and may safely be cast to a generic object to
108  * access its name.
109  */
110 struct sdb_store_obj;
111 typedef struct sdb_store_obj sdb_store_obj_t;
113 /*
114  * sdb_store_host_t represents the meta-data of a stored host object.
115  */
116 typedef struct {
117         const char *name;
119         sdb_time_t last_update;
120         sdb_time_t interval;
121         const char * const *backends;
122         size_t backends_num;
123 } sdb_store_host_t;
125 /*
126  * sdb_store_service_t represents the meta-data of a stored service object.
127  */
128 typedef struct {
129         const char *hostname;
130         const char *name;
132         sdb_time_t last_update;
133         sdb_time_t interval;
134         const char * const *backends;
135         size_t backends_num;
136 } sdb_store_service_t;
138 /*
139  * sdb_metric_store_t specifies how to access a metric's data.
140  */
141 typedef struct {
142         const char *type;
143         const char *id;
144 } sdb_metric_store_t;
146 /*
147  * sdb_store_metric_t represents the meta-data of a stored metric object.
148  */
149 typedef struct {
150         const char *hostname;
151         const char *name;
152         struct {
153                 const char *type;
154                 const char *id;
155         } store;
157         sdb_time_t last_update;
158         sdb_time_t interval;
159         const char * const *backends;
160         size_t backends_num;
161 } sdb_store_metric_t;
163 /*
164  * sdb_store_attribute_t represents a stored attribute.
165  */
166 typedef struct {
167         const char *hostname; /* optional */
168         int parent_type;
169         const char *parent;
170         const char *key;
171         sdb_data_t value;
173         sdb_time_t last_update;
174         sdb_time_t interval;
175         const char * const *backends;
176         size_t backends_num;
177 } sdb_store_attribute_t;
179 /*
180  * Expressions represent arithmetic expressions based on stored objects and
181  * their various attributes.
182  *
183  * An expression object inherits from sdb_object_t and, thus, may safely be
184  * cast to a generic object.
185  */
186 struct sdb_store_expr;
187 typedef struct sdb_store_expr sdb_store_expr_t;
188 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
190 /*
191  * An expression iterator iterates over the values of an iterable expression.
192  */
193 struct sdb_store_expr_iter;
194 typedef struct sdb_store_expr_iter sdb_store_expr_iter_t;
196 /*
197  * Store matchers may be used to lookup hosts from the store based on their
198  * various attributes. Service and attribute matchers are applied to a host's
199  * services and attributes and evaluate to true if *any* service or attribute
200  * matches.
201  *
202  * A store matcher object inherits from sdb_object_t and, thus, may safely be
203  * cast to a generic object.
204  */
205 struct sdb_store_matcher;
206 typedef struct sdb_store_matcher sdb_store_matcher_t;
207 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
209 /*
210  * A JSON formatter converts stored objects into the JSON format.
211  * See http://www.ietf.org/rfc/rfc4627.txt
212  *
213  * A JSON formatter object inherits from sdb_object_t and, thus, may safely be
214  * cast to a generic object.
215  */
216 struct sdb_store_json_formatter;
217 typedef struct sdb_store_json_formatter sdb_store_json_formatter_t;
219 /*
220  * A store writer describes the interface for plugins implementing a store.
221  *
222  * Any of the call-back functions shall return:
223  *  - 0 on success
224  *  - a positive value if the new entry is older than the currently stored
225  *    entry (in this case, no update will happen)
226  *  - a negative value on error
227  */
228 typedef struct {
229         /*
230          * store_host:
231          * Add/update a host in the store. If the host, identified by its
232          * canonicalized name, already exists, it will be updated according to the
233          * specified name and timestamp. Else, a new entry will be created in the
234          * store.
235          */
236         int (*store_host)(sdb_store_host_t *host, sdb_object_t *user_data);
238         /*
239          * store_service:
240          * Add/update a service in the store. If the service, identified by its
241          * name, already exists for the specified host, it will be updated
242          * according to the specified name and timestamp. If the referenced host
243          * does not exist, an error will be reported. Else, a new entry will be
244          * created in the store.
245          */
246         int (*store_service)(sdb_store_service_t *service, sdb_object_t *user_data);
248         /*
249          * store_metric:
250          * Add/update a metric in the store. If the metric, identified by its
251          * name, already exists for the specified host, it will be updated
252          * according to the specified attributes. If the referenced host does not
253          * exist, an error will be reported. Else, a new entry will be created in
254          * the store.
255          */
256         int (*store_metric)(sdb_store_metric_t *metric, sdb_object_t *user_data);
258         /*
259          * store_attribute:
260          * Add/update a host's attribute in the store. If the attribute,
261          * identified by its key, already exists for the specified host, it will
262          * be updated to the specified values. If the referenced host does not
263          * exist, an error will be reported. Else, a new entry will be created in
264          * the store.
265          */
266         int (*store_attribute)(sdb_store_attribute_t *attr, sdb_object_t *user_data);
267 } sdb_store_writer_t;
269 /*
270  * sdb_store_writer:
271  * A store writer implementation that provides an in-memory object store. It
272  * expects a store object as its user-data argument.
273  */
274 extern sdb_store_writer_t sdb_store_writer;
276 /*
277  * A store reader describes the interface to query a store implementation.
278  */
279 typedef struct {
280         /*
281          * prepare_query:
282          * Prepare the query described by 'ast' for execution.
283          */
284         sdb_object_t *(*prepare_query)(sdb_ast_node_t *ast,
285                         sdb_strbuf_t *errbuf, sdb_object_t *user_data);
287         /*
288          * execute_query:
289          * Execute a previously prepared query. The callback may expect that only
290          * queries prepared by its respective prepare callback will be passed to
291          * this function.
292          *
293          * TODO: Instead of letting the executor write directly to a string buffer
294          *       (which cannot easily be merged with other results), let it hand
295          *       all objects to a store-writer.
296          */
297         int (*execute_query)(sdb_object_t *q,
298                         sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
299                         sdb_object_t *user_data);
300 } sdb_store_reader_t;
302 /*
303  * sdb_store_reader:
304  * A store reader implementation that uses an in-memory object store. It
305  * expects a store object as its user-data argument.
306  */
307 extern sdb_store_reader_t sdb_store_reader;
309 /*
310  * sdb_store_create:
311  * Allocate a new in-memory store.
312  */
313 sdb_store_t *
314 sdb_store_create(void);
316 /*
317  * sdb_store_host, sdb_store_service, sdb_store_metric, sdb_store_attribute,
318  * sdb_store_metric_attr:
319  * Store an object in the specified store. The hostname is expected to be
320  * canonical.
321  */
322 int
323 sdb_store_host(sdb_store_t *store, const char *name, sdb_time_t last_update);
324 int
325 sdb_store_service(sdb_store_t *store, const char *hostname, const char *name,
326                 sdb_time_t last_update);
327 int
328 sdb_store_metric(sdb_store_t *store, const char *hostname, const char *name,
329                 sdb_metric_store_t *metric_store, sdb_time_t last_update);
330 int
331 sdb_store_attribute(sdb_store_t *store, const char *hostname,
332                 const char *key, const sdb_data_t *value, sdb_time_t last_update);
333 int
334 sdb_store_service_attr(sdb_store_t *store, const char *hostname,
335                 const char *service, const char *key, const sdb_data_t *value,
336                 sdb_time_t last_update);
337 int
338 sdb_store_metric_attr(sdb_store_t *store, const char *hostname,
339                 const char *metric, const char *key, const sdb_data_t *value,
340                 sdb_time_t last_update);
342 /*
343  * sdb_store_get_host:
344  * Query the specified store for a host by its (canonicalized) name.
345  *
346  * The function increments the ref count of the host object. The caller needs
347  * to deref it when no longer using it.
348  */
349 sdb_store_obj_t *
350 sdb_store_get_host(sdb_store_t *store, const char *name);
352 /*
353  * sdb_store_fetch_timeseries:
354  * Fetch the time-series described by the specified host's metric and
355  * serialize it as JSON into the provided string buffer. The host data is
356  * retrieved from the specified store.
357  *
358  * Returns:
359  *  - 0 on success
360  *  - a negative value else
361  */
362 int
363 sdb_store_fetch_timeseries(sdb_store_t *store,
364                 const char *hostname, const char *metric,
365                 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf);
367 /*
368  * sdb_store_get_child:
369  * Retrieve a host's child object of the specified type and name. The
370  * reference count of the child object will be incremented before returning
371  * it. The caller is responsible for releasing the object once it's no longer
372  * used.
373  *
374  * Returns:
375  *  - the child object on success
376  *  - NULL else
377  */
378 sdb_store_obj_t *
379 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name);
381 /*
382  * sdb_store_get_field:
383  * Get the value of a stored object's queryable field. The caller is
384  * responsible for freeing any dynamically allocated memory possibly stored in
385  * the returned value. If 'res' is NULL, the function will return whether the
386  * field exists.
387  *
388  * Returns:
389  *  - 0 on success
390  *  - a negative value else
391  */
392 int
393 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
395 /*
396  * sdb_store_get_attr:
397  * Get the value of a stored object's attribute. The caller is responsible for
398  * freeing any dynamically allocated memory possibly stored in the returned
399  * value. If 'res' is NULL, the function will return whether the attribute
400  * exists. If specified, only attributes matching the filter will be
401  * considered.
402  *
403  * Returns:
404  *  - 0 if the attribute exists
405  *  - a negative value else
406  */
407 int
408 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
409                 sdb_store_matcher_t *filter);
411 /*
412  * Querying a store:
413  *
414  *  - Query interface: A query is a formal description of an interaction with
415  *    the store. It can be used, both, for read and write access. The query is
416  *    described by its abstract syntax tree (AST). The parser package provides
417  *    means to parse a string (SysQL) representation of the query into an AST.
418  *
419  *  - Matcher / expression interface: This low-level interface provides direct
420  *    control over how to access the store. It is used by the query
421  *    implementation internally and can only be used for read access.
422  */
424 /*
425  * sdb_store_query_t:
426  * A parsed query readily prepared for execution.
427  */
428 struct sdb_store_query;
429 typedef struct sdb_store_query sdb_store_query_t;
431 /*
432  * sdb_store_query_prepare:
433  * Prepare the query described by 'ast' for execution in a store.
434  *
435  * Returns:
436  *  - a store query on success
437  *  - NULL else
438  */
439 sdb_store_query_t *
440 sdb_store_query_prepare(sdb_ast_node_t *ast);
442 /*
443  * sdb_store_query_prepare_matcher:
444  * Prepare the logical expression described by 'ast' for execution as a store
445  * matcher.
446  *
447  * Returns:
448  *  - a matcher on success
449  *  - NULL else
450  */
451 sdb_store_matcher_t *
452 sdb_store_query_prepare_matcher(sdb_ast_node_t *ast);
454 /*
455  * sdb_store_query_execute:
456  * Execute a previously prepared query in the specified store. The query
457  * result will be written to 'buf' and any errors to 'errbuf'.
458  *
459  * Returns:
460  *  - the result type (to be used by the server reply)
461  *  - a negative value on error
462  */
463 int
464 sdb_store_query_execute(sdb_store_t *store, sdb_store_query_t *m,
465                 sdb_strbuf_t *buf, sdb_strbuf_t *errbuf);
467 /*
468  * sdb_store_expr_create:
469  * Creates an arithmetic expression implementing the specified operator on the
470  * specified left and right operand.
471  *
472  * Returns:
473  *  - an expression object on success
474  *  - NULL else
475  */
476 sdb_store_expr_t *
477 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
479 /*
480  * sdb_store_expr_typed:
481  * Creates an expression which evaluates in the context of an object's sibling
482  * as specified by the given type.
483  *
484  * Returns:
485  *  - an expression object on success
486  *  - NULL else
487  */
488 sdb_store_expr_t *
489 sdb_store_expr_typed(int typ, sdb_store_expr_t *expr);
491 /*
492  * sdb_store_expr_fieldvalue:
493  * Creates an expression which evaluates to the value of the specified
494  * queryable field of a stored object.
495  *
496  * Returns:
497  *  - an expression object on success
498  *  - NULL else
499  */
500 sdb_store_expr_t *
501 sdb_store_expr_fieldvalue(int field);
503 /*
504  * sdb_store_expr_attrvalue:
505  * Creates an expression which evaluates to the value of the specified
506  * attribute of a stored object. Evaluates to a NULL value if the attribute
507  * does not exist.
508  *
509  * Returns:
510  *  - an expression object on success
511  *  - NULL else
512  */
513 sdb_store_expr_t *
514 sdb_store_expr_attrvalue(const char *name);
516 /*
517  * sdb_store_expr_constvalue:
518  * Creates an expression which evaluates to the specified constant value.
519  *
520  * Returns:
521  *  - an expression object on success
522  *  - NULL else
523  */
524 sdb_store_expr_t *
525 sdb_store_expr_constvalue(const sdb_data_t *value);
527 /*
528  * sdb_store_expr_eval:
529  * Evaluate an expression for the specified stored object and stores the
530  * result in 'res'. The result's value will be allocated dynamically if
531  * necessary and, thus, should be free'd by the caller (e.g. using
532  * sdb_data_free_datum). The object may be NULL, in which case the expression
533  * needs to evaluate to a constant value. If specified, only objects matching
534  * the filter will be used during the evaluation.
535  *
536  * Returns:
537  *  - 0 on success
538  *  - a negative value else
539  */
540 int
541 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
542                 sdb_data_t *res, sdb_store_matcher_t *filter);
544 /*
545  * sdb_store_expr_iter:
546  * Iterate over the elements of an iterable expression. sdb_store_expr_iter
547  * returns NULL if the expression is not iterable (for the specified object).
548  *
549  * sdb_store_expr_iter_get_next returns NULL if there is no next element.
550  */
551 sdb_store_expr_iter_t *
552 sdb_store_expr_iter(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
553                 sdb_store_matcher_t *filter);
554 void
555 sdb_store_expr_iter_destroy(sdb_store_expr_iter_t *iter);
557 bool
558 sdb_store_expr_iter_has_next(sdb_store_expr_iter_t *iter);
559 sdb_data_t
560 sdb_store_expr_iter_get_next(sdb_store_expr_iter_t *iter);
562 /*
563  * sdb_store_dis_matcher:
564  * Creates a matcher matching the disjunction (logical OR) of two matchers.
565  */
566 sdb_store_matcher_t *
567 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
569 /*
570  * sdb_store_con_matcher:
571  * Creates a matcher matching the conjunction (logical AND) of two matchers.
572  */
573 sdb_store_matcher_t *
574 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
576 /*
577  * sdb_store_inv_matcher::
578  * Creates a matcher matching the inverse (logical NOT) of a matcher.
579  */
580 sdb_store_matcher_t *
581 sdb_store_inv_matcher(sdb_store_matcher_t *m);
583 /*
584  * sdb_store_any_matcher:
585  * Creates a matcher iterating over values of the first expression (which has
586  * to be iterable). It matches if *any* of those elements match 'm'. 'm' has
587  * to be an ary operation with the left operand unset.
588  */
589 sdb_store_matcher_t *
590 sdb_store_any_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m);
592 /*
593  * sdb_store_all_matcher:
594  * Creates a matcher iterating over values of the first expression (which has
595  * to be iterable). It matches if *all* of those elements match 'm'. 'm' has
596  * to be an ary operation with the left operand unset.
597  */
598 sdb_store_matcher_t *
599 sdb_store_all_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m);
601 /*
602  * sdb_store_in_matcher:
603  * Creates a matcher which matches if the right value evaluates to an array
604  * value and the left value is included in that array. See sdb_data_inarray
605  * for more details.
606  */
607 sdb_store_matcher_t *
608 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
610 /*
611  * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
612  * sdb_store_ge_matcher, sdb_store_gt_matcher:
613  * Create conditional matchers comparing the values of two expressions. The
614  * matcher matches if the left expression compres less than, less or equal
615  * than, equal to, not equal to, greater or equal than, or greater than the
616  * right expression.
617  */
618 sdb_store_matcher_t *
619 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
620 sdb_store_matcher_t *
621 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
622 sdb_store_matcher_t *
623 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
624 sdb_store_matcher_t *
625 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
626 sdb_store_matcher_t *
627 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
628 sdb_store_matcher_t *
629 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
631 /*
632  * sdb_store_regex_matcher:
633  * Creates a matcher which matches the string value the left expression
634  * evaluates to against the regular expression the right expression evaluates
635  * to. The right expression may either be a constant value regular expression
636  * or string or a dynamic value evaluating to a string. In the latter case,
637  * the string is compiled to a regex every time the matcher is executed.
638  */
639 sdb_store_matcher_t *
640 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
642 /*
643  * sdb_store_nregex_matcher:
644  * Creates a regex matcher just like sdb_store_regex_matcher except that it
645  * matches in case the regular expression does not match.
646  */
647 sdb_store_matcher_t *
648 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
650 /*
651  * sdb_store_isnull_matcher:
652  * Creates a matcher matching NULL values.
653  */
654 sdb_store_matcher_t *
655 sdb_store_isnull_matcher(sdb_store_expr_t *expr);
657 /*
658  * sdb_store_istrue_matcher, sdb_store_isfalse_matcher:
659  * Creates a matcher matching boolean values.
660  */
661 sdb_store_matcher_t *
662 sdb_store_istrue_matcher(sdb_store_expr_t *expr);
663 sdb_store_matcher_t *
664 sdb_store_isfalse_matcher(sdb_store_expr_t *expr);
666 /*
667  * sdb_store_matcher_matches:
668  * Check whether the specified matcher matches the specified store object. If
669  * specified, the filter will be used to preselect objects for further
670  * evaluation. It is applied to any object that's used during the evaluation
671  * of the matcher. Only those objects matching the filter will be considered.
672  *
673  * Note that the filter is applied to all object types (hosts, service,
674  * metric, attribute). Thus, any object-specific matchers are mostly unsuited
675  * for this purpose and, if used, may result in unexpected behavior.
676  *
677  * Returns:
678  *  - 1 if the object matches
679  *  - 0 else
680  */
681 int
682 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
683                 sdb_store_matcher_t *filter);
685 /*
686  * sdb_store_matcher_op_cb:
687  * Callback constructing a matcher operator.
688  */
689 typedef sdb_store_matcher_t *(*sdb_store_matcher_op_cb)
690         (sdb_store_expr_t *, sdb_store_expr_t *);
692 /*
693  * sdb_store_lookup_cb:
694  * Lookup callback. It is called for each matching object when looking up data
695  * in the store passing on the lookup filter and the specified user-data. The
696  * lookup aborts early if the callback returns non-zero.
697  */
698 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj,
699                 sdb_store_matcher_t *filter, void *user_data);
701 /*
702  * sdb_store_scan:
703  * Look up objects of the specified type in the specified store. The specified
704  * callback function is called for each object in the store matching 'm'. The
705  * function performs a full scan of all stored objects. If specified, the
706  * filter will be used to preselect objects for further evaluation. See the
707  * description of 'sdb_store_matcher_matches' for details.
708  *
709  * Returns:
710  *  - 0 on success
711  *  - a negative value else
712  */
713 int
714 sdb_store_scan(sdb_store_t *store, int type,
715                 sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
716                 sdb_store_lookup_cb cb, void *user_data);
718 /*
719  * Flags for JSON formatting.
720  */
721 enum {
722         SDB_WANT_ARRAY = 1 << 0,
723 };
725 /*
726  * sdb_store_json_formatter:
727  * Create a JSON formatter for the specified object types writing to the
728  * specified buffer.
729  */
730 sdb_store_json_formatter_t *
731 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags);
733 /*
734  * sdb_store_emit:
735  * Serialize a single object to JSON adding it to the string buffer associated
736  * with the formatter object. The serialized object will not include
737  * attributes or any child objects. Instead, call the function again for each
738  * of those objects. All attributes have to be emitted before any other
739  * children types. Use sdb_store_emit_full() to emit a full (filtered) 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_emit(sdb_store_obj_t *obj, sdb_store_writer_t *w, sdb_object_t *wd);
751 /*
752  * sdb_store_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_emit_full(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
767                 sdb_store_writer_t *w, sdb_object_t *wd);
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 /*
778  * sdb_store_json_writer:
779  * A store writer implementation that generates JSON output. It expects a
780  * store JSON formatter as its user-data argument.
781  */
782 extern sdb_store_writer_t sdb_store_json_writer;
784 #ifdef __cplusplus
785 } /* extern "C" */
786 #endif
788 #endif /* ! SDB_CORE_STORE_H */
790 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */