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,
53 SDB_ATTRIBUTE = 1 << 4,
55 /*
56 * Queryable fields of a stored object.
57 */
58 SDB_FIELD_NAME = 1 << 8, /* type: string */
59 SDB_FIELD_LAST_UPDATE, /* type: datetime */
60 SDB_FIELD_AGE, /* type: datetime */
61 SDB_FIELD_INTERVAL, /* type: datetime */
62 SDB_FIELD_BACKEND, /* type: array of strings */
63 };
64 #define SDB_STORE_TYPE_TO_NAME(t) \
65 (((t) == SDB_HOST) ? "host" \
66 : ((t) == SDB_SERVICE) ? "service" \
67 : ((t) == SDB_METRIC) ? "metric" \
68 : ((t) == SDB_ATTRIBUTE) ? "attribute" \
69 : ((t) == (SDB_ATTRIBUTE | SDB_HOST)) ? "host attribute" \
70 : ((t) == (SDB_ATTRIBUTE | SDB_SERVICE)) ? "service attribute" \
71 : ((t) == (SDB_ATTRIBUTE | SDB_METRIC)) ? "metric attribute" \
72 : "unknown")
74 #define SDB_FIELD_TO_NAME(f) \
75 (((f) == SDB_FIELD_NAME) ? "name" \
76 : ((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \
77 : ((f) == SDB_FIELD_AGE) ? "age" \
78 : ((f) == SDB_FIELD_INTERVAL) ? "interval" \
79 : ((f) == SDB_FIELD_BACKEND) ? "backend" : "unknown")
81 #define SDB_FIELD_TYPE(f) \
82 (((f) == SDB_FIELD_NAME) ? SDB_TYPE_STRING \
83 : ((f) == SDB_FIELD_LAST_UPDATE) ? SDB_TYPE_DATETIME \
84 : ((f) == SDB_FIELD_AGE) ? SDB_TYPE_DATETIME \
85 : ((f) == SDB_FIELD_INTERVAL) ? SDB_TYPE_DATETIME \
86 : ((f) == SDB_FIELD_BACKEND) ? (SDB_TYPE_ARRAY | SDB_TYPE_STRING) \
87 : -1)
89 /*
90 * sdb_store_obj_t represents the super-class of any object stored in the
91 * database. It inherits from sdb_object_t and may safely be cast to a generic
92 * object to access its name.
93 */
94 struct sdb_store_obj;
95 typedef struct sdb_store_obj sdb_store_obj_t;
97 /*
98 * Expressions represent arithmetic expressions based on stored objects and
99 * their various attributes.
100 *
101 * An expression object inherits from sdb_object_t and, thus, may safely be
102 * cast to a generic object.
103 */
104 struct sdb_store_expr;
105 typedef struct sdb_store_expr sdb_store_expr_t;
106 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
108 /*
109 * Store matchers may be used to lookup hosts from the store based on their
110 * various attributes. Service and attribute matchers are applied to a host's
111 * services and attributes and evaluate to true if *any* service or attribute
112 * matches.
113 *
114 * A store matcher object inherits from sdb_object_t and, thus, may safely be
115 * cast to a generic object.
116 */
117 struct sdb_store_matcher;
118 typedef struct sdb_store_matcher sdb_store_matcher_t;
119 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
121 /*
122 * A JSON formatter converts stored objects into the JSON format.
123 * See http://www.ietf.org/rfc/rfc4627.txt
124 */
125 struct sdb_store_json_formatter;
126 typedef struct sdb_store_json_formatter sdb_store_json_formatter_t;
128 /*
129 * sdb_store_clear:
130 * Clear the entire store and remove all stored objects.
131 */
132 void
133 sdb_store_clear(void);
135 /*
136 * sdb_store_host:
137 * Add/update a host in the store. If the host, identified by its
138 * canonicalized name, already exists, it will be updated according to the
139 * specified name and timestamp. Else, a new entry will be created in the
140 * store. Any memory required for storing the entry will be allocated an
141 * managed by the store itself.
142 *
143 * Returns:
144 * - 0 on success
145 * - a positive value if the new entry is older than the currently stored
146 * entry (in this case, no update will happen)
147 * - a negative value on error
148 */
149 int
150 sdb_store_host(const char *name, sdb_time_t last_update);
152 /*
153 * sdb_store_has_host:
154 * sdb_store_get_host:
155 * Query the store for a host by its (canonicalized) name.
156 *
157 * sdb_store_get_host increments the ref count of the host object. The caller
158 * needs to deref it when no longer using it.
159 */
160 bool
161 sdb_store_has_host(const char *name);
163 sdb_store_obj_t *
164 sdb_store_get_host(const char *name);
166 /*
167 * sdb_store_attribute:
168 * Add/update a host's attribute in the store. If the attribute, identified by
169 * its key, already exists for the specified host, it will be updated to the
170 * specified values. If the referenced host does not exist, an error will be
171 * reported. Else, a new entry will be created in the store. Any memory
172 * required for storing the entry will be allocated and managed by the store
173 * itself.
174 *
175 * Returns:
176 * - 0 on success
177 * - a positive value if the new entry is older than the currently stored
178 * entry (in this case, no update will happen)
179 * - a negative value on error
180 */
181 int
182 sdb_store_attribute(const char *hostname,
183 const char *key, const sdb_data_t *value,
184 sdb_time_t last_update);
186 /*
187 * sdb_store_service:
188 * Add/update a service in the store. If the service, identified by its name,
189 * already exists for the specified host, it will be updated according to the
190 * specified 'service' object. If the referenced host does not exist, an error
191 * will be reported. Else, a new entry will be created in the store. Any
192 * memory required for storing the entry will be allocated an managed by the
193 * store itself.
194 *
195 * Returns:
196 * - 0 on success
197 * - a positive value if the new entry is older than the currently stored
198 * entry (in this case, no update will happen)
199 * - a negative value on error
200 */
201 int
202 sdb_store_service(const char *hostname, const char *name,
203 sdb_time_t last_update);
205 /*
206 * sdb_store_service_attr:
207 * Add/update a service's attribute in the store. If the attribute, identified
208 * by its key, already exists for the specified service, it will be updated to
209 * the specified value. If the references service (for the specified host)
210 * does not exist, an error will be reported. Any memory required for storing
211 * the entry will be allocated and managed by the store itself.
212 *
213 * Returns:
214 * - 0 on success
215 * - a positive value if the new entry is older than the currently stored
216 * entry (in this case, no update will happen)
217 * - a negative value on error
218 */
219 int
220 sdb_store_service_attr(const char *hostname, const char *service,
221 const char *key, const sdb_data_t *value, sdb_time_t last_update);
223 /*
224 * A metric store describes how to access a metric's data.
225 */
226 typedef struct {
227 const char *type;
228 const char *id;
229 } sdb_metric_store_t;
231 /*
232 * sdb_store_metric:
233 * Add/update a metric in the store. If the metric, identified by its name,
234 * already exists for the specified host, it will be updated according to the
235 * specified 'metric' object. If the referenced host does not exist, an error
236 * will be reported. Else, a new entry will be created in the store. Any
237 * memory required for storing the entry will be allocated an managed by the
238 * store itself.
239 *
240 * If specified, the metric store describes where to access the metric's data.
241 *
242 * Returns:
243 * - 0 on success
244 * - a positive value if the new entry is older than the currently stored
245 * entry (in this case, no update will happen)
246 * - a negative value on error
247 */
248 int
249 sdb_store_metric(const char *hostname, const char *name,
250 sdb_metric_store_t *store, sdb_time_t last_update);
252 /*
253 * sdb_store_metric_attr:
254 * Add/update a metric's attribute in the store. If the attribute, identified
255 * by its key, already exists for the specified metric, it will be updated to
256 * the specified value. If the references metric (for the specified host)
257 * does not exist, an error will be reported. Any memory required for storing
258 * the entry will be allocated and managed by the store itself.
259 *
260 * Returns:
261 * - 0 on success
262 * - a positive value if the new entry is older than the currently stored
263 * entry (in this case, no update will happen)
264 * - a negative value on error
265 */
266 int
267 sdb_store_metric_attr(const char *hostname, const char *metric,
268 const char *key, const sdb_data_t *value, sdb_time_t last_update);
270 /*
271 * sdb_store_fetch_timeseries:
272 * Fetch the time-series described by the specified host's metric and
273 * serialize it as JSON into the provided string buffer.
274 *
275 * Returns:
276 * - 0 on success
277 * - a negative value else
278 */
279 int
280 sdb_store_fetch_timeseries(const char *hostname, const char *metric,
281 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf);
283 /*
284 * sdb_store_get_child:
285 * Retrieve a host's child object of the specified type and name. The
286 * reference count of the child object will be incremented before returning
287 * it. The caller is responsible for releasing the object once it's no longer
288 * used.
289 *
290 * Returns:
291 * - the child object on success
292 * - NULL else
293 */
294 sdb_store_obj_t *
295 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name);
297 /*
298 * sdb_store_get_field:
299 * Get the value of a stored object's queryable field. The caller is
300 * responsible for freeing any dynamically allocated memory possibly stored in
301 * the returned value. If 'res' is NULL, the function will return whether the
302 * field exists.
303 *
304 * Note: Retrieving the backend this way is not currently supported.
305 *
306 * Returns:
307 * - 0 on success
308 * - a negative value else
309 */
310 int
311 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
313 /*
314 * sdb_store_get_attr:
315 * Get the value of a stored object's attribute. The caller is responsible for
316 * freeing any dynamically allocated memory possibly stored in the returned
317 * value. If 'res' is NULL, the function will return whether the attribute
318 * exists. If specified, only attributes matching the filter will be
319 * considered.
320 *
321 * Returns:
322 * - 0 if the attribute exists
323 * - a negative value else
324 */
325 int
326 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
327 sdb_store_matcher_t *filter);
329 /*
330 * sdb_store_expr_create:
331 * Creates an arithmetic expression implementing the specified operator on the
332 * specified left and right operand.
333 *
334 * Returns:
335 * - an expression object on success
336 * - NULL else
337 */
338 sdb_store_expr_t *
339 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
341 /*
342 * sdb_store_expr_typed:
343 * Creates an expression which evaluates in the context of an object's sibling
344 * as specified by the given type.
345 *
346 * Returns:
347 * - an expression object on success
348 * - NULL else
349 */
350 sdb_store_expr_t *
351 sdb_store_expr_typed(int typ, sdb_store_expr_t *expr);
353 /*
354 * sdb_store_expr_fieldvalue:
355 * Creates an expression which evaluates to the value of the specified
356 * queryable field of a stored object.
357 *
358 * Returns:
359 * - an expression object on success
360 * - NULL else
361 */
362 sdb_store_expr_t *
363 sdb_store_expr_fieldvalue(int field);
365 /*
366 * sdb_store_expr_attrvalue:
367 * Creates an expression which evaluates to the value of the specified
368 * attribute of a stored object. Evaluates to a NULL value if the attribute
369 * does not exist.
370 *
371 * Returns:
372 * - an expression object on success
373 * - NULL else
374 */
375 sdb_store_expr_t *
376 sdb_store_expr_attrvalue(const char *name);
378 /*
379 * sdb_store_expr_constvalue:
380 * Creates an expression which evaluates to the specified constant value.
381 *
382 * Returns:
383 * - an expression object on success
384 * - NULL else
385 */
386 sdb_store_expr_t *
387 sdb_store_expr_constvalue(const sdb_data_t *value);
389 /*
390 * sdb_store_expr_eval:
391 * Evaluate an expression for the specified stored object and stores the
392 * result in 'res'. The result's value will be allocated dynamically if
393 * necessary and, thus, should be free'd by the caller (e.g. using
394 * sdb_data_free_datum). The object may be NULL, in which case the expression
395 * needs to evaluate to a constant value. If specified, only objects matching
396 * the filter will be used during the evaluation.
397 *
398 * Returns:
399 * - 0 on success
400 * - a negative value else
401 */
402 int
403 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
404 sdb_data_t *res, sdb_store_matcher_t *filter);
406 /*
407 * sdb_store_dis_matcher:
408 * Creates a matcher matching the disjunction (logical OR) of two matchers.
409 */
410 sdb_store_matcher_t *
411 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
413 /*
414 * sdb_store_con_matcher:
415 * Creates a matcher matching the conjunction (logical AND) of two matchers.
416 */
417 sdb_store_matcher_t *
418 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
420 /*
421 * sdb_store_inv_matcher::
422 * Creates a matcher matching the inverse (logical NOT) of a matcher.
423 */
424 sdb_store_matcher_t *
425 sdb_store_inv_matcher(sdb_store_matcher_t *m);
427 /*
428 * sdb_store_any_matcher:
429 * Creates a matcher iterating over objects of the specified type. It matches
430 * if *any* of those objects match 'm'.
431 */
432 sdb_store_matcher_t *
433 sdb_store_any_matcher(int type, sdb_store_matcher_t *m);
435 /*
436 * sdb_store_all_matcher:
437 * Creates a matcher iterating over objects of the specified type. It matches
438 * if *all* of those objects match 'm'.
439 */
440 sdb_store_matcher_t *
441 sdb_store_all_matcher(int type, sdb_store_matcher_t *m);
443 /*
444 * sdb_store_in_matcher:
445 * Creates a matcher which matches if the right value evaluates to an array
446 * value and the left value is included in that array. See sdb_data_inarray
447 * for more details.
448 */
449 sdb_store_matcher_t *
450 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
452 /*
453 * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
454 * sdb_store_ge_matcher, sdb_store_gt_matcher:
455 * Create conditional matchers comparing the values of two expressions. The
456 * matcher matches if the left expression compres less than, less or equal
457 * than, equal to, not equal to, greater or equal than, or greater than the
458 * right expression.
459 */
460 sdb_store_matcher_t *
461 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
462 sdb_store_matcher_t *
463 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
464 sdb_store_matcher_t *
465 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
466 sdb_store_matcher_t *
467 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
468 sdb_store_matcher_t *
469 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
470 sdb_store_matcher_t *
471 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
473 /*
474 * sdb_store_regex_matcher:
475 * Creates a matcher which matches the string value the left expression
476 * evaluates to against the regular expression the right expression evaluates
477 * to. The right expression may either be a constant value regular expression
478 * or string or a dynamic value evaluating to a string. In the latter case,
479 * the string is compiled to a regex every time the matcher is executed.
480 */
481 sdb_store_matcher_t *
482 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
484 /*
485 * sdb_store_nregex_matcher:
486 * Creates a regex matcher just like sdb_store_regex_matcher except that it
487 * matches in case the regular expression does not match.
488 */
489 sdb_store_matcher_t *
490 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
492 /*
493 * sdb_store_isnull_matcher:
494 * Creates a matcher matching NULL values.
495 */
496 sdb_store_matcher_t *
497 sdb_store_isnull_matcher(sdb_store_expr_t *expr);
499 /*
500 * sdb_store_isnnull_matcher:
501 * Creates a matcher matching non-NULL values.
502 */
503 sdb_store_matcher_t *
504 sdb_store_isnnull_matcher(sdb_store_expr_t *expr);
506 /*
507 * sdb_store_matcher_matches:
508 * Check whether the specified matcher matches the specified store object. If
509 * specified, the filter will be used to preselect objects for further
510 * evaluation. It is applied to any object that's used during the evaluation
511 * of the matcher. Only those objects matching the filter will be considered.
512 *
513 * Note that the filter is applied to all object types (hosts, service,
514 * metric, attribute). Thus, any object-specific matchers are mostly unsuited
515 * for this purpose and, if used, may result in unexpected behavior.
516 *
517 * Returns:
518 * - 1 if the object matches
519 * - 0 else
520 */
521 int
522 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
523 sdb_store_matcher_t *filter);
525 /*
526 * sdb_store_matcher_op_cb:
527 * Callback constructing a matcher operator.
528 */
529 typedef sdb_store_matcher_t *(*sdb_store_matcher_op_cb)
530 (sdb_store_expr_t *, sdb_store_expr_t *);
532 /*
533 * sdb_store_parse_matcher_op:
534 * Parse a matcher operator and return a constructor for the respective
535 * matcher.
536 *
537 * Returns:
538 * - matcher operator constructor on success
539 * - NULL else
540 */
541 sdb_store_matcher_op_cb
542 sdb_store_parse_matcher_op(const char *op);
544 /*
545 * sdb_store_parse_object_type:
546 * Parse the type name of a stored object.
547 *
548 * Returns:
549 * - the object type on success
550 * - a negative value else
551 */
552 int
553 sdb_store_parse_object_type(const char *name);
555 /*
556 * sdb_store_parse_object_type_plural:
557 * Parse the type name (plural) of a stored object.
558 *
559 * Returns:
560 * - the object type on success
561 * - a negative value else
562 */
563 int
564 sdb_store_parse_object_type_plural(const char *name);
566 /*
567 * sdb_store_parse_field_name:
568 * Parse the name of a stored object's queryable field.
569 *
570 * Returns:
571 * - the field id on success
572 * - a negative value else
573 */
574 int
575 sdb_store_parse_field_name(const char *name);
577 /*
578 * sdb_store_lookup_cb:
579 * Lookup callback. It is called for each matching object when looking up data
580 * in the store passing on the lookup filter and the specified user-data. The
581 * lookup aborts early if the callback returns non-zero.
582 */
583 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj,
584 sdb_store_matcher_t *filter, void *user_data);
586 /*
587 * sdb_store_scan:
588 * Look up objects of the specified type in the store. The specified callback
589 * function is called for each object in the store matching 'm'. The function
590 * performs a full scan of all objects stored in the database. If specified,
591 * the filter will be used to preselect objects for further evaluation. See
592 * the description of 'sdb_store_matcher_matches' for details.
593 *
594 * Returns:
595 * - 0 on success
596 * - a negative value else
597 */
598 int
599 sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
600 sdb_store_lookup_cb cb, void *user_data);
602 /*
603 * Flags for JSON formatting.
604 */
605 enum {
606 SDB_WANT_ARRAY = 1 << 0,
607 };
609 /*
610 * sdb_store_json_formatter:
611 * Create a JSON formatter for the specified object types writing to the
612 * specified buffer.
613 */
614 sdb_store_json_formatter_t *
615 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags);
617 /*
618 * sdb_store_json_emit:
619 * Serialize a single object to JSON adding it to the string buffer associated
620 * with the formatter object. The serialized object will not include
621 * attributes or any child objects. Instead, call the function again for each
622 * of those objects. All attributes have to be emitted before any other
623 * children types. Use sdb_store_json_emit_full() to emit a full (filtered)
624 * object.
625 *
626 * Note that the output might not be valid JSON before calling
627 * sdb_store_json_finish().
628 *
629 * Returns:
630 * - 0 on success
631 * - a negative value else
632 */
633 int
634 sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj);
636 /*
637 * sdb_store_json_emit_full:
638 * Serialize a single object including it's attributes and all children to
639 * JSON, adding it to the string buffer associated with the formatter object.
640 * The filter, if specified, is applied to each attribute and child object.
641 * Only matching objects will be included in the output.
642 *
643 * Note that the output might not be valid JSON before calling
644 * sdb_store_json_finish().
645 *
646 * Returns:
647 * - 0 on success
648 * - a negative value else
649 */
650 int
651 sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
652 sdb_store_matcher_t *filter);
654 /*
655 * sdb_store_json_finish:
656 * Finish the JSON output. This function has to be called once after emiting
657 * all objects.
658 */
659 int
660 sdb_store_json_finish(sdb_store_json_formatter_t *f);
662 #ifdef __cplusplus
663 } /* extern "C" */
664 #endif
666 #endif /* ! SDB_CORE_STORE_H */
668 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */