1 /*
2 * SysDB - src/include/core/memstore.h
3 * Copyright (C) 2012-2015 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_MEMSTORE_H
29 #define SDB_CORE_MEMSTORE_H 1
31 #include "sysdb.h"
32 #include "core/object.h"
33 #include "core/data.h"
34 #include "core/store.h"
35 #include "core/time.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 * sdb_memstore_t represents an in-memory store. It inherits from sdb_object_t
48 * and may safely be case to a generic object.
49 */
50 struct sdb_memstore;
51 typedef struct sdb_memstore sdb_memstore_t;
52 #define SDB_MEMSTORE(obj) ((sdb_memstore_t *)(obj))
54 /*
55 * sdb_memstore_obj_t represents the super-class of any stored object. It
56 * inherits from sdb_object_t and may safely be cast to a generic object to
57 * access its name.
58 */
59 struct sdb_memstore_obj;
60 typedef struct sdb_memstore_obj sdb_memstore_obj_t;
62 /*
63 * Expressions represent arithmetic expressions based on stored objects and
64 * their various attributes.
65 *
66 * An expression object inherits from sdb_object_t and, thus, may safely be
67 * cast to a generic object.
68 */
69 struct sdb_memstore_expr;
70 typedef struct sdb_memstore_expr sdb_memstore_expr_t;
71 #define SDB_MEMSTORE_EXPR(obj) ((sdb_memstore_expr_t *)(obj))
73 /*
74 * An expression iterator iterates over the values of an iterable expression.
75 */
76 struct sdb_memstore_expr_iter;
77 typedef struct sdb_memstore_expr_iter sdb_memstore_expr_iter_t;
79 /*
80 * Store matchers may be used to lookup hosts from the store based on their
81 * various attributes. Service and attribute matchers are applied to a host's
82 * services and attributes and evaluate to true if *any* service or attribute
83 * matches.
84 *
85 * A store matcher object inherits from sdb_object_t and, thus, may safely be
86 * cast to a generic object.
87 */
88 struct sdb_memstore_matcher;
89 typedef struct sdb_memstore_matcher sdb_memstore_matcher_t;
90 #define SDB_MEMSTORE_MATCHER(obj) ((sdb_memstore_matcher_t *)(obj))
92 /*
93 * sdb_memstore_writer:
94 * A store writer implementation that provides an in-memory object store. It
95 * expects a store object as its user-data argument.
96 */
97 extern sdb_store_writer_t sdb_memstore_writer;
99 /*
100 * sdb_memstore_reader:
101 * A store reader implementation that uses an in-memory object store. It
102 * expects a store object as its user-data argument.
103 */
104 extern sdb_store_reader_t sdb_memstore_reader;
106 /*
107 * sdb_memstore_create:
108 * Allocate a new in-memory store.
109 */
110 sdb_memstore_t *
111 sdb_memstore_create(void);
113 /*
114 * sdb_memstore_host, sdb_memstore_service, sdb_memstore_metric,
115 * sdb_memstore_attribute, sdb_memstore_metric_attr:
116 * Store an object in the specified store. The hostname is expected to be
117 * canonical.
118 */
119 int
120 sdb_memstore_host(sdb_memstore_t *store, const char *name, sdb_time_t last_update);
121 int
122 sdb_memstore_service(sdb_memstore_t *store, const char *hostname, const char *name,
123 sdb_time_t last_update);
124 int
125 sdb_memstore_metric(sdb_memstore_t *store, const char *hostname, const char *name,
126 sdb_metric_store_t *metric_store, sdb_time_t last_update);
127 int
128 sdb_memstore_attribute(sdb_memstore_t *store, const char *hostname,
129 const char *key, const sdb_data_t *value, sdb_time_t last_update);
130 int
131 sdb_memstore_service_attr(sdb_memstore_t *store, const char *hostname,
132 const char *service, const char *key, const sdb_data_t *value,
133 sdb_time_t last_update);
134 int
135 sdb_memstore_metric_attr(sdb_memstore_t *store, const char *hostname,
136 const char *metric, const char *key, const sdb_data_t *value,
137 sdb_time_t last_update);
139 /*
140 * sdb_memstore_get_host:
141 * Query the specified store for a host by its (canonicalized) name.
142 *
143 * The function increments the ref count of the host object. The caller needs
144 * to deref it when no longer using it.
145 */
146 sdb_memstore_obj_t *
147 sdb_memstore_get_host(sdb_memstore_t *store, const char *name);
149 /*
150 * sdb_memstore_get_child:
151 * Retrieve a host's child object of the specified type and name. The
152 * reference count of the child object will be incremented before returning
153 * it. The caller is responsible for releasing the object once it's no longer
154 * used.
155 *
156 * Returns:
157 * - the child object on success
158 * - NULL else
159 */
160 sdb_memstore_obj_t *
161 sdb_memstore_get_child(sdb_memstore_obj_t *host, int type, const char *name);
163 /*
164 * sdb_memstore_get_field:
165 * Get the value of a stored object's queryable field. The caller is
166 * responsible for freeing any dynamically allocated memory possibly stored in
167 * the returned value. If 'res' is NULL, the function will return whether the
168 * field exists.
169 *
170 * Returns:
171 * - 0 on success
172 * - a negative value else
173 */
174 int
175 sdb_memstore_get_field(sdb_memstore_obj_t *obj, int field, sdb_data_t *res);
177 /*
178 * sdb_memstore_get_attr:
179 * Get the value of a stored object's attribute. The caller is responsible for
180 * freeing any dynamically allocated memory possibly stored in the returned
181 * value. If 'res' is NULL, the function will return whether the attribute
182 * exists. If specified, only attributes matching the filter will be
183 * considered.
184 *
185 * Returns:
186 * - 0 if the attribute exists
187 * - a negative value else
188 */
189 int
190 sdb_memstore_get_attr(sdb_memstore_obj_t *obj, const char *name, sdb_data_t *res,
191 sdb_memstore_matcher_t *filter);
193 /*
194 * Querying a store:
195 *
196 * - Query interface: A query is a formal description of an interaction with
197 * the store. It can be used, both, for read and write access. The query is
198 * described by its abstract syntax tree (AST). The parser package provides
199 * means to parse a string (SysQL) representation of the query into an AST.
200 *
201 * - Matcher / expression interface: This low-level interface provides direct
202 * control over how to access the store. It is used by the query
203 * implementation internally and can only be used for read access.
204 */
206 /*
207 * sdb_memstore_query_t:
208 * A parsed query readily prepared for execution.
209 */
210 struct sdb_memstore_query;
211 typedef struct sdb_memstore_query sdb_memstore_query_t;
213 /*
214 * sdb_memstore_query_prepare:
215 * Prepare the query described by 'ast' for execution in a store.
216 *
217 * Returns:
218 * - a store query on success
219 * - NULL else
220 */
221 sdb_memstore_query_t *
222 sdb_memstore_query_prepare(sdb_ast_node_t *ast);
224 /*
225 * sdb_memstore_query_prepare_matcher:
226 * Prepare the logical expression described by 'ast' for execution as a store
227 * matcher.
228 *
229 * Returns:
230 * - a matcher on success
231 * - NULL else
232 */
233 sdb_memstore_matcher_t *
234 sdb_memstore_query_prepare_matcher(sdb_ast_node_t *ast);
236 /*
237 * sdb_memstore_query_execute:
238 * Execute a previously prepared query in the specified store. The query
239 * result will be written to 'buf' and any errors to 'errbuf'.
240 *
241 * Returns:
242 * - the result type (to be used by the server reply)
243 * - a negative value on error
244 */
245 int
246 sdb_memstore_query_execute(sdb_memstore_t *store, sdb_memstore_query_t *m,
247 sdb_store_writer_t *w, sdb_object_t *wd, sdb_strbuf_t *errbuf);
249 /*
250 * sdb_memstore_expr_create:
251 * Creates an arithmetic expression implementing the specified operator on the
252 * specified left and right operand.
253 *
254 * Returns:
255 * - an expression object on success
256 * - NULL else
257 */
258 sdb_memstore_expr_t *
259 sdb_memstore_expr_create(int op,
260 sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
262 /*
263 * sdb_memstore_expr_typed:
264 * Creates an expression which evaluates in the context of an object's sibling
265 * as specified by the given type.
266 *
267 * Returns:
268 * - an expression object on success
269 * - NULL else
270 */
271 sdb_memstore_expr_t *
272 sdb_memstore_expr_typed(int typ, sdb_memstore_expr_t *expr);
274 /*
275 * sdb_memstore_expr_fieldvalue:
276 * Creates an expression which evaluates to the value of the specified
277 * queryable field of a stored object.
278 *
279 * Returns:
280 * - an expression object on success
281 * - NULL else
282 */
283 sdb_memstore_expr_t *
284 sdb_memstore_expr_fieldvalue(int field);
286 /*
287 * sdb_memstore_expr_attrvalue:
288 * Creates an expression which evaluates to the value of the specified
289 * attribute of a stored object. Evaluates to a NULL value if the attribute
290 * does not exist.
291 *
292 * Returns:
293 * - an expression object on success
294 * - NULL else
295 */
296 sdb_memstore_expr_t *
297 sdb_memstore_expr_attrvalue(const char *name);
299 /*
300 * sdb_memstore_expr_constvalue:
301 * Creates an expression which evaluates to the specified constant value.
302 *
303 * Returns:
304 * - an expression object on success
305 * - NULL else
306 */
307 sdb_memstore_expr_t *
308 sdb_memstore_expr_constvalue(const sdb_data_t *value);
310 /*
311 * sdb_memstore_expr_eval:
312 * Evaluate an expression for the specified stored object and stores the
313 * result in 'res'. The result's value will be allocated dynamically if
314 * necessary and, thus, should be free'd by the caller (e.g. using
315 * sdb_data_free_datum). The object may be NULL, in which case the expression
316 * needs to evaluate to a constant value. If specified, only objects matching
317 * the filter will be used during the evaluation.
318 *
319 * Returns:
320 * - 0 on success
321 * - a negative value else
322 */
323 int
324 sdb_memstore_expr_eval(sdb_memstore_expr_t *expr, sdb_memstore_obj_t *obj,
325 sdb_data_t *res, sdb_memstore_matcher_t *filter);
327 /*
328 * sdb_memstore_expr_iter:
329 * Iterate over the elements of an iterable expression. sdb_memstore_expr_iter
330 * returns NULL if the expression is not iterable (for the specified object).
331 *
332 * sdb_memstore_expr_iter_get_next returns NULL if there is no next element.
333 */
334 sdb_memstore_expr_iter_t *
335 sdb_memstore_expr_iter(sdb_memstore_expr_t *expr, sdb_memstore_obj_t *obj,
336 sdb_memstore_matcher_t *filter);
337 void
338 sdb_memstore_expr_iter_destroy(sdb_memstore_expr_iter_t *iter);
340 bool
341 sdb_memstore_expr_iter_has_next(sdb_memstore_expr_iter_t *iter);
342 sdb_data_t
343 sdb_memstore_expr_iter_get_next(sdb_memstore_expr_iter_t *iter);
345 /*
346 * sdb_memstore_dis_matcher:
347 * Creates a matcher matching the disjunction (logical OR) of two matchers.
348 */
349 sdb_memstore_matcher_t *
350 sdb_memstore_dis_matcher(sdb_memstore_matcher_t *left, sdb_memstore_matcher_t *right);
352 /*
353 * sdb_memstore_con_matcher:
354 * Creates a matcher matching the conjunction (logical AND) of two matchers.
355 */
356 sdb_memstore_matcher_t *
357 sdb_memstore_con_matcher(sdb_memstore_matcher_t *left, sdb_memstore_matcher_t *right);
359 /*
360 * sdb_memstore_inv_matcher:
361 * Creates a matcher matching the inverse (logical NOT) of a matcher.
362 */
363 sdb_memstore_matcher_t *
364 sdb_memstore_inv_matcher(sdb_memstore_matcher_t *m);
366 /*
367 * sdb_memstore_any_matcher:
368 * Creates a matcher iterating over values of the first expression (which has
369 * to be iterable). It matches if *any* of those elements match 'm'. 'm' has
370 * to be an ary operation with the left operand unset.
371 */
372 sdb_memstore_matcher_t *
373 sdb_memstore_any_matcher(sdb_memstore_expr_t *iter, sdb_memstore_matcher_t *m);
375 /*
376 * sdb_memstore_all_matcher:
377 * Creates a matcher iterating over values of the first expression (which has
378 * to be iterable). It matches if *all* of those elements match 'm'. 'm' has
379 * to be an ary operation with the left operand unset.
380 */
381 sdb_memstore_matcher_t *
382 sdb_memstore_all_matcher(sdb_memstore_expr_t *iter, sdb_memstore_matcher_t *m);
384 /*
385 * sdb_memstore_in_matcher:
386 * Creates a matcher which matches if the right value evaluates to an array
387 * value and the left value is included in that array. See sdb_data_inarray
388 * for more details.
389 */
390 sdb_memstore_matcher_t *
391 sdb_memstore_in_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
393 /*
394 * sdb_memstore_lt_matcher, sdb_memstore_le_matcher, sdb_memstore_eq_matcher,
395 * sdb_memstore_ge_matcher, sdb_memstore_gt_matcher:
396 * Create conditional matchers comparing the values of two expressions. The
397 * matcher matches if the left expression compres less than, less or equal
398 * than, equal to, not equal to, greater or equal than, or greater than the
399 * right expression.
400 */
401 sdb_memstore_matcher_t *
402 sdb_memstore_lt_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
403 sdb_memstore_matcher_t *
404 sdb_memstore_le_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
405 sdb_memstore_matcher_t *
406 sdb_memstore_eq_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
407 sdb_memstore_matcher_t *
408 sdb_memstore_ne_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
409 sdb_memstore_matcher_t *
410 sdb_memstore_ge_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
411 sdb_memstore_matcher_t *
412 sdb_memstore_gt_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
414 /*
415 * sdb_memstore_regex_matcher:
416 * Creates a matcher which matches the string value the left expression
417 * evaluates to against the regular expression the right expression evaluates
418 * to. The right expression may either be a constant value regular expression
419 * or string or a dynamic value evaluating to a string. In the latter case,
420 * the string is compiled to a regex every time the matcher is executed.
421 */
422 sdb_memstore_matcher_t *
423 sdb_memstore_regex_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
425 /*
426 * sdb_memstore_nregex_matcher:
427 * Creates a regex matcher just like sdb_memstore_regex_matcher except that it
428 * matches in case the regular expression does not match.
429 */
430 sdb_memstore_matcher_t *
431 sdb_memstore_nregex_matcher(sdb_memstore_expr_t *left, sdb_memstore_expr_t *right);
433 /*
434 * sdb_memstore_isnull_matcher:
435 * Creates a matcher matching NULL values.
436 */
437 sdb_memstore_matcher_t *
438 sdb_memstore_isnull_matcher(sdb_memstore_expr_t *expr);
440 /*
441 * sdb_memstore_istrue_matcher, sdb_memstore_isfalse_matcher:
442 * Creates a matcher matching boolean values.
443 */
444 sdb_memstore_matcher_t *
445 sdb_memstore_istrue_matcher(sdb_memstore_expr_t *expr);
446 sdb_memstore_matcher_t *
447 sdb_memstore_isfalse_matcher(sdb_memstore_expr_t *expr);
449 /*
450 * sdb_memstore_matcher_matches:
451 * Check whether the specified matcher matches the specified store object. If
452 * specified, the filter will be used to preselect objects for further
453 * evaluation. It is applied to any object that's used during the evaluation
454 * of the matcher. Only those objects matching the filter will be considered.
455 *
456 * Note that the filter is applied to all object types (hosts, service,
457 * metric, attribute). Thus, any object-specific matchers are mostly unsuited
458 * for this purpose and, if used, may result in unexpected behavior.
459 *
460 * Returns:
461 * - 1 if the object matches
462 * - 0 else
463 */
464 int
465 sdb_memstore_matcher_matches(sdb_memstore_matcher_t *m, sdb_memstore_obj_t *obj,
466 sdb_memstore_matcher_t *filter);
468 /*
469 * sdb_memstore_matcher_op_cb:
470 * Callback constructing a matcher operator.
471 */
472 typedef sdb_memstore_matcher_t *(*sdb_memstore_matcher_op_cb)
473 (sdb_memstore_expr_t *, sdb_memstore_expr_t *);
475 /*
476 * sdb_memstore_lookup_cb:
477 * Lookup callback. It is called for each matching object when looking up data
478 * in the store passing on the lookup filter and the specified user-data. The
479 * lookup aborts early if the callback returns non-zero.
480 */
481 typedef int (*sdb_memstore_lookup_cb)(sdb_memstore_obj_t *obj,
482 sdb_memstore_matcher_t *filter, void *user_data);
484 /*
485 * sdb_memstore_scan:
486 * Look up objects of the specified type in the specified store. The specified
487 * callback function is called for each object in the store matching 'm'. The
488 * function performs a full scan of all stored objects. If specified, the
489 * filter will be used to preselect objects for further evaluation. See the
490 * description of 'sdb_memstore_matcher_matches' for details.
491 *
492 * Returns:
493 * - 0 on success
494 * - a negative value else
495 */
496 int
497 sdb_memstore_scan(sdb_memstore_t *store, int type,
498 sdb_memstore_matcher_t *m, sdb_memstore_matcher_t *filter,
499 sdb_memstore_lookup_cb cb, void *user_data);
501 /*
502 * sdb_memstore_emit:
503 * Send a single object to the specified store writer. Attributes or any child
504 * objects are not included. Use sdb_memstore_emit_full() to emit a full
505 * (filtered) object.
506 *
507 * Returns:
508 * - 0 on success
509 * - a negative value else
510 */
511 int
512 sdb_memstore_emit(sdb_memstore_obj_t *obj, sdb_store_writer_t *w, sdb_object_t *wd);
514 /*
515 * sdb_memstore_emit_full:
516 * Send a single object and its attributes and all children to the specified
517 * store writer. The filter, if specified, is applied to each attribute and
518 * child object. Only matching objects will be emitted.
519 *
520 * Returns:
521 * - 0 on success
522 * - a negative value else
523 */
524 int
525 sdb_memstore_emit_full(sdb_memstore_obj_t *obj, sdb_memstore_matcher_t *filter,
526 sdb_store_writer_t *w, sdb_object_t *wd);
528 #ifdef __cplusplus
529 } /* extern "C" */
530 #endif
532 #endif /* ! SDB_CORE_MEMSTORE_H */
534 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */