aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/plugin_namestore_sqlite.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/namestore/plugin_namestore_sqlite.c')
-rw-r--r--src/namestore/plugin_namestore_sqlite.c109
1 files changed, 52 insertions, 57 deletions
diff --git a/src/namestore/plugin_namestore_sqlite.c b/src/namestore/plugin_namestore_sqlite.c
index 1602da4ab..15a6586b5 100644
--- a/src/namestore/plugin_namestore_sqlite.c
+++ b/src/namestore/plugin_namestore_sqlite.c
@@ -50,13 +50,14 @@
50 * a failure of the command 'cmd' on file 'filename' 50 * a failure of the command 'cmd' on file 'filename'
51 * with the message given by strerror(errno). 51 * with the message given by strerror(errno).
52 */ 52 */
53#define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, \ 53#define LOG_SQLITE(db, level, cmd) do { \
54 "namestore-sqlite", _ ( \ 54 GNUNET_log_from (level, \
55 "`%s' failed at %s:%d with error: %s\n"), \ 55 "namestore-sqlite", _ ( \
56 cmd, \ 56 "`%s' failed at %s:%d with error: %s\n"), \
57 __FILE__, __LINE__, \ 57 cmd, \
58 sqlite3_errmsg ( \ 58 __FILE__, __LINE__, \
59 db->dbh)); \ 59 sqlite3_errmsg ( \
60 db->dbh)); \
60} while (0) 61} while (0)
61 62
62#define LOG(kind, ...) GNUNET_log_from (kind, "namestore-sqlite", __VA_ARGS__) 63#define LOG(kind, ...) GNUNET_log_from (kind, "namestore-sqlite", __VA_ARGS__)
@@ -75,9 +76,9 @@ struct Plugin
75 char *fn; 76 char *fn;
76 77
77 /** 78 /**
78 * Statements prepared, we are ready to go if GNUNET_YES 79 * Statements prepared, we are ready to go if true.
79 */ 80 */
80 int ready; 81 bool ready;
81 82
82 /** 83 /**
83 * Native SQLite database handle. 84 * Native SQLite database handle.
@@ -124,10 +125,10 @@ struct Plugin
124 * @param plugin the plugin context (state for this module) 125 * @param plugin the plugin context (state for this module)
125 * @return #GNUNET_OK on success 126 * @return #GNUNET_OK on success
126 */ 127 */
127static int 128static enum GNUNET_GenericReturnValue
128database_prepare (struct Plugin *plugin) 129database_prepare (struct Plugin *plugin)
129{ 130{
130 if (GNUNET_YES == plugin->ready) 131 if (plugin->ready)
131 return GNUNET_OK; 132 return GNUNET_OK;
132 struct GNUNET_SQ_ExecuteStatement es[] = { 133 struct GNUNET_SQ_ExecuteStatement es[] = {
133 GNUNET_SQ_make_try_execute ("PRAGMA temp_store=MEMORY"), 134 GNUNET_SQ_make_try_execute ("PRAGMA temp_store=MEMORY"),
@@ -264,7 +265,7 @@ database_shutdown (struct Plugin *plugin)
264 * @param rd array of records with data to store 265 * @param rd array of records with data to store
265 * @return #GNUNET_OK on success, else #GNUNET_SYSERR 266 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
266 */ 267 */
267static int 268static enum GNUNET_GenericReturnValue
268namestore_sqlite_store_records (void *cls, 269namestore_sqlite_store_records (void *cls,
269 const struct 270 const struct
270 GNUNET_IDENTITY_PrivateKey *zone_key, 271 GNUNET_IDENTITY_PrivateKey *zone_key,
@@ -421,7 +422,7 @@ namestore_sqlite_store_records (void *cls,
421 * @param iter_cls closure for @a iter 422 * @param iter_cls closure for @a iter
422 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error 423 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
423 */ 424 */
424static int 425static enum GNUNET_GenericReturnValue
425get_records_and_call_iterator (struct Plugin *plugin, 426get_records_and_call_iterator (struct Plugin *plugin,
426 sqlite3_stmt *stmt, 427 sqlite3_stmt *stmt,
427 const struct 428 const struct
@@ -539,7 +540,7 @@ get_records_and_call_iterator (struct Plugin *plugin,
539 * @param iter_cls closure for @a iter 540 * @param iter_cls closure for @a iter
540 * @return #GNUNET_OK on success, #GNUNET_NO for no results, else #GNUNET_SYSERR 541 * @return #GNUNET_OK on success, #GNUNET_NO for no results, else #GNUNET_SYSERR
541 */ 542 */
542static int 543static enum GNUNET_GenericReturnValue
543namestore_sqlite_lookup_records (void *cls, 544namestore_sqlite_lookup_records (void *cls,
544 const struct 545 const struct
545 GNUNET_IDENTITY_PrivateKey *zone, 546 GNUNET_IDENTITY_PrivateKey *zone,
@@ -591,7 +592,7 @@ namestore_sqlite_lookup_records (void *cls,
591 * @param iter_cls closure for @a iter 592 * @param iter_cls closure for @a iter
592 * @return #GNUNET_OK on success, #GNUNET_NO if there were no more results, #GNUNET_SYSERR on error 593 * @return #GNUNET_OK on success, #GNUNET_NO if there were no more results, #GNUNET_SYSERR on error
593 */ 594 */
594static int 595static enum GNUNET_GenericReturnValue
595namestore_sqlite_iterate_records (void *cls, 596namestore_sqlite_iterate_records (void *cls,
596 const struct 597 const struct
597 GNUNET_IDENTITY_PrivateKey *zone, 598 GNUNET_IDENTITY_PrivateKey *zone,
@@ -659,7 +660,7 @@ namestore_sqlite_iterate_records (void *cls,
659 * @param iter_cls closure for @a iter 660 * @param iter_cls closure for @a iter
660 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error 661 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
661 */ 662 */
662static int 663static enum GNUNET_GenericReturnValue
663namestore_sqlite_zone_to_name (void *cls, 664namestore_sqlite_zone_to_name (void *cls,
664 const struct GNUNET_IDENTITY_PrivateKey *zone, 665 const struct GNUNET_IDENTITY_PrivateKey *zone,
665 const struct 666 const struct
@@ -697,6 +698,7 @@ namestore_sqlite_zone_to_name (void *cls,
697 iter_cls); 698 iter_cls);
698} 699}
699 700
701
700/** 702/**
701 * Begin a transaction for a client. 703 * Begin a transaction for a client.
702 * This locks the database. SQLite is unable to discern between different 704 * This locks the database. SQLite is unable to discern between different
@@ -726,6 +728,7 @@ namestore_sqlite_transaction_begin (void *cls,
726 return (SQLITE_OK != rc) ? GNUNET_SYSERR : GNUNET_YES; 728 return (SQLITE_OK != rc) ? GNUNET_SYSERR : GNUNET_YES;
727} 729}
728 730
731
729/** 732/**
730 * Commit a transaction for a client. 733 * Commit a transaction for a client.
731 * This releases the lock on the database. 734 * This releases the lock on the database.
@@ -753,6 +756,7 @@ namestore_sqlite_transaction_rollback (void *cls,
753 return (SQLITE_OK != rc) ? GNUNET_SYSERR : GNUNET_YES; 756 return (SQLITE_OK != rc) ? GNUNET_SYSERR : GNUNET_YES;
754} 757}
755 758
759
756/** 760/**
757 * Roll back a transaction for a client. 761 * Roll back a transaction for a client.
758 * This releases the lock on the database. 762 * This releases the lock on the database.
@@ -780,14 +784,11 @@ namestore_sqlite_transaction_commit (void *cls,
780 return (SQLITE_OK != rc) ? GNUNET_SYSERR : GNUNET_YES; 784 return (SQLITE_OK != rc) ? GNUNET_SYSERR : GNUNET_YES;
781} 785}
782 786
787
783static enum GNUNET_GenericReturnValue 788static enum GNUNET_GenericReturnValue
784init_database (void *cls, char **emsg, int drop) 789namestore_sqlite_create_tables (void *cls)
785{ 790{
786 struct Plugin *plugin = cls; 791 struct Plugin *plugin = cls;
787 struct GNUNET_SQ_ExecuteStatement es_drop[] = {
788 GNUNET_SQ_make_execute ("DROP TABLE IF EXISTS ns098records"),
789 GNUNET_SQ_EXECUTE_STATEMENT_END
790 };
791 struct GNUNET_SQ_ExecuteStatement es[] = { 792 struct GNUNET_SQ_ExecuteStatement es[] = {
792 GNUNET_SQ_make_try_execute ("PRAGMA temp_store=MEMORY"), 793 GNUNET_SQ_make_try_execute ("PRAGMA temp_store=MEMORY"),
793 GNUNET_SQ_make_try_execute ("PRAGMA synchronous=NORMAL"), 794 GNUNET_SQ_make_try_execute ("PRAGMA synchronous=NORMAL"),
@@ -797,7 +798,7 @@ init_database (void *cls, char **emsg, int drop)
797 GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=NORMAL"), 798 GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=NORMAL"),
798 GNUNET_SQ_make_try_execute ("PRAGMA journal_mode=WAL"), 799 GNUNET_SQ_make_try_execute ("PRAGMA journal_mode=WAL"),
799 GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"), 800 GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"),
800 GNUNET_SQ_make_execute ("CREATE TABLE ns098records (" 801 GNUNET_SQ_make_execute ("CREATE TABLE IF NOT EXISTS ns098records ("
801 " uid INTEGER PRIMARY KEY," 802 " uid INTEGER PRIMARY KEY,"
802 " zone_private_key BLOB NOT NULL," 803 " zone_private_key BLOB NOT NULL,"
803 " pkey BLOB," 804 " pkey BLOB,"
@@ -812,39 +813,42 @@ init_database (void *cls, char **emsg, int drop)
812 "ON ns098records (zone_private_key,uid)"), 813 "ON ns098records (zone_private_key,uid)"),
813 GNUNET_SQ_EXECUTE_STATEMENT_END 814 GNUNET_SQ_EXECUTE_STATEMENT_END
814 }; 815 };
815 if ((GNUNET_YES == drop) && 816
816 (GNUNET_OK != GNUNET_SQ_exec_statements (plugin->dbh,
817 es_drop)))
818 {
819 GNUNET_asprintf (emsg,
820 _ ("Failed to drop database with: `%s'\n"),
821 sqlite3_errmsg (plugin->dbh));
822 return GNUNET_SYSERR;
823 }
824 if (GNUNET_OK != 817 if (GNUNET_OK !=
825 GNUNET_SQ_exec_statements (plugin->dbh, 818 GNUNET_SQ_exec_statements (plugin->dbh,
826 es)) 819 es))
827 { 820 {
828 GNUNET_asprintf (emsg, 821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
829 _ ("Failed to setup database with: `%s'\n"), 822 "Failed to setup database with: `%s'\n",
830 sqlite3_errmsg (plugin->dbh)); 823 sqlite3_errmsg (plugin->dbh));
831 return GNUNET_SYSERR; 824 return GNUNET_SYSERR;
832 } 825 }
833 return GNUNET_OK; 826 return GNUNET_OK;
834} 827}
835 828
836enum GNUNET_GenericReturnValue
837namestore_sqlite_initialize_database (void *cls, char **emsg)
838{
839 return init_database (cls, emsg, GNUNET_NO);
840}
841 829
842enum GNUNET_GenericReturnValue 830static enum GNUNET_GenericReturnValue
843namestore_sqlite_reset_database (void *cls, char **emsg) 831namestore_sqlite_drop_tables (void *cls)
844{ 832{
845 return init_database (cls, emsg, GNUNET_YES); 833 struct Plugin *plugin = cls;
834 struct GNUNET_SQ_ExecuteStatement es_drop[] = {
835 GNUNET_SQ_make_execute ("DROP TABLE IF EXISTS ns098records"),
836 GNUNET_SQ_EXECUTE_STATEMENT_END
837 };
838
839 if (GNUNET_OK !=
840 GNUNET_SQ_exec_statements (plugin->dbh,
841 es_drop))
842 {
843 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
844 "Failed to drop database with: `%s'\n",
845 sqlite3_errmsg (plugin->dbh));
846 return GNUNET_SYSERR;
847 }
848 return GNUNET_OK;
846} 849}
847 850
851
848/** 852/**
849 * Initialize the database connections and associated 853 * Initialize the database connections and associated
850 * data structures (create tables and indices 854 * data structures (create tables and indices
@@ -853,11 +857,10 @@ namestore_sqlite_reset_database (void *cls, char **emsg)
853 * @param plugin the plugin context (state for this module) 857 * @param plugin the plugin context (state for this module)
854 * @return #GNUNET_OK on success 858 * @return #GNUNET_OK on success
855 */ 859 */
856static int 860static enum GNUNET_GenericReturnValue
857database_connect (struct Plugin *plugin) 861database_connect (struct Plugin *plugin)
858{ 862{
859 char *sqlite_filename; 863 char *sqlite_filename;
860 char *emsg;
861 864
862 if (GNUNET_OK != 865 if (GNUNET_OK !=
863 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, 866 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
@@ -902,17 +905,9 @@ database_connect (struct Plugin *plugin)
902 "namestore-sqlite", 905 "namestore-sqlite",
903 "INIT_ON_CONNECT")) 906 "INIT_ON_CONNECT"))
904 { 907 {
905 /** 908 if (GNUNET_OK !=
906 * Gracefully fail as this should not be a critical error if the 909 namestore_sqlite_create_tables (plugin))
907 * database is already created 910 return GNUNET_SYSERR;
908 */
909 if (GNUNET_OK != init_database (plugin, &emsg, GNUNET_NO))
910 {
911 LOG (GNUNET_ERROR_TYPE_DEBUG,
912 "Failed to initialize database on connect: `%s'\n",
913 emsg);
914 GNUNET_free (emsg);
915 }
916 } 911 }
917 return GNUNET_OK; 912 return GNUNET_OK;
918} 913}
@@ -949,8 +944,8 @@ libgnunet_plugin_namestore_sqlite_init (void *cls)
949 api->transaction_begin = &namestore_sqlite_transaction_begin; 944 api->transaction_begin = &namestore_sqlite_transaction_begin;
950 api->transaction_commit = &namestore_sqlite_transaction_commit; 945 api->transaction_commit = &namestore_sqlite_transaction_commit;
951 api->transaction_rollback = &namestore_sqlite_transaction_rollback; 946 api->transaction_rollback = &namestore_sqlite_transaction_rollback;
952 api->initialize_database = &namestore_sqlite_initialize_database; 947 api->create_tables = &namestore_sqlite_create_tables;
953 api->reset_database = &namestore_sqlite_reset_database; 948 api->drop_tables = &namestore_sqlite_drop_tables;
954 /** 949 /**
955 * NOTE: Since SQlite does not support SELECT ... FOR UPDATE this is 950 * NOTE: Since SQlite does not support SELECT ... FOR UPDATE this is
956 * just an alias to lookup_records. The BEGIN IMMEDIATE mechanic currently 951 * just an alias to lookup_records. The BEGIN IMMEDIATE mechanic currently