Code

2720db472f30abd4c853b7c1c805979749407f25
[sysdb.git] / src / include / core / store.h
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  * Expressions represent arithmetic expressions based on stored objects and
69  * their various attributes.
70  *
71  * An expression object inherits from sdb_object_t and, thus, may safely be
72  * cast to a generic object.
73  */
74 struct sdb_store_expr;
75 typedef struct sdb_store_expr sdb_store_expr_t;
76 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
78 /*
79  * Store matchers may be used to lookup hosts from the store based on their
80  * various attributes. Service and attribute matchers are applied to a host's
81  * services and attributes and evaluate to true if *any* service or attribute
82  * matches.
83  *
84  * A store matcher object inherits from sdb_object_t and, thus, may safely be
85  * cast to a generic object.
86  */
87 struct sdb_store_matcher;
88 typedef struct sdb_store_matcher sdb_store_matcher_t;
89 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
91 /*
92  * Queryable fields of a stored object.
93  */
94 enum {
95         SDB_FIELD_NAME = 1,    /* string */
96         SDB_FIELD_LAST_UPDATE, /* datetime */
97         SDB_FIELD_AGE,         /* datetime */
98         SDB_FIELD_INTERVAL,    /* datetime */
99         SDB_FIELD_BACKEND,     /* string */
100 };
102 #define SDB_FIELD_TO_NAME(f) \
103         (((f) == SDB_FIELD_NAME) ? "name" \
104                 : ((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \
105                 : ((f) == SDB_FIELD_AGE) ? "age" \
106                 : ((f) == SDB_FIELD_INTERVAL) ? "interval" \
107                 : ((f) == SDB_FIELD_BACKEND) ? "backend" : "unknown")
109 #define SDB_FIELD_TYPE(f) \
110         (((f) == SDB_FIELD_NAME) ? SDB_TYPE_STRING \
111                 : ((f) == SDB_FIELD_LAST_UPDATE) ? SDB_TYPE_DATETIME \
112                 : ((f) == SDB_FIELD_AGE) ? SDB_TYPE_DATETIME \
113                 : ((f) == SDB_FIELD_INTERVAL) ? SDB_TYPE_DATETIME \
114                 : ((f) == SDB_FIELD_BACKEND) ? (SDB_TYPE_ARRAY | SDB_TYPE_STRING) \
115                 : -1)
117 /*
118  * sdb_store_clear:
119  * Clear the entire store and remove all stored objects.
120  */
121 void
122 sdb_store_clear(void);
124 /*
125  * sdb_store_host:
126  * Add/update a host in the store. If the host, identified by its
127  * canonicalized name, already exists, it will be updated according to the
128  * specified name and timestamp. Else, a new entry will be created in the
129  * store. Any memory required for storing the entry will be allocated an
130  * managed by the store 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_host(const char *name, sdb_time_t last_update);
141 /*
142  * sdb_store_has_host:
143  * sdb_store_get_host:
144  * Query the store for a host by its (canonicalized) name.
145  *
146  * sdb_store_get_host increments the ref count of the host object. The caller
147  * needs to deref it when no longer using it.
148  */
149 _Bool
150 sdb_store_has_host(const char *name);
152 sdb_store_obj_t *
153 sdb_store_get_host(const char *name);
155 /*
156  * sdb_store_attribute:
157  * Add/update a host's attribute in the store. If the attribute, identified by
158  * its key, already exists for the specified host, it will be updated to the
159  * specified values. If the referenced host does not exist, an error will be
160  * reported. Else, a new entry will be created in the store. Any memory
161  * required for storing the entry will be allocated and managed by the store
162  * itself.
163  *
164  * Returns:
165  *  - 0 on success
166  *  - a positive value if the new entry is older than the currently stored
167  *    entry (in this case, no update will happen)
168  *  - a negative value on error
169  */
170 int
171 sdb_store_attribute(const char *hostname,
172                 const char *key, const sdb_data_t *value,
173                 sdb_time_t last_update);
175 /*
176  * sdb_store_service:
177  * Add/update a service in the store. If the service, identified by its name,
178  * already exists for the specified host, it will be updated according to the
179  * specified 'service' object. If the referenced host does not exist, an error
180  * will be reported. Else, a new entry will be created in the store. Any
181  * memory required for storing the entry will be allocated an managed by the
182  * store itself.
183  *
184  * Returns:
185  *  - 0 on success
186  *  - a positive value if the new entry is older than the currently stored
187  *    entry (in this case, no update will happen)
188  *  - a negative value on error
189  */
190 int
191 sdb_store_service(const char *hostname, const char *name,
192                 sdb_time_t last_update);
194 /*
195  * sdb_store_service_attr:
196  * Add/update a service's attribute in the store. If the attribute, identified
197  * by its key, already exists for the specified service, it will be updated to
198  * the specified value. If the references service (for the specified host)
199  * does not exist, an error will be reported. Any memory required for storing
200  * the entry will be allocated and managed by the store itself.
201  *
202  * Returns:
203  *  - 0 on success
204  *  - a positive value if the new entry is older than the currently stored
205  *    entry (in this case, no update will happen)
206  *  - a negative value on error
207  */
208 int
209 sdb_store_service_attr(const char *hostname, const char *service,
210                 const char *key, const sdb_data_t *value, sdb_time_t last_update);
212 /*
213  * A metric store describes how to access a metric's data.
214  */
215 typedef struct {
216         const char *type;
217         const char *id;
218 } sdb_metric_store_t;
220 /*
221  * sdb_store_metric:
222  * Add/update a metric in the store. If the metric, identified by its name,
223  * already exists for the specified host, it will be updated according to the
224  * specified 'metric' object. If the referenced host does not exist, an error
225  * will be reported. Else, a new entry will be created in the store. Any
226  * memory required for storing the entry will be allocated an managed by the
227  * store itself.
228  *
229  * If specified, the metric store describes where to access the metric's data.
230  *
231  * Returns:
232  *  - 0 on success
233  *  - a positive value if the new entry is older than the currently stored
234  *    entry (in this case, no update will happen)
235  *  - a negative value on error
236  */
237 int
238 sdb_store_metric(const char *hostname, const char *name,
239                 sdb_metric_store_t *store, sdb_time_t last_update);
241 /*
242  * sdb_store_metric_attr:
243  * Add/update a metric's attribute in the store. If the attribute, identified
244  * by its key, already exists for the specified metric, it will be updated to
245  * the specified value. If the references metric (for the specified host)
246  * does not exist, an error will be reported. Any memory required for storing
247  * the entry will be allocated and managed by the store itself.
248  *
249  * Returns:
250  *  - 0 on success
251  *  - a positive value if the new entry is older than the currently stored
252  *    entry (in this case, no update will happen)
253  *  - a negative value on error
254  */
255 int
256 sdb_store_metric_attr(const char *hostname, const char *metric,
257                 const char *key, const sdb_data_t *value, sdb_time_t last_update);
259 /*
260  * sdb_store_fetch_timeseries:
261  * Fetch the time-series described by the specified host's metric and
262  * serialize it as JSON into the provided string buffer.
263  *
264  * Returns:
265  *  - 0 on success
266  *  - a negative value else
267  */
268 int
269 sdb_store_fetch_timeseries(const char *hostname, const char *metric,
270                 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf);
272 /*
273  * sdb_store_get_field:
274  * Get the value of a stored object's queryable field. The caller is
275  * responsible for freeing any dynamically allocated memory possibly stored in
276  * the returned value. If 'res' is NULL, the function will return whether the
277  * field exists.
278  *
279  * Note: Retrieving the backend this way is not currently supported.
280  *
281  * Returns:
282  *  - 0 on success
283  *  - a negative value else
284  */
285 int
286 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
288 /*
289  * sdb_store_get_attr:
290  * Get the value of a stored object's attribute. The caller is responsible for
291  * freeing any dynamically allocated memory possibly stored in the returned
292  * value. If 'res' is NULL, the function will return whether the attribute
293  * exists. If specified, only attributes matching the filter will be
294  * considered.
295  *
296  * Returns:
297  *  - 0 if the attribute exists
298  *  - a negative value else
299  */
300 int
301 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
302                 sdb_store_matcher_t *filter);
304 /*
305  * sdb_store_expr_create:
306  * Creates an arithmetic expression implementing the specified operator on the
307  * specified left and right operand.
308  *
309  * Returns:
310  *  - an expression object on success
311  *  - NULL else
312  */
313 sdb_store_expr_t *
314 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
316 /*
317  * sdb_store_expr_fieldvalue:
318  * Creates an expression which evaluates to the value of the specified
319  * queryable field of a stored object.
320  *
321  * Returns:
322  *  - an expression object on success
323  *  - NULL else
324  */
325 sdb_store_expr_t *
326 sdb_store_expr_fieldvalue(int field);
328 /*
329  * sdb_store_expr_attrvalue:
330  * Creates an expression which evaluates to the value of the specified
331  * attribute of a stored object. Evaluates to a NULL value if the attribute
332  * does not exist.
333  *
334  * Returns:
335  *  - an expression object on success
336  *  - NULL else
337  */
338 sdb_store_expr_t *
339 sdb_store_expr_attrvalue(const char *name);
341 /*
342  * sdb_store_expr_constvalue:
343  * Creates an expression which evaluates to the specified constant value.
344  *
345  * Returns:
346  *  - an expression object on success
347  *  - NULL else
348  */
349 sdb_store_expr_t *
350 sdb_store_expr_constvalue(const sdb_data_t *value);
352 /*
353  * sdb_store_expr_eval:
354  * Evaluate an expression for the specified stored object and stores the
355  * result in 'res'. The result's value will be allocated dynamically if
356  * necessary and, thus, should be free'd by the caller (e.g. using
357  * sdb_data_free_datum). The object may be NULL, in which case the expression
358  * needs to evaluate to a constant value. If specified, only objects matching
359  * the filter will be used during the evaluation.
360  *
361  * Returns:
362  *  - 0 on success
363  *  - a negative value else
364  */
365 int
366 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
367                 sdb_data_t *res, sdb_store_matcher_t *filter);
369 /*
370  * sdb_store_isnull_matcher:
371  * Creates a matcher matching NULL values.
372  */
373 sdb_store_matcher_t *
374 sdb_store_isnull_matcher(sdb_store_expr_t *expr);
376 /*
377  * sdb_store_isnnull_matcher:
378  * Creates a matcher matching non-NULL values.
379  */
380 sdb_store_matcher_t *
381 sdb_store_isnnull_matcher(sdb_store_expr_t *expr);
383 /*
384  * sdb_store_any_matcher:
385  * Creates a matcher iterating over objects of the specified type. It matches
386  * if *any* of those objects match 'm'.
387  */
388 sdb_store_matcher_t *
389 sdb_store_any_matcher(int type, sdb_store_matcher_t *m);
391 /*
392  * sdb_store_all_matcher:
393  * Creates a matcher iterating over objects of the specified type. It matches
394  * if *all* of those objects match 'm'.
395  */
396 sdb_store_matcher_t *
397 sdb_store_all_matcher(int type, sdb_store_matcher_t *m);
399 /*
400  * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
401  * sdb_store_ge_matcher, sdb_store_gt_matcher:
402  * Create conditional matchers comparing the values of two expressions. The
403  * matcher matches if the left expression compres less than, less or equal
404  * than, equal to, not equal to, greater or equal than, or greater than the
405  * right expression.
406  */
407 sdb_store_matcher_t *
408 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
409 sdb_store_matcher_t *
410 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
411 sdb_store_matcher_t *
412 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
413 sdb_store_matcher_t *
414 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
415 sdb_store_matcher_t *
416 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
417 sdb_store_matcher_t *
418 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
420 /*
421  * sdb_store_in_matcher:
422  * Creates a matcher which matches if the right value evaluates to an array
423  * value and the left value is included in that array. See sdb_data_inarray
424  * for more details.
425  */
426 sdb_store_matcher_t *
427 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
429 /*
430  * sdb_store_regex_matcher:
431  * Creates a matcher which matches the string value the left expression
432  * evaluates to against the regular expression the right expression evaluates
433  * to. The right expression may either be a constant value regular expression
434  * or string or a dynamic value evaluating to a string. In the latter case,
435  * the string is compiled to a regex every time the matcher is executed.
436  */
437 sdb_store_matcher_t *
438 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
440 /*
441  * sdb_store_nregex_matcher:
442  * Creates a regex matcher just like sdb_store_regex_matcher except that it
443  * matches in case the regular expression does not match.
444  */
445 sdb_store_matcher_t *
446 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
448 /*
449  * sdb_store_matcher_op_cb:
450  * Callback constructing a matcher operator.
451  */
452 typedef sdb_store_matcher_t *(*sdb_store_matcher_op_cb)
453         (sdb_store_expr_t *, sdb_store_expr_t *);
455 /*
456  * sdb_store_parse_matcher_op:
457  * Parse a matcher operator and return a constructor for the respective
458  * matcher.
459  *
460  * Returns:
461  *  - matcher operator constructor on success
462  *  - NULL else
463  */
464 sdb_store_matcher_op_cb
465 sdb_store_parse_matcher_op(const char *op);
467 /*
468  * sdb_store_parse_object_type:
469  * Parse the type name of a stored object.
470  *
471  * Returns:
472  *  - the object type on success
473  *  - a negative value else
474  */
475 int
476 sdb_store_parse_object_type(const char *name);
478 /*
479  * sdb_store_parse_object_type_plural:
480  * Parse the type name (plural) of a stored object.
481  *
482  * Returns:
483  *  - the object type on success
484  *  - a negative value else
485  */
486 int
487 sdb_store_parse_object_type_plural(const char *name);
489 /*
490  * sdb_store_parse_field_name:
491  * Parse the name of a stored object's queryable field.
492  *
493  * Returns:
494  *  - the field id on success
495  *  - a negative value else
496  */
497 int
498 sdb_store_parse_field_name(const char *name);
500 /*
501  * sdb_store_dis_matcher:
502  * Creates a matcher matching the disjunction (logical OR) of two matchers.
503  */
504 sdb_store_matcher_t *
505 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
507 /*
508  * sdb_store_con_matcher:
509  * Creates a matcher matching the conjunction (logical AND) of two matchers.
510  */
511 sdb_store_matcher_t *
512 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
514 /*
515  * sdb_store_con_matcher::
516  * Creates a matcher matching the inverse (logical NOT) of a matcher.
517  */
518 sdb_store_matcher_t *
519 sdb_store_inv_matcher(sdb_store_matcher_t *m);
521 /*
522  * sdb_store_matcher_matches:
523  * Check whether the specified matcher matches the specified store object. If
524  * specified, the filter will be used to preselect objects for further
525  * evaluation. It is applied to any object that's used during the evaluation
526  * of the matcher. Only those objects matching the filter will be considered.
527  *
528  * Note that the filter is applied to all object types (hosts, service,
529  * metric, attribute). Thus, any object-specific matchers are mostly unsuited
530  * for this purpose and, if used, may result in unexpected behavior.
531  *
532  * Returns:
533  *  - 1 if the object matches
534  *  - 0 else
535  */
536 int
537 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
538                 sdb_store_matcher_t *filter);
540 /*
541  * sdb_store_lookup_cb:
542  * Lookup callback. It is called for each matching object when looking up data
543  * in the store passing on the lookup filter and the specified user-data. The
544  * lookup aborts early if the callback returns non-zero.
545  */
546 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj,
547                 sdb_store_matcher_t *filter, void *user_data);
549 /*
550  * sdb_store_scan:
551  * Look up objects of the specified type in the store. The specified callback
552  * function is called for each object in the store matching 'm'. The function
553  * performs a full scan of all objects stored in the database. If specified,
554  * the filter will be used to preselect objects for further evaluation. See
555  * the description of 'sdb_store_matcher_matches' for details.
556  *
557  * Returns:
558  *  - 0 on success
559  *  - a negative value else
560  */
561 int
562 sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
563                 sdb_store_lookup_cb cb, void *user_data);
565 /*
566  * Flags for serialization functions.
567  *
568  * By default, the full host object will be included in the serialized output.
569  * When specifying any of the flags, the respective information will be left
570  * out. The SKIP_EMPTY flags may be used to skip host objects entirely.
571  */
572 enum {
573         SDB_SKIP_ATTRIBUTES         = 1 << 0,
574         SDB_SKIP_SERVICES           = 1 << 1,
575         SDB_SKIP_METRICS            = 1 << 2,
576         SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 3,
578         SDB_SKIP_ALL                = (1 << 8) - 1,
580         /* skip hosts if they do not reference any services/metrics */
581         SDB_SKIP_EMPTY_SERVICES     = 1 << 8,
582         SDB_SKIP_EMPTY_METRICS      = 1 << 9,
583 };
585 /*
586  * sdb_store_tojson:
587  * Serialize the entire store to JSON and append the result to the specified
588  * buffer. If specified, only objects matching the filter will be included in
589  * the result (see sdb_store_host_tojson for details).
590  *
591  * Returns:
592  *  - 0 on success
593  *  - a negative value on error
594  */
595 int
596 sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags);
598 /*
599  * sdb_store_host_tojson:
600  * Serialize a host object to JSON and append the result to the specified
601  * buffer. If specified, only objects matching the filter will be included in
602  * the result. The filter is applied to each object individually and, thus,
603  * should not be of any object-type specific kind. The filter is never applied
604  * to the specified host object; the caller is responsible for this and for
605  * correctly handling skipped hosts.
606  *
607  * Returns:
608  *  - 0 on success
609  *  - a negative value on error
610  */
611 int
612 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf,
613                 sdb_store_matcher_t *filter, int flags);
615 #ifdef __cplusplus
616 } /* extern "C" */
617 #endif
619 #endif /* ! SDB_CORE_STORE_H */
621 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */