header
[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 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 queue_priority ranking of this request in the priority queue
109  * @param max_queue_size at what queue size should this request be dropped
110  *        (if other requests of higher priority are in the queue)
111  * @param timeout how long to wait at most for a response (or before dying in queue)
112  * @param cont continuation to call when done; "success" will be set to
113  *             a positive reservation value if space could be reserved.
114  * @param cont_cls closure for cont
115  * @return NULL if the entry was not queued, otherwise a handle that can be used to
116  *         cancel; note that even if NULL is returned, the callback will be invoked
117  *         (or rather, will already have been invoked)
118  */
119 struct GNUNET_DATASTORE_QueueEntry *
120 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h, uint64_t amount,
121                           uint32_t entries, unsigned int queue_priority,
122                           unsigned int max_queue_size,
123                           struct GNUNET_TIME_Relative timeout,
124                           GNUNET_DATASTORE_ContinuationWithStatus cont,
125                           void *cont_cls);
126
127
128 /**
129  * Store an item in the datastore.  If the item is already present,
130  * the priorities and replication values are summed up and the higher
131  * expiration time and lower anonymity level is used.
132  *
133  * @param h handle to the datastore
134  * @param rid reservation ID to use (from "reserve"); use 0 if no
135  *            prior reservation was made
136  * @param key key for the value
137  * @param size number of bytes in data
138  * @param data content stored
139  * @param type type of the content
140  * @param priority priority of the content
141  * @param anonymity anonymity-level for the content
142  * @param replication how often should the content be replicated to other peers?
143  * @param expiration expiration time for the content
144  * @param queue_priority ranking of this request in the priority queue
145  * @param max_queue_size at what queue size should this request be dropped
146  *        (if other requests of higher priority are in the queue)
147  * @param timeout timeout for the operation
148  * @param cont continuation to call when done
149  * @param cont_cls closure for cont
150  * @return NULL if the entry was not queued, otherwise a handle that can be used to
151  *         cancel; note that even if NULL is returned, the callback will be invoked
152  *         (or rather, will already have been invoked)
153  */
154 struct GNUNET_DATASTORE_QueueEntry *
155 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h, uint32_t rid,
156                       const struct GNUNET_HashCode *key, size_t size,
157                       const void *data, enum GNUNET_BLOCK_Type type,
158                       uint32_t priority, uint32_t anonymity,
159                       uint32_t replication,
160                       struct GNUNET_TIME_Absolute expiration,
161                       unsigned int queue_priority, 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, unsigned int queue_priority,
191                                   unsigned int max_queue_size,
192                                   struct GNUNET_TIME_Relative timeout,
193                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
194                                   void *cont_cls);
195
196
197 /**
198  * Update a value in the datastore.
199  *
200  * @param h handle to the datastore
201  * @param uid identifier for the value
202  * @param priority how much to increase the priority of the value
203  * @param expiration new expiration value should be MAX of existing and this argument
204  * @param queue_priority ranking of this request in the priority queue
205  * @param max_queue_size at what queue size should this request be dropped
206  *        (if other requests of higher priority are in the queue)
207  * @param timeout how long to wait at most for a response
208  * @param cont continuation to call when done
209  * @param cont_cls closure for cont
210  * @return NULL if the entry was not queued, otherwise a handle that can be used to
211  *         cancel; note that even if NULL is returned, the callback will be invoked
212  *         (or rather, will already have been invoked)
213  */
214 struct GNUNET_DATASTORE_QueueEntry *
215 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h, uint64_t uid,
216                          uint32_t priority,
217                          struct GNUNET_TIME_Absolute expiration,
218                          unsigned int queue_priority,
219                          unsigned int max_queue_size,
220                          struct GNUNET_TIME_Relative timeout,
221                          GNUNET_DATASTORE_ContinuationWithStatus cont,
222                          void *cont_cls);
223
224
225 /**
226  * Explicitly remove some content from the database.
227  * The "cont"inuation will be called with status
228  * "GNUNET_OK" if content was removed, "GNUNET_NO"
229  * if no matching entry was found and "GNUNET_SYSERR"
230  * on all other types of errors.
231  *
232  * @param h handle to the datastore
233  * @param key key for the value
234  * @param size number of bytes in data
235  * @param data content stored
236  * @param queue_priority ranking of this request in the priority queue
237  * @param max_queue_size at what queue size should this request be dropped
238  *        (if other requests of higher priority are in the queue)
239  * @param timeout how long to wait at most for a response
240  * @param cont continuation to call when done
241  * @param cont_cls closure for cont
242  * @return NULL if the entry was not queued, otherwise a handle that can be used to
243  *         cancel; note that even if NULL is returned, the callback will be invoked
244  *         (or rather, will already have been invoked)
245  */
246 struct GNUNET_DATASTORE_QueueEntry *
247 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
248                          const struct GNUNET_HashCode * key, size_t size,
249                          const void *data, unsigned int queue_priority,
250                          unsigned int max_queue_size,
251                          struct GNUNET_TIME_Relative timeout,
252                          GNUNET_DATASTORE_ContinuationWithStatus cont,
253                          void *cont_cls);
254
255
256 /**
257  * Process a datum that was stored in the datastore.
258  *
259  * @param cls closure
260  * @param key key for the content
261  * @param size number of bytes in data
262  * @param data content stored
263  * @param type type of the content
264  * @param priority priority of the content
265  * @param anonymity anonymity-level for the content
266  * @param expiration expiration time for the content
267  * @param uid unique identifier for the datum;
268  *        maybe 0 if no unique identifier is available
269  */
270 typedef void (*GNUNET_DATASTORE_DatumProcessor) (void *cls,
271                                                  const struct GNUNET_HashCode * key,
272                                                  size_t size, const void *data,
273                                                  enum GNUNET_BLOCK_Type type,
274                                                  uint32_t priority,
275                                                  uint32_t anonymity,
276                                                  struct GNUNET_TIME_Absolute
277                                                  expiration, 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 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, 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, void *proc_cls);
309
310
311 /**
312  * Get a single zero-anonymity value from the datastore.
313  * Note that some implementations can ignore the 'offset' and
314  * instead return a random zero-anonymity value.  In that case,
315  * detecting the wrap-around based on a repeating UID is at best
316  * probabilistic.
317  *
318  * @param h handle to the datastore
319  * @param offset offset of the result (modulo num-results); set to
320  *               a random 64-bit value initially; then increment by
321  *               one each time; detect that all results have been found by uid
322  *               being again the first uid ever returned.
323  * @param queue_priority ranking of this request in the priority queue
324  * @param max_queue_size at what queue size should this request be dropped
325  *        (if other requests of higher priority are in the queue)
326  * @param timeout how long to wait at most for a response
327  * @param type allowed type for the operation (never zero)
328  * @param proc function to call on a random value; it
329  *        will be called once with a value (if available)
330  *        or with NULL if none value exists.
331  * @param proc_cls closure for proc
332  * @return NULL if the entry was not queued, otherwise a handle that can be used to
333  *         cancel
334  */
335 struct GNUNET_DATASTORE_QueueEntry *
336 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
337                                      uint64_t offset,
338                                      unsigned int queue_priority,
339                                      unsigned int max_queue_size,
340                                      struct GNUNET_TIME_Relative timeout,
341                                      enum GNUNET_BLOCK_Type type,
342                                      GNUNET_DATASTORE_DatumProcessor proc,
343                                      void *proc_cls);
344
345
346 /**
347  * Get a random value from the datastore for content replication.
348  * Returns a single, random value among those with the highest
349  * replication score, lowering positive replication scores by one for
350  * the chosen value (if only content with a replication score exists,
351  * a random value is returned and replication scores are not changed).
352  *
353  * @param h handle to the datastore
354  * @param queue_priority ranking of this request in the priority queue
355  * @param max_queue_size at what queue size should this request be dropped
356  *        (if other requests of higher priority are in the queue)
357  * @param timeout how long to wait at most for a response
358  * @param proc function to call on a random value; it
359  *        will be called once with a value (if available)
360  *        and always once with a value of NULL.
361  * @param proc_cls closure for proc
362  * @return NULL if the entry was not queued, otherwise a handle that can be used to
363  *         cancel
364  */
365 struct GNUNET_DATASTORE_QueueEntry *
366 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
367                                       unsigned int queue_priority,
368                                       unsigned int max_queue_size,
369                                       struct GNUNET_TIME_Relative timeout,
370                                       GNUNET_DATASTORE_DatumProcessor proc,
371                                       void *proc_cls);
372
373
374
375 /**
376  * Cancel a datastore operation.  The final callback from the
377  * operation must not have been done yet.
378  *
379  * @param qe operation to cancel
380  */
381 void
382 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe);
383
384
385 #if 0                           /* keep Emacsens' auto-indent happy */
386 {
387 #endif
388 #ifdef __cplusplus
389 }
390 #endif
391
392 /* end of gnunet_datastore_service.h */
393 #endif