2 This file is part of GNUnet
3 (C) 2012 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 datacache/plugin_datacache_heap.c
23 * @brief heap-only implementation of a database backend for the datacache
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_datacache_plugin.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "datacache-heap", __VA_ARGS__)
32 #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache-heap", op, fn)
37 * Context for all functions in this plugin.
42 * Our execution environment.
44 struct GNUNET_DATACACHE_PluginEnvironment *env;
49 struct GNUNET_CONTAINER_MultiHashMap *map;
52 * Heap for expirations.
54 struct GNUNET_CONTAINER_Heap *heap;
60 * Entry in the hash map.
67 struct GNUNET_HashCode key;
72 struct GNUNET_TIME_Absolute discard_time;
75 * Corresponding node in the heap.
77 struct GNUNET_CONTAINER_HeapNode *hn;
82 struct GNUNET_PeerIdentity *path_info;
85 * Payload (actual payload follows this struct)
90 * Number of entries in 'path_info'.
92 unsigned int path_info_len;
97 enum GNUNET_BLOCK_Type type;
102 #define OVERHEAD (sizeof (struct Value) + 64)
106 * Closure for 'put_cb'.
111 * Expiration time for the new value.
113 struct GNUNET_TIME_Absolute discard_time;
116 * Data for the new value.
121 * Heap from the plugin.
123 struct GNUNET_CONTAINER_Heap *heap;
128 const struct GNUNET_PeerIdentity *path_info;
131 * Number of bytes in 'data'.
138 enum GNUNET_BLOCK_Type type;
141 * Number of entries in 'path_info'.
143 unsigned int path_info_len;
146 * Value to set to GNUNET_YES if an equivalent block was found.
153 * Function called during PUT to detect if an equivalent block
156 * @param cls the 'struct PutContext'
157 * @param key the key for the value(s)
158 * @param value an existing value
159 * @return GNUNET_YES if not found (to continue to iterate)
163 const struct GNUNET_HashCode *key,
166 struct PutContext *put_ctx = cls;
167 struct Value *val = value;
169 if ( (val->size == put_ctx->size) &&
170 (val->type == put_ctx->type) &&
171 (0 == memcmp (&val[1], put_ctx->data, put_ctx->size)) )
173 put_ctx->found = GNUNET_YES;
174 val->discard_time = GNUNET_TIME_absolute_max (val->discard_time,
175 put_ctx->discard_time);
176 /* replace old path with new path */
177 GNUNET_array_grow (val->path_info,
179 put_ctx->path_info_len);
180 memcpy (val->path_info,
182 put_ctx->path_info_len * sizeof (struct GNUNET_PeerIdentity));
183 GNUNET_CONTAINER_heap_update_cost (put_ctx->heap,
185 val->discard_time.abs_value);
188 if (val->type == put_ctx->type)
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190 "Got another value for key %s and type %d (size %u vs %u)\n",
193 (unsigned int) val->size,
194 (unsigned int) put_ctx->size);
200 * Store an item in the datastore.
202 * @param cls closure (our "struct Plugin")
203 * @param key key to store data under
204 * @param size number of bytes in data
205 * @param data data to store
206 * @param type type of the value
207 * @param discard_time when to discard the value in any case
208 * @param path_info_len number of entries in 'path_info'
209 * @param path_info a path through the network
210 * @return 0 if duplicate, -1 on error, number of bytes used otherwise
213 heap_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size,
214 const char *data, enum GNUNET_BLOCK_Type type,
215 struct GNUNET_TIME_Absolute discard_time,
216 unsigned int path_info_len,
217 const struct GNUNET_PeerIdentity *path_info)
219 struct Plugin *plugin = cls;
221 struct PutContext put_ctx;
223 put_ctx.found = GNUNET_NO;
224 put_ctx.heap = plugin->heap;
227 put_ctx.path_info = path_info;
228 put_ctx.path_info_len = path_info_len;
229 put_ctx.discard_time = discard_time;
231 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->map,
235 if (GNUNET_YES == put_ctx.found)
237 val = GNUNET_malloc (sizeof (struct Value) + size);
238 memcpy (&val[1], data, size);
241 val->discard_time = discard_time;
243 GNUNET_array_grow (val->path_info,
246 memcpy (val->path_info, path_info,
247 path_info_len * sizeof (struct GNUNET_PeerIdentity));
248 (void) GNUNET_CONTAINER_multihashmap_put (plugin->map,
251 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
252 val->hn = GNUNET_CONTAINER_heap_insert (plugin->heap,
254 val->discard_time.abs_value);
255 return size + OVERHEAD;
260 * Closure for 'get_cb'.
265 * Function to call for each result.
267 GNUNET_DATACACHE_Iterator iter;
270 * Closure for 'iter'.
275 * Number of results found.
280 * Block type requested.
282 enum GNUNET_BLOCK_Type type;
288 * Function called during GET to find matching blocks.
289 * Only matches by type.
291 * @param cls the 'struct GetContext'
292 * @param key the key for the value(s)
293 * @param value an existing value
294 * @return GNUNET_YES to continue to iterate
298 const struct GNUNET_HashCode *key,
301 struct GetContext *get_ctx = cls;
302 struct Value *val = value;
305 if ( (get_ctx->type != val->type) &&
306 (GNUNET_BLOCK_TYPE_ANY != get_ctx->type) )
308 if (NULL != get_ctx->iter)
309 ret = get_ctx->iter (get_ctx->iter_cls,
312 (const char *) &val[1],
325 * Iterate over the results for a particular key
328 * @param cls closure (our "struct Plugin")
330 * @param type entries of which type are relevant?
331 * @param iter maybe NULL (to just count)
332 * @param iter_cls closure for iter
333 * @return the number of results found
336 heap_plugin_get (void *cls, const struct GNUNET_HashCode * key,
337 enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter,
340 struct Plugin *plugin = cls;
341 struct GetContext get_ctx;
345 get_ctx.iter_cls = iter_cls;
347 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->map,
356 * Delete the entry with the lowest expiration value
357 * from the datacache right now.
359 * @param cls closure (our "struct Plugin")
360 * @return GNUNET_OK on success, GNUNET_SYSERR on error
363 heap_plugin_del (void *cls)
365 struct Plugin *plugin = cls;
368 val = GNUNET_CONTAINER_heap_remove_root (plugin->heap);
370 return GNUNET_SYSERR;
371 GNUNET_assert (GNUNET_YES ==
372 GNUNET_CONTAINER_multihashmap_remove (plugin->map,
375 plugin->env->delete_notify (plugin->env->cls,
377 val->size + OVERHEAD);
378 GNUNET_free_non_null (val->path_info);
385 * Entry point for the plugin.
387 * @param cls closure (the "struct GNUNET_DATACACHE_PluginEnvironmnet")
388 * @return the plugin's closure (our "struct Plugin")
391 libgnunet_plugin_datacache_heap_init (void *cls)
393 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
394 struct GNUNET_DATACACHE_PluginFunctions *api;
395 struct Plugin *plugin;
397 plugin = GNUNET_malloc (sizeof (struct Plugin));
398 plugin->map = GNUNET_CONTAINER_multihashmap_create (1024, /* FIXME: base on quota! */
400 plugin->heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
402 api = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_PluginFunctions));
404 api->get = &heap_plugin_get;
405 api->put = &heap_plugin_put;
406 api->del = &heap_plugin_del;
407 LOG (GNUNET_ERROR_TYPE_INFO, _("Heap datacache running\n"));
413 * Exit point from the plugin.
415 * @param cls closure (our "struct Plugin")
419 libgnunet_plugin_datacache_heap_done (void *cls)
421 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
422 struct Plugin *plugin = api->cls;
425 while (NULL != (val = GNUNET_CONTAINER_heap_remove_root (plugin->heap)))
427 GNUNET_assert (GNUNET_YES ==
428 GNUNET_CONTAINER_multihashmap_remove (plugin->map,
433 GNUNET_CONTAINER_heap_destroy (plugin->heap);
434 GNUNET_CONTAINER_multihashmap_destroy (plugin->map);
435 GNUNET_free (plugin);
442 /* end of plugin_datacache_heap.c */