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"
32 #define DEBUG_DATACACHE GNUNET_EXTRA_LOGGING
34 #define LOG(kind,...) GNUNET_log_from (kind, "datacache", __VA_ARGS__)
36 #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache", op, fn)
39 * Internal state of the datacache library.
41 struct GNUNET_DATACACHE_Handle
45 * Bloomfilter to quickly tell if we don't have the content.
47 struct GNUNET_CONTAINER_BloomFilter *filter;
52 const struct GNUNET_CONFIGURATION_Handle *cfg;
55 * Opaque handle for the statistics service.
57 struct GNUNET_STATISTICS_Handle *stats;
60 * Configuration section to use.
65 * API of the transport as returned by the plugin's
66 * initialization function.
68 struct GNUNET_DATACACHE_PluginFunctions *api;
71 * Short name for the plugin (i.e. "sqlite").
76 * Name of the library (i.e. "gnunet_plugin_datacache_sqlite").
81 * Name for the bloom filter file.
86 * Environment provided to our plugin.
88 struct GNUNET_DATACACHE_PluginEnvironment env;
91 * How much space is in use right now?
93 unsigned long long utilization;
99 * Function called by plugins to notify the datacache
100 * about content deletions.
103 * @param key key of the content that was deleted
104 * @param size number of bytes that were made available
107 env_delete_notify (void *cls, const GNUNET_HashCode * key, size_t size)
109 struct GNUNET_DATACACHE_Handle *h = cls;
112 LOG (GNUNET_ERROR_TYPE_DEBUG,
113 "Content under key `%s' discarded\n",
116 GNUNET_assert (h->utilization >= size);
117 h->utilization -= size;
118 GNUNET_CONTAINER_bloomfilter_remove (h->filter, key);
119 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# bytes stored"), -size,
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_number (cfg, section, "QUOTA", "a))
144 LOG (GNUNET_ERROR_TYPE_ERROR,
145 _("No `%s' specified for `%s' in configuration!\n"), "QUOTA", section);
149 GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name))
151 LOG (GNUNET_ERROR_TYPE_ERROR,
152 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
156 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
158 ret = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_Handle));
159 ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
160 if (NULL != ret->bloom_name)
162 ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name, quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
167 ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
169 ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
170 ret->section = GNUNET_strdup (section);
172 ret->env.delete_notify = &env_delete_notify;
173 ret->env.section = ret->section;
175 ret->env.delete_notify = &env_delete_notify;
176 ret->env.quota = quota;
177 LOG (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datacache plugin\n"), name);
178 GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name);
179 ret->short_name = name;
180 ret->lib_name = libname;
181 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
182 if (ret->api == NULL)
184 LOG (GNUNET_ERROR_TYPE_ERROR,
185 _("Failed to load datacache plugin for `%s'\n"), name);
186 GNUNET_DATACACHE_destroy (ret);
194 * Destroy a data cache (and free associated resources).
196 * @param h handle to the datastore
199 GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
201 if (h->filter != NULL)
202 GNUNET_CONTAINER_bloomfilter_free (h->filter);
204 GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api));
205 GNUNET_free (h->lib_name);
206 GNUNET_free (h->short_name);
207 GNUNET_free (h->section);
208 if (h->bloom_name != NULL)
210 if (0 != UNLINK (h->bloom_name))
211 GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING, "datacache",
212 "unlink", h->bloom_name);
213 GNUNET_free (h->bloom_name);
215 GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
221 * Store an item in the datastore.
223 * @param h handle to the datacache
224 * @param key key to store data under
225 * @param size number of bytes in data
226 * @param data data to store
227 * @param type type of the value
228 * @param discard_time when to discard the value in any case
229 * @return GNUNET_OK on success, GNUNET_SYSERR on error (full, etc.)
232 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
233 const GNUNET_HashCode * key, size_t size,
234 const char *data, enum GNUNET_BLOCK_Type type,
235 struct GNUNET_TIME_Absolute discard_time)
239 used = h->api->put (h->api->cls, key, size, data, type, discard_time);
243 return GNUNET_SYSERR;
246 LOG (GNUNET_ERROR_TYPE_DEBUG,
247 "Stored data under key `%s' in cache\n",
250 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# bytes stored"), size,
252 GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
253 while (h->utilization + used > h->env.quota)
254 GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
255 h->utilization += used;
261 * Iterate over the results for a particular key
264 * @param h handle to the datacache
265 * @param key what to look up
266 * @param type entries of which type are relevant?
267 * @param iter maybe NULL (to just count)
268 * @param iter_cls closure for iter
269 * @return the number of results found
272 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
273 const GNUNET_HashCode * key, enum GNUNET_BLOCK_Type type,
274 GNUNET_DATACACHE_Iterator iter, void *iter_cls)
276 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# requests received"), 1,
279 LOG (GNUNET_ERROR_TYPE_DEBUG,
280 "Processing request for key `%s'\n",
283 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key))
285 GNUNET_STATISTICS_update (h->stats,
287 ("# requests filtered by bloom filter"), 1,
290 LOG (GNUNET_ERROR_TYPE_DEBUG,
291 "Bloomfilter filters request for key `%s'\n",
294 return 0; /* can not be present */
296 return h->api->get (h->api->cls, key, type, iter, iter_cls);
301 /* end of datacache_api.c */