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