2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009, 2010 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file datacache/datacache.c
23 * @brief datacache API implementation
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_datacache_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_datacache_plugin.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "datacache", __VA_ARGS__)
35 #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache", op, fn)
38 * Internal state of the datacache library.
40 struct GNUNET_DATACACHE_Handle
44 * Bloomfilter to quickly tell if we don't have the content.
46 struct GNUNET_CONTAINER_BloomFilter *filter;
51 const struct GNUNET_CONFIGURATION_Handle *cfg;
54 * Opaque handle for the statistics service.
56 struct GNUNET_STATISTICS_Handle *stats;
59 * Configuration section to use.
64 * API of the transport as returned by the plugin's
65 * initialization function.
67 struct GNUNET_DATACACHE_PluginFunctions *api;
70 * Short name for the plugin (i.e. "sqlite").
75 * Name of the library (i.e. "gnunet_plugin_datacache_sqlite").
80 * Name for the bloom filter file.
85 * Environment provided to our plugin.
87 struct GNUNET_DATACACHE_PluginEnvironment env;
90 * How much space is in use right now?
92 unsigned long long utilization;
98 * Function called by plugins to notify the datacache
99 * about content deletions.
102 * @param key key of the content that was deleted
103 * @param size number of bytes that were made available
106 env_delete_notify (void *cls, const struct GNUNET_HashCode * key, size_t size)
108 struct GNUNET_DATACACHE_Handle *h = cls;
110 LOG (GNUNET_ERROR_TYPE_DEBUG, "Content under key `%s' discarded\n",
112 GNUNET_assert (h->utilization >= size);
113 h->utilization -= size;
114 GNUNET_CONTAINER_bloomfilter_remove (h->filter, key);
115 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# bytes stored"), -size,
117 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# items stored"), -1,
123 * Create a data cache.
125 * @param cfg configuration to use
126 * @param section section in the configuration that contains our options
127 * @return handle to use to access the service
129 struct GNUNET_DATACACHE_Handle *
130 GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
133 unsigned int bf_size;
134 unsigned long long quota;
135 struct GNUNET_DATACACHE_Handle *ret;
140 GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", "a))
142 LOG (GNUNET_ERROR_TYPE_ERROR,
143 _("No `%s' specified for `%s' in configuration!\n"), "QUOTA", section);
147 GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name))
149 LOG (GNUNET_ERROR_TYPE_ERROR,
150 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
154 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
156 ret = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_Handle));
159 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF_RC"))
161 fprintf (stderr, "Using RC!\n");
162 ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
164 if (NULL != ret->bloom_name)
166 ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name, quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
170 if (NULL == ret->filter)
172 ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
174 if (NULL == ret->filter)
177 GNUNET_free (ret->bloom_name);
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"), name);
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 (h->filter != NULL)
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 (h->bloom_name != NULL)
222 if (0 != UNLINK (h->bloom_name))
223 GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING, "datacache",
224 "unlink", h->bloom_name);
225 GNUNET_free (h->bloom_name);
227 GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
233 * Store an item in the datastore.
235 * @param h handle to the datacache
236 * @param key key to store data under
237 * @param size number of bytes in data
238 * @param data data to store
239 * @param type type of the value
240 * @param discard_time when to discard the value in any case
241 * @return GNUNET_OK on success, GNUNET_SYSERR on error (full, etc.)
244 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
245 const struct GNUNET_HashCode * key, size_t size,
246 const char *data, enum GNUNET_BLOCK_Type type,
247 struct GNUNET_TIME_Absolute discard_time)
251 used = h->api->put (h->api->cls, key, size, data, type, discard_time);
255 return GNUNET_SYSERR;
257 LOG (GNUNET_ERROR_TYPE_DEBUG, "Stored data under key `%s' in cache\n",
259 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# bytes stored"), size,
261 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# items stored"), 1,
263 GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
264 while (h->utilization + used > h->env.quota)
265 GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
266 h->utilization += used;
272 * Iterate over the results for a particular key
275 * @param h handle to the datacache
276 * @param key what to look up
277 * @param type entries of which type are relevant?
278 * @param iter maybe NULL (to just count)
279 * @param iter_cls closure for iter
280 * @return the number of results found
283 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
284 const struct GNUNET_HashCode * key, enum GNUNET_BLOCK_Type type,
285 GNUNET_DATACACHE_Iterator iter, void *iter_cls)
287 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# requests received"), 1,
289 LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing request for key `%s'\n",
291 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key))
293 GNUNET_STATISTICS_update (h->stats,
295 ("# requests filtered by bloom filter"), 1,
297 LOG (GNUNET_ERROR_TYPE_DEBUG, "Bloomfilter filters request for key `%s'\n",
299 return 0; /* can not be present */
301 return h->api->get (h->api->cls, key, type, iter, iter_cls);
306 /* end of datacache_api.c */