some optimization work...
[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 /**
57  * Connect to the datastore service.
58  *
59  * @param cfg configuration to use
60  * @return handle to use to access the service
61  */
62 struct GNUNET_DATASTORE_Handle *GNUNET_DATASTORE_connect (const struct
63                                                           GNUNET_CONFIGURATION_Handle
64                                                           *cfg);
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 (including timeout/queue drop)
84  *                GNUNET_NO if content was already there
85  *                GNUNET_YES (or other positive value) on success
86  * @param msg NULL on success, otherwise an error message
87  */
88 typedef void (*GNUNET_DATASTORE_ContinuationWithStatus)(void *cls,
89                                                         int32_t 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 and replication values are summed up and the higher
126  * expiration time and 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 replication how often should the content be replicated to other peers?
138  * @param expiration expiration time for the content
139  * @param queue_priority ranking of this request in the priority queue
140  * @param max_queue_size at what queue size should this request be dropped
141  *        (if other requests of higher priority are in the queue)
142  * @param timeout timeout for the operation
143  * @param cont continuation to call when done
144  * @param cont_cls closure for cont
145  * @return NULL if the entry was not queued, otherwise a handle that can be used to
146  *         cancel; note that even if NULL is returned, the callback will be invoked
147  *         (or rather, will already have been invoked)
148  */
149 struct GNUNET_DATASTORE_QueueEntry *
150 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
151                       uint32_t rid,
152                       const GNUNET_HashCode * key,
153                       size_t size,
154                       const void *data,
155                       enum GNUNET_BLOCK_Type type,
156                       uint32_t priority,
157                       uint32_t anonymity,
158                       uint32_t replication,
159                       struct GNUNET_TIME_Absolute expiration,
160                       unsigned int queue_priority,
161                       unsigned int max_queue_size,
162                       struct GNUNET_TIME_Relative timeout,
163                       GNUNET_DATASTORE_ContinuationWithStatus cont,
164                       void *cont_cls);
165
166
167 /**
168  * Signal that all of the data for which a reservation was made has
169  * been stored and that whatever excess space might have been reserved
170  * can now be released.
171  *
172  * @param h handle to the datastore
173  * @param rid reservation ID (value of "success" in original continuation
174  *        from the "reserve" function).
175  * @param queue_priority ranking of this request in the priority queue
176  * @param max_queue_size at what queue size should this request be dropped
177  *        (if other requests of higher priority are in the queue)
178  * @param queue_priority ranking of this request in the priority queue
179  * @param max_queue_size at what queue size should this request be dropped
180  *        (if other requests of higher priority are in the queue)
181  * @param timeout how long to wait at most for a response
182  * @param cont continuation to call when done
183  * @param cont_cls closure for cont
184  * @return NULL if the entry was not queued, otherwise a handle that can be used to
185  *         cancel; note that even if NULL is returned, the callback will be invoked
186  *         (or rather, will already have been invoked)
187  */
188 struct GNUNET_DATASTORE_QueueEntry *
189 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
190                                   uint32_t rid,
191                                   unsigned int queue_priority,
192                                   unsigned int max_queue_size,
193                                   struct GNUNET_TIME_Relative timeout,
194                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
195                                   void *cont_cls);
196
197
198 /**
199  * Update a value in the datastore.
200  *
201  * @param h handle to the datastore
202  * @param uid identifier for the value
203  * @param priority how much to increase the priority of the value
204  * @param expiration new expiration value should be MAX of existing and this argument
205  * @param queue_priority ranking of this request in the priority queue
206  * @param max_queue_size at what queue size should this request be dropped
207  *        (if other requests of higher priority are in the queue)
208  * @param timeout how long to wait at most for a response
209  * @param cont continuation to call when done
210  * @param cont_cls closure for cont
211  * @return NULL if the entry was not queued, otherwise a handle that can be used to
212  *         cancel; note that even if NULL is returned, the callback will be invoked
213  *         (or rather, will already have been invoked)
214  */
215 struct GNUNET_DATASTORE_QueueEntry *
216 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
217                          uint64_t uid,
218                          uint32_t priority,
219                          struct GNUNET_TIME_Absolute expiration,
220                          unsigned int queue_priority,
221                          unsigned int max_queue_size,
222                          struct GNUNET_TIME_Relative timeout,
223                          GNUNET_DATASTORE_ContinuationWithStatus cont,
224                          void *cont_cls);
225
226
227 /**
228  * Explicitly remove some content from the database.
229  * The "cont"inuation will be called with status
230  * "GNUNET_OK" if content was removed, "GNUNET_NO"
231  * if no matching entry was found and "GNUNET_SYSERR"
232  * on all other types of errors.
233  *
234  * @param h handle to the datastore
235  * @param key key for the value
236  * @param size number of bytes in 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 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 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  * An iterator over a set of items 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 (*GNUNET_DATASTORE_Iterator) (void *cls,
275                                            const GNUNET_HashCode * key,
276                                            size_t size,
277                                            const void *data,
278                                            enum GNUNET_BLOCK_Type type,
279                                            uint32_t priority,
280                                            uint32_t anonymity,
281                                            struct GNUNET_TIME_Absolute
282                                            expiration, uint64_t uid);
283
284
285 /**
286  * Iterate over the results for a particular key
287  * in the datastore.  The iterator will only be called
288  * once initially; if the first call did contain a
289  * result, further results can be obtained by calling
290  * "GNUNET_DATASTORE_iterate_get_next" with the given argument.
291  *
292  * @param h handle to the datastore
293  * @param key maybe NULL (to match all entries)
294  * @param type desired type, 0 for any
295  * @param queue_priority ranking of this request in the priority queue
296  * @param max_queue_size at what queue size should this request be dropped
297  *        (if other requests of higher priority are in the queue)
298  * @param timeout how long to wait at most for a response
299  * @param iter function to call on each matching value;
300  *        will be called once with a NULL value at the end
301  * @param iter_cls closure for iter
302  * @return NULL if the entry was not queued, otherwise a handle that can be used to
303  *         cancel; note that even if NULL is returned, the callback will be invoked
304  *         (or rather, will already have been invoked)
305  */
306 struct GNUNET_DATASTORE_QueueEntry *
307 GNUNET_DATASTORE_iterate_key (struct GNUNET_DATASTORE_Handle *h,
308                               const GNUNET_HashCode * key,
309                               enum GNUNET_BLOCK_Type type,
310                               unsigned int queue_priority,
311                               unsigned int max_queue_size,
312                               struct GNUNET_TIME_Relative timeout,
313                               GNUNET_DATASTORE_Iterator iter, 
314                               void *iter_cls);
315
316
317 /**
318  * Get all zero-anonymity values from the datastore.
319  *
320  * @param h handle to the datastore
321  * @param queue_priority ranking of this request in the priority queue
322  * @param max_queue_size at what queue size should this request be dropped
323  *        (if other requests of higher priority are in the queue)
324  * @param timeout how long to wait at most for a response
325  * @param type allowed type for the operation (never zero)
326  * @param iter function to call on a random value; it
327  *        will be called once with a value (if available)
328  *        and always once with a value of NULL at the end.
329  * @param iter_cls closure for iter
330  * @return NULL if the entry was not queued, otherwise a handle that can be used to
331  *         cancel; note that even if NULL is returned, the callback will be invoked
332  *         (or rather, will already have been invoked)
333  */
334 struct GNUNET_DATASTORE_QueueEntry *
335 GNUNET_DATASTORE_iterate_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
336                                          unsigned int queue_priority,
337                                          unsigned int max_queue_size,
338                                          struct GNUNET_TIME_Relative timeout,
339                                          enum GNUNET_BLOCK_Type type,
340                                          GNUNET_DATASTORE_Iterator iter, 
341                                          void *iter_cls);
342
343
344 /**
345  * Function called to trigger obtaining the next result
346  * from the datastore.  ONLY applies for 'GNUNET_DATASTORE_iterate_*'
347  * calls, not for 'get' calls.  FIXME: how much mixing of iterate
348  * calls with other operations can we permit!?  Should we pass
349  * the 'QueueEntry' instead of the datastore handle here instead?
350  * 
351  * @param h handle to the datastore
352  */
353 void
354 GNUNET_DATASTORE_iterate_get_next (struct GNUNET_DATASTORE_Handle *h);
355
356
357 /**
358  * Get a random value from the datastore for content replication.
359  * Returns a single, random value among those with the highest
360  * replication score, lowering positive replication scores by one for
361  * the chosen value (if only content with a replication score exists,
362  * a random value is returned and replication scores are not changed).
363  *
364  * @param h handle to the datastore
365  * @param queue_priority ranking of this request in the priority queue
366  * @param max_queue_size at what queue size should this request be dropped
367  *        (if other requests of higher priority are in the queue)
368  * @param timeout how long to wait at most for a response
369  * @param iter function to call on a random value; it
370  *        will be called once with a value (if available)
371  *        and always once with a value of NULL.
372  * @param iter_cls closure for iter
373  * @return NULL if the entry was not queued, otherwise a handle that can be used to
374  *         cancel; note that even if NULL is returned, the callback will be invoked
375  *         (or rather, will already have been invoked)
376  */
377 struct GNUNET_DATASTORE_QueueEntry *
378 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
379                                       unsigned int queue_priority,
380                                       unsigned int max_queue_size,
381                                       struct GNUNET_TIME_Relative timeout,
382                                       GNUNET_DATASTORE_Iterator iter, 
383                                       void *iter_cls);
384
385
386
387 /**
388  * Cancel a datastore operation.  The final callback from the
389  * operation must not have been done yet.
390  * 
391  * @param qe operation to cancel
392  */
393 void
394 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe);
395
396
397 #if 0                           /* keep Emacsens' auto-indent happy */
398 {
399 #endif
400 #ifdef __cplusplus
401 }
402 #endif
403
404 /* end of gnunet_datastore_service.h */
405 #endif