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