2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
31 #ifndef GNUNET_DATASTORE_SERVICE_H
32 #define GNUNET_DATASTORE_SERVICE_H
34 #include "gnunet_util_lib.h"
35 #include "gnunet_block_lib.h"
40 #if 0 /* keep Emacsens' auto-indent happy */
48 struct GNUNET_DATASTORE_QueueEntry;
51 * Handle to the datastore service.
53 struct GNUNET_DATASTORE_Handle;
56 * Maximum size of a value that can be stored in the datastore.
58 #define GNUNET_DATASTORE_MAX_VALUE_SIZE 65536
61 * Connect to the datastore service.
63 * @param cfg configuration to use
64 * @return handle to use to access the service
66 struct GNUNET_DATASTORE_Handle *GNUNET_DATASTORE_connect (const struct
67 GNUNET_CONFIGURATION_Handle
72 * Disconnect from the datastore service (and free
73 * associated resources).
75 * @param h handle to the datastore
76 * @param drop set to GNUNET_YES to delete all data in datastore (!)
78 void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h, int drop);
82 * Continuation called to notify client about result of the
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 msg NULL on success, otherwise an error message
91 typedef void (*GNUNET_DATASTORE_ContinuationWithStatus) (void *cls,
97 * Reserve space in the datastore. This function should be used
98 * to avoid "out of space" failures during a longer sequence of "put"
99 * operations (for example, when a file is being inserted).
101 * @param h handle to the datastore
102 * @param amount how much space (in bytes) should be reserved (for content only)
103 * @param entries how many entries will be created (to calculate per-entry overhead)
104 * @param queue_priority ranking of this request in the priority queue
105 * @param max_queue_size at what queue size should this request be dropped
106 * (if other requests of higher priority are in the queue)
107 * @param timeout how long to wait at most for a response (or before dying in queue)
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 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)
115 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_reserve (struct
116 GNUNET_DATASTORE_Handle
127 GNUNET_DATASTORE_ContinuationWithStatus
133 * Store an item in the datastore. If the item is already present,
134 * the priorities and replication values are summed up and the higher
135 * expiration time and lower anonymity level is used.
137 * @param h handle to the datastore
138 * @param rid reservation ID to use (from "reserve"); use 0 if no
139 * prior reservation was made
140 * @param key key for the value
141 * @param size number of bytes in data
142 * @param data content stored
143 * @param type type of the content
144 * @param priority priority of the content
145 * @param anonymity anonymity-level for the content
146 * @param replication how often should the content be replicated to other peers?
147 * @param expiration expiration time for the content
148 * @param queue_priority ranking of this request in the priority queue
149 * @param max_queue_size at what queue size should this request be dropped
150 * (if other requests of higher priority are in the queue)
151 * @param timeout timeout for the operation
152 * @param cont continuation to call when done
153 * @param cont_cls closure for 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)
158 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_put (struct
159 GNUNET_DATASTORE_Handle
161 const GNUNET_HashCode
164 enum GNUNET_BLOCK_Type
168 uint32_t replication,
179 GNUNET_DATASTORE_ContinuationWithStatus
180 cont, void *cont_cls);
184 * Signal that all of the data for which a reservation was made has
185 * been stored and that whatever excess space might have been reserved
186 * can now be released.
188 * @param h handle to the datastore
189 * @param rid reservation ID (value of "success" in original continuation
190 * from the "reserve" function).
191 * @param queue_priority ranking of this request in the priority queue
192 * @param max_queue_size at what queue size should this request be dropped
193 * (if other requests of higher priority are in the queue)
194 * @param queue_priority ranking of this request in the priority queue
195 * @param max_queue_size at what queue size should this request be dropped
196 * (if other requests of higher priority are in the queue)
197 * @param timeout how long to wait at most for a response
198 * @param cont continuation to call when done
199 * @param cont_cls closure for cont
200 * @return NULL if the entry was not queued, otherwise a handle that can be used to
201 * cancel; note that even if NULL is returned, the callback will be invoked
202 * (or rather, will already have been invoked)
204 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_release_reserve (struct
205 GNUNET_DATASTORE_Handle
218 GNUNET_DATASTORE_ContinuationWithStatus
225 * Update a value in the datastore.
227 * @param h handle to the datastore
228 * @param uid identifier for the value
229 * @param priority how much to increase the priority of the value
230 * @param expiration new expiration value should be MAX of existing and this argument
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 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)
241 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_update (struct
242 GNUNET_DATASTORE_Handle
255 GNUNET_DATASTORE_ContinuationWithStatus
261 * Explicitly remove some content from the database.
262 * The "cont"inuation will be called with status
263 * "GNUNET_OK" if content was removed, "GNUNET_NO"
264 * if no matching entry was found and "GNUNET_SYSERR"
265 * on all other types of errors.
267 * @param h handle to the datastore
268 * @param key key for the value
269 * @param size number of bytes in data
270 * @param data content stored
271 * @param queue_priority ranking of this request in the priority queue
272 * @param max_queue_size at what queue size should this request be dropped
273 * (if other requests of higher priority are in the queue)
274 * @param timeout how long to wait at most for a response
275 * @param cont continuation to call when done
276 * @param cont_cls closure for cont
277 * @return NULL if the entry was not queued, otherwise a handle that can be used to
278 * cancel; note that even if NULL is returned, the callback will be invoked
279 * (or rather, will already have been invoked)
281 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_remove (struct
282 GNUNET_DATASTORE_Handle
295 GNUNET_DATASTORE_ContinuationWithStatus
301 * Process a datum that was stored in the datastore.
304 * @param key key for the content
305 * @param size number of bytes in data
306 * @param data content stored
307 * @param type type of the content
308 * @param priority priority of the content
309 * @param anonymity anonymity-level for the content
310 * @param expiration expiration time for the content
311 * @param uid unique identifier for the datum;
312 * maybe 0 if no unique identifier is available
314 typedef void (*GNUNET_DATASTORE_DatumProcessor) (void *cls,
315 const GNUNET_HashCode * key,
318 enum GNUNET_BLOCK_Type type,
321 struct GNUNET_TIME_Absolute
322 expiration, uint64_t uid);
326 * Get a result for a particular key from the datastore. The processor
327 * will only be called once.
329 * @param h handle to the datastore
330 * @param offset offset of the result (modulo num-results); set to
331 * a random 64-bit value initially; then increment by
332 * one each time; detect that all results have been found by uid
333 * being again the first uid ever returned.
334 * @param key maybe NULL (to match all entries)
335 * @param type desired type, 0 for any
336 * @param queue_priority ranking of this request in the priority queue
337 * @param max_queue_size at what queue size should this request be dropped
338 * (if other requests of higher priority are in the queue)
339 * @param timeout how long to wait at most for a response
340 * @param proc function to call on each matching value;
341 * will be called once with a NULL value at the end
342 * @param proc_cls closure for proc
343 * @return NULL if the entry was not queued, otherwise a handle that can be used to
346 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_get_key (struct
347 GNUNET_DATASTORE_Handle
363 GNUNET_DATASTORE_DatumProcessor
369 * Get a single zero-anonymity value from the datastore.
370 * Note that some implementations can ignore the 'offset' and
371 * instead return a random zero-anonymity value. In that case,
372 * detecting the wrap-around based on a repeating UID is at best
375 * @param h handle to the datastore
376 * @param offset offset of the result (modulo num-results); set to
377 * a random 64-bit value initially; then increment by
378 * one each time; detect that all results have been found by uid
379 * being again the first uid ever returned.
380 * @param queue_priority ranking of this request in the priority queue
381 * @param max_queue_size at what queue size should this request be dropped
382 * (if other requests of higher priority are in the queue)
383 * @param timeout how long to wait at most for a response
384 * @param type allowed type for the operation (never zero)
385 * @param proc function to call on a random value; it
386 * will be called once with a value (if available)
387 * or with NULL if none value exists.
388 * @param proc_cls closure for proc
389 * @return NULL if the entry was not queued, otherwise a handle that can be used to
392 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_get_zero_anonymity (struct
393 GNUNET_DATASTORE_Handle
409 GNUNET_DATASTORE_DatumProcessor
416 * Get a random value from the datastore for content replication.
417 * Returns a single, random value among those with the highest
418 * replication score, lowering positive replication scores by one for
419 * the chosen value (if only content with a replication score exists,
420 * a random value is returned and replication scores are not changed).
422 * @param h handle to the datastore
423 * @param queue_priority ranking of this request in the priority queue
424 * @param max_queue_size at what queue size should this request be dropped
425 * (if other requests of higher priority are in the queue)
426 * @param timeout how long to wait at most for a response
427 * @param proc function to call on a random value; it
428 * will be called once with a value (if available)
429 * and always once with a value of NULL.
430 * @param proc_cls closure for proc
431 * @return NULL if the entry was not queued, otherwise a handle that can be used to
434 struct GNUNET_DATASTORE_QueueEntry *GNUNET_DATASTORE_get_for_replication (struct
435 GNUNET_DATASTORE_Handle
446 GNUNET_DATASTORE_DatumProcessor
454 * Cancel a datastore operation. The final callback from the
455 * operation must not have been done yet.
457 * @param qe operation to cancel
459 void GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe);
462 #if 0 /* keep Emacsens' auto-indent happy */
469 /* end of gnunet_datastore_service.h */