2 This file is part of GNUnet
3 Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file datacache/datacache.c
22 * @brief datacache API implementation
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_datacache_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_datacache_plugin.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "datacache", __VA_ARGS__)
34 #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache", op, fn)
37 * Internal state of the datacache library.
39 struct GNUNET_DATACACHE_Handle
43 * Bloomfilter to quickly tell if we don't have the content.
45 struct GNUNET_CONTAINER_BloomFilter *filter;
50 const struct GNUNET_CONFIGURATION_Handle *cfg;
53 * Opaque handle for the statistics service.
55 struct GNUNET_STATISTICS_Handle *stats;
58 * Configuration section to use.
63 * API of the transport as returned by the plugin's
64 * initialization function.
66 struct GNUNET_DATACACHE_PluginFunctions *api;
69 * Short name for the plugin (i.e. "sqlite").
74 * Name of the library (i.e. "gnunet_plugin_datacache_sqlite").
79 * Name for the bloom filter file.
84 * Environment provided to our plugin.
86 struct GNUNET_DATACACHE_PluginEnvironment env;
89 * How much space is in use right now?
91 unsigned long long utilization;
97 * Function called by plugins to notify the datacache
98 * about content deletions.
101 * @param key key of the content that was deleted
102 * @param size number of bytes that were made available
105 env_delete_notify (void *cls,
106 const struct GNUNET_HashCode *key,
109 struct GNUNET_DATACACHE_Handle *h = cls;
111 LOG (GNUNET_ERROR_TYPE_DEBUG,
112 "Content under key `%s' discarded\n",
114 GNUNET_assert (h->utilization >= size);
115 h->utilization -= size;
116 GNUNET_CONTAINER_bloomfilter_remove (h->filter,
118 GNUNET_STATISTICS_update (h->stats,
119 gettext_noop ("# bytes stored"),
122 GNUNET_STATISTICS_update (h->stats,
123 gettext_noop ("# items stored"),
130 * Create a data cache.
132 * @param cfg configuration to use
133 * @param section section in the configuration that contains our options
134 * @return handle to use to access the service
136 struct GNUNET_DATACACHE_Handle *
137 GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
140 unsigned int bf_size;
141 unsigned long long quota;
142 struct GNUNET_DATACACHE_Handle *ret;
147 GNUNET_CONFIGURATION_get_value_size (cfg,
152 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
158 GNUNET_CONFIGURATION_get_value_string (cfg,
163 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
168 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
170 ret = GNUNET_new (struct GNUNET_DATACACHE_Handle);
173 GNUNET_CONFIGURATION_get_value_yesno (cfg,
178 GNUNET_CONFIGURATION_get_value_yesno (cfg,
182 ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
184 if (NULL != ret->bloom_name)
186 ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name,
187 quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
190 if (NULL == ret->filter)
192 ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL,
194 5); /* approx. 3% false positives at max use */
197 ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
198 ret->section = GNUNET_strdup (section);
200 ret->env.delete_notify = &env_delete_notify;
201 ret->env.section = ret->section;
203 ret->env.delete_notify = &env_delete_notify;
204 ret->env.quota = quota;
205 LOG (GNUNET_ERROR_TYPE_INFO,
206 _("Loading `%s' datacache plugin\n"),
208 GNUNET_asprintf (&libname,
209 "libgnunet_plugin_datacache_%s",
211 ret->short_name = name;
212 ret->lib_name = libname;
213 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
214 if (ret->api == NULL)
216 LOG (GNUNET_ERROR_TYPE_ERROR,
217 _("Failed to load datacache plugin for `%s'\n"),
219 GNUNET_DATACACHE_destroy (ret);
227 * Destroy a data cache (and free associated resources).
229 * @param h handle to the datastore
232 GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
234 if (NULL != h->filter)
235 GNUNET_CONTAINER_bloomfilter_free (h->filter);
237 GNUNET_break (NULL ==
238 GNUNET_PLUGIN_unload (h->lib_name,
240 GNUNET_free (h->lib_name);
241 GNUNET_free (h->short_name);
242 GNUNET_free (h->section);
243 if (NULL != h->bloom_name)
245 if (0 != UNLINK (h->bloom_name))
246 GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING,
250 GNUNET_free (h->bloom_name);
252 GNUNET_STATISTICS_destroy (h->stats,
259 * Store an item in the datastore.
261 * @param h handle to the datacache
262 * @param key key to store data under
263 * @param am_closest are we the closest peer?
264 * @param data_size number of bytes in @a data
265 * @param data data to store
266 * @param type type of the value
267 * @param discard_time when to discard the value in any case
268 * @param path_info_len number of entries in @a path_info
269 * @param path_info a path through the network
270 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate
273 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
274 const struct GNUNET_HashCode *key,
278 enum GNUNET_BLOCK_Type type,
279 struct GNUNET_TIME_Absolute discard_time,
280 unsigned int path_info_len,
281 const struct GNUNET_PeerIdentity *path_info)
285 used = h->api->put (h->api->cls,
297 return GNUNET_SYSERR;
304 LOG (GNUNET_ERROR_TYPE_DEBUG,
305 "Stored data under key `%s' in cache\n",
307 if (NULL != h->filter)
308 GNUNET_CONTAINER_bloomfilter_add (h->filter,
310 GNUNET_STATISTICS_update (h->stats,
311 gettext_noop ("# bytes stored"),
314 GNUNET_STATISTICS_update (h->stats,
315 gettext_noop ("# items stored"),
318 while (h->utilization + used > h->env.quota)
319 GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
320 h->utilization += used;
326 * Iterate over the results for a particular key
329 * @param h handle to the datacache
330 * @param key what to look up
331 * @param type entries of which type are relevant?
332 * @param iter maybe NULL (to just count)
333 * @param iter_cls closure for @a iter
334 * @return the number of results found
337 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
338 const struct GNUNET_HashCode *key,
339 enum GNUNET_BLOCK_Type type,
340 GNUNET_DATACACHE_Iterator iter,
343 GNUNET_STATISTICS_update (h->stats,
344 gettext_noop ("# requests received"),
347 LOG (GNUNET_ERROR_TYPE_DEBUG,
348 "Processing request for key `%s'\n",
350 if ( (NULL != h->filter) &&
351 (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key)) )
353 GNUNET_STATISTICS_update (h->stats,
354 gettext_noop ("# requests filtered by bloom filter"),
357 LOG (GNUNET_ERROR_TYPE_DEBUG,
358 "Bloomfilter filters request for key `%s'\n",
360 return 0; /* can not be present */
362 return h->api->get (h->api->cls,
371 * Obtain a random element from the datacache.
373 * @param h handle to the datacache
374 * @param iter maybe NULL (to just count)
375 * @param iter_cls closure for @a iter
376 * @return the number of results found (zero or 1)
379 GNUNET_DATACACHE_get_random (struct GNUNET_DATACACHE_Handle *h,
380 GNUNET_DATACACHE_Iterator iter,
383 GNUNET_STATISTICS_update (h->stats,
384 gettext_noop ("# requests for random value received"),
387 LOG (GNUNET_ERROR_TYPE_DEBUG,
388 "Processing request for random value\n");
389 return h->api->get_random (h->api->cls,
396 * Iterate over the results that are "close" to a particular key in
397 * the datacache. "close" is defined as numerically larger than @a
398 * key (when interpreted as a circular address space), with small
401 * @param h handle to the datacache
402 * @param key area of the keyspace to look into
403 * @param num_results number of results that should be returned to @a iter
404 * @param iter maybe NULL (to just count)
405 * @param iter_cls closure for @a iter
406 * @return the number of results found
409 GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h,
410 const struct GNUNET_HashCode *key,
411 unsigned int num_results,
412 GNUNET_DATACACHE_Iterator iter,
415 GNUNET_STATISTICS_update (h->stats,
416 gettext_noop ("# proximity search requests received"),
419 LOG (GNUNET_ERROR_TYPE_DEBUG,
420 "Processing proximity search at `%s'\n",
422 return h->api->get_closest (h->api->cls,
430 /* end of datacache.c */