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