-fix (C) notices
[oweals/gnunet.git] / src / include / gnunet_datastore_plugin.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2009, 2011 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @author Christian Grothoff
23  *
24  * @file
25  * API for the database backend plugins.
26  *
27  * @defgroup datastore-plugin  Data Store service plugin API
28  * API for the database backend plugins.
29  * @{
30  */
31 #ifndef PLUGIN_DATASTORE_H
32 #define PLUGIN_DATASTORE_H
33
34 #include "gnunet_block_lib.h"
35 #include "gnunet_configuration_lib.h"
36 #include "gnunet_datastore_service.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_scheduler_lib.h"
39
40
41 /**
42  * How many bytes of overhead will we assume per entry
43  * in any DB (for reservations)?
44  */
45 #define GNUNET_DATASTORE_ENTRY_OVERHEAD 256
46
47
48 /**
49  * Function invoked to notify service of disk utilization
50  * changes.
51  *
52  * @param cls closure
53  * @param delta change in disk utilization,
54  *        0 for "reset to empty"
55  */
56 typedef void
57 (*GNUNET_DATASTORE_DiskUtilizationChange) (void *cls,
58                                            int delta);
59
60
61 /**
62  * The datastore service will pass a pointer to a struct
63  * of this type as the first and only argument to the
64  * entry point of each datastore plugin.
65  */
66 struct GNUNET_DATASTORE_PluginEnvironment
67 {
68   /**
69    * Configuration to use.
70    */
71   const struct GNUNET_CONFIGURATION_Handle *cfg;
72
73   /**
74    * Function to call on disk utilization change.
75    */
76   GNUNET_DATASTORE_DiskUtilizationChange duc;
77
78   /**
79    * Closure.
80    */
81   void *cls;
82
83 };
84
85
86 /**
87  * An processor over a set of items stored in the datastore.
88  *
89  * @param cls closure
90  * @param key key for the content
91  * @param size number of bytes in data
92  * @param data content stored
93  * @param type type of the content
94  * @param priority priority of the content
95  * @param anonymity anonymity-level for the content
96  * @param expiration expiration time for the content
97  * @param uid unique identifier for the datum
98  * @return #GNUNET_OK to keep the item
99  *         #GNUNET_NO to delete the item
100  */
101 typedef int
102 (*PluginDatumProcessor) (void *cls,
103                          const struct GNUNET_HashCode *key,
104                          uint32_t size,
105                          const void *data,
106                          enum GNUNET_BLOCK_Type type,
107                          uint32_t priority,
108                          uint32_t anonymity,
109                          struct GNUNET_TIME_Absolute expiration,
110                          uint64_t uid);
111
112
113 /**
114  * Get an estimate of how much space the database is
115  * currently using.
116  *
117  * NB: estimate is an output parameter because emscripten cannot handle
118  * returning 64-bit integers from dynamically loaded modules.
119  *
120  * @param cls closure
121  * @param estimate location to store estimate
122  * @return number of bytes used on disk
123  */
124 typedef void
125 (*PluginEstimateSize) (void *cls,
126                        unsigned long long *estimate);
127
128
129 /**
130  * Put continuation.
131  *
132  * @param cls closure
133  * @param key key for the item stored
134  * @param size size of the item stored
135  * @param status #GNUNET_OK or #GNUNET_SYSERROR
136  * @param msg error message on error
137  */
138 typedef void
139 (*PluginPutCont) (void *cls,
140                   const struct GNUNET_HashCode *key,
141                   uint32_t size,
142                   int status,
143                   const char *msg);
144
145
146 /**
147  * Store an item in the datastore.  If the item is already present,
148  * the priorities and replication levels are summed up and the higher
149  * expiration time and lower anonymity level is used.
150  *
151  * @param cls closure
152  * @param key key for the item
153  * @param size number of bytes in @a data
154  * @param data content stored
155  * @param type type of the content
156  * @param priority priority of the content
157  * @param anonymity anonymity-level for the content
158  * @param replication replication-level for the content
159  * @param expiration expiration time for the content
160  * @param cont continuation called with success or failure status
161  * @param cont_cls continuation closure for @a cont
162  */
163 typedef void
164 (*PluginPut) (void *cls, const struct GNUNET_HashCode *key,
165               uint32_t size,
166               const void *data,
167               enum GNUNET_BLOCK_Type type,
168               uint32_t priority,
169               uint32_t anonymity,
170               uint32_t replication,
171               struct GNUNET_TIME_Absolute expiration,
172               PluginPutCont cont,
173               void *cont_cls);
174
175
176 /**
177  * An processor over a set of keys stored in the datastore.
178  *
179  * @param cls closure
180  * @param key key in the data store, if NULL iteration is finished
181  * @param count how many values are stored under this key in the datastore
182  */
183 typedef void
184 (*PluginKeyProcessor) (void *cls,
185                        const struct GNUNET_HashCode *key,
186                        unsigned int count);
187
188
189 /**
190  * Get all of the keys in the datastore.
191  *
192  * @param cls closure
193  * @param proc function to call on each key
194  * @param proc_cls closure for @a proc
195  */
196 typedef void
197 (*PluginGetKeys) (void *cls,
198                   PluginKeyProcessor proc, void *proc_cls);
199
200
201 /**
202  * Get one of the results for a particular key in the datastore.
203  *
204  * @param cls closure
205  * @param offset offset of the result (modulo num-results);
206  *               specific ordering does not matter for the offset
207  * @param key key to match, never NULL
208  * @param vhash hash of the value, maybe NULL (to
209  *        match all values that have the right key).
210  *        Note that for DBlocks there is no difference
211  *        betwen key and vhash, but for other blocks
212  *        there may be!
213  * @param type entries of which type are relevant?
214  *     Use 0 for any type.
215  * @param proc function to call on the matching value;
216  *        proc should be called with NULL if there is no result
217  * @param proc_cls closure for @a proc
218  */
219 typedef void
220 (*PluginGetKey) (void *cls,
221                  uint64_t offset,
222                  const struct GNUNET_HashCode *key,
223                  const struct GNUNET_HashCode *vhash,
224                  enum GNUNET_BLOCK_Type type,
225                  PluginDatumProcessor proc,
226                  void *proc_cls);
227
228
229 /**
230  * Get a random item (additional constraints may apply depending on
231  * the specific implementation).  Calls @a proc with all values ZERO or
232  * NULL if no item applies, otherwise @a proc is called once and only
233  * once with an item.
234  *
235  * @param cls closure
236  * @param proc function to call the value (once only).
237  * @param proc_cls closure for @a proc
238  */
239 typedef void
240 (*PluginGetRandom) (void *cls,
241                     PluginDatumProcessor proc,
242                     void *proc_cls);
243
244
245 /**
246  * Update continuation.
247  *
248  * @param cls closure
249  * @param status #GNUNET_OK or #GNUNET_SYSERROR
250  * @param msg error message on error
251  */
252 typedef void
253 (*PluginUpdateCont) (void *cls,
254                      int status,
255                      const char *msg);
256
257
258 /**
259  * Update the priority for a particular key in the datastore.  If
260  * the expiration time in value is different than the time found in
261  * the datastore, the higher value should be kept.  For the
262  * anonymity level, the lower value is to be used.  The specified
263  * priority should be added to the existing priority, ignoring the
264  * priority in value.
265  *
266  * @param cls closure
267  * @param uid unique identifier of the datum
268  * @param delta by how much should the priority
269  *     change?  If priority + delta < 0 the
270  *     priority should be set to 0 (never go
271  *     negative).
272  * @param expire new expiration time should be the
273  *     MAX of any existing expiration time and
274  *     this value
275  * @param cont continuation called with success or failure status
276  * @param cons_cls continuation closure
277  */
278 typedef void
279 (*PluginUpdate) (void *cls,
280                  uint64_t uid,
281                  int delta,
282                  struct GNUNET_TIME_Absolute expire,
283                  PluginUpdateCont cont,
284                  void *cont_cls);
285
286
287 /**
288  * Select a single item from the datastore at the specified offset
289  * (among those applicable).
290  *
291  * @param cls closure
292  * @param offset offset of the result (modulo num-results);
293  *               specific ordering does not matter for the offset
294  * @param type entries of which type should be considered?
295  *        Must not be zero (ANY).
296  * @param proc function to call on the matching value
297  * @param proc_cls closure for @a proc
298  */
299 typedef void
300 (*PluginGetType) (void *cls,
301                   uint64_t offset,
302                   enum GNUNET_BLOCK_Type type,
303                   PluginDatumProcessor proc,
304                   void *proc_cls);
305
306
307 /**
308  * Drop database.
309  *
310  * @param cls closure
311  */
312 typedef void
313 (*PluginDrop) (void *cls);
314
315
316 /**
317  * Each plugin is required to return a pointer to a struct of this
318  * type as the return value from its entry point.
319  */
320 struct GNUNET_DATASTORE_PluginFunctions
321 {
322
323   /**
324    * Closure to use for all of the following callbacks
325    * (except "next_request").
326    */
327   void *cls;
328
329   /**
330    * Calculate the current on-disk size of the SQ store.  Estimates
331    * are fine, if that's the only thing available.
332    */
333   PluginEstimateSize estimate_size;
334
335   /**
336    * Function to store an item in the datastore.
337    */
338   PluginPut put;
339
340   /**
341    * Update the priority for a particular key in the datastore.  If
342    * the expiration time in value is different than the time found in
343    * the datastore, the higher value should be kept.  For the
344    * anonymity level, the lower value is to be used.  The specified
345    * priority should be added to the existing priority, ignoring the
346    * priority in value.
347    */
348   PluginUpdate update;
349
350   /**
351    * Get a particular datum matching a given hash from the datastore.
352    */
353   PluginGetKey get_key;
354
355   /**
356    * Get datum (of the specified type) with anonymity level zero.
357    * This function is allowed to ignore the 'offset' argument
358    * and instead return a random result (with zero anonymity of
359    * the correct type) if implementing an offset is expensive.
360    */
361   PluginGetType get_zero_anonymity;
362
363   /**
364    * Function to get a random item with high replication score from
365    * the database, lowering the item's replication score.  Returns a
366    * single random item from those with the highest replication
367    * counters.  The item's replication counter is decremented by one
368    * IF it was positive before.
369    */
370   PluginGetRandom get_replication;
371
372   /**
373    * Function to get a random expired item or, if none are expired,
374    * either the oldest entry or one with a low priority (depending
375    * on what was efficiently implementable).
376    */
377   PluginGetRandom get_expiration;
378
379   /**
380    * Delete the database.  The next operation is
381    * guaranteed to be unloading of the module.
382    */
383   PluginDrop drop;
384
385   /**
386    * Iterate over all keys in the database.
387    */
388   PluginGetKeys get_keys;
389
390 };
391
392 #endif
393
394 /** @} */  /* end of group */