diff options
Diffstat (limited to 'src/datacache/datacache.c')
-rw-r--r-- | src/datacache/datacache.c | 371 |
1 files changed, 0 insertions, 371 deletions
diff --git a/src/datacache/datacache.c b/src/datacache/datacache.c deleted file mode 100644 index 761ab801f..000000000 --- a/src/datacache/datacache.c +++ /dev/null | |||
@@ -1,371 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file datacache/datacache.c | ||
22 | * @brief datacache API implementation | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_util_lib.h" | ||
27 | #include "gnunet_datacache_lib.h" | ||
28 | #include "gnunet_statistics_service.h" | ||
29 | #include "gnunet_datacache_plugin.h" | ||
30 | |||
31 | |||
32 | #define LOG(kind, ...) GNUNET_log_from (kind, "datacache", __VA_ARGS__) | ||
33 | |||
34 | #define LOG_STRERROR_FILE(kind, op, fn) \ | ||
35 | 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 | * 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 | * Opaque handle for the statistics service. | ||
54 | */ | ||
55 | struct GNUNET_STATISTICS_Handle *stats; | ||
56 | |||
57 | /** | ||
58 | * Configuration section to use. | ||
59 | */ | ||
60 | char *section; | ||
61 | |||
62 | /** | ||
63 | * API of the transport as returned by the plugin's | ||
64 | * initialization function. | ||
65 | */ | ||
66 | struct GNUNET_DATACACHE_PluginFunctions *api; | ||
67 | |||
68 | /** | ||
69 | * Short name for the plugin (e.g. "sqlite"). | ||
70 | */ | ||
71 | char *short_name; | ||
72 | |||
73 | /** | ||
74 | * Name of the library (e.g. "gnunet_plugin_datacache_sqlite"). | ||
75 | */ | ||
76 | char *lib_name; | ||
77 | |||
78 | /** | ||
79 | * Name for the bloom filter file. | ||
80 | */ | ||
81 | char *bloom_name; | ||
82 | |||
83 | /** | ||
84 | * Environment provided to our plugin. | ||
85 | */ | ||
86 | struct GNUNET_DATACACHE_PluginEnvironment env; | ||
87 | |||
88 | /** | ||
89 | * How much space is in use right now? | ||
90 | */ | ||
91 | unsigned long long utilization; | ||
92 | }; | ||
93 | |||
94 | |||
95 | /** | ||
96 | * Function called by plugins to notify the datacache | ||
97 | * about content deletions. | ||
98 | * | ||
99 | * @param cls closure | ||
100 | * @param key key of the content that was deleted | ||
101 | * @param size number of bytes that were made available | ||
102 | */ | ||
103 | static void | ||
104 | env_delete_notify (void *cls, | ||
105 | const struct GNUNET_HashCode *key, | ||
106 | size_t size) | ||
107 | { | ||
108 | struct GNUNET_DATACACHE_Handle *h = cls; | ||
109 | |||
110 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
111 | "Content under key `%s' discarded\n", | ||
112 | GNUNET_h2s (key)); | ||
113 | GNUNET_assert (h->utilization >= size); | ||
114 | h->utilization -= size; | ||
115 | GNUNET_CONTAINER_bloomfilter_remove (h->filter, key); | ||
116 | GNUNET_STATISTICS_update (h->stats, | ||
117 | "# bytes stored", | ||
118 | -(long long) size, | ||
119 | GNUNET_NO); | ||
120 | GNUNET_STATISTICS_update (h->stats, | ||
121 | "# items stored", | ||
122 | -1, | ||
123 | GNUNET_NO); | ||
124 | } | ||
125 | |||
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 | const struct GNUNET_OS_ProjectData *pd; | ||
137 | |||
138 | if (GNUNET_OK != | ||
139 | GNUNET_CONFIGURATION_get_value_size (cfg, | ||
140 | section, | ||
141 | "QUOTA", | ||
142 | "a)) | ||
143 | { | ||
144 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
145 | section, | ||
146 | "QUOTA"); | ||
147 | return NULL; | ||
148 | } | ||
149 | if (GNUNET_OK != | ||
150 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
151 | section, | ||
152 | "DATABASE", | ||
153 | &name)) | ||
154 | { | ||
155 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
156 | section, | ||
157 | "DATABASE"); | ||
158 | return NULL; | ||
159 | } | ||
160 | bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */ | ||
161 | |||
162 | ret = GNUNET_new (struct GNUNET_DATACACHE_Handle); | ||
163 | |||
164 | if (GNUNET_YES != | ||
165 | GNUNET_CONFIGURATION_get_value_yesno (cfg, | ||
166 | section, | ||
167 | "DISABLE_BF")) | ||
168 | { | ||
169 | if (GNUNET_YES != | ||
170 | GNUNET_CONFIGURATION_get_value_yesno (cfg, | ||
171 | section, | ||
172 | "DISABLE_BF_RC")) | ||
173 | { | ||
174 | ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom"); | ||
175 | } | ||
176 | if (NULL != ret->bloom_name) | ||
177 | { | ||
178 | ret->filter = GNUNET_CONTAINER_bloomfilter_load ( | ||
179 | ret->bloom_name, | ||
180 | quota / 1024, /* 8 bit per entry in DB, expect 1k entries */ | ||
181 | 5); | ||
182 | } | ||
183 | if (NULL == ret->filter) | ||
184 | { | ||
185 | ret->filter = | ||
186 | GNUNET_CONTAINER_bloomfilter_init (NULL, | ||
187 | bf_size, | ||
188 | 5); /* approx. 3% false positives at max use */ | ||
189 | } | ||
190 | } | ||
191 | ret->stats = GNUNET_STATISTICS_create ("datacache", | ||
192 | cfg); | ||
193 | ret->section = GNUNET_strdup (section); | ||
194 | ret->env.cfg = cfg; | ||
195 | ret->env.delete_notify = &env_delete_notify; | ||
196 | ret->env.section = ret->section; | ||
197 | ret->env.cls = ret; | ||
198 | ret->env.delete_notify = &env_delete_notify; | ||
199 | ret->env.quota = quota; | ||
200 | LOG (GNUNET_ERROR_TYPE_INFO, | ||
201 | "Loading `%s' datacache plugin\n", | ||
202 | name); | ||
203 | GNUNET_asprintf (&libname, | ||
204 | "libgnunet_plugin_datacache_%s", | ||
205 | name); | ||
206 | ret->short_name = name; | ||
207 | ret->lib_name = libname; | ||
208 | /* Load the plugin within GNUnet's default context */ | ||
209 | pd = GNUNET_OS_project_data_get (); | ||
210 | GNUNET_OS_init (GNUNET_OS_project_data_default ()); | ||
211 | ret->api = GNUNET_PLUGIN_load (libname, | ||
212 | &ret->env); | ||
213 | GNUNET_OS_init (pd); | ||
214 | if (NULL == ret->api) | ||
215 | { | ||
216 | /* Try to load the plugin within the application's context | ||
217 | This normally happens when the application is not GNUnet itself but a | ||
218 | third party; inside GNUnet this is effectively a double failure. */ | ||
219 | ret->api = GNUNET_PLUGIN_load (libname, | ||
220 | &ret->env); | ||
221 | if (NULL == ret->api) | ||
222 | { | ||
223 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
224 | "Failed to load datacache plugin for `%s'\n", | ||
225 | name); | ||
226 | GNUNET_DATACACHE_destroy (ret); | ||
227 | return NULL; | ||
228 | } | ||
229 | } | ||
230 | return ret; | ||
231 | } | ||
232 | |||
233 | |||
234 | void | ||
235 | GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h) | ||
236 | { | ||
237 | if (NULL != h->filter) | ||
238 | GNUNET_CONTAINER_bloomfilter_free (h->filter); | ||
239 | if (NULL != h->api) | ||
240 | GNUNET_break (NULL == | ||
241 | GNUNET_PLUGIN_unload (h->lib_name, | ||
242 | h->api)); | ||
243 | GNUNET_free (h->lib_name); | ||
244 | GNUNET_free (h->short_name); | ||
245 | GNUNET_free (h->section); | ||
246 | if (NULL != h->bloom_name) | ||
247 | { | ||
248 | if (0 != unlink (h->bloom_name)) | ||
249 | GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING, | ||
250 | "datacache", | ||
251 | "unlink", | ||
252 | h->bloom_name); | ||
253 | GNUNET_free (h->bloom_name); | ||
254 | } | ||
255 | GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO); | ||
256 | GNUNET_free (h); | ||
257 | } | ||
258 | |||
259 | |||
260 | enum GNUNET_GenericReturnValue | ||
261 | GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h, | ||
262 | const struct GNUNET_HashCode *key, | ||
263 | uint32_t xor_distance, | ||
264 | size_t data_size, | ||
265 | const char *data, | ||
266 | enum GNUNET_BLOCK_Type type, | ||
267 | struct GNUNET_TIME_Absolute discard_time, | ||
268 | unsigned int path_info_len, | ||
269 | const struct GNUNET_DHT_PathElement *path_info) | ||
270 | { | ||
271 | ssize_t used; | ||
272 | |||
273 | used = h->api->put (h->api->cls, | ||
274 | key, | ||
275 | xor_distance, | ||
276 | data_size, | ||
277 | data, | ||
278 | type, | ||
279 | discard_time, | ||
280 | path_info_len, | ||
281 | path_info); | ||
282 | if (-1 == used) | ||
283 | { | ||
284 | GNUNET_break (0); | ||
285 | return GNUNET_SYSERR; | ||
286 | } | ||
287 | if (0 == used) | ||
288 | { | ||
289 | /* duplicate */ | ||
290 | return GNUNET_NO; | ||
291 | } | ||
292 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
293 | "Stored data under key `%s' in cache\n", | ||
294 | GNUNET_h2s (key)); | ||
295 | if (NULL != h->filter) | ||
296 | GNUNET_CONTAINER_bloomfilter_add (h->filter, | ||
297 | key); | ||
298 | GNUNET_STATISTICS_update (h->stats, | ||
299 | "# bytes stored", | ||
300 | used, | ||
301 | GNUNET_NO); | ||
302 | GNUNET_STATISTICS_update (h->stats, | ||
303 | "# items stored", | ||
304 | 1, | ||
305 | GNUNET_NO); | ||
306 | while (h->utilization + used > h->env.quota) | ||
307 | GNUNET_assert (GNUNET_OK == | ||
308 | h->api->del (h->api->cls)); | ||
309 | h->utilization += used; | ||
310 | return GNUNET_OK; | ||
311 | } | ||
312 | |||
313 | |||
314 | unsigned int | ||
315 | GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h, | ||
316 | const struct GNUNET_HashCode *key, | ||
317 | enum GNUNET_BLOCK_Type type, | ||
318 | GNUNET_DATACACHE_Iterator iter, | ||
319 | void *iter_cls) | ||
320 | { | ||
321 | GNUNET_STATISTICS_update (h->stats, | ||
322 | "# requests received", | ||
323 | 1, | ||
324 | GNUNET_NO); | ||
325 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
326 | "Processing request for key `%s'\n", | ||
327 | GNUNET_h2s (key)); | ||
328 | if ((NULL != h->filter) && | ||
329 | (GNUNET_OK != | ||
330 | GNUNET_CONTAINER_bloomfilter_test (h->filter, key))) | ||
331 | { | ||
332 | GNUNET_STATISTICS_update (h->stats, | ||
333 | "# requests filtered by bloom filter", | ||
334 | 1, | ||
335 | GNUNET_NO); | ||
336 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
337 | "Bloomfilter filters request for key `%s'\n", | ||
338 | GNUNET_h2s (key)); | ||
339 | return 0; /* can not be present */ | ||
340 | } | ||
341 | return h->api->get (h->api->cls, | ||
342 | key, | ||
343 | type, | ||
344 | iter, iter_cls); | ||
345 | } | ||
346 | |||
347 | |||
348 | unsigned int | ||
349 | GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h, | ||
350 | const struct GNUNET_HashCode *key, | ||
351 | enum GNUNET_BLOCK_Type type, | ||
352 | unsigned int num_results, | ||
353 | GNUNET_DATACACHE_Iterator iter, | ||
354 | void *iter_cls) | ||
355 | { | ||
356 | GNUNET_STATISTICS_update (h->stats, | ||
357 | "# proximity search requests received", | ||
358 | 1, | ||
359 | GNUNET_NO); | ||
360 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
361 | "Processing proximity search at `%s'\n", | ||
362 | GNUNET_h2s (key)); | ||
363 | return h->api->get_closest (h->api->cls, | ||
364 | key, | ||
365 | type, | ||
366 | num_results, | ||
367 | iter, iter_cls); | ||
368 | } | ||
369 | |||
370 | |||
371 | /* end of datacache.c */ | ||