aboutsummaryrefslogtreecommitdiff
path: root/src/datacache
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-04-29 08:17:17 +0000
committerChristian Grothoff <christian@grothoff.org>2015-04-29 08:17:17 +0000
commit11a321ac1c47f39d44fa58149501a7c12d62e379 (patch)
tree534c4373b9ade3f6c3c12afd33ea83f7f8ea28e3 /src/datacache
parentf4d2a974968ab6a3e83815a68dc6b98588105277 (diff)
downloadgnunet-11a321ac1c47f39d44fa58149501a7c12d62e379.tar.gz
gnunet-11a321ac1c47f39d44fa58149501a7c12d62e379.zip
implementing postgres version of get_random
Diffstat (limited to 'src/datacache')
-rw-r--r--src/datacache/plugin_datacache_postgres.c105
1 files changed, 104 insertions, 1 deletions
diff --git a/src/datacache/plugin_datacache_postgres.c b/src/datacache/plugin_datacache_postgres.c
index 836a24251..13f944d7c 100644
--- a/src/datacache/plugin_datacache_postgres.c
+++ b/src/datacache/plugin_datacache_postgres.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2006, 2009, 2010, 2012 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2006, 2009, 2010, 2012, 2015 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -158,6 +158,11 @@ init_connection (struct Plugin *plugin)
158 "ORDER BY discard_time ASC LIMIT 1", 0)) || 158 "ORDER BY discard_time ASC LIMIT 1", 0)) ||
159 (GNUNET_OK != 159 (GNUNET_OK !=
160 GNUNET_POSTGRES_prepare (plugin->dbh, 160 GNUNET_POSTGRES_prepare (plugin->dbh,
161 "get_random",
162 "SELECT discard_time,type,value,path,key FROM gn090dc "
163 "ORDER BY key ASC LIMIT 1 OFFSET $1", 1)) ||
164 (GNUNET_OK !=
165 GNUNET_POSTGRES_prepare (plugin->dbh,
161 "delrow", 166 "delrow",
162 "DELETE FROM gn090dc WHERE oid=$1", 1)) || 167 "DELETE FROM gn090dc WHERE oid=$1", 1)) ||
163 (GNUNET_OK != 168 (GNUNET_OK !=
@@ -405,6 +410,103 @@ postgres_plugin_del (void *cls)
405 410
406 411
407/** 412/**
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 unsigned int off;
427 uint32_t off_be;
428 struct GNUNET_TIME_Absolute expiration_time;
429 uint32_t size;
430 unsigned int path_len;
431 const struct GNUNET_PeerIdentity *path;
432 const struct GNUNET_HashCode *key;
433 unsigned int type;
434 PGresult *res;
435 const char *paramValues[] = {
436 (const char *) &off_be,
437 };
438 int paramLengths[] = {
439 sizeof (off_be),
440 };
441 const int paramFormats[] = { 1 };
442
443 if (0 == plugin->num_items)
444 return 0;
445 if (NULL == iter)
446 return 1;
447 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
448 plugin->num_items);
449 off_be = htonl (off);
450 res =
451 PQexecPrepared (plugin->dbh, "get_random",
452 1, paramValues, paramLengths, paramFormats,
453 1);
454 if (GNUNET_OK !=
455 GNUNET_POSTGRES_check_result (plugin->dbh,
456 res,
457 PGRES_TUPLES_OK,
458 "PQexecPrepared",
459 "get_random"))
460 {
461 GNUNET_break (0);
462 return 0;
463 }
464 if (0 == PQntuples (res))
465 {
466 GNUNET_break (0);
467 return 0;
468 }
469 if ( (5 != PQnfields (res)) ||
470 (sizeof (uint64_t) != PQfsize (res, 0)) ||
471 (sizeof (uint32_t) != PQfsize (res, 1)) ||
472 (sizeof (struct GNUNET_HashCode) != PQfsize (res, 4)) )
473 {
474 GNUNET_break (0);
475 PQclear (res);
476 return 0;
477 }
478 expiration_time.abs_value_us =
479 GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 0));
480 type = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
481 size = PQgetlength (res, 0, 2);
482 path_len = PQgetlength (res, 0, 3);
483 if (0 != (path_len % sizeof (struct GNUNET_PeerIdentity)))
484 {
485 GNUNET_break (0);
486 path_len = 0;
487 }
488 path_len %= sizeof (struct GNUNET_PeerIdentity);
489 path = (const struct GNUNET_PeerIdentity *) PQgetvalue (res, 0, 3);
490 key = (const struct GNUNET_HashCode *) PQgetvalue (res, 0, 4);
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 "Found random value with key %s of size %u bytes and type %u in database\n",
493 GNUNET_h2s (key),
494 (unsigned int) size,
495 (unsigned int) type);
496 (void) iter (iter_cls,
497 key,
498 size,
499 PQgetvalue (res, 0, 2),
500 (enum GNUNET_BLOCK_Type) type,
501 expiration_time,
502 path_len,
503 path);
504 PQclear (res);
505 return 1;
506}
507
508
509/**
408 * Entry point for the plugin. 510 * Entry point for the plugin.
409 * 511 *
410 * @param cls closure (the `struct GNUNET_DATACACHE_PluginEnvironmnet`) 512 * @param cls closure (the `struct GNUNET_DATACACHE_PluginEnvironmnet`)
@@ -431,6 +533,7 @@ libgnunet_plugin_datacache_postgres_init (void *cls)
431 api->get = &postgres_plugin_get; 533 api->get = &postgres_plugin_get;
432 api->put = &postgres_plugin_put; 534 api->put = &postgres_plugin_put;
433 api->del = &postgres_plugin_del; 535 api->del = &postgres_plugin_del;
536 api->get_random = &postgres_plugin_get_random;
434 LOG (GNUNET_ERROR_TYPE_INFO, 537 LOG (GNUNET_ERROR_TYPE_INFO,
435 "Postgres datacache running\n"); 538 "Postgres datacache running\n");
436 return api; 539 return api;