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, section, "DISABLE_BF"))
176 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF_RC"))
178 ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
180 if (NULL != ret->bloom_name)
182 ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name,
183 quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
186 if (NULL == ret->filter)
188 ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL,
190 5); /* approx. 3% false positives at max use */
193 ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
194 ret->section = GNUNET_strdup (section);
196 ret->env.delete_notify = &env_delete_notify;
197 ret->env.section = ret->section;
199 ret->env.delete_notify = &env_delete_notify;
200 ret->env.quota = quota;
201 LOG (GNUNET_ERROR_TYPE_INFO,
202 _("Loading `%s' datacache plugin\n"),
204 GNUNET_asprintf (&libname,
205 "libgnunet_plugin_datacache_%s",
207 ret->short_name = name;
208 ret->lib_name = libname;
209 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
210 if (ret->api == NULL)
212 LOG (GNUNET_ERROR_TYPE_ERROR,
213 _("Failed to load datacache plugin for `%s'\n"),
215 GNUNET_DATACACHE_destroy (ret);
223 * Destroy a data cache (and free associated resources).
225 * @param h handle to the datastore
228 GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
230 if (NULL != h->filter)
231 GNUNET_CONTAINER_bloomfilter_free (h->filter);
233 GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api));
234 GNUNET_free (h->lib_name);
235 GNUNET_free (h->short_name);
236 GNUNET_free (h->section);
237 if (NULL != h->bloom_name)
239 if (0 != UNLINK (h->bloom_name))
240 GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING,
244 GNUNET_free (h->bloom_name);
246 GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
252 * Store an item in the datastore.
254 * @param h handle to the datacache
255 * @param key key to store data under
256 * @param data_size number of bytes in @a data
257 * @param data data to store
258 * @param type type of the value
259 * @param discard_time when to discard the value in any case
260 * @param path_info_len number of entries in @a path_info
261 * @param path_info a path through the network
262 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate
265 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
266 const struct GNUNET_HashCode *key,
269 enum GNUNET_BLOCK_Type type,
270 struct GNUNET_TIME_Absolute discard_time,
271 unsigned int path_info_len,
272 const struct GNUNET_PeerIdentity *path_info)
276 used = h->api->put (h->api->cls, key,
279 path_info_len, path_info);
283 return GNUNET_SYSERR;
290 LOG (GNUNET_ERROR_TYPE_DEBUG,
291 "Stored data under key `%s' in cache\n",
293 GNUNET_STATISTICS_update (h->stats,
294 gettext_noop ("# bytes stored"),
297 GNUNET_STATISTICS_update (h->stats,
298 gettext_noop ("# items stored"),
301 if (NULL != h->filter)
302 GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
303 while (h->utilization + used > h->env.quota)
304 GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
305 h->utilization += used;
311 * Iterate over the results for a particular key
314 * @param h handle to the datacache
315 * @param key what to look up
316 * @param type entries of which type are relevant?
317 * @param iter maybe NULL (to just count)
318 * @param iter_cls closure for @a iter
319 * @return the number of results found
322 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
323 const struct GNUNET_HashCode *key,
324 enum GNUNET_BLOCK_Type type,
325 GNUNET_DATACACHE_Iterator iter,
328 GNUNET_STATISTICS_update (h->stats,
329 gettext_noop ("# requests received"),
332 LOG (GNUNET_ERROR_TYPE_DEBUG,
333 "Processing request for key `%s'\n",
335 if ( (NULL != h->filter) &&
336 (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key)) )
338 GNUNET_STATISTICS_update (h->stats,
339 gettext_noop ("# requests filtered by bloom filter"),
342 LOG (GNUNET_ERROR_TYPE_DEBUG,
343 "Bloomfilter filters request for key `%s'\n",
345 return 0; /* can not be present */
347 return h->api->get (h->api->cls,
354 * Obtain a random element from the datacache.
356 * @param h handle to the datacache
357 * @param iter maybe NULL (to just count)
358 * @param iter_cls closure for @a iter
359 * @return the number of results found (zero or 1)
362 GNUNET_DATACACHE_get_random (struct GNUNET_DATACACHE_Handle *h,
363 GNUNET_DATACACHE_Iterator iter,
366 GNUNET_STATISTICS_update (h->stats,
367 gettext_noop ("# requests for random value received"),
370 LOG (GNUNET_ERROR_TYPE_DEBUG,
371 "Processing request for random value\n");
372 return h->api->get_random (h->api->cls,
379 * Iterate over the results that are "close" to a particular key in
380 * the datacache. "close" is defined as numerically larger than @a
381 * key (when interpreted as a circular address space), with small
384 * @param h handle to the datacache
385 * @param key area of the keyspace to look into
386 * @param num_results number of results that should be returned to @a iter
387 * @param iter maybe NULL (to just count)
388 * @param iter_cls closure for @a iter
389 * @return the number of results found
392 GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h,
393 const struct GNUNET_HashCode *key,
394 unsigned int num_results,
395 GNUNET_DATACACHE_Iterator iter,
398 GNUNET_STATISTICS_update (h->stats,
399 gettext_noop ("# proximity search requests received"),
402 LOG (GNUNET_ERROR_TYPE_DEBUG,
403 "Processing proximity search at `%s'\n",
405 return h->api->get_closest (h->api->cls,
413 /* end of datacache.c */