-consistently use struct GNUNET_HashCode
[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
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"), -size,
116                             GNUNET_NO);
117 }
118
119
120 /**
121  * Create a data cache.
122  *
123  * @param cfg configuration to use
124  * @param section section in the configuration that contains our options
125  * @return handle to use to access the service
126  */
127 struct GNUNET_DATACACHE_Handle *
128 GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
129                          const char *section)
130 {
131   unsigned int bf_size;
132   unsigned long long quota;
133   struct GNUNET_DATACACHE_Handle *ret;
134   char *libname;
135   char *name;
136
137   if (GNUNET_OK !=
138       GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", &quota))
139   {
140     LOG (GNUNET_ERROR_TYPE_ERROR,
141          _("No `%s' specified for `%s' in configuration!\n"), "QUOTA", section);
142     return NULL;
143   }
144   if (GNUNET_OK !=
145       GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name))
146   {
147     LOG (GNUNET_ERROR_TYPE_ERROR,
148          _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
149          section);
150     return NULL;
151   }
152   bf_size = quota / 32;         /* 8 bit per entry, 1 bit per 32 kb in DB */
153
154   ret = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_Handle));
155   ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
156   if (NULL != ret->bloom_name)
157   {
158     ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name, quota / 1024,     /* 8 bit per entry in DB, expect 1k entries */
159                                                      5);
160   }
161   if (NULL == ret->filter)
162   {
163     ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
164   }
165   if (NULL == ret->filter)
166   {
167     GNUNET_free (name);
168     GNUNET_free (ret->bloom_name);
169     GNUNET_free (ret);
170     return NULL;
171   }
172   ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
173   ret->section = GNUNET_strdup (section);
174   ret->env.cfg = cfg;
175   ret->env.delete_notify = &env_delete_notify;
176   ret->env.section = ret->section;
177   ret->env.cls = ret;
178   ret->env.delete_notify = &env_delete_notify;
179   ret->env.quota = quota;
180   LOG (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datacache plugin\n"), name);
181   GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name);
182   ret->short_name = name;
183   ret->lib_name = libname;
184   ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
185   if (ret->api == NULL)
186   {
187     LOG (GNUNET_ERROR_TYPE_ERROR,
188          _("Failed to load datacache plugin for `%s'\n"), name);
189     GNUNET_DATACACHE_destroy (ret);
190     return NULL;
191   }
192   return ret;
193 }
194
195
196 /**
197  * Destroy a data cache (and free associated resources).
198  *
199  * @param h handle to the datastore
200  */
201 void
202 GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
203 {
204   if (h->filter != NULL)
205     GNUNET_CONTAINER_bloomfilter_free (h->filter);
206   if (h->api != NULL)
207     GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api));
208   GNUNET_free (h->lib_name);
209   GNUNET_free (h->short_name);
210   GNUNET_free (h->section);
211   if (h->bloom_name != NULL)
212   {
213     if (0 != UNLINK (h->bloom_name))
214       GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING, "datacache",
215                                      "unlink", h->bloom_name);
216     GNUNET_free (h->bloom_name);
217   }
218   GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
219   GNUNET_free (h);
220 }
221
222
223 /**
224  * Store an item in the datastore.
225  *
226  * @param h handle to the datacache
227  * @param key key to store data under
228  * @param size number of bytes in data
229  * @param data data to store
230  * @param type type of the value
231  * @param discard_time when to discard the value in any case
232  * @return GNUNET_OK on success, GNUNET_SYSERR on error (full, etc.)
233  */
234 int
235 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
236                       const struct GNUNET_HashCode * key, size_t size,
237                       const char *data, enum GNUNET_BLOCK_Type type,
238                       struct GNUNET_TIME_Absolute discard_time)
239 {
240   uint32_t used;
241
242   used = h->api->put (h->api->cls, key, size, data, type, discard_time);
243   if (used == 0)
244   {
245     GNUNET_break (0);
246     return GNUNET_SYSERR;
247   }
248   LOG (GNUNET_ERROR_TYPE_DEBUG, "Stored data under key `%s' in cache\n",
249        GNUNET_h2s (key));
250   GNUNET_STATISTICS_update (h->stats, gettext_noop ("# bytes stored"), size,
251                             GNUNET_NO);
252   GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
253   while (h->utilization + used > h->env.quota)
254     GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
255   h->utilization += used;
256   return GNUNET_OK;
257 }
258
259
260 /**
261  * Iterate over the results for a particular key
262  * in the datacache.
263  *
264  * @param h handle to the datacache
265  * @param key what to look up
266  * @param type entries of which type are relevant?
267  * @param iter maybe NULL (to just count)
268  * @param iter_cls closure for iter
269  * @return the number of results found
270  */
271 unsigned int
272 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
273                       const struct GNUNET_HashCode * key, enum GNUNET_BLOCK_Type type,
274                       GNUNET_DATACACHE_Iterator iter, void *iter_cls)
275 {
276   GNUNET_STATISTICS_update (h->stats, gettext_noop ("# requests received"), 1,
277                             GNUNET_NO);
278   LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing request for key `%s'\n",
279        GNUNET_h2s (key));
280   if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key))
281   {
282     GNUNET_STATISTICS_update (h->stats,
283                               gettext_noop
284                               ("# requests filtered by bloom filter"), 1,
285                               GNUNET_NO);
286     LOG (GNUNET_ERROR_TYPE_DEBUG, "Bloomfilter filters request for key `%s'\n",
287          GNUNET_h2s (key));
288     return 0;                   /* can not be present */
289   }
290   return h->api->get (h->api->cls, key, type, iter, iter_cls);
291 }
292
293
294
295 /* end of datacache_api.c */