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 * Get a random item for expiration.
159 * Call 'iter' with all values ZERO or NULL if the datastore is empty.
162 * @param iter function to call the value (once only).
163 * @param iter_cls closure for iter
166 template_plugin_expiration_get (void *cls,
167 PluginIterator iter, void *iter_cls)
174 * Update the priority for a particular key in the datastore. If
175 * the expiration time in value is different than the time found in
176 * the datastore, the higher value should be kept. For the
177 * anonymity level, the lower value is to be used. The specified
178 * priority should be added to the existing priority, ignoring the
181 * Note that it is possible for multiple values to match this put.
182 * In that case, all of the respective values are updated.
184 * @param cls our "struct Plugin*"
185 * @param uid unique identifier of the datum
186 * @param delta by how much should the priority
187 * change? If priority + delta < 0 the
188 * priority should be set to 0 (never go
190 * @param expire new expiration time should be the
191 * MAX of any existing expiration time and
193 * @param msg set to error message
194 * @return GNUNET_OK on success
197 template_plugin_update (void *cls,
199 int delta, struct GNUNET_TIME_Absolute expire,
203 *msg = GNUNET_strdup ("not implemented");
204 return GNUNET_SYSERR;
209 * Select a subset of the items in the datastore and call
210 * the given iterator for each of them.
212 * @param cls our "struct Plugin*"
213 * @param type entries of which type should be considered?
214 * Use 0 for any type.
215 * @param iter function to call on each matching value;
216 * will be called once with a NULL value at the end
217 * @param iter_cls closure for iter
220 template_plugin_iter_zero_anonymity (void *cls,
221 enum GNUNET_BLOCK_Type type,
233 template_plugin_drop (void *cls)
240 * Entry point for the plugin.
242 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
243 * @return our "struct Plugin*"
246 libgnunet_plugin_datastore_template_init (void *cls)
248 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
249 struct GNUNET_DATASTORE_PluginFunctions *api;
250 struct Plugin *plugin;
252 plugin = GNUNET_malloc (sizeof (struct Plugin));
254 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
256 api->get_size = &template_plugin_get_size;
257 api->put = &template_plugin_put;
258 api->next_request = &template_plugin_next_request;
259 api->get = &template_plugin_get;
260 api->replication_get = &template_plugin_replication_get;
261 api->expiration_get = &template_plugin_expiration_get;
262 api->update = &template_plugin_update;
263 api->iter_zero_anonymity = &template_plugin_iter_zero_anonymity;
264 api->drop = &template_plugin_drop;
265 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
266 "template", _("Template database running\n"));
272 * Exit point from the plugin.
273 * @param cls our "struct Plugin*"
274 * @return always NULL
277 libgnunet_plugin_datastore_template_done (void *cls)
279 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
280 struct Plugin *plugin = api->cls;
282 GNUNET_free (plugin);
287 /* end of plugin_datastore_template.c */