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