run indent twice, it alternates between two 'canonical' forms, also run whitespace...
[oweals/gnunet.git] / src / include / gnunet_datastore_service.h
1 /*
2      This file is part of GNUnet
3      (C) 2004, 2005, 2006, 2007, 2009, 2010, 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_service.h
23  * @brief API that can be used manage the
24  *   datastore for files stored on a GNUnet node;
25  *   note that the datastore is NOT responsible for
26  *   on-demand encoding, that is achieved using
27  *   a special kind of entry.
28  * @author Christian Grothoff
29  */
30
31 #ifndef GNUNET_DATASTORE_SERVICE_H
32 #define GNUNET_DATASTORE_SERVICE_H
33
34 #include "gnunet_util_lib.h"
35 #include "gnunet_block_lib.h"
36
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #if 0                           /* keep Emacsens' auto-indent happy */
41 }
42 #endif
43 #endif
44
45 /**
46  * Entry in the queue.
47  */
48 struct GNUNET_DATASTORE_QueueEntry;
49
50 /**
51  * Handle to the datastore service.
52  */
53 struct GNUNET_DATASTORE_Handle;
54
55 /**
56  * Maximum size of a value that can be stored in the datastore.
57  */
58 #define GNUNET_DATASTORE_MAX_VALUE_SIZE 65536
59
60 /**
61  * Connect to the datastore service.
62  *
63  * @param cfg configuration to use
64  * @return handle to use to access the service
65  */
66 struct GNUNET_DATASTORE_Handle *
67 GNUNET_DATASTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
68
69
70 /**
71  * Disconnect from the datastore service (and free
72  * associated resources).
73  *
74  * @param h handle to the datastore
75  * @param drop set to GNUNET_YES to delete all data in datastore (!)
76  */
77 void
78 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h, int drop);
79
80
81 /**
82  * Continuation called to notify client about result of the
83  * operation.
84  *
85  * @param cls closure
86  * @param success GNUNET_SYSERR on failure (including timeout/queue drop)
87  *                GNUNET_NO if content was already there
88  *                GNUNET_YES (or other positive value) on success
89  * @param msg NULL on success, otherwise an error message
90  */
91 typedef void (*GNUNET_DATASTORE_ContinuationWithStatus) (void *cls,
92                                                          int32_t success,
93                                                          const char *msg);
94
95
96 /**
97  * Reserve space in the datastore.  This function should be used
98  * to avoid "out of space" failures during a longer sequence of "put"
99  * operations (for example, when a file is being inserted).
100  *
101  * @param h handle to the datastore
102  * @param amount how much space (in bytes) should be reserved (for content only)
103  * @param entries how many entries will be created (to calculate per-entry overhead)
104  * @param queue_priority ranking of this request in the priority queue
105  * @param max_queue_size at what queue size should this request be dropped
106  *        (if other requests of higher priority are in the queue)
107  * @param timeout how long to wait at most for a response (or before dying in queue)
108  * @param cont continuation to call when done; "success" will be set to
109  *             a positive reservation value if space could be reserved.
110  * @param cont_cls closure for cont
111  * @return NULL if the entry was not queued, otherwise a handle that can be used to
112  *         cancel; note that even if NULL is returned, the callback will be invoked
113  *         (or rather, will already have been invoked)
114  */
115 struct GNUNET_DATASTORE_QueueEntry *
116 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h, uint64_t amount,
117                           uint32_t entries, unsigned int queue_priority,
118                           unsigned int max_queue_size,
119                           struct GNUNET_TIME_Relative timeout,
120                           GNUNET_DATASTORE_ContinuationWithStatus cont,
121                           void *cont_cls);
122
123
124 /**
125  * Store an item in the datastore.  If the item is already present,
126  * the priorities and replication values are summed up and the higher
127  * expiration time and lower anonymity level is used.
128  *
129  * @param h handle to the datastore
130  * @param rid reservation ID to use (from "reserve"); use 0 if no
131  *            prior reservation was made
132  * @param key key for the value
133  * @param size number of bytes in data
134  * @param data content stored
135  * @param type type of the content
136  * @param priority priority of the content
137  * @param anonymity anonymity-level for the content
138  * @param replication how often should the content be replicated to other peers?
139  * @param expiration expiration time for the content
140  * @param queue_priority ranking of this request in the priority queue
141  * @param max_queue_size at what queue size should this request be dropped
142  *        (if other requests of higher priority are in the queue)
143  * @param timeout timeout for the operation
144  * @param cont continuation to call when done
145  * @param cont_cls closure for cont
146  * @return NULL if the entry was not queued, otherwise a handle that can be used to
147  *         cancel; note that even if NULL is returned, the callback will be invoked
148  *         (or rather, will already have been invoked)
149  */
150 struct GNUNET_DATASTORE_QueueEntry *
151 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h, uint32_t rid,
152                       const GNUNET_HashCode * key, size_t size,
153                       const void *data, enum GNUNET_BLOCK_Type type,
154                       uint32_t priority, uint32_t anonymity,
155                       uint32_t replication,
156                       struct GNUNET_TIME_Absolute expiration,
157                       unsigned int queue_priority, unsigned int max_queue_size,
158                       struct GNUNET_TIME_Relative timeout,
159                       GNUNET_DATASTORE_ContinuationWithStatus cont,
160                       void *cont_cls);
161
162
163 /**
164  * Signal that all of the data for which a reservation was made has
165  * been stored and that whatever excess space might have been reserved
166  * can now be released.
167  *
168  * @param h handle to the datastore
169  * @param rid reservation ID (value of "success" in original continuation
170  *        from the "reserve" function).
171  * @param queue_priority ranking of this request in the priority queue
172  * @param max_queue_size at what queue size should this request be dropped
173  *        (if other requests of higher priority are in the queue)
174  * @param queue_priority ranking of this request in the priority queue
175  * @param max_queue_size at what queue size should this request be dropped
176  *        (if other requests of higher priority are in the queue)
177  * @param timeout how long to wait at most for a response
178  * @param cont continuation to call when done
179  * @param cont_cls closure for cont
180  * @return NULL if the entry was not queued, otherwise a handle that can be used to
181  *         cancel; note that even if NULL is returned, the callback will be invoked
182  *         (or rather, will already have been invoked)
183  */
184 struct GNUNET_DATASTORE_QueueEntry *
185 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
186                                   uint32_t rid, unsigned int queue_priority,
187                                   unsigned int max_queue_size,
188                                   struct GNUNET_TIME_Relative timeout,
189                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
190                                   void *cont_cls);
191
192
193 /**
194  * Update a value in the datastore.
195  *
196  * @param h handle to the datastore
197  * @param uid identifier for the value
198  * @param priority how much to increase the priority of the value
199  * @param expiration new expiration value should be MAX of existing and this argument
200  * @param queue_priority ranking of this request in the priority queue
201  * @param max_queue_size at what queue size should this request be dropped
202  *        (if other requests of higher priority are in the queue)
203  * @param timeout how long to wait at most for a response
204  * @param cont continuation to call when done
205  * @param cont_cls closure for cont
206  * @return NULL if the entry was not queued, otherwise a handle that can be used to
207  *         cancel; note that even if NULL is returned, the callback will be invoked
208  *         (or rather, will already have been invoked)
209  */
210 struct GNUNET_DATASTORE_QueueEntry *
211 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h, uint64_t uid,
212                          uint32_t priority,
213                          struct GNUNET_TIME_Absolute expiration,
214                          unsigned int queue_priority,
215                          unsigned int max_queue_size,
216                          struct GNUNET_TIME_Relative timeout,
217                          GNUNET_DATASTORE_ContinuationWithStatus cont,
218                          void *cont_cls);
219
220
221 /**
222  * Explicitly remove some content from the database.
223  * The "cont"inuation will be called with status
224  * "GNUNET_OK" if content was removed, "GNUNET_NO"
225  * if no matching entry was found and "GNUNET_SYSERR"
226  * on all other types of errors.
227  *
228  * @param h handle to the datastore
229  * @param key key for the value
230  * @param size number of bytes in data
231  * @param data content stored
232  * @param queue_priority ranking of this request in the priority queue
233  * @param max_queue_size at what queue size should this request be dropped
234  *        (if other requests of higher priority are in the queue)
235  * @param timeout how long to wait at most for a response
236  * @param cont continuation to call when done
237  * @param cont_cls closure for cont
238  * @return NULL if the entry was not queued, otherwise a handle that can be used to
239  *         cancel; note that even if NULL is returned, the callback will be invoked
240  *         (or rather, will already have been invoked)
241  */
242 struct GNUNET_DATASTORE_QueueEntry *
243 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
244                          const GNUNET_HashCode * key, size_t size,
245                          const void *data, unsigned int queue_priority,
246                          unsigned int max_queue_size,
247                          struct GNUNET_TIME_Relative timeout,
248                          GNUNET_DATASTORE_ContinuationWithStatus cont,
249                          void *cont_cls);
250
251
252 /**
253  * Process a datum that was stored in the datastore.
254  *
255  * @param cls closure
256  * @param key key for the content
257  * @param size number of bytes in data
258  * @param data content stored
259  * @param type type of the content
260  * @param priority priority of the content
261  * @param anonymity anonymity-level for the content
262  * @param expiration expiration time for the content
263  * @param uid unique identifier for the datum;
264  *        maybe 0 if no unique identifier is available
265  */
266 typedef void (*GNUNET_DATASTORE_DatumProcessor) (void *cls,
267                                                  const GNUNET_HashCode * key,
268                                                  size_t size, const void *data,
269                                                  enum GNUNET_BLOCK_Type type,
270                                                  uint32_t priority,
271                                                  uint32_t anonymity,
272                                                  struct GNUNET_TIME_Absolute
273                                                  expiration, uint64_t uid);
274
275
276 /**
277  * Get a result for a particular key from the datastore.  The processor
278  * will only be called once.
279  *
280  * @param h handle to the datastore
281  * @param offset offset of the result (modulo num-results); set to
282  *               a random 64-bit value initially; then increment by
283  *               one each time; detect that all results have been found by uid
284  *               being again the first uid ever returned.
285  * @param key maybe NULL (to match all entries)
286  * @param type desired type, 0 for any
287  * @param queue_priority ranking of this request in the priority queue
288  * @param max_queue_size at what queue size should this request be dropped
289  *        (if other requests of higher priority are in the queue)
290  * @param timeout how long to wait at most for a response
291  * @param proc function to call on each matching value;
292  *        will be called once with a NULL value at the end
293  * @param proc_cls closure for proc
294  * @return NULL if the entry was not queued, otherwise a handle that can be used to
295  *         cancel
296  */
297 struct GNUNET_DATASTORE_QueueEntry *
298 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h, uint64_t offset,
299                           const GNUNET_HashCode * key,
300                           enum GNUNET_BLOCK_Type type,
301                           unsigned int queue_priority,
302                           unsigned int max_queue_size,
303                           struct GNUNET_TIME_Relative timeout,
304                           GNUNET_DATASTORE_DatumProcessor proc, void *proc_cls);
305
306
307 /**
308  * Get a single zero-anonymity value from the datastore.
309  * Note that some implementations can ignore the 'offset' and
310  * instead return a random zero-anonymity value.  In that case,
311  * detecting the wrap-around based on a repeating UID is at best
312  * probabilistic.
313  *
314  * @param h handle to the datastore
315  * @param offset offset of the result (modulo num-results); set to
316  *               a random 64-bit value initially; then increment by
317  *               one each time; detect that all results have been found by uid
318  *               being again the first uid ever returned.
319  * @param queue_priority ranking of this request in the priority queue
320  * @param max_queue_size at what queue size should this request be dropped
321  *        (if other requests of higher priority are in the queue)
322  * @param timeout how long to wait at most for a response
323  * @param type allowed type for the operation (never zero)
324  * @param proc function to call on a random value; it
325  *        will be called once with a value (if available)
326  *        or with NULL if none value exists.
327  * @param proc_cls closure for proc
328  * @return NULL if the entry was not queued, otherwise a handle that can be used to
329  *         cancel
330  */
331 struct GNUNET_DATASTORE_QueueEntry *
332 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
333                                      uint64_t offset,
334                                      unsigned int queue_priority,
335                                      unsigned int max_queue_size,
336                                      struct GNUNET_TIME_Relative timeout,
337                                      enum GNUNET_BLOCK_Type type,
338                                      GNUNET_DATASTORE_DatumProcessor proc,
339                                      void *proc_cls);
340
341
342 /**
343  * Get a random value from the datastore for content replication.
344  * Returns a single, random value among those with the highest
345  * replication score, lowering positive replication scores by one for
346  * the chosen value (if only content with a replication score exists,
347  * a random value is returned and replication scores are not changed).
348  *
349  * @param h handle to the datastore
350  * @param queue_priority ranking of this request in the priority queue
351  * @param max_queue_size at what queue size should this request be dropped
352  *        (if other requests of higher priority are in the queue)
353  * @param timeout how long to wait at most for a response
354  * @param proc function to call on a random value; it
355  *        will be called once with a value (if available)
356  *        and always once with a value of NULL.
357  * @param proc_cls closure for proc
358  * @return NULL if the entry was not queued, otherwise a handle that can be used to
359  *         cancel
360  */
361 struct GNUNET_DATASTORE_QueueEntry *
362 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
363                                       unsigned int queue_priority,
364                                       unsigned int max_queue_size,
365                                       struct GNUNET_TIME_Relative timeout,
366                                       GNUNET_DATASTORE_DatumProcessor proc,
367                                       void *proc_cls);
368
369
370
371 /**
372  * Cancel a datastore operation.  The final callback from the
373  * operation must not have been done yet.
374  *
375  * @param qe operation to cancel
376  */
377 void
378 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe);
379
380
381 #if 0                           /* keep Emacsens' auto-indent happy */
382 {
383 #endif
384 #ifdef __cplusplus
385 }
386 #endif
387
388 /* end of gnunet_datastore_service.h */
389 #endif