aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/plugin_namestore_postgres.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2018-04-15 20:45:44 +0200
committerChristian Grothoff <christian@grothoff.org>2018-04-15 20:45:44 +0200
commit47a9a3c95de2dfadc431d34c1dc079d35d3d6d18 (patch)
tree3aaf6de1d53a8b2853e03e56e90f725f26184cfa /src/namestore/plugin_namestore_postgres.c
parent284cfac7a521ad275b7536ce1075f62b6e45ea44 (diff)
downloadgnunet-47a9a3c95de2dfadc431d34c1dc079d35d3d6d18.tar.gz
gnunet-47a9a3c95de2dfadc431d34c1dc079d35d3d6d18.zip
add transactions to namestore plugin API
Diffstat (limited to 'src/namestore/plugin_namestore_postgres.c')
-rw-r--r--src/namestore/plugin_namestore_postgres.c79
1 files changed, 78 insertions, 1 deletions
diff --git a/src/namestore/plugin_namestore_postgres.c b/src/namestore/plugin_namestore_postgres.c
index 4828cb190..378a88f51 100644
--- a/src/namestore/plugin_namestore_postgres.c
+++ b/src/namestore/plugin_namestore_postgres.c
@@ -243,7 +243,7 @@ namestore_postgres_store_records (void *cls,
243 data_size, 243 data_size,
244 data); 244 data);
245 if ( (ret < 0) || 245 if ( (ret < 0) ||
246 (data_size != (size_t) ret) ) 246 (data_size != (size_t) ret) )
247 { 247 {
248 GNUNET_break (0); 248 GNUNET_break (0);
249 return GNUNET_SYSERR; 249 return GNUNET_SYSERR;
@@ -518,6 +518,80 @@ database_shutdown (struct Plugin *plugin)
518 518
519 519
520/** 520/**
521 * Start a transaction.
522 *
523 * @param cls closure
524 * @return #GNUNET_OK on success, #GNUNET_NO if transactions are not supported,
525 * #GNUNET_SYSERR on internal errors
526 */
527static int
528namestore_postgres_begin_transaction (void *cls)
529{
530 struct Plugin *plugin = cls;
531 PGresult *result;
532 ExecStatusType ex;
533
534 result = PQexec (plugin->dbh,
535 "START TRANSACTION ISOLATION LEVEL SERIALIZABLE");
536 if (PGRES_COMMAND_OK !=
537 (ex = PQresultStatus (result)))
538 {
539 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
540 "Failed to start transaction (%s): %s\n",
541 PQresStatus (ex),
542 PQerrorMessage (plugin->dbh));
543 GNUNET_break (0);
544 PQclear (result);
545 return GNUNET_SYSERR;
546 }
547 PQclear (result);
548 return GNUNET_OK;
549}
550
551
552/**
553 * Try to commit a transaction.
554 *
555 * @param cls closure
556 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
557 */
558static int
559namestore_postgres_commit_transaction (void *cls)
560{
561 struct Plugin *plugin = cls;
562 PGresult *result;
563 ExecStatusType status;
564 int ret;
565
566 result = PQexec (plugin->dbh,
567 "COMMIT");
568 status = PQresultStatus (result);
569 ret = (PGRES_COMMAND_OK == status) ? GNUNET_OK : GNUNET_SYSERR;
570 PQclear (result);
571 return ret;
572}
573
574
575/**
576 * Rollback a transaction.
577 *
578 * @param cls closure
579 */
580static void
581namestore_postgres_rollback_transaction (void *cls)
582{
583 struct Plugin *plugin = cls;
584 PGresult *result;
585
586 result = PQexec (plugin->dbh,
587 "ROLLBACK");
588 GNUNET_break (PGRES_COMMAND_OK ==
589 PQresultStatus (result));
590 PQclear (result);
591}
592
593
594/**
521 * Entry point for the plugin. 595 * Entry point for the plugin.
522 * 596 *
523 * @param cls the `struct GNUNET_NAMESTORE_PluginEnvironment*` 597 * @param cls the `struct GNUNET_NAMESTORE_PluginEnvironment*`
@@ -545,6 +619,9 @@ libgnunet_plugin_namestore_postgres_init (void *cls)
545 api->iterate_records = &namestore_postgres_iterate_records; 619 api->iterate_records = &namestore_postgres_iterate_records;
546 api->zone_to_name = &namestore_postgres_zone_to_name; 620 api->zone_to_name = &namestore_postgres_zone_to_name;
547 api->lookup_records = &namestore_postgres_lookup_records; 621 api->lookup_records = &namestore_postgres_lookup_records;
622 api->begin_transaction = &namestore_postgres_begin_transaction;
623 api->commit_transaction = &namestore_postgres_commit_transaction;
624 api->rollback_transaction = &namestore_postgres_rollback_transaction;
548 LOG (GNUNET_ERROR_TYPE_INFO, 625 LOG (GNUNET_ERROR_TYPE_INFO,
549 "Postgres namestore plugin running\n"); 626 "Postgres namestore plugin running\n");
550 return api; 627 return api;