aboutsummaryrefslogtreecommitdiff
path: root/src/datacache
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-06-03 21:31:48 +0200
committerChristian Grothoff <christian@grothoff.org>2017-06-03 21:31:48 +0200
commita2ce8dba133e644e304bb41e4205435bebd6145c (patch)
tree2a04397ccc0580a8c24da9fabd1e78a845149eb0 /src/datacache
parent0266efed64df149fd11a73e575be18cf03f36d25 (diff)
downloadgnunet-a2ce8dba133e644e304bb41e4205435bebd6145c.tar.gz
gnunet-a2ce8dba133e644e304bb41e4205435bebd6145c.zip
migrate another function to libgnunetpq
Diffstat (limited to 'src/datacache')
-rw-r--r--src/datacache/plugin_datacache_postgres.c84
1 files changed, 37 insertions, 47 deletions
diff --git a/src/datacache/plugin_datacache_postgres.c b/src/datacache/plugin_datacache_postgres.c
index e69f2b2c6..c5bbb390c 100644
--- a/src/datacache/plugin_datacache_postgres.c
+++ b/src/datacache/plugin_datacache_postgres.c
@@ -405,22 +405,34 @@ postgres_plugin_get_random (void *cls,
405 void *iter_cls) 405 void *iter_cls)
406{ 406{
407 struct Plugin *plugin = cls; 407 struct Plugin *plugin = cls;
408 unsigned int off; 408 uint32_t off;
409 uint32_t off_be;
410 struct GNUNET_TIME_Absolute expiration_time; 409 struct GNUNET_TIME_Absolute expiration_time;
411 uint32_t size; 410 size_t data_size;
412 unsigned int path_len; 411 void *data;
413 const struct GNUNET_PeerIdentity *path; 412 size_t path_len;
414 const struct GNUNET_HashCode *key; 413 struct GNUNET_PeerIdentity *path;
415 unsigned int type; 414 struct GNUNET_HashCode key;
416 PGresult *res; 415 uint32_t type;
417 const char *paramValues[] = { 416 enum GNUNET_PQ_QueryStatus res;
418 (const char *) &off_be 417 struct GNUNET_PQ_QueryParam params[] = {
418 GNUNET_PQ_query_param_uint32 (&off),
419 GNUNET_PQ_query_param_end
419 }; 420 };
420 int paramLengths[] = { 421 struct GNUNET_PQ_ResultSpec rs[] = {
421 sizeof (off_be) 422 GNUNET_PQ_result_spec_absolute_time ("discard_time",
423 &expiration_time),
424 GNUNET_PQ_result_spec_uint32 ("type",
425 &type),
426 GNUNET_PQ_result_spec_variable_size ("value",
427 &data,
428 &data_size),
429 GNUNET_PQ_result_spec_variable_size ("path",
430 (void **) &path,
431 &path_len),
432 GNUNET_PQ_result_spec_auto_from_type ("key",
433 &key),
434 GNUNET_PQ_result_spec_end
422 }; 435 };
423 const int paramFormats[] = { 1 };
424 436
425 if (0 == plugin->num_items) 437 if (0 == plugin->num_items)
426 return 0; 438 return 0;
@@ -428,62 +440,40 @@ postgres_plugin_get_random (void *cls,
428 return 1; 440 return 1;
429 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 441 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
430 plugin->num_items); 442 plugin->num_items);
431 off_be = htonl (off); 443 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
432 res = 444 "get_random",
433 PQexecPrepared (plugin->dbh, "get_random", 445 params,
434 1, paramValues, paramLengths, paramFormats, 446 rs);
435 1); 447 if (0 > res)
436 if (GNUNET_OK !=
437 GNUNET_POSTGRES_check_result (plugin->dbh,
438 res,
439 PGRES_TUPLES_OK,
440 "PQexecPrepared",
441 "get_random"))
442 {
443 GNUNET_break (0);
444 return 0;
445 }
446 if (0 == PQntuples (res))
447 { 448 {
448 GNUNET_break (0); 449 GNUNET_break (0);
449 return 0; 450 return 0;
450 } 451 }
451 if ( (5 != PQnfields (res)) || 452 if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS == res)
452 (sizeof (uint64_t) != PQfsize (res, 0)) ||
453 (sizeof (uint32_t) != PQfsize (res, 1)) ||
454 (sizeof (struct GNUNET_HashCode) != PQfsize (res, 4)) )
455 { 453 {
456 GNUNET_break (0); 454 GNUNET_break (0);
457 PQclear (res);
458 return 0; 455 return 0;
459 } 456 }
460 expiration_time.abs_value_us =
461 GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 0));
462 type = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
463 size = PQgetlength (res, 0, 2);
464 path_len = PQgetlength (res, 0, 3);
465 if (0 != (path_len % sizeof (struct GNUNET_PeerIdentity))) 457 if (0 != (path_len % sizeof (struct GNUNET_PeerIdentity)))
466 { 458 {
467 GNUNET_break (0); 459 GNUNET_break (0);
468 path_len = 0; 460 path_len = 0;
469 } 461 }
470 path_len %= sizeof (struct GNUNET_PeerIdentity); 462 path_len %= sizeof (struct GNUNET_PeerIdentity);
471 path = (const struct GNUNET_PeerIdentity *) PQgetvalue (res, 0, 3);
472 key = (const struct GNUNET_HashCode *) PQgetvalue (res, 0, 4);
473 LOG (GNUNET_ERROR_TYPE_DEBUG, 463 LOG (GNUNET_ERROR_TYPE_DEBUG,
474 "Found random value with key %s of size %u bytes and type %u in database\n", 464 "Found random value with key %s of size %u bytes and type %u in database\n",
475 GNUNET_h2s (key), 465 GNUNET_h2s (&key),
476 (unsigned int) size, 466 (unsigned int) data_size,
477 (unsigned int) type); 467 (unsigned int) type);
478 (void) iter (iter_cls, 468 (void) iter (iter_cls,
479 key, 469 &key,
480 size, 470 data_size,
481 PQgetvalue (res, 0, 2), 471 data,
482 (enum GNUNET_BLOCK_Type) type, 472 (enum GNUNET_BLOCK_Type) type,
483 expiration_time, 473 expiration_time,
484 path_len, 474 path_len,
485 path); 475 path);
486 PQclear (res); 476 GNUNET_PQ_cleanup_result (rs);
487 return 1; 477 return 1;
488} 478}
489 479