improving datastore API
[oweals/gnunet.git] / src / datastore / plugin_datastore_sqlite.c
1 /*
2      This file is part of GNUnet
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file datastore/plugin_datastore_sqlite.c
23  * @brief sqlite-based datastore backend
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "plugin_datastore.h"
29
30
31 /**
32  * Context for all functions in this plugin.
33  */
34 struct Plugin 
35 {
36   /**
37    * Our execution environment.
38    */
39   struct GNUNET_DATASTORE_PluginEnvironment *env;
40 };
41
42
43 /**
44  * Get an estimate of how much space the database is
45  * currently using.
46  * @return number of bytes used on disk
47  */
48 static unsigned long long sqlite_plugin_get_size (void *cls)
49 {
50   return 0;
51 }
52
53
54 /**
55  * Store an item in the datastore.
56  *
57  * @param cls closure
58  * @param key key for the item
59  * @param size number of bytes in data
60  * @param data content stored
61  * @param type type of the content
62  * @param priority priority of the content
63  * @param anonymity anonymity-level for the content
64  * @param expiration expiration time for the content
65  * @param msg set to an error message
66  * @return GNUNET_OK on success
67  */
68 static int
69 sqlite_plugin_put (void *cls,
70                    const GNUNET_HashCode * key,
71                    uint32_t size,
72                    const void *data,
73                    uint32_t type,
74                    uint32_t priority,
75                    uint32_t anonymity,
76                    struct GNUNET_TIME_Absolute expiration,
77                    char ** msg)
78 {
79   *msg = GNUNET_strdup("not implemented");
80   return GNUNET_SYSERR;
81 }
82
83
84 /**
85  * Iterate over the results for a particular key
86  * in the datastore.
87  *
88  * @param cls closure
89  * @param key maybe NULL (to match all entries)
90  * @param vhash hash of the value, maybe NULL (to
91  *        match all values that have the right key).
92  *        Note that for DBlocks there is no difference
93  *        betwen key and vhash, but for other blocks
94  *        there may be!
95  * @param type entries of which type are relevant?
96  *     Use 0 for any type.
97  * @param iter function to call on each matching value;
98  *        will be called once with a NULL value at the end
99  * @param iter_cls closure for iter
100  */
101 static void
102 sqlite_plugin_get (void *cls,
103                    const GNUNET_HashCode * key,
104                    const GNUNET_HashCode * vhash,
105                    uint32_t type,
106                    GNUNET_DATASTORE_Iterator iter, void *iter_cls)
107 {
108   static struct GNUNET_TIME_Absolute zero;
109   iter (iter_cls, NULL, 0, NULL, 0, 0, 0, zero, 0);
110 }
111
112
113 /**
114  * Update the priority for a particular key in the datastore.  If
115  * the expiration time in value is different than the time found in
116  * the datastore, the higher value should be kept.  For the
117  * anonymity level, the lower value is to be used.  The specified
118  * priority should be added to the existing priority, ignoring the
119  * priority in value.
120  *
121  * Note that it is possible for multiple values to match this put.
122  * In that case, all of the respective values are updated.
123  *
124  * @param uid unique identifier of the datum
125  * @param delta by how much should the priority
126  *     change?  If priority + delta < 0 the
127  *     priority should be set to 0 (never go
128  *     negative).
129  * @param expire new expiration time should be the
130  *     MAX of any existing expiration time and
131  *     this value
132  * @param msg set to an error message
133  * @return GNUNET_OK on success
134  */
135 static int
136 sqlite_plugin_update (void *cls,
137                       unsigned long long uid,
138                       int delta, struct GNUNET_TIME_Absolute expire,
139                       char **msg)
140 {
141   *msg = GNUNET_strdup ("not implemented");
142   return GNUNET_SYSERR;
143 }
144
145
146 /**
147  * Select a subset of the items in the datastore and call
148  * the given iterator for each of them.
149  *
150  * @param type entries of which type should be considered?
151  *        Use 0 for any type.
152  * @param iter function to call on each matching value;
153  *        will be called once with a NULL value at the end
154  * @param iter_cls closure for iter
155  */
156 static void
157 sqlite_plugin_iter_low_priority (void *cls,
158                         uint32_t type,
159                         GNUNET_DATASTORE_Iterator iter,
160                         void *iter_cls)
161 {
162   static struct GNUNET_TIME_Absolute zero;
163   iter (iter_cls, NULL, 0, NULL, 0, 0, 0, zero, 0);
164 }
165
166
167 /**
168  * Select a subset of the items in the datastore and call
169  * the given iterator for each of them.
170  *
171  * @param type entries of which type should be considered?
172  *        Use 0 for any type.
173  * @param iter function to call on each matching value;
174  *        will be called once with a NULL value at the end
175  * @param iter_cls closure for iter
176  */
177 static void
178 sqlite_plugin_iter_zero_anonymity (void *cls,
179                         uint32_t type,
180                         GNUNET_DATASTORE_Iterator iter,
181                         void *iter_cls)
182 {
183   static struct GNUNET_TIME_Absolute zero;
184   iter (iter_cls, NULL, 0, NULL, 0, 0, 0, zero, 0);
185 }
186
187
188
189 /**
190  * Select a subset of the items in the datastore and call
191  * the given iterator for each of them.
192  *
193  * @param type entries of which type should be considered?
194  *        Use 0 for any type.
195  * @param iter function to call on each matching value;
196  *        will be called once with a NULL value at the end
197  * @param iter_cls closure for iter
198  */
199 static void
200 sqlite_plugin_iter_ascending_expiration (void *cls,
201                         uint32_t type,
202                         GNUNET_DATASTORE_Iterator iter,
203                         void *iter_cls)
204 {
205   static struct GNUNET_TIME_Absolute zero;
206   iter (iter_cls, NULL, 0, NULL, 0, 0, 0, zero, 0);
207 }
208
209
210
211 /**
212  * Select a subset of the items in the datastore and call
213  * the given iterator for each of them.
214  *
215  * @param type entries of which type should be considered?
216  *        Use 0 for any type.
217  * @param iter function to call on each matching value;
218  *        will be called once with a NULL value at the end
219  * @param iter_cls closure for iter
220  */
221 static void
222 sqlite_plugin_iter_migration_order (void *cls,
223                         uint32_t type,
224                         GNUNET_DATASTORE_Iterator iter,
225                         void *iter_cls)
226 {
227   static struct GNUNET_TIME_Absolute zero;
228   iter (iter_cls, NULL, 0, NULL, 0, 0, 0, zero, 0);
229 }
230
231
232
233 /**
234  * Select a subset of the items in the datastore and call
235  * the given iterator for each of them.
236  *
237  * @param type entries of which type should be considered?
238  *        Use 0 for any type.
239  * @param iter function to call on each matching value;
240  *        will be called once with a NULL value at the end
241  * @param iter_cls closure for iter
242  */
243 static void
244 sqlite_plugin_iter_all_now (void *cls,
245                         uint32_t type,
246                         GNUNET_DATASTORE_Iterator iter,
247                         void *iter_cls)
248 {
249   static struct GNUNET_TIME_Absolute zero;
250   iter (iter_cls, NULL, 0, NULL, 0, 0, 0, zero, 0);
251 }
252
253
254 /**
255  * Drop database.
256  */
257 static void 
258 sqlite_plugin_drop (void *cls)
259 {
260 }
261
262
263 /**
264  * Entry point for the plugin.
265  */
266 void *
267 libgnunet_plugin_datastore_sqlite_init (void *cls)
268 {
269   struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
270   struct GNUNET_DATASTORE_PluginFunctions *api;
271   struct Plugin *plugin;
272
273   plugin = GNUNET_malloc (sizeof (struct Plugin));
274   plugin->env = env;
275   api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
276   api->cls = plugin;
277   api->get_size = &sqlite_plugin_get_size;
278   api->put = &sqlite_plugin_put;
279   api->get = &sqlite_plugin_get;
280   api->update = &sqlite_plugin_update;
281   api->iter_low_priority = &sqlite_plugin_iter_low_priority;
282   api->iter_zero_anonymity = &sqlite_plugin_iter_zero_anonymity;
283   api->iter_ascending_expiration = &sqlite_plugin_iter_ascending_expiration;
284   api->iter_migration_order = &sqlite_plugin_iter_migration_order;
285   api->iter_all_now = &sqlite_plugin_iter_all_now;
286   api->drop = &sqlite_plugin_drop;
287   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
288                    "sqlite", _("Sqlite database running\n"));
289   return api;
290 }
291
292
293 /**
294  * Exit point from the plugin.
295  */
296 void *
297 libgnunet_plugin_datastore_sqlite_done (void *cls)
298 {
299   struct GNUNET_DATASTORE_PluginFunctions *api = cls;
300   struct Plugin *plugin = api->cls;
301
302   GNUNET_free (plugin);
303   GNUNET_free (api);
304   return NULL;
305 }
306
307 /* end of plugin_datastore_sqlite.c */