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