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,
531 enum GNUNET_BLOCK_Type type;
536 struct GNUNET_TIME_Absolute expiration_time;
539 if (GNUNET_OK != check_result (plugin,
547 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
548 "datastore-postgres",
549 "Ending iteration (postgres error)\n");
552 NULL, 0, NULL, 0, 0, 0,
553 GNUNET_TIME_UNIT_ZERO_ABS, 0);
557 if (0 == PQntuples (res))
561 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
562 "datastore-postgres",
563 "Ending iteration (no more results)\n");
566 NULL, 0, NULL, 0, 0, 0,
567 GNUNET_TIME_UNIT_ZERO_ABS, 0);
571 if ((1 != PQntuples (res)) ||
572 (7 != PQnfields (res)) ||
573 (sizeof (uint32_t) != PQfsize (res, 0)) ||
574 (sizeof (uint32_t) != PQfsize (res, 6)))
578 NULL, 0, NULL, 0, 0, 0,
579 GNUNET_TIME_UNIT_ZERO_ABS, 0);
583 rowid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 6));
584 if ((sizeof (uint32_t) != PQfsize (res, 0)) ||
585 (sizeof (uint32_t) != PQfsize (res, 1)) ||
586 (sizeof (uint32_t) != PQfsize (res, 2)) ||
587 (sizeof (uint64_t) != PQfsize (res, 3)) ||
588 (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 4)) )
592 delete_by_rowid (plugin, rowid);
594 NULL, 0, NULL, 0, 0, 0,
595 GNUNET_TIME_UNIT_ZERO_ABS, 0);
599 type = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0));
600 priority = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
601 anonymity = ntohl ( *(uint32_t *) PQgetvalue (res, 0, 2));
602 expiration_time.abs_value = GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 3));
604 PQgetvalue (res, 0, 4),
605 sizeof (GNUNET_HashCode));
606 size = PQgetlength (res, 0, 5);
608 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
609 "datastore-postgres",
610 "Found result of size %u bytes and type %u in database\n",
612 (unsigned int) type);
614 iret = proc (proc_cls,
617 PQgetvalue (res, 0, 5),
618 (enum GNUNET_BLOCK_Type) type,
624 if (iret == GNUNET_NO)
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628 "Processor asked for item %u to be removed.\n",
631 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
634 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
635 "datastore-postgres",
636 "Deleting %u bytes from database\n",
637 (unsigned int) size);
639 plugin->env->duc (plugin->env->cls,
640 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
642 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
643 "datastore-postgres",
644 "Deleted %u bytes from database\n",
645 (unsigned int) size);
653 * Iterate over the results for a particular key
657 * @param key maybe NULL (to match all entries)
658 * @param vhash hash of the value, maybe NULL (to
659 * match all values that have the right key).
660 * Note that for DBlocks there is no difference
661 * betwen key and vhash, but for other blocks
663 * @param type entries of which type are relevant?
664 * Use 0 for any type.
665 * @param iter function to call on each matching value;
666 * will be called once with a NULL value at the end
667 * @param iter_cls closure for iter
670 postgres_plugin_get_key (void *cls,
672 const GNUNET_HashCode *key,
673 const GNUNET_HashCode *vhash,
674 enum GNUNET_BLOCK_Type type,
675 PluginDatumProcessor proc, void *proc_cls)
677 struct Plugin *plugin = cls;
678 const int paramFormats[] = { 1, 1, 1, 1, 1 };
680 const char *paramValues[4];
688 GNUNET_assert (key != NULL);
689 paramValues[0] = (const char*) key;
690 paramLengths[0] = sizeof (GNUNET_HashCode);
691 btype = htonl (type);
696 paramValues[1] = (const char *) vhash;
697 paramLengths[1] = sizeof (GNUNET_HashCode);
698 paramValues[2] = (const char *) &btype;
699 paramLengths[2] = sizeof (btype);
700 paramValues[3] = (const char *) &blimit_off;
701 paramLengths[3] = sizeof (blimit_off);
704 ret = PQexecParams (plugin->dbh,
705 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2 AND type=$3",
714 paramValues[1] = (const char *) &btype;
715 paramLengths[1] = sizeof (btype);
716 paramValues[2] = (const char *) &blimit_off;
717 paramLengths[2] = sizeof (blimit_off);
720 ret = PQexecParams (plugin->dbh,
721 "SELECT count(*) FROM gn090 WHERE hash=$1 AND type=$2",
733 paramValues[1] = (const char *) vhash;
734 paramLengths[1] = sizeof (GNUNET_HashCode);
735 paramValues[2] = (const char *) &blimit_off;
736 paramLengths[2] = sizeof (blimit_off);
739 ret = PQexecParams (plugin->dbh,
740 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2",
749 paramValues[1] = (const char *) &blimit_off;
750 paramLengths[1] = sizeof (blimit_off);
753 ret = PQexecParams (plugin->dbh,
754 "SELECT count(*) FROM gn090 WHERE hash=$1",
762 if (GNUNET_OK != check_result (plugin,
770 NULL, 0, NULL, 0, 0, 0,
771 GNUNET_TIME_UNIT_ZERO_ABS, 0);
774 if ((PQntuples (ret) != 1) ||
775 (PQnfields (ret) != 1) ||
776 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
781 NULL, 0, NULL, 0, 0, 0,
782 GNUNET_TIME_UNIT_ZERO_ABS, 0);
785 total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
790 NULL, 0, NULL, 0, 0, 0,
791 GNUNET_TIME_UNIT_ZERO_ABS, 0);
794 blimit_off = GNUNET_htonll (offset % total);
795 ret = PQexecPrepared (plugin->dbh,
801 process_result (plugin,
808 * Select a subset of the items in the datastore and call
809 * the given iterator for each of them.
811 * @param cls our "struct Plugin*"
812 * @param type entries of which type should be considered?
813 * Use 0 for any type.
814 * @param iter function to call on each matching value;
815 * will be called once with a NULL value at the end
816 * @param iter_cls closure for iter
819 postgres_plugin_get_zero_anonymity (void *cls,
821 enum GNUNET_BLOCK_Type type,
822 PluginDatumProcessor proc, void *proc_cls)
824 struct Plugin *plugin = cls;
827 const int paramFormats[] = { 1, 1 };
828 int paramLengths[] = { sizeof (btype), sizeof (boff) };
829 const char *paramValues[] = { (const char*) &btype, (const char*) &boff };
832 btype = htonl ((uint32_t) type);
833 boff = GNUNET_htonll (offset);
834 ret = PQexecPrepared (plugin->dbh,
835 "select_non_anonymous",
840 process_result (plugin,
847 * Context for 'repl_iter' function.
855 struct Plugin *plugin;
858 * Function to call for the result (or the NULL).
860 PluginDatumProcessor proc;
870 * Wrapper for the iterator for 'sqlite_plugin_replication_get'.
871 * Decrements the replication counter and calls the original
875 * @param next_cls closure to pass to the "next" function.
876 * @param key key for the content
877 * @param size number of bytes in data
878 * @param data content stored
879 * @param type type of the content
880 * @param priority priority of the content
881 * @param anonymity anonymity-level for the content
882 * @param expiration expiration time for the content
883 * @param uid unique identifier for the datum;
884 * maybe 0 if no unique identifier is available
886 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
887 * (continue on call to "next", of course),
888 * GNUNET_NO to delete the item and continue (if supported)
891 repl_proc (void *cls,
892 const GNUNET_HashCode *key,
895 enum GNUNET_BLOCK_Type type,
898 struct GNUNET_TIME_Absolute expiration,
901 struct ReplCtx *rc = cls;
902 struct Plugin *plugin = rc->plugin;
907 ret = rc->proc (rc->proc_cls,
910 type, priority, anonymity, expiration,
914 boid = htonl ( (uint32_t) uid);
915 const char *paramValues[] = {
916 (const char *) &boid,
918 int paramLengths[] = {
921 const int paramFormats[] = { 1 };
922 qret = PQexecPrepared (plugin->dbh,
924 1, paramValues, paramLengths, paramFormats, 1);
925 if (GNUNET_OK != check_result (plugin,
929 "decrepl", __LINE__))
930 return GNUNET_SYSERR;
938 * Get a random item for replication. Returns a single, not expired, random item
939 * from those with the highest replication counters. The item's
940 * replication counter is decremented by one IF it was positive before.
941 * Call 'proc' with all values ZERO or NULL if the datastore is empty.
944 * @param proc function to call the value (once only).
945 * @param proc_cls closure for iter
948 postgres_plugin_get_replication (void *cls,
949 PluginDatumProcessor proc, void *proc_cls)
951 struct Plugin *plugin = cls;
957 rc.proc_cls = proc_cls;
958 ret = PQexecPrepared (plugin->dbh,
959 "select_replication_order",
961 NULL, NULL, NULL, 1);
962 process_result (plugin,
969 * Get a random item for expiration.
970 * Call 'iter' with all values ZERO or NULL if the datastore is empty.
973 * @param proc function to call the value (once only).
974 * @param proc_cls closure for iter
977 postgres_plugin_get_expiration (void *cls,
978 PluginDatumProcessor proc, void *proc_cls)
980 struct Plugin *plugin = cls;
982 const int paramFormats[] = { 1 };
983 int paramLengths[] = { sizeof (btime) };
984 const char *paramValues[] = { (const char*) &btime };
987 btime = GNUNET_htonll (GNUNET_TIME_absolute_get ().abs_value);
988 ret = PQexecPrepared (plugin->dbh,
989 "select_expiration_order",
995 process_result (plugin,
1002 * Update the priority for a particular key in the datastore. If
1003 * the expiration time in value is different than the time found in
1004 * the datastore, the higher value should be kept. For the
1005 * anonymity level, the lower value is to be used. The specified
1006 * priority should be added to the existing priority, ignoring the
1007 * priority in value.
1009 * Note that it is possible for multiple values to match this put.
1010 * In that case, all of the respective values are updated.
1012 * @param cls our "struct Plugin*"
1013 * @param uid unique identifier of the datum
1014 * @param delta by how much should the priority
1015 * change? If priority + delta < 0 the
1016 * priority should be set to 0 (never go
1018 * @param expire new expiration time should be the
1019 * MAX of any existing expiration time and
1021 * @param msg set to error message
1022 * @return GNUNET_OK on success
1025 postgres_plugin_update (void *cls,
1027 int delta, struct GNUNET_TIME_Absolute expire,
1030 struct Plugin *plugin = cls;
1032 int32_t bdelta = (int32_t) htonl ((uint32_t) delta);
1033 uint32_t boid = htonl ( (uint32_t) uid);
1034 uint64_t bexpire = GNUNET_TIME_absolute_hton (expire).abs_value__;
1035 const char *paramValues[] = {
1036 (const char *) &bdelta,
1037 (const char *) &bexpire,
1038 (const char *) &boid,
1040 int paramLengths[] = {
1045 const int paramFormats[] = { 1, 1, 1 };
1047 ret = PQexecPrepared (plugin->dbh,
1049 3, paramValues, paramLengths, paramFormats, 1);
1050 if (GNUNET_OK != check_result (plugin,
1053 "PQexecPrepared", "update", __LINE__))
1054 return GNUNET_SYSERR;
1064 postgres_plugin_drop (void *cls)
1066 struct Plugin *plugin = cls;
1068 pq_exec (plugin, "DROP TABLE gn090", __LINE__);
1073 * Entry point for the plugin.
1075 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
1076 * @return our "struct Plugin*"
1079 libgnunet_plugin_datastore_postgres_init (void *cls)
1081 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
1082 struct GNUNET_DATASTORE_PluginFunctions *api;
1083 struct Plugin *plugin;
1085 plugin = GNUNET_malloc (sizeof (struct Plugin));
1087 if (GNUNET_OK != init_connection (plugin))
1089 GNUNET_free (plugin);
1092 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1094 api->estimate_size = &postgres_plugin_estimate_size;
1095 api->put = &postgres_plugin_put;
1096 api->update = &postgres_plugin_update;
1097 api->get_key = &postgres_plugin_get_key;
1098 api->get_replication = &postgres_plugin_get_replication;
1099 api->get_expiration = &postgres_plugin_get_expiration;
1100 api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity;
1101 api->drop = &postgres_plugin_drop;
1102 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1103 "datastore-postgres",
1104 _("Postgres database running\n"));
1110 * Exit point from the plugin.
1111 * @param cls our "struct Plugin*"
1112 * @return always NULL
1115 libgnunet_plugin_datastore_postgres_done (void *cls)
1117 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1118 struct Plugin *plugin = api->cls;
1120 PQfinish (plugin->dbh);
1121 GNUNET_free (plugin);
1126 /* end of plugin_datastore_postgres.c */