2 * This file is part of GNUnet
3 * Copyright (C) 2016 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_postgres.c
23 * @brief PostgresQL-based psycstore backend
24 * @author Daniel Golle
25 * @author Gabor X Toth
26 * @author Christian Grothoff
27 * @author Christophe Genevey
31 #include "gnunet_psycstore_plugin.h"
32 #include "gnunet_psycstore_service.h"
33 #include "gnunet_multicast_service.h"
34 #include "gnunet_crypto_lib.h"
35 #include "gnunet_psyc_util_lib.h"
36 #include "psycstore.h"
37 #include "gnunet_postgres_lib.h"
38 #include "gnunet_pq_lib.h"
41 * After how many ms "busy" should a DB operation fail for good? A
42 * low value makes sure that we are more responsive to requests
43 * (especially PUTs). A high value guarantees a higher success rate
44 * (SELECTs in iterate can take several seconds despite LIMIT=1).
46 * The default value of 1s should ensure that users do not experience
47 * huge latencies while at the same time allowing operations to
48 * succeed with reasonable probability.
50 #define BUSY_TIMEOUT_MS 1000
52 #define DEBUG_PSYCSTORE GNUNET_EXTRA_LOGGING
54 #define LOG(kind,...) GNUNET_log_from (kind, "psycstore-postgres", __VA_ARGS__)
58 TRANSACTION_STATE_MODIFY,
59 TRANSACTION_STATE_SYNC,
63 * Context for all functions in this plugin.
68 const struct GNUNET_CONFIGURATION_Handle *cfg;
71 * Native Postgres database handle.
75 enum Transactions transaction;
82 * Initialize the database connections and associated
83 * data structures (create tables and indices
86 * @param plugin the plugin context (state for this module)
87 * @return GNUNET_OK on success
90 database_setup (struct Plugin *plugin)
92 /* Open database and precompile statements */
93 plugin->dbh = GNUNET_POSTGRES_connect (plugin->cfg,
94 "psycstore-postgres");
95 if (NULL == plugin->dbh)
100 GNUNET_POSTGRES_exec(plugin->dbh,
101 "CREATE TABLE IF NOT EXISTS channels (\n"
104 " max_state_message_id INT,\n"
105 " state_hash_message_id INT,\n"
110 GNUNET_POSTGRES_exec(plugin->dbh,
111 "CREATE UNIQUE INDEX IF NOT EXISTS channel_pub_key_idx \n"
112 " ON channels (substring(pub_key from 1 for 5)\n"
116 GNUNET_POSTGRES_exec(plugin->dbh,
117 "CREATE OR REPLACE FUNCTION get_chan_id(BYTEA) RETURNS INTEGER AS \n"
118 " 'SELECT id FROM channels WHERE pub_key=$1;' LANGUAGE SQL STABLE \n"
119 "RETURNS NULL ON NULL INPUT")) ||
122 GNUNET_POSTGRES_exec(plugin->dbh,
123 "CREATE TABLE IF NOT EXISTS slaves (\n"
130 GNUNET_POSTGRES_exec(plugin->dbh,
131 "CREATE UNIQUE INDEX IF NOT EXISTS slaves_pub_key_idx \n"
132 " ON slaves (substring(pub_key from 1 for 5)\n"
136 GNUNET_POSTGRES_exec(plugin->dbh,
137 "CREATE OR REPLACE FUNCTION get_slave_id(BYTEA) RETURNS INTEGER AS \n"
138 " 'SELECT id FROM slaves WHERE pub_key=$1;' LANGUAGE SQL STABLE \n"
139 "RETURNS NULL ON NULL INPUT")) ||
142 GNUNET_POSTGRES_exec(plugin->dbh,
143 "CREATE TABLE IF NOT EXISTS membership (\n"
144 " channel_id INT NOT NULL REFERENCES channels(id),\n"
145 " slave_id INT NOT NULL REFERENCES slaves(id),\n"
146 " did_join INT NOT NULL,\n"
147 " announced_at BIGINT NOT NULL,\n"
148 " effective_since BIGINT NOT NULL,\n"
149 " group_generation BIGINT NOT NULL\n"
153 GNUNET_POSTGRES_exec(plugin->dbh,
154 "CREATE INDEX IF NOT EXISTS idx_membership_channel_id_slave_id "
155 "ON membership (channel_id, slave_id)")) ||
157 /** @todo messages table: add method_name column */
159 GNUNET_POSTGRES_exec(plugin->dbh,
160 "CREATE TABLE IF NOT EXISTS messages (\n"
161 " channel_id INT NOT NULL REFERENCES channels(id),\n"
162 " hop_counter BIGINT NOT NULL,\n"
163 " signature BYTEA,\n"
165 " fragment_id BIGINT NOT NULL,\n"
166 " fragment_offset BIGINT NOT NULL,\n"
167 " message_id BIGINT NOT NULL,\n"
168 " group_generation BIGINT NOT NULL,\n"
169 " multicast_flags BIGINT NOT NULL,\n"
170 " psycstore_flags BIGINT NOT NULL,\n"
172 " PRIMARY KEY (channel_id, fragment_id),\n"
173 " UNIQUE (channel_id, message_id, fragment_offset)\n"
177 GNUNET_POSTGRES_exec(plugin->dbh,
178 "CREATE TABLE IF NOT EXISTS state (\n"
179 " channel_id INT NOT NULL REFERENCES channels(id),\n"
180 " name TEXT NOT NULL,\n"
181 " value_current BYTEA,\n"
182 " value_signed BYTEA\n"
186 GNUNET_POSTGRES_exec(plugin->dbh,
187 "CREATE UNIQUE INDEX IF NOT EXISTS state_uniq_idx \n"
188 " ON state (channel_id, substring(name from 1 for 5)\n"
192 GNUNET_POSTGRES_exec(plugin->dbh,
193 "CREATE TABLE IF NOT EXISTS state_sync (\n"
194 " channel_id INT NOT NULL REFERENCES channels(id),\n"
195 " name TEXT NOT NULL,\n"
197 " PRIMARY KEY (channel_id)\n"
201 GNUNET_POSTGRES_exec(plugin->dbh,
202 "CREATE UNIQUE INDEX IF NOT EXISTS state_sync_name_idx \n"
203 " ON state_sync (substring(name from 1 for 5)\n"
206 PQfinish (plugin->dbh);
208 return GNUNET_SYSERR;
212 /* Prepare statements */
213 if ((GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
217 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
218 "transaction_commit",
221 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
222 "transaction_rollback",
225 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
226 "insert_channel_key",
227 "INSERT INTO channels (pub_key) VALUES ($1)"
228 " ON CONFLICT DO NOTHING", 1)) ||
230 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
232 "INSERT INTO slaves (pub_key) VALUES ($1)"
233 " ON CONFLICT DO NOTHING", 1)) ||
235 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
237 "INSERT INTO membership\n"
238 " (channel_id, slave_id, did_join, announced_at,\n"
239 " effective_since, group_generation)\n"
240 "VALUES (get_chan_id($1),\n"
241 " get_slave_id($2),\n"
242 " $3, $4, $5, $6)", 6)) ||
244 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
246 "SELECT did_join FROM membership\n"
247 "WHERE channel_id = get_chan_id($1)\n"
248 " AND slave_id = get_slave_id($2)\n"
249 " AND effective_since <= $3 AND did_join = 1\n"
250 "ORDER BY announced_at DESC LIMIT 1", 3)) ||
252 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
254 "INSERT INTO messages\n"
255 " (channel_id, hop_counter, signature, purpose,\n"
256 " fragment_id, fragment_offset, message_id,\n"
257 " group_generation, multicast_flags, psycstore_flags, data)\n"
258 "VALUES (get_chan_id($1),\n"
259 " $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)"
260 "ON CONFLICT DO NOTHING", 11)) ||
262 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
263 "update_message_flags",
265 "SET psycstore_flags = psycstore_flags | $1\n"
266 "WHERE channel_id = get_chan_id($2) \n"
267 " AND message_id = $3 AND fragment_offset = 0", 3)) ||
269 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
271 "SELECT hop_counter, signature, purpose, fragment_id,\n"
272 " fragment_offset, message_id, group_generation,\n"
273 " multicast_flags, psycstore_flags, data\n"
275 "WHERE channel_id = get_chan_id($1) \n"
276 " AND $2 <= fragment_id AND fragment_id <= $3", 3)) ||
278 /** @todo select_messages: add method_prefix filter */
279 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
281 "SELECT hop_counter, signature, purpose, fragment_id,\n"
282 " fragment_offset, message_id, group_generation,\n"
283 " multicast_flags, psycstore_flags, data\n"
285 "WHERE channel_id = get_chan_id($1) \n"
286 " AND $2 <= message_id AND message_id <= $3"
289 /** @todo select_latest_messages: add method_prefix filter */
290 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
291 "select_latest_fragments",
292 "SELECT rev.hop_counter AS hop_counter,\n"
293 " rev.signature AS signature,\n"
294 " rev.purpose AS purpose,\n"
295 " rev.fragment_id AS fragment_id,\n"
296 " rev.fragment_offset AS fragment_offset,\n"
297 " rev.message_id AS message_id,\n"
298 " rev.group_generation AS group_generation,\n"
299 " rev.multicast_flags AS multicast_flags,\n"
300 " rev.psycstore_flags AS psycstore_flags,\n"
301 " rev.data AS data\n"
303 " (SELECT hop_counter, signature, purpose, fragment_id,\n"
304 " fragment_offset, message_id, group_generation,\n"
305 " multicast_flags, psycstore_flags, data \n"
307 " WHERE channel_id = get_chan_id($1) \n"
308 " ORDER BY fragment_id DESC\n"
309 " LIMIT $2) AS rev\n"
310 " ORDER BY rev.fragment_id;", 2)) ||
312 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
313 "select_latest_messages",
314 "SELECT hop_counter, signature, purpose, fragment_id,\n"
315 " fragment_offset, message_id, group_generation,\n"
316 " multicast_flags, psycstore_flags, data\n"
318 "WHERE channel_id = get_chan_id($1)\n"
319 " AND message_id IN\n"
320 " (SELECT message_id\n"
322 " WHERE channel_id = get_chan_id($2) \n"
323 " GROUP BY message_id\n"
324 " ORDER BY message_id\n"
326 "ORDER BY fragment_id", 3)) ||
328 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
329 "select_message_fragment",
330 "SELECT hop_counter, signature, purpose, fragment_id,\n"
331 " fragment_offset, message_id, group_generation,\n"
332 " multicast_flags, psycstore_flags, data\n"
334 "WHERE channel_id = get_chan_id($1) \n"
335 " AND message_id = $2 AND fragment_offset = $3", 3)) ||
337 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
338 "select_counters_message",
339 "SELECT fragment_id, message_id, group_generation\n"
341 "WHERE channel_id = get_chan_id($1)\n"
342 "ORDER BY fragment_id DESC LIMIT 1", 1)) ||
344 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
345 "select_counters_state",
346 "SELECT max_state_message_id\n"
348 "WHERE pub_key = $1 AND max_state_message_id IS NOT NULL", 1)) ||
350 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
351 "update_max_state_message_id",
353 "SET max_state_message_id = $1\n"
354 "WHERE pub_key = $2", 2)) ||
356 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
357 "update_state_hash_message_id",
359 "SET state_hash_message_id = $1\n"
360 "WHERE pub_key = $2", 2)) ||
362 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
363 "insert_state_current",
364 "INSERT INTO state\n"
365 " (channel_id, name, value_current, value_signed)\n"
366 "SELECT new.channel_id, new.name,\n"
367 " new.value_current, old.value_signed\n"
368 "FROM (SELECT get_chan_id($1) AS channel_id,\n"
369 " $2::TEXT AS name, $3::BYTEA AS value_current) AS new\n"
370 "LEFT JOIN (SELECT channel_id, name, value_signed\n"
371 " FROM state) AS old\n"
372 "ON new.channel_id = old.channel_id AND new.name = old.name\n"
373 "ON CONFLICT ( channel_id, substring(name from 1 for 5) )\n"
374 " DO UPDATE SET value_current = EXCLUDED.value_current,\n"
375 " value_signed = EXCLUDED.value_signed", 3)) ||
377 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
378 "delete_state_empty",
379 "DELETE FROM state\n"
380 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = $1)\n"
381 " AND (value_current IS NULL OR length(value_current) = 0)\n"
382 " AND (value_signed IS NULL OR length(value_signed) = 0)", 1)) ||
384 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
385 "update_state_signed",
387 "SET value_signed = value_current\n"
388 "WHERE channel_id = get_chan_id($1) ", 1)) ||
390 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
392 "DELETE FROM state\n"
393 "WHERE channel_id = get_chan_id($1) ", 1)) ||
395 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
397 "INSERT INTO state_sync (channel_id, name, value)\n"
398 "VALUES (get_chan_id($1), $2, $3)", 3)) ||
400 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
401 "insert_state_from_sync",
402 "INSERT INTO state\n"
403 " (channel_id, name, value_current, value_signed)\n"
404 "SELECT channel_id, name, value, value\n"
406 "WHERE channel_id = get_chan_id($1)", 1)) ||
408 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
410 "DELETE FROM state_sync\n"
411 "WHERE channel_id = get_chan_id($1)", 1)) ||
413 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
415 "SELECT value_current\n"
417 "WHERE channel_id = get_chan_id($1)\n"
418 " AND name = $2", 2)) ||
420 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
421 "select_state_prefix",
422 "SELECT name, value_current\n"
424 "WHERE channel_id = get_chan_id($1)\n"
425 " AND (name = $2 OR substr(name, 1, $3) = $4 || '_')", 4)) ||
427 (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
428 "select_state_signed",
429 "SELECT name, value_signed\n"
431 "WHERE channel_id = get_chan_id($1)\n"
432 " AND value_signed IS NOT NULL", 1)))
434 PQfinish (plugin->dbh);
436 return GNUNET_SYSERR;
444 * Shutdown database connection and associate data
446 * @param plugin the plugin context (state for this module)
449 database_shutdown (struct Plugin *plugin)
451 PQfinish (plugin->dbh);
457 * Execute a prepared statement with a @a channel_key argument.
459 * @param plugin Plugin handle.
460 * @param stmt Statement to execute.
461 * @param channel_key Public key of the channel.
463 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
466 exec_channel (struct Plugin *plugin, const char *stmt,
467 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
470 struct GNUNET_PQ_QueryParam params[] = {
471 GNUNET_PQ_query_param_auto_from_type (channel_key),
472 GNUNET_PQ_query_param_end
475 ret = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params);
477 GNUNET_POSTGRES_check_result (plugin->dbh,
480 "PQexecPrepared", stmt))
481 return GNUNET_SYSERR;
490 * Begin a transaction.
493 transaction_begin (struct Plugin *plugin, enum Transactions transaction)
496 struct GNUNET_PQ_QueryParam params[] = {
497 GNUNET_PQ_query_param_end
500 ret = GNUNET_PQ_exec_prepared (plugin->dbh, "transaction_begin", params);
502 GNUNET_POSTGRES_check_result (plugin->dbh,
505 "PQexecPrepared", "transaction_begin"))
507 return GNUNET_SYSERR;
510 plugin->transaction = transaction;
517 * Commit current transaction.
520 transaction_commit (struct Plugin *plugin)
524 struct GNUNET_PQ_QueryParam params[] = {
525 GNUNET_PQ_query_param_end
528 ret = GNUNET_PQ_exec_prepared (plugin->dbh, "transaction_commit", params);
530 GNUNET_POSTGRES_check_result (plugin->dbh,
533 "PQexecPrepared", "transaction_commit"))
535 return GNUNET_SYSERR;
539 plugin->transaction = TRANSACTION_NONE;
545 * Roll back current transaction.
548 transaction_rollback (struct Plugin *plugin)
552 struct GNUNET_PQ_QueryParam params[] = {
553 GNUNET_PQ_query_param_end
556 ret = GNUNET_PQ_exec_prepared (plugin->dbh, "transaction_rollback", params);
558 GNUNET_POSTGRES_check_result (plugin->dbh,
561 "PQexecPrepared", "transaction_rollback"))
563 return GNUNET_SYSERR;
567 plugin->transaction = TRANSACTION_NONE;
573 channel_key_store (struct Plugin *plugin,
574 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
578 struct GNUNET_PQ_QueryParam params[] = {
579 GNUNET_PQ_query_param_auto_from_type (channel_key),
580 GNUNET_PQ_query_param_end
583 ret = GNUNET_PQ_exec_prepared (plugin->dbh, "insert_channel_key", params);
585 GNUNET_POSTGRES_check_result (plugin->dbh,
588 "PQexecPrepared", "insert_channel_key"))
590 return GNUNET_SYSERR;
599 slave_key_store (struct Plugin *plugin,
600 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key)
604 struct GNUNET_PQ_QueryParam params[] = {
605 GNUNET_PQ_query_param_auto_from_type (slave_key),
606 GNUNET_PQ_query_param_end
609 ret = GNUNET_PQ_exec_prepared (plugin->dbh, "insert_slave_key", params);
611 GNUNET_POSTGRES_check_result (plugin->dbh,
614 "PQexecPrepared", "insert_slave_key"))
616 return GNUNET_SYSERR;
625 * Store join/leave events for a PSYC channel in order to be able to answer
626 * membership test queries later.
628 * @see GNUNET_PSYCSTORE_membership_store()
630 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
633 postgres_membership_store (void *cls,
634 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
635 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
637 uint64_t announced_at,
638 uint64_t effective_since,
639 uint64_t group_generation)
642 struct Plugin *plugin = cls;
644 uint32_t idid_join = (uint32_t)did_join;
646 GNUNET_assert (TRANSACTION_NONE == plugin->transaction);
648 if (announced_at > INT64_MAX ||
649 effective_since > INT64_MAX ||
650 group_generation > INT64_MAX)
653 return GNUNET_SYSERR;
656 if (GNUNET_OK != channel_key_store (plugin, channel_key)
657 || GNUNET_OK != slave_key_store (plugin, slave_key))
658 return GNUNET_SYSERR;
660 struct GNUNET_PQ_QueryParam params[] = {
661 GNUNET_PQ_query_param_auto_from_type (channel_key),
662 GNUNET_PQ_query_param_auto_from_type (slave_key),
663 GNUNET_PQ_query_param_uint32 (&idid_join),
664 GNUNET_PQ_query_param_uint64 (&announced_at),
665 GNUNET_PQ_query_param_uint64 (&effective_since),
666 GNUNET_PQ_query_param_uint64 (&group_generation),
667 GNUNET_PQ_query_param_end
670 ret = GNUNET_PQ_exec_prepared (plugin->dbh, "insert_membership", params);
672 GNUNET_POSTGRES_check_result (plugin->dbh,
675 "PQexecPrepared", "insert_membership"))
677 return GNUNET_SYSERR;
685 * Test if a member was admitted to the channel at the given message ID.
687 * @see GNUNET_PSYCSTORE_membership_test()
689 * @return #GNUNET_YES if the member was admitted, #GNUNET_NO if not,
690 * #GNUNET_SYSERR if there was en error.
693 membership_test (void *cls,
694 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
695 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
699 struct Plugin *plugin = cls;
701 uint32_t did_join = 0;
703 int ret = GNUNET_SYSERR;
705 struct GNUNET_PQ_QueryParam params_select[] = {
706 GNUNET_PQ_query_param_auto_from_type (channel_key),
707 GNUNET_PQ_query_param_auto_from_type (slave_key),
708 GNUNET_PQ_query_param_uint64 (&message_id),
709 GNUNET_PQ_query_param_end
712 res = GNUNET_PQ_exec_prepared (plugin->dbh, "select_membership", params_select);
714 GNUNET_POSTGRES_check_result (plugin->dbh,
717 "PQexecPrepared", "select_membership"))
719 return GNUNET_SYSERR;
722 struct GNUNET_PQ_ResultSpec results_select[] = {
723 GNUNET_PQ_result_spec_uint32 ("did_join", &did_join),
724 GNUNET_PQ_result_spec_end
727 switch(GNUNET_PQ_extract_result (res, results_select, 0))
743 * Store a message fragment sent to a channel.
745 * @see GNUNET_PSYCSTORE_fragment_store()
747 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
750 fragment_store (void *cls,
751 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
752 const struct GNUNET_MULTICAST_MessageHeader *msg,
753 uint32_t psycstore_flags)
756 struct Plugin *plugin = cls;
758 GNUNET_assert (TRANSACTION_NONE == plugin->transaction);
760 uint64_t fragment_id = GNUNET_ntohll (msg->fragment_id);
762 uint64_t fragment_offset = GNUNET_ntohll (msg->fragment_offset);
763 uint64_t message_id = GNUNET_ntohll (msg->message_id);
764 uint64_t group_generation = GNUNET_ntohll (msg->group_generation);
766 uint64_t hop_counter = ntohl(msg->hop_counter);
767 uint64_t flags = ntohl(msg->flags);
769 if (fragment_id > INT64_MAX || fragment_offset > INT64_MAX ||
770 message_id > INT64_MAX || group_generation > INT64_MAX)
772 LOG(GNUNET_ERROR_TYPE_ERROR,
773 "Tried to store fragment with a field > INT64_MAX: "
774 "%lu, %lu, %lu, %lu\n", fragment_id, fragment_offset,
775 message_id, group_generation);
777 return GNUNET_SYSERR;
780 if (GNUNET_OK != channel_key_store (plugin, channel_key))
781 return GNUNET_SYSERR;
783 struct GNUNET_PQ_QueryParam params_insert[] = {
784 GNUNET_PQ_query_param_auto_from_type (channel_key),
785 GNUNET_PQ_query_param_uint64 (&hop_counter),
786 GNUNET_PQ_query_param_auto_from_type (&msg->signature),
787 GNUNET_PQ_query_param_auto_from_type (&msg->purpose),
788 GNUNET_PQ_query_param_uint64 (&fragment_id),
789 GNUNET_PQ_query_param_uint64 (&fragment_offset),
790 GNUNET_PQ_query_param_uint64 (&message_id),
791 GNUNET_PQ_query_param_uint64 (&group_generation),
792 GNUNET_PQ_query_param_uint64 (&flags),
793 GNUNET_PQ_query_param_uint32 (&psycstore_flags),
794 GNUNET_PQ_query_param_fixed_size (&msg[1], ntohs (msg->header.size)
796 GNUNET_PQ_query_param_end
799 res = GNUNET_PQ_exec_prepared (plugin->dbh, "insert_fragment", params_insert);
801 GNUNET_POSTGRES_check_result (plugin->dbh,
804 "PQexecPrepared", "insert_fragment"))
805 return GNUNET_SYSERR;
812 * Set additional flags for a given message.
814 * They are OR'd with any existing flags set.
816 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
819 message_add_flags (void *cls,
820 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
822 uint64_t psycstore_flags)
825 struct Plugin *plugin = cls;
827 int ret = GNUNET_SYSERR;
829 struct GNUNET_PQ_QueryParam params_update[] = {
830 GNUNET_PQ_query_param_uint64 (&psycstore_flags),
831 GNUNET_PQ_query_param_auto_from_type (channel_key),
832 GNUNET_PQ_query_param_uint64 (&message_id),
833 GNUNET_PQ_query_param_end
836 res = GNUNET_PQ_exec_prepared (plugin->dbh, "update_message_flags", params_update);
837 if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
840 "PQexecPrepared", "update_message_flags"))
849 fragment_row (struct Plugin *plugin,
852 GNUNET_PSYCSTORE_FragmentCallback cb,
855 uint32_t hop_counter;
856 void *signature = NULL;
857 void *purpose = NULL;
858 size_t signature_size;
861 uint64_t fragment_id;
862 uint64_t fragment_offset;
864 uint64_t group_generation;
868 int ret = GNUNET_SYSERR;
869 struct GNUNET_MULTICAST_MessageHeader *mp;
874 struct GNUNET_PQ_ResultSpec results[] = {
875 GNUNET_PQ_result_spec_uint32 ("hop_counter", &hop_counter),
876 GNUNET_PQ_result_spec_variable_size ("signature", &signature, &signature_size),
877 GNUNET_PQ_result_spec_variable_size ("purpose", &purpose, &purpose_size),
878 GNUNET_PQ_result_spec_uint64 ("fragment_id", &fragment_id),
879 GNUNET_PQ_result_spec_uint64 ("fragment_offset", &fragment_offset),
880 GNUNET_PQ_result_spec_uint64 ("message_id", &message_id),
881 GNUNET_PQ_result_spec_uint64 ("group_generation", &group_generation),
882 GNUNET_PQ_result_spec_uint64 ("msg_flags", &msg_flags),
883 GNUNET_PQ_result_spec_uint64 ("flags", &flags),
884 GNUNET_PQ_result_spec_variable_size ("data", &buf,
886 GNUNET_PQ_result_spec_end
890 GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK,
894 LOG (GNUNET_ERROR_TYPE_DEBUG,
895 "Failing fragment lookup (postgres error)\n");
896 return GNUNET_SYSERR;
899 cnt = PQntuples (res);
906 if (GNUNET_OK != GNUNET_PQ_extract_result(res, results, 0)) {
908 return GNUNET_SYSERR;
911 mp = GNUNET_malloc (sizeof (*mp) + buf_size);
913 mp->header.size = htons (sizeof (*mp) + buf_size);
914 mp->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
915 mp->hop_counter = htonl (hop_counter);
916 GNUNET_memcpy (&mp->signature,
919 GNUNET_memcpy (&mp->purpose,
922 mp->fragment_id = GNUNET_htonll (fragment_id);
923 mp->fragment_offset = GNUNET_htonll (fragment_offset);
924 mp->message_id = GNUNET_htonll (message_id);
925 mp->group_generation = GNUNET_htonll (group_generation);
926 mp->flags = htonl(msg_flags);
928 GNUNET_memcpy (&mp[1],
931 GNUNET_PQ_cleanup_result(results);
932 ret = cb (cb_cls, mp, (enum GNUNET_PSYCSTORE_MessageFlags) flags);
941 fragment_select (struct Plugin *plugin, const char *stmt,
942 struct GNUNET_PQ_QueryParam *params,
943 uint64_t *returned_fragments,
944 GNUNET_PSYCSTORE_FragmentCallback cb, void *cb_cls)
947 int ret = GNUNET_SYSERR;
950 if (NULL == plugin->dbh || NULL == stmt || NULL == params)
952 fprintf(stderr, "%p %p %p\n", plugin->dbh, stmt, params);
953 return GNUNET_SYSERR;
956 res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params);
958 GNUNET_POSTGRES_check_result (plugin->dbh,
961 "PQexecPrepared", stmt))
963 if (PQntuples (res) == 0)
967 ret = fragment_row (plugin, stmt, res, cb, cb_cls);
968 (*returned_fragments)++;
977 * Retrieve a message fragment range by fragment ID.
979 * @see GNUNET_PSYCSTORE_fragment_get()
981 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
984 fragment_get (void *cls,
985 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
986 uint64_t first_fragment_id,
987 uint64_t last_fragment_id,
988 uint64_t *returned_fragments,
989 GNUNET_PSYCSTORE_FragmentCallback cb,
992 struct Plugin *plugin = cls;
993 *returned_fragments = 0;
995 struct GNUNET_PQ_QueryParam params_select[] = {
996 GNUNET_PQ_query_param_auto_from_type (channel_key),
997 GNUNET_PQ_query_param_uint64 (&first_fragment_id),
998 GNUNET_PQ_query_param_uint64 (&last_fragment_id),
999 GNUNET_PQ_query_param_end
1002 return fragment_select (plugin, "select_fragments", params_select, returned_fragments, cb, cb_cls);
1007 * Retrieve a message fragment range by fragment ID.
1009 * @see GNUNET_PSYCSTORE_fragment_get_latest()
1011 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1014 fragment_get_latest (void *cls,
1015 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1016 uint64_t fragment_limit,
1017 uint64_t *returned_fragments,
1018 GNUNET_PSYCSTORE_FragmentCallback cb,
1021 struct Plugin *plugin = cls;
1023 *returned_fragments = 0;
1025 struct GNUNET_PQ_QueryParam params_select[] = {
1026 GNUNET_PQ_query_param_auto_from_type (channel_key),
1027 GNUNET_PQ_query_param_uint64 (&fragment_limit),
1028 GNUNET_PQ_query_param_end
1031 return fragment_select (plugin, "select_latest_fragments", params_select, returned_fragments, cb, cb_cls);
1036 * Retrieve all fragments of a message ID range.
1038 * @see GNUNET_PSYCSTORE_message_get()
1040 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1043 message_get (void *cls,
1044 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1045 uint64_t first_message_id,
1046 uint64_t last_message_id,
1047 uint64_t fragment_limit,
1048 uint64_t *returned_fragments,
1049 GNUNET_PSYCSTORE_FragmentCallback cb,
1052 struct Plugin *plugin = cls;
1053 *returned_fragments = 0;
1055 struct GNUNET_PQ_QueryParam params_select[] = {
1056 GNUNET_PQ_query_param_auto_from_type (channel_key),
1057 GNUNET_PQ_query_param_uint64 (&first_message_id),
1058 GNUNET_PQ_query_param_uint64 (&last_message_id),
1059 GNUNET_PQ_query_param_uint64 (&fragment_limit),
1060 GNUNET_PQ_query_param_end
1063 return fragment_select (plugin, "select_messages", params_select, returned_fragments, cb, cb_cls);
1068 * Retrieve all fragments of the latest messages.
1070 * @see GNUNET_PSYCSTORE_message_get_latest()
1072 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1075 message_get_latest (void *cls,
1076 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1077 uint64_t message_limit,
1078 uint64_t *returned_fragments,
1079 GNUNET_PSYCSTORE_FragmentCallback cb,
1082 struct Plugin *plugin = cls;
1083 *returned_fragments = 0;
1085 struct GNUNET_PQ_QueryParam params_select[] = {
1086 GNUNET_PQ_query_param_auto_from_type (channel_key),
1087 GNUNET_PQ_query_param_auto_from_type (channel_key),
1088 GNUNET_PQ_query_param_uint64 (&message_limit),
1089 GNUNET_PQ_query_param_end
1092 return fragment_select (plugin, "select_latest_messages", params_select, returned_fragments, cb, cb_cls);
1097 * Retrieve a fragment of message specified by its message ID and fragment
1100 * @see GNUNET_PSYCSTORE_message_get_fragment()
1102 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1105 message_get_fragment (void *cls,
1106 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1107 uint64_t message_id,
1108 uint64_t fragment_offset,
1109 GNUNET_PSYCSTORE_FragmentCallback cb,
1113 struct Plugin *plugin = cls;
1114 int ret = GNUNET_SYSERR;
1115 const char *stmt = "select_message_fragment";
1117 struct GNUNET_PQ_QueryParam params_select[] = {
1118 GNUNET_PQ_query_param_auto_from_type (channel_key),
1119 GNUNET_PQ_query_param_uint64 (&message_id),
1120 GNUNET_PQ_query_param_uint64 (&fragment_offset),
1121 GNUNET_PQ_query_param_end
1124 res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
1125 if (GNUNET_OK == GNUNET_POSTGRES_check_result (plugin->dbh,
1128 "PQexecPrepared", stmt))
1130 if (PQntuples (res) == 0)
1133 ret = fragment_row (plugin, stmt, res, cb, cb_cls);
1142 * Retrieve the max. values of message counters for a channel.
1144 * @see GNUNET_PSYCSTORE_counters_get()
1146 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1149 counters_message_get (void *cls,
1150 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1151 uint64_t *max_fragment_id,
1152 uint64_t *max_message_id,
1153 uint64_t *max_group_generation)
1156 struct Plugin *plugin = cls;
1158 const char *stmt = "select_counters_message";
1160 struct GNUNET_PQ_QueryParam params_select[] = {
1161 GNUNET_PQ_query_param_auto_from_type (channel_key),
1162 GNUNET_PQ_query_param_end
1165 res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
1166 if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
1169 "PQexecPrepared", stmt))
1171 return GNUNET_SYSERR;
1174 struct GNUNET_PQ_ResultSpec results_select[] = {
1175 GNUNET_PQ_result_spec_uint64 ("max_fragment_id", max_fragment_id),
1176 GNUNET_PQ_result_spec_uint64 ("max_message_id", max_message_id),
1177 GNUNET_PQ_result_spec_uint64 ("max_group_generation", max_group_generation),
1178 GNUNET_PQ_result_spec_end
1181 if (GNUNET_OK != GNUNET_PQ_extract_result (res,
1185 return GNUNET_SYSERR;
1188 GNUNET_PQ_cleanup_result(results_select);
1195 * Retrieve the max. values of state counters for a channel.
1197 * @see GNUNET_PSYCSTORE_counters_get()
1199 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1202 counters_state_get (void *cls,
1203 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1204 uint64_t *max_state_message_id)
1207 struct Plugin *plugin = cls;
1209 const char *stmt = "select_counters_state";
1211 int ret = GNUNET_SYSERR;
1213 struct GNUNET_PQ_QueryParam params_select[] = {
1214 GNUNET_PQ_query_param_auto_from_type (channel_key),
1215 GNUNET_PQ_query_param_end
1218 res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
1219 if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
1222 "PQexecPrepared", stmt))
1224 return GNUNET_SYSERR;
1227 struct GNUNET_PQ_ResultSpec results_select[] = {
1228 GNUNET_PQ_result_spec_uint64 ("max_state_message_id", max_state_message_id),
1229 GNUNET_PQ_result_spec_end
1232 ret = GNUNET_PQ_extract_result (res,
1235 if (GNUNET_OK != ret)
1238 return GNUNET_SYSERR;
1241 GNUNET_PQ_cleanup_result(results_select);
1249 * Assign a value to a state variable.
1251 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1254 state_assign (struct Plugin *plugin, const char *stmt,
1255 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1256 const char *name, const void *value, size_t value_size)
1260 struct GNUNET_PQ_QueryParam params[] = {
1261 GNUNET_PQ_query_param_auto_from_type (channel_key),
1262 GNUNET_PQ_query_param_string (name),
1263 GNUNET_PQ_query_param_auto_from_type (value),
1264 GNUNET_PQ_query_param_end
1267 res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params);
1268 if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
1271 "PQexecPrepared", stmt))
1273 return GNUNET_SYSERR;
1283 update_message_id (struct Plugin *plugin, const char *stmt,
1284 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1285 uint64_t message_id)
1289 struct GNUNET_PQ_QueryParam params[] = {
1290 GNUNET_PQ_query_param_uint64 (&message_id),
1291 GNUNET_PQ_query_param_auto_from_type (channel_key),
1292 GNUNET_PQ_query_param_end
1295 res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params);
1296 if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
1299 "PQexecPrepared", stmt))
1301 return GNUNET_SYSERR;
1311 * Begin modifying current state.
1314 state_modify_begin (void *cls,
1315 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1316 uint64_t message_id, uint64_t state_delta)
1318 struct Plugin *plugin = cls;
1320 if (state_delta > 0)
1323 * We can only apply state modifiers in the current message if modifiers in
1324 * the previous stateful message (message_id - state_delta) were already
1328 uint64_t max_state_message_id = 0;
1329 int ret = counters_state_get (plugin, channel_key, &max_state_message_id);
1333 case GNUNET_NO: // no state yet
1340 if (max_state_message_id < message_id - state_delta)
1341 return GNUNET_NO; /* some stateful messages not yet applied */
1342 else if (message_id - state_delta < max_state_message_id)
1343 return GNUNET_NO; /* changes already applied */
1346 if (TRANSACTION_NONE != plugin->transaction)
1348 /** @todo FIXME: wait for other transaction to finish */
1349 return GNUNET_SYSERR;
1351 return transaction_begin (plugin, TRANSACTION_STATE_MODIFY);
1356 * Set the current value of state variable.
1358 * @see GNUNET_PSYCSTORE_state_modify()
1360 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1363 state_modify_op (void *cls,
1364 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1365 enum GNUNET_PSYC_Operator op,
1366 const char *name, const void *value, size_t value_size)
1368 struct Plugin *plugin = cls;
1369 GNUNET_assert (TRANSACTION_STATE_MODIFY == plugin->transaction);
1373 case GNUNET_PSYC_OP_ASSIGN:
1374 return state_assign (plugin, "insert_state_current", channel_key,
1375 name, value, value_size);
1377 default: /** @todo implement more state operations */
1379 return GNUNET_SYSERR;
1385 * End modifying current state.
1388 state_modify_end (void *cls,
1389 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1390 uint64_t message_id)
1392 struct Plugin *plugin = cls;
1393 GNUNET_assert (TRANSACTION_STATE_MODIFY == plugin->transaction);
1396 GNUNET_OK == exec_channel (plugin, "delete_state_empty", channel_key)
1397 && GNUNET_OK == update_message_id (plugin,
1398 "update_max_state_message_id",
1399 channel_key, message_id)
1400 && GNUNET_OK == transaction_commit (plugin)
1401 ? GNUNET_OK : GNUNET_SYSERR;
1406 * Begin state synchronization.
1409 state_sync_begin (void *cls,
1410 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
1412 struct Plugin *plugin = cls;
1413 return exec_channel (plugin, "delete_state_sync", channel_key);
1418 * Assign current value of a state variable.
1420 * @see GNUNET_PSYCSTORE_state_modify()
1422 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1425 state_sync_assign (void *cls,
1426 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1427 const char *name, const void *value, size_t value_size)
1429 struct Plugin *plugin = cls;
1430 return state_assign (plugin, "insert_state_sync", channel_key,
1431 name, value, value_size);
1436 * End modifying current state.
1439 state_sync_end (void *cls,
1440 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1441 uint64_t max_state_message_id,
1442 uint64_t state_hash_message_id)
1444 struct Plugin *plugin = cls;
1445 int ret = GNUNET_SYSERR;
1447 if (TRANSACTION_NONE != plugin->transaction)
1449 /** @todo FIXME: wait for other transaction to finish */
1450 return GNUNET_SYSERR;
1453 GNUNET_OK == transaction_begin (plugin, TRANSACTION_STATE_SYNC)
1454 && GNUNET_OK == exec_channel (plugin, "delete_state", channel_key)
1455 && GNUNET_OK == exec_channel (plugin, "insert_state_from_sync",
1457 && GNUNET_OK == exec_channel (plugin, "delete_state_sync",
1459 && GNUNET_OK == update_message_id (plugin,
1460 "update_state_hash_message_id",
1461 channel_key, state_hash_message_id)
1462 && GNUNET_OK == update_message_id (plugin,
1463 "update_max_state_message_id",
1464 channel_key, max_state_message_id)
1465 && GNUNET_OK == transaction_commit (plugin)
1467 : transaction_rollback (plugin);
1473 * Delete the whole state.
1475 * @see GNUNET_PSYCSTORE_state_reset()
1477 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1480 state_reset (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
1482 struct Plugin *plugin = cls;
1483 return exec_channel (plugin, "delete_state", channel_key);
1488 * Update signed values of state variables in the state store.
1490 * @see GNUNET_PSYCSTORE_state_hash_update()
1492 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1495 state_update_signed (void *cls,
1496 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
1498 struct Plugin *plugin = cls;
1499 return exec_channel (plugin, "update_state_signed", channel_key);
1504 * Retrieve a state variable by name.
1506 * @see GNUNET_PSYCSTORE_state_get()
1508 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1511 state_get (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1512 const char *name, GNUNET_PSYCSTORE_StateCallback cb, void *cb_cls)
1516 struct Plugin *plugin = cls;
1517 int ret = GNUNET_SYSERR;
1519 const char *stmt = "select_state_one";
1521 struct GNUNET_PQ_QueryParam params_select[] = {
1522 GNUNET_PQ_query_param_auto_from_type (channel_key),
1523 GNUNET_PQ_query_param_string (name),
1524 GNUNET_PQ_query_param_end
1527 void *value_current = NULL;
1528 size_t value_size = 0;
1530 struct GNUNET_PQ_ResultSpec results[] = {
1531 GNUNET_PQ_result_spec_variable_size ("value_current", &value_current, &value_size),
1532 GNUNET_PQ_result_spec_end
1535 res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
1536 if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
1539 "PQexecPrepared", stmt))
1541 return GNUNET_SYSERR;
1544 if (PQntuples (res) == 0)
1550 ret = GNUNET_PQ_extract_result (res,
1553 if (GNUNET_OK != ret)
1556 return GNUNET_SYSERR;
1559 ret = cb (cb_cls, name, value_current,
1562 GNUNET_PQ_cleanup_result(results);
1570 * Retrieve all state variables for a channel with the given prefix.
1572 * @see GNUNET_PSYCSTORE_state_get_prefix()
1574 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1577 state_get_prefix (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1578 const char *name, GNUNET_PSYCSTORE_StateCallback cb,
1582 struct Plugin *plugin = cls;
1583 int ret = GNUNET_SYSERR;
1585 const char *stmt = "select_state_prefix";
1587 uint32_t name_len = (uint32_t) strlen (name);
1589 struct GNUNET_PQ_QueryParam params_select[] = {
1590 GNUNET_PQ_query_param_auto_from_type (channel_key),
1591 GNUNET_PQ_query_param_string (name),
1592 GNUNET_PQ_query_param_uint32 (&name_len),
1593 GNUNET_PQ_query_param_string (name),
1594 GNUNET_PQ_query_param_end
1598 void *value_current = NULL;
1599 size_t value_size = 0;
1601 struct GNUNET_PQ_ResultSpec results[] = {
1602 GNUNET_PQ_result_spec_string ("name", &name2),
1603 GNUNET_PQ_result_spec_variable_size ("value_current", &value_current, &value_size),
1604 GNUNET_PQ_result_spec_end
1609 res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
1610 if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
1613 "PQexecPrepared", stmt))
1618 if (PQntuples (res) == 0)
1625 if (GNUNET_OK != GNUNET_PQ_extract_result (res, results, 0))
1631 ret = cb (cb_cls, (const char *) name2,
1634 GNUNET_PQ_cleanup_result(results);
1636 while (ret == GNUNET_YES);
1645 * Retrieve all signed state variables for a channel.
1647 * @see GNUNET_PSYCSTORE_state_get_signed()
1649 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1652 state_get_signed (void *cls,
1653 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1654 GNUNET_PSYCSTORE_StateCallback cb, void *cb_cls)
1657 struct Plugin *plugin = cls;
1658 int ret = GNUNET_SYSERR;
1660 const char *stmt = "select_state_signed";
1662 struct GNUNET_PQ_QueryParam params_select[] = {
1663 GNUNET_PQ_query_param_auto_from_type (channel_key),
1664 GNUNET_PQ_query_param_end
1668 void *value_signed = NULL;
1669 size_t value_size = 0;
1671 struct GNUNET_PQ_ResultSpec results[] = {
1672 GNUNET_PQ_result_spec_string ("name", &name),
1673 GNUNET_PQ_result_spec_variable_size ("value_signed", &value_signed, &value_size),
1674 GNUNET_PQ_result_spec_end
1679 res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
1680 if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
1683 "PQexecPrepared", stmt))
1688 if (PQntuples (res) == 0)
1695 if (GNUNET_OK != GNUNET_PQ_extract_result (res, results, 0))
1701 ret = cb (cb_cls, (const char *) name,
1705 GNUNET_PQ_cleanup_result(results);
1707 while (ret == GNUNET_YES);
1716 * Entry point for the plugin.
1718 * @param cls The struct GNUNET_CONFIGURATION_Handle.
1719 * @return NULL on error, otherwise the plugin context
1722 libgnunet_plugin_psycstore_postgres_init (void *cls)
1724 static struct Plugin plugin;
1725 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1726 struct GNUNET_PSYCSTORE_PluginFunctions *api;
1728 if (NULL != plugin.cfg)
1729 return NULL; /* can only initialize once! */
1730 memset (&plugin, 0, sizeof (struct Plugin));
1732 if (GNUNET_OK != database_setup (&plugin))
1734 database_shutdown (&plugin);
1737 api = GNUNET_new (struct GNUNET_PSYCSTORE_PluginFunctions);
1739 api->membership_store = &postgres_membership_store;
1740 api->membership_test = &membership_test;
1741 api->fragment_store = &fragment_store;
1742 api->message_add_flags = &message_add_flags;
1743 api->fragment_get = &fragment_get;
1744 api->fragment_get_latest = &fragment_get_latest;
1745 api->message_get = &message_get;
1746 api->message_get_latest = &message_get_latest;
1747 api->message_get_fragment = &message_get_fragment;
1748 api->counters_message_get = &counters_message_get;
1749 api->counters_state_get = &counters_state_get;
1750 api->state_modify_begin = &state_modify_begin;
1751 api->state_modify_op = &state_modify_op;
1752 api->state_modify_end = &state_modify_end;
1753 api->state_sync_begin = &state_sync_begin;
1754 api->state_sync_assign = &state_sync_assign;
1755 api->state_sync_end = &state_sync_end;
1756 api->state_reset = &state_reset;
1757 api->state_update_signed = &state_update_signed;
1758 api->state_get = &state_get;
1759 api->state_get_prefix = &state_get_prefix;
1760 api->state_get_signed = &state_get_signed;
1762 LOG (GNUNET_ERROR_TYPE_INFO, _("Postgres database running\n"));
1768 * Exit point from the plugin.
1770 * @param cls The plugin context (as returned by "init")
1771 * @return Always NULL
1774 libgnunet_plugin_psycstore_postgres_done (void *cls)
1776 struct GNUNET_PSYCSTORE_PluginFunctions *api = cls;
1777 struct Plugin *plugin = api->cls;
1779 database_shutdown (plugin);
1782 LOG (GNUNET_ERROR_TYPE_DEBUG, "Postgres plugin has finished\n");
1786 /* end of plugin_psycstore_postgres.c */