-simplify apis, doxygen
[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 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., 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 min_expiration minimum expiration time required for 0-priority content to be stored
90  *                by the datacache at this time, zero for unknown, forever if we have no
91  *                space for 0-priority content
92  * @param msg NULL on success, otherwise an error message
93  */
94 typedef void (*GNUNET_DATASTORE_ContinuationWithStatus) (void *cls,
95                                                          int32_t success,
96                                                          struct GNUNET_TIME_Absolute min_expiration,
97                                                          const char *msg);
98
99
100 /**
101  * Reserve space in the datastore.  This function should be used
102  * to avoid "out of space" failures during a longer sequence of "put"
103  * operations (for example, when a file is being inserted).
104  *
105  * @param h handle to the datastore
106  * @param amount how much space (in bytes) should be reserved (for content only)
107  * @param entries how many entries will be created (to calculate per-entry overhead)
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 @a 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,
117                           uint64_t amount,
118                           uint32_t entries,
119                           GNUNET_DATASTORE_ContinuationWithStatus cont,
120                           void *cont_cls);
121
122
123 /**
124  * Store an item in the datastore.  If the item is already present,
125  * the priorities and replication values are summed up and the higher
126  * expiration time and lower anonymity level is used.
127  *
128  * @param h handle to the datastore
129  * @param rid reservation ID to use (from "reserve"); use 0 if no
130  *            prior reservation was made
131  * @param key key for the value
132  * @param size number of bytes in data
133  * @param data content stored
134  * @param type type of the content
135  * @param priority priority of the content
136  * @param anonymity anonymity-level for the content
137  * @param replication how often should the content be replicated to other peers?
138  * @param expiration expiration time for the content
139  * @param queue_priority ranking of this request in the priority queue
140  * @param max_queue_size at what queue size should this request be dropped
141  *        (if other requests of higher priority are in the queue)
142  * @param timeout timeout for the operation
143  * @param cont continuation to call when done
144  * @param cont_cls closure for @a cont
145  * @return NULL if the entry was not queued, otherwise a handle that can be used to
146  *         cancel; note that even if NULL is returned, the callback will be invoked
147  *         (or rather, will already have been invoked)
148  */
149 struct GNUNET_DATASTORE_QueueEntry *
150 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h, uint32_t rid,
151                       const struct GNUNET_HashCode *key, size_t size,
152                       const void *data, enum GNUNET_BLOCK_Type type,
153                       uint32_t priority, uint32_t anonymity,
154                       uint32_t replication,
155                       struct GNUNET_TIME_Absolute expiration,
156                       unsigned int queue_priority, unsigned int max_queue_size,
157                       struct GNUNET_TIME_Relative timeout,
158                       GNUNET_DATASTORE_ContinuationWithStatus cont,
159                       void *cont_cls);
160
161
162 /**
163  * Signal that all of the data for which a reservation was made has
164  * been stored and that whatever excess space might have been reserved
165  * can now be released.
166  *
167  * @param h handle to the datastore
168  * @param rid reservation ID (value of "success" in original continuation
169  *        from the "reserve" function).
170  * @param queue_priority ranking of this request in the priority queue
171  * @param max_queue_size at what queue size should this request be dropped
172  *        (if other requests of higher priority are in the queue)
173  * @param queue_priority ranking of this request in the priority queue
174  * @param max_queue_size at what queue size should this request be dropped
175  *        (if other requests of higher priority are in the queue)
176  * @param timeout how long to wait at most for a response
177  * @param cont continuation to call when done
178  * @param cont_cls closure for @a cont
179  * @return NULL if the entry was not queued, otherwise a handle that can be used to
180  *         cancel; note that even if NULL is returned, the callback will be invoked
181  *         (or rather, will already have been invoked)
182  */
183 struct GNUNET_DATASTORE_QueueEntry *
184 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
185                                   uint32_t rid, unsigned int queue_priority,
186                                   unsigned int max_queue_size,
187                                   struct GNUNET_TIME_Relative timeout,
188                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
189                                   void *cont_cls);
190
191
192 /**
193  * Update a value in the datastore.
194  *
195  * @param h handle to the datastore
196  * @param uid identifier for the value
197  * @param priority how much to increase the priority of the value
198  * @param expiration new expiration value should be MAX of existing and this argument
199  * @param queue_priority ranking of this request in the priority queue
200  * @param max_queue_size at what queue size should this request be dropped
201  *        (if other requests of higher priority are in the queue)
202  * @param timeout how long to wait at most for a response
203  * @param cont continuation to call when done
204  * @param cont_cls closure for @a cont
205  * @return NULL if the entry was not queued, otherwise a handle that can be used to
206  *         cancel; note that even if NULL is returned, the callback will be invoked
207  *         (or rather, will already have been invoked)
208  */
209 struct GNUNET_DATASTORE_QueueEntry *
210 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h, uint64_t uid,
211                          uint32_t priority,
212                          struct GNUNET_TIME_Absolute expiration,
213                          unsigned int queue_priority,
214                          unsigned int max_queue_size,
215                          struct GNUNET_TIME_Relative timeout,
216                          GNUNET_DATASTORE_ContinuationWithStatus cont,
217                          void *cont_cls);
218
219
220 /**
221  * Explicitly remove some content from the database.  @a cont will be
222  * called with status #GNUNET_OK if content was removed, #GNUNET_NO if
223  * no matching entry was found and #GNUNET_SYSERR on all other types
224  * of errors.
225  *
226  * @param h handle to the datastore
227  * @param key key for the value
228  * @param size number of bytes in @a data
229  * @param data content stored
230  * @param queue_priority ranking of this request in the priority queue
231  * @param max_queue_size at what queue size should this request be dropped
232  *        (if other requests of higher priority are in the queue)
233  * @param timeout how long to wait at most for a response
234  * @param cont continuation to call when done
235  * @param cont_cls closure for @a cont
236  * @return NULL if the entry was not queued, otherwise a handle that can be used to
237  *         cancel; note that even if NULL is returned, the callback will be invoked
238  *         (or rather, will already have been invoked)
239  */
240 struct GNUNET_DATASTORE_QueueEntry *
241 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
242                          const struct GNUNET_HashCode * key, size_t size,
243                          const void *data, unsigned int queue_priority,
244                          unsigned int max_queue_size,
245                          struct GNUNET_TIME_Relative timeout,
246                          GNUNET_DATASTORE_ContinuationWithStatus cont,
247                          void *cont_cls);
248
249
250 /**
251  * Process a datum that was stored in the datastore.
252  *
253  * @param cls closure
254  * @param key key for the content
255  * @param size number of bytes in data
256  * @param data content stored
257  * @param type type of the content
258  * @param priority priority of the content
259  * @param anonymity anonymity-level for the content
260  * @param expiration expiration time for the content
261  * @param uid unique identifier for the datum;
262  *        maybe 0 if no unique identifier is available
263  */
264 typedef void (*GNUNET_DATASTORE_DatumProcessor) (void *cls,
265                                                  const struct GNUNET_HashCode *key,
266                                                  size_t size, const void *data,
267                                                  enum GNUNET_BLOCK_Type type,
268                                                  uint32_t priority,
269                                                  uint32_t anonymity,
270                                                  struct GNUNET_TIME_Absolute expiration,
271                                                  uint64_t uid);
272
273
274 /**
275  * Get a result for a particular key from the datastore.  The processor
276  * will only be called once.
277  *
278  * @param h handle to the datastore
279  * @param offset offset of the result (modulo num-results); set to
280  *               a random 64-bit value initially; then increment by
281  *               one each time; detect that all results have been found by uid
282  *               being again the first uid ever returned.
283  * @param key maybe NULL (to match all entries)
284  * @param type desired type, 0 for any
285  * @param queue_priority ranking of this request in the priority queue
286  * @param max_queue_size at what queue size should this request be dropped
287  *        (if other requests of higher priority are in the queue)
288  * @param timeout how long to wait at most for a response
289  * @param proc function to call on a matching value;
290  *        or with a NULL value if no datum matches
291  * @param proc_cls closure for @a proc
292  * @return NULL if the entry was not queued, otherwise a handle that can be used to
293  *         cancel
294  */
295 struct GNUNET_DATASTORE_QueueEntry *
296 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h, uint64_t offset,
297                           const struct GNUNET_HashCode *key,
298                           enum GNUNET_BLOCK_Type type,
299                           unsigned int queue_priority,
300                           unsigned int max_queue_size,
301                           struct GNUNET_TIME_Relative timeout,
302                           GNUNET_DATASTORE_DatumProcessor proc, void *proc_cls);
303
304
305 /**
306  * Get a single zero-anonymity value from the datastore.
307  * Note that some implementations can ignore the 'offset' and
308  * instead return a random zero-anonymity value.  In that case,
309  * detecting the wrap-around based on a repeating UID is at best
310  * probabilistic.
311  *
312  * @param h handle to the datastore
313  * @param offset offset of the result (modulo num-results); set to
314  *               a random 64-bit value initially; then increment by
315  *               one each time; detect that all results have been found by uid
316  *               being again the first uid ever returned.
317  * @param queue_priority ranking of this request in the priority queue
318  * @param max_queue_size at what queue size should this request be dropped
319  *        (if other requests of higher priority are in the queue)
320  * @param timeout how long to wait at most for a response
321  * @param type allowed type for the operation (never zero)
322  * @param proc function to call on a random value; it
323  *        will be called once with a value (if available)
324  *        or with NULL if none value exists.
325  * @param proc_cls closure for proc
326  * @return NULL if the entry was not queued, otherwise a handle that can be used to
327  *         cancel
328  */
329 struct GNUNET_DATASTORE_QueueEntry *
330 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
331                                      uint64_t offset,
332                                      unsigned int queue_priority,
333                                      unsigned int max_queue_size,
334                                      struct GNUNET_TIME_Relative timeout,
335                                      enum GNUNET_BLOCK_Type type,
336                                      GNUNET_DATASTORE_DatumProcessor proc,
337                                      void *proc_cls);
338
339
340 /**
341  * Get a random value from the datastore for content replication.
342  * Returns a single, random value among those with the highest
343  * replication score, lowering positive replication scores by one for
344  * the chosen value (if only content with a replication score exists,
345  * a random value is returned and replication scores are not changed).
346  *
347  * @param h handle to the datastore
348  * @param queue_priority ranking of this request in the priority queue
349  * @param max_queue_size at what queue size should this request be dropped
350  *        (if other requests of higher priority are in the queue)
351  * @param timeout how long to wait at most for a response
352  * @param proc function to call on a random value; it
353  *        will be called once with a value (if available)
354  *        and always once with a value of NULL.
355  * @param proc_cls closure for proc
356  * @return NULL if the entry was not queued, otherwise a handle that can be used to
357  *         cancel
358  */
359 struct GNUNET_DATASTORE_QueueEntry *
360 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
361                                       unsigned int queue_priority,
362                                       unsigned int max_queue_size,
363                                       struct GNUNET_TIME_Relative timeout,
364                                       GNUNET_DATASTORE_DatumProcessor proc,
365                                       void *proc_cls);
366
367
368
369 /**
370  * Cancel a datastore operation.  The final callback from the
371  * operation must not have been done yet.
372  *
373  * @param qe operation to cancel
374  */
375 void
376 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe);
377
378
379 #if 0                           /* keep Emacsens' auto-indent happy */
380 {
381 #endif
382 #ifdef __cplusplus
383 }
384 #endif
385
386 /* end of gnunet_datastore_service.h */
387 #endif