Code

store: Use AVL trees to store all objects.
[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  * Conditionals may be used to lookup hosts from the store based on a
163  * conditional expression.
164  *
165  * A conditional object inherits from sdb_object_t and, thus, may safely be
166  * cast to a generic object.
167  */
168 struct sdb_store_cond;
169 typedef struct sdb_store_cond sdb_store_cond_t;
170 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
172 /*
173  * sdb_store_attr_cond:
174  * Creates a conditional based on attribute values. The value of stored
175  * attributes is compared against the specified value. See sdb_data_cmp for
176  * details about the comparison.
177  */
178 sdb_store_cond_t *
179 sdb_store_attr_cond(const char *name, const sdb_data_t *value);
181 /*
182  * Store matchers may be used to lookup hosts from the store based on their
183  * various attributes. Service and attribute matchers are applied to a host's
184  * services and attributes and evaluate to true if *any* service or attribute
185  * matches.
186  *
187  * A store matcher object inherits from sdb_object_t and, thus, may safely be
188  * cast to a generic object.
189  */
190 struct sdb_store_matcher;
191 typedef struct sdb_store_matcher sdb_store_matcher_t;
192 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
194 /*
195  * sdb_store_name_matcher:
196  * Creates a matcher matching by the specified object type's name. If 're' is
197  * true, the specified name is treated as a POSIX extended regular expression.
198  * Else, the exact name has to match (case-insensitive).
199  */
200 sdb_store_matcher_t *
201 sdb_store_name_matcher(int type, const char *name, _Bool re);
203 /*
204  * sdb_store_attr_matcher:
205  * Creates a matcher matching attributes based on their value. If 're' is
206  * true, the specified name is treated as a POSIX extended regular expression.
207  * Else, the exact name has to match (case-insensitive).
208  */
209 sdb_store_matcher_t *
210 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
212 /*
213  * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
214  * sdb_store_ge_matcher, sdb_store_gt_matcher:
215  * Creates a matcher based on a conditional. The matcher matches objects for
216  * which the conditional evaluates the object to compare less than, less or
217  * equal, equal, greater or equal, or greater than the conditional's value
218  * repsectively.
219  */
220 sdb_store_matcher_t *
221 sdb_store_lt_matcher(sdb_store_cond_t *cond);
222 sdb_store_matcher_t *
223 sdb_store_le_matcher(sdb_store_cond_t *cond);
224 sdb_store_matcher_t *
225 sdb_store_eq_matcher(sdb_store_cond_t *cond);
226 sdb_store_matcher_t *
227 sdb_store_ge_matcher(sdb_store_cond_t *cond);
228 sdb_store_matcher_t *
229 sdb_store_gt_matcher(sdb_store_cond_t *cond);
231 /*
232  * sdb_store_matcher_parse_cmp:
233  * Parse a simple compare expression (<obj_type>.<attr> <op> <value>).
234  *
235  * Returns:
236  *  - a matcher object on success
237  *  - NULL else
238  */
239 sdb_store_matcher_t *
240 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
241                 const char *op, const sdb_data_t *value);
243 /*
244  * sdb_store_dis_matcher:
245  * Creates a matcher matching the disjunction (logical OR) of two matchers.
246  */
247 sdb_store_matcher_t *
248 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
250 /*
251  * sdb_store_con_matcher:
252  * Creates a matcher matching the conjunction (logical AND) of two matchers.
253  */
254 sdb_store_matcher_t *
255 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
257 /*
258  * sdb_store_con_matcher::
259  * Creates a matcher matching the inverse (logical NOT) of a matcher.
260  */
261 sdb_store_matcher_t *
262 sdb_store_inv_matcher(sdb_store_matcher_t *m);
264 /*
265  * sdb_store_matcher_matches:
266  * Check whether the specified matcher matches the specified store object.
267  *
268  * Returns:
269  *  - 1 if the object matches
270  *  - 0 else
271  */
272 int
273 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj);
275 /*
276  * sdb_store_matcher_tostring:
277  * Format a matcher object as string. This is meant for logging or debugging
278  * purposes.
279  */
280 char *
281 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
283 /*
284  * sdb_store_lookup_cb:
285  * Lookup callback. It is called for each matching object when looking up data
286  * in the store. The lookup aborts if the callback returns non-zero.
287  */
288 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
290 /*
291  * sdb_store_lookup:
292  * Look up objects in the store. The specified callback function is called for
293  * each object in the store matching 'm'.
294  *
295  * Returns:
296  *  - 0 on success
297  *  - a negative value else
298  */
299 int
300 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
301                 void *user_data);
303 /*
304  * Flags for serialization functions.
305  *
306  * By default, the full object will be included in the serialized output. When
307  * specifying any of the flags, the respective information will be left out.
308  */
309 enum {
310         SDB_SKIP_ATTRIBUTES         = 1 << 0,
311         SDB_SKIP_SERVICES           = 1 << 1,
312         SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 2,
314         SDB_SKIP_ALL                = 0xffff,
315 };
317 /*
318  * sdb_store_tojson:
319  * Serialize the entire store to JSON and append the result to the specified
320  * buffer.
321  *
322  * Returns:
323  *  - 0 on success
324  *  - a negative value on error
325  */
326 int
327 sdb_store_tojson(sdb_strbuf_t *buf, int flags);
329 /*
330  * sdb_store_host_tojson:
331  * Serialize a host object to JSON and append the result to the specified
332  * buffer.
333  *
334  * Returns:
335  *  - 0 on success
336  *  - a negative value on error
337  */
338 int
339 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf, int flags);
341 /*
342  * sdb_store_iter_cb:
343  * Store iterator callback. Iteration stops if the callback returns non-zero.
344  */
345 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
347 /*
348  * sdb_store_iterate:
349  * Iterate the entire store, calling the specified callback for each object.
350  * The user_data pointer is passed on to each call of the callback.
351  *
352  * Returns:
353  *  - 0 on success
354  *  - a negative value else
355  */
356 int
357 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
359 #ifdef __cplusplus
360 } /* extern "C" */
361 #endif
363 #endif /* ! SDB_CORE_STORE_H */
365 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */