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