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