516ba525caabbdc00796a7b73b20c43f1dede7d7
[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 replication replication-level for the content
97  * @param expiration expiration time for the content
98  * @param uid unique identifier for the datum
99  * @return #GNUNET_OK to keep the item
100  *         #GNUNET_NO to delete the item
101  */
102 typedef int
103 (*PluginDatumProcessor) (void *cls,
104                          const struct GNUNET_HashCode *key,
105                          uint32_t size,
106                          const void *data,
107                          enum GNUNET_BLOCK_Type type,
108                          uint32_t priority,
109                          uint32_t anonymity,
110                          uint32_t replication,
111                          struct GNUNET_TIME_Absolute expiration,
112                          uint64_t uid);
113
114
115 /**
116  * Get an estimate of how much space the database is
117  * currently using.
118  *
119  * NB: estimate is an output parameter because emscripten cannot handle
120  * returning 64-bit integers from dynamically loaded modules.
121  *
122  * @param cls closure
123  * @param estimate location to store estimate
124  * @return number of bytes used on disk
125  */
126 typedef void
127 (*PluginEstimateSize) (void *cls,
128                        unsigned long long *estimate);
129
130
131 /**
132  * Put continuation.
133  *
134  * @param cls closure
135  * @param key key for the item stored
136  * @param size size of the item stored
137  * @param status #GNUNET_OK or #GNUNET_SYSERROR
138  * @param msg error message on error
139  */
140 typedef void
141 (*PluginPutCont) (void *cls,
142                   const struct GNUNET_HashCode *key,
143                   uint32_t size,
144                   int status,
145                   const char *msg);
146
147
148 /**
149  * Store an item in the datastore.  If the item is already present,
150  * the priorities and replication levels are summed up and the higher
151  * expiration time and lower anonymity level is used.
152  *
153  * @param cls closure
154  * @param key key for the item
155  * @param size number of bytes in @a data
156  * @param data content stored
157  * @param type type of the content
158  * @param priority priority of the content
159  * @param anonymity anonymity-level for the content
160  * @param replication replication-level for the content
161  * @param expiration expiration time for the content
162  * @param cont continuation called with success or failure status
163  * @param cont_cls continuation closure for @a cont
164  */
165 typedef void
166 (*PluginPut) (void *cls,
167               const struct GNUNET_HashCode *key,
168               uint32_t size,
169               const void *data,
170               enum GNUNET_BLOCK_Type type,
171               uint32_t priority,
172               uint32_t anonymity,
173               uint32_t replication,
174               struct GNUNET_TIME_Absolute expiration,
175               PluginPutCont cont,
176               void *cont_cls);
177
178
179 /**
180  * An processor over a set of keys stored in the datastore.
181  *
182  * @param cls closure
183  * @param key key in the data store, if NULL iteration is finished
184  * @param count how many values are stored under this key in the datastore
185  */
186 typedef void
187 (*PluginKeyProcessor) (void *cls,
188                        const struct GNUNET_HashCode *key,
189                        unsigned int count);
190
191
192 /**
193  * Get all of the keys in the datastore.
194  *
195  * @param cls closure
196  * @param proc function to call on each key
197  * @param proc_cls closure for @a proc
198  */
199 typedef void
200 (*PluginGetKeys) (void *cls,
201                   PluginKeyProcessor proc,
202                   void *proc_cls);
203
204
205 /**
206  * Get one of the results for a particular key in the datastore.
207  *
208  * @param cls closure
209  * @param next_uid return the result with lowest uid >= next_uid
210  * @param random if true, return a random result instead of using next_uid
211  * @param key maybe NULL (to match all entries)
212  * @param vhash hash of the value, maybe NULL (to
213  *        match all values that have the right key).
214  *        Note that for DBlocks there is no difference
215  *        betwen key and vhash, but for other blocks
216  *        there may be!
217  * @param type entries of which type are relevant?
218  *     Use 0 for any type.
219  * @param proc function to call on the matching value;
220  *        will be called with NULL if nothing matches
221  * @param proc_cls closure for @a proc
222  */
223 typedef void
224 (*PluginGetKey) (void *cls,
225                  uint64_t next_uid,
226                  bool random,
227                  const struct GNUNET_HashCode *key,
228                  const struct GNUNET_HashCode *vhash,
229                  enum GNUNET_BLOCK_Type type,
230                  PluginDatumProcessor proc,
231                  void *proc_cls);
232
233
234 /**
235  * Get a random item (additional constraints may apply depending on
236  * the specific implementation).  Calls @a proc with all values ZERO or
237  * NULL if no item applies, otherwise @a proc is called once and only
238  * once with an item.
239  *
240  * @param cls closure
241  * @param proc function to call the value (once only).
242  * @param proc_cls closure for @a proc
243  */
244 typedef void
245 (*PluginGetRandom) (void *cls,
246                     PluginDatumProcessor proc,
247                     void *proc_cls);
248
249
250 /**
251  * Update continuation.
252  *
253  * @param cls closure
254  * @param status #GNUNET_OK or #GNUNET_SYSERR
255  * @param msg error message on error
256  */
257 typedef void
258 (*PluginUpdateCont) (void *cls,
259                      int status,
260                      const char *msg);
261
262
263 /**
264  * Update the priority, replication and expiration for a particular
265  * unique ID in the datastore.  If the expiration time in value is
266  * different than the time found in the datastore, the higher value
267  * should be kept.  The specified priority and replication is added
268  * to the existing value.
269  *
270  * @param cls closure
271  * @param uid unique identifier of the datum
272  * @param priority by how much should the priority
273  *     change?
274  * @param replication by how much should the replication
275  *     change?
276  * @param expire new expiration time should be the
277  *     MAX of any existing expiration time and
278  *     this value
279  * @param cont continuation called with success or failure status
280  * @param cons_cls continuation closure
281  */
282 typedef void
283 (*PluginUpdate) (void *cls,
284                  uint64_t uid,
285                  uint32_t priority,
286                  uint32_t replication,
287                  struct GNUNET_TIME_Absolute expire,
288                  PluginUpdateCont cont,
289                  void *cont_cls);
290
291
292 /**
293  * Select a single item from the datastore (among those applicable).
294  *
295  * @param cls closure
296  * @param next_uid return the result with lowest uid >= next_uid
297  * @param type entries of which type should be considered?
298  *        Must not be zero (ANY).
299  * @param proc function to call on the matching value;
300  *        will be called with NULL if no value matches
301  * @param proc_cls closure for @a proc
302  */
303 typedef void
304 (*PluginGetType) (void *cls,
305                   uint64_t next_uid,
306                   enum GNUNET_BLOCK_Type type,
307                   PluginDatumProcessor proc,
308                   void *proc_cls);
309
310
311 /**
312  * Drop database.
313  *
314  * @param cls closure
315  */
316 typedef void
317 (*PluginDrop) (void *cls);
318
319
320 /**
321  * Each plugin is required to return a pointer to a struct of this
322  * type as the return value from its entry point.
323  */
324 struct GNUNET_DATASTORE_PluginFunctions
325 {
326
327   /**
328    * Closure to use for all of the following callbacks
329    * (except "next_request").
330    */
331   void *cls;
332
333   /**
334    * Calculate the current on-disk size of the SQ store.  Estimates
335    * are fine, if that's the only thing available.
336    */
337   PluginEstimateSize estimate_size;
338
339   /**
340    * Function to store an item in the datastore.
341    */
342   PluginPut put;
343
344   /**
345    * Update the priority for a particular key in the datastore.  If
346    * the expiration time in value is different than the time found in
347    * the datastore, the higher value should be kept.  For the
348    * anonymity level, the lower value is to be used.  The specified
349    * priority should be added to the existing priority, ignoring the
350    * priority in value.
351    */
352   PluginUpdate update;
353
354   /**
355    * Get a particular datum matching a given hash from the datastore.
356    */
357   PluginGetKey get_key;
358
359   /**
360    * Get datum (of the specified type) with anonymity level zero.
361    */
362   PluginGetType get_zero_anonymity;
363
364   /**
365    * Function to get a random item with high replication score from
366    * the database, lowering the item's replication score.  Returns a
367    * single random item from those with the highest replication
368    * counters.  The item's replication counter is decremented by one
369    * IF it was positive before.
370    */
371   PluginGetRandom get_replication;
372
373   /**
374    * Function to get a random expired item or, if none are expired,
375    * either the oldest entry or one with a low priority (depending
376    * on what was efficiently implementable).
377    */
378   PluginGetRandom get_expiration;
379
380   /**
381    * Delete the database.  The next operation is
382    * guaranteed to be unloading of the module.
383    */
384   PluginDrop drop;
385
386   /**
387    * Iterate over all keys in the database.
388    */
389   PluginGetKeys get_keys;
390
391 };
392
393 #endif
394
395 /** @} */  /* end of group */