Code

store: Added quaryable field ‘name’.
[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  * 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.
245  *
246  * Note: Retrieving the backend this way is not currently supported.
247  *
248  * Returns:
249  *  - 0 on success
250  *  - a negative value else
251  */
252 int
253 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
255 /*
256  * Expressions specify arithmetic expressions.
257  *
258  * A expression object inherits from sdb_object_t and, thus, may safely be
259  * cast to a generic object.
260  */
261 struct sdb_store_expr;
262 typedef struct sdb_store_expr sdb_store_expr_t;
263 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
265 /*
266  * sdb_store_expr_create:
267  * Creates an arithmetic expression implementing the specified operator on the
268  * specified left and right operand.
269  *
270  * Returns:
271  *  - an expression object on success
272  *  - NULL else
273  */
274 sdb_store_expr_t *
275 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
277 /*
278  * sdb_store_expr_fieldvalue:
279  * Creates an expression which evaluates to the value of the specified
280  * queryable field of a stored object.
281  *
282  * Returns:
283  *  - an expression object on success
284  *  - NULL else
285  */
286 sdb_store_expr_t *
287 sdb_store_expr_fieldvalue(int field);
289 /*
290  * sdb_store_expr_constvalue:
291  * Creates an expression which evaluates to the specified constant value.
292  *
293  * Returns:
294  *  - an expression object on success
295  *  - NULL else
296  */
297 sdb_store_expr_t *
298 sdb_store_expr_constvalue(const sdb_data_t *value);
300 /*
301  * sdb_store_expr_eval:
302  * Evaluate an expression for the specified stored object and stores the
303  * result in 'res'. The result's value will be allocated dynamically if
304  * necessary and, thus, should be free'd by the caller (e.g. using
305  * sdb_data_free_datum). The object may be NULL, in which case the expression
306  * needs to evaluate to a constant value.
307  *
308  * Returns:
309  *  - 0 on success
310  *  - a negative value else
311  */
312 int
313 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
314                 sdb_data_t *res);
316 /*
317  * Conditionals may be used to lookup hosts from the store based on a
318  * conditional expression.
319  *
320  * A conditional object inherits from sdb_object_t and, thus, may safely be
321  * cast to a generic object.
322  */
323 struct sdb_store_cond;
324 typedef struct sdb_store_cond sdb_store_cond_t;
325 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
327 /*
328  * sdb_store_attr_cond:
329  * Creates a conditional based on attribute values. The value of stored
330  * attributes is compared against the value the expression evaluates to. See
331  * sdb_data_cmp for details about the comparison.
332  */
333 sdb_store_cond_t *
334 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr);
336 /*
337  * sdb_store_obj_cond:
338  * Creates a conditional based on queryable object fields. The respective
339  * field of *any* object type is compared against the value the expression
340  * evaluates to.
341  */
342 sdb_store_cond_t *
343 sdb_store_obj_cond(int field, sdb_store_expr_t *expr);
345 /*
346  * Store matchers may be used to lookup hosts from the store based on their
347  * various attributes. Service and attribute matchers are applied to a host's
348  * services and attributes and evaluate to true if *any* service or attribute
349  * matches.
350  *
351  * A store matcher object inherits from sdb_object_t and, thus, may safely be
352  * cast to a generic object.
353  */
354 struct sdb_store_matcher;
355 typedef struct sdb_store_matcher sdb_store_matcher_t;
356 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
358 /*
359  * sdb_store_name_matcher:
360  * Creates a matcher matching by the specified object type's name. If 're' is
361  * true, the specified name is treated as a POSIX extended regular expression.
362  * Else, the exact name has to match (case-insensitive).
363  */
364 sdb_store_matcher_t *
365 sdb_store_name_matcher(int type, const char *name, _Bool re);
367 /*
368  * sdb_store_attr_matcher:
369  * Creates a matcher matching attributes based on their value. If 're' is
370  * true, the specified name is treated as a POSIX extended regular expression.
371  * Else, the exact name has to match (case-insensitive).
372  */
373 sdb_store_matcher_t *
374 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
376 /*
377  * sdb_store_isnull_matcher:
378  * Creates a matcher matching "missing" attributes.
379  */
380 sdb_store_matcher_t *
381 sdb_store_isnull_matcher(const char *attr_name);
383 /*
384  * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
385  * sdb_store_ge_matcher, sdb_store_gt_matcher:
386  * Creates a matcher based on a conditional. The matcher matches objects for
387  * which the conditional evaluates the object to compare less than, less or
388  * equal, equal, greater or equal, or greater than the conditional's value
389  * repsectively.
390  */
391 sdb_store_matcher_t *
392 sdb_store_lt_matcher(sdb_store_cond_t *cond);
393 sdb_store_matcher_t *
394 sdb_store_le_matcher(sdb_store_cond_t *cond);
395 sdb_store_matcher_t *
396 sdb_store_eq_matcher(sdb_store_cond_t *cond);
397 sdb_store_matcher_t *
398 sdb_store_ge_matcher(sdb_store_cond_t *cond);
399 sdb_store_matcher_t *
400 sdb_store_gt_matcher(sdb_store_cond_t *cond);
402 /*
403  * sdb_store_parse_object_type_plural:
404  * Parse the type name (plural) of a stored object.
405  *
406  * Returns:
407  *  - the object type on success
408  *  - a negative value else
409  */
410 int
411 sdb_store_parse_object_type_plural(const char *name);
413 /*
414  * sdb_store_parse_field_name:
415  * Parse the name of a stored object's queryable field.
416  *
417  * Returns:
418  *  - the field id on success
419  *  - a negative value else
420  */
421 int
422 sdb_store_parse_field_name(const char *name);
424 /*
425  * sdb_store_matcher_parse_cmp:
426  * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
427  *
428  * Returns:
429  *  - a matcher object on success
430  *  - NULL else
431  */
432 sdb_store_matcher_t *
433 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
434                 const char *op, sdb_store_expr_t *expr);
436 /*
437  * sdb_store_matcher_parse_field_cmp:
438  * Parse a simple compare expression for queryable object fields (<field> <op>
439  * <expression>).
440  *
441  * Returns:
442  *  - a matcher object on success
443  *  - NULL else
444  */
445 sdb_store_matcher_t *
446 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
447                 sdb_store_expr_t *expr);
449 /*
450  * sdb_store_dis_matcher:
451  * Creates a matcher matching the disjunction (logical OR) of two matchers.
452  */
453 sdb_store_matcher_t *
454 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
456 /*
457  * sdb_store_con_matcher:
458  * Creates a matcher matching the conjunction (logical AND) of two matchers.
459  */
460 sdb_store_matcher_t *
461 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
463 /*
464  * sdb_store_con_matcher::
465  * Creates a matcher matching the inverse (logical NOT) of a matcher.
466  */
467 sdb_store_matcher_t *
468 sdb_store_inv_matcher(sdb_store_matcher_t *m);
470 /*
471  * sdb_store_matcher_matches:
472  * Check whether the specified matcher matches the specified store object. If
473  * specified, the filter will be used to preselect objects for further
474  * evaluation. It is applied to any object that's used during the evaluation
475  * of the matcher. Only those objects matching the filter will be considered.
476  *
477  * Note that the filter is applied to all object types (hosts, service,
478  * metric, attribute). Thus, any object-specific matchers are mostly unsuited
479  * for this purpose and, if used, may result in unexpected behavior.
480  *
481  * Returns:
482  *  - 1 if the object matches
483  *  - 0 else
484  */
485 int
486 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
487                 sdb_store_matcher_t *filter);
489 /*
490  * sdb_store_matcher_tostring:
491  * Format a matcher object as string. This is meant for logging or debugging
492  * purposes.
493  */
494 char *
495 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
497 /*
498  * sdb_store_lookup_cb:
499  * Lookup callback. It is called for each matching object when looking up data
500  * in the store. The lookup aborts if the callback returns non-zero.
501  */
502 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
504 /*
505  * sdb_store_scan:
506  * Look up objects in the store. The specified callback function is called for
507  * each object in the store matching 'm'. The function performs a full scan of
508  * all hosts stored in the database. If specified, the filter will be used to
509  * preselect objects for further evaluation. See the description of
510  * 'sdb_store_matcher_matches' for details.
511  *
512  * Returns:
513  *  - 0 on success
514  *  - a negative value else
515  */
516 int
517 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
518                 sdb_store_lookup_cb cb, void *user_data);
520 /*
521  * Flags for serialization functions.
522  *
523  * By default, the full host object will be included in the serialized output.
524  * When specifying any of the flags, the respective information will be left
525  * out. The SKIP_EMPTY flags may be used to skip host objects entirely.
526  */
527 enum {
528         SDB_SKIP_ATTRIBUTES         = 1 << 0,
529         SDB_SKIP_SERVICES           = 1 << 1,
530         SDB_SKIP_METRICS            = 1 << 2,
531         SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 3,
533         SDB_SKIP_ALL                = (1 << 8) - 1,
535         /* skip hosts if they do not reference any services/metrics */
536         SDB_SKIP_EMPTY_SERVICES     = 1 << 8,
537         SDB_SKIP_EMPTY_METRICS      = 1 << 9,
538 };
540 /*
541  * sdb_store_tojson:
542  * Serialize the entire store to JSON and append the result to the specified
543  * buffer. If specified, only objects matching the filter will be included in
544  * the result (see sdb_store_host_tojson for details).
545  *
546  * Returns:
547  *  - 0 on success
548  *  - a negative value on error
549  */
550 int
551 sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags);
553 /*
554  * sdb_store_host_tojson:
555  * Serialize a host object to JSON and append the result to the specified
556  * buffer. If specified, only objects matching the filter will be included in
557  * the result. The filter is applied to each object individually and, thus,
558  * should not be of any object-type specific kind. The filter is never applied
559  * to the specified host object; the caller is responsible for this and for
560  * correctly handling skipped hosts.
561  *
562  * Returns:
563  *  - 0 on success
564  *  - a negative value on error
565  */
566 int
567 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf,
568                 sdb_store_matcher_t *filter, int flags);
570 /*
571  * sdb_store_iter_cb:
572  * Store iterator callback. Iteration stops if the callback returns non-zero.
573  */
574 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
576 /*
577  * sdb_store_iterate:
578  * Iterate the entire store, calling the specified callback for each object.
579  * The user_data pointer is passed on to each call of the callback.
580  *
581  * Returns:
582  *  - 0 on success
583  *  - a negative value else
584  */
585 int
586 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
588 #ifdef __cplusplus
589 } /* extern "C" */
590 #endif
592 #endif /* ! SDB_CORE_STORE_H */
594 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */