diff options
author | Christian Grothoff <christian@grothoff.org> | 2009-07-27 18:20:25 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2009-07-27 18:20:25 +0000 |
commit | c5cda378386d01412ada7fbf8d51317bab1ab695 (patch) | |
tree | 203d2b0a92fc3482730060808df7bb8a1e724b2b /src/datacache/datacache.c | |
parent | 53f0065b1c2ef64a399f8450a7779ac4fef6b1b5 (diff) | |
download | gnunet-c5cda378386d01412ada7fbf8d51317bab1ab695.tar.gz gnunet-c5cda378386d01412ada7fbf8d51317bab1ab695.zip |
renaming for consistency
Diffstat (limited to 'src/datacache/datacache.c')
-rw-r--r-- | src/datacache/datacache.c | 283 |
1 files changed, 283 insertions, 0 deletions
diff --git a/src/datacache/datacache.c b/src/datacache/datacache.c new file mode 100644 index 000000000..421acac0f --- /dev/null +++ b/src/datacache/datacache.c | |||
@@ -0,0 +1,283 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2004, 2005, 2006, 2007, 2009 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_api.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 "plugin_datacache.h" | ||
30 | |||
31 | /** | ||
32 | * Internal state of the datacache library. | ||
33 | */ | ||
34 | struct GNUNET_DATACACHE_Handle | ||
35 | { | ||
36 | |||
37 | /** | ||
38 | * Our datastore plugin (NULL if not available). | ||
39 | */ | ||
40 | struct DatastorePlugin *plugin; | ||
41 | |||
42 | /** | ||
43 | * Bloomfilter to quickly tell if we don't have the content. | ||
44 | */ | ||
45 | struct GNUNET_CONTAINER_BloomFilter *filter; | ||
46 | |||
47 | /** | ||
48 | * Our configuration. | ||
49 | */ | ||
50 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
51 | |||
52 | /** | ||
53 | * Configuration section to use. | ||
54 | */ | ||
55 | char *section; | ||
56 | |||
57 | /** | ||
58 | * API of the transport as returned by the plugin's | ||
59 | * initialization function. | ||
60 | */ | ||
61 | struct GNUNET_DATACACHE_PluginFunctions *api; | ||
62 | |||
63 | /** | ||
64 | * Short name for the plugin (i.e. "sqlite"). | ||
65 | */ | ||
66 | char *short_name; | ||
67 | |||
68 | /** | ||
69 | * Name of the library (i.e. "gnunet_plugin_datacache_sqlite"). | ||
70 | */ | ||
71 | char *lib_name; | ||
72 | |||
73 | /** | ||
74 | * Name for the bloom filter file. | ||
75 | */ | ||
76 | char *bloom_name; | ||
77 | |||
78 | /** | ||
79 | * Environment provided to our plugin. | ||
80 | */ | ||
81 | struct GNUNET_DATACACHE_PluginEnvironment env; | ||
82 | |||
83 | /** | ||
84 | * How much space is in use right now? | ||
85 | */ | ||
86 | unsigned long long utilization; | ||
87 | |||
88 | }; | ||
89 | |||
90 | |||
91 | /** | ||
92 | * Function called by plugins to notify the datacache | ||
93 | * about content deletions. | ||
94 | * | ||
95 | * @param cls closure | ||
96 | * @param key key of the content that was deleted | ||
97 | * @param size number of bytes that were made available | ||
98 | */ | ||
99 | static void | ||
100 | env_delete_notify (void *cls, | ||
101 | const GNUNET_HashCode *key, | ||
102 | uint32_t size) | ||
103 | { | ||
104 | struct GNUNET_DATACACHE_Handle * h = cls; | ||
105 | GNUNET_assert (h->utilization >= size); | ||
106 | h->utilization -= size; | ||
107 | GNUNET_CONTAINER_bloomfilter_remove (h->filter, key); | ||
108 | } | ||
109 | |||
110 | |||
111 | /** | ||
112 | * Create a data cache. | ||
113 | * | ||
114 | * @param sched scheduler to use | ||
115 | * @param cfg configuration to use | ||
116 | * @param section section in the configuration that contains our options | ||
117 | * @return handle to use to access the service | ||
118 | */ | ||
119 | struct GNUNET_DATACACHE_Handle * | ||
120 | GNUNET_DATACACHE_create (struct GNUNET_SCHEDULER_Handle *sched, | ||
121 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
122 | const char *section) | ||
123 | { | ||
124 | int fd; | ||
125 | unsigned int bf_size; | ||
126 | unsigned long long quota; | ||
127 | struct GNUNET_DATACACHE_Handle *ret; | ||
128 | char *libname; | ||
129 | char *name; | ||
130 | |||
131 | if (GNUNET_OK != | ||
132 | GNUNET_CONFIGURATION_get_value_number (cfg, | ||
133 | section, "QUOTA", "a)) | ||
134 | { | ||
135 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
136 | _("No `%s' specified for `%s' in configuration!\n"), | ||
137 | "QUOTA", | ||
138 | section); | ||
139 | return NULL; | ||
140 | } | ||
141 | if (GNUNET_OK != | ||
142 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
143 | section, | ||
144 | "DATABASE", &name)) | ||
145 | { | ||
146 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
147 | _("No `%s' specified for `%s' in configuration!\n"), | ||
148 | "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_strdup ("/tmp/datacachebloomXXXXXX"); | ||
156 | fd = mkstemp (ret->bloom_name); | ||
157 | if (fd != -1) | ||
158 | { | ||
159 | ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name, | ||
160 | quota / 1024, /* 8 bit per entry in DB, expect 1k entries */ | ||
161 | 5); | ||
162 | CLOSE (fd); | ||
163 | } | ||
164 | else | ||
165 | { | ||
166 | ret->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, bf_size, 5); /* approx. 3% false positives at max use */ | ||
167 | } | ||
168 | ret->section = GNUNET_strdup (section); | ||
169 | ret->env.sched = sched; | ||
170 | ret->env.cfg = cfg; | ||
171 | ret->env.delete_notify = &env_delete_notify; | ||
172 | ret->env.section = ret->section; | ||
173 | ret->env.cls = ret; | ||
174 | ret->env.delete_notify = &env_delete_notify; | ||
175 | ret->env.quota = quota; | ||
176 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
177 | _("Loading `%s' datacache plugin\n"), name); | ||
178 | GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name); | ||
179 | ret->short_name = name; | ||
180 | ret->lib_name = libname; | ||
181 | ret->api = GNUNET_PLUGIN_load (libname, &ret->env); | ||
182 | if (ret->api == NULL) | ||
183 | { | ||
184 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
185 | _("Failed to load datacache plugin for `%s'\n"), name); | ||
186 | GNUNET_DATACACHE_destroy (ret); | ||
187 | return NULL; | ||
188 | } | ||
189 | return ret; | ||
190 | } | ||
191 | |||
192 | |||
193 | /** | ||
194 | * Destroy a data cache (and free associated resources). | ||
195 | * | ||
196 | * @param h handle to the datastore | ||
197 | */ | ||
198 | void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h) | ||
199 | { | ||
200 | if (h->filter != NULL) | ||
201 | GNUNET_CONTAINER_bloomfilter_free (h->filter); | ||
202 | if (h->api != NULL) | ||
203 | GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api)); | ||
204 | GNUNET_free (h->lib_name); | ||
205 | GNUNET_free (h->short_name); | ||
206 | GNUNET_free (h->section); | ||
207 | if (h->bloom_name != NULL) | ||
208 | { | ||
209 | UNLINK (h->bloom_name); | ||
210 | GNUNET_free (h->bloom_name); | ||
211 | } | ||
212 | GNUNET_free (h); | ||
213 | } | ||
214 | |||
215 | |||
216 | /** | ||
217 | * Store an item in the datastore. | ||
218 | * | ||
219 | * @param h handle to the datacache | ||
220 | * @param key key to store data under | ||
221 | * @param size number of bytes in data | ||
222 | * @param data data to store | ||
223 | * @param type type of the value | ||
224 | * @param discard_time when to discard the value in any case | ||
225 | * @return GNUNET_OK on success, GNUNET_SYSERR on error (full, etc.) | ||
226 | */ | ||
227 | int | ||
228 | GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h, | ||
229 | const GNUNET_HashCode * key, | ||
230 | uint32_t size, | ||
231 | const char *data, | ||
232 | unsigned int type, | ||
233 | struct GNUNET_TIME_Absolute discard_time) | ||
234 | { | ||
235 | uint32_t used; | ||
236 | |||
237 | used = h->api->put (h->api->cls, | ||
238 | key, | ||
239 | size, | ||
240 | data, | ||
241 | type, | ||
242 | discard_time); | ||
243 | if (used == 0) | ||
244 | return GNUNET_SYSERR; | ||
245 | GNUNET_CONTAINER_bloomfilter_add (h->filter, key); | ||
246 | while (h->utilization + used > h->env.quota) | ||
247 | GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls)); | ||
248 | h->utilization += used; | ||
249 | return GNUNET_OK; | ||
250 | } | ||
251 | |||
252 | |||
253 | /** | ||
254 | * Iterate over the results for a particular key | ||
255 | * in the datacache. | ||
256 | * | ||
257 | * @param h handle to the datacache | ||
258 | * @param key what to look up | ||
259 | * @param type entries of which type are relevant? | ||
260 | * @param iter maybe NULL (to just count) | ||
261 | * @param iter_cls closure for iter | ||
262 | * @return the number of results found | ||
263 | */ | ||
264 | unsigned int | ||
265 | GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h, | ||
266 | const GNUNET_HashCode * key, | ||
267 | unsigned int type, | ||
268 | GNUNET_DATACACHE_Iterator iter, | ||
269 | void *iter_cls) | ||
270 | { | ||
271 | if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, | ||
272 | key)) | ||
273 | return 0; /* can not be present */ | ||
274 | return h->api->get (h->api->cls, | ||
275 | key, | ||
276 | type, | ||
277 | iter, | ||
278 | iter_cls); | ||
279 | } | ||
280 | |||
281 | |||
282 | |||
283 | /* end of datacache_api.c */ | ||