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 * Internal state of the datacache library.
35 struct GNUNET_DATACACHE_Handle
39 * Bloomfilter to quickly tell if we don't have the content.
41 struct GNUNET_CONTAINER_BloomFilter *filter;
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Opaque handle for the statistics service.
51 struct GNUNET_STATISTICS_Handle *stats;
54 * Configuration section to use.
59 * API of the transport as returned by the plugin's
60 * initialization function.
62 struct GNUNET_DATACACHE_PluginFunctions *api;
65 * Short name for the plugin (i.e. "sqlite").
70 * Name of the library (i.e. "gnunet_plugin_datacache_sqlite").
75 * Name for the bloom filter file.
80 * Environment provided to our plugin.
82 struct GNUNET_DATACACHE_PluginEnvironment env;
85 * How much space is in use right now?
87 unsigned long long utilization;
93 * Function called by plugins to notify the datacache
94 * about content deletions.
97 * @param key key of the content that was deleted
98 * @param size number of bytes that were made available
101 env_delete_notify (void *cls,
102 const GNUNET_HashCode *key,
105 struct GNUNET_DATACACHE_Handle * h = cls;
106 GNUNET_assert (h->utilization >= size);
107 h->utilization -= size;
108 GNUNET_CONTAINER_bloomfilter_remove (h->filter, key);
109 GNUNET_STATISTICS_update (h->stats,
110 gettext_noop ("# bytes stored"),
117 * Create a data cache.
119 * @param cfg configuration to use
120 * @param section section in the configuration that contains our options
121 * @return handle to use to access the service
123 struct GNUNET_DATACACHE_Handle *
124 GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
127 unsigned int bf_size;
128 unsigned long long quota;
129 struct GNUNET_DATACACHE_Handle *ret;
134 GNUNET_CONFIGURATION_get_value_number (cfg,
135 section, "QUOTA", "a))
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
138 _("No `%s' specified for `%s' in configuration!\n"),
144 GNUNET_CONFIGURATION_get_value_string (cfg,
148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
149 _("No `%s' specified for `%s' in configuration!\n"),
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));
157 ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
158 if (NULL != ret->bloom_name)
160 ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name,
161 quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
166 ret->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, bf_size, 5); /* approx. 3% false positives at max use */
168 ret->stats = GNUNET_STATISTICS_create ("datacache",
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 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
178 _("Loading `%s' datacache plugin\n"), name);
179 GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name);
180 ret->short_name = name;
181 ret->lib_name = libname;
182 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
183 if (ret->api == NULL)
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186 _("Failed to load datacache plugin for `%s'\n"), name);
187 GNUNET_DATACACHE_destroy (ret);
195 * Destroy a data cache (and free associated resources).
197 * @param h handle to the datastore
199 void 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_strerror_file (GNUNET_ERROR_TYPE_WARNING,
214 GNUNET_free (h->bloom_name);
216 GNUNET_STATISTICS_destroy (h->stats,
223 * Store an item in the datastore.
225 * @param h handle to the datacache
226 * @param key key to store data under
227 * @param size number of bytes in data
228 * @param data data to store
229 * @param type type of the value
230 * @param discard_time when to discard the value in any case
231 * @return GNUNET_OK on success, GNUNET_SYSERR on error (full, etc.)
234 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
235 const GNUNET_HashCode * key,
238 enum GNUNET_BLOCK_Type type,
239 struct GNUNET_TIME_Absolute discard_time)
243 used = h->api->put (h->api->cls,
252 return GNUNET_SYSERR;
254 GNUNET_STATISTICS_update (h->stats,
255 gettext_noop ("# bytes stored"),
258 GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
259 while (h->utilization + used > h->env.quota)
260 GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
261 h->utilization += used;
267 * Iterate over the results for a particular key
270 * @param h handle to the datacache
271 * @param key what to look up
272 * @param type entries of which type are relevant?
273 * @param iter maybe NULL (to just count)
274 * @param iter_cls closure for iter
275 * @return the number of results found
278 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
279 const GNUNET_HashCode * key,
280 enum GNUNET_BLOCK_Type type,
281 GNUNET_DATACACHE_Iterator iter,
284 GNUNET_STATISTICS_update (h->stats,
285 gettext_noop ("# requests received"),
288 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter,
291 GNUNET_STATISTICS_update (h->stats,
292 gettext_noop ("# requests filtered by bloom filter"),
295 return 0; /* can not be present */
297 return h->api->get (h->api->cls,
306 /* end of datacache_api.c */