ab11a21776a193a460da5e3a8e100b4d1f25cff7
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/time.h"
34 #include "utils/llist.h"
35 #include "utils/strbuf.h"
37 #include <stdio.h>
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
43 /*
44 * sdb_store_base_t represents the super-class of any object stored in the
45 * database. It inherits from sdb_object_t and may safely be cast to a generic
46 * object to access its name.
47 */
48 struct sdb_store_base;
49 typedef struct sdb_store_base sdb_store_base_t;
51 /*
52 * sdb_store_host:
53 * Add/update a host in the store. If the host, identified by its
54 * canonicalized name, already exists, it will be updated according to the
55 * specified name and timestamp. Else, a new entry will be created in the
56 * store. Any memory required for storing the entry will be allocated an
57 * managed by the store itself.
58 *
59 * Returns:
60 * - 0 on success
61 * - a positive value if the new entry is older than the currently stored
62 * entry (in this case, no update will happen)
63 * - a negative value on error
64 */
65 int
66 sdb_store_host(const char *name, sdb_time_t last_update);
68 /*
69 * sdb_store_has_host:
70 * sdb_store_get_host:
71 * Query the store for a host by its (canonicalized) name.
72 *
73 * sdb_store_get_host increments the ref count of the host object. The caller
74 * needs to deref it when no longer using it.
75 */
76 _Bool
77 sdb_store_has_host(const char *name);
79 sdb_store_base_t *
80 sdb_store_get_host(const char *name);
82 /*
83 * sdb_store_attribute:
84 * Add/update a host's attribute in the store. If the attribute, identified by
85 * its key, already exists for the specified host, it will be updated to the
86 * specified values. If the referenced host does not exist, an error will be
87 * reported. Else, a new entry will be created in the store. Any memory
88 * required for storing the entry will be allocated and managed by the store
89 * itself.
90 *
91 * Returns:
92 * - 0 on success
93 * - a positive value if the new entry is older than the currently stored
94 * entry (in this case, no update will happen)
95 * - a negative value on error
96 */
97 int
98 sdb_store_attribute(const char *hostname, const char *key, const char *value,
99 sdb_time_t last_update);
101 /*
102 * sdb_store_service:
103 * Add/update a store in the store. If the service, identified by its name,
104 * already exists for the specified host, it will be updated according to the
105 * specified 'service' object. If the referenced host does not exist, an error
106 * will be reported. Else, a new entry will be created in the store. Any
107 * memory required for storing the entry will be allocated an managed by the
108 * store itself. The specified service-object will not be referenced or
109 * further accessed.
110 *
111 * Returns:
112 * - 0 on success
113 * - a positive value if the new entry is older than the currently stored
114 * entry (in this case, no update will happen)
115 * - a negative value on error
116 */
117 int
118 sdb_store_service(const char *hostname, const char *name,
119 sdb_time_t last_update);
121 /*
122 * Flags for serialization functions.
123 *
124 * By default, the full object will be included in the serialized output. When
125 * specifying any of the flags, the respective information will be left out.
126 */
127 enum {
128 SDB_SKIP_ATTRIBUTES = 1 << 0,
129 SDB_SKIP_SERVICES = 1 << 1,
130 SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 2,
131 };
133 /*
134 * sdb_store_tojson:
135 * Serialize the entire store to JSON and append the result to the specified
136 * buffer.
137 *
138 * Returns:
139 * - 0 on success
140 * - a negative value on error
141 */
142 int
143 sdb_store_tojson(sdb_strbuf_t *buf, int flags);
145 /*
146 * sdb_store_host_tojson:
147 * Serialize a host object to JSON and append the result to the specified
148 * buffer.
149 *
150 * Returns:
151 * - 0 on success
152 * - a negative value on error
153 */
154 int
155 sdb_store_host_tojson(sdb_store_base_t *host, sdb_strbuf_t *buf, int flags);
157 #ifdef __cplusplus
158 } /* extern "C" */
159 #endif
161 #endif /* ! SDB_CORE_STORE_H */
163 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */