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 * sdb_store_query_prepare:
365 * Prepare the query described by 'ast' for execution in a store.
366 *
367 * Returns:
368 * - a store matcher on success
369 * - NULL else
370 */
371 sdb_store_matcher_t *
372 sdb_store_query_prepare(sdb_ast_node_t *ast);
374 /*
375 * sdb_store_expr_create:
376 * Creates an arithmetic expression implementing the specified operator on the
377 * specified left and right operand.
378 *
379 * Returns:
380 * - an expression object on success
381 * - NULL else
382 */
383 sdb_store_expr_t *
384 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
386 /*
387 * sdb_store_expr_typed:
388 * Creates an expression which evaluates in the context of an object's sibling
389 * as specified by the given type.
390 *
391 * Returns:
392 * - an expression object on success
393 * - NULL else
394 */
395 sdb_store_expr_t *
396 sdb_store_expr_typed(int typ, sdb_store_expr_t *expr);
398 /*
399 * sdb_store_expr_fieldvalue:
400 * Creates an expression which evaluates to the value of the specified
401 * queryable field of a stored object.
402 *
403 * Returns:
404 * - an expression object on success
405 * - NULL else
406 */
407 sdb_store_expr_t *
408 sdb_store_expr_fieldvalue(int field);
410 /*
411 * sdb_store_expr_attrvalue:
412 * Creates an expression which evaluates to the value of the specified
413 * attribute of a stored object. Evaluates to a NULL value if the attribute
414 * does not exist.
415 *
416 * Returns:
417 * - an expression object on success
418 * - NULL else
419 */
420 sdb_store_expr_t *
421 sdb_store_expr_attrvalue(const char *name);
423 /*
424 * sdb_store_expr_constvalue:
425 * Creates an expression which evaluates to the specified constant value.
426 *
427 * Returns:
428 * - an expression object on success
429 * - NULL else
430 */
431 sdb_store_expr_t *
432 sdb_store_expr_constvalue(const sdb_data_t *value);
434 /*
435 * sdb_store_expr_eval:
436 * Evaluate an expression for the specified stored object and stores the
437 * result in 'res'. The result's value will be allocated dynamically if
438 * necessary and, thus, should be free'd by the caller (e.g. using
439 * sdb_data_free_datum). The object may be NULL, in which case the expression
440 * needs to evaluate to a constant value. If specified, only objects matching
441 * the filter will be used during the evaluation.
442 *
443 * Returns:
444 * - 0 on success
445 * - a negative value else
446 */
447 int
448 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
449 sdb_data_t *res, sdb_store_matcher_t *filter);
451 /*
452 * sdb_store_expr_iterable:
453 * Check whether an expression, evaluated in the specified context (HOST,
454 * SERVICE, METRIC) is iterable, that is, if it may evaluate to multiple
455 * values.
456 */
457 bool
458 sdb_store_expr_iterable(sdb_store_expr_t *expr, int context);
460 /*
461 * sdb_store_expr_iter:
462 * Iterate over the elements of an iterable expression. sdb_store_expr_iter
463 * returns NULL if the expression is not iterable (for the specified object).
464 * See also sdb_store_expr_iterable.
465 *
466 * sdb_store_expr_iter_get_next returns NULL if there is no next element.
467 */
468 sdb_store_expr_iter_t *
469 sdb_store_expr_iter(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
470 sdb_store_matcher_t *filter);
471 void
472 sdb_store_expr_iter_destroy(sdb_store_expr_iter_t *iter);
474 bool
475 sdb_store_expr_iter_has_next(sdb_store_expr_iter_t *iter);
476 sdb_data_t
477 sdb_store_expr_iter_get_next(sdb_store_expr_iter_t *iter);
479 /*
480 * sdb_store_dis_matcher:
481 * Creates a matcher matching the disjunction (logical OR) of two matchers.
482 */
483 sdb_store_matcher_t *
484 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
486 /*
487 * sdb_store_con_matcher:
488 * Creates a matcher matching the conjunction (logical AND) of two matchers.
489 */
490 sdb_store_matcher_t *
491 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
493 /*
494 * sdb_store_inv_matcher::
495 * Creates a matcher matching the inverse (logical NOT) of a matcher.
496 */
497 sdb_store_matcher_t *
498 sdb_store_inv_matcher(sdb_store_matcher_t *m);
500 /*
501 * sdb_store_any_matcher:
502 * Creates a matcher iterating over values of the first expression (which has
503 * to be iterable). It matches if *any* of those elements match 'm'. 'm' has
504 * to be an ary operation with the left operand unset.
505 */
506 sdb_store_matcher_t *
507 sdb_store_any_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m);
509 /*
510 * sdb_store_all_matcher:
511 * Creates a matcher iterating over values of the first expression (which has
512 * to be iterable). It matches if *all* of those elements match 'm'. 'm' has
513 * to be an ary operation with the left operand unset.
514 */
515 sdb_store_matcher_t *
516 sdb_store_all_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m);
518 /*
519 * sdb_store_in_matcher:
520 * Creates a matcher which matches if the right value evaluates to an array
521 * value and the left value is included in that array. See sdb_data_inarray
522 * for more details.
523 */
524 sdb_store_matcher_t *
525 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
527 /*
528 * sdb_store_nin_matcher:
529 * Like sdb_store_in_matcher but matches if the left value is not included in
530 * the right value.
531 */
532 sdb_store_matcher_t *
533 sdb_store_nin_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
535 /*
536 * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
537 * sdb_store_ge_matcher, sdb_store_gt_matcher:
538 * Create conditional matchers comparing the values of two expressions. The
539 * matcher matches if the left expression compres less than, less or equal
540 * than, equal to, not equal to, greater or equal than, or greater than the
541 * right expression.
542 */
543 sdb_store_matcher_t *
544 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
545 sdb_store_matcher_t *
546 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
547 sdb_store_matcher_t *
548 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
549 sdb_store_matcher_t *
550 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
551 sdb_store_matcher_t *
552 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
553 sdb_store_matcher_t *
554 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
556 /*
557 * sdb_store_regex_matcher:
558 * Creates a matcher which matches the string value the left expression
559 * evaluates to against the regular expression the right expression evaluates
560 * to. The right expression may either be a constant value regular expression
561 * or string or a dynamic value evaluating to a string. In the latter case,
562 * the string is compiled to a regex every time the matcher is executed.
563 */
564 sdb_store_matcher_t *
565 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
567 /*
568 * sdb_store_nregex_matcher:
569 * Creates a regex matcher just like sdb_store_regex_matcher except that it
570 * matches in case the regular expression does not match.
571 */
572 sdb_store_matcher_t *
573 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
575 /*
576 * sdb_store_isnull_matcher:
577 * Creates a matcher matching NULL values.
578 */
579 sdb_store_matcher_t *
580 sdb_store_isnull_matcher(sdb_store_expr_t *expr);
582 /*
583 * sdb_store_isnnull_matcher:
584 * Creates a matcher matching non-NULL values.
585 */
586 sdb_store_matcher_t *
587 sdb_store_isnnull_matcher(sdb_store_expr_t *expr);
589 /*
590 * sdb_store_matcher_matches:
591 * Check whether the specified matcher matches the specified store object. If
592 * specified, the filter will be used to preselect objects for further
593 * evaluation. It is applied to any object that's used during the evaluation
594 * of the matcher. Only those objects matching the filter will be considered.
595 *
596 * Note that the filter is applied to all object types (hosts, service,
597 * metric, attribute). Thus, any object-specific matchers are mostly unsuited
598 * for this purpose and, if used, may result in unexpected behavior.
599 *
600 * Returns:
601 * - 1 if the object matches
602 * - 0 else
603 */
604 int
605 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
606 sdb_store_matcher_t *filter);
608 /*
609 * sdb_store_matcher_op_cb:
610 * Callback constructing a matcher operator.
611 */
612 typedef sdb_store_matcher_t *(*sdb_store_matcher_op_cb)
613 (sdb_store_expr_t *, sdb_store_expr_t *);
615 /*
616 * sdb_store_parse_matcher_op:
617 * Parse a matcher operator and return a constructor for the respective
618 * matcher.
619 *
620 * Returns:
621 * - matcher operator constructor on success
622 * - NULL else
623 */
624 sdb_store_matcher_op_cb
625 sdb_store_parse_matcher_op(const char *op);
627 /*
628 * sdb_store_parse_object_type:
629 * Parse the type name of a stored object.
630 *
631 * Returns:
632 * - the object type on success
633 * - a negative value else
634 */
635 int
636 sdb_store_parse_object_type(const char *name);
638 /*
639 * sdb_store_parse_object_type_plural:
640 * Parse the type name (plural) of a stored object.
641 *
642 * Returns:
643 * - the object type on success
644 * - a negative value else
645 */
646 int
647 sdb_store_parse_object_type_plural(const char *name);
649 /*
650 * sdb_store_parse_field_name:
651 * Parse the name of a stored object's queryable field.
652 *
653 * Returns:
654 * - the field id on success
655 * - a negative value else
656 */
657 int
658 sdb_store_parse_field_name(const char *name);
660 /*
661 * sdb_store_lookup_cb:
662 * Lookup callback. It is called for each matching object when looking up data
663 * in the store passing on the lookup filter and the specified user-data. The
664 * lookup aborts early if the callback returns non-zero.
665 */
666 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj,
667 sdb_store_matcher_t *filter, void *user_data);
669 /*
670 * sdb_store_scan:
671 * Look up objects of the specified type in the store. The specified callback
672 * function is called for each object in the store matching 'm'. The function
673 * performs a full scan of all objects stored in the database. If specified,
674 * the filter will be used to preselect objects for further evaluation. See
675 * the description of 'sdb_store_matcher_matches' for details.
676 *
677 * Returns:
678 * - 0 on success
679 * - a negative value else
680 */
681 int
682 sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
683 sdb_store_lookup_cb cb, void *user_data);
685 /*
686 * Flags for JSON formatting.
687 */
688 enum {
689 SDB_WANT_ARRAY = 1 << 0,
690 };
692 /*
693 * sdb_store_json_formatter:
694 * Create a JSON formatter for the specified object types writing to the
695 * specified buffer.
696 */
697 sdb_store_json_formatter_t *
698 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags);
700 /*
701 * sdb_store_json_emit:
702 * Serialize a single object to JSON adding it to the string buffer associated
703 * with the formatter object. The serialized object will not include
704 * attributes or any child objects. Instead, call the function again for each
705 * of those objects. All attributes have to be emitted before any other
706 * children types. Use sdb_store_json_emit_full() to emit a full (filtered)
707 * object.
708 *
709 * Note that the output might not be valid JSON before calling
710 * sdb_store_json_finish().
711 *
712 * Returns:
713 * - 0 on success
714 * - a negative value else
715 */
716 int
717 sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj);
719 /*
720 * sdb_store_json_emit_full:
721 * Serialize a single object including it's attributes and all children to
722 * JSON, adding it to the string buffer associated with the formatter object.
723 * The filter, if specified, is applied to each attribute and child object.
724 * Only matching objects will be included in the output.
725 *
726 * Note that the output might not be valid JSON before calling
727 * sdb_store_json_finish().
728 *
729 * Returns:
730 * - 0 on success
731 * - a negative value else
732 */
733 int
734 sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
735 sdb_store_matcher_t *filter);
737 /*
738 * sdb_store_json_finish:
739 * Finish the JSON output. This function has to be called once after emiting
740 * all objects.
741 */
742 int
743 sdb_store_json_finish(sdb_store_json_formatter_t *f);
745 #ifdef __cplusplus
746 } /* extern "C" */
747 #endif
749 #endif /* ! SDB_CORE_STORE_H */
751 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */