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