doxygen fixes
[oweals/gnunet.git] / src / datastore / plugin_datastore.h
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.h
23  * @brief API for the database backend plugins.
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - consider defining enumeration or at least typedef
28  *   for the type of "type" (instead of using uint32_t)
29  */
30 #ifndef PLUGIN_DATASTORE_H
31 #define PLUGIN_DATASTORE_H
32
33 #include "gnunet_configuration_lib.h"
34 #include "gnunet_datastore_service.h"
35 #include "gnunet_statistics_service.h"
36 #include "gnunet_scheduler_lib.h"
37
38
39 /**
40  * How many bytes of overhead will we assume per entry
41  * in the SQlite DB?
42  */
43 #define GNUNET_DATASTORE_ENTRY_OVERHEAD 256
44
45
46 /**
47  * The datastore service will pass a pointer to a struct
48  * of this type as the first and only argument to the
49  * entry point of each datastore plugin.
50  */
51 struct GNUNET_DATASTORE_PluginEnvironment
52 {
53   /**
54    * Configuration to use.
55    */
56   const struct GNUNET_CONFIGURATION_Handle *cfg;
57
58   /**
59    * Scheduler to use.
60    */
61   struct GNUNET_SCHEDULER_Handle *sched;
62
63 };
64
65
66 /**
67  * Function invoked on behalf of a "PluginIterator"
68  * asking the database plugin to call the iterator
69  * with the next item.
70  *
71  * @param next_cls whatever argument was given
72  *        to the PluginIterator as "next_cls".
73  * @param end_it set to GNUNET_YES if we
74  *        should terminate the iteration early
75  *        (iterator should be still called once more
76  *         to signal the end of the iteration).
77  */
78 typedef void (*PluginNextRequest)(void *next_cls,
79                                   int end_it);
80
81
82 /**
83  * An iterator over a set of items stored in the datastore.
84  *
85  * @param cls closure
86  * @param next_cls closure to pass to the "next" function.
87  * @param key key for the content
88  * @param size number of bytes in data
89  * @param data content stored
90  * @param type type of the content
91  * @param priority priority of the content
92  * @param anonymity anonymity-level for the content
93  * @param expiration expiration time for the content
94  * @param uid unique identifier for the datum;
95  *        maybe 0 if no unique identifier is available
96  *
97  * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
98  *         (continue on call to "next", of course),
99  *         GNUNET_NO to delete the item and continue (if supported)
100  */
101 typedef int (*PluginIterator) (void *cls,
102                                void *next_cls,
103                                const GNUNET_HashCode * key,
104                                uint32_t size,
105                                const void *data,
106                                uint32_t type,
107                                uint32_t priority,
108                                uint32_t anonymity,
109                                struct GNUNET_TIME_Absolute
110                                expiration, 
111                                uint64_t uid);
112
113 /**
114  * Get an estimate of how much space the database is
115  * currently using.
116  *
117  * @param cls closure
118  * @return number of bytes used on disk
119  */
120 typedef unsigned long long (*PluginGetSize) (void *cls);
121
122
123 /**
124  * Store an item in the datastore.  If the item is already present,
125  * the priorities are summed up and the higher expiration time and
126  * lower anonymity level is used.
127  *
128  * @param cls closure
129  * @param key key for the item
130  * @param size number of bytes in data
131  * @param data content stored
132  * @param type type of the content
133  * @param priority priority of the content
134  * @param anonymity anonymity-level for the content
135  * @param expiration expiration time for the content
136  * @param msg set to an error message (on failure)
137  * @return GNUNET_OK on success, GNUNET_NO if the content
138  *         was already present (and may have been updated);
139  *         GNUNET_SYSERR on failure
140  */
141 typedef int (*PluginPut) (void *cls,
142                           const GNUNET_HashCode * key,
143                           uint32_t size,
144                           const void *data,
145                           uint32_t type,
146                           uint32_t priority,
147                           uint32_t anonymity,
148                           struct GNUNET_TIME_Absolute expiration,
149                            char **msg);
150
151
152 /**
153  * Iterate over the results for a particular key
154  * in the datastore.
155  *
156  * @param cls closure
157  * @param key maybe NULL (to match all entries)
158  * @param vhash hash of the value, maybe NULL (to
159  *        match all values that have the right key).
160  *        Note that for DBlocks there is no difference
161  *        betwen key and vhash, but for other blocks
162  *        there may be!
163  * @param type entries of which type are relevant?
164  *     Use 0 for any type.
165  * @param iter function to call on each matching value; however,
166  *        after the first call to "iter", the plugin must wait
167  *        until "NextRequest" was called before giving the iterator
168  *        the next item; finally, the "iter" should be called once
169  *        once with a NULL value at the end ("next_cls" should be NULL
170  *        for that last call)
171  * @param iter_cls closure for iter
172  */
173 typedef void (*PluginGet) (void *cls,
174                            const GNUNET_HashCode * key,
175                            const GNUNET_HashCode * vhash,
176                            uint32_t type,
177                            PluginIterator iter, void *iter_cls);
178
179
180 /**
181  * Update the priority for a particular key in the datastore.  If
182  * the expiration time in value is different than the time found in
183  * the datastore, the higher value should be kept.  For the
184  * anonymity level, the lower value is to be used.  The specified
185  * priority should be added to the existing priority, ignoring the
186  * priority in value.
187  *
188  * Note that it is possible for multiple values to match this put.
189  * In that case, all of the respective values are updated.
190  *
191  * @param cls closure
192  * @param uid unique identifier of the datum
193  * @param delta by how much should the priority
194  *     change?  If priority + delta < 0 the
195  *     priority should be set to 0 (never go
196  *     negative).
197  * @param expire new expiration time should be the
198  *     MAX of any existing expiration time and
199  *     this value
200  * @param msg set to an error message (on error)
201  * @return GNUNET_OK on success
202  */
203 typedef int (*PluginUpdate) (void *cls,
204                              uint64_t uid,
205                              int delta, struct GNUNET_TIME_Absolute expire,
206                              char **msg);
207
208
209 /**
210  * Select a subset of the items in the datastore and call
211  * the given iterator for each of them.
212  *
213  * @param cls closure
214  * @param type entries of which type should be considered?
215  *        Use 0 for any type.
216  * @param iter function to call on each matching value; however,
217  *        after the first call to "iter", the plugin must wait
218  *        until "NextRequest" was called before giving the iterator
219  *        the next item; finally, the "iter" should be called once
220  *        once with a NULL value at the end ("next_cls" should be NULL
221  *        for that last call)
222  * @param iter_cls closure for iter
223  */
224 typedef void (*PluginSelector) (void *cls,
225                                 uint32_t type,
226                                 PluginIterator iter,
227                                 void *iter_cls);
228
229 /**
230  * Drop database.
231  *
232  * @param cls closure
233  */
234 typedef void (*PluginDrop) (void *cls);
235
236
237
238 /**
239  * Each plugin is required to return a pointer to a struct of this
240  * type as the return value from its entry point.
241  */
242 struct GNUNET_DATASTORE_PluginFunctions
243 {
244
245   /**
246    * Closure to use for all of the following callbacks
247    * (except "next_request").
248    */
249   void *cls;
250
251   /**
252    * Get the current on-disk size of the SQ store.  Estimates are
253    * fine, if that's the only thing available.
254    */
255   PluginGetSize get_size;
256
257   /**
258    * Function to store an item in the datastore.
259    */
260   PluginPut put;
261
262   /**
263    * Function called by iterators whenever they want the next value;
264    * note that unlike all of the other callbacks, this one does get a
265    * the "next_cls" closure which is usually different from the "cls"
266    * member of this struct!
267    */
268   PluginNextRequest next_request;
269
270   /**
271    * Function to iterate over the results for a particular key
272    * in the datastore.
273    */
274   PluginGet get;
275
276   /**
277    * Update the priority for a particular key in the datastore.  If
278    * the expiration time in value is different than the time found in
279    * the datastore, the higher value should be kept.  For the
280    * anonymity level, the lower value is to be used.  The specified
281    * priority should be added to the existing priority, ignoring the
282    * priority in value.
283    */
284   PluginUpdate update;
285
286   /**
287    * Iterate over the items in the datastore in ascending
288    * order of priority.
289    */
290   PluginSelector iter_low_priority;
291
292   /**
293    * Iterate over content with anonymity zero.
294    */
295   PluginSelector iter_zero_anonymity;
296
297   /**
298    * Iterate over the items in the datastore in ascending order of
299    * expiration time. 
300    */
301   PluginSelector iter_ascending_expiration;
302
303   /**
304    * Iterate over the items in the datastore in migration
305    * order.  Call the given function on the next item only
306    * (and then signal 'end' with a second call).  This is
307    * a significant difference from all the other iterators!
308    */
309   PluginSelector iter_migration_order;
310
311   /**
312    * Iterate over all the items in the datastore
313    * as fast as possible in a single transaction
314    * (can lock datastore while this happens, focus
315    * is on doing it fast).
316    */
317   PluginSelector iter_all_now;
318
319   /**
320    * Delete the database.  The next operation is
321    * guaranteed to be unloading of the module.
322    */
323   PluginDrop drop;
324
325 };
326
327
328 #endif