Code

Moved error / time from utils to core.
[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/time.h"
34 #include "utils/llist.h"
36 #include <stdio.h>
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
42 typedef struct {
43         sdb_object_t parent;
45         sdb_time_t last_update;
46         char *name;
47 } sdb_store_obj_t;
48 #define SDB_STORE_OBJ_INIT { SDB_OBJECT_INIT, 0, NULL }
49 #define SDB_STORE_OBJ(obj) ((sdb_store_obj_t *)(obj))
51 typedef struct {
52         sdb_store_obj_t parent;
54         char *hostname;
55 } sdb_service_t;
56 #define SDB_SVC_INIT { SDB_STORE_OBJ_INIT, NULL }
57 #define SDB_SVC(obj) ((sdb_service_t *)(obj))
59 typedef struct {
60         sdb_store_obj_t parent;
62         char *attr_value;
63         char *hostname;
64 } sdb_attribute_t;
65 #define SDB_ATTR_INIT { SDB_STORE_OBJ_INIT, NULL, NULL }
66 #define SDB_ATTR(obj) ((sdb_attribute_t *)(obj))
68 typedef struct {
69         sdb_store_obj_t parent;
71         sdb_llist_t *attributes;
72         sdb_llist_t *services;
73 } sdb_host_t;
74 #define SDB_HOST_INIT { SDB_STORE_OBJ_INIT, NULL, NULL }
75 #define SDB_HOST(obj) ((sdb_host_t *)(obj))
77 /* shortcuts for accessing the sdb_store_obj_t attributes of inheriting
78  * objects */
79 #define _last_update parent.last_update
80 #define _name parent.name
82 sdb_host_t *
83 sdb_host_create(const char *name);
85 sdb_host_t *
86 sdb_host_clone(const sdb_host_t *host);
88 /*
89  * sdb_store_host:
90  * Add/update a host in the store. If the host, identified by its name,
91  * already exists, it will be updated according to the specified 'host'
92  * object. Else, a new entry will be created in the store. Any memory required
93  * for storing the entry will be allocated an managed by the store itself. The
94  * specified host-object will not be referenced or further accessed.
95  *
96  * Returns:
97  *  - 0 on success
98  *  - a positive value if the new entry is older than the currently stored
99  *    entry (in this case, no update will happen)
100  *  - a negative value on error
101  */
102 int
103 sdb_store_host(const sdb_host_t *host);
105 const sdb_host_t *
106 sdb_store_get_host(const char *name);
108 sdb_attribute_t *
109 sdb_attribute_create(const char *hostname,
110                 const char *name, const char *value);
112 sdb_attribute_t *
113 sdb_attribute_clone(const sdb_attribute_t *attr);
115 /*
116  * sdb_store_attribute:
117  * Add/update a host's attribute in the store. If the attribute, identified by
118  * its name, already exists for the specified host, it will be updated
119  * according to the specified 'attr' object. If the referenced host does not
120  * exist, an error will be reported. Else, a new entry will be created in the
121  * store. Any memory required for storing the entry will be allocated and
122  * managed by the store itself. The specified attribute-object will not be
123  * referenced or further accessed.
124  *
125  * Returns:
126  *  - 0 on success
127  *  - a positive value if the new entry is older than the currently stored
128  *    entry (in this case, no update will happen)
129  *  - a negative value on error
130  */
131 int
132 sdb_store_attribute(const sdb_attribute_t *attr);
134 sdb_service_t *
135 sdb_service_create(const char *hostname, const char *name);
137 sdb_service_t *
138 sdb_service_clone(const sdb_service_t *svc);
140 /*
141  * sdb_store_service:
142  * Add/update a store in the store. If the service, identified by its name,
143  * already exists for the specified host, it will be updated according to the
144  * specified 'service' object. If the referenced host does not exist, an error
145  * will be reported. Else, a new entry will be created in the store. Any
146  * memory required for storing the entry will be allocated an managed by the
147  * store itself. The specified service-object will not be referenced or
148  * further accessed.
149  *
150  * Returns:
151  *  - 0 on success
152  *  - a positive value if the new entry is older than the currently stored
153  *    entry (in this case, no update will happen)
154  *  - a negative value on error
155  */
156 int
157 sdb_store_service(const sdb_service_t *svc);
159 const sdb_service_t *
160 sdb_store_get_service(const sdb_host_t *host, const char *name);
162 int
163 sdb_store_dump(FILE *fh);
165 #ifdef __cplusplus
166 } /* extern "C" */
167 #endif
169 #endif /* ! SDB_CORE_STORE_H */
171 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */