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/llist.h"
36 #include "utils/strbuf.h"
38 #include <stdio.h>
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
44 /*
45 * Store object types.
46 */
47 enum {
48 SDB_HOST = 1,
49 SDB_SERVICE,
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_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 * sdb_store_clear:
68 * Clear the entire store and remove all stored objects.
69 */
70 void
71 sdb_store_clear(void);
73 /*
74 * sdb_store_host:
75 * Add/update a host in the store. If the host, identified by its
76 * canonicalized name, already exists, it will be updated according to the
77 * specified name and timestamp. Else, a new entry will be created in the
78 * store. Any memory required for storing the entry will be allocated an
79 * managed by the store itself.
80 *
81 * Returns:
82 * - 0 on success
83 * - a positive value if the new entry is older than the currently stored
84 * entry (in this case, no update will happen)
85 * - a negative value on error
86 */
87 int
88 sdb_store_host(const char *name, sdb_time_t last_update);
90 /*
91 * sdb_store_has_host:
92 * sdb_store_get_host:
93 * Query the store for a host by its (canonicalized) name.
94 *
95 * sdb_store_get_host increments the ref count of the host object. The caller
96 * needs to deref it when no longer using it.
97 */
98 _Bool
99 sdb_store_has_host(const char *name);
101 sdb_store_obj_t *
102 sdb_store_get_host(const char *name);
104 /*
105 * sdb_store_attribute:
106 * Add/update a host's attribute in the store. If the attribute, identified by
107 * its key, already exists for the specified host, it will be updated to the
108 * specified values. If the referenced host does not exist, an error will be
109 * reported. Else, a new entry will be created in the store. Any memory
110 * required for storing the entry will be allocated and managed by the store
111 * itself.
112 *
113 * Returns:
114 * - 0 on success
115 * - a positive value if the new entry is older than the currently stored
116 * entry (in this case, no update will happen)
117 * - a negative value on error
118 */
119 int
120 sdb_store_attribute(const char *hostname,
121 const char *key, const sdb_data_t *value,
122 sdb_time_t last_update);
124 /*
125 * sdb_store_service:
126 * Add/update a service in the store. If the service, identified by its name,
127 * already exists for the specified host, it will be updated according to the
128 * specified 'service' object. If the referenced host does not exist, an error
129 * will be reported. Else, a new entry will be created in the store. Any
130 * memory required for storing the entry will be allocated an managed by the
131 * store itself. The specified service-object will not be referenced or
132 * further accessed.
133 *
134 * Returns:
135 * - 0 on success
136 * - a positive value if the new entry is older than the currently stored
137 * entry (in this case, no update will happen)
138 * - a negative value on error
139 */
140 int
141 sdb_store_service(const char *hostname, const char *name,
142 sdb_time_t last_update);
144 /*
145 * sdb_store_service_attr:
146 * Add/update a service's attribute in the store. If the attribute, identified
147 * by its key, already exists for the specified service, it will be updated to
148 * the specified value. If the references service (for the specified host)
149 * does not exist, an error will be reported. Any memory required for storing
150 * the entry will be allocated and managed by the store itself.
151 *
152 * Returns:
153 * - 0 on success
154 * - a positive value if the new entry is older than the currently stored
155 * entry (in this case, no update will happen)
156 * - a negative value on error
157 */
158 int
159 sdb_store_service_attr(const char *hostname, const char *service,
160 const char *key, const sdb_data_t *value, sdb_time_t last_update);
162 /*
163 * Conditionals may be used to lookup hosts from the store based on a
164 * conditional expression.
165 *
166 * A conditional object inherits from sdb_object_t and, thus, may safely be
167 * cast to a generic object.
168 */
169 struct sdb_store_cond;
170 typedef struct sdb_store_cond sdb_store_cond_t;
171 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
173 /*
174 * sdb_store_attr_cond:
175 * Creates a conditional based on attribute values. The value of stored
176 * attributes is compared against the specified value. See sdb_data_cmp for
177 * details about the comparison.
178 */
179 sdb_store_cond_t *
180 sdb_store_attr_cond(const char *name, const sdb_data_t *value);
182 /*
183 * Store matchers may be used to lookup hosts from the store based on their
184 * various attributes. Service and attribute matchers are applied to a host's
185 * services and attributes and evaluate to true if *any* service or attribute
186 * matches.
187 *
188 * A store matcher object inherits from sdb_object_t and, thus, may safely be
189 * cast to a generic object.
190 */
191 struct sdb_store_matcher;
192 typedef struct sdb_store_matcher sdb_store_matcher_t;
193 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
195 /*
196 * sdb_store_name_matcher:
197 * Creates a matcher matching by the specified object type's name. If 're' is
198 * true, the specified name is treated as a POSIX extended regular expression.
199 * Else, the exact name has to match (case-insensitive).
200 */
201 sdb_store_matcher_t *
202 sdb_store_name_matcher(int type, const char *name, _Bool re);
204 /*
205 * sdb_store_attr_matcher:
206 * Creates a matcher matching attributes based on their value. If 're' is
207 * true, the specified name is treated as a POSIX extended regular expression.
208 * Else, the exact name has to match (case-insensitive).
209 */
210 sdb_store_matcher_t *
211 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
213 /*
214 * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
215 * sdb_store_ge_matcher, sdb_store_gt_matcher:
216 * Creates a matcher based on a conditional. The matcher matches objects for
217 * which the conditional evaluates the object to compare less than, less or
218 * equal, equal, greater or equal, or greater than the conditional's value
219 * repsectively.
220 */
221 sdb_store_matcher_t *
222 sdb_store_lt_matcher(sdb_store_cond_t *cond);
223 sdb_store_matcher_t *
224 sdb_store_le_matcher(sdb_store_cond_t *cond);
225 sdb_store_matcher_t *
226 sdb_store_eq_matcher(sdb_store_cond_t *cond);
227 sdb_store_matcher_t *
228 sdb_store_ge_matcher(sdb_store_cond_t *cond);
229 sdb_store_matcher_t *
230 sdb_store_gt_matcher(sdb_store_cond_t *cond);
232 /*
233 * sdb_store_matcher_parse_cmp:
234 * Parse a simple compare expression (<obj_type>.<attr> <op> <value>).
235 *
236 * Returns:
237 * - a matcher object on success
238 * - NULL else
239 */
240 sdb_store_matcher_t *
241 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
242 const char *op, const sdb_data_t *value);
244 /*
245 * sdb_store_dis_matcher:
246 * Creates a matcher matching the disjunction (logical OR) of two matchers.
247 */
248 sdb_store_matcher_t *
249 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
251 /*
252 * sdb_store_con_matcher:
253 * Creates a matcher matching the conjunction (logical AND) of two matchers.
254 */
255 sdb_store_matcher_t *
256 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
258 /*
259 * sdb_store_con_matcher::
260 * Creates a matcher matching the inverse (logical NOT) of a matcher.
261 */
262 sdb_store_matcher_t *
263 sdb_store_inv_matcher(sdb_store_matcher_t *m);
265 /*
266 * sdb_store_matcher_matches:
267 * Check whether the specified matcher matches the specified store object.
268 *
269 * Returns:
270 * - 1 if the object matches
271 * - 0 else
272 */
273 int
274 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj);
276 /*
277 * sdb_store_matcher_tostring:
278 * Format a matcher object as string. This is meant for logging or debugging
279 * purposes.
280 */
281 char *
282 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
284 /*
285 * sdb_store_lookup_cb:
286 * Lookup callback. It is called for each matching object when looking up data
287 * in the store. The lookup aborts if the callback returns non-zero.
288 */
289 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
291 /*
292 * sdb_store_lookup:
293 * Look up objects in the store. The specified callback function is called for
294 * each object in the store matching 'm'.
295 *
296 * Returns:
297 * - 0 on success
298 * - a negative value else
299 */
300 int
301 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
302 void *user_data);
304 /*
305 * Flags for serialization functions.
306 *
307 * By default, the full object will be included in the serialized output. When
308 * specifying any of the flags, the respective information will be left out.
309 */
310 enum {
311 SDB_SKIP_ATTRIBUTES = 1 << 0,
312 SDB_SKIP_SERVICES = 1 << 1,
313 SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 2,
315 SDB_SKIP_ALL = 0xffff,
316 };
318 /*
319 * sdb_store_tojson:
320 * Serialize the entire store to JSON and append the result to the specified
321 * buffer.
322 *
323 * Returns:
324 * - 0 on success
325 * - a negative value on error
326 */
327 int
328 sdb_store_tojson(sdb_strbuf_t *buf, int flags);
330 /*
331 * sdb_store_host_tojson:
332 * Serialize a host object to JSON and append the result to the specified
333 * buffer.
334 *
335 * Returns:
336 * - 0 on success
337 * - a negative value on error
338 */
339 int
340 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf, int flags);
342 /*
343 * sdb_store_iter_cb:
344 * Store iterator callback. Iteration stops if the callback returns non-zero.
345 */
346 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
348 /*
349 * sdb_store_iterate:
350 * Iterate the entire store, calling the specified callback for each object.
351 * The user_data pointer is passed on to each call of the callback.
352 *
353 * Returns:
354 * - 0 on success
355 * - a negative value else
356 */
357 int
358 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
360 #ifdef __cplusplus
361 } /* extern "C" */
362 #endif
364 #endif /* ! SDB_CORE_STORE_H */
366 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */