2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009 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 "plugin_datacache.h"
32 * Internal state of the datacache library.
34 struct GNUNET_DATACACHE_Handle
38 * Bloomfilter to quickly tell if we don't have the content.
40 struct GNUNET_CONTAINER_BloomFilter *filter;
45 const struct GNUNET_CONFIGURATION_Handle *cfg;
48 * Configuration section to use.
53 * API of the transport as returned by the plugin's
54 * initialization function.
56 struct GNUNET_DATACACHE_PluginFunctions *api;
59 * Short name for the plugin (i.e. "sqlite").
64 * Name of the library (i.e. "gnunet_plugin_datacache_sqlite").
69 * Name for the bloom filter file.
74 * Environment provided to our plugin.
76 struct GNUNET_DATACACHE_PluginEnvironment env;
79 * How much space is in use right now?
81 unsigned long long utilization;
87 * Function called by plugins to notify the datacache
88 * about content deletions.
91 * @param key key of the content that was deleted
92 * @param size number of bytes that were made available
95 env_delete_notify (void *cls,
96 const GNUNET_HashCode *key,
99 struct GNUNET_DATACACHE_Handle * h = cls;
100 GNUNET_assert (h->utilization >= size);
101 h->utilization -= size;
102 GNUNET_CONTAINER_bloomfilter_remove (h->filter, key);
107 * Create a data cache.
109 * @param sched scheduler to use
110 * @param cfg configuration to use
111 * @param section section in the configuration that contains our options
112 * @return handle to use to access the service
114 struct GNUNET_DATACACHE_Handle *
115 GNUNET_DATACACHE_create (struct GNUNET_SCHEDULER_Handle *sched,
116 const struct GNUNET_CONFIGURATION_Handle *cfg,
119 unsigned int bf_size;
120 unsigned long long quota;
121 struct GNUNET_DATACACHE_Handle *ret;
126 GNUNET_CONFIGURATION_get_value_number (cfg,
127 section, "QUOTA", "a))
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130 _("No `%s' specified for `%s' in configuration!\n"),
136 GNUNET_CONFIGURATION_get_value_string (cfg,
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
141 _("No `%s' specified for `%s' in configuration!\n"),
146 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
148 ret = GNUNET_malloc (sizeof(struct GNUNET_DATACACHE_Handle));
149 ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
150 if (NULL != ret->bloom_name)
152 ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name,
153 quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
158 ret->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, bf_size, 5); /* approx. 3% false positives at max use */
160 ret->section = GNUNET_strdup (section);
161 ret->env.sched = sched;
163 ret->env.delete_notify = &env_delete_notify;
164 ret->env.section = ret->section;
166 ret->env.delete_notify = &env_delete_notify;
167 ret->env.quota = quota;
168 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
169 _("Loading `%s' datacache plugin\n"), name);
170 GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name);
171 ret->short_name = name;
172 ret->lib_name = libname;
173 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
174 if (ret->api == NULL)
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
177 _("Failed to load datacache plugin for `%s'\n"), name);
178 GNUNET_DATACACHE_destroy (ret);
186 * Destroy a data cache (and free associated resources).
188 * @param h handle to the datastore
190 void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
192 if (h->filter != NULL)
193 GNUNET_CONTAINER_bloomfilter_free (h->filter);
195 GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api));
196 GNUNET_free (h->lib_name);
197 GNUNET_free (h->short_name);
198 GNUNET_free (h->section);
199 if (h->bloom_name != NULL)
201 if (0 != UNLINK (h->bloom_name))
202 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
205 GNUNET_free (h->bloom_name);
212 * Store an item in the datastore.
214 * @param h handle to the datacache
215 * @param key key to store data under
216 * @param size number of bytes in data
217 * @param data data to store
218 * @param type type of the value
219 * @param discard_time when to discard the value in any case
220 * @return GNUNET_OK on success, GNUNET_SYSERR on error (full, etc.)
223 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
224 const GNUNET_HashCode * key,
228 struct GNUNET_TIME_Absolute discard_time)
232 used = h->api->put (h->api->cls,
239 return GNUNET_SYSERR;
240 GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
241 while (h->utilization + used > h->env.quota)
242 GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
243 h->utilization += used;
249 * Iterate over the results for a particular key
252 * @param h handle to the datacache
253 * @param key what to look up
254 * @param type entries of which type are relevant?
255 * @param iter maybe NULL (to just count)
256 * @param iter_cls closure for iter
257 * @return the number of results found
260 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
261 const GNUNET_HashCode * key,
263 GNUNET_DATACACHE_Iterator iter,
266 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter,
268 return 0; /* can not be present */
269 return h->api->get (h->api->cls,
278 /* end of datacache_api.c */