Code

store: Let JSON serializers support (object) filters.
[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 "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  * Expressions specify arithmetic expressions.
178  *
179  * A expression object inherits from sdb_object_t and, thus, may safely be
180  * cast to a generic object.
181  */
182 struct sdb_store_expr;
183 typedef struct sdb_store_expr sdb_store_expr_t;
184 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
186 /*
187  * sdb_store_expr_create:
188  * Creates an arithmetic expression implementing the specified operator on the
189  * specified left and right operand.
190  *
191  * Returns:
192  *  - an expression object on success
193  *  - NULL else
194  */
195 sdb_store_expr_t *
196 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
198 /*
199  * sdb_store_expr_constvalue:
200  * Creates an expression which evaluates to the specified constant value.
201  *
202  * Returns:
203  *  - an expression object on success
204  *  - NULL else
205  */
206 sdb_store_expr_t *
207 sdb_store_expr_constvalue(const sdb_data_t *value);
209 /*
210  * sdb_store_expr_eval:
211  * Evaluate an expression and stores the result in 'res'. The result's value
212  * will be allocated dynamically if necessary and, thus, should be free'd by
213  * the caller (e.g. using sdb_data_free_datum);
214  *
215  * Returns:
216  *  - 0 on success
217  *  - a negative value else
218  */
219 int
220 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_data_t *res);
222 /*
223  * Conditionals may be used to lookup hosts from the store based on a
224  * conditional expression.
225  *
226  * A conditional object inherits from sdb_object_t and, thus, may safely be
227  * cast to a generic object.
228  */
229 struct sdb_store_cond;
230 typedef struct sdb_store_cond sdb_store_cond_t;
231 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
233 /*
234  * sdb_store_attr_cond:
235  * Creates a conditional based on attribute values. The value of stored
236  * attributes is compared against the value the expression evaluates to. See
237  * sdb_data_cmp for details about the comparison.
238  */
239 sdb_store_cond_t *
240 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr);
242 /*
243  * sdb_store_obj_cond:
244  * Creates a conditional based on queryable object fields. The respective
245  * field of *any* object type is compared against the value the expression
246  * evaluates to.
247  */
248 sdb_store_cond_t *
249 sdb_store_obj_cond(int field, sdb_store_expr_t *expr);
251 /*
252  * Store matchers may be used to lookup hosts from the store based on their
253  * various attributes. Service and attribute matchers are applied to a host's
254  * services and attributes and evaluate to true if *any* service or attribute
255  * matches.
256  *
257  * A store matcher object inherits from sdb_object_t and, thus, may safely be
258  * cast to a generic object.
259  */
260 struct sdb_store_matcher;
261 typedef struct sdb_store_matcher sdb_store_matcher_t;
262 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
264 /*
265  * sdb_store_name_matcher:
266  * Creates a matcher matching by the specified object type's name. If 're' is
267  * true, the specified name is treated as a POSIX extended regular expression.
268  * Else, the exact name has to match (case-insensitive).
269  */
270 sdb_store_matcher_t *
271 sdb_store_name_matcher(int type, const char *name, _Bool re);
273 /*
274  * sdb_store_attr_matcher:
275  * Creates a matcher matching attributes based on their value. If 're' is
276  * true, the specified name is treated as a POSIX extended regular expression.
277  * Else, the exact name has to match (case-insensitive).
278  */
279 sdb_store_matcher_t *
280 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
282 /*
283  * sdb_store_isnull_matcher:
284  * Creates a matcher matching "missing" attributes.
285  */
286 sdb_store_matcher_t *
287 sdb_store_isnull_matcher(const char *attr_name);
289 /*
290  * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
291  * sdb_store_ge_matcher, sdb_store_gt_matcher:
292  * Creates a matcher based on a conditional. The matcher matches objects for
293  * which the conditional evaluates the object to compare less than, less or
294  * equal, equal, greater or equal, or greater than the conditional's value
295  * repsectively.
296  */
297 sdb_store_matcher_t *
298 sdb_store_lt_matcher(sdb_store_cond_t *cond);
299 sdb_store_matcher_t *
300 sdb_store_le_matcher(sdb_store_cond_t *cond);
301 sdb_store_matcher_t *
302 sdb_store_eq_matcher(sdb_store_cond_t *cond);
303 sdb_store_matcher_t *
304 sdb_store_ge_matcher(sdb_store_cond_t *cond);
305 sdb_store_matcher_t *
306 sdb_store_gt_matcher(sdb_store_cond_t *cond);
308 /*
309  * sdb_store_matcher_parse_cmp:
310  * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
311  *
312  * Returns:
313  *  - a matcher object on success
314  *  - NULL else
315  */
316 sdb_store_matcher_t *
317 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
318                 const char *op, sdb_store_expr_t *expr);
320 /*
321  * sdb_store_dis_matcher:
322  * Creates a matcher matching the disjunction (logical OR) of two matchers.
323  */
324 sdb_store_matcher_t *
325 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
327 /*
328  * sdb_store_con_matcher:
329  * Creates a matcher matching the conjunction (logical AND) of two matchers.
330  */
331 sdb_store_matcher_t *
332 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
334 /*
335  * sdb_store_con_matcher::
336  * Creates a matcher matching the inverse (logical NOT) of a matcher.
337  */
338 sdb_store_matcher_t *
339 sdb_store_inv_matcher(sdb_store_matcher_t *m);
341 /*
342  * sdb_store_matcher_matches:
343  * Check whether the specified matcher matches the specified store object. If
344  * specified, the filter will be used to preselect objects for further
345  * evaluation. It is applied to any object that's used during the evaluation
346  * of the matcher. Only those objects matching the filter will be considered.
347  *
348  * Note that the filter is applied to all object types (hosts, service,
349  * attribute). Thus, any object-specific matchers are mostly unsuited for this
350  * purpose and, if used, may result in unexpected behavior.
351  *
352  * Returns:
353  *  - 1 if the object matches
354  *  - 0 else
355  */
356 int
357 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
358                 sdb_store_matcher_t *filter);
360 /*
361  * sdb_store_matcher_tostring:
362  * Format a matcher object as string. This is meant for logging or debugging
363  * purposes.
364  */
365 char *
366 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
368 /*
369  * sdb_store_lookup_cb:
370  * Lookup callback. It is called for each matching object when looking up data
371  * in the store. The lookup aborts if the callback returns non-zero.
372  */
373 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
375 /*
376  * sdb_store_scan:
377  * Look up objects in the store. The specified callback function is called for
378  * each object in the store matching 'm'. The function performs a full scan of
379  * all hosts stored in the database. If specified, the filter will be used to
380  * preselect objects for further evaluation. See the description of
381  * 'sdb_store_matcher_matches' for details.
382  *
383  * Returns:
384  *  - 0 on success
385  *  - a negative value else
386  */
387 int
388 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
389                 sdb_store_lookup_cb cb, void *user_data);
391 /*
392  * Flags for serialization functions.
393  *
394  * By default, the full object will be included in the serialized output. When
395  * specifying any of the flags, the respective information will be left out.
396  */
397 enum {
398         SDB_SKIP_ATTRIBUTES         = 1 << 0,
399         SDB_SKIP_SERVICES           = 1 << 1,
400         SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 2,
402         SDB_SKIP_ALL                = 0xffff,
403 };
405 /*
406  * sdb_store_tojson:
407  * Serialize the entire store to JSON and append the result to the specified
408  * buffer. If specified, only objects matching the filter will be included in
409  * the result (see sdb_store_host_tojson for details).
410  *
411  * Returns:
412  *  - 0 on success
413  *  - a negative value on error
414  */
415 int
416 sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags);
418 /*
419  * sdb_store_host_tojson:
420  * Serialize a host object to JSON and append the result to the specified
421  * buffer. If specified, only objects matching the filter will be included in
422  * the result. The filter is applied to each object individually and, thus,
423  * should not be of any object-type specific kind. If the filter rejects the
424  * host object, the function returns success but leaves the buffer unmodified.
425  *
426  * Returns:
427  *  - 0 on success
428  *  - a negative value on error
429  */
430 int
431 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf,
432                 sdb_store_matcher_t *filter, int flags);
434 /*
435  * sdb_store_iter_cb:
436  * Store iterator callback. Iteration stops if the callback returns non-zero.
437  */
438 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
440 /*
441  * sdb_store_iterate:
442  * Iterate the entire store, calling the specified callback for each object.
443  * The user_data pointer is passed on to each call of the callback.
444  *
445  * Returns:
446  *  - 0 on success
447  *  - a negative value else
448  */
449 int
450 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
452 #ifdef __cplusplus
453 } /* extern "C" */
454 #endif
456 #endif /* ! SDB_CORE_STORE_H */
458 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */