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