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