DHT PUT integration into FS
[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 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 /**
57  * Connect to the datastore service.
58  *
59  * @param cfg configuration to use
60  * @param sched scheduler to use
61  * @return handle to use to access the service
62  */
63 struct GNUNET_DATASTORE_Handle *GNUNET_DATASTORE_connect (const struct
64                                                           GNUNET_CONFIGURATION_Handle
65                                                           *cfg,
66                                                           struct
67                                                           GNUNET_SCHEDULER_Handle
68                                                           *sched);
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,
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, 
88  *                GNUNET_NO on timeout/queue drop
89  *                GNUNET_YES on success
90  * @param msg NULL on success, otherwise an error message
91  */
92 typedef void (*GNUNET_DATASTORE_ContinuationWithStatus)(void *cls,
93                                                         int success,
94                                                         const char *msg);
95
96
97 /**
98  * Reserve space in the datastore.  This function should be used
99  * to avoid "out of space" failures during a longer sequence of "put"
100  * operations (for example, when a file is being inserted).
101  *
102  * @param h handle to the datastore
103  * @param amount how much space (in bytes) should be reserved (for content only)
104  * @param entries how many entries will be created (to calculate per-entry overhead)
105  * @param queue_priority ranking of this request in the priority queue
106  * @param max_queue_size at what queue size should this request be dropped
107  *        (if other requests of higher priority are in the queue)
108  * @param timeout how long to wait at most for a response (or before dying in queue)
109  * @param cont continuation to call when done; "success" will be set to
110  *             a positive reservation value if space could be reserved.
111  * @param cont_cls closure for cont
112  * @return NULL if the entry was not queued, otherwise a handle that can be used to
113  *         cancel; note that even if NULL is returned, the callback will be invoked
114  *         (or rather, will already have been invoked)
115  */
116 struct GNUNET_DATASTORE_QueueEntry *
117 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
118                           uint64_t amount,
119                           uint32_t entries,
120                           unsigned int queue_priority,
121                           unsigned int max_queue_size,
122                           struct GNUNET_TIME_Relative timeout,
123                           GNUNET_DATASTORE_ContinuationWithStatus cont,
124                           void *cont_cls);
125
126
127 /**
128  * Store an item in the datastore.  If the item is already present,
129  * the priorities are summed up and the higher expiration time and
130  * lower anonymity level is used.
131  *
132  * @param h handle to the datastore
133  * @param rid reservation ID to use (from "reserve"); use 0 if no
134  *            prior reservation was made
135  * @param key key for the value
136  * @param size number of bytes in data
137  * @param data content stored
138  * @param type type of the content
139  * @param priority priority of the content
140  * @param anonymity anonymity-level for the content
141  * @param expiration expiration time for the content
142  * @param queue_priority ranking of this request in the priority queue
143  * @param max_queue_size at what queue size should this request be dropped
144  *        (if other requests of higher priority are in the queue)
145  * @param timeout timeout for the operation
146  * @param cont continuation to call when done
147  * @param cont_cls closure for cont
148  * @return NULL if the entry was not queued, otherwise a handle that can be used to
149  *         cancel; note that even if NULL is returned, the callback will be invoked
150  *         (or rather, will already have been invoked)
151  */
152 struct GNUNET_DATASTORE_QueueEntry *
153 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
154                       int rid,
155                       const GNUNET_HashCode * key,
156                       size_t size,
157                       const void *data,
158                       enum GNUNET_BLOCK_Type type,
159                       uint32_t priority,
160                       uint32_t anonymity,
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 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                                   int rid,
193                                   unsigned int queue_priority,
194                                   unsigned int max_queue_size,
195                                   struct GNUNET_TIME_Relative timeout,
196                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
197                                   void *cont_cls);
198
199
200 /**
201  * Update a value in the datastore.
202  *
203  * @param h handle to the datastore
204  * @param uid identifier for the value
205  * @param priority how much to increase the priority of the value
206  * @param expiration new expiration value should be MAX of existing and this argument
207  * @param queue_priority ranking of this request in the priority queue
208  * @param max_queue_size at what queue size should this request be dropped
209  *        (if other requests of higher priority are in the queue)
210  * @param timeout how long to wait at most for a response
211  * @param cont continuation to call when done
212  * @param cont_cls closure for cont
213  * @return NULL if the entry was not queued, otherwise a handle that can be used to
214  *         cancel; note that even if NULL is returned, the callback will be invoked
215  *         (or rather, will already have been invoked)
216  */
217 struct GNUNET_DATASTORE_QueueEntry *
218 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
219                          unsigned long long uid,
220                          uint32_t priority,
221                          struct GNUNET_TIME_Absolute expiration,
222                          unsigned int queue_priority,
223                          unsigned int max_queue_size,
224                          struct GNUNET_TIME_Relative timeout,
225                          GNUNET_DATASTORE_ContinuationWithStatus cont,
226                          void *cont_cls);
227
228
229 /**
230  * Explicitly remove some content from the database.
231  * The "cont"inuation will be called with status
232  * "GNUNET_OK" if content was removed, "GNUNET_NO"
233  * if no matching entry was found and "GNUNET_SYSERR"
234  * on all other types of errors.
235  *
236  * @param h handle to the datastore
237  * @param key key for the value
238  * @param size number of bytes in data
239  * @param data content stored
240  * @param queue_priority ranking of this request in the priority queue
241  * @param max_queue_size at what queue size should this request be dropped
242  *        (if other requests of higher priority are in the queue)
243  * @param timeout how long to wait at most for a response
244  * @param cont continuation to call when done
245  * @param cont_cls closure for cont
246  * @return NULL if the entry was not queued, otherwise a handle that can be used to
247  *         cancel; note that even if NULL is returned, the callback will be invoked
248  *         (or rather, will already have been invoked)
249  */
250 struct GNUNET_DATASTORE_QueueEntry *
251 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
252                          const GNUNET_HashCode *key,
253                          size_t size, 
254                          const void *data,
255                          unsigned int queue_priority,
256                          unsigned int max_queue_size,
257                          struct GNUNET_TIME_Relative timeout,
258                          GNUNET_DATASTORE_ContinuationWithStatus cont,
259                          void *cont_cls);
260
261
262 /**
263  * An iterator over a set of items stored in the datastore.
264  *
265  * @param cls closure
266  * @param key key for the content
267  * @param size number of bytes in data
268  * @param data content stored
269  * @param type type of the content
270  * @param priority priority of the content
271  * @param anonymity anonymity-level for the content
272  * @param expiration expiration time for the content
273  * @param uid unique identifier for the datum;
274  *        maybe 0 if no unique identifier is available
275  */
276 typedef void (*GNUNET_DATASTORE_Iterator) (void *cls,
277                                            const GNUNET_HashCode * key,
278                                            size_t size,
279                                            const void *data,
280                                            enum GNUNET_BLOCK_Type type,
281                                            uint32_t priority,
282                                            uint32_t anonymity,
283                                            struct GNUNET_TIME_Absolute
284                                            expiration, uint64_t uid);
285
286
287 /**
288  * Iterate over the results for a particular key
289  * in the datastore.  The iterator will only be called
290  * once initially; if the first call did contain a
291  * result, further results can be obtained by calling
292  * "GNUNET_DATASTORE_get_next" with the given argument.
293  *
294  * @param h handle to the datastore
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 iter function to call on each matching value;
302  *        will be called once with a NULL value at the end
303  * @param iter_cls closure for iter
304  * @return NULL if the entry was not queued, otherwise a handle that can be used to
305  *         cancel; note that even if NULL is returned, the callback will be invoked
306  *         (or rather, will already have been invoked)
307  */
308 struct GNUNET_DATASTORE_QueueEntry *
309 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
310                       const 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_Iterator iter, 
316                       void *iter_cls);
317
318
319 /**
320  * Function called to trigger obtaining the next result
321  * from the datastore.
322  * 
323  * @param h handle to the datastore
324  * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
325  *        iteration (with a final call to "iter" with key/data == NULL).
326  */
327 void
328 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
329                            int more);
330
331
332 /**
333  * Get a random value from the datastore.
334  *
335  * @param h handle to the datastore
336  * @param queue_priority ranking of this request in the priority queue
337  * @param max_queue_size at what queue size should this request be dropped
338  *        (if other requests of higher priority are in the queue)
339  * @param timeout how long to wait at most for a response
340  * @param iter function to call on a random value; it
341  *        will be called once with a value (if available)
342  *        and always once with a value of NULL.
343  * @param iter_cls closure for iter
344  * @return NULL if the entry was not queued, otherwise a handle that can be used to
345  *         cancel; note that even if NULL is returned, the callback will be invoked
346  *         (or rather, will already have been invoked)
347  */
348 struct GNUNET_DATASTORE_QueueEntry *
349 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
350                              unsigned int queue_priority,
351                              unsigned int max_queue_size,
352                              struct GNUNET_TIME_Relative timeout,
353                              GNUNET_DATASTORE_Iterator iter, 
354                              void *iter_cls);
355
356
357 /**
358  * Get a zero-anonymity value from the datastore.
359  *
360  * @param h handle to the datastore
361  * @param queue_priority ranking of this request in the priority queue
362  * @param max_queue_size at what queue size should this request be dropped
363  *        (if other requests of higher priority are in the queue)
364  * @param timeout how long to wait at most for a response
365  * @param type allowed type for the operation
366  * @param iter 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 iter_cls closure for iter
370  * @return NULL if the entry was not queued, otherwise a handle that can be used to
371  *         cancel; note that even if NULL is returned, the callback will be invoked
372  *         (or rather, will already have been invoked)
373  */
374 struct GNUNET_DATASTORE_QueueEntry *
375 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
376                                      unsigned int queue_priority,
377                                      unsigned int max_queue_size,
378                                      struct GNUNET_TIME_Relative timeout,
379                                      enum GNUNET_BLOCK_Type type,
380                                      GNUNET_DATASTORE_Iterator iter, 
381                                      void *iter_cls);
382
383
384 /**
385  * Cancel a datastore operation.  The final callback from the
386  * operation must not have been done yet.
387  * 
388  * @param qe operation to cancel
389  */
390 void
391 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe);
392
393
394 #if 0                           /* keep Emacsens' auto-indent happy */
395 {
396 #endif
397 #ifdef __cplusplus
398 }
399 #endif
400
401 /* end of gnunet_datastore_service.h */
402 #endif