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