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