fix zombie-fest
[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
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 cont continuation to call when done
153  * @param cont_cls closure for @a cont
154  * @return NULL if the entry was not queued, otherwise a handle that can be used to
155  *         cancel; note that even if NULL is returned, the callback will be invoked
156  *         (or rather, will already have been invoked)
157  */
158 struct GNUNET_DATASTORE_QueueEntry *
159 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
160                       uint32_t rid,
161                       const struct GNUNET_HashCode *key,
162                       size_t size,
163                       const void *data,
164                       enum GNUNET_BLOCK_Type type,
165                       uint32_t priority,
166                       uint32_t anonymity,
167                       uint32_t replication,
168                       struct GNUNET_TIME_Absolute expiration,
169                       unsigned int queue_priority,
170                       unsigned int max_queue_size,
171                       GNUNET_DATASTORE_ContinuationWithStatus cont,
172                       void *cont_cls);
173
174
175 /**
176  * Signal that all of the data for which a reservation was made has
177  * been stored and that whatever excess space might have been reserved
178  * can now be released.
179  *
180  * @param h handle to the datastore
181  * @param rid reservation ID (value of "success" in original continuation
182  *        from the "reserve" function).
183  * @param queue_priority ranking of this request in the priority queue
184  * @param max_queue_size at what queue size should this request be dropped
185  *        (if other requests of higher priority are in the queue)
186  * @param queue_priority ranking of this request in the priority queue
187  * @param max_queue_size at what queue size should this request be dropped
188  *        (if other requests of higher priority are in the queue)
189  * @param cont continuation to call when done
190  * @param cont_cls closure for @a cont
191  * @return NULL if the entry was not queued, otherwise a handle that can be used to
192  *         cancel; note that even if NULL is returned, the callback will be invoked
193  *         (or rather, will already have been invoked)
194  */
195 struct GNUNET_DATASTORE_QueueEntry *
196 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
197                                   uint32_t rid, unsigned int queue_priority,
198                                   unsigned int max_queue_size,
199                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
200                                   void *cont_cls);
201
202
203 /**
204  * Update a value in the datastore.
205  *
206  * @param h handle to the datastore
207  * @param uid identifier for the value
208  * @param priority how much to increase the priority of the value
209  * @param expiration new expiration value should be MAX of existing and this argument
210  * @param queue_priority ranking of this request in the priority queue
211  * @param max_queue_size at what queue size should this request be dropped
212  *        (if other requests of higher priority are in the queue)
213  * @param cont continuation to call when done
214  * @param cont_cls closure for @a 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                          GNUNET_DATASTORE_ContinuationWithStatus cont,
227                          void *cont_cls);
228
229
230 /**
231  * Explicitly remove some content from the database.  @a cont will be
232  * called with status #GNUNET_OK if content was removed, #GNUNET_NO if
233  * no matching entry was found and #GNUNET_SYSERR on all other types
234  * of errors.
235  *
236  * @param h handle to the datastore
237  * @param key key for the value
238  * @param size number of bytes in @a 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 cont continuation to call when done
244  * @param cont_cls closure for @a cont
245  * @return NULL if the entry was not queued, otherwise a handle that can be used to
246  *         cancel; note that even if NULL is returned, the callback will be invoked
247  *         (or rather, will already have been invoked)
248  */
249 struct GNUNET_DATASTORE_QueueEntry *
250 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
251                          const struct GNUNET_HashCode *key,
252                          size_t size,
253                          const void *data,
254                          unsigned int queue_priority,
255                          unsigned int max_queue_size,
256                          GNUNET_DATASTORE_ContinuationWithStatus cont,
257                          void *cont_cls);
258
259
260 /**
261  * Process a datum that was 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
275 (*GNUNET_DATASTORE_DatumProcessor) (void *cls,
276                                     const struct GNUNET_HashCode *key,
277                                     size_t size,
278                                     const void *data,
279                                     enum GNUNET_BLOCK_Type type,
280                                     uint32_t priority,
281                                     uint32_t anonymity,
282                                     struct GNUNET_TIME_Absolute expiration,
283                                     uint64_t uid);
284
285
286 /**
287  * Get a result for a particular key from the datastore.  The processor
288  * will only be called once.
289  *
290  * @param h handle to the datastore
291  * @param offset offset of the result (modulo num-results); set to
292  *               a random 64-bit value initially; then increment by
293  *               one each time; detect that all results have been found by uid
294  *               being again the first uid ever returned.
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 proc function to call on a matching value;
301  *        or with a NULL value if no datum matches
302  * @param proc_cls closure for @a proc
303  * @return NULL if the entry was not queued, otherwise a handle that can be used to
304  *         cancel
305  */
306 struct GNUNET_DATASTORE_QueueEntry *
307 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
308                           uint64_t offset,
309                           const struct GNUNET_HashCode *key,
310                           enum GNUNET_BLOCK_Type type,
311                           unsigned int queue_priority,
312                           unsigned int max_queue_size,
313                           GNUNET_DATASTORE_DatumProcessor proc,
314                           void *proc_cls);
315
316
317 /**
318  * Get a single zero-anonymity value from the datastore.
319  * Note that some implementations can ignore the 'offset' and
320  * instead return a random zero-anonymity value.  In that case,
321  * detecting the wrap-around based on a repeating UID is at best
322  * probabilistic.
323  *
324  * @param h handle to the datastore
325  * @param offset offset of the result (modulo num-results); set to
326  *               a random 64-bit value initially; then increment by
327  *               one each time; detect that all results have been found by uid
328  *               being again the first uid ever returned.
329  * @param queue_priority ranking of this request in the priority queue
330  * @param max_queue_size at what queue size should this request be dropped
331  *        (if other requests of higher priority are in the queue)
332  * @param type allowed type for the operation (never zero)
333  * @param proc function to call on a random value; it
334  *        will be called once with a value (if available)
335  *        or with NULL if none value exists.
336  * @param proc_cls closure for proc
337  * @return NULL if the entry was not queued, otherwise a handle that can be used to
338  *         cancel
339  */
340 struct GNUNET_DATASTORE_QueueEntry *
341 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
342                                      uint64_t offset,
343                                      unsigned int queue_priority,
344                                      unsigned int max_queue_size,
345                                      enum GNUNET_BLOCK_Type type,
346                                      GNUNET_DATASTORE_DatumProcessor proc,
347                                      void *proc_cls);
348
349
350 /**
351  * Get a random value from the datastore for content replication.
352  * Returns a single, random value among those with the highest
353  * replication score, lowering positive replication scores by one for
354  * the chosen value (if only content with a replication score exists,
355  * a random value is returned and replication scores are not changed).
356  *
357  * @param h handle to the datastore
358  * @param queue_priority ranking of this request in the priority queue
359  * @param max_queue_size at what queue size should this request be dropped
360  *        (if other requests of higher priority are in the queue)
361  * @param proc function to call on a random value; it
362  *        will be called once with a value (if available)
363  *        and always once with a value of NULL.
364  * @param proc_cls closure for @a proc
365  * @return NULL if the entry was not queued, otherwise a handle that can be used to
366  *         cancel
367  */
368 struct GNUNET_DATASTORE_QueueEntry *
369 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
370                                       unsigned int queue_priority,
371                                       unsigned int max_queue_size,
372                                       GNUNET_DATASTORE_DatumProcessor proc,
373                                       void *proc_cls);
374
375
376
377 /**
378  * Cancel a datastore operation.  The final callback from the
379  * operation must not have been done yet.
380  *
381  * @param qe operation to cancel
382  */
383 void
384 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe);
385
386
387 #if 0                           /* keep Emacsens' auto-indent happy */
388 {
389 #endif
390 #ifdef __cplusplus
391 }
392 #endif
393
394 #endif
395
396 /** @} */  /* end of group */