2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
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
7 * by the Free Software Foundation; either version 3, or (at your
8 * option) any later version.
10 * GNUnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNUnet; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @file psycstore/plugin_psycstore_mysql.c
23 * @brief sqlite-based psycstore backend
24 * @author Gabor X Toth
25 * @author Christian Grothoff
26 * @author Christophe Genevey
30 * FIXME: SQLite3 only supports signed 64-bit integers natively,
31 * thus it can only store 63 bits of the uint64_t's.
35 #include "gnunet_psycstore_plugin.h"
36 #include "gnunet_psycstore_service.h"
37 #include "gnunet_multicast_service.h"
38 #include "gnunet_crypto_lib.h"
39 #include "gnunet_psyc_util_lib.h"
40 #include "psycstore.h"
41 #include "gnunet_my_lib.h"
42 #include "gnunet_mysql_lib.h"
45 #include <mysql/mysql.h>
48 * After how many ms "busy" should a DB operation fail for good? A
49 * low value makes sure that we are more responsive to requests
50 * (especially PUTs). A high value guarantees a higher success rate
51 * (SELECTs in iterate can take several seconds despite LIMIT=1).
53 * The default value of 1s should ensure that users do not experience
54 * huge latencies while at the same time allowing operations to
55 * succeed with reasonable probability.
57 #define BUSY_TIMEOUT_MS 1000
59 #define DEBUG_PSYCSTORE GNUNET_EXTRA_LOGGING
62 * Log an error message at log-level 'level' that indicates
63 * a failure of the command 'cmd' on file 'filename'
64 * with the message given by strerror(errno).
66 #define LOG_MYSQL(db, level, cmd, stmt) do { GNUNET_log_from (level, "psycstore-mysql", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, mysql_stmt_error (stmt)); } while(0)
68 #define LOG(kind,...) GNUNET_log_from (kind, "psycstore-mysql", __VA_ARGS__)
72 TRANSACTION_STATE_MODIFY,
73 TRANSACTION_STATE_SYNC,
77 * Context for all functions in this plugin.
82 const struct GNUNET_CONFIGURATION_Handle *cfg;
90 *Handle to talk to Mysql
92 struct GNUNET_MYSQL_Context *mc;
95 * Current transaction.
97 enum Transactions transaction;
99 struct GNUNET_MYSQL_StatementHandle *transaction_begin;
101 struct GNUNET_MYSQL_StatementHandle *transaction_commit;
103 struct GNUNET_MYSQL_StatementHandle *transaction_rollback;
106 * Precompiled SQL for channel_key_store()
108 struct GNUNET_MYSQL_StatementHandle *insert_channel_key;
112 * Precompiled SQL for slave_key_store()
114 struct GNUNET_MYSQL_StatementHandle *insert_slave_key;
117 * Precompiled SQL for membership_store()
119 struct GNUNET_MYSQL_StatementHandle *insert_membership;
122 * Precompiled SQL for membership_test()
124 struct GNUNET_MYSQL_StatementHandle *select_membership;
127 * Precompiled SQL for fragment_store()
129 struct GNUNET_MYSQL_StatementHandle *insert_fragment;
132 * Precompiled SQL for message_add_flags()
134 struct GNUNET_MYSQL_StatementHandle *update_message_flags;
137 * Precompiled SQL for fragment_get()
139 struct GNUNET_MYSQL_StatementHandle *select_fragments;
142 * Precompiled SQL for fragment_get()
144 struct GNUNET_MYSQL_StatementHandle *select_latest_fragments;
147 * Precompiled SQL for message_get()
149 struct GNUNET_MYSQL_StatementHandle *select_messages;
152 * Precompiled SQL for message_get()
154 struct GNUNET_MYSQL_StatementHandle *select_latest_messages;
157 * Precompiled SQL for message_get_fragment()
159 struct GNUNET_MYSQL_StatementHandle *select_message_fragment;
162 * Precompiled SQL for counters_get_message()
164 struct GNUNET_MYSQL_StatementHandle *select_counters_message;
167 * Precompiled SQL for counters_get_state()
169 struct GNUNET_MYSQL_StatementHandle *select_counters_state;
172 * Precompiled SQL for state_modify_end()
174 struct GNUNET_MYSQL_StatementHandle *update_state_hash_message_id;
177 * Precompiled SQL for state_sync_end()
179 struct GNUNET_MYSQL_StatementHandle *update_max_state_message_id;
182 * Precompiled SQL for state_modify_op()
184 struct GNUNET_MYSQL_StatementHandle *insert_state_current;
187 * Precompiled SQL for state_modify_end()
189 struct GNUNET_MYSQL_StatementHandle *delete_state_empty;
192 * Precompiled SQL for state_set_signed()
194 struct GNUNET_MYSQL_StatementHandle *update_state_signed;
197 * Precompiled SQL for state_sync()
199 struct GNUNET_MYSQL_StatementHandle *insert_state_sync;
202 * Precompiled SQL for state_sync()
204 struct GNUNET_MYSQL_StatementHandle *delete_state;
207 * Precompiled SQL for state_sync()
209 struct GNUNET_MYSQL_StatementHandle *insert_state_from_sync;
212 * Precompiled SQL for state_sync()
214 struct GNUNET_MYSQL_StatementHandle *delete_state_sync;
217 * Precompiled SQL for state_get_signed()
219 struct GNUNET_MYSQL_StatementHandle *select_state_signed;
222 * Precompiled SQL for state_get()
224 struct GNUNET_MYSQL_StatementHandle *select_state_one;
227 * Precompiled SQL for state_get_prefix()
229 struct GNUNET_MYSQL_StatementHandle *select_state_prefix;
236 sql_trace (void *cls, const char *sql)
238 LOG (GNUNET_ERROR_TYPE_DEBUG, "MYSQL query:\n%s\n", sql);
245 * @brief Prepare a SQL statement
247 * @param dbh handle to the database
248 * @param sql SQL statement, UTF-8 encoded
249 * @param stmt set to the prepared statement
250 * @return 0 on success
253 mysql_prepare (struct GNUNET_MYSQL_Context *mc,
255 struct GNUNET_MYSQL_StatementHandle *stmt)
257 stmt = GNUNET_MYSQL_statement_prepare (mc,
260 LOG (GNUNET_ERROR_TYPE_DEBUG,
261 "Prepared `%s' / %p\n", sql, stmt);
263 LOG (GNUNET_ERROR_TYPE_ERROR,
264 _("Error preparing SQL query: %s\n %s\n"),
265 mysql_stmt_error (GNUNET_MYSQL_statement_get_stmt (stmt)), sql);
272 * @brief Prepare a SQL statement
274 * @param dbh handle to the database
275 * @param sql SQL statement, UTF-8 encoded
276 * @return 0 on success
279 mysql_exec (struct GNUNET_MYSQL_Context *mc,
280 struct GNUNET_MYSQL_StatementHandle *sh,
281 struct GNUNET_MY_QueryParam *qp)
285 result = GNUNET_MY_exec_prepared (mc, sh, qp);
286 LOG (GNUNET_ERROR_TYPE_DEBUG,
287 "Executed `GNUNET_MY_exec_prepared`' / %d\n", result);
288 if (GNUNET_OK != result)
289 LOG (GNUNET_ERROR_TYPE_ERROR,
290 _("Error executing SQL query: %s\n"),
291 mysql_stmt_error (GNUNET_MYSQL_statement_get_stmt (sh)));
297 * Initialize the database connections and associated
298 * data structures (create tables and indices
299 * as needed as well).
301 * @param plugin the plugin context (state for this module)
302 * @return GNUNET_OK on success
305 database_setup (struct Plugin *plugin)
310 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "psycstore-mysql",
311 "FILENAME", &filename))
313 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
314 "psycstore-sqlite", "FILENAME");
315 return GNUNET_SYSERR;
317 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
319 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (filename))
322 GNUNET_free (filename);
323 return GNUNET_SYSERR;
326 /* filename should be UTF-8-encoded. If it isn't, it's a bug */
327 plugin->fn = filename;
329 /* Open database and precompile statements */
330 plugin->mc = GNUNET_MYSQL_context_create(plugin->cfg, "psycstore-mysql");
332 if (NULL == plugin->mc)
334 LOG (GNUNET_ERROR_TYPE_ERROR,
335 _("Unable to initialize SQLite: %s.\n"),
336 sqlite3_errmsg (plugin->dbh));
337 return GNUNET_SYSERR;
342 sqlite3_trace (plugin->dbh, &sql_trace, NULL);
345 sql_exec (plugin->dbh, "PRAGMA temp_store=MEMORY");
346 sql_exec (plugin->dbh, "PRAGMA synchronous=NORMAL");
347 sql_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF");
348 sql_exec (plugin->dbh, "PRAGMA auto_vacuum=INCREMENTAL");
349 sql_exec (plugin->dbh, "PRAGMA encoding=\"UTF-8\"");
350 #if ! DEBUG_PSYCSTORE
351 sql_exec (plugin->dbh, "PRAGMA locking_mode=EXCLUSIVE");
353 sql_exec (plugin->dbh, "PRAGMA page_size=4096");
355 sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS);
360 GNUNET_MYSQL_statement_run (plugin->mc,
361 "CREATE TABLE IF NOT EXISTS channels (\n"
362 " id INT PRIMARY KEY,\n"
363 " pub_key BLOB UNIQUE,\n"
364 " max_state_message_id INT,\n" // last applied state message ID
365 " state_hash_message_id INT\n" // last message ID with a state hash
368 GNUNET_MYSQL_statement_run (plugin->mc,
369 "CREATE TABLE IF NOT EXISTS slaves (\n"
370 " id INT PRIMARY KEY,\n"
371 " pub_key BLOB UNIQUE\n"
374 GNUNET_MYSQL_statement_run (plugin->mc,
375 "CREATE TABLE IF NOT EXISTS membership (\n"
376 " channel_id INT NOT NULL REFERENCES channels(id),\n"
377 " slave_id INT NOT NULL REFERENCES slaves(id),\n"
378 " did_join INT NOT NULL,\n"
379 " announced_at INT NOT NULL,\n"
380 " effective_since INT NOT NULL,\n"
381 " group_generation INT NOT NULL\n"
384 GNUNET_MYSQL_statement_run (plugin->mc,
385 "CREATE INDEX IF NOT EXISTS idx_membership_channel_id_slave_id "
386 "ON membership (channel_id, slave_id);");
388 /** @todo messages table: add method_name column */
389 GNUNET_MYSQL_statement_run (plugin->mc,
390 "CREATE TABLE IF NOT EXISTS messages (\n"
391 " channel_id INT NOT NULL REFERENCES channels(id),\n"
392 " hop_counter INT NOT NULL,\n"
395 " fragment_id INT NOT NULL,\n"
396 " fragment_offset INT NOT NULL,\n"
397 " message_id INT NOT NULL,\n"
398 " group_generation INT NOT NULL,\n"
399 " multicast_flags INT NOT NULL,\n"
400 " psycstore_flags INT NOT NULL,\n"
402 " PRIMARY KEY (channel_id, fragment_id),\n"
403 " UNIQUE (channel_id, message_id, fragment_offset)\n"
406 GNUNET_MYSQL_statement_run (plugin->mc,
407 "CREATE TABLE IF NOT EXISTS state (\n"
408 " channel_id INT NOT NULL REFERENCES channels(id),\n"
409 " name TEXT NOT NULL,\n"
410 " value_current BLOB,\n"
411 " value_signed BLOB,\n"
412 " PRIMARY KEY (channel_id, name)\n"
415 GNUNET_MYSQL_statement_run (plugin->mc,
416 "CREATE TABLE IF NOT EXISTS state_sync (\n"
417 " channel_id INT NOT NULL REFERENCES channels(id),\n"
418 " name TEXT NOT NULL,\n"
420 " PRIMARY KEY (channel_id, name)\n"
423 /* Prepare statements */
424 mysql_prepare (plugin->mc,
426 plugin->transaction_begin);
428 mysql_prepare (plugin->mc,
430 plugin->transaction_commit);
432 mysql_prepare (plugin->mc,
434 plugin->transaction_rollback);
436 mysql_prepare (plugin->mc,
437 "INSERT OR IGNORE INTO channels (pub_key) VALUES (?);",
438 plugin->insert_channel_key);
440 mysql_prepare (plugin->mc,
441 "INSERT OR IGNORE INTO slaves (pub_key) VALUES (?);",
442 plugin->insert_slave_key);
444 mysql_prepare (plugin->mc,
445 "INSERT INTO membership\n"
446 " (channel_id, slave_id, did_join, announced_at,\n"
447 " effective_since, group_generation)\n"
448 "VALUES ((SELECT id FROM channels WHERE pub_key = ?),\n"
449 " (SELECT id FROM slaves WHERE pub_key = ?),\n"
451 plugin->insert_membership);
453 mysql_prepare (plugin->mc,
454 "SELECT did_join FROM membership\n"
455 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
456 " AND slave_id = (SELECT id FROM slaves WHERE pub_key = ?)\n"
457 " AND effective_since <= ? AND did_join = 1\n"
458 "ORDER BY announced_at DESC LIMIT 1;",
459 plugin->select_membership);
461 mysql_prepare (plugin->mc,
462 "INSERT OR IGNORE INTO messages\n"
463 " (channel_id, hop_counter, signature, purpose,\n"
464 " fragment_id, fragment_offset, message_id,\n"
465 " group_generation, multicast_flags, psycstore_flags, data)\n"
466 "VALUES ((SELECT id FROM channels WHERE pub_key = ?),\n"
467 " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
468 plugin->insert_fragment);
470 mysql_prepare (plugin->mc,
472 "SET psycstore_flags = psycstore_flags | ?\n"
473 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
474 " AND message_id = ? AND fragment_offset = 0;",
475 plugin->update_message_flags);
477 mysql_prepare (plugin->mc,
478 "SELECT hop_counter, signature, purpose, fragment_id,\n"
479 " fragment_offset, message_id, group_generation,\n"
480 " multicast_flags, psycstore_flags, data\n"
482 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
483 " AND ? <= fragment_id AND fragment_id <= ?;",
484 plugin->select_fragments);
486 /** @todo select_messages: add method_prefix filter */
487 mysql_prepare (plugin->mc,
488 "SELECT hop_counter, signature, purpose, fragment_id,\n"
489 " fragment_offset, message_id, group_generation,\n"
490 " multicast_flags, psycstore_flags, data\n"
492 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
493 " AND ? <= message_id AND message_id <= ?"
495 plugin->select_messages);
497 mysql_prepare (plugin->mc,
499 "(SELECT hop_counter, signature, purpose, fragment_id,\n"
500 " fragment_offset, message_id, group_generation,\n"
501 " multicast_flags, psycstore_flags, data\n"
503 " WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
504 " ORDER BY fragment_id DESC\n"
506 "ORDER BY fragment_id;",
507 plugin->select_latest_fragments);
509 /** @todo select_latest_messages: add method_prefix filter */
510 mysql_prepare (plugin->mc,
511 "SELECT hop_counter, signature, purpose, fragment_id,\n"
512 " fragment_offset, message_id, group_generation,\n"
513 " multicast_flags, psycstore_flags, data\n"
515 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
516 " AND message_id IN\n"
517 " (SELECT message_id\n"
519 " WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
520 " GROUP BY message_id\n"
521 " ORDER BY message_id\n"
523 "ORDER BY fragment_id;",
524 plugin->select_latest_messages);
526 mysql_prepare (plugin->mc,
527 "SELECT hop_counter, signature, purpose, fragment_id,\n"
528 " fragment_offset, message_id, group_generation,\n"
529 " multicast_flags, psycstore_flags, data\n"
531 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
532 " AND message_id = ? AND fragment_offset = ?;",
533 plugin->select_message_fragment);
535 mysql_prepare (plugin->mc,
536 "SELECT fragment_id, message_id, group_generation\n"
538 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
539 "ORDER BY fragment_id DESC LIMIT 1;",
540 plugin->select_counters_message);
542 mysql_prepare (plugin->mc,
543 "SELECT max_state_message_id\n"
545 "WHERE pub_key = ? AND max_state_message_id IS NOT NULL;",
546 plugin->select_counters_state);
548 mysql_prepare (plugin->mc,
550 "SET max_state_message_id = ?\n"
551 "WHERE pub_key = ?;",
552 plugin->update_max_state_message_id);
554 mysql_prepare (plugin->mc,
556 "SET state_hash_message_id = ?\n"
557 "WHERE pub_key = ?;",
558 plugin->update_state_hash_message_id);
560 mysql_prepare (plugin->mc,
561 "INSERT OR REPLACE INTO state\n"
562 " (channel_id, name, value_current, value_signed)\n"
563 "SELECT new.channel_id, new.name,\n"
564 " new.value_current, old.value_signed\n"
565 "FROM (SELECT (SELECT id FROM channels WHERE pub_key = ?)\n"
567 " ? AS name, ? AS value_current) AS new\n"
568 "LEFT JOIN (SELECT channel_id, name, value_signed\n"
569 " FROM state) AS old\n"
570 "ON new.channel_id = old.channel_id AND new.name = old.name;",
571 plugin->insert_state_current);
573 mysql_prepare (plugin->mc,
574 "DELETE FROM state\n"
575 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
576 " AND (value_current IS NULL OR length(value_current) = 0)\n"
577 " AND (value_signed IS NULL OR length(value_signed) = 0);",
578 plugin->delete_state_empty);
580 mysql_prepare (plugin->mc,
582 "SET value_signed = value_current\n"
583 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);",
584 plugin->update_state_signed);
586 mysql_prepare (plugin->mc,
587 "DELETE FROM state\n"
588 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);",
589 plugin->delete_state);
591 mysql_prepare (plugin->mc,
592 "INSERT INTO state_sync (channel_id, name, value)\n"
593 "VALUES ((SELECT id FROM channels WHERE pub_key = ?), ?, ?);",
594 plugin->insert_state_sync);
596 mysql_prepare (plugin->mc,
597 "INSERT INTO state\n"
598 " (channel_id, name, value_current, value_signed)\n"
599 "SELECT channel_id, name, value, value\n"
601 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);",
602 plugin->insert_state_from_sync);
604 mysql_prepare (plugin->mc,
605 "DELETE FROM state_sync\n"
606 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);",
607 plugin->delete_state_sync);
609 mysql_prepare (plugin->mc,
610 "SELECT value_current\n"
612 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
614 plugin->select_state_one);
616 mysql_prepare (plugin->mc,
617 "SELECT name, value_current\n"
619 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
620 " AND (name = ? OR substr(name, 1, ?) = ? || '_');",
621 plugin->select_state_prefix);
623 mysql_prepare (plugin->mc,
624 "SELECT name, value_signed\n"
626 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)"
627 " AND value_signed IS NOT NULL;",
628 plugin->select_state_signed);
635 * Shutdown database connection and associate data
637 * @param plugin the plugin context (state for this module)
640 database_shutdown (struct Plugin *plugin)
647 while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh, NULL)))
649 result = sqlite3_finalize (stmt);
650 if (SQLITE_OK != result)
651 LOG (GNUNET_ERROR_TYPE_WARNING,
652 "Failed to close statement %p: %d\n", stmt, result);
654 if (SQLITE_OK != sqlite3_close (plugin->dbh))
655 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
657 GNUNET_free_non_null (plugin->fn);
662 * Execute a prepared statement with a @a channel_key argument.
664 * @param plugin Plugin handle.
665 * @param stmt Statement to execute.
666 * @param channel_key Public key of the channel.
668 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
671 exec_channel (struct Plugin *plugin, struct GNUNET_MYSQL_StatementHandle *stmt,
672 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
674 MYSQL_STMT * statement = NULL;
675 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
677 if (NULL == statement)
679 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
680 "mysql statement invalide", statement);
681 return GNUNET_SYSERR;
684 struct GNUNET_MY_QueryParam params[] = {
685 GNUNET_MY_query_param_auto_from_type (channel_key),
686 GNUNET_MY_query_param_end
689 if(GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
693 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
694 "mysql exec_channel", stmt);
697 if (0 != mysql_stmt_reset (statement))
699 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
700 "mysql_stmt_reset", statement);
701 return GNUNET_SYSERR;
709 * Begin a transaction.
712 transaction_begin (struct Plugin *plugin, enum Transactions transaction)
714 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->transaction_begin;
715 MYSQL_STMT * statement = NULL;
717 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
719 if (NULL == statement)
721 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
722 "mysql statement invalide", statement);
723 return GNUNET_SYSERR;
726 struct GNUNET_MY_QueryParam params[] = {
727 GNUNET_MY_query_param_end
730 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
734 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
735 "mysql extract_result", statement);
736 return GNUNET_SYSERR;
739 if (0 != mysql_stmt_reset (statement))
741 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
742 "mysql_stmt_reset", statement);
743 return GNUNET_SYSERR;
746 plugin->transaction = transaction;
752 * Commit current transaction.
755 transaction_commit (struct Plugin *plugin)
757 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->transaction_commit;
758 MYSQL_STMT *statement = NULL;
760 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
762 if (NULL == statement)
764 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
765 "mysql statement invalide", statement);
766 return GNUNET_SYSERR;
769 struct GNUNET_MY_QueryParam params[] = {
770 GNUNET_MY_query_param_end
773 if (GNUNET_OK != GNUNET_MY_exec_prepared( plugin->mc,
777 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
778 "mysql extract_result", statement);
779 return GNUNET_SYSERR;
782 if (0 != mysql_stmt_reset (statement))
784 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
785 "mysql_stmt_reset", statement);
786 return GNUNET_SYSERR;
789 plugin->transaction = TRANSACTION_NONE;
795 * Roll back current transaction.
798 transaction_rollback (struct Plugin *plugin)
800 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->transaction_rollback;
801 MYSQL_STMT* statement = NULL;
803 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
804 if (NULL == statement)
806 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
807 "mysql statement invalide", statement);
808 return GNUNET_SYSERR;
811 struct GNUNET_MY_QueryParam params[] = {
812 GNUNET_MY_query_param_end
815 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
819 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
820 "mysql extract_result", statement);
821 return GNUNET_SYSERR;
824 if (0 != mysql_stmt_reset (statement))
826 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
827 "mysql_stmt_reset", statement);
828 return GNUNET_SYSERR;
831 plugin->transaction = TRANSACTION_NONE;
837 channel_key_store (struct Plugin *plugin,
838 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
840 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->insert_channel_key;
842 MYSQL_STMT *statement = NULL;
843 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
845 if(NULL == statement)
847 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
848 "mysql statement invalide", statement);
849 return GNUNET_SYSERR;
852 struct GNUNET_MY_QueryParam params[] = {
853 GNUNET_MY_query_param_auto_from_type (channel_key),
854 GNUNET_MY_query_param_end
857 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
861 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
862 "mysql extract_result", statement);
863 return GNUNET_SYSERR;
866 if (0 != mysql_stmt_reset (statement))
868 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
869 "mysql_stmt_reset", statement);
870 return GNUNET_SYSERR;
878 slave_key_store (struct Plugin *plugin,
879 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key)
881 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->insert_slave_key;
883 MYSQL_STMT *statement = NULL;
884 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
886 if(NULL == statement)
888 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
889 "mysql statement invalide", statement);
890 return GNUNET_SYSERR;
893 struct GNUNET_MY_QueryParam params[] = {
894 GNUNET_MY_query_param_auto_from_type (slave_key),
895 GNUNET_MY_query_param_end
898 if (GNUNET_OK != GNUNET_MY_exec_prepared( plugin->mc,
902 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
903 "mysql extract_result", statement);
904 return GNUNET_SYSERR;
907 if (0 != mysql_stmt_reset (statement))
909 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
910 "mysql_stmt_reset", statement);
911 return GNUNET_SYSERR;
919 * Store join/leave events for a PSYC channel in order to be able to answer
920 * membership test queries later.
922 * @see GNUNET_PSYCSTORE_membership_store()
924 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
927 sqlite_membership_store (void *cls,
928 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
929 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
931 uint64_t announced_at,
932 uint64_t effective_since,
933 uint64_t group_generation)
935 struct Plugin *plugin = cls;
937 uint32_t idid_join = (uint32_t)did_join;
938 uint64_t iannounced_at = (uint64_t)announced_at;
939 uint64_t ieffective_since = (uint64_t)effective_since;
940 uint64_t igroup_generation = (uint64_t)group_generation;
942 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->insert_membership;
943 MYSQL_STMT *statement = NULL;
945 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
947 GNUNET_assert (TRANSACTION_NONE == plugin->transaction);
949 if (announced_at > INT64_MAX ||
950 effective_since > INT64_MAX ||
951 group_generation > INT64_MAX)
954 return GNUNET_SYSERR;
957 if (GNUNET_OK != channel_key_store (plugin, channel_key)
958 || GNUNET_OK != slave_key_store (plugin, slave_key))
959 return GNUNET_SYSERR;
961 struct GNUNET_MY_QueryParam params[] = {
962 GNUNET_MY_query_param_auto_from_type (channel_key),
963 GNUNET_MY_query_param_auto_from_type (slave_key),
964 GNUNET_MY_query_param_uint32 (&idid_join),
965 GNUNET_MY_query_param_uint64 (&iannounced_at),
966 GNUNET_MY_query_param_uint64 (&ieffective_since),
967 GNUNET_MY_query_param_uint64 (&igroup_generation),
968 GNUNET_MY_query_param_end
971 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
975 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
976 "mysql extract_result", statement);
977 return GNUNET_SYSERR;
980 if (0 != mysql_stmt_reset (statement))
982 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
983 "mysql_stmt_reset", statement);
984 return GNUNET_SYSERR;
990 * Test if a member was admitted to the channel at the given message ID.
992 * @see GNUNET_PSYCSTORE_membership_test()
994 * @return #GNUNET_YES if the member was admitted, #GNUNET_NO if not,
995 * #GNUNET_SYSERR if there was en error.
998 membership_test (void *cls,
999 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1000 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
1001 uint64_t message_id)
1003 struct Plugin *plugin = cls;
1005 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_membership;
1006 MYSQL_STMT *statement = NULL;
1008 uint32_t did_join = 0;
1010 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1012 if(NULL == statement)
1014 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1015 "mysql statement invalide", statement);
1016 return GNUNET_SYSERR;
1019 int ret = GNUNET_SYSERR;
1021 struct GNUNET_MY_QueryParam params_select[] = {
1022 GNUNET_MY_query_param_auto_from_type (channel_key),
1023 GNUNET_MY_query_param_auto_from_type (slave_key),
1024 GNUNET_MY_query_param_uint64 (&message_id),
1025 GNUNET_MY_query_param_end
1028 if (GNUNET_MY_exec_prepared (plugin->mc,
1032 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1033 "mysql execute prepared", statement);
1034 return GNUNET_SYSERR;
1037 struct GNUNET_MY_ResultSpec results_select[] = {
1038 GNUNET_MY_result_spec_uint32 (&did_join),
1039 GNUNET_MY_result_spec_end
1042 if (GNUNET_OK != GNUNET_MY_extract_result (stmt,
1045 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1046 "mysql extract_result", statement);
1047 return GNUNET_SYSERR;
1059 if (0 != mysql_stmt_reset (statement))
1061 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1062 "mysql_stmt_reset", statement);
1063 return GNUNET_SYSERR;
1070 * Store a message fragment sent to a channel.
1072 * @see GNUNET_PSYCSTORE_fragment_store()
1074 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1077 fragment_store (void *cls,
1078 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1079 const struct GNUNET_MULTICAST_MessageHeader *msg,
1080 uint32_t psycstore_flags)
1082 struct Plugin *plugin = cls;
1084 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->insert_fragment;
1085 MYSQL_STMT *statement = NULL;
1087 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1089 GNUNET_assert (TRANSACTION_NONE == plugin->transaction);
1091 uint64_t fragment_id = GNUNET_ntohll (msg->fragment_id);
1092 uint64_t fragment_offset = GNUNET_ntohll (msg->fragment_offset);
1093 uint64_t message_id = GNUNET_ntohll (msg->message_id);
1094 uint64_t group_generation = GNUNET_ntohll (msg->group_generation);
1096 if (fragment_id > INT64_MAX || fragment_offset > INT64_MAX ||
1097 message_id > INT64_MAX || group_generation > INT64_MAX)
1099 LOG (GNUNET_ERROR_TYPE_ERROR,
1100 "Tried to store fragment with a field > INT64_MAX: "
1101 "%lu, %lu, %lu, %lu\n", fragment_id, fragment_offset,
1102 message_id, group_generation);
1104 return GNUNET_SYSERR;
1107 if (GNUNET_OK != channel_key_store (plugin, channel_key))
1108 return GNUNET_SYSERR;
1110 struct GNUNET_MY_QueryParam params_insert[] = {
1111 GNUNET_MY_query_param_auto_from_type (channel_key),
1112 GNUNET_MY_query_param_uint32 (msg->hop_counter),
1113 GNUNET_MY_query_param_auto_from_type (&msg->signature),
1114 GNUNET_MY_query_param_auto_from_type (&msg->purpose),
1115 GNUNET_MY_query_param_uint64 (&fragment_id),
1116 GNUNET_MY_query_param_uint64 (&fragment_offset),
1117 GNUNET_MY_query_param_uint64 (&message_id),
1118 GNUNET_MY_query_param_uint64 (&group_generation),
1119 GNUNET_MY_query_param_uint32 ( msg->flags),
1120 GNUNET_MY_query_param_uint32 (&psycstore_flags),
1121 GNUNET_MY_query_param_auto_from_type (&msg[1]),
1122 GNUNET_MY_query_param_end
1125 if (GNUNET_MY_exec_prepared (plugin->mc,
1129 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1130 "mysql execute prepared", statement);
1131 return GNUNET_SYSERR;
1134 if (0 != mysql_stmt_reset (statement))
1136 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1137 "mysql_stmt_reset", statement);
1138 return GNUNET_SYSERR;
1145 * Set additional flags for a given message.
1147 * They are OR'd with any existing flags set.
1149 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1152 message_add_flags (void *cls,
1153 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1154 uint64_t message_id,
1155 uint64_t psycstore_flags)
1157 struct Plugin *plugin = cls;
1159 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->update_message_flags;
1160 MYSQL_STMT *statement = NULL;
1162 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1164 int ret = GNUNET_SYSERR;
1166 struct GNUNET_MY_QueryParam params_update[] = {
1167 GNUNET_MY_query_param_uint64 (&psycstore_flags),
1168 GNUNET_MY_query_param_auto_from_type (channel_key),
1169 GNUNET_MY_query_param_uint64 (&message_id),
1170 GNUNET_MY_query_param_end
1173 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
1177 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1178 "mysql execute prepared", statement);
1179 return GNUNET_SYSERR;
1182 if (0 != mysql_stmt_reset (statement))
1184 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1185 "mysql_stmt_reset", statement);
1186 return GNUNET_SYSERR;
1192 /** Extract result from statement **/
1194 fragment_row (sqlite3_stmt *stmt, GNUNET_PSYCSTORE_FragmentCallback cb,
1197 int data_size = sqlite3_column_bytes (stmt, 9);
1198 struct GNUNET_MULTICAST_MessageHeader *msg
1199 = GNUNET_malloc (sizeof (*msg) + data_size);
1201 msg->header.size = htons (sizeof (*msg) + data_size);
1202 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
1203 msg->hop_counter = htonl ((uint32_t) sqlite3_column_int64 (stmt, 0));
1204 memcpy (&msg->signature,
1205 sqlite3_column_blob (stmt, 1),
1206 sqlite3_column_bytes (stmt, 1));
1207 memcpy (&msg->purpose,
1208 sqlite3_column_blob (stmt, 2),
1209 sqlite3_column_bytes (stmt, 2));
1210 msg->fragment_id = GNUNET_htonll (sqlite3_column_int64 (stmt, 3));
1211 msg->fragment_offset = GNUNET_htonll (sqlite3_column_int64 (stmt, 4));
1212 msg->message_id = GNUNET_htonll (sqlite3_column_int64 (stmt, 5));
1213 msg->group_generation = GNUNET_htonll (sqlite3_column_int64 (stmt, 6));
1214 msg->flags = htonl (sqlite3_column_int64 (stmt, 7));
1215 memcpy (&msg[1], sqlite3_column_blob (stmt, 9), data_size);
1217 return cb (cb_cls, (void *) msg, sqlite3_column_int64 (stmt, 8));
1222 fragment_select (struct Plugin *plugin, sqlite3_stmt *stmt,
1223 uint64_t *returned_fragments,
1224 GNUNET_PSYCSTORE_FragmentCallback cb, void *cb_cls)
1226 int ret = GNUNET_SYSERR;
1231 sql_ret = sqlite3_step (stmt);
1235 if (ret != GNUNET_OK)
1239 ret = fragment_row (stmt, cb, cb_cls);
1240 (*returned_fragments)++;
1241 if (ret != GNUNET_YES)
1242 sql_ret = SQLITE_DONE;
1245 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1249 while (sql_ret == SQLITE_ROW);
1255 * Retrieve a message fragment range by fragment ID.
1257 * @see GNUNET_PSYCSTORE_fragment_get()
1259 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1262 fragment_get (void *cls,
1263 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1264 uint64_t first_fragment_id,
1265 uint64_t last_fragment_id,
1266 uint64_t *returned_fragments,
1267 GNUNET_PSYCSTORE_FragmentCallback cb,
1270 struct Plugin *plugin = cls;
1272 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_fragments;
1273 MYSQL_STMT *statement = NULL;
1275 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1276 if (NULL == statement)
1278 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1279 "mysql get_stmt", statement);
1280 return GNUNET_SYSERR;
1283 int ret = GNUNET_SYSERR;
1284 *returned_fragments = 0;
1286 struct GNUNET_MY_QueryParam params_select[] = {
1287 GNUNET_MY_query_param_auto_from_type (channel_key),
1288 GNUNET_MY_query_param_uint64 (&first_fragment_id),
1289 GNUNET_MY_query_param_uint64 (&last_fragment_id),
1290 GNUNET_MY_query_param_end
1293 ret = fragment_select (plugin, stmt, returned_fragments, cb, cb_cls);
1295 if (0 != mysql_stmt_reset (statement))
1297 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1298 "mysql_stmt_reset", statement);
1299 return GNUNET_SYSERR;
1307 * Retrieve a message fragment range by fragment ID.
1309 * @see GNUNET_PSYCSTORE_fragment_get_latest()
1311 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1314 fragment_get_latest (void *cls,
1315 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1316 uint64_t fragment_limit,
1317 uint64_t *returned_fragments,
1318 GNUNET_PSYCSTORE_FragmentCallback cb,
1321 struct Plugin *plugin = cls;
1323 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_latest_fragments;
1324 MYSQL_STMT * statement = NULL;
1326 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1328 int ret = GNUNET_SYSERR;
1329 *returned_fragments = 0;
1331 struct GNUNET_MY_QueryParam params_select[] = {
1332 GNUNET_MY_query_param_auto_from_type (channel_key),
1333 GNUNET_MY_query_param_uint64 (&fragment_limit),
1334 GNUNET_MY_query_param_end
1337 ret = fragment_select (plugin, stmt, returned_fragments, cb, cb_cls);
1339 if (0 != mysql_stmt_reset (statement))
1341 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1342 "mysql_stmt_reset", statement);
1343 return GNUNET_SYSERR;
1351 * Retrieve all fragments of a message ID range.
1353 * @see GNUNET_PSYCSTORE_message_get()
1355 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1358 message_get (void *cls,
1359 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1360 uint64_t first_message_id,
1361 uint64_t last_message_id,
1362 uint64_t fragment_limit,
1363 uint64_t *returned_fragments,
1364 GNUNET_PSYCSTORE_FragmentCallback cb,
1367 struct Plugin *plugin = cls;
1369 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_messages;
1370 MYSQL_STMT *statement = NULL;
1372 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1374 int ret = GNUNET_SYSERR;
1375 *returned_fragments = 0;
1377 struct GNUNET_MY_QueryParam params_select[] = {
1378 GNUNET_MY_query_param_auto_from_type (channel_key),
1379 GNUNET_MY_query_param_uint64 (&first_message_id),
1380 GNUNET_MY_query_param_uint64 (&last_message_id),
1381 GNUNET_MY_query_param_uint64 (&fragment_limit),
1382 GNUNET_MY_query_param_end
1385 ret = fragment_select (plugin, stmt, returned_fragments, cb, cb_cls);
1387 if (0 != mysql_stmt_reset (statement))
1389 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1390 "mysql_stmt_reset", statement);
1391 return GNUNET_SYSERR;
1399 * Retrieve all fragments of the latest messages.
1401 * @see GNUNET_PSYCSTORE_message_get_latest()
1403 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1406 message_get_latest (void *cls,
1407 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1408 uint64_t message_limit,
1409 uint64_t *returned_fragments,
1410 GNUNET_PSYCSTORE_FragmentCallback cb,
1413 struct Plugin *plugin = cls;
1415 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_latest_messages;
1416 MYSQL_STMT *statement;
1418 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1420 int ret = GNUNET_SYSERR;
1421 *returned_fragments = 0;
1423 struct GNUNET_MY_QueryParam params_select[] = {
1424 GNUNET_MY_query_param_auto_from_type (channel_key),
1425 GNUNET_MY_query_param_auto_from_type (channel_key),
1426 GNUNET_MY_query_param_uint64 (&message_limit),
1427 GNUNET_MY_query_param_end
1430 ret = fragment_select (plugin, stmt, returned_fragments, cb, cb_cls);
1432 if (0 != mysql_stmt_reset (statement))
1434 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1435 "mysql_stmt_reset", statement);
1436 return GNUNET_SYSERR;
1444 * Retrieve a fragment of message specified by its message ID and fragment
1447 * @see GNUNET_PSYCSTORE_message_get_fragment()
1449 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1452 message_get_fragment (void *cls,
1453 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1454 uint64_t message_id,
1455 uint64_t fragment_offset,
1456 GNUNET_PSYCSTORE_FragmentCallback cb,
1459 struct Plugin *plugin = cls;
1461 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_message_fragment;
1462 MYSQL_STMT *statement = NULL;
1464 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1466 int ret = GNUNET_SYSERR;
1468 struct GNUNET_MY_QueryParam params_select[] = {
1469 GNUNET_MY_query_param_auto_from_type (channel_key),
1470 GNUNET_MY_query_param_uint64 (&message_id),
1471 GNUNET_MY_query_param_uint64 (&fragment_offset),
1472 GNUNET_MY_query_param_end
1475 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
1479 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1480 "mysql execute prepared", statement);
1481 return GNUNET_SYSERR;
1484 ret = fragment_row (stmt, cb, cb_cls);
1486 if (0 != mysql_stmt_reset (statement))
1488 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1489 "mysql_stmt_reset", statement);
1490 return GNUNET_SYSERR;
1497 * Retrieve the max. values of message counters for a channel.
1499 * @see GNUNET_PSYCSTORE_counters_get()
1501 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1504 counters_message_get (void *cls,
1505 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1506 uint64_t *max_fragment_id,
1507 uint64_t *max_message_id,
1508 uint64_t *max_group_generation)
1510 struct Plugin *plugin = cls;
1512 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_counters_message;
1513 MYSQL_STMT *statement = NULL;
1515 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1516 if (NULL == statement)
1518 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1519 "mysql get statement", statement);
1520 return GNUNET_SYSERR;
1523 int ret = GNUNET_SYSERR;
1525 struct GNUNET_MY_QueryParam params_select[] = {
1526 GNUNET_MY_query_param_auto_from_type (channel_key),
1527 GNUNET_MY_query_param_end
1530 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
1534 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1535 "mysql execute prepared", statement);
1536 return GNUNET_SYSERR;
1539 struct GNUNET_MY_ResultSpec results_select[] = {
1540 GNUNET_MY_result_spec_uint64 (max_fragment_id),
1541 GNUNET_MY_result_spec_uint64 (max_message_id),
1542 GNUNET_MY_result_spec_uint64 (max_group_generation),
1543 GNUNET_MY_result_spec_end
1546 ret = GNUNET_MY_extract_result (stmt,
1549 if (GNUNET_OK != ret)
1551 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1552 "mysql extract_result", statement);
1553 return GNUNET_SYSERR;
1556 if (0 != mysql_stmt_reset (statement))
1558 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1559 "mysql_stmt_reset", statement);
1560 return GNUNET_SYSERR;
1567 * Retrieve the max. values of state counters for a channel.
1569 * @see GNUNET_PSYCSTORE_counters_get()
1571 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1574 counters_state_get (void *cls,
1575 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1576 uint64_t *max_state_message_id)
1578 struct Plugin *plugin = cls;
1580 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_counters_state;
1581 MYSQL_STMT *statement = NULL;
1583 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1584 if (NULL == statement)
1586 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1587 "mysql get_stmt", statement);
1588 return GNUNET_SYSERR;
1591 int ret = GNUNET_SYSERR;
1593 struct GNUNET_MY_QueryParam params_select[] = {
1594 GNUNET_MY_query_param_auto_from_type (channel_key),
1595 GNUNET_MY_query_param_end
1598 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
1602 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1603 "mysql execute prepared", statement);
1604 return GNUNET_SYSERR;
1607 struct GNUNET_MY_ResultSpec results_select[] = {
1608 GNUNET_MY_result_spec_uint64 (max_state_message_id),
1609 GNUNET_MY_result_spec_end
1612 ret = GNUNET_MY_extract_result (stmt,
1615 if (GNUNET_OK != ret)
1617 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1618 "mysql extract_result", statement);
1619 return GNUNET_SYSERR;
1622 if (0 != mysql_stmt_reset (statement))
1624 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1625 "mysql_stmt_reset", statement);
1626 return GNUNET_SYSERR;
1634 * Assign a value to a state variable.
1636 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1639 state_assign (struct Plugin *plugin, struct GNUNET_MYSQL_StatementHandle *stmt,
1640 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1641 const char *name, const void *value, size_t value_size)
1643 int ret = GNUNET_SYSERR;
1645 MYSQL_STMT *statement = NULL;
1646 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1648 if (NULL == statement)
1650 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1651 "mysql get_stmt", statement);
1652 return GNUNET_SYSERR;
1655 struct GNUNET_MY_QueryParam params[] = {
1656 GNUNET_MY_query_param_auto_from_type (channel_key),
1657 GNUNET_MY_query_param_string (name),
1658 GNUNET_MY_query_param_auto_from_type (value_size),
1659 GNUNET_MY_query_param_end
1662 ret = GNUNET_MY_exec_prepared (plugin->mc,
1666 if (GNUNET_OK != ret)
1668 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1669 "mysql execute prepared", statement);
1670 return GNUNET_SYSERR;
1673 if (0 != mysql_stmt_reset (statement))
1675 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1676 "mysql_stmt_reset", statement);
1677 return GNUNET_SYSERR;
1685 update_message_id (struct Plugin *plugin, struct GNUNET_MYSQL_StatementHandle *stmt,
1686 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1687 uint64_t message_id)
1689 MYSQL_STMT *statement = NULL;
1690 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1692 if (NULL == statement)
1694 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1695 "mysql get_stmt", statement);
1696 return GNUNET_SYSERR;
1699 struct GNUNET_MY_QueryParam params[] = {
1700 GNUNET_MY_query_param_uint64 (&message_id),
1701 GNUNET_MY_query_param_auto_from_type (channel_key),
1702 GNUNET_MY_query_param_end
1705 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
1709 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1710 "mysql execute prepared", statement);
1711 return GNUNET_SYSERR;
1714 if (0 != mysql_stmt_reset (statement))
1716 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1717 "mysql_stmt_reset", statement);
1718 return GNUNET_SYSERR;
1726 * Begin modifying current state.
1729 state_modify_begin (void *cls,
1730 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1731 uint64_t message_id, uint64_t state_delta)
1733 struct Plugin *plugin = cls;
1735 if (state_delta > 0)
1738 * We can only apply state modifiers in the current message if modifiers in
1739 * the previous stateful message (message_id - state_delta) were already
1743 uint64_t max_state_message_id = 0;
1744 int ret = counters_state_get (plugin, channel_key, &max_state_message_id);
1748 case GNUNET_NO: // no state yet
1755 if (max_state_message_id < message_id - state_delta)
1756 return GNUNET_NO; /* some stateful messages not yet applied */
1757 else if (message_id - state_delta < max_state_message_id)
1758 return GNUNET_NO; /* changes already applied */
1761 if (TRANSACTION_NONE != plugin->transaction)
1763 /** @todo FIXME: wait for other transaction to finish */
1764 return GNUNET_SYSERR;
1766 return transaction_begin (plugin, TRANSACTION_STATE_MODIFY);
1771 * Set the current value of state variable.
1773 * @see GNUNET_PSYCSTORE_state_modify()
1775 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1778 state_modify_op (void *cls,
1779 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1780 enum GNUNET_PSYC_Operator op,
1781 const char *name, const void *value, size_t value_size)
1783 struct Plugin *plugin = cls;
1784 GNUNET_assert (TRANSACTION_STATE_MODIFY == plugin->transaction);
1788 case GNUNET_PSYC_OP_ASSIGN:
1789 return state_assign (plugin, plugin->insert_state_current, channel_key,
1790 name, value, value_size);
1792 default: /** @todo implement more state operations */
1794 return GNUNET_SYSERR;
1800 * End modifying current state.
1803 state_modify_end (void *cls,
1804 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1805 uint64_t message_id)
1807 struct Plugin *plugin = cls;
1808 GNUNET_assert (TRANSACTION_STATE_MODIFY == plugin->transaction);
1811 GNUNET_OK == exec_channel (plugin, plugin->delete_state_empty, channel_key)
1812 && GNUNET_OK == update_message_id (plugin,
1813 plugin->update_max_state_message_id,
1814 channel_key, message_id)
1815 && GNUNET_OK == transaction_commit (plugin)
1816 ? GNUNET_OK : GNUNET_SYSERR;
1821 * Begin state synchronization.
1824 state_sync_begin (void *cls,
1825 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
1827 struct Plugin *plugin = cls;
1828 return exec_channel (plugin, plugin->delete_state_sync, channel_key);
1833 * Assign current value of a state variable.
1835 * @see GNUNET_PSYCSTORE_state_modify()
1837 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1840 state_sync_assign (void *cls,
1841 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1842 const char *name, const void *value, size_t value_size)
1844 struct Plugin *plugin = cls;
1845 return state_assign (cls, plugin->insert_state_sync, channel_key,
1846 name, value, value_size);
1851 * End modifying current state.
1854 state_sync_end (void *cls,
1855 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1856 uint64_t max_state_message_id,
1857 uint64_t state_hash_message_id)
1859 struct Plugin *plugin = cls;
1860 int ret = GNUNET_SYSERR;
1862 if (TRANSACTION_NONE != plugin->transaction)
1864 /** @todo FIXME: wait for other transaction to finish */
1865 return GNUNET_SYSERR;
1868 GNUNET_OK == transaction_begin (plugin, TRANSACTION_STATE_SYNC)
1869 && GNUNET_OK == exec_channel (plugin, plugin->delete_state, channel_key)
1870 && GNUNET_OK == exec_channel (plugin, plugin->insert_state_from_sync,
1872 && GNUNET_OK == exec_channel (plugin, plugin->delete_state_sync,
1874 && GNUNET_OK == update_message_id (plugin,
1875 plugin->update_state_hash_message_id,
1876 channel_key, state_hash_message_id)
1877 && GNUNET_OK == update_message_id (plugin,
1878 plugin->update_max_state_message_id,
1879 channel_key, max_state_message_id)
1880 && GNUNET_OK == transaction_commit (plugin)
1882 : transaction_rollback (plugin);
1888 * Delete the whole state.
1890 * @see GNUNET_PSYCSTORE_state_reset()
1892 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1895 state_reset (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
1897 struct Plugin *plugin = cls;
1898 return exec_channel (plugin, plugin->delete_state, channel_key);
1903 * Update signed values of state variables in the state store.
1905 * @see GNUNET_PSYCSTORE_state_hash_update()
1907 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1910 state_update_signed (void *cls,
1911 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
1913 struct Plugin *plugin = cls;
1914 return exec_channel (plugin, plugin->update_state_signed, channel_key);
1919 * Retrieve a state variable by name.
1921 * @see GNUNET_PSYCSTORE_state_get()
1923 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1926 state_get (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1927 const char *name, GNUNET_PSYCSTORE_StateCallback cb, void *cb_cls)
1929 struct Plugin *plugin = cls;
1930 int ret = GNUNET_SYSERR;
1932 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_state_one;
1933 MYSQL_STMT *statement = NULL;
1935 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1937 struct GNUNET_MY_QueryParam params_select[] = {
1938 GNUNET_MY_query_param_auto_from_type (channel_key),
1939 GNUNET_MY_query_param_string (name),
1940 GNUNET_MY_query_param_end
1943 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
1950 ret = cb (cb_cls, name, sqlite3_column_blob (stmt, 0),
1951 sqlite3_column_bytes (stmt, 0));
1953 if (0 != mysql_stmt_reset (statement))
1955 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1956 "mysql_stmt_reset", statement);
1957 return GNUNET_SYSERR;
1965 * Retrieve all state variables for a channel with the given prefix.
1967 * @see GNUNET_PSYCSTORE_state_get_prefix()
1969 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1972 state_get_prefix (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1973 const char *name, GNUNET_PSYCSTORE_StateCallback cb,
1976 struct Plugin *plugin = cls;
1977 int ret = GNUNET_SYSERR;
1979 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_state_prefix;
1980 MYSQL_STMT *statement = NULL;
1981 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
1983 if (NULL == statement)
1985 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1986 "mysql get_stmt", statement);
1987 return GNUNET_SYSERR;
1990 uint32_t name_len = (uint32_t) strlen (name);
1992 struct GNUNET_MY_QueryParam params_select[] = {
1993 GNUNET_MY_query_param_auto_from_type (channel_key),
1994 GNUNET_MY_query_param_string (name),
1995 GNUNET_MY_query_param_uint32 (&name_len),
1996 GNUNET_MY_query_param_string (name),
1997 GNUNET_MY_query_param_end
2002 sql_ret = GNUNET_MY_exec_prepared (plugin->mc,
2006 if (GNUNET_OK != sql_ret)
2008 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
2009 "mysql exec_prepared", statement);
2010 return GNUNET_SYSERR;
2013 ret = cb (cb_cls, (const char *) sqlite3_column_text (stmt, 0),
2014 sqlite3_column_blob (stmt, 1),
2015 sqlite3_column_bytes (stmt, 1));
2017 if (0 != mysql_stmt_reset (statement))
2019 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
2020 "mysql_stmt_reset", statement);
2021 return GNUNET_SYSERR;
2029 * Retrieve all signed state variables for a channel.
2031 * @see GNUNET_PSYCSTORE_state_get_signed()
2033 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
2036 state_get_signed (void *cls,
2037 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
2038 GNUNET_PSYCSTORE_StateCallback cb, void *cb_cls)
2040 struct Plugin *plugin = cls;
2041 int ret = GNUNET_SYSERR;
2043 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_state_signed;
2044 MYSQL_STMT *statement = NULL;
2046 statement = GNUNET_MYSQL_statement_get_stmt (stmt);
2047 if (NULL == statement)
2049 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
2050 "mysql get_stmt", statement);
2051 return GNUNET_SYSERR;
2054 struct GNUNET_MY_QueryParam params_select[] = {
2055 GNUNET_MY_query_param_auto_from_type (channel_key),
2056 GNUNET_MY_query_param_end
2061 sql_ret = GNUNET_MY_exec_prepared (plugin->mc,
2065 if (GNUNET_OK != sql_ret)
2067 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
2068 "mysql_exec_prepared", statement);
2069 return GNUNET_SYSERR;
2072 ret = cb (cb_cls, (const char *) sqlite3_column_text (stmt, 0),
2073 sqlite3_column_blob (stmt, 1),
2074 sqlite3_column_bytes (stmt, 1));
2076 if (0 != mysql_stmt_reset (statement))
2078 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
2079 "mysql_stmt_reset", statement);
2080 return GNUNET_SYSERR;
2088 * Entry point for the plugin.
2090 * @param cls The struct GNUNET_CONFIGURATION_Handle.
2091 * @return NULL on error, otherwise the plugin context
2094 libgnunet_plugin_psycstore_sqlite_init (void *cls)
2096 static struct Plugin plugin;
2097 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
2098 struct GNUNET_PSYCSTORE_PluginFunctions *api;
2100 if (NULL != plugin.cfg)
2101 return NULL; /* can only initialize once! */
2102 memset (&plugin, 0, sizeof (struct Plugin));
2104 if (GNUNET_OK != database_setup (&plugin))
2106 database_shutdown (&plugin);
2109 api = GNUNET_new (struct GNUNET_PSYCSTORE_PluginFunctions);
2111 api->membership_store = &sqlite_membership_store;
2112 api->membership_test = &membership_test;
2113 api->fragment_store = &fragment_store;
2114 api->message_add_flags = &message_add_flags;
2115 api->fragment_get = &fragment_get;
2116 api->fragment_get_latest = &fragment_get_latest;
2117 api->message_get = &message_get;
2118 api->message_get_latest = &message_get_latest;
2119 api->message_get_fragment = &message_get_fragment;
2120 api->counters_message_get = &counters_message_get;
2121 api->counters_state_get = &counters_state_get;
2122 api->state_modify_begin = &state_modify_begin;
2123 api->state_modify_op = &state_modify_op;
2124 api->state_modify_end = &state_modify_end;
2125 api->state_sync_begin = &state_sync_begin;
2126 api->state_sync_assign = &state_sync_assign;
2127 api->state_sync_end = &state_sync_end;
2128 api->state_reset = &state_reset;
2129 api->state_update_signed = &state_update_signed;
2130 api->state_get = &state_get;
2131 api->state_get_prefix = &state_get_prefix;
2132 api->state_get_signed = &state_get_signed;
2134 LOG (GNUNET_ERROR_TYPE_INFO, _("SQLite database running\n"));
2140 * Exit point from the plugin.
2142 * @param cls The plugin context (as returned by "init")
2143 * @return Always NULL
2146 libgnunet_plugin_psycstore_sqlite_done (void *cls)
2148 struct GNUNET_PSYCSTORE_PluginFunctions *api = cls;
2149 struct Plugin *plugin = api->cls;
2151 database_shutdown (plugin);
2154 LOG (GNUNET_ERROR_TYPE_DEBUG, "SQLite plugin is finished\n");
2158 /* end of plugin_psycstore_mysql.c */