- doc
[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, int delta);
52
53
54 /**
55  * The datastore service will pass a pointer to a struct
56  * of this type as the first and only argument to the
57  * entry point of each datastore plugin.
58  */
59 struct GNUNET_DATASTORE_PluginEnvironment
60 {
61   /**
62    * Configuration to use.
63    */
64   const struct GNUNET_CONFIGURATION_Handle *cfg;
65
66   /**
67    * Function to call on disk utilization change.
68    */
69   DiskUtilizationChange duc;
70
71   /**
72    * Closure.
73    */
74   void *cls;
75
76 };
77
78
79 /**
80  * An processor over a set of items stored in the datastore.
81  *
82  * @param cls closure
83  * @param key key for the content
84  * @param size number of bytes in data
85  * @param data content stored
86  * @param type type of the content
87  * @param priority priority of the content
88  * @param anonymity anonymity-level for the content
89  * @param expiration expiration time for the content
90  * @param uid unique identifier for the datum
91  *
92  * @return GNUNET_OK to keep the item
93  *         GNUNET_NO to delete the item
94  */
95 typedef int (*PluginDatumProcessor) (void *cls, const struct GNUNET_HashCode * key,
96                                      uint32_t size, const void *data,
97                                      enum GNUNET_BLOCK_Type type,
98                                      uint32_t priority, uint32_t anonymity,
99                                      struct GNUNET_TIME_Absolute expiration,
100                                      uint64_t uid);
101
102 /**
103  * Get an estimate of how much space the database is
104  * currently using.
105  *
106  * @param cls closure
107  * @return number of bytes used on disk
108  */
109 typedef unsigned long long (*PluginEstimateSize) (void *cls);
110
111
112 /**
113  * Store an item in the datastore.  If the item is already present,
114  * the priorities and replication levels are summed up and the higher
115  * expiration time and lower anonymity level is used.
116  *
117  * @param cls closure
118  * @param key key for the item
119  * @param size number of bytes in data
120  * @param data content stored
121  * @param type type of the content
122  * @param priority priority of the content
123  * @param anonymity anonymity-level for the content
124  * @param replication replication-level for the content
125  * @param expiration expiration time for the content
126  * @param msg set to an error message (on failure)
127  * @return GNUNET_OK on success,
128  *         GNUNET_SYSERR on failure
129  */
130 typedef int (*PluginPut) (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
131                           const void *data, enum GNUNET_BLOCK_Type type,
132                           uint32_t priority, uint32_t anonymity,
133                           uint32_t replication,
134                           struct GNUNET_TIME_Absolute expiration, char **msg);
135
136
137 /**
138  * An processor over a set of keys stored in the datastore.
139  *
140  * @param cls closure
141  * @param key key in the data store
142  * @param count how many values are stored under this key in the datastore
143  */
144 typedef void (*PluginKeyProcessor) (void *cls, 
145                                    const struct GNUNET_HashCode *key,
146                                    unsigned int count);
147
148
149 /**
150  * Get all of the keys in the datastore.
151  *
152  * @param cls closure
153  * @param proc function to call on each key
154  * @param proc_cls closure for proc
155  */
156 typedef void (*PluginGetKeys) (void *cls,
157                                PluginKeyProcessor proc, void *proc_cls);
158
159
160 /**
161  * Get one of the results for a particular key in the datastore.
162  *
163  * @param cls closure
164  * @param offset offset of the result (modulo num-results);
165  *               specific ordering does not matter for the offset
166  * @param key key to match, never NULL
167  * @param vhash hash of the value, maybe NULL (to
168  *        match all values that have the right key).
169  *        Note that for DBlocks there is no difference
170  *        betwen key and vhash, but for other blocks
171  *        there may be!
172  * @param type entries of which type are relevant?
173  *     Use 0 for any type.
174  * @param min find the smallest key that is larger than the given min,
175  *            NULL for no minimum (return smallest key)
176  * @param proc function to call on the matching value;
177  *        proc should be called with NULL if there is no result
178  * @param proc_cls closure for proc
179  */
180 typedef void (*PluginGetKey) (void *cls, uint64_t offset,
181                               const struct GNUNET_HashCode * key,
182                               const struct GNUNET_HashCode * vhash,
183                               enum GNUNET_BLOCK_Type type,
184                               PluginDatumProcessor proc, void *proc_cls);
185
186
187 /**
188  * Get a random item (additional constraints may apply depending on
189  * the specific implementation).  Calls 'proc' with all values ZERO or
190  * NULL if no item applies, otherwise 'proc' is called once and only
191  * once with an item.
192  *
193  * @param cls closure
194  * @param proc function to call the value (once only).
195  * @param proc_cls closure for proc
196  */
197 typedef void (*PluginGetRandom) (void *cls, PluginDatumProcessor proc,
198                                  void *proc_cls);
199
200
201
202
203 /**
204  * Update the priority for a particular key in the datastore.  If
205  * the expiration time in value is different than the time found in
206  * the datastore, the higher value should be kept.  For the
207  * anonymity level, the lower value is to be used.  The specified
208  * priority should be added to the existing priority, ignoring the
209  * priority in value.
210  *
211  * Note that it is possible for multiple values to match this put.
212  * In that case, all of the respective values are updated.
213  *
214  * @param cls closure
215  * @param uid unique identifier of the datum
216  * @param delta by how much should the priority
217  *     change?  If priority + delta < 0 the
218  *     priority should be set to 0 (never go
219  *     negative).
220  * @param expire new expiration time should be the
221  *     MAX of any existing expiration time and
222  *     this value
223  * @param msg set to an error message (on error)
224  * @return GNUNET_OK on success
225  */
226 typedef int (*PluginUpdate) (void *cls, uint64_t uid, int delta,
227                              struct GNUNET_TIME_Absolute expire, char **msg);
228
229
230 /**
231  * Select a single item from the datastore at the specified offset
232  * (among those applicable).
233  *
234  * @param cls closure
235  * @param offset offset of the result (modulo num-results);
236  *               specific ordering does not matter for the offset
237  * @param type entries of which type should be considered?
238  *        Must not be zero (ANY).
239  * @param proc function to call on the matching value
240  * @param proc_cls closure for proc
241  */
242 typedef void (*PluginGetType) (void *cls, uint64_t offset,
243                                enum GNUNET_BLOCK_Type type,
244                                PluginDatumProcessor proc, void *proc_cls);
245
246
247 /**
248  * Drop database.
249  *
250  * @param cls closure
251  */
252 typedef void (*PluginDrop) (void *cls);
253
254
255
256 /**
257  * Each plugin is required to return a pointer to a struct of this
258  * type as the return value from its entry point.
259  */
260 struct GNUNET_DATASTORE_PluginFunctions
261 {
262
263   /**
264    * Closure to use for all of the following callbacks
265    * (except "next_request").
266    */
267   void *cls;
268
269   /**
270    * Calculate the current on-disk size of the SQ store.  Estimates
271    * are fine, if that's the only thing available.
272    */
273   PluginEstimateSize estimate_size;
274
275   /**
276    * Function to store an item in the datastore.
277    */
278   PluginPut put;
279
280   /**
281    * Update the priority for a particular key in the datastore.  If
282    * the expiration time in value is different than the time found in
283    * the datastore, the higher value should be kept.  For the
284    * anonymity level, the lower value is to be used.  The specified
285    * priority should be added to the existing priority, ignoring the
286    * priority in value.
287    */
288   PluginUpdate update;
289
290   /**
291    * Get a particular datum matching a given hash from the datastore.
292    */
293   PluginGetKey get_key;
294
295   /**
296    * Get datum (of the specified type) with anonymity level zero.
297    * This function is allowed to ignore the 'offset' argument
298    * and instead return a random result (with zero anonymity of
299    * the correct type) if implementing an offset is expensive.
300    */
301   PluginGetType get_zero_anonymity;
302
303   /**
304    * Function to get a random item with high replication score from
305    * the database, lowering the item's replication score.  Returns a
306    * single random item from those with the highest replication
307    * counters.  The item's replication counter is decremented by one
308    * IF it was positive before.
309    */
310   PluginGetRandom get_replication;
311
312   /**
313    * Function to get a random expired item or, if none are expired,
314    * either the oldest entry or one with a low priority (depending
315    * on what was efficiently implementable).
316    */
317   PluginGetRandom get_expiration;
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    * Iterate over all keys in the database.
327    */
328   PluginGetKeys get_keys;
329   
330 };
331
332
333 #endif