wip
[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,
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 msg NULL on success, otherwise an error message
91  */
92 typedef void (*GNUNET_DATASTORE_ContinuationWithStatus)(void *cls,
93                                                         int32_t 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 and replication values are summed up and the higher
130  * expiration time and 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 replication how often should the content be replicated to other peers?
142  * @param expiration expiration time for the content
143  * @param queue_priority ranking of this request in the priority queue
144  * @param max_queue_size at what queue size should this request be dropped
145  *        (if other requests of higher priority are in the queue)
146  * @param timeout timeout for the operation
147  * @param cont continuation to call when done
148  * @param cont_cls closure for cont
149  * @return NULL if the entry was not queued, otherwise a handle that can be used to
150  *         cancel; note that even if NULL is returned, the callback will be invoked
151  *         (or rather, will already have been invoked)
152  */
153 struct GNUNET_DATASTORE_QueueEntry *
154 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
155                       uint32_t rid,
156                       const GNUNET_HashCode * key,
157                       size_t size,
158                       const void *data,
159                       enum GNUNET_BLOCK_Type type,
160                       uint32_t priority,
161                       uint32_t anonymity,
162                       uint32_t replication,
163                       struct GNUNET_TIME_Absolute expiration,
164                       unsigned int queue_priority,
165                       unsigned int max_queue_size,
166                       struct GNUNET_TIME_Relative timeout,
167                       GNUNET_DATASTORE_ContinuationWithStatus cont,
168                       void *cont_cls);
169
170
171 /**
172  * Signal that all of the data for which a reservation was made has
173  * been stored and that whatever excess space might have been reserved
174  * can now be released.
175  *
176  * @param h handle to the datastore
177  * @param rid reservation ID (value of "success" in original continuation
178  *        from the "reserve" function).
179  * @param queue_priority ranking of this request in the priority queue
180  * @param max_queue_size at what queue size should this request be dropped
181  *        (if other requests of higher priority are in the queue)
182  * @param queue_priority ranking of this request in the priority queue
183  * @param max_queue_size at what queue size should this request be dropped
184  *        (if other requests of higher priority are in the queue)
185  * @param timeout how long to wait at most for a response
186  * @param cont continuation to call when done
187  * @param cont_cls closure for cont
188  * @return NULL if the entry was not queued, otherwise a handle that can be used to
189  *         cancel; note that even if NULL is returned, the callback will be invoked
190  *         (or rather, will already have been invoked)
191  */
192 struct GNUNET_DATASTORE_QueueEntry *
193 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
194                                   uint32_t rid,
195                                   unsigned int queue_priority,
196                                   unsigned int max_queue_size,
197                                   struct GNUNET_TIME_Relative timeout,
198                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
199                                   void *cont_cls);
200
201
202 /**
203  * Update a value in the datastore.
204  *
205  * @param h handle to the datastore
206  * @param uid identifier for the value
207  * @param priority how much to increase the priority of the value
208  * @param expiration new expiration value should be MAX of existing and this argument
209  * @param queue_priority ranking of this request in the priority queue
210  * @param max_queue_size at what queue size should this request be dropped
211  *        (if other requests of higher priority are in the queue)
212  * @param timeout how long to wait at most for a response
213  * @param cont continuation to call when done
214  * @param cont_cls closure for cont
215  * @return NULL if the entry was not queued, otherwise a handle that can be used to
216  *         cancel; note that even if NULL is returned, the callback will be invoked
217  *         (or rather, will already have been invoked)
218  */
219 struct GNUNET_DATASTORE_QueueEntry *
220 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
221                          uint64_t uid,
222                          uint32_t priority,
223                          struct GNUNET_TIME_Absolute expiration,
224                          unsigned int queue_priority,
225                          unsigned int max_queue_size,
226                          struct GNUNET_TIME_Relative timeout,
227                          GNUNET_DATASTORE_ContinuationWithStatus cont,
228                          void *cont_cls);
229
230
231 /**
232  * Explicitly remove some content from the database.
233  * The "cont"inuation will be called with status
234  * "GNUNET_OK" if content was removed, "GNUNET_NO"
235  * if no matching entry was found and "GNUNET_SYSERR"
236  * on all other types of errors.
237  *
238  * @param h handle to the datastore
239  * @param key key for the value
240  * @param size number of bytes in data
241  * @param data content stored
242  * @param queue_priority ranking of this request in the priority queue
243  * @param max_queue_size at what queue size should this request be dropped
244  *        (if other requests of higher priority are in the queue)
245  * @param timeout how long to wait at most for a response
246  * @param cont continuation to call when done
247  * @param cont_cls closure for cont
248  * @return NULL if the entry was not queued, otherwise a handle that can be used to
249  *         cancel; note that even if NULL is returned, the callback will be invoked
250  *         (or rather, will already have been invoked)
251  */
252 struct GNUNET_DATASTORE_QueueEntry *
253 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
254                          const GNUNET_HashCode *key,
255                          size_t size, 
256                          const void *data,
257                          unsigned int queue_priority,
258                          unsigned int max_queue_size,
259                          struct GNUNET_TIME_Relative timeout,
260                          GNUNET_DATASTORE_ContinuationWithStatus cont,
261                          void *cont_cls);
262
263
264 /**
265  * An iterator over a set of items stored in the datastore.
266  *
267  * @param cls closure
268  * @param key key for the content
269  * @param size number of bytes in data
270  * @param data content stored
271  * @param type type of the content
272  * @param priority priority of the content
273  * @param anonymity anonymity-level for the content
274  * @param expiration expiration time for the content
275  * @param uid unique identifier for the datum;
276  *        maybe 0 if no unique identifier is available
277  */
278 typedef void (*GNUNET_DATASTORE_Iterator) (void *cls,
279                                            const GNUNET_HashCode * key,
280                                            size_t size,
281                                            const void *data,
282                                            enum GNUNET_BLOCK_Type type,
283                                            uint32_t priority,
284                                            uint32_t anonymity,
285                                            struct GNUNET_TIME_Absolute
286                                            expiration, uint64_t uid);
287
288
289 /**
290  * Iterate over the results for a particular key
291  * in the datastore.  The iterator will only be called
292  * once initially; if the first call did contain a
293  * result, further results can be obtained by calling
294  * "GNUNET_DATASTORE_iterate_get_next" with the given argument.
295  *
296  * @param h handle to the datastore
297  * @param key maybe NULL (to match all entries)
298  * @param type desired type, 0 for any
299  * @param queue_priority ranking of this request in the priority queue
300  * @param max_queue_size at what queue size should this request be dropped
301  *        (if other requests of higher priority are in the queue)
302  * @param timeout how long to wait at most for a response
303  * @param iter function to call on each matching value;
304  *        will be called once with a NULL value at the end
305  * @param iter_cls closure for iter
306  * @return NULL if the entry was not queued, otherwise a handle that can be used to
307  *         cancel; note that even if NULL is returned, the callback will be invoked
308  *         (or rather, will already have been invoked)
309  */
310 struct GNUNET_DATASTORE_QueueEntry *
311 GNUNET_DATASTORE_iterate_key (struct GNUNET_DATASTORE_Handle *h,
312                               const GNUNET_HashCode * key,
313                               enum GNUNET_BLOCK_Type type,
314                               unsigned int queue_priority,
315                               unsigned int max_queue_size,
316                               struct GNUNET_TIME_Relative timeout,
317                               GNUNET_DATASTORE_Iterator iter, 
318                               void *iter_cls);
319
320
321 /**
322  * Get all zero-anonymity values from the datastore.
323  *
324  * @param h handle to the datastore
325  * @param queue_priority ranking of this request in the priority queue
326  * @param max_queue_size at what queue size should this request be dropped
327  *        (if other requests of higher priority are in the queue)
328  * @param timeout how long to wait at most for a response
329  * @param type allowed type for the operation (never zero)
330  * @param iter function to call on a random value; it
331  *        will be called once with a value (if available)
332  *        and always once with a value of NULL at the end.
333  * @param iter_cls closure for iter
334  * @return NULL if the entry was not queued, otherwise a handle that can be used to
335  *         cancel; note that even if NULL is returned, the callback will be invoked
336  *         (or rather, will already have been invoked)
337  */
338 struct GNUNET_DATASTORE_QueueEntry *
339 GNUNET_DATASTORE_iterate_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
340                                          unsigned int queue_priority,
341                                          unsigned int max_queue_size,
342                                          struct GNUNET_TIME_Relative timeout,
343                                          enum GNUNET_BLOCK_Type type,
344                                          GNUNET_DATASTORE_Iterator iter, 
345                                          void *iter_cls);
346
347
348 /**
349  * Function called to trigger obtaining the next result
350  * from the datastore.  ONLY applies for 'GNUNET_DATASTORE_iterate_*'
351  * calls, not for 'get' calls.  FIXME: how much mixing of iterate
352  * calls with other operations can we permit!?  Should we pass
353  * the 'QueueEntry' instead of the datastore handle here instead?
354  * 
355  * @param h handle to the datastore
356  */
357 void
358 GNUNET_DATASTORE_iterate_get_next (struct GNUNET_DATASTORE_Handle *h);
359
360
361 /**
362  * Get a random value from the datastore for content replication.
363  * Returns a single, random value among those with the highest
364  * replication score, lowering positive replication scores by one for
365  * the chosen value (if only content with a replication score exists,
366  * a random value is returned and replication scores are not changed).
367  *
368  * @param h handle to the datastore
369  * @param queue_priority ranking of this request in the priority queue
370  * @param max_queue_size at what queue size should this request be dropped
371  *        (if other requests of higher priority are in the queue)
372  * @param timeout how long to wait at most for a response
373  * @param iter function to call on a random value; it
374  *        will be called once with a value (if available)
375  *        and always once with a value of NULL.
376  * @param iter_cls closure for iter
377  * @return NULL if the entry was not queued, otherwise a handle that can be used to
378  *         cancel; note that even if NULL is returned, the callback will be invoked
379  *         (or rather, will already have been invoked)
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_Iterator iter, 
387                                       void *iter_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 /* end of gnunet_datastore_service.h */
409 #endif