diff options
Diffstat (limited to 'src/datacache/datacache.c')
-rw-r--r-- | src/datacache/datacache.c | 126 |
1 files changed, 47 insertions, 79 deletions
diff --git a/src/datacache/datacache.c b/src/datacache/datacache.c index 7dcd54fcf..57e0b0bb7 100644 --- a/src/datacache/datacache.c +++ b/src/datacache/datacache.c | |||
@@ -29,9 +29,10 @@ | |||
29 | #include "gnunet_datacache_plugin.h" | 29 | #include "gnunet_datacache_plugin.h" |
30 | 30 | ||
31 | 31 | ||
32 | #define LOG(kind,...) GNUNET_log_from (kind, "datacache", __VA_ARGS__) | 32 | #define LOG(kind, ...) GNUNET_log_from (kind, "datacache", __VA_ARGS__) |
33 | 33 | ||
34 | #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache", op, fn) | 34 | #define LOG_STRERROR_FILE(kind, op, fn) \ |
35 | GNUNET_log_from_strerror_file (kind, "datacache", op, fn) | ||
35 | 36 | ||
36 | /** | 37 | /** |
37 | * Internal state of the datacache library. | 38 | * Internal state of the datacache library. |
@@ -89,7 +90,6 @@ struct GNUNET_DATACACHE_Handle | |||
89 | * How much space is in use right now? | 90 | * How much space is in use right now? |
90 | */ | 91 | */ |
91 | unsigned long long utilization; | 92 | unsigned long long utilization; |
92 | |||
93 | }; | 93 | }; |
94 | 94 | ||
95 | 95 | ||
@@ -102,9 +102,7 @@ struct GNUNET_DATACACHE_Handle | |||
102 | * @param size number of bytes that were made available | 102 | * @param size number of bytes that were made available |
103 | */ | 103 | */ |
104 | static void | 104 | static void |
105 | env_delete_notify (void *cls, | 105 | env_delete_notify (void *cls, const struct GNUNET_HashCode *key, size_t size) |
106 | const struct GNUNET_HashCode *key, | ||
107 | size_t size) | ||
108 | { | 106 | { |
109 | struct GNUNET_DATACACHE_Handle *h = cls; | 107 | struct GNUNET_DATACACHE_Handle *h = cls; |
110 | 108 | ||
@@ -113,11 +111,10 @@ env_delete_notify (void *cls, | |||
113 | GNUNET_h2s (key)); | 111 | GNUNET_h2s (key)); |
114 | GNUNET_assert (h->utilization >= size); | 112 | GNUNET_assert (h->utilization >= size); |
115 | h->utilization -= size; | 113 | h->utilization -= size; |
116 | GNUNET_CONTAINER_bloomfilter_remove (h->filter, | 114 | GNUNET_CONTAINER_bloomfilter_remove (h->filter, key); |
117 | key); | ||
118 | GNUNET_STATISTICS_update (h->stats, | 115 | GNUNET_STATISTICS_update (h->stats, |
119 | gettext_noop ("# bytes stored"), | 116 | gettext_noop ("# bytes stored"), |
120 | - (long long) size, | 117 | -(long long) size, |
121 | GNUNET_NO); | 118 | GNUNET_NO); |
122 | GNUNET_STATISTICS_update (h->stats, | 119 | GNUNET_STATISTICS_update (h->stats, |
123 | gettext_noop ("# items stored"), | 120 | gettext_noop ("# items stored"), |
@@ -144,54 +141,42 @@ GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
144 | char *name; | 141 | char *name; |
145 | 142 | ||
146 | if (GNUNET_OK != | 143 | if (GNUNET_OK != |
147 | GNUNET_CONFIGURATION_get_value_size (cfg, | 144 | GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", "a)) |
148 | section, | ||
149 | "QUOTA", | ||
150 | "a)) | ||
151 | { | 145 | { |
152 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | 146 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "QUOTA"); |
153 | section, | ||
154 | "QUOTA"); | ||
155 | return NULL; | 147 | return NULL; |
156 | } | 148 | } |
157 | if (GNUNET_OK != | 149 | if (GNUNET_OK != |
158 | GNUNET_CONFIGURATION_get_value_string (cfg, | 150 | GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name)) |
159 | section, | ||
160 | "DATABASE", | ||
161 | &name)) | ||
162 | { | 151 | { |
163 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | 152 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "DATABASE"); |
164 | section, | ||
165 | "DATABASE"); | ||
166 | return NULL; | 153 | return NULL; |
167 | } | 154 | } |
168 | bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */ | 155 | bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */ |
169 | 156 | ||
170 | ret = GNUNET_new (struct GNUNET_DATACACHE_Handle); | 157 | ret = GNUNET_new (struct GNUNET_DATACACHE_Handle); |
171 | 158 | ||
172 | if (GNUNET_YES != | 159 | if (GNUNET_YES != |
173 | GNUNET_CONFIGURATION_get_value_yesno (cfg, | 160 | GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF")) |
174 | section, | ||
175 | "DISABLE_BF")) | ||
176 | { | 161 | { |
177 | if (GNUNET_YES != | 162 | if (GNUNET_YES != |
178 | GNUNET_CONFIGURATION_get_value_yesno (cfg, | 163 | GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF_RC")) |
179 | section, | ||
180 | "DISABLE_BF_RC")) | ||
181 | { | 164 | { |
182 | ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom"); | 165 | ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom"); |
183 | } | 166 | } |
184 | if (NULL != ret->bloom_name) | 167 | if (NULL != ret->bloom_name) |
185 | { | 168 | { |
186 | ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name, | 169 | ret->filter = GNUNET_CONTAINER_bloomfilter_load ( |
187 | quota / 1024, /* 8 bit per entry in DB, expect 1k entries */ | 170 | ret->bloom_name, |
188 | 5); | 171 | quota / 1024, /* 8 bit per entry in DB, expect 1k entries */ |
172 | 5); | ||
189 | } | 173 | } |
190 | if (NULL == ret->filter) | 174 | if (NULL == ret->filter) |
191 | { | 175 | { |
192 | ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, | 176 | ret->filter = |
193 | bf_size, | 177 | GNUNET_CONTAINER_bloomfilter_init (NULL, |
194 | 5); /* approx. 3% false positives at max use */ | 178 | bf_size, |
179 | 5); /* approx. 3% false positives at max use */ | ||
195 | } | 180 | } |
196 | } | 181 | } |
197 | ret->stats = GNUNET_STATISTICS_create ("datacache", cfg); | 182 | ret->stats = GNUNET_STATISTICS_create ("datacache", cfg); |
@@ -202,19 +187,15 @@ GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
202 | ret->env.cls = ret; | 187 | ret->env.cls = ret; |
203 | ret->env.delete_notify = &env_delete_notify; | 188 | ret->env.delete_notify = &env_delete_notify; |
204 | ret->env.quota = quota; | 189 | ret->env.quota = quota; |
205 | LOG (GNUNET_ERROR_TYPE_INFO, | 190 | LOG (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' datacache plugin\n"), name); |
206 | _("Loading `%s' datacache plugin\n"), | 191 | GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name); |
207 | name); | ||
208 | GNUNET_asprintf (&libname, | ||
209 | "libgnunet_plugin_datacache_%s", | ||
210 | name); | ||
211 | ret->short_name = name; | 192 | ret->short_name = name; |
212 | ret->lib_name = libname; | 193 | ret->lib_name = libname; |
213 | ret->api = GNUNET_PLUGIN_load (libname, &ret->env); | 194 | ret->api = GNUNET_PLUGIN_load (libname, &ret->env); |
214 | if (ret->api == NULL) | 195 | if (ret->api == NULL) |
215 | { | 196 | { |
216 | LOG (GNUNET_ERROR_TYPE_ERROR, | 197 | LOG (GNUNET_ERROR_TYPE_ERROR, |
217 | _("Failed to load datacache plugin for `%s'\n"), | 198 | _ ("Failed to load datacache plugin for `%s'\n"), |
218 | name); | 199 | name); |
219 | GNUNET_DATACACHE_destroy (ret); | 200 | GNUNET_DATACACHE_destroy (ret); |
220 | return NULL; | 201 | return NULL; |
@@ -234,23 +215,20 @@ GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h) | |||
234 | if (NULL != h->filter) | 215 | if (NULL != h->filter) |
235 | GNUNET_CONTAINER_bloomfilter_free (h->filter); | 216 | GNUNET_CONTAINER_bloomfilter_free (h->filter); |
236 | if (NULL != h->api) | 217 | if (NULL != h->api) |
237 | GNUNET_break (NULL == | 218 | GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api)); |
238 | GNUNET_PLUGIN_unload (h->lib_name, | ||
239 | h->api)); | ||
240 | GNUNET_free (h->lib_name); | 219 | GNUNET_free (h->lib_name); |
241 | GNUNET_free (h->short_name); | 220 | GNUNET_free (h->short_name); |
242 | GNUNET_free (h->section); | 221 | GNUNET_free (h->section); |
243 | if (NULL != h->bloom_name) | 222 | if (NULL != h->bloom_name) |
244 | { | 223 | { |
245 | if (0 != UNLINK (h->bloom_name)) | 224 | if (0 != unlink (h->bloom_name)) |
246 | GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING, | 225 | GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING, |
247 | "datacache", | 226 | "datacache", |
248 | "unlink", | 227 | "unlink", |
249 | h->bloom_name); | 228 | h->bloom_name); |
250 | GNUNET_free (h->bloom_name); | 229 | GNUNET_free (h->bloom_name); |
251 | } | 230 | } |
252 | GNUNET_STATISTICS_destroy (h->stats, | 231 | GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO); |
253 | GNUNET_NO); | ||
254 | GNUNET_free (h); | 232 | GNUNET_free (h); |
255 | } | 233 | } |
256 | 234 | ||
@@ -277,19 +255,19 @@ GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h, | |||
277 | const char *data, | 255 | const char *data, |
278 | enum GNUNET_BLOCK_Type type, | 256 | enum GNUNET_BLOCK_Type type, |
279 | struct GNUNET_TIME_Absolute discard_time, | 257 | struct GNUNET_TIME_Absolute discard_time, |
280 | unsigned int path_info_len, | 258 | unsigned int path_info_len, |
281 | const struct GNUNET_PeerIdentity *path_info) | 259 | const struct GNUNET_PeerIdentity *path_info) |
282 | { | 260 | { |
283 | ssize_t used; | 261 | ssize_t used; |
284 | 262 | ||
285 | used = h->api->put (h->api->cls, | 263 | used = h->api->put (h->api->cls, |
286 | key, | 264 | key, |
287 | xor_distance, | 265 | xor_distance, |
288 | data_size, | 266 | data_size, |
289 | data, | 267 | data, |
290 | type, | 268 | type, |
291 | discard_time, | 269 | discard_time, |
292 | path_info_len, | 270 | path_info_len, |
293 | path_info); | 271 | path_info); |
294 | if (-1 == used) | 272 | if (-1 == used) |
295 | { | 273 | { |
@@ -305,8 +283,7 @@ GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h, | |||
305 | "Stored data under key `%s' in cache\n", | 283 | "Stored data under key `%s' in cache\n", |
306 | GNUNET_h2s (key)); | 284 | GNUNET_h2s (key)); |
307 | if (NULL != h->filter) | 285 | if (NULL != h->filter) |
308 | GNUNET_CONTAINER_bloomfilter_add (h->filter, | 286 | GNUNET_CONTAINER_bloomfilter_add (h->filter, key); |
309 | key); | ||
310 | GNUNET_STATISTICS_update (h->stats, | 287 | GNUNET_STATISTICS_update (h->stats, |
311 | gettext_noop ("# bytes stored"), | 288 | gettext_noop ("# bytes stored"), |
312 | used, | 289 | used, |
@@ -316,8 +293,7 @@ GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h, | |||
316 | 1, | 293 | 1, |
317 | GNUNET_NO); | 294 | GNUNET_NO); |
318 | while (h->utilization + used > h->env.quota) | 295 | while (h->utilization + used > h->env.quota) |
319 | GNUNET_assert (GNUNET_OK == | 296 | GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls)); |
320 | h->api->del (h->api->cls)); | ||
321 | h->utilization += used; | 297 | h->utilization += used; |
322 | return GNUNET_OK; | 298 | return GNUNET_OK; |
323 | } | 299 | } |
@@ -348,23 +324,20 @@ GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h, | |||
348 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 324 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
349 | "Processing request for key `%s'\n", | 325 | "Processing request for key `%s'\n", |
350 | GNUNET_h2s (key)); | 326 | GNUNET_h2s (key)); |
351 | if ( (NULL != h->filter) && | 327 | if ((NULL != h->filter) && |
352 | (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key)) ) | 328 | (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key))) |
353 | { | 329 | { |
354 | GNUNET_STATISTICS_update (h->stats, | 330 | GNUNET_STATISTICS_update (h->stats, |
355 | gettext_noop ("# requests filtered by bloom filter"), | 331 | gettext_noop ( |
332 | "# requests filtered by bloom filter"), | ||
356 | 1, | 333 | 1, |
357 | GNUNET_NO); | 334 | GNUNET_NO); |
358 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 335 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
359 | "Bloomfilter filters request for key `%s'\n", | 336 | "Bloomfilter filters request for key `%s'\n", |
360 | GNUNET_h2s (key)); | 337 | GNUNET_h2s (key)); |
361 | return 0; /* can not be present */ | 338 | return 0; /* can not be present */ |
362 | } | 339 | } |
363 | return h->api->get (h->api->cls, | 340 | return h->api->get (h->api->cls, key, type, iter, iter_cls); |
364 | key, | ||
365 | type, | ||
366 | iter, | ||
367 | iter_cls); | ||
368 | } | 341 | } |
369 | 342 | ||
370 | 343 | ||
@@ -382,14 +355,12 @@ GNUNET_DATACACHE_get_random (struct GNUNET_DATACACHE_Handle *h, | |||
382 | void *iter_cls) | 355 | void *iter_cls) |
383 | { | 356 | { |
384 | GNUNET_STATISTICS_update (h->stats, | 357 | GNUNET_STATISTICS_update (h->stats, |
385 | gettext_noop ("# requests for random value received"), | 358 | gettext_noop ( |
359 | "# requests for random value received"), | ||
386 | 1, | 360 | 1, |
387 | GNUNET_NO); | 361 | GNUNET_NO); |
388 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 362 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing request for random value\n"); |
389 | "Processing request for random value\n"); | 363 | return h->api->get_random (h->api->cls, iter, iter_cls); |
390 | return h->api->get_random (h->api->cls, | ||
391 | iter, | ||
392 | iter_cls); | ||
393 | } | 364 | } |
394 | 365 | ||
395 | 366 | ||
@@ -414,17 +385,14 @@ GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h, | |||
414 | void *iter_cls) | 385 | void *iter_cls) |
415 | { | 386 | { |
416 | GNUNET_STATISTICS_update (h->stats, | 387 | GNUNET_STATISTICS_update (h->stats, |
417 | gettext_noop ("# proximity search requests received"), | 388 | gettext_noop ( |
389 | "# proximity search requests received"), | ||
418 | 1, | 390 | 1, |
419 | GNUNET_NO); | 391 | GNUNET_NO); |
420 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 392 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
421 | "Processing proximity search at `%s'\n", | 393 | "Processing proximity search at `%s'\n", |
422 | GNUNET_h2s (key)); | 394 | GNUNET_h2s (key)); |
423 | return h->api->get_closest (h->api->cls, | 395 | return h->api->get_closest (h->api->cls, key, num_results, iter, iter_cls); |
424 | key, | ||
425 | num_results, | ||
426 | iter, | ||
427 | iter_cls); | ||
428 | } | 396 | } |
429 | 397 | ||
430 | 398 | ||