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