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