-fix
[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  * Explicitly remove some content from the database.  @a cont will be
205  * called with status #GNUNET_OK if content was removed, #GNUNET_NO if
206  * no matching entry was found and #GNUNET_SYSERR on all other types
207  * of errors.
208  *
209  * @param h handle to the datastore
210  * @param key key for the value
211  * @param size number of bytes in @a data
212  * @param data content stored
213  * @param queue_priority ranking of this request in the priority queue
214  * @param max_queue_size at what queue size should this request be dropped
215  *        (if other requests of higher priority are in the queue)
216  * @param cont continuation to call when done
217  * @param cont_cls closure for @a cont
218  * @return NULL if the entry was not queued, otherwise a handle that can be used to
219  *         cancel; note that even if NULL is returned, the callback will be invoked
220  *         (or rather, will already have been invoked)
221  */
222 struct GNUNET_DATASTORE_QueueEntry *
223 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
224                          const struct GNUNET_HashCode *key,
225                          size_t size,
226                          const void *data,
227                          unsigned int queue_priority,
228                          unsigned int max_queue_size,
229                          GNUNET_DATASTORE_ContinuationWithStatus cont,
230                          void *cont_cls);
231
232
233 /**
234  * Process a datum that was stored in the datastore.
235  *
236  * @param cls closure
237  * @param key key for the content
238  * @param size number of bytes in data
239  * @param data content stored
240  * @param type type of the content
241  * @param priority priority of the content
242  * @param anonymity anonymity-level for the content
243  * @param replication how often should the content be replicated to other peers?
244  * @param expiration expiration time for the content
245  * @param uid unique identifier for the datum;
246  *        maybe 0 if no unique identifier is available
247  */
248 typedef void
249 (*GNUNET_DATASTORE_DatumProcessor) (void *cls,
250                                     const struct GNUNET_HashCode *key,
251                                     size_t size,
252                                     const void *data,
253                                     enum GNUNET_BLOCK_Type type,
254                                     uint32_t priority,
255                                     uint32_t anonymity,
256                                     uint32_t replication,
257                                     struct GNUNET_TIME_Absolute expiration,
258                                     uint64_t uid);
259
260
261 /**
262  * Get a result for a particular key from the datastore.  The processor
263  * will only be called once.
264  *
265  * @param h handle to the datastore
266  * @param next_uid return the result with lowest uid >= next_uid
267  * @param random if true, return a random result instead of using next_uid
268  * @param key maybe NULL (to match all entries)
269  * @param type desired type, 0 for any
270  * @param queue_priority ranking of this request in the priority queue
271  * @param max_queue_size at what queue size should this request be dropped
272  *        (if other requests of higher priority are in the queue)
273  * @param proc function to call on a matching value;
274  *        or with a NULL value if no datum matches
275  * @param proc_cls closure for @a proc
276  * @return NULL if the entry was not queued, otherwise a handle that can be used to
277  *         cancel
278  */
279 struct GNUNET_DATASTORE_QueueEntry *
280 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
281                           uint64_t next_uid,
282                           bool random,
283                           const struct GNUNET_HashCode *key,
284                           enum GNUNET_BLOCK_Type type,
285                           unsigned int queue_priority,
286                           unsigned int max_queue_size,
287                           GNUNET_DATASTORE_DatumProcessor proc,
288                           void *proc_cls);
289
290
291 /**
292  * Get a single zero-anonymity value from the datastore.
293  *
294  * @param h handle to the datastore
295  * @param next_uid return the result with lowest uid >= next_uid
296  * @param queue_priority ranking of this request in the priority queue
297  * @param max_queue_size at what queue size should this request be dropped
298  *        (if other requests of higher priority are in the queue)
299  * @param type allowed type for the operation (never zero)
300  * @param proc function to call on a random value; it
301  *        will be called once with a value (if available)
302  *        or with NULL if none value exists.
303  * @param proc_cls closure for proc
304  * @return NULL if the entry was not queued, otherwise a handle that can be used to
305  *         cancel
306  */
307 struct GNUNET_DATASTORE_QueueEntry *
308 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
309                                      uint64_t next_uid,
310                                      unsigned int queue_priority,
311                                      unsigned int max_queue_size,
312                                      enum GNUNET_BLOCK_Type type,
313                                      GNUNET_DATASTORE_DatumProcessor proc,
314                                      void *proc_cls);
315
316
317 /**
318  * Get a random value from the datastore for content replication.
319  * Returns a single, random value among those with the highest
320  * replication score, lowering positive replication scores by one for
321  * the chosen value (if only content with a replication score exists,
322  * a random value is returned and replication scores are not changed).
323  *
324  * @param h handle to the datastore
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 proc function to call on a random value; it
329  *        will be called once with a value (if available)
330  *        and always once with a value of NULL.
331  * @param proc_cls closure for @a 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_for_replication (struct GNUNET_DATASTORE_Handle *h,
337                                       unsigned int queue_priority,
338                                       unsigned int max_queue_size,
339                                       GNUNET_DATASTORE_DatumProcessor proc,
340                                       void *proc_cls);
341
342
343
344 /**
345  * Cancel a datastore operation.  The final callback from the
346  * operation must not have been done yet.
347  *
348  * @param qe operation to cancel
349  */
350 void
351 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe);
352
353
354 #if 0                           /* keep Emacsens' auto-indent happy */
355 {
356 #endif
357 #ifdef __cplusplus
358 }
359 #endif
360
361 #endif
362
363 /** @} */  /* end of group */