add cancellation
[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 /**
47  * Handle to the datastore service.
48  */
49 struct GNUNET_DATASTORE_Handle;
50
51
52 /**
53  * Connect to the datastore service.
54  *
55  * @param cfg configuration to use
56  * @param sched scheduler to use
57  * @return handle to use to access the service
58  */
59 struct GNUNET_DATASTORE_Handle *GNUNET_DATASTORE_connect (const struct
60                                                           GNUNET_CONFIGURATION_Handle
61                                                           *cfg,
62                                                           struct
63                                                           GNUNET_SCHEDULER_Handle
64                                                           *sched);
65
66
67 /**
68  * Disconnect from the datastore service (and free
69  * associated resources).
70  *
71  * @param h handle to the datastore
72  * @param drop set to GNUNET_YES to delete all data in datastore (!)
73  */
74 void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
75                                   int drop);
76
77
78 /**
79  * Continuation called to notify client about result of the
80  * operation.
81  *
82  * @param cls closure
83  * @param success GNUNET_SYSERR on failure, 
84  *                GNUNET_NO on timeout/queue drop
85  *                GNUNET_YES on success
86  * @param msg NULL on success, otherwise an error message
87  */
88 typedef void (*GNUNET_DATASTORE_ContinuationWithStatus)(void *cls,
89                                                         int success,
90                                                         const char *msg);
91
92
93 /**
94  * Reserve space in the datastore.  This function should be used
95  * to avoid "out of space" failures during a longer sequence of "put"
96  * operations (for example, when a file is being inserted).
97  *
98  * @param h handle to the datastore
99  * @param amount how much space (in bytes) should be reserved (for content only)
100  * @param entries how many entries will be created (to calculate per-entry overhead)
101  * @param queue_priority ranking of this request in the priority queue
102  * @param max_queue_size at what queue size should this request be dropped
103  *        (if other requests of higher priority are in the queue)
104  * @param timeout how long to wait at most for a response (or before dying in queue)
105  * @param cont continuation to call when done; "success" will be set to
106  *             a positive reservation value if space could be reserved.
107  * @param cont_cls closure for cont
108  * @return NULL if the entry was not queued, otherwise a handle that can be used to
109  *         cancel; note that even if NULL is returned, the callback will be invoked
110  *         (or rather, will already have been invoked)
111  */
112 struct GNUNET_DATASTORE_QueueEntry *
113 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
114                           uint64_t amount,
115                           uint32_t entries,
116                           unsigned int queue_priority,
117                           unsigned int max_queue_size,
118                           struct GNUNET_TIME_Relative timeout,
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 are summed up and the higher expiration time and
126  * 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 expiration expiration time for the content
138  * @param queue_priority ranking of this request in the priority queue
139  * @param max_queue_size at what queue size should this request be dropped
140  *        (if other requests of higher priority are in the queue)
141  * @param timeout timeout for the operation
142  * @param cont continuation to call when done
143  * @param cont_cls closure for cont
144  * @return NULL if the entry was not queued, otherwise a handle that can be used to
145  *         cancel; note that even if NULL is returned, the callback will be invoked
146  *         (or rather, will already have been invoked)
147  */
148 struct GNUNET_DATASTORE_QueueEntry *
149 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
150                       int rid,
151                       const GNUNET_HashCode * key,
152                       uint32_t size,
153                       const void *data,
154                       enum GNUNET_BLOCK_Type type,
155                       uint32_t priority,
156                       uint32_t anonymity,
157                       struct GNUNET_TIME_Absolute expiration,
158                       unsigned int queue_priority,
159                       unsigned int max_queue_size,
160                       struct GNUNET_TIME_Relative timeout,
161                       GNUNET_DATASTORE_ContinuationWithStatus cont,
162                       void *cont_cls);
163
164
165 /**
166  * Signal that all of the data for which a reservation was made has
167  * been stored and that whatever excess space might have been reserved
168  * can now be released.
169  *
170  * @param h handle to the datastore
171  * @param rid reservation ID (value of "success" in original continuation
172  *        from the "reserve" function).
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 queue_priority ranking of this request in the priority queue
177  * @param max_queue_size at what queue size should this request be dropped
178  *        (if other requests of higher priority are in the queue)
179  * @param timeout how long to wait at most for a response
180  * @param cont continuation to call when done
181  * @param cont_cls closure for cont
182  * @return NULL if the entry was not queued, otherwise a handle that can be used to
183  *         cancel; note that even if NULL is returned, the callback will be invoked
184  *         (or rather, will already have been invoked)
185  */
186 struct GNUNET_DATASTORE_QueueEntry *
187 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
188                                   int rid,
189                                   unsigned int queue_priority,
190                                   unsigned int max_queue_size,
191                                   struct GNUNET_TIME_Relative timeout,
192                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
193                                   void *cont_cls);
194
195
196 /**
197  * Update a value in the datastore.
198  *
199  * @param h handle to the datastore
200  * @param uid identifier for the value
201  * @param priority how much to increase the priority of the value
202  * @param expiration new expiration value should be MAX of existing and this argument
203  * @param queue_priority ranking of this request in the priority queue
204  * @param max_queue_size at what queue size should this request be dropped
205  *        (if other requests of higher priority are in the queue)
206  * @param timeout how long to wait at most for a response
207  * @param cont continuation to call when done
208  * @param cont_cls closure for cont
209  * @return NULL if the entry was not queued, otherwise a handle that can be used to
210  *         cancel; note that even if NULL is returned, the callback will be invoked
211  *         (or rather, will already have been invoked)
212  */
213 struct GNUNET_DATASTORE_QueueEntry *
214 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
215                          unsigned long long uid,
216                          uint32_t priority,
217                          struct GNUNET_TIME_Absolute expiration,
218                          unsigned int queue_priority,
219                          unsigned int max_queue_size,
220                          struct GNUNET_TIME_Relative timeout,
221                          GNUNET_DATASTORE_ContinuationWithStatus cont,
222                          void *cont_cls);
223
224
225 /**
226  * Explicitly remove some content from the database.
227  * The "cont"inuation will be called with status
228  * "GNUNET_OK" if content was removed, "GNUNET_NO"
229  * if no matching entry was found and "GNUNET_SYSERR"
230  * on all other types of errors.
231  *
232  * @param h handle to the datastore
233  * @param key key for the value
234  * @param size number of bytes in data
235  * @param data content stored
236  * @param queue_priority ranking of this request in the priority queue
237  * @param max_queue_size at what queue size should this request be dropped
238  *        (if other requests of higher priority are in the queue)
239  * @param timeout how long to wait at most for a response
240  * @param cont continuation to call when done
241  * @param cont_cls closure for cont
242  * @return NULL if the entry was not queued, otherwise a handle that can be used to
243  *         cancel; note that even if NULL is returned, the callback will be invoked
244  *         (or rather, will already have been invoked)
245  */
246 struct GNUNET_DATASTORE_QueueEntry *
247 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
248                          const GNUNET_HashCode *key,
249                          uint32_t size, 
250                          const void *data,
251                          unsigned int queue_priority,
252                          unsigned int max_queue_size,
253                          struct GNUNET_TIME_Relative timeout,
254                          GNUNET_DATASTORE_ContinuationWithStatus cont,
255                          void *cont_cls);
256
257
258 /**
259  * An iterator over a set of items stored in the datastore.
260  *
261  * @param cls closure
262  * @param key key for the content
263  * @param size number of bytes in data
264  * @param data content stored
265  * @param type type of the content
266  * @param priority priority of the content
267  * @param anonymity anonymity-level for the content
268  * @param expiration expiration time for the content
269  * @param uid unique identifier for the datum;
270  *        maybe 0 if no unique identifier is available
271  */
272 typedef void (*GNUNET_DATASTORE_Iterator) (void *cls,
273                                            const GNUNET_HashCode * key,
274                                            uint32_t size,
275                                            const void *data,
276                                            enum GNUNET_BLOCK_Type type,
277                                            uint32_t priority,
278                                            uint32_t anonymity,
279                                            struct GNUNET_TIME_Absolute
280                                            expiration, uint64_t uid);
281
282
283 /**
284  * Iterate over the results for a particular key
285  * in the datastore.  The iterator will only be called
286  * once initially; if the first call did contain a
287  * result, further results can be obtained by calling
288  * "GNUNET_DATASTORE_get_next" with the given argument.
289  *
290  * @param h handle to the datastore
291  * @param key maybe NULL (to match all entries)
292  * @param type desired type, 0 for any
293  * @param queue_priority ranking of this request in the priority queue
294  * @param max_queue_size at what queue size should this request be dropped
295  *        (if other requests of higher priority are in the queue)
296  * @param timeout how long to wait at most for a response
297  * @param iter function to call on each matching value;
298  *        will be called once with a NULL value at the end
299  * @param iter_cls closure for iter
300  * @return NULL if the entry was not queued, otherwise a handle that can be used to
301  *         cancel; note that even if NULL is returned, the callback will be invoked
302  *         (or rather, will already have been invoked)
303  */
304 struct GNUNET_DATASTORE_QueueEntry *
305 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
306                       const GNUNET_HashCode * key,
307                       enum GNUNET_BLOCK_Type type,
308                       unsigned int queue_priority,
309                       unsigned int max_queue_size,
310                       struct GNUNET_TIME_Relative timeout,
311                       GNUNET_DATASTORE_Iterator iter, 
312                       void *iter_cls);
313
314
315 /**
316  * Function called to trigger obtaining the next result
317  * from the datastore.
318  * 
319  * @param h handle to the datastore
320  * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
321  *        iteration (with a final call to "iter" with key/data == NULL).
322  */
323 void
324 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
325                            int more);
326
327
328 /**
329  * Get a random value from the datastore.
330  *
331  * @param h handle to the datastore
332  * @param queue_priority ranking of this request in the priority queue
333  * @param max_queue_size at what queue size should this request be dropped
334  *        (if other requests of higher priority are in the queue)
335  * @param timeout how long to wait at most for a response
336  * @param iter function to call on a random value; it
337  *        will be called once with a value (if available)
338  *        and always once with a value of NULL.
339  * @param iter_cls closure for iter
340  * @return NULL if the entry was not queued, otherwise a handle that can be used to
341  *         cancel; note that even if NULL is returned, the callback will be invoked
342  *         (or rather, will already have been invoked)
343  */
344 struct GNUNET_DATASTORE_QueueEntry *
345 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
346                              unsigned int queue_priority,
347                              unsigned int max_queue_size,
348                              struct GNUNET_TIME_Relative timeout,
349                              GNUNET_DATASTORE_Iterator iter, 
350                              void *iter_cls);
351
352 /**
353  * Cancel a datastore operation.  The final callback from the
354  * operation must not have been done yet.
355  * 
356  * @param qe operation to cancel
357  */
358 void
359 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe);
360
361
362 #if 0                           /* keep Emacsens' auto-indent happy */
363 {
364 #endif
365 #ifdef __cplusplus
366 }
367 #endif
368
369 /* end of gnunet_datastore_service.h */
370 #endif