2 This file is part of GNUnet
3 (C) 2009 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 datastore/plugin_datastore_template.c
23 * @brief template-based datastore backend
24 * @author Christian Grothoff
28 #include "gnunet_datastore_plugin.h"
32 * Context for all functions in this plugin.
37 * Our execution environment.
39 struct GNUNET_DATASTORE_PluginEnvironment *env;
44 * Get an estimate of how much space the database is
47 * @param cls our "struct Plugin*"
48 * @return number of bytes used on disk
50 static unsigned long long template_plugin_get_size (void *cls)
58 * Store an item in the datastore.
61 * @param key key for the item
62 * @param size number of bytes in data
63 * @param data content stored
64 * @param type type of the content
65 * @param priority priority of the content
66 * @param anonymity anonymity-level for the content
67 * @param replication replication-level for the content
68 * @param expiration expiration time for the content
69 * @param msg set to error message
70 * @return GNUNET_OK on success
73 template_plugin_put (void *cls,
74 const GNUNET_HashCode * key,
77 enum GNUNET_BLOCK_Type type,
81 struct GNUNET_TIME_Absolute expiration,
85 *msg = GNUNET_strdup ("not implemented");
91 * Function invoked on behalf of a "PluginIterator"
92 * asking the database plugin to call the iterator
95 * @param next_cls whatever argument was given
96 * to the PluginIterator as "next_cls".
97 * @param end_it set to GNUNET_YES if we
98 * should terminate the iteration early
99 * (iterator should be still called once more
100 * to signal the end of the iteration).
103 template_plugin_next_request (void *next_cls,
111 * Iterate over the results for a particular key
115 * @param key maybe NULL (to match all entries)
116 * @param vhash hash of the value, maybe NULL (to
117 * match all values that have the right key).
118 * Note that for DBlocks there is no difference
119 * betwen key and vhash, but for other blocks
121 * @param type entries of which type are relevant?
122 * Use 0 for any type.
123 * @param iter function to call on each matching value;
124 * will be called once with a NULL value at the end
125 * @param iter_cls closure for iter
128 template_plugin_get (void *cls,
129 const GNUNET_HashCode * key,
130 const GNUNET_HashCode * vhash,
131 enum GNUNET_BLOCK_Type type,
132 PluginIterator iter, void *iter_cls)
140 * Get a random item for replication. Returns a single, not expired, random item
141 * from those with the highest replication counters. The item's
142 * replication counter is decremented by one IF it was positive before.
143 * Call 'iter' with all values ZERO or NULL if the datastore is empty.
146 * @param iter function to call the value (once only).
147 * @param iter_cls closure for iter
150 template_plugin_replication_get (void *cls,
151 PluginIterator iter, void *iter_cls)
158 * Update the priority for a particular key in the datastore. If
159 * the expiration time in value is different than the time found in
160 * the datastore, the higher value should be kept. For the
161 * anonymity level, the lower value is to be used. The specified
162 * priority should be added to the existing priority, ignoring the
165 * Note that it is possible for multiple values to match this put.
166 * In that case, all of the respective values are updated.
168 * @param cls our "struct Plugin*"
169 * @param uid unique identifier of the datum
170 * @param delta by how much should the priority
171 * change? If priority + delta < 0 the
172 * priority should be set to 0 (never go
174 * @param expire new expiration time should be the
175 * MAX of any existing expiration time and
177 * @param msg set to error message
178 * @return GNUNET_OK on success
181 template_plugin_update (void *cls,
183 int delta, struct GNUNET_TIME_Absolute expire,
187 *msg = GNUNET_strdup ("not implemented");
188 return GNUNET_SYSERR;
193 * Select a subset of the items in the datastore and call
194 * the given iterator for each of them.
196 * @param cls our "struct Plugin*"
197 * @param type entries of which type should be considered?
198 * Use 0 for any type.
199 * @param iter function to call on each matching value;
200 * will be called once with a NULL value at the end
201 * @param iter_cls closure for iter
204 template_plugin_iter_low_priority (void *cls,
205 enum GNUNET_BLOCK_Type type,
215 * Select a subset of the items in the datastore and call
216 * the given iterator for each of them.
218 * @param cls our "struct Plugin*"
219 * @param type entries of which type should be considered?
220 * Use 0 for any type.
221 * @param iter function to call on each matching value;
222 * will be called once with a NULL value at the end
223 * @param iter_cls closure for iter
226 template_plugin_iter_zero_anonymity (void *cls,
227 enum GNUNET_BLOCK_Type type,
237 * Select a subset of the items in the datastore and call
238 * the given iterator for each of them.
240 * @param cls our "struct Plugin*"
241 * @param type entries of which type should be considered?
242 * Use 0 for any type.
243 * @param iter function to call on each matching value;
244 * will be called once with a NULL value at the end
245 * @param iter_cls closure for iter
248 template_plugin_iter_ascending_expiration (void *cls,
249 enum GNUNET_BLOCK_Type type,
259 * Select a subset of the items in the datastore and call
260 * the given iterator for each of them.
262 * @param cls our "struct Plugin*"
263 * @param type entries of which type should be considered?
264 * Use 0 for any type.
265 * @param iter function to call on each matching value;
266 * will be called once with a NULL value at the end
267 * @param iter_cls closure for iter
270 template_plugin_iter_migration_order (void *cls,
271 enum GNUNET_BLOCK_Type type,
281 * Select a subset of the items in the datastore and call
282 * the given iterator for each of them.
284 * @param cls our "struct Plugin*"
285 * @param type entries of which type should be considered?
286 * Use 0 for any type.
287 * @param iter function to call on each matching value;
288 * will be called once with a NULL value at the end
289 * @param iter_cls closure for iter
292 template_plugin_iter_all_now (void *cls,
293 enum GNUNET_BLOCK_Type type,
305 template_plugin_drop (void *cls)
312 * Entry point for the plugin.
314 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
315 * @return our "struct Plugin*"
318 libgnunet_plugin_datastore_template_init (void *cls)
320 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
321 struct GNUNET_DATASTORE_PluginFunctions *api;
322 struct Plugin *plugin;
324 plugin = GNUNET_malloc (sizeof (struct Plugin));
326 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
328 api->get_size = &template_plugin_get_size;
329 api->put = &template_plugin_put;
330 api->next_request = &template_plugin_next_request;
331 api->get = &template_plugin_get;
332 api->replication_get = &template_plugin_replication_get;
333 api->update = &template_plugin_update;
334 api->iter_low_priority = &template_plugin_iter_low_priority;
335 api->iter_zero_anonymity = &template_plugin_iter_zero_anonymity;
336 api->iter_ascending_expiration = &template_plugin_iter_ascending_expiration;
337 api->iter_migration_order = &template_plugin_iter_migration_order;
338 api->iter_all_now = &template_plugin_iter_all_now;
339 api->drop = &template_plugin_drop;
340 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
341 "template", _("Template database running\n"));
347 * Exit point from the plugin.
348 * @param cls our "struct Plugin*"
349 * @return always NULL
352 libgnunet_plugin_datastore_template_done (void *cls)
354 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
355 struct Plugin *plugin = api->cls;
357 GNUNET_free (plugin);
362 /* end of plugin_datastore_template.c */