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