diff options
-rw-r--r-- | src/datacache/plugin_datacache_sqlite.c | 94 | ||||
-rw-r--r-- | src/datacache/plugin_datacache_template.c | 6 |
2 files changed, 95 insertions, 5 deletions
diff --git a/src/datacache/plugin_datacache_sqlite.c b/src/datacache/plugin_datacache_sqlite.c index fe6daf4dd..1e313c686 100644 --- a/src/datacache/plugin_datacache_sqlite.c +++ b/src/datacache/plugin_datacache_sqlite.c | |||
@@ -412,9 +412,97 @@ sqlite_plugin_del (void *cls) | |||
412 | 412 | ||
413 | 413 | ||
414 | /** | 414 | /** |
415 | * Obtain a random key-value pair from the datacache. | ||
416 | * | ||
417 | * @param cls closure (our `struct Plugin`) | ||
418 | * @param iter maybe NULL (to just count) | ||
419 | * @param iter_cls closure for @a iter | ||
420 | * @return the number of results found, zero (datacache empty) or one | ||
421 | */ | ||
422 | static unsigned int | ||
423 | sqlite_plugin_get_random (void *cls, | ||
424 | GNUNET_DATACACHE_Iterator iter, | ||
425 | void *iter_cls) | ||
426 | { | ||
427 | struct Plugin *plugin = cls; | ||
428 | sqlite3_stmt *stmt; | ||
429 | struct GNUNET_TIME_Absolute exp; | ||
430 | unsigned int size; | ||
431 | const char *dat; | ||
432 | unsigned int off; | ||
433 | unsigned int psize; | ||
434 | unsigned int type; | ||
435 | char scratch[256]; | ||
436 | int64_t ntime; | ||
437 | const struct GNUNET_PeerIdentity *path; | ||
438 | const struct GNUNET_HashCode *key; | ||
439 | |||
440 | if (0 == plugin->num_items) | ||
441 | return 0; | ||
442 | off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, | ||
443 | plugin->num_items); | ||
444 | GNUNET_snprintf (scratch, | ||
445 | sizeof (scratch), | ||
446 | "SELECT value,expire,path,key,type FROM ds090 ORDER BY key LIMIT 1 OFFSET %u", | ||
447 | off); | ||
448 | if (SQLITE_OK != | ||
449 | sq_prepare (plugin->dbh, scratch, &stmt)) | ||
450 | { | ||
451 | LOG_SQLITE (plugin->dbh, | ||
452 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
453 | "sq_prepare"); | ||
454 | return 0; | ||
455 | } | ||
456 | if (SQLITE_ROW != sqlite3_step (stmt)) | ||
457 | { | ||
458 | GNUNET_break (0); | ||
459 | sqlite3_finalize (stmt); | ||
460 | return 0; | ||
461 | } | ||
462 | size = sqlite3_column_bytes (stmt, 0); | ||
463 | dat = sqlite3_column_blob (stmt, 0); | ||
464 | exp.abs_value_us = sqlite3_column_int64 (stmt, 1); | ||
465 | psize = sqlite3_column_bytes (stmt, 2); | ||
466 | if (0 != psize % sizeof (struct GNUNET_PeerIdentity)) | ||
467 | { | ||
468 | GNUNET_break (0); | ||
469 | psize = 0; | ||
470 | } | ||
471 | psize /= sizeof (struct GNUNET_PeerIdentity); | ||
472 | if (0 != psize) | ||
473 | path = sqlite3_column_blob (stmt, 2); | ||
474 | else | ||
475 | path = NULL; | ||
476 | |||
477 | GNUNET_assert (sizeof (struct GNUNET_HashCode) == | ||
478 | sqlite3_column_bytes (stmt, 3)); | ||
479 | key = sqlite3_column_blob (stmt, 3); | ||
480 | type = sqlite3_column_int (stmt, 4); | ||
481 | |||
482 | ntime = (int64_t) exp.abs_value_us; | ||
483 | if (ntime == INT64_MAX) | ||
484 | exp = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
485 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
486 | "Found %u-byte result with key %s when processing GET-RANDOM\n", | ||
487 | (unsigned int) size, | ||
488 | GNUNET_h2s (key)); | ||
489 | (void) iter (iter_cls, | ||
490 | key, | ||
491 | size, | ||
492 | dat, | ||
493 | type, | ||
494 | exp, | ||
495 | psize, | ||
496 | path); | ||
497 | sqlite3_finalize (stmt); | ||
498 | return 1; | ||
499 | } | ||
500 | |||
501 | |||
502 | /** | ||
415 | * Entry point for the plugin. | 503 | * Entry point for the plugin. |
416 | * | 504 | * |
417 | * @param cls closure (the `struct GNUNET_DATACACHE_PluginEnvironmnet`) | 505 | * @param cls closure (the `struct GNUNET_DATACACHE_PluginEnvironment`) |
418 | * @return the plugin's closure (our `struct Plugin`) | 506 | * @return the plugin's closure (our `struct Plugin`) |
419 | */ | 507 | */ |
420 | void * | 508 | void * |
@@ -484,7 +572,9 @@ libgnunet_plugin_datacache_sqlite_init (void *cls) | |||
484 | api->get = &sqlite_plugin_get; | 572 | api->get = &sqlite_plugin_get; |
485 | api->put = &sqlite_plugin_put; | 573 | api->put = &sqlite_plugin_put; |
486 | api->del = &sqlite_plugin_del; | 574 | api->del = &sqlite_plugin_del; |
487 | LOG (GNUNET_ERROR_TYPE_INFO, _("Sqlite datacache running\n")); | 575 | api->get_random = &sqlite_plugin_get_random; |
576 | LOG (GNUNET_ERROR_TYPE_INFO, | ||
577 | "Sqlite datacache running\n"); | ||
488 | return api; | 578 | return api; |
489 | } | 579 | } |
490 | 580 | ||
diff --git a/src/datacache/plugin_datacache_template.c b/src/datacache/plugin_datacache_template.c index 86e0675c1..16d018995 100644 --- a/src/datacache/plugin_datacache_template.c +++ b/src/datacache/plugin_datacache_template.c | |||
@@ -115,9 +115,9 @@ template_plugin_del (void *cls) | |||
115 | * @return the number of results found (zero or one) | 115 | * @return the number of results found (zero or one) |
116 | */ | 116 | */ |
117 | static unsigned int | 117 | static unsigned int |
118 | template_get_random (void *cls, | 118 | template_plugin_get_random (void *cls, |
119 | GNUNET_DATACACHE_Iterator iter, | 119 | GNUNET_DATACACHE_Iterator iter, |
120 | void *iter_cls) | 120 | void *iter_cls) |
121 | { | 121 | { |
122 | GNUNET_break (0); | 122 | GNUNET_break (0); |
123 | return 0; | 123 | return 0; |