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