Code

store, frontend: Let lookups support arbitrary expressions for comparison.
[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")
57 /*
58  * sdb_store_obj_t represents the super-class of any object stored in the
59  * database. It inherits from sdb_object_t and may safely be cast to a generic
60  * object to access its name.
61  */
62 struct sdb_store_obj;
63 typedef struct sdb_store_obj sdb_store_obj_t;
65 /*
66  * sdb_store_clear:
67  * Clear the entire store and remove all stored objects.
68  */
69 void
70 sdb_store_clear(void);
72 /*
73  * sdb_store_host:
74  * Add/update a host in the store. If the host, identified by its
75  * canonicalized name, already exists, it will be updated according to the
76  * specified name and timestamp. Else, a new entry will be created in the
77  * store. Any memory required for storing the entry will be allocated an
78  * managed by the store itself.
79  *
80  * Returns:
81  *  - 0 on success
82  *  - a positive value if the new entry is older than the currently stored
83  *    entry (in this case, no update will happen)
84  *  - a negative value on error
85  */
86 int
87 sdb_store_host(const char *name, sdb_time_t last_update);
89 /*
90  * sdb_store_has_host:
91  * sdb_store_get_host:
92  * Query the store for a host by its (canonicalized) name.
93  *
94  * sdb_store_get_host increments the ref count of the host object. The caller
95  * needs to deref it when no longer using it.
96  */
97 _Bool
98 sdb_store_has_host(const char *name);
100 sdb_store_obj_t *
101 sdb_store_get_host(const char *name);
103 /*
104  * sdb_store_attribute:
105  * Add/update a host's attribute in the store. If the attribute, identified by
106  * its key, already exists for the specified host, it will be updated to the
107  * specified values. If the referenced host does not exist, an error will be
108  * reported. Else, a new entry will be created in the store. Any memory
109  * required for storing the entry will be allocated and managed by the store
110  * itself.
111  *
112  * Returns:
113  *  - 0 on success
114  *  - a positive value if the new entry is older than the currently stored
115  *    entry (in this case, no update will happen)
116  *  - a negative value on error
117  */
118 int
119 sdb_store_attribute(const char *hostname,
120                 const char *key, const sdb_data_t *value,
121                 sdb_time_t last_update);
123 /*
124  * sdb_store_service:
125  * Add/update a service in the store. If the service, identified by its name,
126  * already exists for the specified host, it will be updated according to the
127  * specified 'service' object. If the referenced host does not exist, an error
128  * will be reported. Else, a new entry will be created in the store. Any
129  * memory required for storing the entry will be allocated an managed by the
130  * store itself. The specified service-object will not be referenced or
131  * further accessed.
132  *
133  * Returns:
134  *  - 0 on success
135  *  - a positive value if the new entry is older than the currently stored
136  *    entry (in this case, no update will happen)
137  *  - a negative value on error
138  */
139 int
140 sdb_store_service(const char *hostname, const char *name,
141                 sdb_time_t last_update);
143 /*
144  * sdb_store_service_attr:
145  * Add/update a service's attribute in the store. If the attribute, identified
146  * by its key, already exists for the specified service, it will be updated to
147  * the specified value. If the references service (for the specified host)
148  * does not exist, an error will be reported. Any memory required for storing
149  * the entry will be allocated and managed by the store itself.
150  *
151  * Returns:
152  *  - 0 on success
153  *  - a positive value if the new entry is older than the currently stored
154  *    entry (in this case, no update will happen)
155  *  - a negative value on error
156  */
157 int
158 sdb_store_service_attr(const char *hostname, const char *service,
159                 const char *key, const sdb_data_t *value, sdb_time_t last_update);
161 /*
162  * Expressions specify arithmetic expressions.
163  *
164  * A expression object inherits from sdb_object_t and, thus, may safely be
165  * cast to a generic object.
166  */
167 struct sdb_store_expr;
168 typedef struct sdb_store_expr sdb_store_expr_t;
169 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
171 /*
172  * sdb_store_expr_create:
173  * Creates an arithmetic expression implementing the specified operator on the
174  * specified left and right operand.
175  *
176  * Returns:
177  *  - an expression object on success
178  *  - NULL else
179  */
180 sdb_store_expr_t *
181 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
183 /*
184  * sdb_store_expr_constvalue:
185  * Creates an expression which evaluates to the specified constant value.
186  *
187  * Returns:
188  *  - an expression object on success
189  *  - NULL else
190  */
191 sdb_store_expr_t *
192 sdb_store_expr_constvalue(const sdb_data_t *value);
194 /*
195  * sdb_store_expr_eval:
196  * Evaluate an expression and stores the result in 'res'. The result's value
197  * will be allocated dynamically if necessary and, thus, should be free'd by
198  * the caller (e.g. using sdb_data_free_datum);
199  *
200  * Returns:
201  *  - 0 on success
202  *  - a negative value else
203  */
204 int
205 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_data_t *res);
207 /*
208  * Conditionals may be used to lookup hosts from the store based on a
209  * conditional expression.
210  *
211  * A conditional object inherits from sdb_object_t and, thus, may safely be
212  * cast to a generic object.
213  */
214 struct sdb_store_cond;
215 typedef struct sdb_store_cond sdb_store_cond_t;
216 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
218 /*
219  * sdb_store_attr_cond:
220  * Creates a conditional based on attribute values. The value of stored
221  * attributes is compared against the value the expression evaluates to. See
222  * sdb_data_cmp for details about the comparison.
223  */
224 sdb_store_cond_t *
225 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr);
227 /*
228  * Store matchers may be used to lookup hosts from the store based on their
229  * various attributes. Service and attribute matchers are applied to a host's
230  * services and attributes and evaluate to true if *any* service or attribute
231  * matches.
232  *
233  * A store matcher object inherits from sdb_object_t and, thus, may safely be
234  * cast to a generic object.
235  */
236 struct sdb_store_matcher;
237 typedef struct sdb_store_matcher sdb_store_matcher_t;
238 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
240 /*
241  * sdb_store_name_matcher:
242  * Creates a matcher matching by the specified object type's name. If 're' is
243  * true, the specified name is treated as a POSIX extended regular expression.
244  * Else, the exact name has to match (case-insensitive).
245  */
246 sdb_store_matcher_t *
247 sdb_store_name_matcher(int type, const char *name, _Bool re);
249 /*
250  * sdb_store_attr_matcher:
251  * Creates a matcher matching attributes based on their value. If 're' is
252  * true, the specified name is treated as a POSIX extended regular expression.
253  * Else, the exact name has to match (case-insensitive).
254  */
255 sdb_store_matcher_t *
256 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
258 /*
259  * sdb_store_isnull_matcher:
260  * Creates a matcher matching "missing" attributes.
261  */
262 sdb_store_matcher_t *
263 sdb_store_isnull_matcher(const char *attr_name);
265 /*
266  * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
267  * sdb_store_ge_matcher, sdb_store_gt_matcher:
268  * Creates a matcher based on a conditional. The matcher matches objects for
269  * which the conditional evaluates the object to compare less than, less or
270  * equal, equal, greater or equal, or greater than the conditional's value
271  * repsectively.
272  */
273 sdb_store_matcher_t *
274 sdb_store_lt_matcher(sdb_store_cond_t *cond);
275 sdb_store_matcher_t *
276 sdb_store_le_matcher(sdb_store_cond_t *cond);
277 sdb_store_matcher_t *
278 sdb_store_eq_matcher(sdb_store_cond_t *cond);
279 sdb_store_matcher_t *
280 sdb_store_ge_matcher(sdb_store_cond_t *cond);
281 sdb_store_matcher_t *
282 sdb_store_gt_matcher(sdb_store_cond_t *cond);
284 /*
285  * sdb_store_matcher_parse_cmp:
286  * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
287  *
288  * Returns:
289  *  - a matcher object on success
290  *  - NULL else
291  */
292 sdb_store_matcher_t *
293 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
294                 const char *op, sdb_store_expr_t *expr);
296 /*
297  * sdb_store_dis_matcher:
298  * Creates a matcher matching the disjunction (logical OR) of two matchers.
299  */
300 sdb_store_matcher_t *
301 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
303 /*
304  * sdb_store_con_matcher:
305  * Creates a matcher matching the conjunction (logical AND) of two matchers.
306  */
307 sdb_store_matcher_t *
308 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
310 /*
311  * sdb_store_con_matcher::
312  * Creates a matcher matching the inverse (logical NOT) of a matcher.
313  */
314 sdb_store_matcher_t *
315 sdb_store_inv_matcher(sdb_store_matcher_t *m);
317 /*
318  * sdb_store_matcher_matches:
319  * Check whether the specified matcher matches the specified store object.
320  *
321  * Returns:
322  *  - 1 if the object matches
323  *  - 0 else
324  */
325 int
326 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj);
328 /*
329  * sdb_store_matcher_tostring:
330  * Format a matcher object as string. This is meant for logging or debugging
331  * purposes.
332  */
333 char *
334 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
336 /*
337  * sdb_store_lookup_cb:
338  * Lookup callback. It is called for each matching object when looking up data
339  * in the store. The lookup aborts if the callback returns non-zero.
340  */
341 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
343 /*
344  * sdb_store_scan:
345  * Look up objects in the store. The specified callback function is called for
346  * each object in the store matching 'm'. The function performs a full scan of
347  * all hosts stored in the database.
348  *
349  * Returns:
350  *  - 0 on success
351  *  - a negative value else
352  */
353 int
354 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
355                 void *user_data);
357 /*
358  * Flags for serialization functions.
359  *
360  * By default, the full object will be included in the serialized output. When
361  * specifying any of the flags, the respective information will be left out.
362  */
363 enum {
364         SDB_SKIP_ATTRIBUTES         = 1 << 0,
365         SDB_SKIP_SERVICES           = 1 << 1,
366         SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 2,
368         SDB_SKIP_ALL                = 0xffff,
369 };
371 /*
372  * sdb_store_tojson:
373  * Serialize the entire store to JSON and append the result to the specified
374  * buffer.
375  *
376  * Returns:
377  *  - 0 on success
378  *  - a negative value on error
379  */
380 int
381 sdb_store_tojson(sdb_strbuf_t *buf, int flags);
383 /*
384  * sdb_store_host_tojson:
385  * Serialize a host object to JSON and append the result to the specified
386  * buffer.
387  *
388  * Returns:
389  *  - 0 on success
390  *  - a negative value on error
391  */
392 int
393 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf, int flags);
395 /*
396  * sdb_store_iter_cb:
397  * Store iterator callback. Iteration stops if the callback returns non-zero.
398  */
399 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
401 /*
402  * sdb_store_iterate:
403  * Iterate the entire store, calling the specified callback for each object.
404  * The user_data pointer is passed on to each call of the callback.
405  *
406  * Returns:
407  *  - 0 on success
408  *  - a negative value else
409  */
410 int
411 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
413 #ifdef __cplusplus
414 } /* extern "C" */
415 #endif
417 #endif /* ! SDB_CORE_STORE_H */
419 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */