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_sqlite.c
23 * @brief sqlite-based psycstore backend
24 * @author Gabor X Toth
25 * @author Christian Grothoff
29 * FIXME: SQLite3 only supports signed 64-bit integers natively,
30 * thus it can only store 63 bits of the uint64_t's.
34 #include "gnunet_psycstore_plugin.h"
35 #include "gnunet_psycstore_service.h"
36 #include "gnunet_multicast_service.h"
37 #include "gnunet_crypto_lib.h"
38 #include "gnunet_psyc_util_lib.h"
39 #include "psycstore.h"
43 * After how many ms "busy" should a DB operation fail for good? A
44 * low value makes sure that we are more responsive to requests
45 * (especially PUTs). A high value guarantees a higher success rate
46 * (SELECTs in iterate can take several seconds despite LIMIT=1).
48 * The default value of 1s should ensure that users do not experience
49 * huge latencies while at the same time allowing operations to
50 * succeed with reasonable probability.
52 #define BUSY_TIMEOUT_MS 1000
54 #define DEBUG_PSYCSTORE GNUNET_EXTRA_LOGGING
57 * Log an error message at log-level 'level' that indicates
58 * a failure of the command 'cmd' on file 'filename'
59 * with the message given by strerror(errno).
61 #define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, "psycstore-sqlite", _("`%s' failed at %s:%d with error: %s (%d)\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh), sqlite3_errcode(db->dbh)); } while(0)
63 #define LOG(kind,...) GNUNET_log_from (kind, "psycstore-sqlite", __VA_ARGS__)
67 TRANSACTION_STATE_MODIFY,
68 TRANSACTION_STATE_SYNC,
72 * Context for all functions in this plugin.
77 const struct GNUNET_CONFIGURATION_Handle *cfg;
85 * Native SQLite database handle.
90 * Current transaction.
92 enum Transactions transaction;
94 sqlite3_stmt *transaction_begin;
96 sqlite3_stmt *transaction_commit;
98 sqlite3_stmt *transaction_rollback;
101 * Precompiled SQL for channel_key_store()
103 sqlite3_stmt *insert_channel_key;
106 * Precompiled SQL for slave_key_store()
108 sqlite3_stmt *insert_slave_key;
112 * Precompiled SQL for membership_store()
114 sqlite3_stmt *insert_membership;
117 * Precompiled SQL for membership_test()
119 sqlite3_stmt *select_membership;
123 * Precompiled SQL for fragment_store()
125 sqlite3_stmt *insert_fragment;
128 * Precompiled SQL for message_add_flags()
130 sqlite3_stmt *update_message_flags;
133 * Precompiled SQL for fragment_get()
135 sqlite3_stmt *select_fragments;
138 * Precompiled SQL for fragment_get()
140 sqlite3_stmt *select_latest_fragments;
143 * Precompiled SQL for message_get()
145 sqlite3_stmt *select_messages;
148 * Precompiled SQL for message_get()
150 sqlite3_stmt *select_latest_messages;
153 * Precompiled SQL for message_get_fragment()
155 sqlite3_stmt *select_message_fragment;
158 * Precompiled SQL for counters_get_message()
160 sqlite3_stmt *select_counters_message;
163 * Precompiled SQL for counters_get_state()
165 sqlite3_stmt *select_counters_state;
168 * Precompiled SQL for state_modify_end()
170 sqlite3_stmt *update_state_hash_message_id;
173 * Precompiled SQL for state_sync_end()
175 sqlite3_stmt *update_max_state_message_id;
178 * Precompiled SQL for state_modify_op()
180 sqlite3_stmt *insert_state_current;
183 * Precompiled SQL for state_modify_end()
185 sqlite3_stmt *delete_state_empty;
188 * Precompiled SQL for state_set_signed()
190 sqlite3_stmt *update_state_signed;
193 * Precompiled SQL for state_sync()
195 sqlite3_stmt *insert_state_sync;
198 * Precompiled SQL for state_sync()
200 sqlite3_stmt *delete_state;
203 * Precompiled SQL for state_sync()
205 sqlite3_stmt *insert_state_from_sync;
208 * Precompiled SQL for state_sync()
210 sqlite3_stmt *delete_state_sync;
213 * Precompiled SQL for state_get_signed()
215 sqlite3_stmt *select_state_signed;
218 * Precompiled SQL for state_get()
220 sqlite3_stmt *select_state_one;
223 * Precompiled SQL for state_get_prefix()
225 sqlite3_stmt *select_state_prefix;
232 sql_trace (void *cls, const char *sql)
234 LOG (GNUNET_ERROR_TYPE_DEBUG, "SQL query:\n%s\n", sql);
240 * @brief Prepare a SQL statement
242 * @param dbh handle to the database
243 * @param sql SQL statement, UTF-8 encoded
244 * @param stmt set to the prepared statement
245 * @return 0 on success
248 sql_prepare (sqlite3 *dbh, const char *sql, sqlite3_stmt **stmt)
253 result = sqlite3_prepare_v2 (dbh, sql, strlen (sql), stmt,
254 (const char **) &tail);
255 LOG (GNUNET_ERROR_TYPE_DEBUG,
256 "Prepared `%s' / %p: %d\n", sql, *stmt, result);
257 if (result != SQLITE_OK)
258 LOG (GNUNET_ERROR_TYPE_ERROR,
259 _("Error preparing SQL query: %s\n %s\n"),
260 sqlite3_errmsg (dbh), sql);
266 * @brief Prepare a SQL statement
268 * @param dbh handle to the database
269 * @param sql SQL statement, UTF-8 encoded
270 * @return 0 on success
273 sql_exec (sqlite3 *dbh, const char *sql)
277 result = sqlite3_exec (dbh, sql, NULL, NULL, NULL);
278 LOG (GNUNET_ERROR_TYPE_DEBUG,
279 "Executed `%s' / %d\n", sql, result);
280 if (result != SQLITE_OK)
281 LOG (GNUNET_ERROR_TYPE_ERROR,
282 _("Error executing SQL query: %s\n %s\n"),
283 sqlite3_errmsg (dbh), sql);
289 * Initialize the database connections and associated
290 * data structures (create tables and indices
291 * as needed as well).
293 * @param plugin the plugin context (state for this module)
294 * @return GNUNET_OK on success
297 database_setup (struct Plugin *plugin)
302 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "psycstore-sqlite",
303 "FILENAME", &filename))
305 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
306 "psycstore-sqlite", "FILENAME");
307 return GNUNET_SYSERR;
309 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
311 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (filename))
314 GNUNET_free (filename);
315 return GNUNET_SYSERR;
318 /* filename should be UTF-8-encoded. If it isn't, it's a bug */
319 plugin->fn = filename;
321 /* Open database and precompile statements */
322 if (SQLITE_OK != sqlite3_open (plugin->fn, &plugin->dbh))
324 LOG (GNUNET_ERROR_TYPE_ERROR,
325 _("Unable to initialize SQLite: %s.\n"),
326 sqlite3_errmsg (plugin->dbh));
327 return GNUNET_SYSERR;
331 sqlite3_trace (plugin->dbh, &sql_trace, NULL);
334 sql_exec (plugin->dbh, "PRAGMA temp_store=MEMORY");
335 sql_exec (plugin->dbh, "PRAGMA synchronous=NORMAL");
336 sql_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF");
337 sql_exec (plugin->dbh, "PRAGMA auto_vacuum=INCREMENTAL");
338 sql_exec (plugin->dbh, "PRAGMA encoding=\"UTF-8\"");
339 #if ! DEBUG_PSYCSTORE
340 sql_exec (plugin->dbh, "PRAGMA locking_mode=EXCLUSIVE");
342 sql_exec (plugin->dbh, "PRAGMA page_size=4096");
344 sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS);
348 sql_exec (plugin->dbh,
349 "CREATE TABLE IF NOT EXISTS channels (\n"
350 " id INTEGER PRIMARY KEY,\n"
351 " pub_key BLOB UNIQUE,\n"
352 " max_state_message_id INTEGER,\n" // last applied state message ID
353 " state_hash_message_id INTEGER\n" // last message ID with a state hash
356 sql_exec (plugin->dbh,
357 "CREATE TABLE IF NOT EXISTS slaves (\n"
358 " id INTEGER PRIMARY KEY,\n"
359 " pub_key BLOB UNIQUE\n"
362 sql_exec (plugin->dbh,
363 "CREATE TABLE IF NOT EXISTS membership (\n"
364 " channel_id INTEGER NOT NULL REFERENCES channels(id),\n"
365 " slave_id INTEGER NOT NULL REFERENCES slaves(id),\n"
366 " did_join INTEGER NOT NULL,\n"
367 " announced_at INTEGER NOT NULL,\n"
368 " effective_since INTEGER NOT NULL,\n"
369 " group_generation INTEGER NOT NULL\n"
371 sql_exec (plugin->dbh,
372 "CREATE INDEX IF NOT EXISTS idx_membership_channel_id_slave_id "
373 "ON membership (channel_id, slave_id);");
375 /** @todo messages table: add method_name column */
376 sql_exec (plugin->dbh,
377 "CREATE TABLE IF NOT EXISTS messages (\n"
378 " channel_id INTEGER NOT NULL REFERENCES channels(id),\n"
379 " hop_counter INTEGER NOT NULL,\n"
382 " fragment_id INTEGER NOT NULL,\n"
383 " fragment_offset INTEGER NOT NULL,\n"
384 " message_id INTEGER NOT NULL,\n"
385 " group_generation INTEGER NOT NULL,\n"
386 " multicast_flags INTEGER NOT NULL,\n"
387 " psycstore_flags INTEGER NOT NULL,\n"
389 " PRIMARY KEY (channel_id, fragment_id),\n"
390 " UNIQUE (channel_id, message_id, fragment_offset)\n"
393 sql_exec (plugin->dbh,
394 "CREATE TABLE IF NOT EXISTS state (\n"
395 " channel_id INTEGER NOT NULL REFERENCES channels(id),\n"
396 " name TEXT NOT NULL,\n"
397 " value_current BLOB,\n"
398 " value_signed BLOB,\n"
399 " PRIMARY KEY (channel_id, name)\n"
402 sql_exec (plugin->dbh,
403 "CREATE TABLE IF NOT EXISTS state_sync (\n"
404 " channel_id INTEGER NOT NULL REFERENCES channels(id),\n"
405 " name TEXT NOT NULL,\n"
407 " PRIMARY KEY (channel_id, name)\n"
410 /* Prepare statements */
412 sql_prepare (plugin->dbh, "BEGIN;", &plugin->transaction_begin);
414 sql_prepare (plugin->dbh, "COMMIT;", &plugin->transaction_commit);
416 sql_prepare (plugin->dbh, "ROLLBACK;", &plugin->transaction_rollback);
418 sql_prepare (plugin->dbh,
419 "INSERT OR IGNORE INTO channels (pub_key) VALUES (?);",
420 &plugin->insert_channel_key);
422 sql_prepare (plugin->dbh,
423 "INSERT OR IGNORE INTO slaves (pub_key) VALUES (?);",
424 &plugin->insert_slave_key);
426 sql_prepare (plugin->dbh,
427 "INSERT INTO membership\n"
428 " (channel_id, slave_id, did_join, announced_at,\n"
429 " effective_since, group_generation)\n"
430 "VALUES ((SELECT id FROM channels WHERE pub_key = ?),\n"
431 " (SELECT id FROM slaves WHERE pub_key = ?),\n"
433 &plugin->insert_membership);
435 sql_prepare (plugin->dbh,
436 "SELECT did_join FROM membership\n"
437 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
438 " AND slave_id = (SELECT id FROM slaves WHERE pub_key = ?)\n"
439 " AND effective_since <= ? AND did_join = 1\n"
440 "ORDER BY announced_at DESC LIMIT 1;",
441 &plugin->select_membership);
443 sql_prepare (plugin->dbh,
444 "INSERT OR IGNORE INTO messages\n"
445 " (channel_id, hop_counter, signature, purpose,\n"
446 " fragment_id, fragment_offset, message_id,\n"
447 " group_generation, multicast_flags, psycstore_flags, data)\n"
448 "VALUES ((SELECT id FROM channels WHERE pub_key = ?),\n"
449 " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
450 &plugin->insert_fragment);
452 sql_prepare (plugin->dbh,
454 "SET psycstore_flags = psycstore_flags | ?\n"
455 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
456 " AND message_id = ? AND fragment_offset = 0;",
457 &plugin->update_message_flags);
459 sql_prepare (plugin->dbh,
460 "SELECT hop_counter, signature, purpose, fragment_id,\n"
461 " fragment_offset, message_id, group_generation,\n"
462 " multicast_flags, psycstore_flags, data\n"
464 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
465 " AND ? <= fragment_id AND fragment_id <= ?;",
466 &plugin->select_fragments);
468 /** @todo select_messages: add method_prefix filter */
469 sql_prepare (plugin->dbh,
470 "SELECT hop_counter, signature, purpose, fragment_id,\n"
471 " fragment_offset, message_id, group_generation,\n"
472 " multicast_flags, psycstore_flags, data\n"
474 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
475 " AND ? <= message_id AND message_id <= ?"
477 &plugin->select_messages);
479 sql_prepare (plugin->dbh,
481 "(SELECT hop_counter, signature, purpose, fragment_id,\n"
482 " fragment_offset, message_id, group_generation,\n"
483 " multicast_flags, psycstore_flags, data\n"
485 " WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
486 " ORDER BY fragment_id DESC\n"
488 "ORDER BY fragment_id;",
489 &plugin->select_latest_fragments);
491 /** @todo select_latest_messages: add method_prefix filter */
492 sql_prepare (plugin->dbh,
493 "SELECT hop_counter, signature, purpose, fragment_id,\n"
494 " fragment_offset, message_id, group_generation,\n"
495 " multicast_flags, psycstore_flags, data\n"
497 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
498 " AND message_id IN\n"
499 " (SELECT message_id\n"
501 " WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
502 " GROUP BY message_id\n"
503 " ORDER BY message_id\n"
505 "ORDER BY fragment_id;",
506 &plugin->select_latest_messages);
508 sql_prepare (plugin->dbh,
509 "SELECT hop_counter, signature, purpose, fragment_id,\n"
510 " fragment_offset, message_id, group_generation,\n"
511 " multicast_flags, psycstore_flags, data\n"
513 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
514 " AND message_id = ? AND fragment_offset = ?;",
515 &plugin->select_message_fragment);
517 sql_prepare (plugin->dbh,
518 "SELECT fragment_id, message_id, group_generation\n"
520 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
521 "ORDER BY fragment_id DESC LIMIT 1;",
522 &plugin->select_counters_message);
524 sql_prepare (plugin->dbh,
525 "SELECT max_state_message_id\n"
527 "WHERE pub_key = ? AND max_state_message_id IS NOT NULL;",
528 &plugin->select_counters_state);
530 sql_prepare (plugin->dbh,
532 "SET max_state_message_id = ?\n"
533 "WHERE pub_key = ?;",
534 &plugin->update_max_state_message_id);
536 sql_prepare (plugin->dbh,
538 "SET state_hash_message_id = ?\n"
539 "WHERE pub_key = ?;",
540 &plugin->update_state_hash_message_id);
542 sql_prepare (plugin->dbh,
543 "INSERT OR REPLACE INTO state\n"
544 " (channel_id, name, value_current, value_signed)\n"
545 "SELECT new.channel_id, new.name,\n"
546 " new.value_current, old.value_signed\n"
547 "FROM (SELECT (SELECT id FROM channels WHERE pub_key = ?)\n"
549 " ? AS name, ? AS value_current) AS new\n"
550 "LEFT JOIN (SELECT channel_id, name, value_signed\n"
551 " FROM state) AS old\n"
552 "ON new.channel_id = old.channel_id AND new.name = old.name;",
553 &plugin->insert_state_current);
555 sql_prepare (plugin->dbh,
556 "DELETE FROM state\n"
557 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
558 " AND (value_current IS NULL OR length(value_current) = 0)\n"
559 " AND (value_signed IS NULL OR length(value_signed) = 0);",
560 &plugin->delete_state_empty);
562 sql_prepare (plugin->dbh,
564 "SET value_signed = value_current\n"
565 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);",
566 &plugin->update_state_signed);
568 sql_prepare (plugin->dbh,
569 "DELETE FROM state\n"
570 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);",
571 &plugin->delete_state);
573 sql_prepare (plugin->dbh,
574 "INSERT INTO state_sync (channel_id, name, value)\n"
575 "VALUES ((SELECT id FROM channels WHERE pub_key = ?), ?, ?);",
576 &plugin->insert_state_sync);
578 sql_prepare (plugin->dbh,
579 "INSERT INTO state\n"
580 " (channel_id, name, value_current, value_signed)\n"
581 "SELECT channel_id, name, value, value\n"
583 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);",
584 &plugin->insert_state_from_sync);
586 sql_prepare (plugin->dbh,
587 "DELETE FROM state_sync\n"
588 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);",
589 &plugin->delete_state_sync);
591 sql_prepare (plugin->dbh,
592 "SELECT value_current\n"
594 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
596 &plugin->select_state_one);
598 sql_prepare (plugin->dbh,
599 "SELECT name, value_current\n"
601 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
602 " AND (name = ? OR substr(name, 1, ?) = ? || '_');",
603 &plugin->select_state_prefix);
605 sql_prepare (plugin->dbh,
606 "SELECT name, value_signed\n"
608 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)"
609 " AND value_signed IS NOT NULL;",
610 &plugin->select_state_signed);
617 * Shutdown database connection and associate data
619 * @param plugin the plugin context (state for this module)
622 database_shutdown (struct Plugin *plugin)
626 while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh, NULL)))
628 result = sqlite3_finalize (stmt);
629 if (SQLITE_OK != result)
630 LOG (GNUNET_ERROR_TYPE_WARNING,
631 "Failed to close statement %p: %d\n", stmt, result);
633 if (SQLITE_OK != sqlite3_close (plugin->dbh))
634 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
636 GNUNET_free_non_null (plugin->fn);
640 * Execute a prepared statement with a @a channel_key argument.
642 * @param plugin Plugin handle.
643 * @param stmt Statement to execute.
644 * @param channel_key Public key of the channel.
646 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
649 exec_channel (struct Plugin *plugin, sqlite3_stmt *stmt,
650 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
652 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
653 sizeof (*channel_key), SQLITE_STATIC))
655 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
658 else if (SQLITE_DONE != sqlite3_step (stmt))
660 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
664 if (SQLITE_OK != sqlite3_reset (stmt))
666 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
668 return GNUNET_SYSERR;
675 * Begin a transaction.
678 transaction_begin (struct Plugin *plugin, enum Transactions transaction)
680 sqlite3_stmt *stmt = plugin->transaction_begin;
682 if (SQLITE_DONE != sqlite3_step (stmt))
684 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
687 if (SQLITE_OK != sqlite3_reset (stmt))
689 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
691 return GNUNET_SYSERR;
694 plugin->transaction = transaction;
700 * Commit current transaction.
703 transaction_commit (struct Plugin *plugin)
705 sqlite3_stmt *stmt = plugin->transaction_commit;
707 if (SQLITE_DONE != sqlite3_step (stmt))
709 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
712 if (SQLITE_OK != sqlite3_reset (stmt))
714 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
716 return GNUNET_SYSERR;
719 plugin->transaction = TRANSACTION_NONE;
725 * Roll back current transaction.
728 transaction_rollback (struct Plugin *plugin)
730 sqlite3_stmt *stmt = plugin->transaction_rollback;
732 if (SQLITE_DONE != sqlite3_step (stmt))
734 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
737 if (SQLITE_OK != sqlite3_reset (stmt))
739 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
741 return GNUNET_SYSERR;
743 plugin->transaction = TRANSACTION_NONE;
749 channel_key_store (struct Plugin *plugin,
750 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
752 sqlite3_stmt *stmt = plugin->insert_channel_key;
754 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
755 sizeof (*channel_key), SQLITE_STATIC))
757 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
760 else if (SQLITE_DONE != sqlite3_step (stmt))
762 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
766 if (SQLITE_OK != sqlite3_reset (stmt))
768 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
770 return GNUNET_SYSERR;
778 slave_key_store (struct Plugin *plugin,
779 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key)
781 sqlite3_stmt *stmt = plugin->insert_slave_key;
783 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, slave_key,
784 sizeof (*slave_key), SQLITE_STATIC))
786 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
789 else if (SQLITE_DONE != sqlite3_step (stmt))
791 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
796 if (SQLITE_OK != sqlite3_reset (stmt))
798 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
800 return GNUNET_SYSERR;
808 * Store join/leave events for a PSYC channel in order to be able to answer
809 * membership test queries later.
811 * @see GNUNET_PSYCSTORE_membership_store()
813 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
816 sqlite_membership_store (void *cls,
817 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
818 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
820 uint64_t announced_at,
821 uint64_t effective_since,
822 uint64_t group_generation)
824 struct Plugin *plugin = cls;
825 sqlite3_stmt *stmt = plugin->insert_membership;
827 GNUNET_assert (TRANSACTION_NONE == plugin->transaction);
829 if (announced_at > INT64_MAX ||
830 effective_since > INT64_MAX ||
831 group_generation > INT64_MAX)
834 return GNUNET_SYSERR;
837 if (GNUNET_OK != channel_key_store (plugin, channel_key)
838 || GNUNET_OK != slave_key_store (plugin, slave_key))
839 return GNUNET_SYSERR;
841 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
842 sizeof (*channel_key), SQLITE_STATIC)
843 || SQLITE_OK != sqlite3_bind_blob (stmt, 2, slave_key,
844 sizeof (*slave_key), SQLITE_STATIC)
845 || SQLITE_OK != sqlite3_bind_int (stmt, 3, did_join)
846 || SQLITE_OK != sqlite3_bind_int64 (stmt, 4, announced_at)
847 || SQLITE_OK != sqlite3_bind_int64 (stmt, 5, effective_since)
848 || SQLITE_OK != sqlite3_bind_int64 (stmt, 6, group_generation))
850 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
853 else if (SQLITE_DONE != sqlite3_step (stmt))
855 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
859 if (SQLITE_OK != sqlite3_reset (stmt))
861 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
863 return GNUNET_SYSERR;
870 * Test if a member was admitted to the channel at the given message ID.
872 * @see GNUNET_PSYCSTORE_membership_test()
874 * @return #GNUNET_YES if the member was admitted, #GNUNET_NO if not,
875 * #GNUNET_SYSERR if there was en error.
878 membership_test (void *cls,
879 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
880 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
883 struct Plugin *plugin = cls;
884 sqlite3_stmt *stmt = plugin->select_membership;
885 int ret = GNUNET_SYSERR;
887 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
888 sizeof (*channel_key), SQLITE_STATIC)
889 || SQLITE_OK != sqlite3_bind_blob (stmt, 2, slave_key,
890 sizeof (*slave_key), SQLITE_STATIC)
891 || SQLITE_OK != sqlite3_bind_int64 (stmt, 3, message_id))
893 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
898 switch (sqlite3_step (stmt))
908 if (SQLITE_OK != sqlite3_reset (stmt))
910 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
918 * Store a message fragment sent to a channel.
920 * @see GNUNET_PSYCSTORE_fragment_store()
922 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
925 fragment_store (void *cls,
926 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
927 const struct GNUNET_MULTICAST_MessageHeader *msg,
928 uint32_t psycstore_flags)
930 struct Plugin *plugin = cls;
931 sqlite3_stmt *stmt = plugin->insert_fragment;
933 GNUNET_assert (TRANSACTION_NONE == plugin->transaction);
935 uint64_t fragment_id = GNUNET_ntohll (msg->fragment_id);
936 uint64_t fragment_offset = GNUNET_ntohll (msg->fragment_offset);
937 uint64_t message_id = GNUNET_ntohll (msg->message_id);
938 uint64_t group_generation = GNUNET_ntohll (msg->group_generation);
940 if (fragment_id > INT64_MAX || fragment_offset > INT64_MAX ||
941 message_id > INT64_MAX || group_generation > INT64_MAX)
943 LOG (GNUNET_ERROR_TYPE_ERROR,
944 "Tried to store fragment with a field > INT64_MAX: "
945 "%lu, %lu, %lu, %lu\n", fragment_id, fragment_offset,
946 message_id, group_generation);
948 return GNUNET_SYSERR;
951 if (GNUNET_OK != channel_key_store (plugin, channel_key))
952 return GNUNET_SYSERR;
954 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
955 sizeof (*channel_key), SQLITE_STATIC)
956 || SQLITE_OK != sqlite3_bind_int64 (stmt, 2, ntohl (msg->hop_counter) )
957 || SQLITE_OK != sqlite3_bind_blob (stmt, 3, (const void *) &msg->signature,
958 sizeof (msg->signature), SQLITE_STATIC)
959 || SQLITE_OK != sqlite3_bind_blob (stmt, 4, (const void *) &msg->purpose,
960 sizeof (msg->purpose), SQLITE_STATIC)
961 || SQLITE_OK != sqlite3_bind_int64 (stmt, 5, fragment_id)
962 || SQLITE_OK != sqlite3_bind_int64 (stmt, 6, fragment_offset)
963 || SQLITE_OK != sqlite3_bind_int64 (stmt, 7, message_id)
964 || SQLITE_OK != sqlite3_bind_int64 (stmt, 8, group_generation)
965 || SQLITE_OK != sqlite3_bind_int64 (stmt, 9, ntohl (msg->flags))
966 || SQLITE_OK != sqlite3_bind_int64 (stmt, 10, psycstore_flags)
967 || SQLITE_OK != sqlite3_bind_blob (stmt, 11, (const void *) &msg[1],
968 ntohs (msg->header.size)
969 - sizeof (*msg), SQLITE_STATIC))
971 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
974 else if (SQLITE_DONE != sqlite3_step (stmt))
976 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
980 if (SQLITE_OK != sqlite3_reset (stmt))
982 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
984 return GNUNET_SYSERR;
991 * Set additional flags for a given message.
993 * They are OR'd with any existing flags set.
995 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
998 message_add_flags (void *cls,
999 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1000 uint64_t message_id,
1001 uint64_t psycstore_flags)
1003 struct Plugin *plugin = cls;
1004 sqlite3_stmt *stmt = plugin->update_message_flags;
1005 int ret = GNUNET_SYSERR;
1007 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, psycstore_flags)
1008 || SQLITE_OK != sqlite3_bind_blob (stmt, 2, channel_key,
1009 sizeof (*channel_key), SQLITE_STATIC)
1010 || SQLITE_OK != sqlite3_bind_int64 (stmt, 3, message_id))
1012 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1017 switch (sqlite3_step (stmt))
1020 ret = sqlite3_total_changes (plugin->dbh) > 0 ? GNUNET_OK : GNUNET_NO;
1023 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1028 if (SQLITE_OK != sqlite3_reset (stmt))
1030 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1032 return GNUNET_SYSERR;
1039 fragment_row (sqlite3_stmt *stmt, GNUNET_PSYCSTORE_FragmentCallback cb,
1042 int data_size = sqlite3_column_bytes (stmt, 9);
1043 struct GNUNET_MULTICAST_MessageHeader *msg
1044 = GNUNET_malloc (sizeof (*msg) + data_size);
1046 msg->header.size = htons (sizeof (*msg) + data_size);
1047 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
1048 msg->hop_counter = htonl ((uint32_t) sqlite3_column_int64 (stmt, 0));
1049 memcpy (&msg->signature,
1050 sqlite3_column_blob (stmt, 1),
1051 sqlite3_column_bytes (stmt, 1));
1052 memcpy (&msg->purpose,
1053 sqlite3_column_blob (stmt, 2),
1054 sqlite3_column_bytes (stmt, 2));
1055 msg->fragment_id = GNUNET_htonll (sqlite3_column_int64 (stmt, 3));
1056 msg->fragment_offset = GNUNET_htonll (sqlite3_column_int64 (stmt, 4));
1057 msg->message_id = GNUNET_htonll (sqlite3_column_int64 (stmt, 5));
1058 msg->group_generation = GNUNET_htonll (sqlite3_column_int64 (stmt, 6));
1059 msg->flags = htonl (sqlite3_column_int64 (stmt, 7));
1060 memcpy (&msg[1], sqlite3_column_blob (stmt, 9), data_size);
1062 return cb (cb_cls, (void *) msg, sqlite3_column_int64 (stmt, 8));
1067 fragment_select (struct Plugin *plugin, sqlite3_stmt *stmt,
1068 uint64_t *returned_fragments,
1069 GNUNET_PSYCSTORE_FragmentCallback cb, void *cb_cls)
1071 int ret = GNUNET_SYSERR;
1076 sql_ret = sqlite3_step (stmt);
1080 if (ret != GNUNET_OK)
1084 ret = fragment_row (stmt, cb, cb_cls);
1085 (*returned_fragments)++;
1086 if (ret != GNUNET_YES)
1087 sql_ret = SQLITE_DONE;
1090 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1094 while (sql_ret == SQLITE_ROW);
1100 * Retrieve a message fragment range by fragment ID.
1102 * @see GNUNET_PSYCSTORE_fragment_get()
1104 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1107 fragment_get (void *cls,
1108 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1109 uint64_t first_fragment_id,
1110 uint64_t last_fragment_id,
1111 uint64_t *returned_fragments,
1112 GNUNET_PSYCSTORE_FragmentCallback cb,
1115 struct Plugin *plugin = cls;
1116 sqlite3_stmt *stmt = plugin->select_fragments;
1117 int ret = GNUNET_SYSERR;
1118 *returned_fragments = 0;
1120 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
1121 sizeof (*channel_key),
1123 || SQLITE_OK != sqlite3_bind_int64 (stmt, 2, first_fragment_id)
1124 || SQLITE_OK != sqlite3_bind_int64 (stmt, 3, last_fragment_id))
1126 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1131 ret = fragment_select (plugin, stmt, returned_fragments, cb, cb_cls);
1134 if (SQLITE_OK != sqlite3_reset (stmt))
1136 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1145 * Retrieve a message fragment range by fragment ID.
1147 * @see GNUNET_PSYCSTORE_fragment_get_latest()
1149 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1152 fragment_get_latest (void *cls,
1153 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1154 uint64_t fragment_limit,
1155 uint64_t *returned_fragments,
1156 GNUNET_PSYCSTORE_FragmentCallback cb,
1159 struct Plugin *plugin = cls;
1160 sqlite3_stmt *stmt = plugin->select_latest_fragments;
1161 int ret = GNUNET_SYSERR;
1162 *returned_fragments = 0;
1164 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
1165 sizeof (*channel_key),
1167 || SQLITE_OK != sqlite3_bind_int64 (stmt, 2, fragment_limit))
1169 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1174 ret = fragment_select (plugin, stmt, returned_fragments, cb, cb_cls);
1177 if (SQLITE_OK != sqlite3_reset (stmt))
1179 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1188 * Retrieve all fragments of a message ID range.
1190 * @see GNUNET_PSYCSTORE_message_get()
1192 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1195 message_get (void *cls,
1196 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1197 uint64_t first_message_id,
1198 uint64_t last_message_id,
1199 uint64_t fragment_limit,
1200 uint64_t *returned_fragments,
1201 GNUNET_PSYCSTORE_FragmentCallback cb,
1204 struct Plugin *plugin = cls;
1205 sqlite3_stmt *stmt = plugin->select_messages;
1206 int ret = GNUNET_SYSERR;
1207 *returned_fragments = 0;
1209 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
1210 sizeof (*channel_key),
1212 || SQLITE_OK != sqlite3_bind_int64 (stmt, 2, first_message_id)
1213 || SQLITE_OK != sqlite3_bind_int64 (stmt, 3, last_message_id)
1214 || SQLITE_OK != sqlite3_bind_int64 (stmt, 4,
1215 (0 != fragment_limit)
1219 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1224 ret = fragment_select (plugin, stmt, returned_fragments, cb, cb_cls);
1227 if (SQLITE_OK != sqlite3_reset (stmt))
1229 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1238 * Retrieve all fragments of the latest messages.
1240 * @see GNUNET_PSYCSTORE_message_get_latest()
1242 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1245 message_get_latest (void *cls,
1246 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1247 uint64_t message_limit,
1248 uint64_t *returned_fragments,
1249 GNUNET_PSYCSTORE_FragmentCallback cb,
1252 struct Plugin *plugin = cls;
1253 sqlite3_stmt *stmt = plugin->select_latest_messages;
1254 int ret = GNUNET_SYSERR;
1255 *returned_fragments = 0;
1257 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
1258 sizeof (*channel_key),
1260 || SQLITE_OK != sqlite3_bind_blob (stmt, 2, channel_key,
1261 sizeof (*channel_key),
1263 || SQLITE_OK != sqlite3_bind_int64 (stmt, 3, message_limit))
1265 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1270 ret = fragment_select (plugin, stmt, returned_fragments, cb, cb_cls);
1273 if (SQLITE_OK != sqlite3_reset (stmt))
1275 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1284 * Retrieve a fragment of message specified by its message ID and fragment
1287 * @see GNUNET_PSYCSTORE_message_get_fragment()
1289 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1292 message_get_fragment (void *cls,
1293 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1294 uint64_t message_id,
1295 uint64_t fragment_offset,
1296 GNUNET_PSYCSTORE_FragmentCallback cb,
1299 struct Plugin *plugin = cls;
1300 sqlite3_stmt *stmt = plugin->select_message_fragment;
1301 int ret = GNUNET_SYSERR;
1303 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
1304 sizeof (*channel_key),
1306 || SQLITE_OK != sqlite3_bind_int64 (stmt, 2, message_id)
1307 || SQLITE_OK != sqlite3_bind_int64 (stmt, 3, fragment_offset))
1309 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1314 switch (sqlite3_step (stmt))
1320 ret = fragment_row (stmt, cb, cb_cls);
1323 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1328 if (SQLITE_OK != sqlite3_reset (stmt))
1330 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1338 * Retrieve the max. values of message counters for a channel.
1340 * @see GNUNET_PSYCSTORE_counters_get()
1342 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1345 counters_message_get (void *cls,
1346 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1347 uint64_t *max_fragment_id,
1348 uint64_t *max_message_id,
1349 uint64_t *max_group_generation)
1351 struct Plugin *plugin = cls;
1352 sqlite3_stmt *stmt = plugin->select_counters_message;
1353 int ret = GNUNET_SYSERR;
1355 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
1356 sizeof (*channel_key),
1359 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1364 switch (sqlite3_step (stmt))
1370 *max_fragment_id = sqlite3_column_int64 (stmt, 0);
1371 *max_message_id = sqlite3_column_int64 (stmt, 1);
1372 *max_group_generation = sqlite3_column_int64 (stmt, 2);
1376 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1381 if (SQLITE_OK != sqlite3_reset (stmt))
1383 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1391 * Retrieve the max. values of state counters for a channel.
1393 * @see GNUNET_PSYCSTORE_counters_get()
1395 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1398 counters_state_get (void *cls,
1399 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1400 uint64_t *max_state_message_id)
1402 struct Plugin *plugin = cls;
1403 sqlite3_stmt *stmt = plugin->select_counters_state;
1404 int ret = GNUNET_SYSERR;
1406 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
1407 sizeof (*channel_key),
1410 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1415 switch (sqlite3_step (stmt))
1421 *max_state_message_id = sqlite3_column_int64 (stmt, 0);
1425 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1430 if (SQLITE_OK != sqlite3_reset (stmt))
1432 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1441 * Assign a value to a state variable.
1443 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1446 state_assign (struct Plugin *plugin, sqlite3_stmt *stmt,
1447 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1448 const char *name, const void *value, size_t value_size)
1450 int ret = GNUNET_SYSERR;
1452 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
1453 sizeof (*channel_key), SQLITE_STATIC)
1454 || SQLITE_OK != sqlite3_bind_text (stmt, 2, name, -1, SQLITE_STATIC)
1455 || SQLITE_OK != sqlite3_bind_blob (stmt, 3, value, value_size,
1458 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1463 switch (sqlite3_step (stmt))
1466 ret = 0 < sqlite3_total_changes (plugin->dbh) ? GNUNET_OK : GNUNET_NO;
1469 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1474 if (SQLITE_OK != sqlite3_reset (stmt))
1476 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1478 return GNUNET_SYSERR;
1486 update_message_id (struct Plugin *plugin, sqlite3_stmt *stmt,
1487 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1488 uint64_t message_id)
1490 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, message_id)
1491 || SQLITE_OK != sqlite3_bind_blob (stmt, 2, channel_key,
1492 sizeof (*channel_key), SQLITE_STATIC))
1494 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1497 else if (SQLITE_DONE != sqlite3_step (stmt))
1499 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1502 if (SQLITE_OK != sqlite3_reset (stmt))
1504 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1506 return GNUNET_SYSERR;
1513 * Begin modifying current state.
1516 state_modify_begin (void *cls,
1517 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1518 uint64_t message_id, uint64_t state_delta)
1520 struct Plugin *plugin = cls;
1522 if (state_delta > 0)
1525 * We can only apply state modifiers in the current message if modifiers in
1526 * the previous stateful message (message_id - state_delta) were already
1530 uint64_t max_state_message_id = 0;
1531 int ret = counters_state_get (plugin, channel_key, &max_state_message_id);
1535 case GNUNET_NO: // no state yet
1542 if (max_state_message_id < message_id - state_delta)
1543 return GNUNET_NO; /* some stateful messages not yet applied */
1544 else if (message_id - state_delta < max_state_message_id)
1545 return GNUNET_NO; /* changes already applied */
1548 if (TRANSACTION_NONE != plugin->transaction)
1550 /** @todo FIXME: wait for other transaction to finish */
1551 return GNUNET_SYSERR;
1553 return transaction_begin (plugin, TRANSACTION_STATE_MODIFY);
1558 * Set the current value of state variable.
1560 * @see GNUNET_PSYCSTORE_state_modify()
1562 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1565 state_modify_op (void *cls,
1566 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1567 enum GNUNET_PSYC_Operator op,
1568 const char *name, const void *value, size_t value_size)
1570 struct Plugin *plugin = cls;
1571 GNUNET_assert (TRANSACTION_STATE_MODIFY == plugin->transaction);
1575 case GNUNET_PSYC_OP_ASSIGN:
1576 return state_assign (plugin, plugin->insert_state_current, channel_key,
1577 name, value, value_size);
1579 default: /** @todo implement more state operations */
1581 return GNUNET_SYSERR;
1587 * End modifying current state.
1590 state_modify_end (void *cls,
1591 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1592 uint64_t message_id)
1594 struct Plugin *plugin = cls;
1595 GNUNET_assert (TRANSACTION_STATE_MODIFY == plugin->transaction);
1598 GNUNET_OK == exec_channel (plugin, plugin->delete_state_empty, channel_key)
1599 && GNUNET_OK == update_message_id (plugin,
1600 plugin->update_max_state_message_id,
1601 channel_key, message_id)
1602 && GNUNET_OK == transaction_commit (plugin)
1603 ? GNUNET_OK : GNUNET_SYSERR;
1608 * Begin state synchronization.
1611 state_sync_begin (void *cls,
1612 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
1614 struct Plugin *plugin = cls;
1615 return exec_channel (plugin, plugin->delete_state_sync, channel_key);
1620 * Assign current value of a state variable.
1622 * @see GNUNET_PSYCSTORE_state_modify()
1624 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1627 state_sync_assign (void *cls,
1628 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1629 const char *name, const void *value, size_t value_size)
1631 struct Plugin *plugin = cls;
1632 return state_assign (cls, plugin->insert_state_sync, channel_key,
1633 name, value, value_size);
1638 * End modifying current state.
1641 state_sync_end (void *cls,
1642 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1643 uint64_t max_state_message_id,
1644 uint64_t state_hash_message_id)
1646 struct Plugin *plugin = cls;
1647 int ret = GNUNET_SYSERR;
1649 if (TRANSACTION_NONE != plugin->transaction)
1651 /** @todo FIXME: wait for other transaction to finish */
1652 return GNUNET_SYSERR;
1655 GNUNET_OK == transaction_begin (plugin, TRANSACTION_STATE_SYNC)
1656 && GNUNET_OK == exec_channel (plugin, plugin->delete_state, channel_key)
1657 && GNUNET_OK == exec_channel (plugin, plugin->insert_state_from_sync,
1659 && GNUNET_OK == exec_channel (plugin, plugin->delete_state_sync,
1661 && GNUNET_OK == update_message_id (plugin,
1662 plugin->update_state_hash_message_id,
1663 channel_key, state_hash_message_id)
1664 && GNUNET_OK == update_message_id (plugin,
1665 plugin->update_max_state_message_id,
1666 channel_key, max_state_message_id)
1667 && GNUNET_OK == transaction_commit (plugin)
1669 : transaction_rollback (plugin);
1675 * Delete the whole state.
1677 * @see GNUNET_PSYCSTORE_state_reset()
1679 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1682 state_reset (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
1684 struct Plugin *plugin = cls;
1685 return exec_channel (plugin, plugin->delete_state, channel_key);
1690 * Update signed values of state variables in the state store.
1692 * @see GNUNET_PSYCSTORE_state_hash_update()
1694 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1697 state_update_signed (void *cls,
1698 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
1700 struct Plugin *plugin = cls;
1701 return exec_channel (plugin, plugin->update_state_signed, channel_key);
1706 * Retrieve a state variable by name.
1708 * @see GNUNET_PSYCSTORE_state_get()
1710 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1713 state_get (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1714 const char *name, GNUNET_PSYCSTORE_StateCallback cb, void *cb_cls)
1716 struct Plugin *plugin = cls;
1717 int ret = GNUNET_SYSERR;
1719 sqlite3_stmt *stmt = plugin->select_state_one;
1721 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
1722 sizeof (*channel_key),
1724 || SQLITE_OK != sqlite3_bind_text (stmt, 2, name, -1, SQLITE_STATIC))
1726 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1731 switch (sqlite3_step (stmt))
1737 ret = cb (cb_cls, name, sqlite3_column_blob (stmt, 0),
1738 sqlite3_column_bytes (stmt, 0));
1741 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1746 if (SQLITE_OK != sqlite3_reset (stmt))
1748 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1757 * Retrieve all state variables for a channel with the given prefix.
1759 * @see GNUNET_PSYCSTORE_state_get_prefix()
1761 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1764 state_get_prefix (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1765 const char *name, GNUNET_PSYCSTORE_StateCallback cb,
1768 struct Plugin *plugin = cls;
1769 int ret = GNUNET_SYSERR;
1770 sqlite3_stmt *stmt = plugin->select_state_prefix;
1771 size_t name_len = strlen (name);
1773 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
1774 sizeof (*channel_key), SQLITE_STATIC)
1775 || SQLITE_OK != sqlite3_bind_text (stmt, 2, name, name_len, SQLITE_STATIC)
1776 || SQLITE_OK != sqlite3_bind_int (stmt, 3, name_len + 1)
1777 || SQLITE_OK != sqlite3_bind_text (stmt, 4, name, name_len, SQLITE_STATIC))
1779 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1787 sql_ret = sqlite3_step (stmt);
1791 if (ret != GNUNET_OK)
1795 ret = cb (cb_cls, (const char *) sqlite3_column_text (stmt, 0),
1796 sqlite3_column_blob (stmt, 1),
1797 sqlite3_column_bytes (stmt, 1));
1798 if (ret != GNUNET_YES)
1799 sql_ret = SQLITE_DONE;
1802 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1806 while (sql_ret == SQLITE_ROW);
1808 if (SQLITE_OK != sqlite3_reset (stmt))
1810 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1818 * Retrieve all signed state variables for a channel.
1820 * @see GNUNET_PSYCSTORE_state_get_signed()
1822 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1825 state_get_signed (void *cls,
1826 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1827 GNUNET_PSYCSTORE_StateCallback cb, void *cb_cls)
1829 struct Plugin *plugin = cls;
1830 int ret = GNUNET_SYSERR;
1832 sqlite3_stmt *stmt = plugin->select_state_signed;
1834 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
1835 sizeof (*channel_key), SQLITE_STATIC))
1837 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1845 sql_ret = sqlite3_step (stmt);
1849 if (ret != GNUNET_OK)
1853 ret = cb (cb_cls, (const char *) sqlite3_column_text (stmt, 0),
1854 sqlite3_column_blob (stmt, 1),
1855 sqlite3_column_bytes (stmt, 1));
1856 if (ret != GNUNET_YES)
1857 sql_ret = SQLITE_DONE;
1860 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1864 while (sql_ret == SQLITE_ROW);
1867 if (SQLITE_OK != sqlite3_reset (stmt))
1869 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1878 * Entry point for the plugin.
1880 * @param cls The struct GNUNET_CONFIGURATION_Handle.
1881 * @return NULL on error, otherwise the plugin context
1884 libgnunet_plugin_psycstore_sqlite_init (void *cls)
1886 static struct Plugin plugin;
1887 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1888 struct GNUNET_PSYCSTORE_PluginFunctions *api;
1890 if (NULL != plugin.cfg)
1891 return NULL; /* can only initialize once! */
1892 memset (&plugin, 0, sizeof (struct Plugin));
1894 if (GNUNET_OK != database_setup (&plugin))
1896 database_shutdown (&plugin);
1899 api = GNUNET_new (struct GNUNET_PSYCSTORE_PluginFunctions);
1901 api->membership_store = &sqlite_membership_store;
1902 api->membership_test = &membership_test;
1903 api->fragment_store = &fragment_store;
1904 api->message_add_flags = &message_add_flags;
1905 api->fragment_get = &fragment_get;
1906 api->fragment_get_latest = &fragment_get_latest;
1907 api->message_get = &message_get;
1908 api->message_get_latest = &message_get_latest;
1909 api->message_get_fragment = &message_get_fragment;
1910 api->counters_message_get = &counters_message_get;
1911 api->counters_state_get = &counters_state_get;
1912 api->state_modify_begin = &state_modify_begin;
1913 api->state_modify_op = &state_modify_op;
1914 api->state_modify_end = &state_modify_end;
1915 api->state_sync_begin = &state_sync_begin;
1916 api->state_sync_assign = &state_sync_assign;
1917 api->state_sync_end = &state_sync_end;
1918 api->state_reset = &state_reset;
1919 api->state_update_signed = &state_update_signed;
1920 api->state_get = &state_get;
1921 api->state_get_prefix = &state_get_prefix;
1922 api->state_get_signed = &state_get_signed;
1924 LOG (GNUNET_ERROR_TYPE_INFO, _("SQLite database running\n"));
1930 * Exit point from the plugin.
1932 * @param cls The plugin context (as returned by "init")
1933 * @return Always NULL
1936 libgnunet_plugin_psycstore_sqlite_done (void *cls)
1938 struct GNUNET_PSYCSTORE_PluginFunctions *api = cls;
1939 struct Plugin *plugin = api->cls;
1941 database_shutdown (plugin);
1944 LOG (GNUNET_ERROR_TYPE_DEBUG, "SQLite plugin is finished\n");
1948 /* end of plugin_psycstore_sqlite.c */