diff options
author | Christian Grothoff <christian@grothoff.org> | 2018-04-15 20:45:44 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2018-04-15 20:45:44 +0200 |
commit | 47a9a3c95de2dfadc431d34c1dc079d35d3d6d18 (patch) | |
tree | 3aaf6de1d53a8b2853e03e56e90f725f26184cfa /src/namestore | |
parent | 284cfac7a521ad275b7536ce1075f62b6e45ea44 (diff) | |
download | gnunet-47a9a3c95de2dfadc431d34c1dc079d35d3d6d18.tar.gz gnunet-47a9a3c95de2dfadc431d34c1dc079d35d3d6d18.zip |
add transactions to namestore plugin API
Diffstat (limited to 'src/namestore')
-rw-r--r-- | src/namestore/plugin_namestore_flat.c | 93 | ||||
-rw-r--r-- | src/namestore/plugin_namestore_postgres.c | 79 | ||||
-rw-r--r-- | src/namestore/plugin_namestore_sqlite.c | 44 |
3 files changed, 190 insertions, 26 deletions
diff --git a/src/namestore/plugin_namestore_flat.c b/src/namestore/plugin_namestore_flat.c index 024fc34f2..170adb49e 100644 --- a/src/namestore/plugin_namestore_flat.c +++ b/src/namestore/plugin_namestore_flat.c | |||
@@ -383,7 +383,7 @@ static void | |||
383 | database_shutdown (struct Plugin *plugin) | 383 | database_shutdown (struct Plugin *plugin) |
384 | { | 384 | { |
385 | struct GNUNET_DISK_FileHandle *fh; | 385 | struct GNUNET_DISK_FileHandle *fh; |
386 | 386 | ||
387 | fh = GNUNET_DISK_file_open (plugin->fn, | 387 | fh = GNUNET_DISK_file_open (plugin->fn, |
388 | GNUNET_DISK_OPEN_CREATE | | 388 | GNUNET_DISK_OPEN_CREATE | |
389 | GNUNET_DISK_OPEN_TRUNCATE | | 389 | GNUNET_DISK_OPEN_TRUNCATE | |
@@ -418,11 +418,11 @@ database_shutdown (struct Plugin *plugin) | |||
418 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR | 418 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR |
419 | */ | 419 | */ |
420 | static int | 420 | static int |
421 | namestore_store_records (void *cls, | 421 | namestore_flat_store_records (void *cls, |
422 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key, | 422 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key, |
423 | const char *label, | 423 | const char *label, |
424 | unsigned int rd_count, | 424 | unsigned int rd_count, |
425 | const struct GNUNET_GNSRECORD_Data *rd) | 425 | const struct GNUNET_GNSRECORD_Data *rd) |
426 | { | 426 | { |
427 | struct Plugin *plugin = cls; | 427 | struct Plugin *plugin = cls; |
428 | uint64_t rvalue; | 428 | uint64_t rvalue; |
@@ -488,11 +488,11 @@ namestore_store_records (void *cls, | |||
488 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR | 488 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR |
489 | */ | 489 | */ |
490 | static int | 490 | static int |
491 | namestore_lookup_records (void *cls, | 491 | namestore_flat_lookup_records (void *cls, |
492 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, | 492 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, |
493 | const char *label, | 493 | const char *label, |
494 | GNUNET_NAMESTORE_RecordIterator iter, | 494 | GNUNET_NAMESTORE_RecordIterator iter, |
495 | void *iter_cls) | 495 | void *iter_cls) |
496 | { | 496 | { |
497 | struct Plugin *plugin = cls; | 497 | struct Plugin *plugin = cls; |
498 | struct FlatFileEntry *entry; | 498 | struct FlatFileEntry *entry; |
@@ -571,11 +571,11 @@ iterate_zones (void *cls, | |||
571 | * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error | 571 | * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error |
572 | */ | 572 | */ |
573 | static int | 573 | static int |
574 | namestore_iterate_records (void *cls, | 574 | namestore_flat_iterate_records (void *cls, |
575 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, | 575 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, |
576 | uint64_t offset, | 576 | uint64_t offset, |
577 | GNUNET_NAMESTORE_RecordIterator iter, | 577 | GNUNET_NAMESTORE_RecordIterator iter, |
578 | void *iter_cls) | 578 | void *iter_cls) |
579 | { | 579 | { |
580 | struct Plugin *plugin = cls; | 580 | struct Plugin *plugin = cls; |
581 | 581 | ||
@@ -641,11 +641,11 @@ zone_to_name (void *cls, | |||
641 | * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error | 641 | * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error |
642 | */ | 642 | */ |
643 | static int | 643 | static int |
644 | namestore_zone_to_name (void *cls, | 644 | namestore_flat_zone_to_name (void *cls, |
645 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, | 645 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, |
646 | const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone, | 646 | const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone, |
647 | GNUNET_NAMESTORE_RecordIterator iter, | 647 | GNUNET_NAMESTORE_RecordIterator iter, |
648 | void *iter_cls) | 648 | void *iter_cls) |
649 | { | 649 | { |
650 | struct Plugin *plugin = cls; | 650 | struct Plugin *plugin = cls; |
651 | 651 | ||
@@ -667,6 +667,46 @@ namestore_zone_to_name (void *cls, | |||
667 | 667 | ||
668 | 668 | ||
669 | /** | 669 | /** |
670 | * Start a transaction. | ||
671 | * | ||
672 | * @param cls closure | ||
673 | * @return #GNUNET_OK on success, #GNUNET_NO if transactions are not supported, | ||
674 | * #GNUNET_SYSERR on internal errors | ||
675 | */ | ||
676 | static int | ||
677 | namestore_flat_begin_transaction (void *cls) | ||
678 | { | ||
679 | return GNUNET_NO; | ||
680 | } | ||
681 | |||
682 | |||
683 | /** | ||
684 | * Try to commit a transaction. | ||
685 | * | ||
686 | * @param cls closure | ||
687 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | ||
688 | */ | ||
689 | static int | ||
690 | namestore_flat_commit_transaction (void *cls) | ||
691 | { | ||
692 | GNUNET_break (0); | ||
693 | return GNUNET_SYSERR; | ||
694 | } | ||
695 | |||
696 | |||
697 | /** | ||
698 | * Rollback a transaction. | ||
699 | * | ||
700 | * @param cls closure | ||
701 | */ | ||
702 | static void | ||
703 | namestore_flat_rollback_transaction (void *cls) | ||
704 | { | ||
705 | GNUNET_break (0); | ||
706 | } | ||
707 | |||
708 | |||
709 | /** | ||
670 | * Entry point for the plugin. | 710 | * Entry point for the plugin. |
671 | * | 711 | * |
672 | * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*" | 712 | * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*" |
@@ -692,10 +732,13 @@ libgnunet_plugin_namestore_flat_init (void *cls) | |||
692 | } | 732 | } |
693 | api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions); | 733 | api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions); |
694 | api->cls = &plugin; | 734 | api->cls = &plugin; |
695 | api->store_records = &namestore_store_records; | 735 | api->store_records = &namestore_flat_store_records; |
696 | api->iterate_records = &namestore_iterate_records; | 736 | api->iterate_records = &namestore_flat_iterate_records; |
697 | api->zone_to_name = &namestore_zone_to_name; | 737 | api->zone_to_name = &namestore_flat_zone_to_name; |
698 | api->lookup_records = &namestore_lookup_records; | 738 | api->lookup_records = &namestore_flat_lookup_records; |
739 | api->begin_transaction = &namestore_flat_begin_transaction; | ||
740 | api->commit_transaction = &namestore_flat_commit_transaction; | ||
741 | api->rollback_transaction = &namestore_flat_rollback_transaction; | ||
699 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 742 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
700 | _("flat file database running\n")); | 743 | _("flat file database running\n")); |
701 | return api; | 744 | return api; |
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 | */ | ||
527 | static int | ||
528 | namestore_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 | */ | ||
558 | static int | ||
559 | namestore_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 | */ | ||
580 | static void | ||
581 | namestore_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; |
diff --git a/src/namestore/plugin_namestore_sqlite.c b/src/namestore/plugin_namestore_sqlite.c index 5ad84688c..1ebb6bfc7 100644 --- a/src/namestore/plugin_namestore_sqlite.c +++ b/src/namestore/plugin_namestore_sqlite.c | |||
@@ -773,6 +773,47 @@ namestore_sqlite_zone_to_name (void *cls, | |||
773 | 773 | ||
774 | 774 | ||
775 | /** | 775 | /** |
776 | * Start a transaction. | ||
777 | * | ||
778 | * @param cls closure | ||
779 | * @return #GNUNET_OK on success, #GNUNET_NO if transactions are not supported, | ||
780 | * #GNUNET_SYSERR on internal errors | ||
781 | */ | ||
782 | static int | ||
783 | namestore_sqlite_begin_transaction (void *cls) | ||
784 | { | ||
785 | return GNUNET_NO; | ||
786 | } | ||
787 | |||
788 | |||
789 | /** | ||
790 | * Try to commit a transaction. | ||
791 | * | ||
792 | * @param cls closure | ||
793 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | ||
794 | */ | ||
795 | static int | ||
796 | namestore_sqlite_commit_transaction (void *cls) | ||
797 | { | ||
798 | GNUNET_break (0); | ||
799 | return GNUNET_SYSERR; | ||
800 | } | ||
801 | |||
802 | |||
803 | /** | ||
804 | * Rollback a transaction. | ||
805 | * | ||
806 | * @param cls closure | ||
807 | */ | ||
808 | static void | ||
809 | namestore_sqlite_rollback_transaction (void *cls) | ||
810 | { | ||
811 | GNUNET_break (0); | ||
812 | } | ||
813 | |||
814 | |||
815 | |||
816 | /** | ||
776 | * Entry point for the plugin. | 817 | * Entry point for the plugin. |
777 | * | 818 | * |
778 | * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*" | 819 | * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*" |
@@ -800,6 +841,9 @@ libgnunet_plugin_namestore_sqlite_init (void *cls) | |||
800 | api->iterate_records = &namestore_sqlite_iterate_records; | 841 | api->iterate_records = &namestore_sqlite_iterate_records; |
801 | api->zone_to_name = &namestore_sqlite_zone_to_name; | 842 | api->zone_to_name = &namestore_sqlite_zone_to_name; |
802 | api->lookup_records = &namestore_sqlite_lookup_records; | 843 | api->lookup_records = &namestore_sqlite_lookup_records; |
844 | api->begin_transaction = &namestore_sqlite_begin_transaction; | ||
845 | api->commit_transaction = &namestore_sqlite_commit_transaction; | ||
846 | api->rollback_transaction = &namestore_sqlite_rollback_transaction; | ||
803 | LOG (GNUNET_ERROR_TYPE_INFO, | 847 | LOG (GNUNET_ERROR_TYPE_INFO, |
804 | _("Sqlite database running\n")); | 848 | _("Sqlite database running\n")); |
805 | return api; | 849 | return api; |