Code

store: Added support for different data-types for attributes.
[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/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_host:
54  * Add/update a host in the store. If the host, identified by its
55  * canonicalized name, already exists, it will be updated according to the
56  * specified name and timestamp. Else, a new entry will be created in the
57  * store. Any memory required for storing the entry will be allocated an
58  * managed by the store itself.
59  *
60  * Returns:
61  *  - 0 on success
62  *  - a positive value if the new entry is older than the currently stored
63  *    entry (in this case, no update will happen)
64  *  - a negative value on error
65  */
66 int
67 sdb_store_host(const char *name, sdb_time_t last_update);
69 /*
70  * sdb_store_has_host:
71  * sdb_store_get_host:
72  * Query the store for a host by its (canonicalized) name.
73  *
74  * sdb_store_get_host increments the ref count of the host object. The caller
75  * needs to deref it when no longer using it.
76  */
77 _Bool
78 sdb_store_has_host(const char *name);
80 sdb_store_base_t *
81 sdb_store_get_host(const char *name);
83 /*
84  * sdb_store_attribute:
85  * Add/update a host's attribute in the store. If the attribute, identified by
86  * its key, already exists for the specified host, it will be updated to the
87  * specified values. If the referenced host does not exist, an error will be
88  * reported. Else, a new entry will be created in the store. Any memory
89  * required for storing the entry will be allocated and managed by the store
90  * itself.
91  *
92  * Returns:
93  *  - 0 on success
94  *  - a positive value if the new entry is older than the currently stored
95  *    entry (in this case, no update will happen)
96  *  - a negative value on error
97  */
98 int
99 sdb_store_attribute(const char *hostname,
100                 const char *key, const sdb_data_t *value,
101                 sdb_time_t last_update);
103 /*
104  * sdb_store_service:
105  * Add/update a store in the store. If the service, identified by its name,
106  * already exists for the specified host, it will be updated according to the
107  * specified 'service' object. If the referenced host does not exist, an error
108  * will be reported. Else, a new entry will be created in the store. Any
109  * memory required for storing the entry will be allocated an managed by the
110  * store itself. The specified service-object will not be referenced or
111  * further accessed.
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_service(const char *hostname, const char *name,
121                 sdb_time_t last_update);
123 /*
124  * Flags for serialization functions.
125  *
126  * By default, the full object will be included in the serialized output. When
127  * specifying any of the flags, the respective information will be left out.
128  */
129 enum {
130         SDB_SKIP_ATTRIBUTES         = 1 << 0,
131         SDB_SKIP_SERVICES           = 1 << 1,
132         SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 2,
133 };
135 /*
136  * sdb_store_tojson:
137  * Serialize the entire store to JSON and append the result to the specified
138  * buffer.
139  *
140  * Returns:
141  *  - 0 on success
142  *  - a negative value on error
143  */
144 int
145 sdb_store_tojson(sdb_strbuf_t *buf, int flags);
147 /*
148  * sdb_store_host_tojson:
149  * Serialize a host object to JSON and append the result to the specified
150  * buffer.
151  *
152  * Returns:
153  *  - 0 on success
154  *  - a negative value on error
155  */
156 int
157 sdb_store_host_tojson(sdb_store_base_t *host, sdb_strbuf_t *buf, int flags);
159 #ifdef __cplusplus
160 } /* extern "C" */
161 #endif
163 #endif /* ! SDB_CORE_STORE_H */
165 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */