aboutsummaryrefslogtreecommitdiff
path: root/src/datacache/plugin_datacache_postgres.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-01-02 22:10:16 +0100
committerChristian Grothoff <christian@grothoff.org>2022-01-02 22:10:16 +0100
commit1b39a393307288188d790c8e89f794a136a2653f (patch)
tree1c50c7c21afb4072758709be239813afe66c1536 /src/datacache/plugin_datacache_postgres.c
parent0eca5d452354979721c52ba8e0e583d063b92d3d (diff)
downloadgnunet-1b39a393307288188d790c8e89f794a136a2653f.tar.gz
gnunet-1b39a393307288188d790c8e89f794a136a2653f.zip
-dce
Diffstat (limited to 'src/datacache/plugin_datacache_postgres.c')
-rw-r--r--src/datacache/plugin_datacache_postgres.c99
1 files changed, 2 insertions, 97 deletions
diff --git a/src/datacache/plugin_datacache_postgres.c b/src/datacache/plugin_datacache_postgres.c
index 6613ae928..070619137 100644
--- a/src/datacache/plugin_datacache_postgres.c
+++ b/src/datacache/plugin_datacache_postgres.c
@@ -67,7 +67,8 @@ static int
67init_connection (struct Plugin *plugin) 67init_connection (struct Plugin *plugin)
68{ 68{
69 struct GNUNET_PQ_ExecuteStatement es[] = { 69 struct GNUNET_PQ_ExecuteStatement es[] = {
70 GNUNET_PQ_make_try_execute ("CREATE TEMPORARY SEQUENCE IF NOT EXISTS gn011dc_oid_seq"), 70 GNUNET_PQ_make_try_execute (
71 "CREATE TEMPORARY SEQUENCE IF NOT EXISTS gn011dc_oid_seq"),
71 GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS gn011dc (" 72 GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS gn011dc ("
72 " oid OID NOT NULL DEFAULT nextval('gn011dc_oid_seq')," 73 " oid OID NOT NULL DEFAULT nextval('gn011dc_oid_seq'),"
73 " type INTEGER NOT NULL," 74 " type INTEGER NOT NULL,"
@@ -107,11 +108,6 @@ init_connection (struct Plugin *plugin)
107 "SELECT length(value) AS len,oid,key FROM gn011dc" 108 "SELECT length(value) AS len,oid,key FROM gn011dc"
108 " ORDER BY prox ASC, discard_time ASC LIMIT 1", 109 " ORDER BY prox ASC, discard_time ASC LIMIT 1",
109 0), 110 0),
110 GNUNET_PQ_make_prepare ("get_random",
111 "SELECT discard_time,type,value,path,key FROM gn011dc"
112 " WHERE discard_time >= $1"
113 " ORDER BY key ASC LIMIT 1 OFFSET $2",
114 2),
115 GNUNET_PQ_make_prepare ("get_closest", 111 GNUNET_PQ_make_prepare ("get_closest",
116 "SELECT discard_time,type,value,path,key FROM gn011dc " 112 "SELECT discard_time,type,value,path,key FROM gn011dc "
117 "WHERE key>=$1 AND discard_time >= $2 ORDER BY key ASC LIMIT $3", 113 "WHERE key>=$1 AND discard_time >= $2 ORDER BY key ASC LIMIT $3",
@@ -410,96 +406,6 @@ postgres_plugin_del (void *cls)
410 406
411 407
412/** 408/**
413 * Obtain a random key-value pair from the datacache.
414 *
415 * @param cls closure (our `struct Plugin`)
416 * @param iter maybe NULL (to just count)
417 * @param iter_cls closure for @a iter
418 * @return the number of results found, zero (datacache empty) or one
419 */
420static unsigned int
421postgres_plugin_get_random (void *cls,
422 GNUNET_DATACACHE_Iterator iter,
423 void *iter_cls)
424{
425 struct Plugin *plugin = cls;
426 uint32_t off;
427 struct GNUNET_TIME_Absolute now = { 0 };
428 struct GNUNET_TIME_Absolute expiration_time;
429 size_t data_size;
430 void *data;
431 size_t path_len;
432 struct GNUNET_PeerIdentity *path;
433 struct GNUNET_HashCode key;
434 uint32_t type;
435 enum GNUNET_DB_QueryStatus res;
436 struct GNUNET_PQ_QueryParam params[] = {
437 GNUNET_PQ_query_param_absolute_time (&now),
438 GNUNET_PQ_query_param_uint32 (&off),
439 GNUNET_PQ_query_param_end
440 };
441 struct GNUNET_PQ_ResultSpec rs[] = {
442 GNUNET_PQ_result_spec_absolute_time ("discard_time",
443 &expiration_time),
444 GNUNET_PQ_result_spec_uint32 ("type",
445 &type),
446 GNUNET_PQ_result_spec_variable_size ("value",
447 &data,
448 &data_size),
449 GNUNET_PQ_result_spec_variable_size ("path",
450 (void **) &path,
451 &path_len),
452 GNUNET_PQ_result_spec_auto_from_type ("key",
453 &key),
454 GNUNET_PQ_result_spec_end
455 };
456
457 if (0 == plugin->num_items)
458 return 0;
459 if (NULL == iter)
460 return 1;
461 now = GNUNET_TIME_absolute_get ();
462 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
463 plugin->num_items);
464 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
465 "get_random",
466 params,
467 rs);
468 if (0 > res)
469 {
470 GNUNET_break (0);
471 return 0;
472 }
473 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res)
474 {
475 GNUNET_break (0);
476 return 0;
477 }
478 if (0 != (path_len % sizeof(struct GNUNET_PeerIdentity)))
479 {
480 GNUNET_break (0);
481 path_len = 0;
482 }
483 path_len %= sizeof(struct GNUNET_PeerIdentity);
484 LOG (GNUNET_ERROR_TYPE_DEBUG,
485 "Found random value with key %s of size %u bytes and type %u in database\n",
486 GNUNET_h2s (&key),
487 (unsigned int) data_size,
488 (unsigned int) type);
489 (void) iter (iter_cls,
490 &key,
491 data_size,
492 data,
493 (enum GNUNET_BLOCK_Type) type,
494 expiration_time,
495 path_len,
496 path);
497 GNUNET_PQ_cleanup_result (rs);
498 return 1;
499}
500
501
502/**
503 * Closure for #extract_result_cb. 409 * Closure for #extract_result_cb.
504 */ 410 */
505struct ExtractResultContext 411struct ExtractResultContext
@@ -681,7 +587,6 @@ libgnunet_plugin_datacache_postgres_init (void *cls)
681 api->get = &postgres_plugin_get; 587 api->get = &postgres_plugin_get;
682 api->put = &postgres_plugin_put; 588 api->put = &postgres_plugin_put;
683 api->del = &postgres_plugin_del; 589 api->del = &postgres_plugin_del;
684 api->get_random = &postgres_plugin_get_random;
685 api->get_closest = &postgres_plugin_get_closest; 590 api->get_closest = &postgres_plugin_get_closest;
686 LOG (GNUNET_ERROR_TYPE_INFO, 591 LOG (GNUNET_ERROR_TYPE_INFO,
687 "Postgres datacache running\n"); 592 "Postgres datacache running\n");