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