2 This file is part of GNUnet
3 (C) 2009, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file include/gnunet_datastore_plugin.h
23 * @brief API for the database backend plugins.
24 * @author Christian Grothoff
26 #ifndef PLUGIN_DATASTORE_H
27 #define PLUGIN_DATASTORE_H
29 #include "gnunet_block_lib.h"
30 #include "gnunet_configuration_lib.h"
31 #include "gnunet_datastore_service.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_scheduler_lib.h"
37 * How many bytes of overhead will we assume per entry
38 * in any DB (for reservations)?
40 #define GNUNET_DATASTORE_ENTRY_OVERHEAD 256
44 * Function invoked to notify service of disk utilization
48 * @param delta change in disk utilization,
49 * 0 for "reset to empty"
51 typedef void (*DiskUtilizationChange)(void *cls,
56 * The datastore service will pass a pointer to a struct
57 * of this type as the first and only argument to the
58 * entry point of each datastore plugin.
60 struct GNUNET_DATASTORE_PluginEnvironment
63 * Configuration to use.
65 const struct GNUNET_CONFIGURATION_Handle *cfg;
68 * Function to call on disk utilization change.
70 DiskUtilizationChange duc;
81 * Function invoked on behalf of a "PluginIterator"
82 * asking the database plugin to call the iterator
85 * @param next_cls whatever argument was given
86 * to the PluginIterator as "next_cls".
87 * @param end_it set to GNUNET_YES if we
88 * should terminate the iteration early
89 * (iterator should be still called once more
90 * to signal the end of the iteration).
92 typedef void (*PluginNextRequest)(void *next_cls,
97 * An iterator over a set of items stored in the datastore.
100 * @param next_cls closure to pass to the "next" function.
101 * @param key key for the content
102 * @param size number of bytes in data
103 * @param data content stored
104 * @param type type of the content
105 * @param priority priority of the content
106 * @param anonymity anonymity-level for the content
107 * @param expiration expiration time for the content
108 * @param uid unique identifier for the datum;
109 * maybe 0 if no unique identifier is available
111 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
112 * (continue on call to "next", of course),
113 * GNUNET_NO to delete the item and continue (if supported)
115 typedef int (*PluginIterator) (void *cls,
117 const GNUNET_HashCode * key,
120 enum GNUNET_BLOCK_Type type,
123 struct GNUNET_TIME_Absolute
128 * Get an estimate of how much space the database is
132 * @return number of bytes used on disk
134 typedef unsigned long long (*PluginGetSize) (void *cls);
138 * Store an item in the datastore. If the item is already present,
139 * the priorities and replication levels are summed up and the higher
140 * expiration time and lower anonymity level is used.
143 * @param key key for the item
144 * @param size number of bytes in data
145 * @param data content stored
146 * @param type type of the content
147 * @param priority priority of the content
148 * @param anonymity anonymity-level for the content
149 * @param replication replication-level for the content
150 * @param expiration expiration time for the content
151 * @param msg set to an error message (on failure)
152 * @return GNUNET_OK on success,
153 * GNUNET_SYSERR on failure
155 typedef int (*PluginPut) (void *cls,
156 const GNUNET_HashCode * key,
159 enum GNUNET_BLOCK_Type type,
162 uint32_t replication,
163 struct GNUNET_TIME_Absolute expiration,
168 * Iterate over the results for a particular key
172 * @param key key to match, never NULL
173 * @param vhash hash of the value, maybe NULL (to
174 * match all values that have the right key).
175 * Note that for DBlocks there is no difference
176 * betwen key and vhash, but for other blocks
178 * @param type entries of which type are relevant?
179 * Use 0 for any type.
180 * @param iter function to call on each matching value; however,
181 * after the first call to "iter", the plugin must wait
182 * until "NextRequest" was called before giving the iterator
183 * the next item; finally, the "iter" should be called once
184 * once with a NULL value at the end ("next_cls" should be NULL
185 * for that last call)
186 * @param iter_cls closure for iter
188 typedef void (*PluginGet) (void *cls,
189 const GNUNET_HashCode *key,
190 const GNUNET_HashCode *vhash,
191 enum GNUNET_BLOCK_Type type,
192 PluginIterator iter, void *iter_cls);
197 * Get a random item (additional constraints may apply depending on
198 * the specific implementation). Calls 'iter' with all values ZERO or
199 * NULL if no item applies, otherwise 'iter' is called once and only
200 * once with an item, with the 'next_cls' argument being NULL.
203 * @param iter function to call the value (once only).
204 * @param iter_cls closure for iter
206 typedef void (*PluginRandomGet) (void *cls,
207 PluginIterator iter, void *iter_cls);
211 * Update the priority for a particular key in the datastore. If
212 * the expiration time in value is different than the time found in
213 * the datastore, the higher value should be kept. For the
214 * anonymity level, the lower value is to be used. The specified
215 * priority should be added to the existing priority, ignoring the
218 * Note that it is possible for multiple values to match this put.
219 * In that case, all of the respective values are updated.
222 * @param uid unique identifier of the datum
223 * @param delta by how much should the priority
224 * change? If priority + delta < 0 the
225 * priority should be set to 0 (never go
227 * @param expire new expiration time should be the
228 * MAX of any existing expiration time and
230 * @param msg set to an error message (on error)
231 * @return GNUNET_OK on success
233 typedef int (*PluginUpdate) (void *cls,
236 struct GNUNET_TIME_Absolute expire,
241 * Select a subset of the items in the datastore and call the given
242 * iterator for the first item; then allow getting more items by
243 * calling the 'next_request' callback with the given 'next_cls'
244 * argument passed to 'iter'.
247 * @param type entries of which type should be considered?
248 * Myst not be zero (ANY).
249 * @param iter function to call on each matching value; however,
250 * after the first call to "iter", the plugin must wait
251 * until "NextRequest" was called before giving the iterator
252 * the next item; finally, the "iter" should be called once
253 * once with a NULL value at the end ("next_cls" should be NULL
254 * for that last call)
255 * @param iter_cls closure for iter
257 typedef void (*PluginSelector) (void *cls,
258 enum GNUNET_BLOCK_Type type,
268 typedef void (*PluginDrop) (void *cls);
273 * Each plugin is required to return a pointer to a struct of this
274 * type as the return value from its entry point.
276 struct GNUNET_DATASTORE_PluginFunctions
280 * Closure to use for all of the following callbacks
281 * (except "next_request").
286 * Get the current on-disk size of the SQ store. Estimates are
287 * fine, if that's the only thing available.
289 PluginGetSize get_size;
292 * Function to store an item in the datastore.
297 * Update the priority for a particular key in the datastore. If
298 * the expiration time in value is different than the time found in
299 * the datastore, the higher value should be kept. For the
300 * anonymity level, the lower value is to be used. The specified
301 * priority should be added to the existing priority, ignoring the
307 * Function called by iterators whenever they want the next value;
308 * note that unlike all of the other callbacks, this one does get a
309 * the "next_cls" closure which is usually different from the "cls"
310 * member of this struct!
312 PluginNextRequest next_request;
315 * Function to iterate over the results for a particular key
321 * Iterate over content with anonymity level zero.
323 PluginSelector iter_zero_anonymity;
326 * Function to get a random item with high replication score from
327 * the database, lowering the item's replication score. Returns a
328 * single, not expired, random item from those with the highest
329 * replication counters. The item's replication counter is
330 * decremented by one IF it was positive before.
332 PluginRandomGet replication_get;
335 * Function to get a random expired item or, if none are expired, one
336 * with a low priority.
338 PluginRandomGet expiration_get;
341 * Delete the database. The next operation is
342 * guaranteed to be unloading of the module.