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,
52 };
53 #define SDB_STORE_TYPE_TO_NAME(t) \
54 (((t) == SDB_HOST) ? "host" \
55 : ((t) == SDB_SERVICE) ? "service" \
56 : ((t) == SDB_METRIC) ? "metric" \
57 : ((t) == SDB_ATTRIBUTE) ? "attribute" : "unknown")
59 /*
60 * sdb_store_obj_t represents the super-class of any object stored in the
61 * database. It inherits from sdb_object_t and may safely be cast to a generic
62 * object to access its name.
63 */
64 struct sdb_store_obj;
65 typedef struct sdb_store_obj sdb_store_obj_t;
67 /*
68 * Queryable fields of a stored object.
69 */
70 enum {
71 SDB_FIELD_NAME = 1, /* string */
72 SDB_FIELD_LAST_UPDATE, /* datetime */
73 SDB_FIELD_AGE, /* datetime */
74 SDB_FIELD_INTERVAL, /* datetime */
75 SDB_FIELD_BACKEND, /* string */
76 };
78 #define SDB_FIELD_TO_NAME(f) \
79 (((f) == SDB_FIELD_NAME) ? "name" \
80 : ((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \
81 : ((f) == SDB_FIELD_AGE) ? "age" \
82 : ((f) == SDB_FIELD_INTERVAL) ? "interval" \
83 : ((f) == SDB_FIELD_BACKEND) ? "backend" : "unknown")
85 /*
86 * sdb_store_clear:
87 * Clear the entire store and remove all stored objects.
88 */
89 void
90 sdb_store_clear(void);
92 /*
93 * sdb_store_host:
94 * Add/update a host in the store. If the host, identified by its
95 * canonicalized name, already exists, it will be updated according to the
96 * specified name and timestamp. Else, a new entry will be created in the
97 * store. Any memory required for storing the entry will be allocated an
98 * managed by the store itself.
99 *
100 * Returns:
101 * - 0 on success
102 * - a positive value if the new entry is older than the currently stored
103 * entry (in this case, no update will happen)
104 * - a negative value on error
105 */
106 int
107 sdb_store_host(const char *name, sdb_time_t last_update);
109 /*
110 * sdb_store_has_host:
111 * sdb_store_get_host:
112 * Query the store for a host by its (canonicalized) name.
113 *
114 * sdb_store_get_host increments the ref count of the host object. The caller
115 * needs to deref it when no longer using it.
116 */
117 _Bool
118 sdb_store_has_host(const char *name);
120 sdb_store_obj_t *
121 sdb_store_get_host(const char *name);
123 /*
124 * sdb_store_attribute:
125 * Add/update a host's attribute in the store. If the attribute, identified by
126 * its key, already exists for the specified host, it will be updated to the
127 * specified values. If the referenced host does not exist, an error will be
128 * reported. Else, a new entry will be created in the store. Any memory
129 * required for storing the entry will be allocated and managed by the store
130 * itself.
131 *
132 * Returns:
133 * - 0 on success
134 * - a positive value if the new entry is older than the currently stored
135 * entry (in this case, no update will happen)
136 * - a negative value on error
137 */
138 int
139 sdb_store_attribute(const char *hostname,
140 const char *key, const sdb_data_t *value,
141 sdb_time_t last_update);
143 /*
144 * sdb_store_service:
145 * Add/update a service in the store. If the service, identified by its name,
146 * already exists for the specified host, it will be updated according to the
147 * specified 'service' object. If the referenced host does not exist, an error
148 * will be reported. Else, a new entry will be created in the store. Any
149 * memory required for storing the entry will be allocated an managed by the
150 * store itself.
151 *
152 * Returns:
153 * - 0 on success
154 * - a positive value if the new entry is older than the currently stored
155 * entry (in this case, no update will happen)
156 * - a negative value on error
157 */
158 int
159 sdb_store_service(const char *hostname, const char *name,
160 sdb_time_t last_update);
162 /*
163 * sdb_store_service_attr:
164 * Add/update a service's attribute in the store. If the attribute, identified
165 * by its key, already exists for the specified service, it will be updated to
166 * the specified value. If the references service (for the specified host)
167 * does not exist, an error will be reported. Any memory required for storing
168 * the entry will be allocated and managed by the store 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_service_attr(const char *hostname, const char *service,
178 const char *key, const sdb_data_t *value, sdb_time_t last_update);
180 /*
181 * A metric store describes how to access a metric's data.
182 */
183 typedef struct {
184 const char *type;
185 const char *id;
186 } sdb_metric_store_t;
188 /*
189 * sdb_store_metric:
190 * Add/update a metric in the store. If the metric, identified by its name,
191 * already exists for the specified host, it will be updated according to the
192 * specified 'metric' object. If the referenced host does not exist, an error
193 * will be reported. Else, a new entry will be created in the store. Any
194 * memory required for storing the entry will be allocated an managed by the
195 * store itself.
196 *
197 * If specified, the metric store describes where to access the metric's data.
198 *
199 * Returns:
200 * - 0 on success
201 * - a positive value if the new entry is older than the currently stored
202 * entry (in this case, no update will happen)
203 * - a negative value on error
204 */
205 int
206 sdb_store_metric(const char *hostname, const char *name,
207 sdb_metric_store_t *store, sdb_time_t last_update);
209 /*
210 * sdb_store_metric_attr:
211 * Add/update a metric's attribute in the store. If the attribute, identified
212 * by its key, already exists for the specified metric, it will be updated to
213 * the specified value. If the references metric (for the specified host)
214 * does not exist, an error will be reported. Any memory required for storing
215 * the entry will be allocated and managed by the store itself.
216 *
217 * Returns:
218 * - 0 on success
219 * - a positive value if the new entry is older than the currently stored
220 * entry (in this case, no update will happen)
221 * - a negative value on error
222 */
223 int
224 sdb_store_metric_attr(const char *hostname, const char *metric,
225 const char *key, const sdb_data_t *value, sdb_time_t last_update);
227 /*
228 * sdb_store_fetch_timeseries:
229 * Fetch the time-series described by the specified host's metric and
230 * serialize it as JSON into the provided string buffer.
231 *
232 * Returns:
233 * - 0 on success
234 * - a negative value else
235 */
236 int
237 sdb_store_fetch_timeseries(const char *hostname, const char *metric,
238 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf);
240 /*
241 * sdb_store_get_field:
242 * Get the value of a stored object's queryable field. The caller is
243 * responsible for freeing any dynamically allocated memory possibly stored in
244 * the returned value. If 'res' is NULL, the function will return whether the
245 * field exists.
246 *
247 * Note: Retrieving the backend this way is not currently supported.
248 *
249 * Returns:
250 * - 0 on success
251 * - a negative value else
252 */
253 int
254 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
256 /*
257 * Expressions specify arithmetic expressions.
258 *
259 * A expression object inherits from sdb_object_t and, thus, may safely be
260 * cast to a generic object.
261 */
262 struct sdb_store_expr;
263 typedef struct sdb_store_expr sdb_store_expr_t;
264 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
266 /*
267 * sdb_store_expr_create:
268 * Creates an arithmetic expression implementing the specified operator on the
269 * specified left and right operand.
270 *
271 * Returns:
272 * - an expression object on success
273 * - NULL else
274 */
275 sdb_store_expr_t *
276 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
278 /*
279 * sdb_store_expr_fieldvalue:
280 * Creates an expression which evaluates to the value of the specified
281 * queryable field of a stored object.
282 *
283 * Returns:
284 * - an expression object on success
285 * - NULL else
286 */
287 sdb_store_expr_t *
288 sdb_store_expr_fieldvalue(int field);
290 /*
291 * sdb_store_expr_constvalue:
292 * Creates an expression which evaluates to the specified constant value.
293 *
294 * Returns:
295 * - an expression object on success
296 * - NULL else
297 */
298 sdb_store_expr_t *
299 sdb_store_expr_constvalue(const sdb_data_t *value);
301 /*
302 * sdb_store_expr_eval:
303 * Evaluate an expression for the specified stored object and stores the
304 * result in 'res'. The result's value will be allocated dynamically if
305 * necessary and, thus, should be free'd by the caller (e.g. using
306 * sdb_data_free_datum). The object may be NULL, in which case the expression
307 * needs to evaluate to a constant value.
308 *
309 * Returns:
310 * - 0 on success
311 * - a negative value else
312 */
313 int
314 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
315 sdb_data_t *res);
317 /*
318 * Conditionals may be used to lookup hosts from the store based on a
319 * conditional expression.
320 *
321 * A conditional object inherits from sdb_object_t and, thus, may safely be
322 * cast to a generic object.
323 */
324 struct sdb_store_cond;
325 typedef struct sdb_store_cond sdb_store_cond_t;
326 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
328 /*
329 * sdb_store_attr_cond:
330 * Creates a conditional based on attribute values. The value of stored
331 * attributes is compared against the value the expression evaluates to. See
332 * sdb_data_cmp for details about the comparison.
333 */
334 sdb_store_cond_t *
335 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr);
337 /*
338 * sdb_store_obj_cond:
339 * Creates a conditional based on queryable object fields. The respective
340 * field of *any* object type is compared against the value the expression
341 * evaluates to.
342 */
343 sdb_store_cond_t *
344 sdb_store_obj_cond(int field, sdb_store_expr_t *expr);
346 /*
347 * Store matchers may be used to lookup hosts from the store based on their
348 * various attributes. Service and attribute matchers are applied to a host's
349 * services and attributes and evaluate to true if *any* service or attribute
350 * matches.
351 *
352 * A store matcher object inherits from sdb_object_t and, thus, may safely be
353 * cast to a generic object.
354 */
355 struct sdb_store_matcher;
356 typedef struct sdb_store_matcher sdb_store_matcher_t;
357 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
359 /*
360 * sdb_store_name_matcher:
361 * Creates a matcher matching by the specified object type's name. If 're' is
362 * true, the specified name is treated as a POSIX extended regular expression.
363 * Else, the exact name has to match (case-insensitive).
364 */
365 sdb_store_matcher_t *
366 sdb_store_name_matcher(int type, const char *name, _Bool re);
368 /*
369 * sdb_store_attr_matcher:
370 * Creates a matcher matching attributes based on their value. If 're' is
371 * true, the specified name is treated as a POSIX extended regular expression.
372 * Else, the exact name has to match (case-insensitive).
373 */
374 sdb_store_matcher_t *
375 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
377 /*
378 * sdb_store_isnull_matcher:
379 * Creates a matcher matching "missing" attributes.
380 */
381 sdb_store_matcher_t *
382 sdb_store_isnull_matcher(const char *attr_name);
384 /*
385 * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
386 * sdb_store_ge_matcher, sdb_store_gt_matcher:
387 * Creates a matcher based on a conditional. The matcher matches objects for
388 * which the conditional evaluates the object to compare less than, less or
389 * equal, equal, greater or equal, or greater than the conditional's value
390 * repsectively.
391 */
392 sdb_store_matcher_t *
393 sdb_store_lt_matcher(sdb_store_cond_t *cond);
394 sdb_store_matcher_t *
395 sdb_store_le_matcher(sdb_store_cond_t *cond);
396 sdb_store_matcher_t *
397 sdb_store_eq_matcher(sdb_store_cond_t *cond);
398 sdb_store_matcher_t *
399 sdb_store_ge_matcher(sdb_store_cond_t *cond);
400 sdb_store_matcher_t *
401 sdb_store_gt_matcher(sdb_store_cond_t *cond);
403 /*
404 * sdb_store_parse_object_type_plural:
405 * Parse the type name (plural) of a stored object.
406 *
407 * Returns:
408 * - the object type on success
409 * - a negative value else
410 */
411 int
412 sdb_store_parse_object_type_plural(const char *name);
414 /*
415 * sdb_store_parse_field_name:
416 * Parse the name of a stored object's queryable field.
417 *
418 * Returns:
419 * - the field id on success
420 * - a negative value else
421 */
422 int
423 sdb_store_parse_field_name(const char *name);
425 /*
426 * sdb_store_matcher_parse_cmp:
427 * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
428 *
429 * Returns:
430 * - a matcher object on success
431 * - NULL else
432 */
433 sdb_store_matcher_t *
434 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
435 const char *op, sdb_store_expr_t *expr);
437 /*
438 * sdb_store_matcher_parse_field_cmp:
439 * Parse a simple compare expression for queryable object fields (<field> <op>
440 * <expression>).
441 *
442 * Returns:
443 * - a matcher object on success
444 * - NULL else
445 */
446 sdb_store_matcher_t *
447 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
448 sdb_store_expr_t *expr);
450 /*
451 * sdb_store_dis_matcher:
452 * Creates a matcher matching the disjunction (logical OR) of two matchers.
453 */
454 sdb_store_matcher_t *
455 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
457 /*
458 * sdb_store_con_matcher:
459 * Creates a matcher matching the conjunction (logical AND) of two matchers.
460 */
461 sdb_store_matcher_t *
462 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
464 /*
465 * sdb_store_con_matcher::
466 * Creates a matcher matching the inverse (logical NOT) of a matcher.
467 */
468 sdb_store_matcher_t *
469 sdb_store_inv_matcher(sdb_store_matcher_t *m);
471 /*
472 * sdb_store_matcher_matches:
473 * Check whether the specified matcher matches the specified store object. If
474 * specified, the filter will be used to preselect objects for further
475 * evaluation. It is applied to any object that's used during the evaluation
476 * of the matcher. Only those objects matching the filter will be considered.
477 *
478 * Note that the filter is applied to all object types (hosts, service,
479 * metric, attribute). Thus, any object-specific matchers are mostly unsuited
480 * for this purpose and, if used, may result in unexpected behavior.
481 *
482 * Returns:
483 * - 1 if the object matches
484 * - 0 else
485 */
486 int
487 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
488 sdb_store_matcher_t *filter);
490 /*
491 * sdb_store_matcher_tostring:
492 * Format a matcher object as string. This is meant for logging or debugging
493 * purposes.
494 */
495 char *
496 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
498 /*
499 * sdb_store_lookup_cb:
500 * Lookup callback. It is called for each matching object when looking up data
501 * in the store. The lookup aborts if the callback returns non-zero.
502 */
503 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
505 /*
506 * sdb_store_scan:
507 * Look up objects in the store. The specified callback function is called for
508 * each object in the store matching 'm'. The function performs a full scan of
509 * all hosts stored in the database. If specified, the filter will be used to
510 * preselect objects for further evaluation. See the description of
511 * 'sdb_store_matcher_matches' for details.
512 *
513 * Returns:
514 * - 0 on success
515 * - a negative value else
516 */
517 int
518 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
519 sdb_store_lookup_cb cb, void *user_data);
521 /*
522 * Flags for serialization functions.
523 *
524 * By default, the full host object will be included in the serialized output.
525 * When specifying any of the flags, the respective information will be left
526 * out. The SKIP_EMPTY flags may be used to skip host objects entirely.
527 */
528 enum {
529 SDB_SKIP_ATTRIBUTES = 1 << 0,
530 SDB_SKIP_SERVICES = 1 << 1,
531 SDB_SKIP_METRICS = 1 << 2,
532 SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 3,
534 SDB_SKIP_ALL = (1 << 8) - 1,
536 /* skip hosts if they do not reference any services/metrics */
537 SDB_SKIP_EMPTY_SERVICES = 1 << 8,
538 SDB_SKIP_EMPTY_METRICS = 1 << 9,
539 };
541 /*
542 * sdb_store_tojson:
543 * Serialize the entire store to JSON and append the result to the specified
544 * buffer. If specified, only objects matching the filter will be included in
545 * the result (see sdb_store_host_tojson for details).
546 *
547 * Returns:
548 * - 0 on success
549 * - a negative value on error
550 */
551 int
552 sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags);
554 /*
555 * sdb_store_host_tojson:
556 * Serialize a host object to JSON and append the result to the specified
557 * buffer. If specified, only objects matching the filter will be included in
558 * the result. The filter is applied to each object individually and, thus,
559 * should not be of any object-type specific kind. The filter is never applied
560 * to the specified host object; the caller is responsible for this and for
561 * correctly handling skipped hosts.
562 *
563 * Returns:
564 * - 0 on success
565 * - a negative value on error
566 */
567 int
568 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf,
569 sdb_store_matcher_t *filter, int flags);
571 /*
572 * sdb_store_iter_cb:
573 * Store iterator callback. Iteration stops if the callback returns non-zero.
574 */
575 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
577 /*
578 * sdb_store_iterate:
579 * Iterate the entire store, calling the specified callback for each object.
580 * The user_data pointer is passed on to each call of the callback.
581 *
582 * Returns:
583 * - 0 on success
584 * - a negative value else
585 */
586 int
587 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
589 #ifdef __cplusplus
590 } /* extern "C" */
591 #endif
593 #endif /* ! SDB_CORE_STORE_H */
595 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */