fix #3869: outdated FSF address
[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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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,
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 min_expiration minimum expiration time required for 0-priority content to be stored
91  *                by the datacache at this time, zero for unknown, forever if we have no
92  *                space for 0-priority content
93  * @param msg NULL on success, otherwise an error message
94  */
95 typedef void (*GNUNET_DATASTORE_ContinuationWithStatus) (void *cls,
96                                                          int32_t success,
97                                                          struct GNUNET_TIME_Absolute min_expiration,
98                                                          const char *msg);
99
100
101 /**
102  * Reserve space in the datastore.  This function should be used
103  * to avoid "out of space" failures during a longer sequence of "put"
104  * operations (for example, when a file is being inserted).
105  *
106  * @param h handle to the datastore
107  * @param amount how much space (in bytes) should be reserved (for content only)
108  * @param entries how many entries will be created (to calculate per-entry overhead)
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 @a 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                           GNUNET_DATASTORE_ContinuationWithStatus cont,
121                           void *cont_cls);
122
123
124 /**
125  * Store an item in the datastore.  If the item is already present,
126  * the priorities and replication values are summed up and the higher
127  * expiration time and lower anonymity level is used.
128  *
129  * @param h handle to the datastore
130  * @param rid reservation ID to use (from "reserve"); use 0 if no
131  *            prior reservation was made
132  * @param key key for the value
133  * @param size number of bytes in data
134  * @param data content stored
135  * @param type type of the content
136  * @param priority priority of the content
137  * @param anonymity anonymity-level for the content
138  * @param replication how often should the content be replicated to other peers?
139  * @param expiration expiration time for the content
140  * @param queue_priority ranking of this request in the priority queue
141  * @param max_queue_size at what queue size should this request be dropped
142  *        (if other requests of higher priority are in the queue)
143  * @param timeout timeout for the operation
144  * @param cont continuation to call when done
145  * @param cont_cls closure for @a cont
146  * @return NULL if the entry was not queued, otherwise a handle that can be used to
147  *         cancel; note that even if NULL is returned, the callback will be invoked
148  *         (or rather, will already have been invoked)
149  */
150 struct GNUNET_DATASTORE_QueueEntry *
151 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h, uint32_t rid,
152                       const struct GNUNET_HashCode *key, size_t size,
153                       const void *data, enum GNUNET_BLOCK_Type type,
154                       uint32_t priority, uint32_t anonymity,
155                       uint32_t replication,
156                       struct GNUNET_TIME_Absolute expiration,
157                       unsigned int queue_priority, unsigned int max_queue_size,
158                       struct GNUNET_TIME_Relative timeout,
159                       GNUNET_DATASTORE_ContinuationWithStatus cont,
160                       void *cont_cls);
161
162
163 /**
164  * Signal that all of the data for which a reservation was made has
165  * been stored and that whatever excess space might have been reserved
166  * can now be released.
167  *
168  * @param h handle to the datastore
169  * @param rid reservation ID (value of "success" in original continuation
170  *        from the "reserve" function).
171  * @param queue_priority ranking of this request in the priority queue
172  * @param max_queue_size at what queue size should this request be dropped
173  *        (if other requests of higher priority are in the queue)
174  * @param queue_priority ranking of this request in the priority queue
175  * @param max_queue_size at what queue size should this request be dropped
176  *        (if other requests of higher priority are in the queue)
177  * @param timeout how long to wait at most for a response
178  * @param cont continuation to call when done
179  * @param cont_cls closure for @a cont
180  * @return NULL if the entry was not queued, otherwise a handle that can be used to
181  *         cancel; note that even if NULL is returned, the callback will be invoked
182  *         (or rather, will already have been invoked)
183  */
184 struct GNUNET_DATASTORE_QueueEntry *
185 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
186                                   uint32_t rid, unsigned int queue_priority,
187                                   unsigned int max_queue_size,
188                                   struct GNUNET_TIME_Relative timeout,
189                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
190                                   void *cont_cls);
191
192
193 /**
194  * Update a value in the datastore.
195  *
196  * @param h handle to the datastore
197  * @param uid identifier for the value
198  * @param priority how much to increase the priority of the value
199  * @param expiration new expiration value should be MAX of existing and this argument
200  * @param queue_priority ranking of this request in the priority queue
201  * @param max_queue_size at what queue size should this request be dropped
202  *        (if other requests of higher priority are in the queue)
203  * @param timeout how long to wait at most for a response
204  * @param cont continuation to call when done
205  * @param cont_cls closure for @a cont
206  * @return NULL if the entry was not queued, otherwise a handle that can be used to
207  *         cancel; note that even if NULL is returned, the callback will be invoked
208  *         (or rather, will already have been invoked)
209  */
210 struct GNUNET_DATASTORE_QueueEntry *
211 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
212                          uint64_t uid,
213                          uint32_t priority,
214                          struct GNUNET_TIME_Absolute expiration,
215                          unsigned int queue_priority,
216                          unsigned int max_queue_size,
217                          struct GNUNET_TIME_Relative timeout,
218                          GNUNET_DATASTORE_ContinuationWithStatus cont,
219                          void *cont_cls);
220
221
222 /**
223  * Explicitly remove some content from the database.  @a cont will be
224  * called with status #GNUNET_OK if content was removed, #GNUNET_NO if
225  * no matching entry was found and #GNUNET_SYSERR on all other types
226  * of errors.
227  *
228  * @param h handle to the datastore
229  * @param key key for the value
230  * @param size number of bytes in @a data
231  * @param data content stored
232  * @param queue_priority ranking of this request in the priority queue
233  * @param max_queue_size at what queue size should this request be dropped
234  *        (if other requests of higher priority are in the queue)
235  * @param timeout how long to wait at most for a response
236  * @param cont continuation to call when done
237  * @param cont_cls closure for @a cont
238  * @return NULL if the entry was not queued, otherwise a handle that can be used to
239  *         cancel; note that even if NULL is returned, the callback will be invoked
240  *         (or rather, will already have been invoked)
241  */
242 struct GNUNET_DATASTORE_QueueEntry *
243 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
244                          const struct GNUNET_HashCode *key,
245                          size_t size,
246                          const void *data,
247                          unsigned int queue_priority,
248                          unsigned int max_queue_size,
249                          struct GNUNET_TIME_Relative timeout,
250                          GNUNET_DATASTORE_ContinuationWithStatus cont,
251                          void *cont_cls);
252
253
254 /**
255  * Process a datum that was stored in the datastore.
256  *
257  * @param cls closure
258  * @param key key for the content
259  * @param size number of bytes in data
260  * @param data content stored
261  * @param type type of the content
262  * @param priority priority of the content
263  * @param anonymity anonymity-level for the content
264  * @param expiration expiration time for the content
265  * @param uid unique identifier for the datum;
266  *        maybe 0 if no unique identifier is available
267  */
268 typedef void
269 (*GNUNET_DATASTORE_DatumProcessor) (void *cls,
270                                     const struct GNUNET_HashCode *key,
271                                     size_t size,
272                                     const void *data,
273                                     enum GNUNET_BLOCK_Type type,
274                                     uint32_t priority,
275                                     uint32_t anonymity,
276                                     struct GNUNET_TIME_Absolute expiration,
277                                     uint64_t uid);
278
279
280 /**
281  * Get a result for a particular key from the datastore.  The processor
282  * will only be called once.
283  *
284  * @param h handle to the datastore
285  * @param offset offset of the result (modulo num-results); set to
286  *               a random 64-bit value initially; then increment by
287  *               one each time; detect that all results have been found by uid
288  *               being again the first uid ever returned.
289  * @param key maybe NULL (to match all entries)
290  * @param type desired type, 0 for any
291  * @param queue_priority ranking of this request in the priority queue
292  * @param max_queue_size at what queue size should this request be dropped
293  *        (if other requests of higher priority are in the queue)
294  * @param timeout how long to wait at most for a response
295  * @param proc function to call on a matching value;
296  *        or with a NULL value if no datum matches
297  * @param proc_cls closure for @a proc
298  * @return NULL if the entry was not queued, otherwise a handle that can be used to
299  *         cancel
300  */
301 struct GNUNET_DATASTORE_QueueEntry *
302 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
303                           uint64_t offset,
304                           const struct GNUNET_HashCode *key,
305                           enum GNUNET_BLOCK_Type type,
306                           unsigned int queue_priority,
307                           unsigned int max_queue_size,
308                           struct GNUNET_TIME_Relative timeout,
309                           GNUNET_DATASTORE_DatumProcessor proc,
310                           void *proc_cls);
311
312
313 /**
314  * Get a single zero-anonymity value from the datastore.
315  * Note that some implementations can ignore the 'offset' and
316  * instead return a random zero-anonymity value.  In that case,
317  * detecting the wrap-around based on a repeating UID is at best
318  * probabilistic.
319  *
320  * @param h handle to the datastore
321  * @param offset offset of the result (modulo num-results); set to
322  *               a random 64-bit value initially; then increment by
323  *               one each time; detect that all results have been found by uid
324  *               being again the first uid ever returned.
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 proc function to call on a random value; it
331  *        will be called once with a value (if available)
332  *        or with NULL if none value exists.
333  * @param proc_cls closure for proc
334  * @return NULL if the entry was not queued, otherwise a handle that can be used to
335  *         cancel
336  */
337 struct GNUNET_DATASTORE_QueueEntry *
338 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
339                                      uint64_t offset,
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_DatumProcessor proc,
345                                      void *proc_cls);
346
347
348 /**
349  * Get a random value from the datastore for content replication.
350  * Returns a single, random value among those with the highest
351  * replication score, lowering positive replication scores by one for
352  * the chosen value (if only content with a replication score exists,
353  * a random value is returned and replication scores are not changed).
354  *
355  * @param h handle to the datastore
356  * @param queue_priority ranking of this request in the priority queue
357  * @param max_queue_size at what queue size should this request be dropped
358  *        (if other requests of higher priority are in the queue)
359  * @param timeout how long to wait at most for a response
360  * @param proc function to call on a random value; it
361  *        will be called once with a value (if available)
362  *        and always once with a value of NULL.
363  * @param proc_cls closure for @a proc
364  * @return NULL if the entry was not queued, otherwise a handle that can be used to
365  *         cancel
366  */
367 struct GNUNET_DATASTORE_QueueEntry *
368 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
369                                       unsigned int queue_priority,
370                                       unsigned int max_queue_size,
371                                       struct GNUNET_TIME_Relative timeout,
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 /* end of gnunet_datastore_service.h */
395 #endif