Code

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