Code

3722a7262d6aa773d4f3210e12217e7505d449f1
[sysdb.git] / src / include / core / store.h
1 /*
2  * SysDB - src/include/core/store.h
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #ifndef SDB_CORE_STORE_H
29 #define SDB_CORE_STORE_H 1
31 #include "sysdb.h"
32 #include "core/object.h"
33 #include "core/data.h"
34 #include "core/time.h"
35 #include "core/timeseries.h"
36 #include "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 };
64 #define SDB_STORE_TYPE_TO_NAME(t) \
65         (((t) == SDB_HOST) ? "host" \
66                 : ((t) == SDB_SERVICE) ? "service" \
67                 : ((t) == SDB_METRIC) ? "metric" \
68                 : ((t) == SDB_ATTRIBUTE) ? "attribute" \
69                 : ((t) == (SDB_ATTRIBUTE | SDB_HOST)) ? "host attribute" \
70                 : ((t) == (SDB_ATTRIBUTE | SDB_SERVICE)) ? "service attribute" \
71                 : ((t) == (SDB_ATTRIBUTE | SDB_METRIC)) ? "metric attribute" \
72                 : "unknown")
74 #define SDB_FIELD_TO_NAME(f) \
75         (((f) == SDB_FIELD_NAME) ? "name" \
76                 : ((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \
77                 : ((f) == SDB_FIELD_AGE) ? "age" \
78                 : ((f) == SDB_FIELD_INTERVAL) ? "interval" \
79                 : ((f) == SDB_FIELD_BACKEND) ? "backend" : "unknown")
81 #define SDB_FIELD_TYPE(f) \
82         (((f) == SDB_FIELD_NAME) ? SDB_TYPE_STRING \
83                 : ((f) == SDB_FIELD_LAST_UPDATE) ? SDB_TYPE_DATETIME \
84                 : ((f) == SDB_FIELD_AGE) ? SDB_TYPE_DATETIME \
85                 : ((f) == SDB_FIELD_INTERVAL) ? SDB_TYPE_DATETIME \
86                 : ((f) == SDB_FIELD_BACKEND) ? (SDB_TYPE_ARRAY | SDB_TYPE_STRING) \
87                 : -1)
89 /*
90  * sdb_store_obj_t represents the super-class of any object stored in the
91  * database. It inherits from sdb_object_t and may safely be cast to a generic
92  * object to access its name.
93  */
94 struct sdb_store_obj;
95 typedef struct sdb_store_obj sdb_store_obj_t;
97 /*
98  * A metric store describes how to access a metric's data.
99  */
100 typedef struct {
101         const char *type;
102         const char *id;
103 } sdb_metric_store_t;
105 /*
106  * Expressions represent arithmetic expressions based on stored objects and
107  * their various attributes.
108  *
109  * An expression object inherits from sdb_object_t and, thus, may safely be
110  * cast to a generic object.
111  */
112 struct sdb_store_expr;
113 typedef struct sdb_store_expr sdb_store_expr_t;
114 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
116 /*
117  * An expression iterator iterates over the values of an iterable expression
118  * (see sdb_store_expr_iterable).
119  */
120 struct sdb_store_expr_iter;
121 typedef struct sdb_store_expr_iter sdb_store_expr_iter_t;
123 /*
124  * Store matchers may be used to lookup hosts from the store based on their
125  * various attributes. Service and attribute matchers are applied to a host's
126  * services and attributes and evaluate to true if *any* service or attribute
127  * matches.
128  *
129  * A store matcher object inherits from sdb_object_t and, thus, may safely be
130  * cast to a generic object.
131  */
132 struct sdb_store_matcher;
133 typedef struct sdb_store_matcher sdb_store_matcher_t;
134 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
136 /*
137  * A JSON formatter converts stored objects into the JSON format.
138  * See http://www.ietf.org/rfc/rfc4627.txt
139  */
140 struct sdb_store_json_formatter;
141 typedef struct sdb_store_json_formatter sdb_store_json_formatter_t;
143 /*
144  * A store writer describes the interface for plugins implementing a store.
145  */
146 typedef struct {
147         int (*store_host)(const char *name, sdb_time_t last_update,
148                         sdb_object_t *user_data);
149         int (*store_service)(const char *hostname, const char *name,
150                         sdb_time_t last_update, sdb_object_t *user_data);
151         int (*store_metric)(const char *hostname, const char *name,
152                         sdb_metric_store_t *store, sdb_time_t last_update,
153                         sdb_object_t *user_data);
154         int (*store_attribute)(const char *hostname,
155                         const char *key, const sdb_data_t *value, sdb_time_t last_update,
156                         sdb_object_t *user_data);
157         int (*store_service_attr)(const char *hostname, const char *service,
158                         const char *key, const sdb_data_t *value, sdb_time_t last_update,
159                         sdb_object_t *user_data);
160         int (*store_metric_attr)(const char *hostname, const char *metric,
161                         const char *key, const sdb_data_t *value, sdb_time_t last_update,
162                         sdb_object_t *user_data);
163 } sdb_store_writer_t;
165 /*
166  * sdb_store_clear:
167  * Clear the entire store and remove all stored objects.
168  */
169 void
170 sdb_store_clear(void);
172 /*
173  * sdb_store_host:
174  * Add/update a host in the store. If the host, identified by its
175  * canonicalized name, already exists, it will be updated according to the
176  * specified name and timestamp. Else, a new entry will be created in the
177  * store. Any memory required for storing the entry will be allocated an
178  * managed by the store itself.
179  *
180  * Returns:
181  *  - 0 on success
182  *  - a positive value if the new entry is older than the currently stored
183  *    entry (in this case, no update will happen)
184  *  - a negative value on error
185  */
186 int
187 sdb_store_host(const char *name, sdb_time_t last_update);
189 /*
190  * sdb_store_has_host:
191  * sdb_store_get_host:
192  * Query the store for a host by its (canonicalized) name.
193  *
194  * sdb_store_get_host increments the ref count of the host object. The caller
195  * needs to deref it when no longer using it.
196  */
197 bool
198 sdb_store_has_host(const char *name);
200 sdb_store_obj_t *
201 sdb_store_get_host(const char *name);
203 /*
204  * sdb_store_attribute:
205  * Add/update a host's attribute in the store. If the attribute, identified by
206  * its key, already exists for the specified host, it will be updated to the
207  * specified values. If the referenced host does not exist, an error will be
208  * reported. Else, a new entry will be created in the store. Any memory
209  * required for storing the entry will be allocated and managed by the store
210  * itself.
211  *
212  * Returns:
213  *  - 0 on success
214  *  - a positive value if the new entry is older than the currently stored
215  *    entry (in this case, no update will happen)
216  *  - a negative value on error
217  */
218 int
219 sdb_store_attribute(const char *hostname,
220                 const char *key, const sdb_data_t *value,
221                 sdb_time_t last_update);
223 /*
224  * sdb_store_service:
225  * Add/update a service in the store. If the service, identified by its name,
226  * already exists for the specified host, it will be updated according to the
227  * specified 'service' object. If the referenced host does not exist, an error
228  * will be reported. Else, a new entry will be created in the store. Any
229  * memory required for storing the entry will be allocated an managed by the
230  * store itself.
231  *
232  * Returns:
233  *  - 0 on success
234  *  - a positive value if the new entry is older than the currently stored
235  *    entry (in this case, no update will happen)
236  *  - a negative value on error
237  */
238 int
239 sdb_store_service(const char *hostname, const char *name,
240                 sdb_time_t last_update);
242 /*
243  * sdb_store_service_attr:
244  * Add/update a service's attribute in the store. If the attribute, identified
245  * by its key, already exists for the specified service, it will be updated to
246  * the specified value. If the references service (for the specified host)
247  * does not exist, an error will be reported. Any memory required for storing
248  * the entry will be allocated and managed by the store itself.
249  *
250  * Returns:
251  *  - 0 on success
252  *  - a positive value if the new entry is older than the currently stored
253  *    entry (in this case, no update will happen)
254  *  - a negative value on error
255  */
256 int
257 sdb_store_service_attr(const char *hostname, const char *service,
258                 const char *key, const sdb_data_t *value, sdb_time_t last_update);
260 /*
261  * sdb_store_metric:
262  * Add/update a metric in the store. If the metric, identified by its name,
263  * already exists for the specified host, it will be updated according to the
264  * specified 'metric' object. If the referenced host does not exist, an error
265  * will be reported. Else, a new entry will be created in the store. Any
266  * memory required for storing the entry will be allocated an managed by the
267  * store itself.
268  *
269  * If specified, the metric store describes where to access the metric's data.
270  *
271  * Returns:
272  *  - 0 on success
273  *  - a positive value if the new entry is older than the currently stored
274  *    entry (in this case, no update will happen)
275  *  - a negative value on error
276  */
277 int
278 sdb_store_metric(const char *hostname, const char *name,
279                 sdb_metric_store_t *store, sdb_time_t last_update);
281 /*
282  * sdb_store_metric_attr:
283  * Add/update a metric's attribute in the store. If the attribute, identified
284  * by its key, already exists for the specified metric, it will be updated to
285  * the specified value. If the references metric (for the specified host)
286  * does not exist, an error will be reported. Any memory required for storing
287  * the entry will be allocated and managed by the store itself.
288  *
289  * Returns:
290  *  - 0 on success
291  *  - a positive value if the new entry is older than the currently stored
292  *    entry (in this case, no update will happen)
293  *  - a negative value on error
294  */
295 int
296 sdb_store_metric_attr(const char *hostname, const char *metric,
297                 const char *key, const sdb_data_t *value, sdb_time_t last_update);
299 /*
300  * sdb_store_fetch_timeseries:
301  * Fetch the time-series described by the specified host's metric and
302  * serialize it as JSON into the provided string buffer.
303  *
304  * Returns:
305  *  - 0 on success
306  *  - a negative value else
307  */
308 int
309 sdb_store_fetch_timeseries(const char *hostname, const char *metric,
310                 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf);
312 /*
313  * sdb_store_get_child:
314  * Retrieve a host's child object of the specified type and name. The
315  * reference count of the child object will be incremented before returning
316  * it. The caller is responsible for releasing the object once it's no longer
317  * used.
318  *
319  * Returns:
320  *  - the child object on success
321  *  - NULL else
322  */
323 sdb_store_obj_t *
324 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name);
326 /*
327  * sdb_store_get_field:
328  * Get the value of a stored object's queryable field. The caller is
329  * responsible for freeing any dynamically allocated memory possibly stored in
330  * the returned value. If 'res' is NULL, the function will return whether the
331  * field exists.
332  *
333  * Note: Retrieving the backend this way is not currently supported.
334  *
335  * Returns:
336  *  - 0 on success
337  *  - a negative value else
338  */
339 int
340 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
342 /*
343  * sdb_store_get_attr:
344  * Get the value of a stored object's attribute. The caller is responsible for
345  * freeing any dynamically allocated memory possibly stored in the returned
346  * value. If 'res' is NULL, the function will return whether the attribute
347  * exists. If specified, only attributes matching the filter will be
348  * considered.
349  *
350  * Returns:
351  *  - 0 if the attribute exists
352  *  - a negative value else
353  */
354 int
355 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
356                 sdb_store_matcher_t *filter);
358 /*
359  * sdb_store_expr_create:
360  * Creates an arithmetic expression implementing the specified operator on the
361  * specified left and right operand.
362  *
363  * Returns:
364  *  - an expression object on success
365  *  - NULL else
366  */
367 sdb_store_expr_t *
368 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
370 /*
371  * sdb_store_expr_typed:
372  * Creates an expression which evaluates in the context of an object's sibling
373  * as specified by the given type.
374  *
375  * Returns:
376  *  - an expression object on success
377  *  - NULL else
378  */
379 sdb_store_expr_t *
380 sdb_store_expr_typed(int typ, sdb_store_expr_t *expr);
382 /*
383  * sdb_store_expr_fieldvalue:
384  * Creates an expression which evaluates to the value of the specified
385  * queryable field of a stored object.
386  *
387  * Returns:
388  *  - an expression object on success
389  *  - NULL else
390  */
391 sdb_store_expr_t *
392 sdb_store_expr_fieldvalue(int field);
394 /*
395  * sdb_store_expr_attrvalue:
396  * Creates an expression which evaluates to the value of the specified
397  * attribute of a stored object. Evaluates to a NULL value if the attribute
398  * does not exist.
399  *
400  * Returns:
401  *  - an expression object on success
402  *  - NULL else
403  */
404 sdb_store_expr_t *
405 sdb_store_expr_attrvalue(const char *name);
407 /*
408  * sdb_store_expr_constvalue:
409  * Creates an expression which evaluates to the specified constant value.
410  *
411  * Returns:
412  *  - an expression object on success
413  *  - NULL else
414  */
415 sdb_store_expr_t *
416 sdb_store_expr_constvalue(const sdb_data_t *value);
418 /*
419  * sdb_store_expr_eval:
420  * Evaluate an expression for the specified stored object and stores the
421  * result in 'res'. The result's value will be allocated dynamically if
422  * necessary and, thus, should be free'd by the caller (e.g. using
423  * sdb_data_free_datum). The object may be NULL, in which case the expression
424  * needs to evaluate to a constant value. If specified, only objects matching
425  * the filter will be used during the evaluation.
426  *
427  * Returns:
428  *  - 0 on success
429  *  - a negative value else
430  */
431 int
432 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
433                 sdb_data_t *res, sdb_store_matcher_t *filter);
435 /*
436  * sdb_store_expr_iterable:
437  * Check whether an expression, evaluated in the specified context (HOST,
438  * SERVICE, METRIC) is iterable, that is, if it may evaluate to multiple
439  * values.
440  */
441 bool
442 sdb_store_expr_iterable(sdb_store_expr_t *expr, int context);
444 /*
445  * sdb_store_expr_iter:
446  * Iterate over the elements of an iterable expression. sdb_store_expr_iter
447  * returns NULL if the expression is not iterable (for the specified object).
448  * See also sdb_store_expr_iterable.
449  *
450  * sdb_store_expr_iter_get_next returns NULL if there is no next element.
451  */
452 sdb_store_expr_iter_t *
453 sdb_store_expr_iter(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
454                 sdb_store_matcher_t *filter);
455 void
456 sdb_store_expr_iter_destroy(sdb_store_expr_iter_t *iter);
458 bool
459 sdb_store_expr_iter_has_next(sdb_store_expr_iter_t *iter);
460 sdb_data_t
461 sdb_store_expr_iter_get_next(sdb_store_expr_iter_t *iter);
463 /*
464  * sdb_store_dis_matcher:
465  * Creates a matcher matching the disjunction (logical OR) of two matchers.
466  */
467 sdb_store_matcher_t *
468 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
470 /*
471  * sdb_store_con_matcher:
472  * Creates a matcher matching the conjunction (logical AND) of two matchers.
473  */
474 sdb_store_matcher_t *
475 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
477 /*
478  * sdb_store_inv_matcher::
479  * Creates a matcher matching the inverse (logical NOT) of a matcher.
480  */
481 sdb_store_matcher_t *
482 sdb_store_inv_matcher(sdb_store_matcher_t *m);
484 /*
485  * sdb_store_any_matcher:
486  * Creates a matcher iterating over values of the first expression (which has
487  * to be iterable). It matches if *any* of those elements match 'm'. 'm' has
488  * to be an ary operation with the left operand unset.
489  */
490 sdb_store_matcher_t *
491 sdb_store_any_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m);
493 /*
494  * sdb_store_all_matcher:
495  * Creates a matcher iterating over values of the first expression (which has
496  * to be iterable). It matches if *all* of those elements match 'm'. 'm' has
497  * to be an ary operation with the left operand unset.
498  */
499 sdb_store_matcher_t *
500 sdb_store_all_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m);
502 /*
503  * sdb_store_in_matcher:
504  * Creates a matcher which matches if the right value evaluates to an array
505  * value and the left value is included in that array. See sdb_data_inarray
506  * for more details.
507  */
508 sdb_store_matcher_t *
509 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
511 /*
512  * sdb_store_nin_matcher:
513  * Like sdb_store_in_matcher but matches if the left value is not included in
514  * the right value.
515  */
516 sdb_store_matcher_t *
517 sdb_store_nin_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
519 /*
520  * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
521  * sdb_store_ge_matcher, sdb_store_gt_matcher:
522  * Create conditional matchers comparing the values of two expressions. The
523  * matcher matches if the left expression compres less than, less or equal
524  * than, equal to, not equal to, greater or equal than, or greater than the
525  * right expression.
526  */
527 sdb_store_matcher_t *
528 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
529 sdb_store_matcher_t *
530 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
531 sdb_store_matcher_t *
532 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
533 sdb_store_matcher_t *
534 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
535 sdb_store_matcher_t *
536 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
537 sdb_store_matcher_t *
538 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
540 /*
541  * sdb_store_regex_matcher:
542  * Creates a matcher which matches the string value the left expression
543  * evaluates to against the regular expression the right expression evaluates
544  * to. The right expression may either be a constant value regular expression
545  * or string or a dynamic value evaluating to a string. In the latter case,
546  * the string is compiled to a regex every time the matcher is executed.
547  */
548 sdb_store_matcher_t *
549 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
551 /*
552  * sdb_store_nregex_matcher:
553  * Creates a regex matcher just like sdb_store_regex_matcher except that it
554  * matches in case the regular expression does not match.
555  */
556 sdb_store_matcher_t *
557 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
559 /*
560  * sdb_store_isnull_matcher:
561  * Creates a matcher matching NULL values.
562  */
563 sdb_store_matcher_t *
564 sdb_store_isnull_matcher(sdb_store_expr_t *expr);
566 /*
567  * sdb_store_isnnull_matcher:
568  * Creates a matcher matching non-NULL values.
569  */
570 sdb_store_matcher_t *
571 sdb_store_isnnull_matcher(sdb_store_expr_t *expr);
573 /*
574  * sdb_store_matcher_matches:
575  * Check whether the specified matcher matches the specified store object. If
576  * specified, the filter will be used to preselect objects for further
577  * evaluation. It is applied to any object that's used during the evaluation
578  * of the matcher. Only those objects matching the filter will be considered.
579  *
580  * Note that the filter is applied to all object types (hosts, service,
581  * metric, attribute). Thus, any object-specific matchers are mostly unsuited
582  * for this purpose and, if used, may result in unexpected behavior.
583  *
584  * Returns:
585  *  - 1 if the object matches
586  *  - 0 else
587  */
588 int
589 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
590                 sdb_store_matcher_t *filter);
592 /*
593  * sdb_store_matcher_op_cb:
594  * Callback constructing a matcher operator.
595  */
596 typedef sdb_store_matcher_t *(*sdb_store_matcher_op_cb)
597         (sdb_store_expr_t *, sdb_store_expr_t *);
599 /*
600  * sdb_store_parse_matcher_op:
601  * Parse a matcher operator and return a constructor for the respective
602  * matcher.
603  *
604  * Returns:
605  *  - matcher operator constructor on success
606  *  - NULL else
607  */
608 sdb_store_matcher_op_cb
609 sdb_store_parse_matcher_op(const char *op);
611 /*
612  * sdb_store_parse_object_type:
613  * Parse the type name of a stored object.
614  *
615  * Returns:
616  *  - the object type on success
617  *  - a negative value else
618  */
619 int
620 sdb_store_parse_object_type(const char *name);
622 /*
623  * sdb_store_parse_object_type_plural:
624  * Parse the type name (plural) of a stored object.
625  *
626  * Returns:
627  *  - the object type on success
628  *  - a negative value else
629  */
630 int
631 sdb_store_parse_object_type_plural(const char *name);
633 /*
634  * sdb_store_parse_field_name:
635  * Parse the name of a stored object's queryable field.
636  *
637  * Returns:
638  *  - the field id on success
639  *  - a negative value else
640  */
641 int
642 sdb_store_parse_field_name(const char *name);
644 /*
645  * sdb_store_lookup_cb:
646  * Lookup callback. It is called for each matching object when looking up data
647  * in the store passing on the lookup filter and the specified user-data. The
648  * lookup aborts early if the callback returns non-zero.
649  */
650 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj,
651                 sdb_store_matcher_t *filter, void *user_data);
653 /*
654  * sdb_store_scan:
655  * Look up objects of the specified type in the store. The specified callback
656  * function is called for each object in the store matching 'm'. The function
657  * performs a full scan of all objects stored in the database. If specified,
658  * the filter will be used to preselect objects for further evaluation. See
659  * the description of 'sdb_store_matcher_matches' for details.
660  *
661  * Returns:
662  *  - 0 on success
663  *  - a negative value else
664  */
665 int
666 sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
667                 sdb_store_lookup_cb cb, void *user_data);
669 /*
670  * Flags for JSON formatting.
671  */
672 enum {
673         SDB_WANT_ARRAY = 1 << 0,
674 };
676 /*
677  * sdb_store_json_formatter:
678  * Create a JSON formatter for the specified object types writing to the
679  * specified buffer.
680  */
681 sdb_store_json_formatter_t *
682 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags);
684 /*
685  * sdb_store_json_emit:
686  * Serialize a single object to JSON adding it to the string buffer associated
687  * with the formatter object. The serialized object will not include
688  * attributes or any child objects. Instead, call the function again for each
689  * of those objects. All attributes have to be emitted before any other
690  * children types. Use sdb_store_json_emit_full() to emit a full (filtered)
691  * object.
692  *
693  * Note that the output might not be valid JSON before calling
694  * sdb_store_json_finish().
695  *
696  * Returns:
697  *  - 0 on success
698  *  - a negative value else
699  */
700 int
701 sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj);
703 /*
704  * sdb_store_json_emit_full:
705  * Serialize a single object including it's attributes and all children to
706  * JSON, adding it to the string buffer associated with the formatter object.
707  * The filter, if specified, is applied to each attribute and child object.
708  * Only matching objects will be included in the output.
709  *
710  * Note that the output might not be valid JSON before calling
711  * sdb_store_json_finish().
712  *
713  * Returns:
714  *  - 0 on success
715  *  - a negative value else
716  */
717 int
718 sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
719                 sdb_store_matcher_t *filter);
721 /*
722  * sdb_store_json_finish:
723  * Finish the JSON output. This function has to be called once after emiting
724  * all objects.
725  */
726 int
727 sdb_store_json_finish(sdb_store_json_formatter_t *f);
729 #ifdef __cplusplus
730 } /* extern "C" */
731 #endif
733 #endif /* ! SDB_CORE_STORE_H */
735 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */