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 _Bool
69 sdb_store_has_host(const char *name);
71 /*
72 * sdb_store_attribute:
73 * Add/update a host's attribute in the store. If the attribute, identified by
74 * its key, already exists for the specified host, it will be updated to the
75 * specified values. If the referenced host does not exist, an error will be
76 * reported. Else, a new entry will be created in the store. Any memory
77 * required for storing the entry will be allocated and managed by the store
78 * 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_attribute(const char *hostname, const char *key, const char *value,
88 sdb_time_t last_update);
90 /*
91 * sdb_store_service:
92 * Add/update a store in the store. If the service, identified by its name,
93 * already exists for the specified host, it will be updated according to the
94 * specified 'service' object. If the referenced host does not exist, an error
95 * will be reported. Else, a new entry will be created in the store. Any
96 * memory required for storing the entry will be allocated an managed by the
97 * store itself. The specified service-object will not be referenced or
98 * further accessed.
99 *
100 * Returns:
101 * - 0 on success
102 * - a positive value if the new entry is older than the currently stored
103 * entry (in this case, no update will happen)
104 * - a negative value on error
105 */
106 int
107 sdb_store_service(const char *hostname, const char *name,
108 sdb_time_t last_update);
110 /*
111 * Flags for serialization functions.
112 *
113 * By default, the full object will be included in the serialized output. When
114 * specifying any of the flags, the respective information will be left out.
115 */
116 enum {
117 SDB_SKIP_ATTRIBUTES = 1 << 0,
118 SDB_SKIP_SERVICES = 1 << 1,
119 SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 2,
120 };
122 /*
123 * sdb_store_tojson:
124 * Serialize the entire store to JSON and append the result to the specified
125 * buffer.
126 *
127 * Returns:
128 * - 0 on success
129 * - a negative value on error
130 */
131 int
132 sdb_store_tojson(sdb_strbuf_t *buf, int flags);
134 /*
135 * sdb_store_host_tojson:
136 * Serialize a host object to JSON and append the result to the specified
137 * buffer.
138 *
139 * Returns:
140 * - 0 on success
141 * - a negative value on error
142 */
143 int
144 sdb_store_host_tojson(sdb_store_base_t *host, sdb_strbuf_t *buf, int flags);
146 #ifdef __cplusplus
147 } /* extern "C" */
148 #endif
150 #endif /* ! SDB_CORE_STORE_H */
152 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */