oops, missing commit
[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 #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  * @param msg NULL on success, otherwise an error message
85  */
86 typedef void (*GNUNET_DATASTORE_ContinuationWithStatus)(void *cls,
87                                                         int success,
88                                                         const char *msg);
89
90
91 /**
92  * Reserve space in the datastore.  This function should be used
93  * to avoid "out of space" failures during a longer sequence of "put"
94  * operations (for example, when a file is being inserted).
95  *
96  * @param h handle to the datastore
97  * @param amount how much space (in bytes) should be reserved (for content only)
98  * @param entries how many entries will be created (to calculate per-entry overhead)
99  * @param cont continuation to call when done; "success" will be set to
100  *             a positive reservation value if space could be reserved.
101  * @param cont_cls closure for cont
102  * @param timeout how long to wait at most for a response
103  */
104 void
105 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
106                           uint64_t amount,
107                           uint32_t entries,
108                           GNUNET_DATASTORE_ContinuationWithStatus cont,
109                           void *cont_cls,
110                           struct GNUNET_TIME_Relative timeout);
111
112
113 /**
114  * Store an item in the datastore.  If the item is already present,
115  * the priorities are summed up and the higher expiration time and
116  * lower anonymity level is used.
117  *
118  * @param h handle to the datastore
119  * @param rid reservation ID to use (from "reserve"); use 0 if no
120  *            prior reservation was made
121  * @param key key for the value
122  * @param size number of bytes in data
123  * @param data content stored
124  * @param type type of the content
125  * @param priority priority of the content
126  * @param anonymity anonymity-level for the content
127  * @param expiration expiration time for the content
128  * @param timeout timeout for the operation
129  * @param cont continuation to call when done
130  * @param cont_cls closure for cont
131  */
132 void
133 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
134                       int rid,
135                       const GNUNET_HashCode * key,
136                       uint32_t size,
137                       const void *data,
138                       enum GNUNET_BLOCK_Type type,
139                       uint32_t priority,
140                       uint32_t anonymity,
141                       struct GNUNET_TIME_Absolute expiration,
142                       struct GNUNET_TIME_Relative timeout,
143                       GNUNET_DATASTORE_ContinuationWithStatus cont,
144                       void *cont_cls);
145
146
147 /**
148  * Signal that all of the data for which a reservation was made has
149  * been stored and that whatever excess space might have been reserved
150  * can now be released.
151  *
152  * @param h handle to the datastore
153  * @param rid reservation ID (value of "success" in original continuation
154  *        from the "reserve" function).
155  * @param cont continuation to call when done
156  * @param cont_cls closure for cont
157  * @param timeout how long to wait at most for a response
158  */
159 void
160 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
161                                   int rid,
162                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
163                                   void *cont_cls,
164                                   struct GNUNET_TIME_Relative timeout);
165
166
167 /**
168  * Update a value in the datastore.
169  *
170  * @param h handle to the datastore
171  * @param uid identifier for the value
172  * @param priority how much to increase the priority of the value
173  * @param expiration new expiration value should be MAX of existing and this argument
174  * @param cont continuation to call when done
175  * @param cont_cls closure for cont
176  * @param timeout how long to wait at most for a response
177  */
178 void
179 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
180                          unsigned long long uid,
181                          uint32_t priority,
182                          struct GNUNET_TIME_Absolute expiration,
183                          GNUNET_DATASTORE_ContinuationWithStatus cont,
184                          void *cont_cls,
185                          struct GNUNET_TIME_Relative timeout);
186
187
188 /**
189  * An iterator over a set of items stored in the datastore.
190  *
191  * @param cls closure
192  * @param key key for the content
193  * @param size number of bytes in data
194  * @param data content stored
195  * @param type type of the content
196  * @param priority priority of the content
197  * @param anonymity anonymity-level for the content
198  * @param expiration expiration time for the content
199  * @param uid unique identifier for the datum;
200  *        maybe 0 if no unique identifier is available
201  */
202 typedef void (*GNUNET_DATASTORE_Iterator) (void *cls,
203                                            const GNUNET_HashCode * key,
204                                            uint32_t size,
205                                            const void *data,
206                                            enum GNUNET_BLOCK_Type type,
207                                            uint32_t priority,
208                                            uint32_t anonymity,
209                                            struct GNUNET_TIME_Absolute
210                                            expiration, uint64_t uid);
211
212
213 /**
214  * Iterate over the results for a particular key
215  * in the datastore.  The iterator will only be called
216  * once initially; if the first call did contain a
217  * result, further results can be obtained by calling
218  * "GNUNET_DATASTORE_get_next" with the given argument.
219  *
220  * @param h handle to the datastore
221  * @param key maybe NULL (to match all entries)
222  * @param type desired type, 0 for any
223  * @param iter function to call on each matching value;
224  *        will be called once with a NULL value at the end
225  * @param iter_cls closure for iter
226  * @param timeout how long to wait at most for a response
227  */
228 void
229 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
230                       const GNUNET_HashCode * key,
231                       enum GNUNET_BLOCK_Type type,
232                       GNUNET_DATASTORE_Iterator iter, 
233                       void *iter_cls,
234                       struct GNUNET_TIME_Relative timeout);
235
236
237 /**
238  * Function called to trigger obtaining the next result
239  * from the datastore.
240  * 
241  * @param h handle to the datastore
242  * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
243  *        iteration (with a final call to "iter" with key/data == NULL).
244  */
245 void
246 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
247                            int more);
248
249
250 /**
251  * Get a random value from the datastore.
252  *
253  * @param h handle to the datastore
254  * @param iter function to call on a random value; it
255  *        will be called once with a value (if available)
256  *        and always once with a value of NULL.
257  * @param iter_cls closure for iter
258  * @param timeout how long to wait at most for a response
259  */
260 void
261 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
262                              GNUNET_DATASTORE_Iterator iter, void *iter_cls,
263                              struct GNUNET_TIME_Relative timeout);
264
265
266 /**
267  * Explicitly remove some content from the database.
268  * The "cont"inuation will be called with status
269  * "GNUNET_OK" if content was removed, "GNUNET_NO"
270  * if no matching entry was found and "GNUNET_SYSERR"
271  * on all other types of errors.
272  *
273  * @param h handle to the datastore
274  * @param key key for the value
275  * @param size number of bytes in data
276  * @param data content stored
277  * @param cont continuation to call when done
278  * @param cont_cls closure for cont
279  * @param timeout how long to wait at most for a response
280  */
281 void
282 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
283                          const GNUNET_HashCode *key,
284                          uint32_t size, const void *data,
285                          GNUNET_DATASTORE_ContinuationWithStatus cont,
286                          void *cont_cls,
287                          struct GNUNET_TIME_Relative timeout);
288
289
290 #if 0                           /* keep Emacsens' auto-indent happy */
291 {
292 #endif
293 #ifdef __cplusplus
294 }
295 #endif
296
297 /* end of gnunet_datastore_service.h */
298 #endif