Code

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