diff options
Diffstat (limited to 'src/datacache/datacache.c')
-rw-r--r-- | src/datacache/datacache.c | 410 |
1 files changed, 0 insertions, 410 deletions
diff --git a/src/datacache/datacache.c b/src/datacache/datacache.c deleted file mode 100644 index 331a9b784..000000000 --- a/src/datacache/datacache.c +++ /dev/null | |||
@@ -1,410 +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, const struct GNUNET_HashCode *key, size_t size) | ||
105 | { | ||
106 | struct GNUNET_DATACACHE_Handle *h = cls; | ||
107 | |||
108 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
109 | "Content under key `%s' discarded\n", | ||
110 | GNUNET_h2s (key)); | ||
111 | GNUNET_assert (h->utilization >= size); | ||
112 | h->utilization -= size; | ||
113 | GNUNET_CONTAINER_bloomfilter_remove (h->filter, key); | ||
114 | GNUNET_STATISTICS_update (h->stats, | ||
115 | gettext_noop ("# bytes stored"), | ||
116 | -(long long) size, | ||
117 | GNUNET_NO); | ||
118 | GNUNET_STATISTICS_update (h->stats, | ||
119 | gettext_noop ("# items stored"), | ||
120 | -1, | ||
121 | GNUNET_NO); | ||
122 | } | ||
123 | |||
124 | |||
125 | /** | ||
126 | * Create a data cache. | ||
127 | * | ||
128 | * @param cfg configuration to use | ||
129 | * @param section section in the configuration that contains our options | ||
130 | * @return handle to use to access the service | ||
131 | */ | ||
132 | struct GNUNET_DATACACHE_Handle * | ||
133 | GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
134 | const char *section) | ||
135 | { | ||
136 | unsigned int bf_size; | ||
137 | unsigned long long quota; | ||
138 | struct GNUNET_DATACACHE_Handle *ret; | ||
139 | char *libname; | ||
140 | char *name; | ||
141 | const struct GNUNET_OS_ProjectData *pd; | ||
142 | |||
143 | if (GNUNET_OK != | ||
144 | GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", "a)) | ||
145 | { | ||
146 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "QUOTA"); | ||
147 | return NULL; | ||
148 | } | ||
149 | if (GNUNET_OK != | ||
150 | GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name)) | ||
151 | { | ||
152 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "DATABASE"); | ||
153 | return NULL; | ||
154 | } | ||
155 | bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */ | ||
156 | |||
157 | ret = GNUNET_new (struct GNUNET_DATACACHE_Handle); | ||
158 | |||
159 | if (GNUNET_YES != | ||
160 | GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF")) | ||
161 | { | ||
162 | if (GNUNET_YES != | ||
163 | GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF_RC")) | ||
164 | { | ||
165 | ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom"); | ||
166 | } | ||
167 | if (NULL != ret->bloom_name) | ||
168 | { | ||
169 | ret->filter = GNUNET_CONTAINER_bloomfilter_load ( | ||
170 | ret->bloom_name, | ||
171 | quota / 1024, /* 8 bit per entry in DB, expect 1k entries */ | ||
172 | 5); | ||
173 | } | ||
174 | if (NULL == ret->filter) | ||
175 | { | ||
176 | ret->filter = | ||
177 | GNUNET_CONTAINER_bloomfilter_init (NULL, | ||
178 | bf_size, | ||
179 | 5); /* approx. 3% false positives at max use */ | ||
180 | } | ||
181 | } | ||
182 | ret->stats = GNUNET_STATISTICS_create ("datacache", cfg); | ||
183 | ret->section = GNUNET_strdup (section); | ||
184 | ret->env.cfg = cfg; | ||
185 | ret->env.delete_notify = &env_delete_notify; | ||
186 | ret->env.section = ret->section; | ||
187 | ret->env.cls = ret; | ||
188 | ret->env.delete_notify = &env_delete_notify; | ||
189 | ret->env.quota = quota; | ||
190 | LOG (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' datacache plugin\n"), name); | ||
191 | GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name); | ||
192 | ret->short_name = name; | ||
193 | ret->lib_name = libname; | ||
194 | /* Load the plugin within GNUnet's default context */ | ||
195 | pd = GNUNET_OS_project_data_get (); | ||
196 | GNUNET_OS_init(GNUNET_OS_project_data_default ()); | ||
197 | ret->api = GNUNET_PLUGIN_load (libname, &ret->env); | ||
198 | GNUNET_OS_init(pd); | ||
199 | if (NULL == ret->api) | ||
200 | { | ||
201 | /* Try to load the plugin within the application's context | ||
202 | This normally happens when the application is not GNUnet itself but a | ||
203 | third party; inside GNUnet this is effectively a double failure. */ | ||
204 | ret->api = GNUNET_PLUGIN_load (libname, &ret->env); | ||
205 | if (NULL == ret->api) | ||
206 | { | ||
207 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
208 | _ ("Failed to load datacache plugin for `%s'\n"), | ||
209 | name); | ||
210 | GNUNET_DATACACHE_destroy (ret); | ||
211 | return NULL; | ||
212 | } | ||
213 | } | ||
214 | return ret; | ||
215 | } | ||
216 | |||
217 | |||
218 | /** | ||
219 | * Destroy a data cache (and free associated resources). | ||
220 | * | ||
221 | * @param h handle to the datastore | ||
222 | */ | ||
223 | void | ||
224 | GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h) | ||
225 | { | ||
226 | if (NULL != h->filter) | ||
227 | GNUNET_CONTAINER_bloomfilter_free (h->filter); | ||
228 | if (NULL != h->api) | ||
229 | GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api)); | ||
230 | GNUNET_free (h->lib_name); | ||
231 | GNUNET_free (h->short_name); | ||
232 | GNUNET_free (h->section); | ||
233 | if (NULL != h->bloom_name) | ||
234 | { | ||
235 | if (0 != unlink (h->bloom_name)) | ||
236 | GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING, | ||
237 | "datacache", | ||
238 | "unlink", | ||
239 | h->bloom_name); | ||
240 | GNUNET_free (h->bloom_name); | ||
241 | } | ||
242 | GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO); | ||
243 | GNUNET_free (h); | ||
244 | } | ||
245 | |||
246 | |||
247 | /** | ||
248 | * Store an item in the datastore. | ||
249 | * | ||
250 | * @param h handle to the datacache | ||
251 | * @param key key to store data under | ||
252 | * @param xor_distance distance of @a key to our PID | ||
253 | * @param data_size number of bytes in @a data | ||
254 | * @param data data to store | ||
255 | * @param type type of the value | ||
256 | * @param discard_time when to discard the value in any case | ||
257 | * @param path_info_len number of entries in @a path_info | ||
258 | * @param path_info a path through the network | ||
259 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate | ||
260 | */ | ||
261 | int | ||
262 | GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h, | ||
263 | const struct GNUNET_HashCode *key, | ||
264 | uint32_t xor_distance, | ||
265 | size_t data_size, | ||
266 | const char *data, | ||
267 | enum GNUNET_BLOCK_Type type, | ||
268 | struct GNUNET_TIME_Absolute discard_time, | ||
269 | unsigned int path_info_len, | ||
270 | const struct GNUNET_PeerIdentity *path_info) | ||
271 | { | ||
272 | ssize_t used; | ||
273 | |||
274 | used = h->api->put (h->api->cls, | ||
275 | key, | ||
276 | xor_distance, | ||
277 | data_size, | ||
278 | data, | ||
279 | type, | ||
280 | discard_time, | ||
281 | path_info_len, | ||
282 | path_info); | ||
283 | if (-1 == used) | ||
284 | { | ||
285 | GNUNET_break (0); | ||
286 | return GNUNET_SYSERR; | ||
287 | } | ||
288 | if (0 == used) | ||
289 | { | ||
290 | /* duplicate */ | ||
291 | return GNUNET_NO; | ||
292 | } | ||
293 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
294 | "Stored data under key `%s' in cache\n", | ||
295 | GNUNET_h2s (key)); | ||
296 | if (NULL != h->filter) | ||
297 | GNUNET_CONTAINER_bloomfilter_add (h->filter, key); | ||
298 | GNUNET_STATISTICS_update (h->stats, | ||
299 | gettext_noop ("# bytes stored"), | ||
300 | used, | ||
301 | GNUNET_NO); | ||
302 | GNUNET_STATISTICS_update (h->stats, | ||
303 | gettext_noop ("# items stored"), | ||
304 | 1, | ||
305 | GNUNET_NO); | ||
306 | while (h->utilization + used > h->env.quota) | ||
307 | GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls)); | ||
308 | h->utilization += used; | ||
309 | return GNUNET_OK; | ||
310 | } | ||
311 | |||
312 | |||
313 | /** | ||
314 | * Iterate over the results for a particular key | ||
315 | * in the datacache. | ||
316 | * | ||
317 | * @param h handle to the datacache | ||
318 | * @param key what to look up | ||
319 | * @param type entries of which type are relevant? | ||
320 | * @param iter maybe NULL (to just count) | ||
321 | * @param iter_cls closure for @a iter | ||
322 | * @return the number of results found | ||
323 | */ | ||
324 | unsigned int | ||
325 | GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h, | ||
326 | const struct GNUNET_HashCode *key, | ||
327 | enum GNUNET_BLOCK_Type type, | ||
328 | GNUNET_DATACACHE_Iterator iter, | ||
329 | void *iter_cls) | ||
330 | { | ||
331 | GNUNET_STATISTICS_update (h->stats, | ||
332 | gettext_noop ("# requests received"), | ||
333 | 1, | ||
334 | GNUNET_NO); | ||
335 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
336 | "Processing request for key `%s'\n", | ||
337 | GNUNET_h2s (key)); | ||
338 | if ((NULL != h->filter) && | ||
339 | (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key))) | ||
340 | { | ||
341 | GNUNET_STATISTICS_update (h->stats, | ||
342 | gettext_noop ( | ||
343 | "# requests filtered by bloom filter"), | ||
344 | 1, | ||
345 | GNUNET_NO); | ||
346 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
347 | "Bloomfilter filters request for key `%s'\n", | ||
348 | GNUNET_h2s (key)); | ||
349 | return 0; /* can not be present */ | ||
350 | } | ||
351 | return h->api->get (h->api->cls, key, type, iter, iter_cls); | ||
352 | } | ||
353 | |||
354 | |||
355 | /** | ||
356 | * Obtain a random element from the datacache. | ||
357 | * | ||
358 | * @param h handle to the datacache | ||
359 | * @param iter maybe NULL (to just count) | ||
360 | * @param iter_cls closure for @a iter | ||
361 | * @return the number of results found (zero or 1) | ||
362 | */ | ||
363 | unsigned int | ||
364 | GNUNET_DATACACHE_get_random (struct GNUNET_DATACACHE_Handle *h, | ||
365 | GNUNET_DATACACHE_Iterator iter, | ||
366 | void *iter_cls) | ||
367 | { | ||
368 | GNUNET_STATISTICS_update (h->stats, | ||
369 | gettext_noop ( | ||
370 | "# requests for random value received"), | ||
371 | 1, | ||
372 | GNUNET_NO); | ||
373 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing request for random value\n"); | ||
374 | return h->api->get_random (h->api->cls, iter, iter_cls); | ||
375 | } | ||
376 | |||
377 | |||
378 | /** | ||
379 | * Iterate over the results that are "close" to a particular key in | ||
380 | * the datacache. "close" is defined as numerically larger than @a | ||
381 | * key (when interpreted as a circular address space), with small | ||
382 | * distance. | ||
383 | * | ||
384 | * @param h handle to the datacache | ||
385 | * @param key area of the keyspace to look into | ||
386 | * @param num_results number of results that should be returned to @a iter | ||
387 | * @param iter maybe NULL (to just count) | ||
388 | * @param iter_cls closure for @a iter | ||
389 | * @return the number of results found | ||
390 | */ | ||
391 | unsigned int | ||
392 | GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h, | ||
393 | const struct GNUNET_HashCode *key, | ||
394 | unsigned int num_results, | ||
395 | GNUNET_DATACACHE_Iterator iter, | ||
396 | void *iter_cls) | ||
397 | { | ||
398 | GNUNET_STATISTICS_update (h->stats, | ||
399 | gettext_noop ( | ||
400 | "# proximity search requests received"), | ||
401 | 1, | ||
402 | GNUNET_NO); | ||
403 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
404 | "Processing proximity search at `%s'\n", | ||
405 | GNUNET_h2s (key)); | ||
406 | return h->api->get_closest (h->api->cls, key, num_results, iter, iter_cls); | ||
407 | } | ||
408 | |||
409 | |||
410 | /* end of datacache.c */ | ||