2 This file is part of GNUnet
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file datastore/plugin_datastore_postgres.c
23 * @brief postgres-based datastore backend
24 * @author Christian Grothoff
28 #include "gnunet_datastore_plugin.h"
29 #include <postgresql/libpq-fe.h>
31 #define DEBUG_POSTGRES GNUNET_NO
34 * After how many ms "busy" should a DB operation fail for good?
35 * A low value makes sure that we are more responsive to requests
36 * (especially PUTs). A high value guarantees a higher success
37 * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
39 * The default value of 1s should ensure that users do not experience
40 * huge latencies while at the same time allowing operations to succeed
41 * with reasonable probability.
43 #define BUSY_TIMEOUT GNUNET_TIME_UNIT_SECONDS
47 * Context for all functions in this plugin.
52 * Our execution environment.
54 struct GNUNET_DATASTORE_PluginEnvironment *env;
57 * Native Postgres database handle.
65 * Check if the result obtained from Postgres has
66 * the desired status code. If not, log an error, clear the
67 * result and return GNUNET_SYSERR.
69 * @param plugin global context
70 * @param ret result to check
71 * @param expected_status expected return value
72 * @param command name of SQL command that was run
73 * @param args arguments to SQL command
74 * @param line line number for error reporting
75 * @return GNUNET_OK if the result is acceptable
78 check_result (struct Plugin *plugin,
81 const char *command, const char *args, int line)
85 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
87 "Postgres failed to allocate result for `%s:%s' at %d\n",
91 if (PQresultStatus (ret) != expected_status)
93 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
95 _("`%s:%s' failed at %s:%d with error: %s"),
96 command, args, __FILE__, line, PQerrorMessage (plugin->dbh));
104 * Run simple SQL statement (without results).
106 * @param plugin global context
107 * @param sql statement to run
108 * @param line code line for error reporting
111 pq_exec (struct Plugin *plugin,
112 const char *sql, int line)
115 ret = PQexec (plugin->dbh, sql);
116 if (GNUNET_OK != check_result (plugin,
118 PGRES_COMMAND_OK, "PQexec", sql, line))
119 return GNUNET_SYSERR;
125 * Prepare SQL statement.
127 * @param plugin global context
128 * @param name name for the prepared SQL statement
129 * @param sql SQL code to prepare
130 * @param nparams number of parameters in sql
131 * @param line code line for error reporting
132 * @return GNUNET_OK on success
135 pq_prepare (struct Plugin *plugin,
136 const char *name, const char *sql, int nparams, int line)
139 ret = PQprepare (plugin->dbh, name, sql, nparams, NULL);
141 check_result (plugin,
142 ret, PGRES_COMMAND_OK, "PQprepare", sql, line))
143 return GNUNET_SYSERR;
149 * @brief Get a database handle
151 * @param plugin global context
152 * @return GNUNET_OK on success, GNUNET_SYSERR on error
155 init_connection (struct Plugin *plugin)
160 /* Open database and precompile statements */
162 (void) GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
163 "datastore-postgres",
166 plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
167 if (NULL == plugin->dbh)
169 /* FIXME: warn about out-of-memory? */
170 GNUNET_free_non_null (conninfo);
171 return GNUNET_SYSERR;
173 if (PQstatus (plugin->dbh) != CONNECTION_OK)
175 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
176 "datastore-postgres",
177 _("Unable to initialize Postgres with configuration `%s': %s"),
179 PQerrorMessage (plugin->dbh));
180 PQfinish (plugin->dbh);
182 GNUNET_free_non_null (conninfo);
183 return GNUNET_SYSERR;
185 GNUNET_free_non_null (conninfo);
186 ret = PQexec (plugin->dbh,
187 "CREATE TABLE gn090 ("
188 " repl INTEGER NOT NULL DEFAULT 0,"
189 " type INTEGER NOT NULL DEFAULT 0,"
190 " prio INTEGER NOT NULL DEFAULT 0,"
191 " anonLevel INTEGER NOT NULL DEFAULT 0,"
192 " expire BIGINT NOT NULL DEFAULT 0,"
193 " rvalue BIGINT NOT NULL DEFAULT 0,"
194 " hash BYTEA NOT NULL DEFAULT '',"
195 " vhash BYTEA NOT NULL DEFAULT '',"
196 " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS");
197 if ( (ret == NULL) ||
198 ( (PQresultStatus (ret) != PGRES_COMMAND_OK) &&
199 (0 != strcmp ("42P07", /* duplicate table */
202 PG_DIAG_SQLSTATE)))))
204 (void) check_result (plugin,
205 ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090", __LINE__);
206 PQfinish (plugin->dbh);
208 return GNUNET_SYSERR;
210 if (PQresultStatus (ret) == PGRES_COMMAND_OK)
213 pq_exec (plugin, "CREATE INDEX idx_hash ON gn090 (hash)", __LINE__)) ||
215 pq_exec (plugin, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)",
218 pq_exec (plugin, "CREATE INDEX idx_prio ON gn090 (prio)", __LINE__))
220 pq_exec (plugin, "CREATE INDEX idx_expire ON gn090 (expire)", __LINE__))
222 pq_exec (plugin, "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)",
226 (plugin, "CREATE INDEX idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)",
230 (plugin, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)",
233 pq_exec (plugin, "CREATE INDEX idx_expire_hash ON gn090 (expire,hash)",
237 PQfinish (plugin->dbh);
239 return GNUNET_SYSERR;
243 ret = PQexec (plugin->dbh,
244 "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL");
246 check_result (plugin,
247 ret, PGRES_COMMAND_OK,
248 "ALTER TABLE", "gn090", __LINE__))
250 PQfinish (plugin->dbh);
252 return GNUNET_SYSERR;
255 ret = PQexec (plugin->dbh,
256 "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN");
258 check_result (plugin,
259 ret, PGRES_COMMAND_OK,
260 "ALTER TABLE", "gn090", __LINE__))
262 PQfinish (plugin->dbh);
264 return GNUNET_SYSERR;
267 ret = PQexec (plugin->dbh,
268 "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN");
270 check_result (plugin,
271 ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090", __LINE__))
273 PQfinish (plugin->dbh);
275 return GNUNET_SYSERR;
281 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
282 "WHERE hash=$1 AND vhash=$2 AND type=$3 "
283 "ORDER BY oid ASC LIMIT 1 OFFSET $4",
289 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
290 "WHERE hash=$1 AND type=$2 "
291 "ORDER BY oid ASC LIMIT 1 OFFSET $3",
297 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
298 "WHERE hash=$1 AND vhash=$2 "
299 "ORDER BY oid ASC LIMIT 1 OFFSET $3",
305 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
307 "ORDER BY oid ASC LIMIT 1 OFFSET $2",
313 "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
314 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
320 "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END "
327 "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) "
333 "select_non_anonymous",
334 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
335 "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2",
340 "select_expiration_order",
341 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
342 "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) "
344 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
345 "ORDER BY prio ASC LIMIT 1) "
346 "ORDER BY expire ASC LIMIT 1",
351 "select_replication_order",
352 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " \
353 "ORDER BY repl DESC,RANDOM() LIMIT 1",
359 "DELETE FROM gn090 " "WHERE oid=$1",
363 PQfinish (plugin->dbh);
365 return GNUNET_SYSERR;
372 * Delete the row identified by the given rowid (qid
375 * @param plugin global context
376 * @param rowid which row to delete
377 * @return GNUNET_OK on success
380 delete_by_rowid (struct Plugin *plugin,
384 const char *paramValues[] = { (const char *) &browid };
385 int paramLengths[] = { sizeof (browid) };
386 const int paramFormats[] = { 1 };
389 browid = htonl (rowid);
390 ret = PQexecPrepared (plugin->dbh,
392 1, paramValues, paramLengths, paramFormats, 1);
394 check_result (plugin,
395 ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow",
398 return GNUNET_SYSERR;
406 * Get an estimate of how much space the database is
409 * @param cls our "struct Plugin*"
410 * @return number of bytes used on disk
412 static unsigned long long
413 postgres_plugin_estimate_size (void *cls)
415 struct Plugin *plugin = cls;
416 unsigned long long total;
419 ret = PQexecParams (plugin->dbh,
420 "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090",
421 0, NULL, NULL, NULL, NULL, 1);
422 if (GNUNET_OK != check_result (plugin,
431 if ((PQntuples (ret) != 1) ||
432 (PQnfields (ret) != 1) ||
433 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
439 total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
446 * Store an item in the datastore.
449 * @param key key for the item
450 * @param size number of bytes in data
451 * @param data content stored
452 * @param type type of the content
453 * @param priority priority of the content
454 * @param anonymity anonymity-level for the content
455 * @param replication replication-level for the content
456 * @param expiration expiration time for the content
457 * @param msg set to error message
458 * @return GNUNET_OK on success
461 postgres_plugin_put (void *cls,
462 const GNUNET_HashCode * key,
465 enum GNUNET_BLOCK_Type type,
468 uint32_t replication,
469 struct GNUNET_TIME_Absolute expiration,
472 struct Plugin *plugin = cls;
473 GNUNET_HashCode vhash;
475 uint32_t btype = htonl (type);
476 uint32_t bprio = htonl (priority);
477 uint32_t banon = htonl (anonymity);
478 uint32_t brepl = htonl (replication);
479 uint64_t bexpi = GNUNET_TIME_absolute_hton (expiration).abs_value__;
480 uint64_t rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
481 const char *paramValues[] = {
482 (const char *) &brepl,
483 (const char *) &btype,
484 (const char *) &bprio,
485 (const char *) &banon,
486 (const char *) &bexpi,
487 (const char *) &rvalue,
489 (const char *) &vhash,
492 int paramLengths[] = {
499 sizeof (GNUNET_HashCode),
500 sizeof (GNUNET_HashCode),
503 const int paramFormats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
505 GNUNET_CRYPTO_hash (data, size, &vhash);
506 ret = PQexecPrepared (plugin->dbh,
507 "put", 9, paramValues, paramLengths, paramFormats, 1);
508 if (GNUNET_OK != check_result (plugin, ret,
510 "PQexecPrepared", "put", __LINE__))
511 return GNUNET_SYSERR;
513 plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
515 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
516 "datastore-postgres",
517 "Stored %u bytes in database\n",
518 (unsigned int) size);
525 * Function invoked to process the result and call
528 * @param plugin global plugin data
529 * @param proc function to call the value (once only).
530 * @param proc_cls closure for proc
531 * @param res result from exec
532 * @param line line number for error messages
535 process_result (struct Plugin *plugin,
536 PluginDatumProcessor proc, void *proc_cls,
541 enum GNUNET_BLOCK_Type type;
546 struct GNUNET_TIME_Absolute expiration_time;
549 if (GNUNET_OK != check_result (plugin,
557 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
558 "datastore-postgres",
559 "Ending iteration (postgres error)\n");
562 NULL, 0, NULL, 0, 0, 0,
563 GNUNET_TIME_UNIT_ZERO_ABS, 0);
567 if (0 == PQntuples (res))
571 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
572 "datastore-postgres",
573 "Ending iteration (no more results)\n");
576 NULL, 0, NULL, 0, 0, 0,
577 GNUNET_TIME_UNIT_ZERO_ABS, 0);
581 if ((1 != PQntuples (res)) ||
582 (7 != PQnfields (res)) ||
583 (sizeof (uint32_t) != PQfsize (res, 0)) ||
584 (sizeof (uint32_t) != PQfsize (res, 6)))
588 NULL, 0, NULL, 0, 0, 0,
589 GNUNET_TIME_UNIT_ZERO_ABS, 0);
593 rowid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 6));
594 if ((sizeof (uint32_t) != PQfsize (res, 0)) ||
595 (sizeof (uint32_t) != PQfsize (res, 1)) ||
596 (sizeof (uint32_t) != PQfsize (res, 2)) ||
597 (sizeof (uint64_t) != PQfsize (res, 3)) ||
598 (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 4)) )
602 delete_by_rowid (plugin, rowid);
604 NULL, 0, NULL, 0, 0, 0,
605 GNUNET_TIME_UNIT_ZERO_ABS, 0);
609 type = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0));
610 priority = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
611 anonymity = ntohl ( *(uint32_t *) PQgetvalue (res, 0, 2));
612 expiration_time.abs_value = GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 3));
614 PQgetvalue (res, 0, 4),
615 sizeof (GNUNET_HashCode));
616 size = PQgetlength (res, 0, 5);
618 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
619 "datastore-postgres",
620 "Found result of size %u bytes and type %u in database\n",
622 (unsigned int) type);
624 iret = proc (proc_cls,
627 PQgetvalue (res, 0, 5),
628 (enum GNUNET_BLOCK_Type) type,
634 if (iret == GNUNET_NO)
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "Processor asked for item %u to be removed.\n",
641 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
644 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
645 "datastore-postgres",
646 "Deleting %u bytes from database\n",
647 (unsigned int) size);
649 plugin->env->duc (plugin->env->cls,
650 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
652 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
653 "datastore-postgres",
654 "Deleted %u bytes from database\n",
655 (unsigned int) size);
663 * Iterate over the results for a particular key
667 * @param offset offset of the result (modulo num-results);
668 * specific ordering does not matter for the offset
669 * @param key maybe NULL (to match all entries)
670 * @param vhash hash of the value, maybe NULL (to
671 * match all values that have the right key).
672 * Note that for DBlocks there is no difference
673 * betwen key and vhash, but for other blocks
675 * @param type entries of which type are relevant?
676 * Use 0 for any type.
677 * @param proc function to call on the matching value;
678 * will be called once with a NULL if no value matches
679 * @param proc_cls closure for iter
682 postgres_plugin_get_key (void *cls,
684 const GNUNET_HashCode *key,
685 const GNUNET_HashCode *vhash,
686 enum GNUNET_BLOCK_Type type,
687 PluginDatumProcessor proc, void *proc_cls)
689 struct Plugin *plugin = cls;
690 const int paramFormats[] = { 1, 1, 1, 1, 1 };
692 const char *paramValues[4];
700 GNUNET_assert (key != NULL);
701 paramValues[0] = (const char*) key;
702 paramLengths[0] = sizeof (GNUNET_HashCode);
703 btype = htonl (type);
708 paramValues[1] = (const char *) vhash;
709 paramLengths[1] = sizeof (GNUNET_HashCode);
710 paramValues[2] = (const char *) &btype;
711 paramLengths[2] = sizeof (btype);
712 paramValues[3] = (const char *) &blimit_off;
713 paramLengths[3] = sizeof (blimit_off);
716 ret = PQexecParams (plugin->dbh,
717 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2 AND type=$3",
726 paramValues[1] = (const char *) &btype;
727 paramLengths[1] = sizeof (btype);
728 paramValues[2] = (const char *) &blimit_off;
729 paramLengths[2] = sizeof (blimit_off);
732 ret = PQexecParams (plugin->dbh,
733 "SELECT count(*) FROM gn090 WHERE hash=$1 AND type=$2",
745 paramValues[1] = (const char *) vhash;
746 paramLengths[1] = sizeof (GNUNET_HashCode);
747 paramValues[2] = (const char *) &blimit_off;
748 paramLengths[2] = sizeof (blimit_off);
751 ret = PQexecParams (plugin->dbh,
752 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2",
761 paramValues[1] = (const char *) &blimit_off;
762 paramLengths[1] = sizeof (blimit_off);
765 ret = PQexecParams (plugin->dbh,
766 "SELECT count(*) FROM gn090 WHERE hash=$1",
774 if (GNUNET_OK != check_result (plugin,
782 NULL, 0, NULL, 0, 0, 0,
783 GNUNET_TIME_UNIT_ZERO_ABS, 0);
786 if ((PQntuples (ret) != 1) ||
787 (PQnfields (ret) != 1) ||
788 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
793 NULL, 0, NULL, 0, 0, 0,
794 GNUNET_TIME_UNIT_ZERO_ABS, 0);
797 total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
802 NULL, 0, NULL, 0, 0, 0,
803 GNUNET_TIME_UNIT_ZERO_ABS, 0);
806 blimit_off = GNUNET_htonll (offset % total);
807 ret = PQexecPrepared (plugin->dbh,
813 process_result (plugin,
820 * Select a subset of the items in the datastore and call
821 * the given iterator for each of them.
823 * @param cls our "struct Plugin*"
824 * @param type entries of which type should be considered?
825 * Use 0 for any type.
826 * @param proc function to call on the matching value;
827 * will be called with a NULL if no value matches
828 * @param proc_cls closure for proc
831 postgres_plugin_get_zero_anonymity (void *cls,
833 enum GNUNET_BLOCK_Type type,
834 PluginDatumProcessor proc, void *proc_cls)
836 struct Plugin *plugin = cls;
839 const int paramFormats[] = { 1, 1 };
840 int paramLengths[] = { sizeof (btype), sizeof (boff) };
841 const char *paramValues[] = { (const char*) &btype, (const char*) &boff };
844 btype = htonl ((uint32_t) type);
845 boff = GNUNET_htonll (offset);
846 ret = PQexecPrepared (plugin->dbh,
847 "select_non_anonymous",
852 process_result (plugin,
859 * Context for 'repl_iter' function.
867 struct Plugin *plugin;
870 * Function to call for the result (or the NULL).
872 PluginDatumProcessor proc;
882 * Wrapper for the iterator for 'sqlite_plugin_replication_get'.
883 * Decrements the replication counter and calls the original
887 * @param next_cls closure to pass to the "next" function.
888 * @param key key for the content
889 * @param size number of bytes in data
890 * @param data content stored
891 * @param type type of the content
892 * @param priority priority of the content
893 * @param anonymity anonymity-level for the content
894 * @param expiration expiration time for the content
895 * @param uid unique identifier for the datum;
896 * maybe 0 if no unique identifier is available
898 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
899 * (continue on call to "next", of course),
900 * GNUNET_NO to delete the item and continue (if supported)
903 repl_proc (void *cls,
904 const GNUNET_HashCode *key,
907 enum GNUNET_BLOCK_Type type,
910 struct GNUNET_TIME_Absolute expiration,
913 struct ReplCtx *rc = cls;
914 struct Plugin *plugin = rc->plugin;
919 ret = rc->proc (rc->proc_cls,
922 type, priority, anonymity, expiration,
926 boid = htonl ( (uint32_t) uid);
927 const char *paramValues[] = {
928 (const char *) &boid,
930 int paramLengths[] = {
933 const int paramFormats[] = { 1 };
934 qret = PQexecPrepared (plugin->dbh,
936 1, paramValues, paramLengths, paramFormats, 1);
937 if (GNUNET_OK != check_result (plugin,
941 "decrepl", __LINE__))
942 return GNUNET_SYSERR;
950 * Get a random item for replication. Returns a single, not expired, random item
951 * from those with the highest replication counters. The item's
952 * replication counter is decremented by one IF it was positive before.
953 * Call 'proc' with all values ZERO or NULL if the datastore is empty.
956 * @param proc function to call the value (once only).
957 * @param proc_cls closure for proc
960 postgres_plugin_get_replication (void *cls,
961 PluginDatumProcessor proc, void *proc_cls)
963 struct Plugin *plugin = cls;
969 rc.proc_cls = proc_cls;
970 ret = PQexecPrepared (plugin->dbh,
971 "select_replication_order",
973 NULL, NULL, NULL, 1);
974 process_result (plugin,
981 * Get a random item for expiration.
982 * Call 'proc' with all values ZERO or NULL if the datastore is empty.
985 * @param proc function to call the value (once only).
986 * @param proc_cls closure for proc
989 postgres_plugin_get_expiration (void *cls,
990 PluginDatumProcessor proc, void *proc_cls)
992 struct Plugin *plugin = cls;
994 const int paramFormats[] = { 1 };
995 int paramLengths[] = { sizeof (btime) };
996 const char *paramValues[] = { (const char*) &btime };
999 btime = GNUNET_htonll (GNUNET_TIME_absolute_get ().abs_value);
1000 ret = PQexecPrepared (plugin->dbh,
1001 "select_expiration_order",
1007 process_result (plugin,
1014 * Update the priority for a particular key in the datastore. If
1015 * the expiration time in value is different than the time found in
1016 * the datastore, the higher value should be kept. For the
1017 * anonymity level, the lower value is to be used. The specified
1018 * priority should be added to the existing priority, ignoring the
1019 * priority in value.
1021 * Note that it is possible for multiple values to match this put.
1022 * In that case, all of the respective values are updated.
1024 * @param cls our "struct Plugin*"
1025 * @param uid unique identifier of the datum
1026 * @param delta by how much should the priority
1027 * change? If priority + delta < 0 the
1028 * priority should be set to 0 (never go
1030 * @param expire new expiration time should be the
1031 * MAX of any existing expiration time and
1033 * @param msg set to error message
1034 * @return GNUNET_OK on success
1037 postgres_plugin_update (void *cls,
1039 int delta, struct GNUNET_TIME_Absolute expire,
1042 struct Plugin *plugin = cls;
1044 int32_t bdelta = (int32_t) htonl ((uint32_t) delta);
1045 uint32_t boid = htonl ( (uint32_t) uid);
1046 uint64_t bexpire = GNUNET_TIME_absolute_hton (expire).abs_value__;
1047 const char *paramValues[] = {
1048 (const char *) &bdelta,
1049 (const char *) &bexpire,
1050 (const char *) &boid,
1052 int paramLengths[] = {
1057 const int paramFormats[] = { 1, 1, 1 };
1059 ret = PQexecPrepared (plugin->dbh,
1061 3, paramValues, paramLengths, paramFormats, 1);
1062 if (GNUNET_OK != check_result (plugin,
1065 "PQexecPrepared", "update", __LINE__))
1066 return GNUNET_SYSERR;
1076 postgres_plugin_drop (void *cls)
1078 struct Plugin *plugin = cls;
1080 pq_exec (plugin, "DROP TABLE gn090", __LINE__);
1085 * Entry point for the plugin.
1087 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
1088 * @return our "struct Plugin*"
1091 libgnunet_plugin_datastore_postgres_init (void *cls)
1093 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
1094 struct GNUNET_DATASTORE_PluginFunctions *api;
1095 struct Plugin *plugin;
1097 plugin = GNUNET_malloc (sizeof (struct Plugin));
1099 if (GNUNET_OK != init_connection (plugin))
1101 GNUNET_free (plugin);
1104 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1106 api->estimate_size = &postgres_plugin_estimate_size;
1107 api->put = &postgres_plugin_put;
1108 api->update = &postgres_plugin_update;
1109 api->get_key = &postgres_plugin_get_key;
1110 api->get_replication = &postgres_plugin_get_replication;
1111 api->get_expiration = &postgres_plugin_get_expiration;
1112 api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity;
1113 api->drop = &postgres_plugin_drop;
1114 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1115 "datastore-postgres",
1116 _("Postgres database running\n"));
1122 * Exit point from the plugin.
1123 * @param cls our "struct Plugin*"
1124 * @return always NULL
1127 libgnunet_plugin_datastore_postgres_done (void *cls)
1129 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1130 struct Plugin *plugin = api->cls;
1132 PQfinish (plugin->dbh);
1133 GNUNET_free (plugin);
1138 /* end of plugin_datastore_postgres.c */