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 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 " hash BYTEA NOT NULL DEFAULT '',"
194 " vhash BYTEA NOT NULL DEFAULT '',"
195 " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS");
196 if ( (ret == NULL) ||
197 ( (PQresultStatus (ret) != PGRES_COMMAND_OK) &&
198 (0 != strcmp ("42P07", /* duplicate table */
201 PG_DIAG_SQLSTATE)))))
203 (void) check_result (plugin,
204 ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090", __LINE__);
205 PQfinish (plugin->dbh);
207 return GNUNET_SYSERR;
209 if (PQresultStatus (ret) == PGRES_COMMAND_OK)
212 pq_exec (plugin, "CREATE INDEX idx_hash ON gn090 (hash)", __LINE__)) ||
214 pq_exec (plugin, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)",
217 pq_exec (plugin, "CREATE INDEX idx_prio ON gn090 (prio)", __LINE__))
219 pq_exec (plugin, "CREATE INDEX idx_expire ON gn090 (expire)", __LINE__))
221 pq_exec (plugin, "CREATE INDEX idx_comb3 ON gn090 (prio,anonLevel)",
225 (plugin, "CREATE INDEX idx_comb4 ON gn090 (prio,hash,anonLevel)",
228 pq_exec (plugin, "CREATE INDEX idx_comb7 ON gn090 (expire,hash)",
232 PQfinish (plugin->dbh);
234 return GNUNET_SYSERR;
238 ret = PQexec (plugin->dbh,
239 "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL");
241 check_result (plugin,
242 ret, PGRES_COMMAND_OK,
243 "ALTER TABLE", "gn090", __LINE__))
245 PQfinish (plugin->dbh);
247 return GNUNET_SYSERR;
250 ret = PQexec (plugin->dbh,
251 "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN");
253 check_result (plugin,
254 ret, PGRES_COMMAND_OK,
255 "ALTER TABLE", "gn090", __LINE__))
257 PQfinish (plugin->dbh);
259 return GNUNET_SYSERR;
262 ret = PQexec (plugin->dbh,
263 "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN");
265 check_result (plugin,
266 ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090", __LINE__))
268 PQfinish (plugin->dbh);
270 return GNUNET_SYSERR;
276 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
277 "WHERE hash=$1 AND vhash=$2 AND type=$3 "
278 "ORDER BY oid ASC LIMIT 1 OFFSET $4",
284 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
285 "WHERE hash=$1 AND type=$2 "
286 "ORDER BY oid ASC LIMIT 1 OFFSET $3",
292 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
293 "WHERE hash=$1 AND vhash=$2 "
294 "ORDER BY oid ASC LIMIT 1 OFFSET $3",
300 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
302 "ORDER BY oid ASC LIMIT 1 OFFSET $2",
308 "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, hash, vhash, value) "
309 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
315 "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END "
322 "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) "
328 "select_non_anonymous",
329 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
330 "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2",
335 "select_expiration_order",
336 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
337 "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) "
339 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
340 "ORDER BY prio ASC LIMIT 1) "
341 "ORDER BY expire ASC LIMIT 1",
346 "select_replication_order",
347 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " \
348 "ORDER BY repl DESC,RANDOM() LIMIT 1",
354 "DELETE FROM gn090 " "WHERE oid=$1",
358 PQfinish (plugin->dbh);
360 return GNUNET_SYSERR;
367 * Delete the row identified by the given rowid (qid
370 * @param plugin global context
371 * @param rowid which row to delete
372 * @return GNUNET_OK on success
375 delete_by_rowid (struct Plugin *plugin,
379 const char *paramValues[] = { (const char *) &browid };
380 int paramLengths[] = { sizeof (browid) };
381 const int paramFormats[] = { 1 };
384 browid = htonl (rowid);
385 ret = PQexecPrepared (plugin->dbh,
387 1, paramValues, paramLengths, paramFormats, 1);
389 check_result (plugin,
390 ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow",
393 return GNUNET_SYSERR;
401 * Get an estimate of how much space the database is
404 * @param cls our "struct Plugin*"
405 * @return number of bytes used on disk
407 static unsigned long long
408 postgres_plugin_estimate_size (void *cls)
410 struct Plugin *plugin = cls;
411 unsigned long long total;
414 ret = PQexecParams (plugin->dbh,
415 "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090",
416 0, NULL, NULL, NULL, NULL, 1);
417 if (GNUNET_OK != check_result (plugin,
426 if ((PQntuples (ret) != 1) ||
427 (PQnfields (ret) != 1) ||
428 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
434 total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
441 * Store an item in the datastore.
444 * @param key key for the item
445 * @param size number of bytes in data
446 * @param data content stored
447 * @param type type of the content
448 * @param priority priority of the content
449 * @param anonymity anonymity-level for the content
450 * @param replication replication-level for the content
451 * @param expiration expiration time for the content
452 * @param msg set to error message
453 * @return GNUNET_OK on success
456 postgres_plugin_put (void *cls,
457 const GNUNET_HashCode * key,
460 enum GNUNET_BLOCK_Type type,
463 uint32_t replication,
464 struct GNUNET_TIME_Absolute expiration,
467 struct Plugin *plugin = cls;
468 GNUNET_HashCode vhash;
470 uint32_t btype = htonl (type);
471 uint32_t bprio = htonl (priority);
472 uint32_t banon = htonl (anonymity);
473 uint32_t brepl = htonl (replication);
474 uint64_t bexpi = GNUNET_TIME_absolute_hton (expiration).abs_value__;
475 const char *paramValues[] = {
476 (const char *) &brepl,
477 (const char *) &btype,
478 (const char *) &bprio,
479 (const char *) &banon,
480 (const char *) &bexpi,
482 (const char *) &vhash,
485 int paramLengths[] = {
491 sizeof (GNUNET_HashCode),
492 sizeof (GNUNET_HashCode),
495 const int paramFormats[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
497 GNUNET_CRYPTO_hash (data, size, &vhash);
498 ret = PQexecPrepared (plugin->dbh,
499 "put", 8, paramValues, paramLengths, paramFormats, 1);
500 if (GNUNET_OK != check_result (plugin, ret,
502 "PQexecPrepared", "put", __LINE__))
503 return GNUNET_SYSERR;
505 plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
507 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
508 "datastore-postgres",
509 "Stored %u bytes in database\n",
510 (unsigned int) size);
517 * Function invoked to process the result and call
520 * @param plugin global plugin data
521 * @param proc function to call the value (once only).
522 * @param proc_cls closure for proc
523 * @param res result from exec
526 process_result (struct Plugin *plugin,
527 PluginDatumProcessor proc, void *proc_cls,
532 enum GNUNET_BLOCK_Type type;
537 struct GNUNET_TIME_Absolute expiration_time;
540 if (GNUNET_OK != check_result (plugin,
548 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
549 "datastore-postgres",
550 "Ending iteration (postgres error)\n");
553 NULL, 0, NULL, 0, 0, 0,
554 GNUNET_TIME_UNIT_ZERO_ABS, 0);
558 if (0 == PQntuples (res))
562 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
563 "datastore-postgres",
564 "Ending iteration (no more results)\n");
567 NULL, 0, NULL, 0, 0, 0,
568 GNUNET_TIME_UNIT_ZERO_ABS, 0);
572 if ((1 != PQntuples (res)) ||
573 (7 != PQnfields (res)) ||
574 (sizeof (uint32_t) != PQfsize (res, 0)) ||
575 (sizeof (uint32_t) != PQfsize (res, 6)))
579 NULL, 0, NULL, 0, 0, 0,
580 GNUNET_TIME_UNIT_ZERO_ABS, 0);
584 rowid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 6));
585 if ((sizeof (uint32_t) != PQfsize (res, 0)) ||
586 (sizeof (uint32_t) != PQfsize (res, 1)) ||
587 (sizeof (uint32_t) != PQfsize (res, 2)) ||
588 (sizeof (uint64_t) != PQfsize (res, 3)) ||
589 (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 4)) )
593 delete_by_rowid (plugin, rowid);
595 NULL, 0, NULL, 0, 0, 0,
596 GNUNET_TIME_UNIT_ZERO_ABS, 0);
600 type = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0));
601 priority = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
602 anonymity = ntohl ( *(uint32_t *) PQgetvalue (res, 0, 2));
603 expiration_time.abs_value = GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 3));
605 PQgetvalue (res, 0, 4),
606 sizeof (GNUNET_HashCode));
607 size = PQgetlength (res, 0, 5);
609 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
610 "datastore-postgres",
611 "Found result of size %u bytes and type %u in database\n",
613 (unsigned int) type);
615 iret = proc (proc_cls,
618 PQgetvalue (res, 0, 5),
619 (enum GNUNET_BLOCK_Type) type,
625 if (iret == GNUNET_NO)
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 "Processor asked for item %u to be removed.\n",
632 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
635 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
636 "datastore-postgres",
637 "Deleting %u bytes from database\n",
638 (unsigned int) size);
640 plugin->env->duc (plugin->env->cls,
641 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
643 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
644 "datastore-postgres",
645 "Deleted %u bytes from database\n",
646 (unsigned int) size);
654 * Iterate over the results for a particular key
658 * @param key maybe NULL (to match all entries)
659 * @param vhash hash of the value, maybe NULL (to
660 * match all values that have the right key).
661 * Note that for DBlocks there is no difference
662 * betwen key and vhash, but for other blocks
664 * @param type entries of which type are relevant?
665 * Use 0 for any type.
666 * @param iter function to call on each matching value;
667 * will be called once with a NULL value at the end
668 * @param iter_cls closure for iter
671 postgres_plugin_get_key (void *cls,
673 const GNUNET_HashCode *key,
674 const GNUNET_HashCode *vhash,
675 enum GNUNET_BLOCK_Type type,
676 PluginDatumProcessor proc, void *proc_cls)
678 struct Plugin *plugin = cls;
679 const int paramFormats[] = { 1, 1, 1, 1, 1 };
681 const char *paramValues[4];
689 GNUNET_assert (key != NULL);
690 paramValues[0] = (const char*) key;
691 paramLengths[0] = sizeof (GNUNET_HashCode);
692 btype = htonl (type);
697 paramValues[1] = (const char *) vhash;
698 paramLengths[1] = sizeof (GNUNET_HashCode);
699 paramValues[2] = (const char *) &btype;
700 paramLengths[2] = sizeof (btype);
701 paramValues[3] = (const char *) &blimit_off;
702 paramLengths[3] = sizeof (blimit_off);
705 ret = PQexecParams (plugin->dbh,
706 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2 AND type=$3",
715 paramValues[1] = (const char *) &btype;
716 paramLengths[1] = sizeof (btype);
717 paramValues[2] = (const char *) &blimit_off;
718 paramLengths[2] = sizeof (blimit_off);
721 ret = PQexecParams (plugin->dbh,
722 "SELECT count(*) FROM gn090 WHERE hash=$1 AND type=$2",
734 paramValues[1] = (const char *) vhash;
735 paramLengths[1] = sizeof (GNUNET_HashCode);
736 paramValues[2] = (const char *) &blimit_off;
737 paramLengths[2] = sizeof (blimit_off);
740 ret = PQexecParams (plugin->dbh,
741 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2",
750 paramValues[1] = (const char *) &blimit_off;
751 paramLengths[1] = sizeof (blimit_off);
754 ret = PQexecParams (plugin->dbh,
755 "SELECT count(*) FROM gn090 WHERE hash=$1",
763 if (GNUNET_OK != check_result (plugin,
771 NULL, 0, NULL, 0, 0, 0,
772 GNUNET_TIME_UNIT_ZERO_ABS, 0);
775 if ((PQntuples (ret) != 1) ||
776 (PQnfields (ret) != 1) ||
777 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
782 NULL, 0, NULL, 0, 0, 0,
783 GNUNET_TIME_UNIT_ZERO_ABS, 0);
786 total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
791 NULL, 0, NULL, 0, 0, 0,
792 GNUNET_TIME_UNIT_ZERO_ABS, 0);
795 blimit_off = GNUNET_htonll (offset % total);
796 ret = PQexecPrepared (plugin->dbh,
802 process_result (plugin,
809 * Select a subset of the items in the datastore and call
810 * the given iterator for each of them.
812 * @param cls our "struct Plugin*"
813 * @param type entries of which type should be considered?
814 * Use 0 for any type.
815 * @param iter function to call on each matching value;
816 * will be called once with a NULL value at the end
817 * @param iter_cls closure for iter
820 postgres_plugin_get_zero_anonymity (void *cls,
822 enum GNUNET_BLOCK_Type type,
823 PluginDatumProcessor proc, void *proc_cls)
825 struct Plugin *plugin = cls;
828 const int paramFormats[] = { 1, 1 };
829 int paramLengths[] = { sizeof (btype), sizeof (boff) };
830 const char *paramValues[] = { (const char*) &btype, (const char*) &boff };
833 btype = htonl ((uint32_t) type);
834 boff = GNUNET_htonll (offset);
835 ret = PQexecPrepared (plugin->dbh,
836 "select_non_anonymous",
841 process_result (plugin,
848 * Context for 'repl_iter' function.
856 struct Plugin *plugin;
859 * Function to call for the result (or the NULL).
861 PluginDatumProcessor proc;
871 * Wrapper for the iterator for 'sqlite_plugin_replication_get'.
872 * Decrements the replication counter and calls the original
876 * @param next_cls closure to pass to the "next" function.
877 * @param key key for the content
878 * @param size number of bytes in data
879 * @param data content stored
880 * @param type type of the content
881 * @param priority priority of the content
882 * @param anonymity anonymity-level for the content
883 * @param expiration expiration time for the content
884 * @param uid unique identifier for the datum;
885 * maybe 0 if no unique identifier is available
887 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
888 * (continue on call to "next", of course),
889 * GNUNET_NO to delete the item and continue (if supported)
892 repl_proc (void *cls,
893 const GNUNET_HashCode *key,
896 enum GNUNET_BLOCK_Type type,
899 struct GNUNET_TIME_Absolute expiration,
902 struct ReplCtx *rc = cls;
903 struct Plugin *plugin = rc->plugin;
908 ret = rc->proc (rc->proc_cls,
911 type, priority, anonymity, expiration,
915 boid = htonl ( (uint32_t) uid);
916 const char *paramValues[] = {
917 (const char *) &boid,
919 int paramLengths[] = {
922 const int paramFormats[] = { 1 };
923 qret = PQexecPrepared (plugin->dbh,
925 1, paramValues, paramLengths, paramFormats, 1);
926 if (GNUNET_OK != check_result (plugin,
930 "decrepl", __LINE__))
931 return GNUNET_SYSERR;
939 * Get a random item for replication. Returns a single, not expired, random item
940 * from those with the highest replication counters. The item's
941 * replication counter is decremented by one IF it was positive before.
942 * Call 'proc' with all values ZERO or NULL if the datastore is empty.
945 * @param proc function to call the value (once only).
946 * @param proc_cls closure for iter
949 postgres_plugin_get_replication (void *cls,
950 PluginDatumProcessor proc, void *proc_cls)
952 struct Plugin *plugin = cls;
958 rc.proc_cls = proc_cls;
959 ret = PQexecPrepared (plugin->dbh,
960 "select_replication_order",
962 NULL, NULL, NULL, 1);
963 process_result (plugin,
970 * Get a random item for expiration.
971 * Call 'iter' with all values ZERO or NULL if the datastore is empty.
974 * @param proc function to call the value (once only).
975 * @param proc_cls closure for iter
978 postgres_plugin_get_expiration (void *cls,
979 PluginDatumProcessor proc, void *proc_cls)
981 struct Plugin *plugin = cls;
983 const int paramFormats[] = { 1 };
984 int paramLengths[] = { sizeof (btime) };
985 const char *paramValues[] = { (const char*) &btime };
988 btime = GNUNET_htonll (GNUNET_TIME_absolute_get ().abs_value);
989 ret = PQexecPrepared (plugin->dbh,
990 "select_expiration_order",
996 process_result (plugin,
1003 * Update the priority for a particular key in the datastore. If
1004 * the expiration time in value is different than the time found in
1005 * the datastore, the higher value should be kept. For the
1006 * anonymity level, the lower value is to be used. The specified
1007 * priority should be added to the existing priority, ignoring the
1008 * priority in value.
1010 * Note that it is possible for multiple values to match this put.
1011 * In that case, all of the respective values are updated.
1013 * @param cls our "struct Plugin*"
1014 * @param uid unique identifier of the datum
1015 * @param delta by how much should the priority
1016 * change? If priority + delta < 0 the
1017 * priority should be set to 0 (never go
1019 * @param expire new expiration time should be the
1020 * MAX of any existing expiration time and
1022 * @param msg set to error message
1023 * @return GNUNET_OK on success
1026 postgres_plugin_update (void *cls,
1028 int delta, struct GNUNET_TIME_Absolute expire,
1031 struct Plugin *plugin = cls;
1033 int32_t bdelta = (int32_t) htonl ((uint32_t) delta);
1034 uint32_t boid = htonl ( (uint32_t) uid);
1035 uint64_t bexpire = GNUNET_TIME_absolute_hton (expire).abs_value__;
1036 const char *paramValues[] = {
1037 (const char *) &bdelta,
1038 (const char *) &bexpire,
1039 (const char *) &boid,
1041 int paramLengths[] = {
1046 const int paramFormats[] = { 1, 1, 1 };
1048 ret = PQexecPrepared (plugin->dbh,
1050 3, paramValues, paramLengths, paramFormats, 1);
1051 if (GNUNET_OK != check_result (plugin,
1054 "PQexecPrepared", "update", __LINE__))
1055 return GNUNET_SYSERR;
1065 postgres_plugin_drop (void *cls)
1067 struct Plugin *plugin = cls;
1069 pq_exec (plugin, "DROP TABLE gn090", __LINE__);
1074 * Entry point for the plugin.
1076 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
1077 * @return our "struct Plugin*"
1080 libgnunet_plugin_datastore_postgres_init (void *cls)
1082 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
1083 struct GNUNET_DATASTORE_PluginFunctions *api;
1084 struct Plugin *plugin;
1086 plugin = GNUNET_malloc (sizeof (struct Plugin));
1088 if (GNUNET_OK != init_connection (plugin))
1090 GNUNET_free (plugin);
1093 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1095 api->estimate_size = &postgres_plugin_estimate_size;
1096 api->put = &postgres_plugin_put;
1097 api->update = &postgres_plugin_update;
1098 api->get_key = &postgres_plugin_get_key;
1099 api->get_replication = &postgres_plugin_get_replication;
1100 api->get_expiration = &postgres_plugin_get_expiration;
1101 api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity;
1102 api->drop = &postgres_plugin_drop;
1103 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1104 "datastore-postgres",
1105 _("Postgres database running\n"));
1111 * Exit point from the plugin.
1112 * @param cls our "struct Plugin*"
1113 * @return always NULL
1116 libgnunet_plugin_datastore_postgres_done (void *cls)
1118 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1119 struct Plugin *plugin = api->cls;
1121 PQfinish (plugin->dbh);
1122 GNUNET_free (plugin);
1127 /* end of plugin_datastore_postgres.c */