- refactor to check messages from both enc systems
[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 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 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., 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 *
67 GNUNET_DATASTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
68
69
70 /**
71  * Disconnect from the datastore service (and free
72  * associated resources).
73  *
74  * @param h handle to the datastore
75  * @param drop set to GNUNET_YES to delete all data in datastore (!)
76  */
77 void
78 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h, int drop);
79
80
81 /**
82  * Continuation called to notify client about result of the
83  * operation.
84  *
85  * @param cls closure
86  * @param success #GNUNET_SYSERR on failure (including timeout/queue drop)
87  *                #GNUNET_NO if content was already there
88  *                #GNUNET_YES (or other positive value) on success
89  * @param min_expiration minimum expiration time required for 0-priority content to be stored
90  *                by the datacache at this time, zero for unknown, forever if we have no
91  *                space for 0-priority content
92  * @param msg NULL on success, otherwise an error message
93  */
94 typedef void (*GNUNET_DATASTORE_ContinuationWithStatus) (void *cls,
95                                                          int32_t success,
96                                                          struct GNUNET_TIME_Absolute min_expiration,
97                                                          const char *msg);
98
99
100 /**
101  * Reserve space in the datastore.  This function should be used
102  * to avoid "out of space" failures during a longer sequence of "put"
103  * operations (for example, when a file is being inserted).
104  *
105  * @param h handle to the datastore
106  * @param amount how much space (in bytes) should be reserved (for content only)
107  * @param entries how many entries will be created (to calculate per-entry overhead)
108  * @param cont continuation to call when done; "success" will be set to
109  *             a positive reservation value if space could be reserved.
110  * @param cont_cls closure for @a cont
111  * @return NULL if the entry was not queued, otherwise a handle that can be used to
112  *         cancel; note that even if NULL is returned, the callback will be invoked
113  *         (or rather, will already have been invoked)
114  */
115 struct GNUNET_DATASTORE_QueueEntry *
116 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
117                           uint64_t amount,
118                           uint32_t entries,
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 @a 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, uint32_t rid,
151                       const struct GNUNET_HashCode *key, size_t size,
152                       const void *data, enum GNUNET_BLOCK_Type type,
153                       uint32_t priority, uint32_t anonymity,
154                       uint32_t replication,
155                       struct GNUNET_TIME_Absolute expiration,
156                       unsigned int queue_priority, unsigned int max_queue_size,
157                       struct GNUNET_TIME_Relative timeout,
158                       GNUNET_DATASTORE_ContinuationWithStatus cont,
159                       void *cont_cls);
160
161
162 /**
163  * Signal that all of the data for which a reservation was made has
164  * been stored and that whatever excess space might have been reserved
165  * can now be released.
166  *
167  * @param h handle to the datastore
168  * @param rid reservation ID (value of "success" in original continuation
169  *        from the "reserve" function).
170  * @param queue_priority ranking of this request in the priority queue
171  * @param max_queue_size at what queue size should this request be dropped
172  *        (if other requests of higher priority are in the queue)
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 timeout how long to wait at most for a response
177  * @param cont continuation to call when done
178  * @param cont_cls closure for @a cont
179  * @return NULL if the entry was not queued, otherwise a handle that can be used to
180  *         cancel; note that even if NULL is returned, the callback will be invoked
181  *         (or rather, will already have been invoked)
182  */
183 struct GNUNET_DATASTORE_QueueEntry *
184 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
185                                   uint32_t rid, unsigned int queue_priority,
186                                   unsigned int max_queue_size,
187                                   struct GNUNET_TIME_Relative timeout,
188                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
189                                   void *cont_cls);
190
191
192 /**
193  * Update a value in the datastore.
194  *
195  * @param h handle to the datastore
196  * @param uid identifier for the value
197  * @param priority how much to increase the priority of the value
198  * @param expiration new expiration value should be MAX of existing and this argument
199  * @param queue_priority ranking of this request in the priority queue
200  * @param max_queue_size at what queue size should this request be dropped
201  *        (if other requests of higher priority are in the queue)
202  * @param timeout how long to wait at most for a response
203  * @param cont continuation to call when done
204  * @param cont_cls closure for @a cont
205  * @return NULL if the entry was not queued, otherwise a handle that can be used to
206  *         cancel; note that even if NULL is returned, the callback will be invoked
207  *         (or rather, will already have been invoked)
208  */
209 struct GNUNET_DATASTORE_QueueEntry *
210 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
211                          uint64_t uid,
212                          uint32_t priority,
213                          struct GNUNET_TIME_Absolute expiration,
214                          unsigned int queue_priority,
215                          unsigned int max_queue_size,
216                          struct GNUNET_TIME_Relative timeout,
217                          GNUNET_DATASTORE_ContinuationWithStatus cont,
218                          void *cont_cls);
219
220
221 /**
222  * Explicitly remove some content from the database.  @a cont will be
223  * called with status #GNUNET_OK if content was removed, #GNUNET_NO if
224  * no matching entry was found and #GNUNET_SYSERR on all other types
225  * of errors.
226  *
227  * @param h handle to the datastore
228  * @param key key for the value
229  * @param size number of bytes in @a data
230  * @param data content stored
231  * @param queue_priority ranking of this request in the priority queue
232  * @param max_queue_size at what queue size should this request be dropped
233  *        (if other requests of higher priority are in the queue)
234  * @param timeout how long to wait at most for a response
235  * @param cont continuation to call when done
236  * @param cont_cls closure for @a cont
237  * @return NULL if the entry was not queued, otherwise a handle that can be used to
238  *         cancel; note that even if NULL is returned, the callback will be invoked
239  *         (or rather, will already have been invoked)
240  */
241 struct GNUNET_DATASTORE_QueueEntry *
242 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
243                          const struct GNUNET_HashCode *key,
244                          size_t size,
245                          const void *data,
246                          unsigned int queue_priority,
247                          unsigned int max_queue_size,
248                          struct GNUNET_TIME_Relative timeout,
249                          GNUNET_DATASTORE_ContinuationWithStatus cont,
250                          void *cont_cls);
251
252
253 /**
254  * Process a datum that was stored in the datastore.
255  *
256  * @param cls closure
257  * @param key key for the content
258  * @param size number of bytes in data
259  * @param data content stored
260  * @param type type of the content
261  * @param priority priority of the content
262  * @param anonymity anonymity-level for the content
263  * @param expiration expiration time for the content
264  * @param uid unique identifier for the datum;
265  *        maybe 0 if no unique identifier is available
266  */
267 typedef void
268 (*GNUNET_DATASTORE_DatumProcessor) (void *cls,
269                                     const struct GNUNET_HashCode *key,
270                                     size_t size,
271                                     const void *data,
272                                     enum GNUNET_BLOCK_Type type,
273                                     uint32_t priority,
274                                     uint32_t anonymity,
275                                     struct GNUNET_TIME_Absolute expiration,
276                                     uint64_t uid);
277
278
279 /**
280  * Get a result for a particular key from the datastore.  The processor
281  * will only be called once.
282  *
283  * @param h handle to the datastore
284  * @param offset offset of the result (modulo num-results); set to
285  *               a random 64-bit value initially; then increment by
286  *               one each time; detect that all results have been found by uid
287  *               being again the first uid ever returned.
288  * @param key maybe NULL (to match all entries)
289  * @param type desired type, 0 for any
290  * @param queue_priority ranking of this request in the priority queue
291  * @param max_queue_size at what queue size should this request be dropped
292  *        (if other requests of higher priority are in the queue)
293  * @param timeout how long to wait at most for a response
294  * @param proc function to call on a matching value;
295  *        or with a NULL value if no datum matches
296  * @param proc_cls closure for @a proc
297  * @return NULL if the entry was not queued, otherwise a handle that can be used to
298  *         cancel
299  */
300 struct GNUNET_DATASTORE_QueueEntry *
301 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
302                           uint64_t offset,
303                           const struct GNUNET_HashCode *key,
304                           enum GNUNET_BLOCK_Type type,
305                           unsigned int queue_priority,
306                           unsigned int max_queue_size,
307                           struct GNUNET_TIME_Relative timeout,
308                           GNUNET_DATASTORE_DatumProcessor proc,
309                           void *proc_cls);
310
311
312 /**
313  * Get a single zero-anonymity value from the datastore.
314  * Note that some implementations can ignore the 'offset' and
315  * instead return a random zero-anonymity value.  In that case,
316  * detecting the wrap-around based on a repeating UID is at best
317  * probabilistic.
318  *
319  * @param h handle to the datastore
320  * @param offset offset of the result (modulo num-results); set to
321  *               a random 64-bit value initially; then increment by
322  *               one each time; detect that all results have been found by uid
323  *               being again the first uid ever returned.
324  * @param queue_priority ranking of this request in the priority queue
325  * @param max_queue_size at what queue size should this request be dropped
326  *        (if other requests of higher priority are in the queue)
327  * @param timeout how long to wait at most for a response
328  * @param type allowed type for the operation (never zero)
329  * @param proc function to call on a random value; it
330  *        will be called once with a value (if available)
331  *        or with NULL if none value exists.
332  * @param proc_cls closure for proc
333  * @return NULL if the entry was not queued, otherwise a handle that can be used to
334  *         cancel
335  */
336 struct GNUNET_DATASTORE_QueueEntry *
337 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
338                                      uint64_t offset,
339                                      unsigned int queue_priority,
340                                      unsigned int max_queue_size,
341                                      struct GNUNET_TIME_Relative timeout,
342                                      enum GNUNET_BLOCK_Type type,
343                                      GNUNET_DATASTORE_DatumProcessor proc,
344                                      void *proc_cls);
345
346
347 /**
348  * Get a random value from the datastore for content replication.
349  * Returns a single, random value among those with the highest
350  * replication score, lowering positive replication scores by one for
351  * the chosen value (if only content with a replication score exists,
352  * a random value is returned and replication scores are not changed).
353  *
354  * @param h handle to the datastore
355  * @param queue_priority ranking of this request in the priority queue
356  * @param max_queue_size at what queue size should this request be dropped
357  *        (if other requests of higher priority are in the queue)
358  * @param timeout how long to wait at most for a response
359  * @param proc function to call on a random value; it
360  *        will be called once with a value (if available)
361  *        and always once with a value of NULL.
362  * @param proc_cls closure for @a proc
363  * @return NULL if the entry was not queued, otherwise a handle that can be used to
364  *         cancel
365  */
366 struct GNUNET_DATASTORE_QueueEntry *
367 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
368                                       unsigned int queue_priority,
369                                       unsigned int max_queue_size,
370                                       struct GNUNET_TIME_Relative timeout,
371                                       GNUNET_DATASTORE_DatumProcessor proc,
372                                       void *proc_cls);
373
374
375
376 /**
377  * Cancel a datastore operation.  The final callback from the
378  * operation must not have been done yet.
379  *
380  * @param qe operation to cancel
381  */
382 void
383 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe);
384
385
386 #if 0                           /* keep Emacsens' auto-indent happy */
387 {
388 #endif
389 #ifdef __cplusplus
390 }
391 #endif
392
393 /* end of gnunet_datastore_service.h */
394 #endif