Code

Renamed in-memory store types and functions to sdb_memstore*.
[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         SDB_FIELD_TIMESERIES,    /* metrics only;  type: boolean */
66 };
67 #define SDB_STORE_TYPE_TO_NAME(t) \
68         (((t) == SDB_HOST) ? "host" \
69                 : ((t) == SDB_SERVICE) ? "service" \
70                 : ((t) == SDB_METRIC) ? "metric" \
71                 : ((t) == SDB_ATTRIBUTE) ? "attribute" \
72                 : ((t) == (SDB_ATTRIBUTE | SDB_HOST)) ? "host attribute" \
73                 : ((t) == (SDB_ATTRIBUTE | SDB_SERVICE)) ? "service attribute" \
74                 : ((t) == (SDB_ATTRIBUTE | SDB_METRIC)) ? "metric attribute" \
75                 : "unknown")
77 #define SDB_FIELD_TO_NAME(f) \
78         (((f) == SDB_FIELD_NAME) ? "name" \
79                 : ((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \
80                 : ((f) == SDB_FIELD_AGE) ? "age" \
81                 : ((f) == SDB_FIELD_INTERVAL) ? "interval" \
82                 : ((f) == SDB_FIELD_BACKEND) ? "backend" \
83                 : ((f) == SDB_FIELD_VALUE) ? "value" \
84                 : ((f) == SDB_FIELD_TIMESERIES) ? "timeseries" \
85                 : "unknown")
87 #define SDB_FIELD_TYPE(f) \
88         (((f) == SDB_FIELD_NAME) ? SDB_TYPE_STRING \
89                 : ((f) == SDB_FIELD_LAST_UPDATE) ? SDB_TYPE_DATETIME \
90                 : ((f) == SDB_FIELD_AGE) ? SDB_TYPE_DATETIME \
91                 : ((f) == SDB_FIELD_INTERVAL) ? SDB_TYPE_DATETIME \
92                 : ((f) == SDB_FIELD_BACKEND) ? (SDB_TYPE_ARRAY | SDB_TYPE_STRING) \
93                 : ((f) == SDB_FIELD_VALUE) ? -1 /* unknown */ \
94                 : ((f) == SDB_FIELD_TIMESERIES) ? SDB_TYPE_BOOLEAN \
95                 : -1)
97 /*
98  * sdb_memstore_t represents an in-memory store. It inherits from sdb_object_t
99  * and may safely be case to a generic object.
100  */
101 struct sdb_memstore;
102 typedef struct sdb_memstore sdb_memstore_t;
103 #define SDB_MEMSTORE(obj) ((sdb_memstore_t *)(obj))
105 /*
106  * sdb_memstore_obj_t represents the super-class of any stored object. It
107  * inherits from sdb_object_t and may safely be cast to a generic object to
108  * access its name.
109  */
110 struct sdb_memstore_obj;
111 typedef struct sdb_memstore_obj sdb_memstore_obj_t;
113 /*
114  * sdb_store_host_t represents the meta-data of a stored host object.
115  */
116 typedef struct {
117         const char *name;
119         sdb_time_t last_update;
120         sdb_time_t interval;
121         const char * const *backends;
122         size_t backends_num;
123 } sdb_store_host_t;
124 #define SDB_STORE_HOST_INIT { NULL, 0, 0, NULL, 0 }
126 /*
127  * sdb_store_service_t represents the meta-data of a stored service object.
128  */
129 typedef struct {
130         const char *hostname;
131         const char *name;
133         sdb_time_t last_update;
134         sdb_time_t interval;
135         const char * const *backends;
136         size_t backends_num;
137 } sdb_store_service_t;
138 #define SDB_STORE_SERVICE_INIT { NULL, NULL, 0, 0, NULL, 0 }
140 /*
141  * sdb_metric_store_t specifies how to access a metric's data.
142  */
143 typedef struct {
144         const char *type;
145         const char *id;
146 } sdb_metric_store_t;
148 /*
149  * sdb_store_metric_t represents the meta-data of a stored metric object.
150  */
151 typedef struct {
152         const char *hostname;
153         const char *name;
154         struct {
155                 const char *type;
156                 const char *id;
157         } store;
159         sdb_time_t last_update;
160         sdb_time_t interval;
161         const char * const *backends;
162         size_t backends_num;
163 } sdb_store_metric_t;
164 #define SDB_STORE_METRIC_INIT { NULL, NULL, { NULL, NULL }, 0, 0, NULL, 0 }
166 /*
167  * sdb_store_attribute_t represents a stored attribute.
168  */
169 typedef struct {
170         const char *hostname; /* optional */
171         int parent_type;
172         const char *parent;
173         const char *key;
174         sdb_data_t value;
176         sdb_time_t last_update;
177         sdb_time_t interval;
178         const char * const *backends;
179         size_t backends_num;
180 } sdb_store_attribute_t;
181 #define SDB_STORE_ATTRIBUTE_INIT { NULL, 0, NULL, NULL, SDB_DATA_INIT, 0, 0, NULL, 0 }
183 /*
184  * Expressions represent arithmetic expressions based on stored objects and
185  * their various attributes.
186  *
187  * An expression object inherits from sdb_object_t and, thus, may safely be
188  * cast to a generic object.
189  */
190 struct sdb_memstore_expr;
191 typedef struct sdb_memstore_expr sdb_memstore_expr_t;
192 #define SDB_MEMSTORE_EXPR(obj) ((sdb_memstore_expr_t *)(obj))
194 /*
195  * An expression iterator iterates over the values of an iterable expression.
196  */
197 struct sdb_memstore_expr_iter;
198 typedef struct sdb_memstore_expr_iter sdb_memstore_expr_iter_t;
200 /*
201  * Store matchers may be used to lookup hosts from the store based on their
202  * various attributes. Service and attribute matchers are applied to a host's
203  * services and attributes and evaluate to true if *any* service or attribute
204  * matches.
205  *
206  * A store matcher object inherits from sdb_object_t and, thus, may safely be
207  * cast to a generic object.
208  */
209 struct sdb_memstore_matcher;
210 typedef struct sdb_memstore_matcher sdb_memstore_matcher_t;
211 #define SDB_MEMSTORE_MATCHER(obj) ((sdb_memstore_matcher_t *)(obj))
213 /*
214  * A JSON formatter converts stored objects into the JSON format.
215  * See http://www.ietf.org/rfc/rfc4627.txt
216  *
217  * A JSON formatter object inherits from sdb_object_t and, thus, may safely be
218  * cast to a generic object.
219  */
220 struct sdb_store_json_formatter;
221 typedef struct sdb_store_json_formatter sdb_store_json_formatter_t;
223 /*
224  * A store writer describes the interface for plugins implementing a store.
225  *
226  * Any of the call-back functions shall return:
227  *  - 0 on success
228  *  - a positive value if the new entry is older than the currently stored
229  *    entry (in this case, no update will happen)
230  *  - a negative value on error
231  */
232 typedef struct {
233         /*
234          * store_host:
235          * Add/update a host in the store. If the host, identified by its
236          * canonicalized name, already exists, it will be updated according to the
237          * specified name and timestamp. Else, a new entry will be created in the
238          * store.
239          */
240         int (*store_host)(sdb_store_host_t *host, sdb_object_t *user_data);
242         /*
243          * store_service:
244          * Add/update a service in the store. If the service, identified by its
245          * name, already exists for the specified host, it will be updated
246          * according to the specified name and timestamp. If the referenced host
247          * does not exist, an error will be reported. Else, a new entry will be
248          * created in the store.
249          */
250         int (*store_service)(sdb_store_service_t *service, sdb_object_t *user_data);
252         /*
253          * store_metric:
254          * Add/update a metric in the store. If the metric, identified by its
255          * name, already exists for the specified host, it will be updated
256          * according to the specified attributes. If the referenced host does not
257          * exist, an error will be reported. Else, a new entry will be created in
258          * the store.
259          */
260         int (*store_metric)(sdb_store_metric_t *metric, sdb_object_t *user_data);
262         /*
263          * store_attribute:
264          * Add/update a host's attribute in the store. If the attribute,
265          * identified by its key, already exists for the specified host, it will
266          * be updated to the specified values. If the referenced host does not
267          * exist, an error will be reported. Else, a new entry will be created in
268          * the store.
269          */
270         int (*store_attribute)(sdb_store_attribute_t *attr, sdb_object_t *user_data);
271 } sdb_store_writer_t;
273 /*
274  * sdb_memstore_writer:
275  * A store writer implementation that provides an in-memory object store. It
276  * expects a store object as its user-data argument.
277  */
278 extern sdb_store_writer_t sdb_memstore_writer;
280 /*
281  * A store reader describes the interface to query a store implementation.
282  */
283 typedef struct {
284         /*
285          * prepare_query:
286          * Prepare the query described by 'ast' for execution.
287          */
288         sdb_object_t *(*prepare_query)(sdb_ast_node_t *ast,
289                         sdb_strbuf_t *errbuf, sdb_object_t *user_data);
291         /*
292          * execute_query:
293          * Execute a previously prepared query. The callback may expect that only
294          * queries prepared by its respective prepare callback will be passed to
295          * this function. The query result will be passed back via the specified
296          * store writer.
297          */
298         int (*execute_query)(sdb_object_t *q,
299                         sdb_store_writer_t *w, sdb_object_t *wd,
300                         sdb_strbuf_t *errbuf, sdb_object_t *user_data);
301 } sdb_store_reader_t;
303 /*
304  * sdb_memstore_reader:
305  * A store reader implementation that uses an in-memory object store. It
306  * expects a store object as its user-data argument.
307  */
308 extern sdb_store_reader_t sdb_memstore_reader;
310 /*
311  * sdb_memstore_create:
312  * Allocate a new in-memory store.
313  */
314 sdb_memstore_t *
315 sdb_memstore_create(void);
317 /*
318  * sdb_memstore_host, sdb_memstore_service, sdb_memstore_metric,
319  * sdb_memstore_attribute, sdb_memstore_metric_attr:
320  * Store an object in the specified store. The hostname is expected to be
321  * canonical.
322  */
323 int
324 sdb_memstore_host(sdb_memstore_t *store, const char *name, sdb_time_t last_update);
325 int
326 sdb_memstore_service(sdb_memstore_t *store, const char *hostname, const char *name,
327                 sdb_time_t last_update);
328 int
329 sdb_memstore_metric(sdb_memstore_t *store, const char *hostname, const char *name,
330                 sdb_metric_store_t *metric_store, sdb_time_t last_update);
331 int
332 sdb_memstore_attribute(sdb_memstore_t *store, const char *hostname,
333                 const char *key, const sdb_data_t *value, sdb_time_t last_update);
334 int
335 sdb_memstore_service_attr(sdb_memstore_t *store, const char *hostname,
336                 const char *service, const char *key, const sdb_data_t *value,
337                 sdb_time_t last_update);
338 int
339 sdb_memstore_metric_attr(sdb_memstore_t *store, const char *hostname,
340                 const char *metric, const char *key, const sdb_data_t *value,
341                 sdb_time_t last_update);
343 /*
344  * sdb_memstore_get_host:
345  * Query the specified store for a host by its (canonicalized) name.
346  *
347  * The function increments the ref count of the host object. The caller needs
348  * to deref it when no longer using it.
349  */
350 sdb_memstore_obj_t *
351 sdb_memstore_get_host(sdb_memstore_t *store, const char *name);
353 /*
354  * sdb_memstore_get_child:
355  * Retrieve a host's child object of the specified type and name. The
356  * reference count of the child object will be incremented before returning
357  * it. The caller is responsible for releasing the object once it's no longer
358  * used.
359  *
360  * Returns:
361  *  - the child object on success
362  *  - NULL else
363  */
364 sdb_memstore_obj_t *
365 sdb_memstore_get_child(sdb_memstore_obj_t *host, int type, const char *name);
367 /*
368  * sdb_memstore_get_field:
369  * Get the value of a stored object's queryable field. The caller is
370  * responsible for freeing any dynamically allocated memory possibly stored in
371  * the returned value. If 'res' is NULL, the function will return whether the
372  * field exists.
373  *
374  * Returns:
375  *  - 0 on success
376  *  - a negative value else
377  */
378 int
379 sdb_memstore_get_field(sdb_memstore_obj_t *obj, int field, sdb_data_t *res);
381 /*
382  * sdb_memstore_get_attr:
383  * Get the value of a stored object's attribute. The caller is responsible for
384  * freeing any dynamically allocated memory possibly stored in the returned
385  * value. If 'res' is NULL, the function will return whether the attribute
386  * exists. If specified, only attributes matching the filter will be
387  * considered.
388  *
389  * Returns:
390  *  - 0 if the attribute exists
391  *  - a negative value else
392  */
393 int
394 sdb_memstore_get_attr(sdb_memstore_obj_t *obj, const char *name, sdb_data_t *res,
395                 sdb_memstore_matcher_t *filter);
397 /*
398  * Querying a store:
399  *
400  *  - Query interface: A query is a formal description of an interaction with
401  *    the store. It can be used, both, for read and write access. The query is
402  *    described by its abstract syntax tree (AST). The parser package provides
403  *    means to parse a string (SysQL) representation of the query into an AST.
404  *
405  *  - Matcher / expression interface: This low-level interface provides direct
406  *    control over how to access the store. It is used by the query
407  *    implementation internally and can only be used for read access.
408  */
410 /*
411  * sdb_memstore_query_t:
412  * A parsed query readily prepared for execution.
413  */
414 struct sdb_memstore_query;
415 typedef struct sdb_memstore_query sdb_memstore_query_t;
417 /*
418  * sdb_memstore_query_prepare:
419  * Prepare the query described by 'ast' for execution in a store.
420  *
421  * Returns:
422  *  - a store query on success
423  *  - NULL else
424  */
425 sdb_memstore_query_t *
426 sdb_memstore_query_prepare(sdb_ast_node_t *ast);
428 /*
429  * sdb_memstore_query_prepare_matcher:
430  * Prepare the logical expression described by 'ast' for execution as a store
431  * matcher.
432  *
433  * Returns:
434  *  - a matcher on success
435  *  - NULL else
436  */
437 sdb_memstore_matcher_t *
438 sdb_memstore_query_prepare_matcher(sdb_ast_node_t *ast);
440 /*
441  * sdb_memstore_query_execute:
442  * Execute a previously prepared query in the specified store. The query
443  * result will be written to 'buf' and any errors to 'errbuf'.
444  *
445  * Returns:
446  *  - the result type (to be used by the server reply)
447  *  - a negative value on error
448  */
449 int
450 sdb_memstore_query_execute(sdb_memstore_t *store, sdb_memstore_query_t *m,
451                 sdb_store_writer_t *w, sdb_object_t *wd, sdb_strbuf_t *errbuf);
453 /*
454  * sdb_memstore_expr_create:
455  * Creates an arithmetic expression implementing the specified operator on the
456  * specified left and right operand.
457  *
458  * Returns:
459  *  - an expression object on success
460  *  - NULL else
461  */
462 sdb_memstore_expr_t *
463 sdb_memstore_expr_create(int op,
464                 sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
466 /*
467  * sdb_memstore_expr_typed:
468  * Creates an expression which evaluates in the context of an object's sibling
469  * as specified by the given type.
470  *
471  * Returns:
472  *  - an expression object on success
473  *  - NULL else
474  */
475 sdb_memstore_expr_t *
476 sdb_memstore_expr_typed(int typ, sdb_memstore_expr_t *expr);
478 /*
479  * sdb_memstore_expr_fieldvalue:
480  * Creates an expression which evaluates to the value of the specified
481  * queryable field of a stored object.
482  *
483  * Returns:
484  *  - an expression object on success
485  *  - NULL else
486  */
487 sdb_memstore_expr_t *
488 sdb_memstore_expr_fieldvalue(int field);
490 /*
491  * sdb_memstore_expr_attrvalue:
492  * Creates an expression which evaluates to the value of the specified
493  * attribute of a stored object. Evaluates to a NULL value if the attribute
494  * does not exist.
495  *
496  * Returns:
497  *  - an expression object on success
498  *  - NULL else
499  */
500 sdb_memstore_expr_t *
501 sdb_memstore_expr_attrvalue(const char *name);
503 /*
504  * sdb_memstore_expr_constvalue:
505  * Creates an expression which evaluates to the specified constant value.
506  *
507  * Returns:
508  *  - an expression object on success
509  *  - NULL else
510  */
511 sdb_memstore_expr_t *
512 sdb_memstore_expr_constvalue(const sdb_data_t *value);
514 /*
515  * sdb_memstore_expr_eval:
516  * Evaluate an expression for the specified stored object and stores the
517  * result in 'res'. The result's value will be allocated dynamically if
518  * necessary and, thus, should be free'd by the caller (e.g. using
519  * sdb_data_free_datum). The object may be NULL, in which case the expression
520  * needs to evaluate to a constant value. If specified, only objects matching
521  * the filter will be used during the evaluation.
522  *
523  * Returns:
524  *  - 0 on success
525  *  - a negative value else
526  */
527 int
528 sdb_memstore_expr_eval(sdb_memstore_expr_t *expr, sdb_memstore_obj_t *obj,
529                 sdb_data_t *res, sdb_memstore_matcher_t *filter);
531 /*
532  * sdb_memstore_expr_iter:
533  * Iterate over the elements of an iterable expression. sdb_memstore_expr_iter
534  * returns NULL if the expression is not iterable (for the specified object).
535  *
536  * sdb_memstore_expr_iter_get_next returns NULL if there is no next element.
537  */
538 sdb_memstore_expr_iter_t *
539 sdb_memstore_expr_iter(sdb_memstore_expr_t *expr, sdb_memstore_obj_t *obj,
540                 sdb_memstore_matcher_t *filter);
541 void
542 sdb_memstore_expr_iter_destroy(sdb_memstore_expr_iter_t *iter);
544 bool
545 sdb_memstore_expr_iter_has_next(sdb_memstore_expr_iter_t *iter);
546 sdb_data_t
547 sdb_memstore_expr_iter_get_next(sdb_memstore_expr_iter_t *iter);
549 /*
550  * sdb_memstore_dis_matcher:
551  * Creates a matcher matching the disjunction (logical OR) of two matchers.
552  */
553 sdb_memstore_matcher_t *
554 sdb_memstore_dis_matcher(sdb_memstore_matcher_t *left, sdb_memstore_matcher_t *right);
556 /*
557  * sdb_memstore_con_matcher:
558  * Creates a matcher matching the conjunction (logical AND) of two matchers.
559  */
560 sdb_memstore_matcher_t *
561 sdb_memstore_con_matcher(sdb_memstore_matcher_t *left, sdb_memstore_matcher_t *right);
563 /*
564  * sdb_memstore_inv_matcher:
565  * Creates a matcher matching the inverse (logical NOT) of a matcher.
566  */
567 sdb_memstore_matcher_t *
568 sdb_memstore_inv_matcher(sdb_memstore_matcher_t *m);
570 /*
571  * sdb_memstore_any_matcher:
572  * Creates a matcher iterating over values of the first expression (which has
573  * to be iterable). It matches if *any* of those elements match 'm'. 'm' has
574  * to be an ary operation with the left operand unset.
575  */
576 sdb_memstore_matcher_t *
577 sdb_memstore_any_matcher(sdb_memstore_expr_t *iter, sdb_memstore_matcher_t *m);
579 /*
580  * sdb_memstore_all_matcher:
581  * Creates a matcher iterating over values of the first expression (which has
582  * to be iterable). It matches if *all* of those elements match 'm'. 'm' has
583  * to be an ary operation with the left operand unset.
584  */
585 sdb_memstore_matcher_t *
586 sdb_memstore_all_matcher(sdb_memstore_expr_t *iter, sdb_memstore_matcher_t *m);
588 /*
589  * sdb_memstore_in_matcher:
590  * Creates a matcher which matches if the right value evaluates to an array
591  * value and the left value is included in that array. See sdb_data_inarray
592  * for more details.
593  */
594 sdb_memstore_matcher_t *
595 sdb_memstore_in_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
597 /*
598  * sdb_memstore_lt_matcher, sdb_memstore_le_matcher, sdb_memstore_eq_matcher,
599  * sdb_memstore_ge_matcher, sdb_memstore_gt_matcher:
600  * Create conditional matchers comparing the values of two expressions. The
601  * matcher matches if the left expression compres less than, less or equal
602  * than, equal to, not equal to, greater or equal than, or greater than the
603  * right expression.
604  */
605 sdb_memstore_matcher_t *
606 sdb_memstore_lt_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
607 sdb_memstore_matcher_t *
608 sdb_memstore_le_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
609 sdb_memstore_matcher_t *
610 sdb_memstore_eq_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
611 sdb_memstore_matcher_t *
612 sdb_memstore_ne_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
613 sdb_memstore_matcher_t *
614 sdb_memstore_ge_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
615 sdb_memstore_matcher_t *
616 sdb_memstore_gt_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
618 /*
619  * sdb_memstore_regex_matcher:
620  * Creates a matcher which matches the string value the left expression
621  * evaluates to against the regular expression the right expression evaluates
622  * to. The right expression may either be a constant value regular expression
623  * or string or a dynamic value evaluating to a string. In the latter case,
624  * the string is compiled to a regex every time the matcher is executed.
625  */
626 sdb_memstore_matcher_t *
627 sdb_memstore_regex_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
629 /*
630  * sdb_memstore_nregex_matcher:
631  * Creates a regex matcher just like sdb_memstore_regex_matcher except that it
632  * matches in case the regular expression does not match.
633  */
634 sdb_memstore_matcher_t *
635 sdb_memstore_nregex_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
637 /*
638  * sdb_memstore_isnull_matcher:
639  * Creates a matcher matching NULL values.
640  */
641 sdb_memstore_matcher_t *
642 sdb_memstore_isnull_matcher(sdb_memstore_expr_t *expr);
644 /*
645  * sdb_memstore_istrue_matcher, sdb_memstore_isfalse_matcher:
646  * Creates a matcher matching boolean values.
647  */
648 sdb_memstore_matcher_t *
649 sdb_memstore_istrue_matcher(sdb_memstore_expr_t *expr);
650 sdb_memstore_matcher_t *
651 sdb_memstore_isfalse_matcher(sdb_memstore_expr_t *expr);
653 /*
654  * sdb_memstore_matcher_matches:
655  * Check whether the specified matcher matches the specified store object. If
656  * specified, the filter will be used to preselect objects for further
657  * evaluation. It is applied to any object that's used during the evaluation
658  * of the matcher. Only those objects matching the filter will be considered.
659  *
660  * Note that the filter is applied to all object types (hosts, service,
661  * metric, attribute). Thus, any object-specific matchers are mostly unsuited
662  * for this purpose and, if used, may result in unexpected behavior.
663  *
664  * Returns:
665  *  - 1 if the object matches
666  *  - 0 else
667  */
668 int
669 sdb_memstore_matcher_matches(sdb_memstore_matcher_t *m, sdb_memstore_obj_t *obj,
670                 sdb_memstore_matcher_t *filter);
672 /*
673  * sdb_memstore_matcher_op_cb:
674  * Callback constructing a matcher operator.
675  */
676 typedef sdb_memstore_matcher_t *(*sdb_memstore_matcher_op_cb)
677         (sdb_memstore_expr_t *, sdb_memstore_expr_t *);
679 /*
680  * sdb_memstore_lookup_cb:
681  * Lookup callback. It is called for each matching object when looking up data
682  * in the store passing on the lookup filter and the specified user-data. The
683  * lookup aborts early if the callback returns non-zero.
684  */
685 typedef int (*sdb_memstore_lookup_cb)(sdb_memstore_obj_t *obj,
686                 sdb_memstore_matcher_t *filter, void *user_data);
688 /*
689  * sdb_memstore_scan:
690  * Look up objects of the specified type in the specified store. The specified
691  * callback function is called for each object in the store matching 'm'. The
692  * function performs a full scan of all stored objects. If specified, the
693  * filter will be used to preselect objects for further evaluation. See the
694  * description of 'sdb_memstore_matcher_matches' for details.
695  *
696  * Returns:
697  *  - 0 on success
698  *  - a negative value else
699  */
700 int
701 sdb_memstore_scan(sdb_memstore_t *store, int type,
702                 sdb_memstore_matcher_t *m, sdb_memstore_matcher_t *filter,
703                 sdb_memstore_lookup_cb cb, void *user_data);
705 /*
706  * Flags for JSON formatting.
707  */
708 enum {
709         SDB_WANT_ARRAY = 1 << 0,
710 };
712 /*
713  * sdb_store_json_formatter:
714  * Create a JSON formatter for the specified object types writing to the
715  * specified buffer.
716  */
717 sdb_store_json_formatter_t *
718 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags);
720 /*
721  * sdb_memstore_emit:
722  * Serialize a single object to JSON adding it to the string buffer associated
723  * with the formatter object. The serialized object will not include
724  * attributes or any child objects. Instead, call the function again for each
725  * of those objects. All attributes have to be emitted before any other
726  * children types. Use sdb_memstore_emit_full() to emit a full (filtered) object.
727  *
728  * Note that the output might not be valid JSON before calling
729  * sdb_store_json_finish().
730  *
731  * Returns:
732  *  - 0 on success
733  *  - a negative value else
734  */
735 int
736 sdb_memstore_emit(sdb_memstore_obj_t *obj, sdb_store_writer_t *w, sdb_object_t *wd);
738 /*
739  * sdb_memstore_emit_full:
740  * Serialize a single object including it's attributes and all children to
741  * JSON, adding it to the string buffer associated with the formatter object.
742  * The filter, if specified, is applied to each attribute and child object.
743  * Only matching objects will be included in the output.
744  *
745  * Note that the output might not be valid JSON before calling
746  * sdb_store_json_finish().
747  *
748  * Returns:
749  *  - 0 on success
750  *  - a negative value else
751  */
752 int
753 sdb_memstore_emit_full(sdb_memstore_obj_t *obj, sdb_memstore_matcher_t *filter,
754                 sdb_store_writer_t *w, sdb_object_t *wd);
756 /*
757  * sdb_store_json_finish:
758  * Finish the JSON output. This function has to be called once after emiting
759  * all objects.
760  */
761 int
762 sdb_store_json_finish(sdb_store_json_formatter_t *f);
764 /*
765  * sdb_store_json_writer:
766  * A store writer implementation that generates JSON output. It expects a
767  * store JSON formatter as its user-data argument.
768  */
769 extern sdb_store_writer_t sdb_store_json_writer;
771 #ifdef __cplusplus
772 } /* extern "C" */
773 #endif
775 #endif /* ! SDB_CORE_STORE_H */
777 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */