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 * 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 */
135 struct sdb_store_expr_iter;
136 typedef struct sdb_store_expr_iter sdb_store_expr_iter_t;
138 /*
139 * Store matchers may be used to lookup hosts from the store based on their
140 * various attributes. Service and attribute matchers are applied to a host's
141 * services and attributes and evaluate to true if *any* service or attribute
142 * matches.
143 *
144 * A store matcher object inherits from sdb_object_t and, thus, may safely be
145 * cast to a generic object.
146 */
147 struct sdb_store_matcher;
148 typedef struct sdb_store_matcher sdb_store_matcher_t;
149 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
151 /*
152 * A JSON formatter converts stored objects into the JSON format.
153 * See http://www.ietf.org/rfc/rfc4627.txt
154 *
155 * A JSON formatter object inherits from sdb_object_t and, thus, may safely be
156 * cast to a generic object.
157 */
158 struct sdb_store_json_formatter;
159 typedef struct sdb_store_json_formatter sdb_store_json_formatter_t;
161 /*
162 * A store writer describes the interface for plugins implementing a store.
163 *
164 * Any of the call-back functions shall return:
165 * - 0 on success
166 * - a positive value if the new entry is older than the currently stored
167 * entry (in this case, no update will happen)
168 * - a negative value on error
169 */
170 typedef struct {
171 /*
172 * store_host:
173 * Add/update a host in the store. If the host, identified by its
174 * canonicalized name, already exists, it will be updated according to the
175 * specified name and timestamp. Else, a new entry will be created in the
176 * store.
177 */
178 int (*store_host)(const char *name, sdb_time_t last_update,
179 sdb_object_t *user_data);
181 /*
182 * store_service:
183 * Add/update a service in the store. If the service, identified by its
184 * name, already exists for the specified host, it will be updated
185 * according to the specified name and timestamp. If the referenced host
186 * does not exist, an error will be reported. Else, a new entry will be
187 * created in the store.
188 */
189 int (*store_service)(const char *hostname, const char *name,
190 sdb_time_t last_update, sdb_object_t *user_data);
192 /*
193 * store_metric:
194 * Add/update a metric in the store. If the metric, identified by its
195 * name, already exists for the specified host, it will be updated
196 * according to the specified attributes. If the referenced host does not
197 * exist, an error will be reported. Else, a new entry will be created in
198 * the store.
199 */
200 int (*store_metric)(const char *hostname, const char *name,
201 sdb_metric_store_t *store, sdb_time_t last_update,
202 sdb_object_t *user_data);
204 /*
205 * store_attribute:
206 * Add/update a host's attribute in the store. If the attribute,
207 * identified by its key, already exists for the specified host, it will
208 * be updated to the specified values. If the referenced host does not
209 * exist, an error will be reported. Else, a new entry will be created in
210 * the store.
211 */
212 int (*store_attribute)(const char *hostname,
213 const char *key, const sdb_data_t *value, sdb_time_t last_update,
214 sdb_object_t *user_data);
216 /*
217 * store_service_attr:
218 * Add/update a service's attribute in the store. If the attribute,
219 * identified by its key, already exists for the specified service, it
220 * will be updated to the specified value. If the references service (for
221 * the specified host) does not exist, an error will be reported.
222 */
223 int (*store_service_attr)(const char *hostname, const char *service,
224 const char *key, const sdb_data_t *value, sdb_time_t last_update,
225 sdb_object_t *user_data);
227 /*
228 * store_metric_attr:
229 * Add/update a metric's attribute in the store. If the attribute,
230 * identified by its key, already exists for the specified metric, it will
231 * be updated to the specified value. If the references metric (for the
232 * specified host) does not exist, an error will be reported.
233 */
234 int (*store_metric_attr)(const char *hostname, const char *metric,
235 const char *key, const sdb_data_t *value, sdb_time_t last_update,
236 sdb_object_t *user_data);
237 } sdb_store_writer_t;
239 /*
240 * sdb_store_writer:
241 * A store writer implementation that provides an in-memory object store. It
242 * expects a store object as its user-data argument.
243 */
244 extern sdb_store_writer_t sdb_store_writer;
246 /*
247 * A store reader describes the interface to query a store implementation.
248 */
249 typedef struct {
250 /*
251 * prepare_query:
252 * Prepare the query described by 'ast' for execution.
253 */
254 sdb_object_t *(*prepare_query)(sdb_ast_node_t *ast,
255 sdb_strbuf_t *errbuf, sdb_object_t *user_data);
257 /*
258 * execute_query:
259 * Execute a previously prepared query. The callback may expect that only
260 * queries prepared by its respective prepare callback will be passed to
261 * this function.
262 *
263 * TODO: Instead of letting the executor write directly to a string buffer
264 * (which cannot easily be merged with other results), let it hand
265 * all objects to a store-writer.
266 */
267 int (*execute_query)(sdb_object_t *q,
268 sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
269 sdb_object_t *user_data);
270 } sdb_store_reader_t;
272 /*
273 * sdb_store_reader:
274 * A store reader implementation that uses an in-memory object store. It
275 * expects a store object as its user-data argument.
276 */
277 extern sdb_store_reader_t sdb_store_reader;
279 /*
280 * sdb_store_create:
281 * Allocate a new in-memory store.
282 */
283 sdb_store_t *
284 sdb_store_create(void);
286 /*
287 * sdb_store_host, sdb_store_service, sdb_store_metric, sdb_store_attribute,
288 * sdb_store_metric_attr:
289 * Store an object in the specified store. The hostname is expected to be
290 * canonical.
291 */
292 int
293 sdb_store_host(sdb_store_t *store, const char *name, sdb_time_t last_update);
294 int
295 sdb_store_service(sdb_store_t *store, const char *hostname, const char *name,
296 sdb_time_t last_update);
297 int
298 sdb_store_metric(sdb_store_t *store, const char *hostname, const char *name,
299 sdb_metric_store_t *metric_store, sdb_time_t last_update);
300 int
301 sdb_store_attribute(sdb_store_t *store, const char *hostname,
302 const char *key, const sdb_data_t *value, sdb_time_t last_update);
303 int
304 sdb_store_service_attr(sdb_store_t *store, const char *hostname,
305 const char *service, const char *key, const sdb_data_t *value,
306 sdb_time_t last_update);
307 int
308 sdb_store_metric_attr(sdb_store_t *store, const char *hostname,
309 const char *metric, const char *key, const sdb_data_t *value,
310 sdb_time_t last_update);
312 /*
313 * sdb_store_get_host:
314 * Query the specified store for a host by its (canonicalized) name.
315 *
316 * The function increments the ref count of the host object. The caller needs
317 * to deref it when no longer using it.
318 */
319 sdb_store_obj_t *
320 sdb_store_get_host(sdb_store_t *store, const char *name);
322 /*
323 * sdb_store_fetch_timeseries:
324 * Fetch the time-series described by the specified host's metric and
325 * serialize it as JSON into the provided string buffer. The host data is
326 * retrieved from the specified store.
327 *
328 * Returns:
329 * - 0 on success
330 * - a negative value else
331 */
332 int
333 sdb_store_fetch_timeseries(sdb_store_t *store,
334 const char *hostname, const char *metric,
335 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf);
337 /*
338 * sdb_store_get_child:
339 * Retrieve a host's child object of the specified type and name. The
340 * reference count of the child object will be incremented before returning
341 * it. The caller is responsible for releasing the object once it's no longer
342 * used.
343 *
344 * Returns:
345 * - the child object on success
346 * - NULL else
347 */
348 sdb_store_obj_t *
349 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name);
351 /*
352 * sdb_store_get_field:
353 * Get the value of a stored object's queryable field. The caller is
354 * responsible for freeing any dynamically allocated memory possibly stored in
355 * the returned value. If 'res' is NULL, the function will return whether the
356 * field exists.
357 *
358 * Returns:
359 * - 0 on success
360 * - a negative value else
361 */
362 int
363 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
365 /*
366 * sdb_store_get_attr:
367 * Get the value of a stored object's attribute. The caller is responsible for
368 * freeing any dynamically allocated memory possibly stored in the returned
369 * value. If 'res' is NULL, the function will return whether the attribute
370 * exists. If specified, only attributes matching the filter will be
371 * considered.
372 *
373 * Returns:
374 * - 0 if the attribute exists
375 * - a negative value else
376 */
377 int
378 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
379 sdb_store_matcher_t *filter);
381 /*
382 * Querying a store:
383 *
384 * - Query interface: A query is a formal description of an interaction with
385 * the store. It can be used, both, for read and write access. The query is
386 * described by its abstract syntax tree (AST). The parser package provides
387 * means to parse a string (SysQL) representation of the query into an AST.
388 *
389 * - Matcher / expression interface: This low-level interface provides direct
390 * control over how to access the store. It is used by the query
391 * implementation internally and can only be used for read access.
392 */
394 /*
395 * sdb_store_query_t:
396 * A parsed query readily prepared for execution.
397 */
398 struct sdb_store_query;
399 typedef struct sdb_store_query sdb_store_query_t;
401 /*
402 * sdb_store_query_prepare:
403 * Prepare the query described by 'ast' for execution in a store.
404 *
405 * Returns:
406 * - a store query on success
407 * - NULL else
408 */
409 sdb_store_query_t *
410 sdb_store_query_prepare(sdb_ast_node_t *ast);
412 /*
413 * sdb_store_query_prepare_matcher:
414 * Prepare the logical expression described by 'ast' for execution as a store
415 * matcher.
416 *
417 * Returns:
418 * - a matcher on success
419 * - NULL else
420 */
421 sdb_store_matcher_t *
422 sdb_store_query_prepare_matcher(sdb_ast_node_t *ast);
424 /*
425 * sdb_store_query_execute:
426 * Execute a previously prepared query in the specified store. The query
427 * result will be written to 'buf' and any errors to 'errbuf'.
428 *
429 * Returns:
430 * - the result type (to be used by the server reply)
431 * - a negative value on error
432 */
433 int
434 sdb_store_query_execute(sdb_store_t *store, sdb_store_query_t *m,
435 sdb_strbuf_t *buf, sdb_strbuf_t *errbuf);
437 /*
438 * sdb_store_expr_create:
439 * Creates an arithmetic expression implementing the specified operator on the
440 * specified left and right operand.
441 *
442 * Returns:
443 * - an expression object on success
444 * - NULL else
445 */
446 sdb_store_expr_t *
447 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
449 /*
450 * sdb_store_expr_typed:
451 * Creates an expression which evaluates in the context of an object's sibling
452 * as specified by the given type.
453 *
454 * Returns:
455 * - an expression object on success
456 * - NULL else
457 */
458 sdb_store_expr_t *
459 sdb_store_expr_typed(int typ, sdb_store_expr_t *expr);
461 /*
462 * sdb_store_expr_fieldvalue:
463 * Creates an expression which evaluates to the value of the specified
464 * queryable field of a stored object.
465 *
466 * Returns:
467 * - an expression object on success
468 * - NULL else
469 */
470 sdb_store_expr_t *
471 sdb_store_expr_fieldvalue(int field);
473 /*
474 * sdb_store_expr_attrvalue:
475 * Creates an expression which evaluates to the value of the specified
476 * attribute of a stored object. Evaluates to a NULL value if the attribute
477 * does not exist.
478 *
479 * Returns:
480 * - an expression object on success
481 * - NULL else
482 */
483 sdb_store_expr_t *
484 sdb_store_expr_attrvalue(const char *name);
486 /*
487 * sdb_store_expr_constvalue:
488 * Creates an expression which evaluates to the specified constant value.
489 *
490 * Returns:
491 * - an expression object on success
492 * - NULL else
493 */
494 sdb_store_expr_t *
495 sdb_store_expr_constvalue(const sdb_data_t *value);
497 /*
498 * sdb_store_expr_eval:
499 * Evaluate an expression for the specified stored object and stores the
500 * result in 'res'. The result's value will be allocated dynamically if
501 * necessary and, thus, should be free'd by the caller (e.g. using
502 * sdb_data_free_datum). The object may be NULL, in which case the expression
503 * needs to evaluate to a constant value. If specified, only objects matching
504 * the filter will be used during the evaluation.
505 *
506 * Returns:
507 * - 0 on success
508 * - a negative value else
509 */
510 int
511 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
512 sdb_data_t *res, sdb_store_matcher_t *filter);
514 /*
515 * sdb_store_expr_iter:
516 * Iterate over the elements of an iterable expression. sdb_store_expr_iter
517 * returns NULL if the expression is not iterable (for the specified object).
518 *
519 * sdb_store_expr_iter_get_next returns NULL if there is no next element.
520 */
521 sdb_store_expr_iter_t *
522 sdb_store_expr_iter(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
523 sdb_store_matcher_t *filter);
524 void
525 sdb_store_expr_iter_destroy(sdb_store_expr_iter_t *iter);
527 bool
528 sdb_store_expr_iter_has_next(sdb_store_expr_iter_t *iter);
529 sdb_data_t
530 sdb_store_expr_iter_get_next(sdb_store_expr_iter_t *iter);
532 /*
533 * sdb_store_dis_matcher:
534 * Creates a matcher matching the disjunction (logical OR) of two matchers.
535 */
536 sdb_store_matcher_t *
537 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
539 /*
540 * sdb_store_con_matcher:
541 * Creates a matcher matching the conjunction (logical AND) of two matchers.
542 */
543 sdb_store_matcher_t *
544 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
546 /*
547 * sdb_store_inv_matcher::
548 * Creates a matcher matching the inverse (logical NOT) of a matcher.
549 */
550 sdb_store_matcher_t *
551 sdb_store_inv_matcher(sdb_store_matcher_t *m);
553 /*
554 * sdb_store_any_matcher:
555 * Creates a matcher iterating over values of the first expression (which has
556 * to be iterable). It matches if *any* of those elements match 'm'. 'm' has
557 * to be an ary operation with the left operand unset.
558 */
559 sdb_store_matcher_t *
560 sdb_store_any_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m);
562 /*
563 * sdb_store_all_matcher:
564 * Creates a matcher iterating over values of the first expression (which has
565 * to be iterable). It matches if *all* of those elements match 'm'. 'm' has
566 * to be an ary operation with the left operand unset.
567 */
568 sdb_store_matcher_t *
569 sdb_store_all_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m);
571 /*
572 * sdb_store_in_matcher:
573 * Creates a matcher which matches if the right value evaluates to an array
574 * value and the left value is included in that array. See sdb_data_inarray
575 * for more details.
576 */
577 sdb_store_matcher_t *
578 sdb_store_in_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_istrue_matcher, sdb_store_isfalse_matcher:
629 * Creates a matcher matching boolean values.
630 */
631 sdb_store_matcher_t *
632 sdb_store_istrue_matcher(sdb_store_expr_t *expr);
633 sdb_store_matcher_t *
634 sdb_store_isfalse_matcher(sdb_store_expr_t *expr);
636 /*
637 * sdb_store_matcher_matches:
638 * Check whether the specified matcher matches the specified store object. If
639 * specified, the filter will be used to preselect objects for further
640 * evaluation. It is applied to any object that's used during the evaluation
641 * of the matcher. Only those objects matching the filter will be considered.
642 *
643 * Note that the filter is applied to all object types (hosts, service,
644 * metric, attribute). Thus, any object-specific matchers are mostly unsuited
645 * for this purpose and, if used, may result in unexpected behavior.
646 *
647 * Returns:
648 * - 1 if the object matches
649 * - 0 else
650 */
651 int
652 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
653 sdb_store_matcher_t *filter);
655 /*
656 * sdb_store_matcher_op_cb:
657 * Callback constructing a matcher operator.
658 */
659 typedef sdb_store_matcher_t *(*sdb_store_matcher_op_cb)
660 (sdb_store_expr_t *, sdb_store_expr_t *);
662 /*
663 * sdb_store_lookup_cb:
664 * Lookup callback. It is called for each matching object when looking up data
665 * in the store passing on the lookup filter and the specified user-data. The
666 * lookup aborts early if the callback returns non-zero.
667 */
668 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj,
669 sdb_store_matcher_t *filter, void *user_data);
671 /*
672 * sdb_store_scan:
673 * Look up objects of the specified type in the specified store. The specified
674 * callback function is called for each object in the store matching 'm'. The
675 * function performs a full scan of all stored objects. If specified, the
676 * filter will be used to preselect objects for further evaluation. See the
677 * description of 'sdb_store_matcher_matches' for details.
678 *
679 * Returns:
680 * - 0 on success
681 * - a negative value else
682 */
683 int
684 sdb_store_scan(sdb_store_t *store, int type,
685 sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
686 sdb_store_lookup_cb cb, void *user_data);
688 /*
689 * Flags for JSON formatting.
690 */
691 enum {
692 SDB_WANT_ARRAY = 1 << 0,
693 };
695 /*
696 * sdb_store_json_formatter:
697 * Create a JSON formatter for the specified object types writing to the
698 * specified buffer.
699 */
700 sdb_store_json_formatter_t *
701 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags);
703 /*
704 * sdb_store_json_emit:
705 * Serialize a single object to JSON adding it to the string buffer associated
706 * with the formatter object. The serialized object will not include
707 * attributes or any child objects. Instead, call the function again for each
708 * of those objects. All attributes have to be emitted before any other
709 * children types. Use sdb_store_json_emit_full() to emit a full (filtered)
710 * object.
711 *
712 * Note that the output might not be valid JSON before calling
713 * sdb_store_json_finish().
714 *
715 * Returns:
716 * - 0 on success
717 * - a negative value else
718 */
719 int
720 sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj);
722 /*
723 * sdb_store_json_emit_full:
724 * Serialize a single object including it's attributes and all children to
725 * JSON, adding it to the string buffer associated with the formatter object.
726 * The filter, if specified, is applied to each attribute and child object.
727 * Only matching objects will be included in the output.
728 *
729 * Note that the output might not be valid JSON before calling
730 * sdb_store_json_finish().
731 *
732 * Returns:
733 * - 0 on success
734 * - a negative value else
735 */
736 int
737 sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
738 sdb_store_matcher_t *filter);
740 /*
741 * sdb_store_json_finish:
742 * Finish the JSON output. This function has to be called once after emiting
743 * all objects.
744 */
745 int
746 sdb_store_json_finish(sdb_store_json_formatter_t *f);
748 #ifdef __cplusplus
749 } /* extern "C" */
750 #endif
752 #endif /* ! SDB_CORE_STORE_H */
754 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */