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 * sdb_store_base_t represents the super-class of any object stored in the
46 * database. It inherits from sdb_object_t and may safely be cast to a generic
47 * object to access its name.
48 */
49 struct sdb_store_base;
50 typedef struct sdb_store_base sdb_store_base_t;
52 /*
53 * sdb_store_clear:
54 * Clear the entire store and remove all stored objects.
55 */
56 void
57 sdb_store_clear(void);
59 /*
60 * sdb_store_host:
61 * Add/update a host in the store. If the host, identified by its
62 * canonicalized name, already exists, it will be updated according to the
63 * specified name and timestamp. Else, a new entry will be created in the
64 * store. Any memory required for storing the entry will be allocated an
65 * managed by the store itself.
66 *
67 * Returns:
68 * - 0 on success
69 * - a positive value if the new entry is older than the currently stored
70 * entry (in this case, no update will happen)
71 * - a negative value on error
72 */
73 int
74 sdb_store_host(const char *name, sdb_time_t last_update);
76 /*
77 * sdb_store_has_host:
78 * sdb_store_get_host:
79 * Query the store for a host by its (canonicalized) name.
80 *
81 * sdb_store_get_host increments the ref count of the host object. The caller
82 * needs to deref it when no longer using it.
83 */
84 _Bool
85 sdb_store_has_host(const char *name);
87 sdb_store_base_t *
88 sdb_store_get_host(const char *name);
90 /*
91 * sdb_store_attribute:
92 * Add/update a host's attribute in the store. If the attribute, identified by
93 * its key, already exists for the specified host, it will be updated to the
94 * specified values. If the referenced host does not exist, an error will be
95 * reported. Else, a new entry will be created in the store. Any memory
96 * required for storing the entry will be allocated and managed by the store
97 * itself.
98 *
99 * Returns:
100 * - 0 on success
101 * - a positive value if the new entry is older than the currently stored
102 * entry (in this case, no update will happen)
103 * - a negative value on error
104 */
105 int
106 sdb_store_attribute(const char *hostname,
107 const char *key, const sdb_data_t *value,
108 sdb_time_t last_update);
110 /*
111 * sdb_store_service:
112 * Add/update a store in the store. If the service, identified by its name,
113 * already exists for the specified host, it will be updated according to the
114 * specified 'service' object. If the referenced host does not exist, an error
115 * will be reported. Else, a new entry will be created in the store. Any
116 * memory required for storing the entry will be allocated an managed by the
117 * store itself. The specified service-object will not be referenced or
118 * further accessed.
119 *
120 * Returns:
121 * - 0 on success
122 * - a positive value if the new entry is older than the currently stored
123 * entry (in this case, no update will happen)
124 * - a negative value on error
125 */
126 int
127 sdb_store_service(const char *hostname, const char *name,
128 sdb_time_t last_update);
130 /*
131 * Store matchers may be used to lookup objects from the host based on their
132 * various attributes. Each type of matcher evaluates attributes of the
133 * respective object type.
134 *
135 * For each matcher object, *all* specified attributes have to match.
136 *
137 * A store matcher object inherits from sdb_object_t and, thus, may safely be
138 * cast to a generic object.
139 */
140 struct sdb_store_matcher;
141 typedef struct sdb_store_matcher sdb_store_matcher_t;
142 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
144 /*
145 * sdb_store_attr_matcher:
146 * Creates a matcher matching attributes based on their name or value. Either
147 * a complete name (which will have to match completely but case-independent)
148 * or an extended POSIX regular expression may be specified.
149 */
150 sdb_store_matcher_t *
151 sdb_store_attr_matcher(const char *attr_name, const char *attr_name_re,
152 const char *attr_value, const char *attr_value_re);
154 /*
155 * sdb_store_service_matcher:
156 * Creates a matcher matching services based on their name or attributes.
157 */
158 sdb_store_matcher_t *
159 sdb_store_service_matcher(const char *service_name, const char *service_name_re,
160 sdb_store_matcher_t *attr_matcher);
162 /*
163 * sdb_store_host_matcher:
164 * Creates a matcher matching hosts based on their name, services assigned to
165 * the host, or its attributes.
166 */
167 sdb_store_matcher_t *
168 sdb_store_host_matcher(const char *host_name, const char *host_name_re,
169 sdb_store_matcher_t *service_matcher,
170 sdb_store_matcher_t *attr_matcher);
172 /*
173 * sdb_store_matcher_parse_cmp:
174 * Parse a simple compare expression (<obj_type>.<attr> <op> <value>).
175 *
176 * Returns:
177 * - a matcher object on success
178 * - NULL else
179 */
180 sdb_store_matcher_t *
181 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
182 const char *op, const char *value);
184 /*
185 * sdb_store_dis_matcher:
186 * Creates a matcher matching the disjunction (logical OR) of two matchers.
187 */
188 sdb_store_matcher_t *
189 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
191 /*
192 * sdb_store_con_matcher:
193 * Creates a matcher matching the conjunction (logical AND) of two matchers.
194 */
195 sdb_store_matcher_t *
196 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
198 /*
199 * sdb_store_matcher_matches:
200 * Check whether the specified matcher matches the specified store object.
201 *
202 * Returns:
203 * - 1 if the object matches
204 * - 0 else
205 */
206 int
207 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj);
209 /*
210 * sdb_store_lookup_cb:
211 * Lookup callback. It is called for each matching object when looking up data
212 * in the store. The lookup aborts if the callback returns non-zero.
213 */
214 typedef int (*sdb_store_lookup_cb)(sdb_store_base_t *obj, void *user_data);
216 /*
217 * sdb_store_lookup:
218 * Look up objects in the store. The specified callback function is called for
219 * each object in the store matching 'm'.
220 *
221 * Returns:
222 * - 0 on success
223 * - a negative value else
224 */
225 int
226 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
227 void *user_data);
229 /*
230 * Flags for serialization functions.
231 *
232 * By default, the full object will be included in the serialized output. When
233 * specifying any of the flags, the respective information will be left out.
234 */
235 enum {
236 SDB_SKIP_ATTRIBUTES = 1 << 0,
237 SDB_SKIP_SERVICES = 1 << 1,
238 SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 2,
240 SDB_SKIP_ALL = 0xffff,
241 };
243 /*
244 * sdb_store_tojson:
245 * Serialize the entire store to JSON and append the result to the specified
246 * buffer.
247 *
248 * Returns:
249 * - 0 on success
250 * - a negative value on error
251 */
252 int
253 sdb_store_tojson(sdb_strbuf_t *buf, int flags);
255 /*
256 * sdb_store_host_tojson:
257 * Serialize a host object to JSON and append the result to the specified
258 * buffer.
259 *
260 * Returns:
261 * - 0 on success
262 * - a negative value on error
263 */
264 int
265 sdb_store_host_tojson(sdb_store_base_t *host, sdb_strbuf_t *buf, int flags);
267 /*
268 * sdb_store_iter_cb:
269 * Store iterator callback. Iteration stops if the callback returns non-zero.
270 */
271 typedef int (*sdb_store_iter_cb)(sdb_store_base_t *obj, void *user_data);
273 /*
274 * sdb_store_iterate:
275 * Iterate the entire store, calling the specified callback for each object.
276 * The user_data pointer is passed on to each call of the callback.
277 *
278 * Returns:
279 * - 0 on success
280 * - a negative value else
281 */
282 int
283 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
285 #ifdef __cplusplus
286 } /* extern "C" */
287 #endif
289 #endif /* ! SDB_CORE_STORE_H */
291 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */