584537f39cc9ffadea4ca5f10568eae741aab156
[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 *GNUNET_DATASTORE_connect (const struct
67                                                           GNUNET_CONFIGURATION_Handle
68                                                           *cfg);
69
70
71 /**
72  * Disconnect from the datastore service (and free
73  * associated resources).
74  *
75  * @param h handle to the datastore
76  * @param drop set to GNUNET_YES to delete all data in datastore (!)
77  */
78 void 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 *GNUNET_DATASTORE_reserve (struct
116                                                               GNUNET_DATASTORE_Handle
117                                                               *h,
118                                                               uint64_t amount,
119                                                               uint32_t entries,
120                                                               unsigned int
121                                                               queue_priority,
122                                                               unsigned int
123                                                               max_queue_size,
124                                                               struct
125                                                               GNUNET_TIME_Relative
126                                                               timeout,
127                                                               GNUNET_DATASTORE_ContinuationWithStatus
128                                                               cont,
129                                                               void *cont_cls);
130
131
132 /**
133  * Store an item in the datastore.  If the item is already present,
134  * the priorities and replication values are summed up and the higher
135  * expiration time and lower anonymity level is used.
136  *
137  * @param h handle to the datastore
138  * @param rid reservation ID to use (from "reserve"); use 0 if no
139  *            prior reservation was made
140  * @param key key for the value
141  * @param size number of bytes in data
142  * @param data content stored
143  * @param type type of the content
144  * @param priority priority of the content
145  * @param anonymity anonymity-level for the content
146  * @param replication how often should the content be replicated to other peers?
147  * @param expiration expiration time for the content
148  * @param queue_priority ranking of this request in the priority queue
149  * @param max_queue_size at what queue size should this request be dropped
150  *        (if other requests of higher priority are in the queue)
151  * @param timeout timeout for the operation
152  * @param cont continuation to call when done
153  * @param cont_cls closure for cont
154  * @return NULL if the entry was not queued, otherwise a handle that can be used to
155  *         cancel; note that even if NULL is returned, the callback will be invoked
156  *         (or rather, will already have been invoked)
157  */
158 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_put (struct
159                                                           GNUNET_DATASTORE_Handle
160                                                           *h, uint32_t rid,
161                                                           const GNUNET_HashCode
162                                                           * key, size_t size,
163                                                           const void *data,
164                                                           enum GNUNET_BLOCK_Type
165                                                           type,
166                                                           uint32_t priority,
167                                                           uint32_t anonymity,
168                                                           uint32_t replication,
169                                                           struct
170                                                           GNUNET_TIME_Absolute
171                                                           expiration,
172                                                           unsigned int
173                                                           queue_priority,
174                                                           unsigned int
175                                                           max_queue_size,
176                                                           struct
177                                                           GNUNET_TIME_Relative
178                                                           timeout,
179                                                           GNUNET_DATASTORE_ContinuationWithStatus
180                                                           cont, void *cont_cls);
181
182
183 /**
184  * Signal that all of the data for which a reservation was made has
185  * been stored and that whatever excess space might have been reserved
186  * can now be released.
187  *
188  * @param h handle to the datastore
189  * @param rid reservation ID (value of "success" in original continuation
190  *        from the "reserve" function).
191  * @param queue_priority ranking of this request in the priority queue
192  * @param max_queue_size at what queue size should this request be dropped
193  *        (if other requests of higher priority are in the queue)
194  * @param queue_priority ranking of this request in the priority queue
195  * @param max_queue_size at what queue size should this request be dropped
196  *        (if other requests of higher priority are in the queue)
197  * @param timeout how long to wait at most for a response
198  * @param cont continuation to call when done
199  * @param cont_cls closure for cont
200  * @return NULL if the entry was not queued, otherwise a handle that can be used to
201  *         cancel; note that even if NULL is returned, the callback will be invoked
202  *         (or rather, will already have been invoked)
203  */
204 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_release_reserve (struct
205                                                                       GNUNET_DATASTORE_Handle
206                                                                       *h,
207                                                                       uint32_t
208                                                                       rid,
209                                                                       unsigned
210                                                                       int
211                                                                       queue_priority,
212                                                                       unsigned
213                                                                       int
214                                                                       max_queue_size,
215                                                                       struct
216                                                                       GNUNET_TIME_Relative
217                                                                       timeout,
218                                                                       GNUNET_DATASTORE_ContinuationWithStatus
219                                                                       cont,
220                                                                       void
221                                                                       *cont_cls);
222
223
224 /**
225  * Update a value in the datastore.
226  *
227  * @param h handle to the datastore
228  * @param uid identifier for the value
229  * @param priority how much to increase the priority of the value
230  * @param expiration new expiration value should be MAX of existing and this argument
231  * @param queue_priority ranking of this request in the priority queue
232  * @param max_queue_size at what queue size should this request be dropped
233  *        (if other requests of higher priority are in the queue)
234  * @param timeout how long to wait at most for a response
235  * @param cont continuation to call when done
236  * @param cont_cls closure for cont
237  * @return NULL if the entry was not queued, otherwise a handle that can be used to
238  *         cancel; note that even if NULL is returned, the callback will be invoked
239  *         (or rather, will already have been invoked)
240  */
241 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_update (struct
242                                                              GNUNET_DATASTORE_Handle
243                                                              *h, uint64_t uid,
244                                                              uint32_t priority,
245                                                              struct
246                                                              GNUNET_TIME_Absolute
247                                                              expiration,
248                                                              unsigned int
249                                                              queue_priority,
250                                                              unsigned int
251                                                              max_queue_size,
252                                                              struct
253                                                              GNUNET_TIME_Relative
254                                                              timeout,
255                                                              GNUNET_DATASTORE_ContinuationWithStatus
256                                                              cont,
257                                                              void *cont_cls);
258
259
260 /**
261  * Explicitly remove some content from the database.
262  * The "cont"inuation will be called with status
263  * "GNUNET_OK" if content was removed, "GNUNET_NO"
264  * if no matching entry was found and "GNUNET_SYSERR"
265  * on all other types of errors.
266  *
267  * @param h handle to the datastore
268  * @param key key for the value
269  * @param size number of bytes in data
270  * @param data content stored
271  * @param queue_priority ranking of this request in the priority queue
272  * @param max_queue_size at what queue size should this request be dropped
273  *        (if other requests of higher priority are in the queue)
274  * @param timeout how long to wait at most for a response
275  * @param cont continuation to call when done
276  * @param cont_cls closure for cont
277  * @return NULL if the entry was not queued, otherwise a handle that can be used to
278  *         cancel; note that even if NULL is returned, the callback will be invoked
279  *         (or rather, will already have been invoked)
280  */
281 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_remove (struct
282                                                              GNUNET_DATASTORE_Handle
283                                                              *h,
284                                                              const
285                                                              GNUNET_HashCode *
286                                                              key, size_t size,
287                                                              const void *data,
288                                                              unsigned int
289                                                              queue_priority,
290                                                              unsigned int
291                                                              max_queue_size,
292                                                              struct
293                                                              GNUNET_TIME_Relative
294                                                              timeout,
295                                                              GNUNET_DATASTORE_ContinuationWithStatus
296                                                              cont,
297                                                              void *cont_cls);
298
299
300 /**
301  * Process a datum that was stored in the datastore.
302  *
303  * @param cls closure
304  * @param key key for the content
305  * @param size number of bytes in data
306  * @param data content stored
307  * @param type type of the content
308  * @param priority priority of the content
309  * @param anonymity anonymity-level for the content
310  * @param expiration expiration time for the content
311  * @param uid unique identifier for the datum;
312  *        maybe 0 if no unique identifier is available
313  */
314 typedef void (*GNUNET_DATASTORE_DatumProcessor) (void *cls,
315                                                  const GNUNET_HashCode * key,
316                                                  size_t size, const void *data,
317                                                  enum GNUNET_BLOCK_Type type,
318                                                  uint32_t priority,
319                                                  uint32_t anonymity,
320                                                  struct GNUNET_TIME_Absolute
321                                                  expiration, uint64_t uid);
322
323
324 /**
325  * Get a result for a particular key from the datastore.  The processor
326  * will only be called once.
327  *
328  * @param h handle to the datastore
329  * @param offset offset of the result (modulo num-results); set to
330  *               a random 64-bit value initially; then increment by
331  *               one each time; detect that all results have been found by uid
332  *               being again the first uid ever returned.
333  * @param key maybe NULL (to match all entries)
334  * @param type desired type, 0 for any
335  * @param queue_priority ranking of this request in the priority queue
336  * @param max_queue_size at what queue size should this request be dropped
337  *        (if other requests of higher priority are in the queue)
338  * @param timeout how long to wait at most for a response
339  * @param proc function to call on each matching value;
340  *        will be called once with a NULL value at the end
341  * @param proc_cls closure for proc
342  * @return NULL if the entry was not queued, otherwise a handle that can be used to
343  *         cancel
344  */
345 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_get_key (struct
346                                                               GNUNET_DATASTORE_Handle
347                                                               *h,
348                                                               uint64_t offset,
349                                                               const
350                                                               GNUNET_HashCode *
351                                                               key,
352                                                               enum
353                                                               GNUNET_BLOCK_Type
354                                                               type,
355                                                               unsigned int
356                                                               queue_priority,
357                                                               unsigned int
358                                                               max_queue_size,
359                                                               struct
360                                                               GNUNET_TIME_Relative
361                                                               timeout,
362                                                               GNUNET_DATASTORE_DatumProcessor
363                                                               proc,
364                                                               void *proc_cls);
365
366
367 /**
368  * Get a single zero-anonymity value from the datastore.
369  * Note that some implementations can ignore the 'offset' and
370  * instead return a random zero-anonymity value.  In that case,
371  * detecting the wrap-around based on a repeating UID is at best
372  * probabilistic.
373  *
374  * @param h handle to the datastore
375  * @param offset offset of the result (modulo num-results); set to
376  *               a random 64-bit value initially; then increment by
377  *               one each time; detect that all results have been found by uid
378  *               being again the first uid ever returned.
379  * @param queue_priority ranking of this request in the priority queue
380  * @param max_queue_size at what queue size should this request be dropped
381  *        (if other requests of higher priority are in the queue)
382  * @param timeout how long to wait at most for a response
383  * @param type allowed type for the operation (never zero)
384  * @param proc function to call on a random value; it
385  *        will be called once with a value (if available)
386  *        or with NULL if none value exists.
387  * @param proc_cls closure for proc
388  * @return NULL if the entry was not queued, otherwise a handle that can be used to
389  *         cancel
390  */
391 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_get_zero_anonymity (struct
392                                                                          GNUNET_DATASTORE_Handle
393                                                                          *h,
394                                                                          uint64_t
395                                                                          offset,
396                                                                          unsigned
397                                                                          int
398                                                                          queue_priority,
399                                                                          unsigned
400                                                                          int
401                                                                          max_queue_size,
402                                                                          struct
403                                                                          GNUNET_TIME_Relative
404                                                                          timeout,
405                                                                          enum
406                                                                          GNUNET_BLOCK_Type
407                                                                          type,
408                                                                          GNUNET_DATASTORE_DatumProcessor
409                                                                          proc,
410                                                                          void
411                                                                          *proc_cls);
412
413
414 /**
415  * Get a random value from the datastore for content replication.
416  * Returns a single, random value among those with the highest
417  * replication score, lowering positive replication scores by one for
418  * the chosen value (if only content with a replication score exists,
419  * a random value is returned and replication scores are not changed).
420  *
421  * @param h handle to the datastore
422  * @param queue_priority ranking of this request in the priority queue
423  * @param max_queue_size at what queue size should this request be dropped
424  *        (if other requests of higher priority are in the queue)
425  * @param timeout how long to wait at most for a response
426  * @param proc function to call on a random value; it
427  *        will be called once with a value (if available)
428  *        and always once with a value of NULL.
429  * @param proc_cls closure for proc
430  * @return NULL if the entry was not queued, otherwise a handle that can be used to
431  *         cancel
432  */
433 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_get_for_replication (struct
434                                                                           GNUNET_DATASTORE_Handle
435                                                                           *h,
436                                                                           unsigned
437                                                                           int
438                                                                           queue_priority,
439                                                                           unsigned
440                                                                           int
441                                                                           max_queue_size,
442                                                                           struct
443                                                                           GNUNET_TIME_Relative
444                                                                           timeout,
445                                                                           GNUNET_DATASTORE_DatumProcessor
446                                                                           proc,
447                                                                           void
448                                                                           *proc_cls);
449
450
451
452 /**
453  * Cancel a datastore operation.  The final callback from the
454  * operation must not have been done yet.
455  * 
456  * @param qe operation to cancel
457  */
458 void GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe);
459
460
461 #if 0                           /* keep Emacsens' auto-indent happy */
462 {
463 #endif
464 #ifdef __cplusplus
465 }
466 #endif
467
468 /* end of gnunet_datastore_service.h */
469 #endif