- remove http
[oweals/gnunet.git] / src / datacache / datacache.c
1 /*
2      This file is part of GNUnet
3      (C) 2004, 2005, 2006, 2007, 2009, 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file datacache/datacache.c
23  * @brief datacache API implementation
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_datacache_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_datacache_plugin.h"
31
32 #define DEBUG_DATACACHE GNUNET_EXTRA_LOGGING
33
34 #define LOG(kind,...) GNUNET_log_from (kind, "datacache", __VA_ARGS__)
35
36 #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache", op, fn)
37
38 /**
39  * Internal state of the datacache library.
40  */
41 struct GNUNET_DATACACHE_Handle
42 {
43
44   /**
45    * Bloomfilter to quickly tell if we don't have the content.
46    */
47   struct GNUNET_CONTAINER_BloomFilter *filter;
48
49   /**
50    * Our configuration.
51    */
52   const struct GNUNET_CONFIGURATION_Handle *cfg;
53
54   /**
55    * Opaque handle for the statistics service.
56    */
57   struct GNUNET_STATISTICS_Handle *stats;
58
59   /**
60    * Configuration section to use.
61    */
62   char *section;
63
64   /**
65    * API of the transport as returned by the plugin's
66    * initialization function.
67    */
68   struct GNUNET_DATACACHE_PluginFunctions *api;
69
70   /**
71    * Short name for the plugin (i.e. "sqlite").
72    */
73   char *short_name;
74
75   /**
76    * Name of the library (i.e. "gnunet_plugin_datacache_sqlite").
77    */
78   char *lib_name;
79
80   /**
81    * Name for the bloom filter file.
82    */
83   char *bloom_name;
84
85   /**
86    * Environment provided to our plugin.
87    */
88   struct GNUNET_DATACACHE_PluginEnvironment env;
89
90   /**
91    * How much space is in use right now?
92    */
93   unsigned long long utilization;
94
95 };
96
97
98 /**
99  * Function called by plugins to notify the datacache
100  * about content deletions.
101  *
102  * @param cls closure
103  * @param key key of the content that was deleted
104  * @param size number of bytes that were made available
105  */
106 static void
107 env_delete_notify (void *cls, const GNUNET_HashCode * key, size_t size)
108 {
109   struct GNUNET_DATACACHE_Handle *h = cls;
110
111 #if DEBUG_DATACACHE
112   LOG (GNUNET_ERROR_TYPE_DEBUG, "Content under key `%s' discarded\n",
113        GNUNET_h2s (key));
114 #endif
115   GNUNET_assert (h->utilization >= size);
116   h->utilization -= size;
117   GNUNET_CONTAINER_bloomfilter_remove (h->filter, key);
118   GNUNET_STATISTICS_update (h->stats, gettext_noop ("# bytes stored"), -size,
119                             GNUNET_NO);
120 }
121
122
123 /**
124  * Create a data cache.
125  *
126  * @param cfg configuration to use
127  * @param section section in the configuration that contains our options
128  * @return handle to use to access the service
129  */
130 struct GNUNET_DATACACHE_Handle *
131 GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
132                          const char *section)
133 {
134   unsigned int bf_size;
135   unsigned long long quota;
136   struct GNUNET_DATACACHE_Handle *ret;
137   char *libname;
138   char *name;
139
140   if (GNUNET_OK !=
141       GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", &quota))
142   {
143     LOG (GNUNET_ERROR_TYPE_ERROR,
144          _("No `%s' specified for `%s' in configuration!\n"), "QUOTA", section);
145     return NULL;
146   }
147   if (GNUNET_OK !=
148       GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name))
149   {
150     LOG (GNUNET_ERROR_TYPE_ERROR,
151          _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
152          section);
153     return NULL;
154   }
155   bf_size = quota / 32;         /* 8 bit per entry, 1 bit per 32 kb in DB */
156
157   ret = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_Handle));
158   ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
159   if (NULL != ret->bloom_name)
160   {
161     ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name, quota / 1024,     /* 8 bit per entry in DB, expect 1k entries */
162                                                      5);
163   }
164   if (NULL == ret->filter)
165   {
166     ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
167   }
168   if (NULL == ret->filter)
169   {
170     GNUNET_free (name);
171     GNUNET_free (ret->bloom_name);
172     GNUNET_free (ret);
173     return NULL;
174   }
175   ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
176   ret->section = GNUNET_strdup (section);
177   ret->env.cfg = cfg;
178   ret->env.delete_notify = &env_delete_notify;
179   ret->env.section = ret->section;
180   ret->env.cls = ret;
181   ret->env.delete_notify = &env_delete_notify;
182   ret->env.quota = quota;
183   LOG (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datacache plugin\n"), name);
184   GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name);
185   ret->short_name = name;
186   ret->lib_name = libname;
187   ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
188   if (ret->api == NULL)
189   {
190     LOG (GNUNET_ERROR_TYPE_ERROR,
191          _("Failed to load datacache plugin for `%s'\n"), name);
192     GNUNET_DATACACHE_destroy (ret);
193     return NULL;
194   }
195   return ret;
196 }
197
198
199 /**
200  * Destroy a data cache (and free associated resources).
201  *
202  * @param h handle to the datastore
203  */
204 void
205 GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
206 {
207   if (h->filter != NULL)
208     GNUNET_CONTAINER_bloomfilter_free (h->filter);
209   if (h->api != NULL)
210     GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api));
211   GNUNET_free (h->lib_name);
212   GNUNET_free (h->short_name);
213   GNUNET_free (h->section);
214   if (h->bloom_name != NULL)
215   {
216     if (0 != UNLINK (h->bloom_name))
217       GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING, "datacache",
218                                      "unlink", h->bloom_name);
219     GNUNET_free (h->bloom_name);
220   }
221   GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
222   GNUNET_free (h);
223 }
224
225
226 /**
227  * Store an item in the datastore.
228  *
229  * @param h handle to the datacache
230  * @param key key to store data under
231  * @param size number of bytes in data
232  * @param data data to store
233  * @param type type of the value
234  * @param discard_time when to discard the value in any case
235  * @return GNUNET_OK on success, GNUNET_SYSERR on error (full, etc.)
236  */
237 int
238 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
239                       const GNUNET_HashCode * key, size_t size,
240                       const char *data, enum GNUNET_BLOCK_Type type,
241                       struct GNUNET_TIME_Absolute discard_time)
242 {
243   uint32_t used;
244
245   used = h->api->put (h->api->cls, key, size, data, type, discard_time);
246   if (used == 0)
247   {
248     GNUNET_break (0);
249     return GNUNET_SYSERR;
250   }
251 #if DEBUG_DATACACHE
252   LOG (GNUNET_ERROR_TYPE_DEBUG, "Stored data under key `%s' in cache\n",
253        GNUNET_h2s (key));
254 #endif
255   GNUNET_STATISTICS_update (h->stats, gettext_noop ("# bytes stored"), size,
256                             GNUNET_NO);
257   GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
258   while (h->utilization + used > h->env.quota)
259     GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
260   h->utilization += used;
261   return GNUNET_OK;
262 }
263
264
265 /**
266  * Iterate over the results for a particular key
267  * in the datacache.
268  *
269  * @param h handle to the datacache
270  * @param key what to look up
271  * @param type entries of which type are relevant?
272  * @param iter maybe NULL (to just count)
273  * @param iter_cls closure for iter
274  * @return the number of results found
275  */
276 unsigned int
277 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
278                       const GNUNET_HashCode * key, enum GNUNET_BLOCK_Type type,
279                       GNUNET_DATACACHE_Iterator iter, void *iter_cls)
280 {
281   GNUNET_STATISTICS_update (h->stats, gettext_noop ("# requests received"), 1,
282                             GNUNET_NO);
283 #if DEBUG_DATACACHE
284   LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing request for key `%s'\n",
285        GNUNET_h2s (key));
286 #endif
287   if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key))
288   {
289     GNUNET_STATISTICS_update (h->stats,
290                               gettext_noop
291                               ("# requests filtered by bloom filter"), 1,
292                               GNUNET_NO);
293 #if DEBUG_DATACACHE
294     LOG (GNUNET_ERROR_TYPE_DEBUG, "Bloomfilter filters request for key `%s'\n",
295          GNUNET_h2s (key));
296 #endif
297     return 0;                   /* can not be present */
298   }
299   return h->api->get (h->api->cls, key, type, iter, iter_cls);
300 }
301
302
303
304 /* end of datacache_api.c */