e2de185bda586878c41888cae77b52680a7c6d38
[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 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 /**
47  * Handle to the datastore service.
48  */
49 struct GNUNET_DATASTORE_Handle;
50
51
52 /**
53  * Connect to the datastore service.
54  *
55  * @param cfg configuration to use
56  * @param sched scheduler to use
57  * @return handle to use to access the service
58  */
59 struct GNUNET_DATASTORE_Handle *GNUNET_DATASTORE_connect (const struct
60                                                           GNUNET_CONFIGURATION_Handle
61                                                           *cfg,
62                                                           struct
63                                                           GNUNET_SCHEDULER_Handle
64                                                           *sched);
65
66
67 /**
68  * Disconnect from the datastore service (and free
69  * associated resources).
70  *
71  * @param h handle to the datastore
72  * @param drop set to GNUNET_YES to delete all data in datastore (!)
73  */
74 void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
75                                   int drop);
76
77
78 /**
79  * Continuation called to notify client about result of the
80  * operation.
81  *
82  * @param cls closure
83  * @param success GNUNET_SYSERR on failure, 
84  *                GNUNET_NO on timeout/queue drop
85  *                GNUNET_YES on success
86  * @param msg NULL on success, otherwise an error message
87  */
88 typedef void (*GNUNET_DATASTORE_ContinuationWithStatus)(void *cls,
89                                                         int success,
90                                                         const char *msg);
91
92
93 /**
94  * Reserve space in the datastore.  This function should be used
95  * to avoid "out of space" failures during a longer sequence of "put"
96  * operations (for example, when a file is being inserted).
97  *
98  * @param h handle to the datastore
99  * @param amount how much space (in bytes) should be reserved (for content only)
100  * @param entries how many entries will be created (to calculate per-entry overhead)
101  * @param queue_priority ranking of this request in the priority queue
102  * @param max_queue_size at what queue size should this request be dropped
103  *        (if other requests of higher priority are in the queue)
104  * @param timeout how long to wait at most for a response (or before dying in queue)
105  * @param cont continuation to call when done; "success" will be set to
106  *             a positive reservation value if space could be reserved.
107  * @param cont_cls closure for cont
108  */
109 void
110 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
111                           uint64_t amount,
112                           uint32_t entries,
113                           unsigned int queue_priority,
114                           unsigned int max_queue_size,
115                           struct GNUNET_TIME_Relative timeout,
116                           GNUNET_DATASTORE_ContinuationWithStatus cont,
117                           void *cont_cls);
118
119
120 /**
121  * Store an item in the datastore.  If the item is already present,
122  * the priorities are summed up and the higher expiration time and
123  * lower anonymity level is used.
124  *
125  * @param h handle to the datastore
126  * @param rid reservation ID to use (from "reserve"); use 0 if no
127  *            prior reservation was made
128  * @param key key for the value
129  * @param size number of bytes in data
130  * @param data content stored
131  * @param type type of the content
132  * @param priority priority of the content
133  * @param anonymity anonymity-level for the content
134  * @param expiration expiration time for the content
135  * @param queue_priority ranking of this request in the priority queue
136  * @param max_queue_size at what queue size should this request be dropped
137  *        (if other requests of higher priority are in the queue)
138  * @param timeout timeout for the operation
139  * @param cont continuation to call when done
140  * @param cont_cls closure for cont
141  */
142 void
143 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
144                       int rid,
145                       const GNUNET_HashCode * key,
146                       uint32_t size,
147                       const void *data,
148                       enum GNUNET_BLOCK_Type type,
149                       uint32_t priority,
150                       uint32_t anonymity,
151                       struct GNUNET_TIME_Absolute expiration,
152                       unsigned int queue_priority,
153                       unsigned int max_queue_size,
154                       struct GNUNET_TIME_Relative timeout,
155                       GNUNET_DATASTORE_ContinuationWithStatus cont,
156                       void *cont_cls);
157
158
159 /**
160  * Signal that all of the data for which a reservation was made has
161  * been stored and that whatever excess space might have been reserved
162  * can now be released.
163  *
164  * @param h handle to the datastore
165  * @param rid reservation ID (value of "success" in original continuation
166  *        from the "reserve" function).
167  * @param queue_priority ranking of this request in the priority queue
168  * @param max_queue_size at what queue size should this request be dropped
169  *        (if other requests of higher priority are in the queue)
170  * @param queue_priority ranking of this request in the priority queue
171  * @param max_queue_size at what queue size should this request be dropped
172  *        (if other requests of higher priority are in the queue)
173  * @param timeout how long to wait at most for a response
174  * @param cont continuation to call when done
175  * @param cont_cls closure for cont
176  */
177 void
178 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
179                                   int rid,
180                                   unsigned int queue_priority,
181                                   unsigned int max_queue_size,
182                                   struct GNUNET_TIME_Relative timeout,
183                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
184                                   void *cont_cls);
185
186
187 /**
188  * Update a value in the datastore.
189  *
190  * @param h handle to the datastore
191  * @param uid identifier for the value
192  * @param priority how much to increase the priority of the value
193  * @param expiration new expiration value should be MAX of existing and this argument
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  */
201 void
202 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
203                          unsigned long long uid,
204                          uint32_t priority,
205                          struct GNUNET_TIME_Absolute expiration,
206                          unsigned int queue_priority,
207                          unsigned int max_queue_size,
208                          struct GNUNET_TIME_Relative timeout,
209                          GNUNET_DATASTORE_ContinuationWithStatus cont,
210                          void *cont_cls);
211
212
213 /**
214  * Explicitly remove some content from the database.
215  * The "cont"inuation will be called with status
216  * "GNUNET_OK" if content was removed, "GNUNET_NO"
217  * if no matching entry was found and "GNUNET_SYSERR"
218  * on all other types of errors.
219  *
220  * @param h handle to the datastore
221  * @param key key for the value
222  * @param size number of bytes in data
223  * @param data content stored
224  * @param queue_priority ranking of this request in the priority queue
225  * @param max_queue_size at what queue size should this request be dropped
226  *        (if other requests of higher priority are in the queue)
227  * @param timeout how long to wait at most for a response
228  * @param cont continuation to call when done
229  * @param cont_cls closure for cont
230  */
231 void
232 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
233                          const GNUNET_HashCode *key,
234                          uint32_t size, 
235                          const void *data,
236                          unsigned int queue_priority,
237                          unsigned int max_queue_size,
238                          struct GNUNET_TIME_Relative timeout,
239                          GNUNET_DATASTORE_ContinuationWithStatus cont,
240                          void *cont_cls);
241
242
243 /**
244  * An iterator over a set of items stored in the datastore.
245  *
246  * @param cls closure
247  * @param key key for the content
248  * @param size number of bytes in data
249  * @param data content stored
250  * @param type type of the content
251  * @param priority priority of the content
252  * @param anonymity anonymity-level for the content
253  * @param expiration expiration time for the content
254  * @param uid unique identifier for the datum;
255  *        maybe 0 if no unique identifier is available
256  */
257 typedef void (*GNUNET_DATASTORE_Iterator) (void *cls,
258                                            const GNUNET_HashCode * key,
259                                            uint32_t size,
260                                            const void *data,
261                                            enum GNUNET_BLOCK_Type type,
262                                            uint32_t priority,
263                                            uint32_t anonymity,
264                                            struct GNUNET_TIME_Absolute
265                                            expiration, uint64_t uid);
266
267
268 /**
269  * Iterate over the results for a particular key
270  * in the datastore.  The iterator will only be called
271  * once initially; if the first call did contain a
272  * result, further results can be obtained by calling
273  * "GNUNET_DATASTORE_get_next" with the given argument.
274  *
275  * @param h handle to the datastore
276  * @param key maybe NULL (to match all entries)
277  * @param type desired type, 0 for any
278  * @param queue_priority ranking of this request in the priority queue
279  * @param max_queue_size at what queue size should this request be dropped
280  *        (if other requests of higher priority are in the queue)
281  * @param timeout how long to wait at most for a response
282  * @param iter function to call on each matching value;
283  *        will be called once with a NULL value at the end
284  * @param iter_cls closure for iter
285  */
286 void
287 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
288                       const GNUNET_HashCode * key,
289                       enum GNUNET_BLOCK_Type type,
290                       unsigned int queue_priority,
291                       unsigned int max_queue_size,
292                       struct GNUNET_TIME_Relative timeout,
293                       GNUNET_DATASTORE_Iterator iter, 
294                       void *iter_cls);
295
296
297 /**
298  * Function called to trigger obtaining the next result
299  * from the datastore.
300  * 
301  * @param h handle to the datastore
302  * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
303  *        iteration (with a final call to "iter" with key/data == NULL).
304  */
305 void
306 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
307                            int more);
308
309
310 /**
311  * Get a random value from the datastore.
312  *
313  * @param h handle to the datastore
314  * @param queue_priority ranking of this request in the priority queue
315  * @param max_queue_size at what queue size should this request be dropped
316  *        (if other requests of higher priority are in the queue)
317  * @param timeout how long to wait at most for a response
318  * @param iter function to call on a random value; it
319  *        will be called once with a value (if available)
320  *        and always once with a value of NULL.
321  * @param iter_cls closure for iter
322  */
323 void
324 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
325                              unsigned int queue_priority,
326                              unsigned int max_queue_size,
327                              struct GNUNET_TIME_Relative timeout,
328                              GNUNET_DATASTORE_Iterator iter, 
329                              void *iter_cls);
330
331
332
333 #if 0                           /* keep Emacsens' auto-indent happy */
334 {
335 #endif
336 #ifdef __cplusplus
337 }
338 #endif
339
340 /* end of gnunet_datastore_service.h */
341 #endif