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