error handling
[oweals/gnunet.git] / src / include / gnunet_datastore_service.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @author Christian Grothoff
23  *
24  * @file
25  * datastore service
26  *
27  * @defgroup datastore  Data Store service
28  * Data store for files stored on a GNUnet node.
29  *
30  * Provides an API that can be used manage the
31  * datastore for files stored on a GNUnet node.
32  * Note that the datastore is NOT responsible for
33  * on-demand encoding, that is achieved using
34  * a special kind of entry.
35  *
36  * @{
37  */
38
39 #ifndef GNUNET_DATASTORE_SERVICE_H
40 #define GNUNET_DATASTORE_SERVICE_H
41
42 #include "gnunet_util_lib.h"
43 #include "gnunet_block_lib.h"
44
45 #ifdef __cplusplus
46 extern "C"
47 {
48 #if 0                           /* keep Emacsens' auto-indent happy */
49 }
50 #endif
51 #endif
52
53 /**
54  * Entry in the queue.
55  */
56 struct GNUNET_DATASTORE_QueueEntry;
57
58 /**
59  * Handle to the datastore service.
60  */
61 struct GNUNET_DATASTORE_Handle;
62
63 /**
64  * Maximum size of a value that can be stored in the datastore.
65  */
66 #define GNUNET_DATASTORE_MAX_VALUE_SIZE 65536
67
68 /**
69  * Connect to the datastore service.
70  *
71  * @param cfg configuration to use
72  * @return handle to use to access the service
73  */
74 struct GNUNET_DATASTORE_Handle *
75 GNUNET_DATASTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
76
77
78 /**
79  * Disconnect from the datastore service (and free
80  * associated resources).
81  *
82  * @param h handle to the datastore
83  * @param drop set to #GNUNET_YES to delete all data in datastore (!)
84  */
85 void
86 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
87                              int drop);
88
89
90 /**
91  * Continuation called to notify client about result of the
92  * operation.
93  *
94  * @param cls closure
95  * @param success #GNUNET_SYSERR on failure
96  *                #GNUNET_NO if content was already there
97  *                #GNUNET_YES (or other positive value) on success
98  * @param min_expiration minimum expiration time required for 0-priority content to be stored
99  *                by the datacache at this time, zero for unknown, forever if we have no
100  *                space for 0-priority content
101  * @param msg NULL on success, otherwise an error message
102  */
103 typedef void
104 (*GNUNET_DATASTORE_ContinuationWithStatus) (void *cls,
105                                             int32_t success,
106                                             struct GNUNET_TIME_Absolute
107                                             min_expiration,
108                                             const char *msg);
109
110
111 /**
112  * Reserve space in the datastore.  This function should be used
113  * to avoid "out of space" failures during a longer sequence of "put"
114  * operations (for example, when a file is being inserted).
115  *
116  * @param h handle to the datastore
117  * @param amount how much space (in bytes) should be reserved (for content only)
118  * @param entries how many entries will be created (to calculate per-entry overhead)
119  * @param cont continuation to call when done; "success" will be set to
120  *             a positive reservation value if space could be reserved.
121  * @param cont_cls closure for @a cont
122  * @return NULL if the entry was not queued, otherwise a handle that can be used to
123  *         cancel; note that even if NULL is returned, the callback will be invoked
124  *         (or rather, will already have been invoked)
125  */
126 struct GNUNET_DATASTORE_QueueEntry *
127 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
128                           uint64_t amount,
129                           uint32_t entries,
130                           GNUNET_DATASTORE_ContinuationWithStatus cont,
131                           void *cont_cls);
132
133
134 /**
135  * Store an item in the datastore.  If the item is already present,
136  * the priorities and replication values are summed up and the higher
137  * expiration time and lower anonymity level is used.
138  *
139  * @param h handle to the datastore
140  * @param rid reservation ID to use (from "reserve"); use 0 if no
141  *            prior reservation was made
142  * @param key key for the value
143  * @param size number of bytes in data
144  * @param data content stored
145  * @param type type of the content
146  * @param priority priority of the content
147  * @param anonymity anonymity-level for the content
148  * @param replication how often should the content be replicated to other peers?
149  * @param expiration expiration time for the content
150  * @param queue_priority ranking of this request in the priority queue
151  * @param max_queue_size at what queue size should this request be dropped
152  *        (if other requests of higher priority are in the queue)
153  * @param cont continuation to call when done
154  * @param cont_cls closure for @a cont
155  * @return NULL if the entry was not queued, otherwise a handle that can be used to
156  *         cancel; note that even if NULL is returned, the callback will be invoked
157  *         (or rather, will already have been invoked)
158  */
159 struct GNUNET_DATASTORE_QueueEntry *
160 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
161                       uint32_t rid,
162                       const struct GNUNET_HashCode *key,
163                       size_t size,
164                       const void *data,
165                       enum GNUNET_BLOCK_Type type,
166                       uint32_t priority,
167                       uint32_t anonymity,
168                       uint32_t replication,
169                       struct GNUNET_TIME_Absolute expiration,
170                       unsigned int queue_priority,
171                       unsigned int max_queue_size,
172                       GNUNET_DATASTORE_ContinuationWithStatus cont,
173                       void *cont_cls);
174
175
176 /**
177  * Signal that all of the data for which a reservation was made has
178  * been stored and that whatever excess space might have been reserved
179  * can now be released.
180  *
181  * @param h handle to the datastore
182  * @param rid reservation ID (value of "success" in original continuation
183  *        from the "reserve" function).
184  * @param queue_priority ranking of this request in the priority queue
185  * @param max_queue_size at what queue size should this request be dropped
186  *        (if other requests of higher priority are in the queue)
187  * @param queue_priority ranking of this request in the priority queue
188  * @param max_queue_size at what queue size should this request be dropped
189  *        (if other requests of higher priority are in the queue)
190  * @param cont continuation to call when done
191  * @param cont_cls closure for @a cont
192  * @return NULL if the entry was not queued, otherwise a handle that can be used to
193  *         cancel; note that even if NULL is returned, the callback will be invoked
194  *         (or rather, will already have been invoked)
195  */
196 struct GNUNET_DATASTORE_QueueEntry *
197 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
198                                   uint32_t rid, unsigned int queue_priority,
199                                   unsigned int max_queue_size,
200                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
201                                   void *cont_cls);
202
203
204 /**
205  * Explicitly remove some content from the database.  @a cont will be
206  * called with status #GNUNET_OK if content was removed, #GNUNET_NO if
207  * no matching entry was found and #GNUNET_SYSERR on all other types
208  * of errors.
209  *
210  * @param h handle to the datastore
211  * @param key key for the value
212  * @param size number of bytes in @a data
213  * @param data content stored
214  * @param queue_priority ranking of this request in the priority queue
215  * @param max_queue_size at what queue size should this request be dropped
216  *        (if other requests of higher priority are in the queue)
217  * @param cont continuation to call when done
218  * @param cont_cls closure for @a cont
219  * @return NULL if the entry was not queued, otherwise a handle that can be used to
220  *         cancel; note that even if NULL is returned, the callback will be invoked
221  *         (or rather, will already have been invoked)
222  */
223 struct GNUNET_DATASTORE_QueueEntry *
224 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
225                          const struct GNUNET_HashCode *key,
226                          size_t size,
227                          const void *data,
228                          unsigned int queue_priority,
229                          unsigned int max_queue_size,
230                          GNUNET_DATASTORE_ContinuationWithStatus cont,
231                          void *cont_cls);
232
233
234 /**
235  * Process a datum that was stored in the datastore.
236  *
237  * @param cls closure
238  * @param key key for the content
239  * @param size number of bytes in data
240  * @param data content stored
241  * @param type type of the content
242  * @param priority priority of the content
243  * @param anonymity anonymity-level for the content
244  * @param replication how often should the content be replicated to other peers?
245  * @param expiration expiration time for the content
246  * @param uid unique identifier for the datum;
247  *        maybe 0 if no unique identifier is available
248  */
249 typedef void
250 (*GNUNET_DATASTORE_DatumProcessor) (void *cls,
251                                     const struct GNUNET_HashCode *key,
252                                     size_t size,
253                                     const void *data,
254                                     enum GNUNET_BLOCK_Type type,
255                                     uint32_t priority,
256                                     uint32_t anonymity,
257                                     uint32_t replication,
258                                     struct GNUNET_TIME_Absolute expiration,
259                                     uint64_t uid);
260
261
262 /**
263  * Get a result for a particular key from the datastore.  The processor
264  * will only be called once.
265  *
266  * @param h handle to the datastore
267  * @param next_uid return the result with lowest uid >= next_uid
268  * @param random if true, return a random result instead of using next_uid
269  * @param key maybe NULL (to match all entries)
270  * @param type desired type, 0 for any
271  * @param queue_priority ranking of this request in the priority queue
272  * @param max_queue_size at what queue size should this request be dropped
273  *        (if other requests of higher priority are in the queue)
274  * @param proc function to call on a matching value;
275  *        or with a NULL value if no datum matches
276  * @param proc_cls closure for @a proc
277  * @return NULL if the entry was not queued, otherwise a handle that can be used to
278  *         cancel
279  */
280 struct GNUNET_DATASTORE_QueueEntry *
281 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
282                           uint64_t next_uid,
283                           bool random,
284                           const struct GNUNET_HashCode *key,
285                           enum GNUNET_BLOCK_Type type,
286                           unsigned int queue_priority,
287                           unsigned int max_queue_size,
288                           GNUNET_DATASTORE_DatumProcessor proc,
289                           void *proc_cls);
290
291
292 /**
293  * Get a single zero-anonymity value from the datastore.
294  *
295  * @param h handle to the datastore
296  * @param next_uid return the result with lowest uid >= next_uid
297  * @param queue_priority ranking of this request in the priority queue
298  * @param max_queue_size at what queue size should this request be dropped
299  *        (if other requests of higher priority are in the queue)
300  * @param type allowed type for the operation (never zero)
301  * @param proc function to call on a random value; it
302  *        will be called once with a value (if available)
303  *        or with NULL if none value exists.
304  * @param proc_cls closure for proc
305  * @return NULL if the entry was not queued, otherwise a handle that can be used to
306  *         cancel
307  */
308 struct GNUNET_DATASTORE_QueueEntry *
309 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
310                                      uint64_t next_uid,
311                                      unsigned int queue_priority,
312                                      unsigned int max_queue_size,
313                                      enum GNUNET_BLOCK_Type type,
314                                      GNUNET_DATASTORE_DatumProcessor proc,
315                                      void *proc_cls);
316
317
318 /**
319  * Get a random value from the datastore for content replication.
320  * Returns a single, random value among those with the highest
321  * replication score, lowering positive replication scores by one for
322  * the chosen value (if only content with a replication score exists,
323  * a random value is returned and replication scores are not changed).
324  *
325  * @param h handle to the datastore
326  * @param queue_priority ranking of this request in the priority queue
327  * @param max_queue_size at what queue size should this request be dropped
328  *        (if other requests of higher priority are in the queue)
329  * @param proc function to call on a random value; it
330  *        will be called once with a value (if available)
331  *        and always once with a value of NULL.
332  * @param proc_cls closure for @a proc
333  * @return NULL if the entry was not queued, otherwise a handle that can be used to
334  *         cancel
335  */
336 struct GNUNET_DATASTORE_QueueEntry *
337 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
338                                       unsigned int queue_priority,
339                                       unsigned int max_queue_size,
340                                       GNUNET_DATASTORE_DatumProcessor proc,
341                                       void *proc_cls);
342
343
344 /**
345  * Cancel a datastore operation.  The final callback from the
346  * operation must not have been done yet.
347  *
348  * @param qe operation to cancel
349  */
350 void
351 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe);
352
353
354 #if 0                           /* keep Emacsens' auto-indent happy */
355 {
356 #endif
357 #ifdef __cplusplus
358 }
359 #endif
360
361 #endif
362
363 /** @} */  /* end of group */