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 {
42 * Bloomfilter to quickly tell if we don't have the content.
44 struct GNUNET_CONTAINER_BloomFilter *filter;
49 const struct GNUNET_CONFIGURATION_Handle *cfg;
52 * Opaque handle for the statistics service.
54 struct GNUNET_STATISTICS_Handle *stats;
57 * Configuration section to use.
62 * API of the transport as returned by the plugin's
63 * initialization function.
65 struct GNUNET_DATACACHE_PluginFunctions *api;
68 * Short name for the plugin (i.e. "sqlite").
73 * Name of the library (i.e. "gnunet_plugin_datacache_sqlite").
78 * Name for the bloom filter file.
83 * Environment provided to our plugin.
85 struct GNUNET_DATACACHE_PluginEnvironment env;
88 * How much space is in use right now?
90 unsigned long long utilization;
95 * Function called by plugins to notify the datacache
96 * about content deletions.
99 * @param key key of the content that was deleted
100 * @param size number of bytes that were made available
103 env_delete_notify(void *cls, const struct GNUNET_HashCode *key, size_t size)
105 struct GNUNET_DATACACHE_Handle *h = cls;
107 LOG(GNUNET_ERROR_TYPE_DEBUG,
108 "Content under key `%s' discarded\n",
110 GNUNET_assert(h->utilization >= size);
111 h->utilization -= size;
112 GNUNET_CONTAINER_bloomfilter_remove(h->filter, key);
113 GNUNET_STATISTICS_update(h->stats,
114 gettext_noop("# bytes stored"),
117 GNUNET_STATISTICS_update(h->stats,
118 gettext_noop("# items stored"),
125 * Create a data cache.
127 * @param cfg configuration to use
128 * @param section section in the configuration that contains our options
129 * @return handle to use to access the service
131 struct GNUNET_DATACACHE_Handle *
132 GNUNET_DATACACHE_create(const struct GNUNET_CONFIGURATION_Handle *cfg,
135 unsigned int bf_size;
136 unsigned long long quota;
137 struct GNUNET_DATACACHE_Handle *ret;
142 GNUNET_CONFIGURATION_get_value_size(cfg, section, "QUOTA", "a))
144 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, section, "QUOTA");
148 GNUNET_CONFIGURATION_get_value_string(cfg, section, "DATABASE", &name))
150 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, section, "DATABASE");
153 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
155 ret = GNUNET_new(struct GNUNET_DATACACHE_Handle);
158 GNUNET_CONFIGURATION_get_value_yesno(cfg, section, "DISABLE_BF"))
161 GNUNET_CONFIGURATION_get_value_yesno(cfg, section, "DISABLE_BF_RC"))
163 ret->bloom_name = GNUNET_DISK_mktemp("gnunet-datacachebloom");
165 if (NULL != ret->bloom_name)
167 ret->filter = GNUNET_CONTAINER_bloomfilter_load(
169 quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
172 if (NULL == ret->filter)
175 GNUNET_CONTAINER_bloomfilter_init(NULL,
177 5); /* approx. 3% false positives at max use */
180 ret->stats = GNUNET_STATISTICS_create("datacache", cfg);
181 ret->section = GNUNET_strdup(section);
183 ret->env.delete_notify = &env_delete_notify;
184 ret->env.section = ret->section;
186 ret->env.delete_notify = &env_delete_notify;
187 ret->env.quota = quota;
188 LOG(GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datacache plugin\n"), name);
189 GNUNET_asprintf(&libname, "libgnunet_plugin_datacache_%s", name);
190 ret->short_name = name;
191 ret->lib_name = libname;
192 ret->api = GNUNET_PLUGIN_load(libname, &ret->env);
193 if (ret->api == NULL)
195 LOG(GNUNET_ERROR_TYPE_ERROR,
196 _("Failed to load datacache plugin for `%s'\n"),
198 GNUNET_DATACACHE_destroy(ret);
206 * Destroy a data cache (and free associated resources).
208 * @param h handle to the datastore
211 GNUNET_DATACACHE_destroy(struct GNUNET_DATACACHE_Handle *h)
213 if (NULL != h->filter)
214 GNUNET_CONTAINER_bloomfilter_free(h->filter);
216 GNUNET_break(NULL == GNUNET_PLUGIN_unload(h->lib_name, h->api));
217 GNUNET_free(h->lib_name);
218 GNUNET_free(h->short_name);
219 GNUNET_free(h->section);
220 if (NULL != h->bloom_name)
222 if (0 != unlink(h->bloom_name))
223 GNUNET_log_from_strerror_file(GNUNET_ERROR_TYPE_WARNING,
227 GNUNET_free(h->bloom_name);
229 GNUNET_STATISTICS_destroy(h->stats, GNUNET_NO);
235 * Store an item in the datastore.
237 * @param h handle to the datacache
238 * @param key key to store data under
239 * @param xor_distance distance of @a key to our PID
240 * @param data_size number of bytes in @a data
241 * @param data data to store
242 * @param type type of the value
243 * @param discard_time when to discard the value in any case
244 * @param path_info_len number of entries in @a path_info
245 * @param path_info a path through the network
246 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate
249 GNUNET_DATACACHE_put(struct GNUNET_DATACACHE_Handle *h,
250 const struct GNUNET_HashCode *key,
251 uint32_t xor_distance,
254 enum GNUNET_BLOCK_Type type,
255 struct GNUNET_TIME_Absolute discard_time,
256 unsigned int path_info_len,
257 const struct GNUNET_PeerIdentity *path_info)
261 used = h->api->put(h->api->cls,
273 return GNUNET_SYSERR;
280 LOG(GNUNET_ERROR_TYPE_DEBUG,
281 "Stored data under key `%s' in cache\n",
283 if (NULL != h->filter)
284 GNUNET_CONTAINER_bloomfilter_add(h->filter, key);
285 GNUNET_STATISTICS_update(h->stats,
286 gettext_noop("# bytes stored"),
289 GNUNET_STATISTICS_update(h->stats,
290 gettext_noop("# items stored"),
293 while (h->utilization + used > h->env.quota)
294 GNUNET_assert(GNUNET_OK == h->api->del(h->api->cls));
295 h->utilization += used;
301 * Iterate over the results for a particular key
304 * @param h handle to the datacache
305 * @param key what to look up
306 * @param type entries of which type are relevant?
307 * @param iter maybe NULL (to just count)
308 * @param iter_cls closure for @a iter
309 * @return the number of results found
312 GNUNET_DATACACHE_get(struct GNUNET_DATACACHE_Handle *h,
313 const struct GNUNET_HashCode *key,
314 enum GNUNET_BLOCK_Type type,
315 GNUNET_DATACACHE_Iterator iter,
318 GNUNET_STATISTICS_update(h->stats,
319 gettext_noop("# requests received"),
322 LOG(GNUNET_ERROR_TYPE_DEBUG,
323 "Processing request for key `%s'\n",
325 if ((NULL != h->filter) &&
326 (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test(h->filter, key)))
328 GNUNET_STATISTICS_update(h->stats,
330 "# requests filtered by bloom filter"),
333 LOG(GNUNET_ERROR_TYPE_DEBUG,
334 "Bloomfilter filters request for key `%s'\n",
336 return 0; /* can not be present */
338 return h->api->get(h->api->cls, key, type, iter, iter_cls);
343 * Obtain a random element from the datacache.
345 * @param h handle to the datacache
346 * @param iter maybe NULL (to just count)
347 * @param iter_cls closure for @a iter
348 * @return the number of results found (zero or 1)
351 GNUNET_DATACACHE_get_random(struct GNUNET_DATACACHE_Handle *h,
352 GNUNET_DATACACHE_Iterator iter,
355 GNUNET_STATISTICS_update(h->stats,
357 "# requests for random value received"),
360 LOG(GNUNET_ERROR_TYPE_DEBUG, "Processing request for random value\n");
361 return h->api->get_random(h->api->cls, iter, iter_cls);
366 * Iterate over the results that are "close" to a particular key in
367 * the datacache. "close" is defined as numerically larger than @a
368 * key (when interpreted as a circular address space), with small
371 * @param h handle to the datacache
372 * @param key area of the keyspace to look into
373 * @param num_results number of results that should be returned to @a iter
374 * @param iter maybe NULL (to just count)
375 * @param iter_cls closure for @a iter
376 * @return the number of results found
379 GNUNET_DATACACHE_get_closest(struct GNUNET_DATACACHE_Handle *h,
380 const struct GNUNET_HashCode *key,
381 unsigned int num_results,
382 GNUNET_DATACACHE_Iterator iter,
385 GNUNET_STATISTICS_update(h->stats,
387 "# proximity search requests received"),
390 LOG(GNUNET_ERROR_TYPE_DEBUG,
391 "Processing proximity search at `%s'\n",
393 return h->api->get_closest(h->api->cls, key, num_results, iter, iter_cls);
397 /* end of datacache.c */