Code

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