169d1128ac03d1fcef4c809f47984ef57f61317b
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.
148 *
149 * Returns:
150 * - 0 on success
151 * - a positive value if the new entry is older than the currently stored
152 * entry (in this case, no update will happen)
153 * - a negative value on error
154 */
155 int
156 sdb_store_service(const char *hostname, const char *name,
157 sdb_time_t last_update);
159 /*
160 * sdb_store_service_attr:
161 * Add/update a service's attribute in the store. If the attribute, identified
162 * by its key, already exists for the specified service, it will be updated to
163 * the specified value. If the references service (for the specified host)
164 * does not exist, an error will be reported. Any memory required for storing
165 * the entry will be allocated and managed by the store itself.
166 *
167 * Returns:
168 * - 0 on success
169 * - a positive value if the new entry is older than the currently stored
170 * entry (in this case, no update will happen)
171 * - a negative value on error
172 */
173 int
174 sdb_store_service_attr(const char *hostname, const char *service,
175 const char *key, const sdb_data_t *value, sdb_time_t last_update);
177 /*
178 * sdb_store_metric:
179 * Add/update a metric in the store. If the metric, identified by its name,
180 * already exists for the specified host, it will be updated according to the
181 * specified 'metric' object. If the referenced host does not exist, an error
182 * will be reported. Else, a new entry will be created in the store. Any
183 * memory required for storing the entry will be allocated an managed by the
184 * store itself.
185 *
186 * Returns:
187 * - 0 on success
188 * - a positive value if the new entry is older than the currently stored
189 * entry (in this case, no update will happen)
190 * - a negative value on error
191 */
192 int
193 sdb_store_metric(const char *hostname, const char *name,
194 sdb_time_t last_update);
196 /*
197 * sdb_store_metric_attr:
198 * Add/update a metric's attribute in the store. If the attribute, identified
199 * by its key, already exists for the specified metric, it will be updated to
200 * the specified value. If the references metric (for the specified host)
201 * does not exist, an error will be reported. Any memory required for storing
202 * the entry will be allocated and managed by the store itself.
203 *
204 * Returns:
205 * - 0 on success
206 * - a positive value if the new entry is older than the currently stored
207 * entry (in this case, no update will happen)
208 * - a negative value on error
209 */
210 int
211 sdb_store_metric_attr(const char *hostname, const char *metric,
212 const char *key, const sdb_data_t *value, sdb_time_t last_update);
214 /*
215 * sdb_store_get_field:
216 * Get the value of a stored object's queryable field. The caller is
217 * responsible for freeing any dynamically allocated memory possibly stored in
218 * the returned value.
219 *
220 * Note: Retrieving the backend this way is not currently supported.
221 *
222 * Returns:
223 * - 0 on success
224 * - a negative value else
225 */
226 int
227 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
229 /*
230 * Expressions specify arithmetic expressions.
231 *
232 * A expression object inherits from sdb_object_t and, thus, may safely be
233 * cast to a generic object.
234 */
235 struct sdb_store_expr;
236 typedef struct sdb_store_expr sdb_store_expr_t;
237 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
239 /*
240 * sdb_store_expr_create:
241 * Creates an arithmetic expression implementing the specified operator on the
242 * specified left and right operand.
243 *
244 * Returns:
245 * - an expression object on success
246 * - NULL else
247 */
248 sdb_store_expr_t *
249 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
251 /*
252 * sdb_store_expr_fieldvalue:
253 * Creates an expression which evaluates to the value of the specified
254 * queryable field of a stored object.
255 *
256 * Returns:
257 * - an expression object on success
258 * - NULL else
259 */
260 sdb_store_expr_t *
261 sdb_store_expr_fieldvalue(int field);
263 /*
264 * sdb_store_expr_constvalue:
265 * Creates an expression which evaluates to the specified constant value.
266 *
267 * Returns:
268 * - an expression object on success
269 * - NULL else
270 */
271 sdb_store_expr_t *
272 sdb_store_expr_constvalue(const sdb_data_t *value);
274 /*
275 * sdb_store_expr_eval:
276 * Evaluate an expression for the specified stored object and stores the
277 * result in 'res'. The result's value will be allocated dynamically if
278 * necessary and, thus, should be free'd by the caller (e.g. using
279 * sdb_data_free_datum). The object may be NULL, in which case the expression
280 * needs to evaluate to a constant value.
281 *
282 * Returns:
283 * - 0 on success
284 * - a negative value else
285 */
286 int
287 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
288 sdb_data_t *res);
290 /*
291 * Conditionals may be used to lookup hosts from the store based on a
292 * conditional expression.
293 *
294 * A conditional object inherits from sdb_object_t and, thus, may safely be
295 * cast to a generic object.
296 */
297 struct sdb_store_cond;
298 typedef struct sdb_store_cond sdb_store_cond_t;
299 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
301 /*
302 * sdb_store_attr_cond:
303 * Creates a conditional based on attribute values. The value of stored
304 * attributes is compared against the value the expression evaluates to. See
305 * sdb_data_cmp for details about the comparison.
306 */
307 sdb_store_cond_t *
308 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr);
310 /*
311 * sdb_store_obj_cond:
312 * Creates a conditional based on queryable object fields. The respective
313 * field of *any* object type is compared against the value the expression
314 * evaluates to.
315 */
316 sdb_store_cond_t *
317 sdb_store_obj_cond(int field, sdb_store_expr_t *expr);
319 /*
320 * Store matchers may be used to lookup hosts from the store based on their
321 * various attributes. Service and attribute matchers are applied to a host's
322 * services and attributes and evaluate to true if *any* service or attribute
323 * matches.
324 *
325 * A store matcher object inherits from sdb_object_t and, thus, may safely be
326 * cast to a generic object.
327 */
328 struct sdb_store_matcher;
329 typedef struct sdb_store_matcher sdb_store_matcher_t;
330 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
332 /*
333 * sdb_store_name_matcher:
334 * Creates a matcher matching by the specified object type's name. If 're' is
335 * true, the specified name is treated as a POSIX extended regular expression.
336 * Else, the exact name has to match (case-insensitive).
337 */
338 sdb_store_matcher_t *
339 sdb_store_name_matcher(int type, const char *name, _Bool re);
341 /*
342 * sdb_store_attr_matcher:
343 * Creates a matcher matching attributes based on their value. If 're' is
344 * true, the specified name is treated as a POSIX extended regular expression.
345 * Else, the exact name has to match (case-insensitive).
346 */
347 sdb_store_matcher_t *
348 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
350 /*
351 * sdb_store_isnull_matcher:
352 * Creates a matcher matching "missing" attributes.
353 */
354 sdb_store_matcher_t *
355 sdb_store_isnull_matcher(const char *attr_name);
357 /*
358 * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
359 * sdb_store_ge_matcher, sdb_store_gt_matcher:
360 * Creates a matcher based on a conditional. The matcher matches objects for
361 * which the conditional evaluates the object to compare less than, less or
362 * equal, equal, greater or equal, or greater than the conditional's value
363 * repsectively.
364 */
365 sdb_store_matcher_t *
366 sdb_store_lt_matcher(sdb_store_cond_t *cond);
367 sdb_store_matcher_t *
368 sdb_store_le_matcher(sdb_store_cond_t *cond);
369 sdb_store_matcher_t *
370 sdb_store_eq_matcher(sdb_store_cond_t *cond);
371 sdb_store_matcher_t *
372 sdb_store_ge_matcher(sdb_store_cond_t *cond);
373 sdb_store_matcher_t *
374 sdb_store_gt_matcher(sdb_store_cond_t *cond);
376 /*
377 * sdb_store_parse_field_name:
378 * Parse the name of a stored object's queryable field.
379 *
380 * Returns:
381 * - the field id on success
382 * - a negative value else
383 */
384 int
385 sdb_store_parse_field_name(const char *name);
387 /*
388 * sdb_store_matcher_parse_cmp:
389 * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
390 *
391 * Returns:
392 * - a matcher object on success
393 * - NULL else
394 */
395 sdb_store_matcher_t *
396 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
397 const char *op, sdb_store_expr_t *expr);
399 /*
400 * sdb_store_matcher_parse_field_cmp:
401 * Parse a simple compare expression for queryable object fields (<field> <op>
402 * <expression>).
403 *
404 * Returns:
405 * - a matcher object on success
406 * - NULL else
407 */
408 sdb_store_matcher_t *
409 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
410 sdb_store_expr_t *expr);
412 /*
413 * sdb_store_dis_matcher:
414 * Creates a matcher matching the disjunction (logical OR) of two matchers.
415 */
416 sdb_store_matcher_t *
417 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
419 /*
420 * sdb_store_con_matcher:
421 * Creates a matcher matching the conjunction (logical AND) of two matchers.
422 */
423 sdb_store_matcher_t *
424 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
426 /*
427 * sdb_store_con_matcher::
428 * Creates a matcher matching the inverse (logical NOT) of a matcher.
429 */
430 sdb_store_matcher_t *
431 sdb_store_inv_matcher(sdb_store_matcher_t *m);
433 /*
434 * sdb_store_matcher_matches:
435 * Check whether the specified matcher matches the specified store object. If
436 * specified, the filter will be used to preselect objects for further
437 * evaluation. It is applied to any object that's used during the evaluation
438 * of the matcher. Only those objects matching the filter will be considered.
439 *
440 * Note that the filter is applied to all object types (hosts, service,
441 * metric, attribute). Thus, any object-specific matchers are mostly unsuited
442 * for this purpose and, if used, may result in unexpected behavior.
443 *
444 * Returns:
445 * - 1 if the object matches
446 * - 0 else
447 */
448 int
449 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
450 sdb_store_matcher_t *filter);
452 /*
453 * sdb_store_matcher_tostring:
454 * Format a matcher object as string. This is meant for logging or debugging
455 * purposes.
456 */
457 char *
458 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
460 /*
461 * sdb_store_lookup_cb:
462 * Lookup callback. It is called for each matching object when looking up data
463 * in the store. The lookup aborts if the callback returns non-zero.
464 */
465 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
467 /*
468 * sdb_store_scan:
469 * Look up objects in the store. The specified callback function is called for
470 * each object in the store matching 'm'. The function performs a full scan of
471 * all hosts stored in the database. If specified, the filter will be used to
472 * preselect objects for further evaluation. See the description of
473 * 'sdb_store_matcher_matches' for details.
474 *
475 * Returns:
476 * - 0 on success
477 * - a negative value else
478 */
479 int
480 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
481 sdb_store_lookup_cb cb, void *user_data);
483 /*
484 * Flags for serialization functions.
485 *
486 * By default, the full object will be included in the serialized output. When
487 * specifying any of the flags, the respective information will be left out.
488 */
489 enum {
490 SDB_SKIP_ATTRIBUTES = 1 << 0,
491 SDB_SKIP_SERVICES = 1 << 1,
492 SDB_SKIP_METRICS = 1 << 2,
493 SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 3,
495 SDB_SKIP_ALL = 0xffff,
496 };
498 /*
499 * sdb_store_tojson:
500 * Serialize the entire store to JSON and append the result to the specified
501 * buffer. If specified, only objects matching the filter will be included in
502 * the result (see sdb_store_host_tojson for details).
503 *
504 * Returns:
505 * - 0 on success
506 * - a negative value on error
507 */
508 int
509 sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags);
511 /*
512 * sdb_store_host_tojson:
513 * Serialize a host object to JSON and append the result to the specified
514 * buffer. If specified, only objects matching the filter will be included in
515 * the result. The filter is applied to each object individually and, thus,
516 * should not be of any object-type specific kind. The filter is never applied
517 * to the specified host object; the caller is responsible for this and for
518 * correctly handling skipped hosts.
519 *
520 * Returns:
521 * - 0 on success
522 * - a negative value on error
523 */
524 int
525 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf,
526 sdb_store_matcher_t *filter, int flags);
528 /*
529 * sdb_store_iter_cb:
530 * Store iterator callback. Iteration stops if the callback returns non-zero.
531 */
532 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
534 /*
535 * sdb_store_iterate:
536 * Iterate the entire store, calling the specified callback for each object.
537 * The user_data pointer is passed on to each call of the callback.
538 *
539 * Returns:
540 * - 0 on success
541 * - a negative value else
542 */
543 int
544 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
546 #ifdef __cplusplus
547 } /* extern "C" */
548 #endif
550 #endif /* ! SDB_CORE_STORE_H */
552 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */