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 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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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) \
35 GNUNET_log_from_strerror_file (kind, "datacache", op, fn)
38 * Internal state of the datacache library.
40 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;
96 * Function called by plugins to notify the datacache
97 * about content deletions.
100 * @param key key of the content that was deleted
101 * @param size number of bytes that were made available
104 env_delete_notify (void *cls, const struct GNUNET_HashCode *key, size_t size)
106 struct GNUNET_DATACACHE_Handle *h = cls;
108 LOG (GNUNET_ERROR_TYPE_DEBUG,
109 "Content under key `%s' discarded\n",
111 GNUNET_assert (h->utilization >= size);
112 h->utilization -= size;
113 GNUNET_CONTAINER_bloomfilter_remove (h->filter, key);
114 GNUNET_STATISTICS_update (h->stats,
115 gettext_noop ("# bytes stored"),
118 GNUNET_STATISTICS_update (h->stats,
119 gettext_noop ("# items stored"),
126 * Create a data cache.
128 * @param cfg configuration to use
129 * @param section section in the configuration that contains our options
130 * @return handle to use to access the service
132 struct GNUNET_DATACACHE_Handle *
133 GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
136 unsigned int bf_size;
137 unsigned long long quota;
138 struct GNUNET_DATACACHE_Handle *ret;
143 GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", "a))
145 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "QUOTA");
149 GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name))
151 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "DATABASE");
154 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
156 ret = GNUNET_new (struct GNUNET_DATACACHE_Handle);
159 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF"))
162 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF_RC"))
164 ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
166 if (NULL != ret->bloom_name)
168 ret->filter = GNUNET_CONTAINER_bloomfilter_load (
170 quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
173 if (NULL == ret->filter)
176 GNUNET_CONTAINER_bloomfilter_init (NULL,
178 5); /* approx. 3% false positives at max use */
181 ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
182 ret->section = GNUNET_strdup (section);
184 ret->env.delete_notify = &env_delete_notify;
185 ret->env.section = ret->section;
187 ret->env.delete_notify = &env_delete_notify;
188 ret->env.quota = quota;
189 LOG (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' datacache plugin\n"), name);
190 GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name);
191 ret->short_name = name;
192 ret->lib_name = libname;
193 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
194 if (ret->api == NULL)
196 LOG (GNUNET_ERROR_TYPE_ERROR,
197 _ ("Failed to load datacache plugin for `%s'\n"),
199 GNUNET_DATACACHE_destroy (ret);
207 * Destroy a data cache (and free associated resources).
209 * @param h handle to the datastore
212 GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
214 if (NULL != h->filter)
215 GNUNET_CONTAINER_bloomfilter_free (h->filter);
217 GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api));
218 GNUNET_free (h->lib_name);
219 GNUNET_free (h->short_name);
220 GNUNET_free (h->section);
221 if (NULL != h->bloom_name)
223 if (0 != unlink (h->bloom_name))
224 GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING,
228 GNUNET_free (h->bloom_name);
230 GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
236 * Store an item in the datastore.
238 * @param h handle to the datacache
239 * @param key key to store data under
240 * @param xor_distance distance of @a key to our PID
241 * @param data_size number of bytes in @a data
242 * @param data data to store
243 * @param type type of the value
244 * @param discard_time when to discard the value in any case
245 * @param path_info_len number of entries in @a path_info
246 * @param path_info a path through the network
247 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate
250 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
251 const struct GNUNET_HashCode *key,
252 uint32_t xor_distance,
255 enum GNUNET_BLOCK_Type type,
256 struct GNUNET_TIME_Absolute discard_time,
257 unsigned int path_info_len,
258 const struct GNUNET_PeerIdentity *path_info)
262 used = h->api->put (h->api->cls,
274 return GNUNET_SYSERR;
281 LOG (GNUNET_ERROR_TYPE_DEBUG,
282 "Stored data under key `%s' in cache\n",
284 if (NULL != h->filter)
285 GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
286 GNUNET_STATISTICS_update (h->stats,
287 gettext_noop ("# bytes stored"),
290 GNUNET_STATISTICS_update (h->stats,
291 gettext_noop ("# items stored"),
294 while (h->utilization + used > h->env.quota)
295 GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
296 h->utilization += used;
302 * Iterate over the results for a particular key
305 * @param h handle to the datacache
306 * @param key what to look up
307 * @param type entries of which type are relevant?
308 * @param iter maybe NULL (to just count)
309 * @param iter_cls closure for @a iter
310 * @return the number of results found
313 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
314 const struct GNUNET_HashCode *key,
315 enum GNUNET_BLOCK_Type type,
316 GNUNET_DATACACHE_Iterator iter,
319 GNUNET_STATISTICS_update (h->stats,
320 gettext_noop ("# requests received"),
323 LOG (GNUNET_ERROR_TYPE_DEBUG,
324 "Processing request for key `%s'\n",
326 if ((NULL != h->filter) &&
327 (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key)))
329 GNUNET_STATISTICS_update (h->stats,
331 "# requests filtered by bloom filter"),
334 LOG (GNUNET_ERROR_TYPE_DEBUG,
335 "Bloomfilter filters request for key `%s'\n",
337 return 0; /* can not be present */
339 return h->api->get (h->api->cls, key, type, iter, iter_cls);
344 * Obtain a random element from the datacache.
346 * @param h handle to the datacache
347 * @param iter maybe NULL (to just count)
348 * @param iter_cls closure for @a iter
349 * @return the number of results found (zero or 1)
352 GNUNET_DATACACHE_get_random (struct GNUNET_DATACACHE_Handle *h,
353 GNUNET_DATACACHE_Iterator iter,
356 GNUNET_STATISTICS_update (h->stats,
358 "# requests for random value received"),
361 LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing request for random value\n");
362 return h->api->get_random (h->api->cls, iter, iter_cls);
367 * Iterate over the results that are "close" to a particular key in
368 * the datacache. "close" is defined as numerically larger than @a
369 * key (when interpreted as a circular address space), with small
372 * @param h handle to the datacache
373 * @param key area of the keyspace to look into
374 * @param num_results number of results that should be returned to @a iter
375 * @param iter maybe NULL (to just count)
376 * @param iter_cls closure for @a iter
377 * @return the number of results found
380 GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h,
381 const struct GNUNET_HashCode *key,
382 unsigned int num_results,
383 GNUNET_DATACACHE_Iterator iter,
386 GNUNET_STATISTICS_update (h->stats,
388 "# proximity search requests received"),
391 LOG (GNUNET_ERROR_TYPE_DEBUG,
392 "Processing proximity search at `%s'\n",
394 return h->api->get_closest (h->api->cls, key, num_results, iter, iter_cls);
398 /* end of datacache.c */