Code

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