2 This file is part of GNUnet
3 Copyright (C) 2012, 2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file datacache/plugin_datacache_heap.c
21 * @brief heap-only implementation of a database backend for the datacache
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_datacache_plugin.h"
28 #define LOG(kind,...) GNUNET_log_from (kind, "datacache-heap", __VA_ARGS__)
30 #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache-heap", op, fn)
35 * Context for all functions in this plugin.
40 * Our execution environment.
42 struct GNUNET_DATACACHE_PluginEnvironment *env;
47 struct GNUNET_CONTAINER_MultiHashMap *map;
50 * Heaps sorted by distance.
52 struct GNUNET_CONTAINER_Heap *heaps[NUM_HEAPS];
58 * Entry in the hash map.
65 struct GNUNET_HashCode key;
70 struct GNUNET_TIME_Absolute discard_time;
73 * Corresponding node in the heap.
75 struct GNUNET_CONTAINER_HeapNode *hn;
80 struct GNUNET_PeerIdentity *path_info;
83 * Payload (actual payload follows this struct)
88 * Number of entries in @e path_info.
90 unsigned int path_info_len;
93 * How close is the hash to us? Determines which heap we are in!
100 enum GNUNET_BLOCK_Type type;
105 #define OVERHEAD (sizeof (struct Value) + 64)
109 * Closure for #put_cb().
114 * Expiration time for the new value.
116 struct GNUNET_TIME_Absolute discard_time;
119 * Data for the new value.
126 const struct GNUNET_PeerIdentity *path_info;
129 * Number of bytes in @e data.
136 enum GNUNET_BLOCK_Type type;
139 * Number of entries in @e path_info.
141 unsigned int path_info_len;
144 * Value to set to #GNUNET_YES if an equivalent block was found.
151 * Function called during PUT to detect if an equivalent block
154 * @param cls the `struct PutContext`
155 * @param key the key for the value(s)
156 * @param value an existing value
157 * @return #GNUNET_YES if not found (to continue to iterate)
161 const struct GNUNET_HashCode *key,
164 struct PutContext *put_ctx = cls;
165 struct Value *val = value;
167 if ( (val->size == put_ctx->size) &&
168 (val->type == put_ctx->type) &&
169 (0 == memcmp (&val[1],
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 GNUNET_memcpy (val->path_info,
182 put_ctx->path_info_len * sizeof (struct GNUNET_PeerIdentity));
183 GNUNET_CONTAINER_heap_update_cost (val->hn,
184 val->discard_time.abs_value_us);
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 "Got same value for key %s and type %d (size %u vs %u)\n",
189 (unsigned int) val->size,
190 (unsigned int) put_ctx->size);
198 * Store an item in the datastore.
200 * @param cls closure (our `struct Plugin`)
201 * @param key key to store data under
202 * @param xor_distance how close is @a key to our PID?
203 * @param size number of bytes in @a data
204 * @param data data to store
205 * @param type type of the value
206 * @param discard_time when to discard the value in any case
207 * @param path_info_len number of entries in @a path_info
208 * @param path_info a path through the network
209 * @return 0 if duplicate, -1 on error, number of bytes used otherwise
212 heap_plugin_put (void *cls,
213 const struct GNUNET_HashCode *key,
214 uint32_t xor_distance,
217 enum GNUNET_BLOCK_Type type,
218 struct GNUNET_TIME_Absolute discard_time,
219 unsigned int path_info_len,
220 const struct GNUNET_PeerIdentity *path_info)
222 struct Plugin *plugin = cls;
224 struct PutContext put_ctx;
226 put_ctx.found = GNUNET_NO;
229 put_ctx.path_info = path_info;
230 put_ctx.path_info_len = path_info_len;
231 put_ctx.discard_time = discard_time;
233 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->map,
237 if (GNUNET_YES == put_ctx.found)
239 val = GNUNET_malloc (sizeof (struct Value) + size);
240 GNUNET_memcpy (&val[1],
245 val->discard_time = discard_time;
247 if (xor_distance >= NUM_HEAPS)
248 val->distance = NUM_HEAPS - 1;
250 val->distance = xor_distance;
251 GNUNET_array_grow (val->path_info,
254 GNUNET_memcpy (val->path_info,
256 path_info_len * sizeof (struct GNUNET_PeerIdentity));
257 (void) GNUNET_CONTAINER_multihashmap_put (plugin->map,
260 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
261 val->hn = GNUNET_CONTAINER_heap_insert (plugin->heaps[val->distance],
263 val->discard_time.abs_value_us);
264 return size + OVERHEAD;
269 * Closure for #get_cb().
274 * Function to call for each result.
276 GNUNET_DATACACHE_Iterator iter;
279 * Closure for @e iter.
284 * Number of results found.
289 * Block type requested.
291 enum GNUNET_BLOCK_Type type;
297 * Function called during GET to find matching blocks.
298 * Only matches by type.
300 * @param cls the `struct GetContext`
301 * @param key the key for the value(s)
302 * @param value an existing value
303 * @return #GNUNET_YES to continue to iterate
307 const struct GNUNET_HashCode *key,
310 struct GetContext *get_ctx = cls;
311 struct Value *val = value;
314 if ( (get_ctx->type != val->type) &&
315 (GNUNET_BLOCK_TYPE_ANY != get_ctx->type) )
317 if (NULL != get_ctx->iter)
318 ret = get_ctx->iter (get_ctx->iter_cls,
321 (const char *) &val[1],
334 * Iterate over the results for a particular key
337 * @param cls closure (our `struct Plugin`)
339 * @param type entries of which type are relevant?
340 * @param iter maybe NULL (to just count)
341 * @param iter_cls closure for @a iter
342 * @return the number of results found
345 heap_plugin_get (void *cls,
346 const struct GNUNET_HashCode *key,
347 enum GNUNET_BLOCK_Type type,
348 GNUNET_DATACACHE_Iterator iter,
351 struct Plugin *plugin = cls;
352 struct GetContext get_ctx;
356 get_ctx.iter_cls = iter_cls;
358 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->map,
367 * Delete the entry with the lowest expiration value
368 * from the datacache right now.
370 * @param cls closure (our `struct Plugin`)
371 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
374 heap_plugin_del (void *cls)
376 struct Plugin *plugin = cls;
379 for (unsigned int i=0;i<NUM_HEAPS;i++)
381 val = GNUNET_CONTAINER_heap_remove_root (plugin->heaps[i]);
386 return GNUNET_SYSERR;
387 GNUNET_assert (GNUNET_YES ==
388 GNUNET_CONTAINER_multihashmap_remove (plugin->map,
391 plugin->env->delete_notify (plugin->env->cls,
393 val->size + OVERHEAD);
394 GNUNET_free_non_null (val->path_info);
401 * Return a random value from the datastore.
403 * @param cls closure (our `struct Plugin`)
404 * @param iter maybe NULL (to just count)
405 * @param iter_cls closure for @a iter
406 * @return the number of results found
409 heap_plugin_get_random (void *cls,
410 GNUNET_DATACACHE_Iterator iter,
413 struct Plugin *plugin = cls;
414 struct GetContext get_ctx;
416 get_ctx.type = GNUNET_BLOCK_TYPE_ANY;
418 get_ctx.iter_cls = iter_cls;
420 GNUNET_CONTAINER_multihashmap_get_random (plugin->map,
428 * Closure for #find_closest().
430 struct GetClosestContext
432 struct Value **values;
434 unsigned int num_results;
436 const struct GNUNET_HashCode *key;
441 find_closest (void *cls,
442 const struct GNUNET_HashCode *key,
445 struct GetClosestContext *gcc = cls;
446 struct Value *val = value;
449 if (1 != GNUNET_CRYPTO_hash_cmp (key,
451 return GNUNET_OK; /* useless */
452 j = gcc->num_results;
453 for (unsigned int i=0;i<gcc->num_results;i++)
455 if (NULL == gcc->values[i])
460 if (1 == GNUNET_CRYPTO_hash_cmp (&gcc->values[i]->key,
467 if (j == gcc->num_results)
469 gcc->values[j] = val;
475 * Iterate over the results that are "close" to a particular key in
476 * the datacache. "close" is defined as numerically larger than @a
477 * key (when interpreted as a circular address space), with small
480 * @param cls closure (internal context for the plugin)
481 * @param key area of the keyspace to look into
482 * @param num_results number of results that should be returned to @a iter
483 * @param iter maybe NULL (to just count)
484 * @param iter_cls closure for @a iter
485 * @return the number of results found
488 heap_plugin_get_closest (void *cls,
489 const struct GNUNET_HashCode *key,
490 unsigned int num_results,
491 GNUNET_DATACACHE_Iterator iter,
494 struct Plugin *plugin = cls;
495 struct Value *values[num_results];
496 struct GetClosestContext gcc = {
498 .num_results = num_results,
501 GNUNET_CONTAINER_multihashmap_iterate (plugin->map,
504 for (unsigned int i=0;i<num_results;i++)
506 if (NULL == values[i])
511 (void *) &values[i][1],
513 values[i]->discard_time,
514 values[i]->path_info_len,
515 values[i]->path_info);
522 * Entry point for the plugin.
524 * @param cls closure (the `struct GNUNET_DATACACHE_PluginEnvironmnet`)
525 * @return the plugin's closure (our `struct Plugin`)
528 libgnunet_plugin_datacache_heap_init (void *cls)
530 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
531 struct GNUNET_DATACACHE_PluginFunctions *api;
532 struct Plugin *plugin;
534 plugin = GNUNET_new (struct Plugin);
535 plugin->map = GNUNET_CONTAINER_multihashmap_create (1024, /* FIXME: base on quota! */
537 for (unsigned int i=0;i<NUM_HEAPS;i++)
538 plugin->heaps[i] = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
540 api = GNUNET_new (struct GNUNET_DATACACHE_PluginFunctions);
542 api->get = &heap_plugin_get;
543 api->put = &heap_plugin_put;
544 api->del = &heap_plugin_del;
545 api->get_random = &heap_plugin_get_random;
546 api->get_closest = &heap_plugin_get_closest;
547 LOG (GNUNET_ERROR_TYPE_INFO,
548 _("Heap datacache running\n"));
554 * Exit point from the plugin.
556 * @param cls closure (our "struct Plugin")
560 libgnunet_plugin_datacache_heap_done (void *cls)
562 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
563 struct Plugin *plugin = api->cls;
566 for (unsigned int i=0;i<NUM_HEAPS;i++)
568 while (NULL != (val = GNUNET_CONTAINER_heap_remove_root (plugin->heaps[i])))
570 GNUNET_assert (GNUNET_YES ==
571 GNUNET_CONTAINER_multihashmap_remove (plugin->map,
574 GNUNET_free_non_null (val->path_info);
577 GNUNET_CONTAINER_heap_destroy (plugin->heaps[i]);
579 GNUNET_CONTAINER_multihashmap_destroy (plugin->map);
580 GNUNET_free (plugin);
587 /* end of plugin_datacache_heap.c */