Code

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