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 3, 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, int delta);
55 * The datastore service will pass a pointer to a struct
56 * of this type as the first and only argument to the
57 * entry point of each datastore plugin.
59 struct GNUNET_DATASTORE_PluginEnvironment
62 * Configuration to use.
64 const struct GNUNET_CONFIGURATION_Handle *cfg;
67 * Function to call on disk utilization change.
69 DiskUtilizationChange duc;
80 * An processor over a set of items stored in the datastore.
83 * @param key key for the content
84 * @param size number of bytes in data
85 * @param data content stored
86 * @param type type of the content
87 * @param priority priority of the content
88 * @param anonymity anonymity-level for the content
89 * @param expiration expiration time for the content
90 * @param uid unique identifier for the datum
91 * @return #GNUNET_OK to keep the item
92 * #GNUNET_NO to delete the item
94 typedef int (*PluginDatumProcessor) (void *cls, const struct GNUNET_HashCode * key,
95 uint32_t size, const void *data,
96 enum GNUNET_BLOCK_Type type,
97 uint32_t priority, uint32_t anonymity,
98 struct GNUNET_TIME_Absolute expiration,
102 * Get an estimate of how much space the database is
106 * @return number of bytes used on disk
108 typedef unsigned long long (*PluginEstimateSize) (void *cls);
112 * Store an item in the datastore. If the item is already present,
113 * the priorities and replication levels are summed up and the higher
114 * expiration time and lower anonymity level is used.
117 * @param key key for the item
118 * @param size number of bytes in @a data
119 * @param data content stored
120 * @param type type of the content
121 * @param priority priority of the content
122 * @param anonymity anonymity-level for the content
123 * @param replication replication-level for the content
124 * @param expiration expiration time for the content
125 * @param msg set to an error message (on failure)
126 * @return #GNUNET_OK on success,
127 * #GNUNET_SYSERR on failure
129 typedef int (*PluginPut) (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
130 const void *data, enum GNUNET_BLOCK_Type type,
131 uint32_t priority, uint32_t anonymity,
132 uint32_t replication,
133 struct GNUNET_TIME_Absolute expiration, char **msg);
137 * An processor over a set of keys stored in the datastore.
140 * @param key key in the data store
141 * @param count how many values are stored under this key in the datastore
143 typedef void (*PluginKeyProcessor) (void *cls,
144 const struct GNUNET_HashCode *key,
149 * Get all of the keys in the datastore.
152 * @param proc function to call on each key
153 * @param proc_cls closure for @a proc
155 typedef void (*PluginGetKeys) (void *cls,
156 PluginKeyProcessor proc, void *proc_cls);
160 * Get one of the results for a particular key in the datastore.
163 * @param offset offset of the result (modulo num-results);
164 * specific ordering does not matter for the offset
165 * @param key key to match, never NULL
166 * @param vhash hash of the value, maybe NULL (to
167 * match all values that have the right key).
168 * Note that for DBlocks there is no difference
169 * betwen key and vhash, but for other blocks
171 * @param type entries of which type are relevant?
172 * Use 0 for any type.
173 * @param min find the smallest key that is larger than the given min,
174 * NULL for no minimum (return smallest key)
175 * @param proc function to call on the matching value;
176 * proc should be called with NULL if there is no result
177 * @param proc_cls closure for @a proc
179 typedef void (*PluginGetKey) (void *cls, uint64_t offset,
180 const struct GNUNET_HashCode * key,
181 const struct GNUNET_HashCode * vhash,
182 enum GNUNET_BLOCK_Type type,
183 PluginDatumProcessor proc, void *proc_cls);
187 * Get a random item (additional constraints may apply depending on
188 * the specific implementation). Calls @a proc with all values ZERO or
189 * NULL if no item applies, otherwise @a proc is called once and only
193 * @param proc function to call the value (once only).
194 * @param proc_cls closure for @a proc
196 typedef void (*PluginGetRandom) (void *cls, PluginDatumProcessor proc,
203 * Update the priority for a particular key in the datastore. If
204 * the expiration time in value is different than the time found in
205 * the datastore, the higher value should be kept. For the
206 * anonymity level, the lower value is to be used. The specified
207 * priority should be added to the existing priority, ignoring the
211 * @param uid unique identifier of the datum
212 * @param delta by how much should the priority
213 * change? If priority + delta < 0 the
214 * priority should be set to 0 (never go
216 * @param expire new expiration time should be the
217 * MAX of any existing expiration time and
219 * @param msg set to an error message (on error)
220 * @return #GNUNET_OK on success
222 typedef int (*PluginUpdate) (void *cls, uint64_t uid, int delta,
223 struct GNUNET_TIME_Absolute expire, char **msg);
227 * Select a single item from the datastore at the specified offset
228 * (among those applicable).
231 * @param offset offset of the result (modulo num-results);
232 * specific ordering does not matter for the offset
233 * @param type entries of which type should be considered?
234 * Must not be zero (ANY).
235 * @param proc function to call on the matching value
236 * @param proc_cls closure for @a proc
238 typedef void (*PluginGetType) (void *cls, uint64_t offset,
239 enum GNUNET_BLOCK_Type type,
240 PluginDatumProcessor proc, void *proc_cls);
248 typedef void (*PluginDrop) (void *cls);
253 * Each plugin is required to return a pointer to a struct of this
254 * type as the return value from its entry point.
256 struct GNUNET_DATASTORE_PluginFunctions
260 * Closure to use for all of the following callbacks
261 * (except "next_request").
266 * Calculate the current on-disk size of the SQ store. Estimates
267 * are fine, if that's the only thing available.
269 PluginEstimateSize estimate_size;
272 * Function to store an item in the datastore.
277 * Update the priority for a particular key in the datastore. If
278 * the expiration time in value is different than the time found in
279 * the datastore, the higher value should be kept. For the
280 * anonymity level, the lower value is to be used. The specified
281 * priority should be added to the existing priority, ignoring the
287 * Get a particular datum matching a given hash from the datastore.
289 PluginGetKey get_key;
292 * Get datum (of the specified type) with anonymity level zero.
293 * This function is allowed to ignore the 'offset' argument
294 * and instead return a random result (with zero anonymity of
295 * the correct type) if implementing an offset is expensive.
297 PluginGetType get_zero_anonymity;
300 * Function to get a random item with high replication score from
301 * the database, lowering the item's replication score. Returns a
302 * single random item from those with the highest replication
303 * counters. The item's replication counter is decremented by one
304 * IF it was positive before.
306 PluginGetRandom get_replication;
309 * Function to get a random expired item or, if none are expired,
310 * either the oldest entry or one with a low priority (depending
311 * on what was efficiently implementable).
313 PluginGetRandom get_expiration;
316 * Delete the database. The next operation is
317 * guaranteed to be unloading of the module.
322 * Iterate over all keys in the database.
324 PluginGetKeys get_keys;