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, PGresult * ret, int expected_status,
79 const char *command, const char *args, int line)
83 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
85 "Postgres failed to allocate result for `%s:%s' at %d\n",
89 if (PQresultStatus (ret) != expected_status)
91 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
93 _("`%s:%s' failed at %s:%d with error: %s"), command, args,
94 __FILE__, line, PQerrorMessage (plugin->dbh));
102 * Run simple SQL statement (without results).
104 * @param plugin global context
105 * @param sql statement to run
106 * @param line code line for error reporting
109 pq_exec (struct Plugin *plugin, const char *sql, int line)
113 ret = PQexec (plugin->dbh, sql);
115 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexec", sql, line))
116 return GNUNET_SYSERR;
122 * Prepare SQL statement.
124 * @param plugin global context
125 * @param name name for the prepared SQL statement
126 * @param sql SQL code to prepare
127 * @param nparams number of parameters in sql
128 * @param line code line for error reporting
129 * @return GNUNET_OK on success
132 pq_prepare (struct Plugin *plugin, const char *name, const char *sql,
133 int nparams, int line)
137 ret = PQprepare (plugin->dbh, name, sql, nparams, NULL);
139 check_result (plugin, ret, PGRES_COMMAND_OK, "PQprepare", sql, line))
140 return GNUNET_SYSERR;
146 * @brief Get a database handle
148 * @param plugin global context
149 * @return GNUNET_OK on success, GNUNET_SYSERR on error
152 init_connection (struct Plugin *plugin)
157 /* Open database and precompile statements */
159 (void) GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
160 "datastore-postgres", "CONFIG",
162 plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
163 if (NULL == plugin->dbh)
165 /* FIXME: warn about out-of-memory? */
166 GNUNET_free_non_null (conninfo);
167 return GNUNET_SYSERR;
169 if (PQstatus (plugin->dbh) != CONNECTION_OK)
171 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "datastore-postgres",
173 ("Unable to initialize Postgres with configuration `%s': %s"),
174 conninfo, PQerrorMessage (plugin->dbh));
175 PQfinish (plugin->dbh);
177 GNUNET_free_non_null (conninfo);
178 return GNUNET_SYSERR;
180 GNUNET_free_non_null (conninfo);
183 "CREATE TABLE gn090 (" " repl INTEGER NOT NULL DEFAULT 0,"
184 " type INTEGER NOT NULL DEFAULT 0,"
185 " prio INTEGER NOT NULL DEFAULT 0,"
186 " anonLevel INTEGER NOT NULL DEFAULT 0,"
187 " expire BIGINT NOT NULL DEFAULT 0,"
188 " rvalue BIGINT NOT NULL DEFAULT 0,"
189 " hash BYTEA NOT NULL DEFAULT '',"
190 " vhash BYTEA NOT NULL DEFAULT '',"
191 " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS");
192 if ((ret == NULL) || ((PQresultStatus (ret) != PGRES_COMMAND_OK) && (0 != strcmp ("42P07", /* duplicate table */
195 PG_DIAG_SQLSTATE)))))
197 (void) check_result (plugin, ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090",
199 PQfinish (plugin->dbh);
201 return GNUNET_SYSERR;
203 if (PQresultStatus (ret) == PGRES_COMMAND_OK)
206 pq_exec (plugin, "CREATE INDEX idx_hash ON gn090 (hash)", __LINE__)) ||
208 pq_exec (plugin, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)",
211 pq_exec (plugin, "CREATE INDEX idx_prio ON gn090 (prio)", __LINE__)) ||
213 pq_exec (plugin, "CREATE INDEX idx_expire ON gn090 (expire)",
217 "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)",
221 "CREATE INDEX idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)",
224 pq_exec (plugin, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)",
227 pq_exec (plugin, "CREATE INDEX idx_expire_hash ON gn090 (expire,hash)",
231 PQfinish (plugin->dbh);
233 return GNUNET_SYSERR;
239 "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL");
241 check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090",
244 PQfinish (plugin->dbh);
246 return GNUNET_SYSERR;
249 ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN");
251 check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090",
254 PQfinish (plugin->dbh);
256 return GNUNET_SYSERR;
259 ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN");
261 check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090",
264 PQfinish (plugin->dbh);
266 return GNUNET_SYSERR;
270 pq_prepare (plugin, "getvt",
271 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
272 "WHERE hash=$1 AND vhash=$2 AND type=$3 "
273 "ORDER BY oid ASC LIMIT 1 OFFSET $4", 4, __LINE__)) ||
275 pq_prepare (plugin, "gett",
276 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
277 "WHERE hash=$1 AND type=$2 "
278 "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3, __LINE__)) ||
280 pq_prepare (plugin, "getv",
281 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
282 "WHERE hash=$1 AND vhash=$2 "
283 "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3, __LINE__)) ||
285 pq_prepare (plugin, "get",
286 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
287 "WHERE hash=$1 " "ORDER BY oid ASC LIMIT 1 OFFSET $2", 2,
290 pq_prepare (plugin, "put",
291 "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
292 "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", 9,
295 pq_prepare (plugin, "update",
296 "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END "
297 "WHERE oid = $3", 3, __LINE__)) ||
299 pq_prepare (plugin, "decrepl",
300 "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) "
301 "WHERE oid = $1", 1, __LINE__)) ||
303 pq_prepare (plugin, "select_non_anonymous",
304 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
305 "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2",
308 pq_prepare (plugin, "select_expiration_order",
309 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
310 "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " "UNION "
311 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
312 "ORDER BY prio ASC LIMIT 1) " "ORDER BY expire ASC LIMIT 1",
315 pq_prepare (plugin, "select_replication_order",
316 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
317 "ORDER BY repl DESC,RANDOM() LIMIT 1", 0, __LINE__)) ||
319 pq_prepare (plugin, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1,
322 PQfinish (plugin->dbh);
324 return GNUNET_SYSERR;
331 * Delete the row identified by the given rowid (qid
334 * @param plugin global context
335 * @param rowid which row to delete
336 * @return GNUNET_OK on success
339 delete_by_rowid (struct Plugin *plugin, unsigned int rowid)
342 const char *paramValues[] = { (const char *) &browid };
343 int paramLengths[] = { sizeof (browid) };
344 const int paramFormats[] = { 1 };
347 browid = htonl (rowid);
349 PQexecPrepared (plugin->dbh, "delrow", 1, paramValues, paramLengths,
352 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow",
355 return GNUNET_SYSERR;
363 * Get an estimate of how much space the database is
366 * @param cls our "struct Plugin*"
367 * @return number of bytes used on disk
369 static unsigned long long
370 postgres_plugin_estimate_size (void *cls)
372 struct Plugin *plugin = cls;
373 unsigned long long total;
377 PQexecParams (plugin->dbh,
378 "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090", 0,
379 NULL, NULL, NULL, NULL, 1);
381 check_result (plugin, ret, PGRES_TUPLES_OK, "PQexecParams", "get_size",
386 if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) ||
387 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
393 total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
400 * Store an item in the datastore.
403 * @param key key for the item
404 * @param size number of bytes in data
405 * @param data content stored
406 * @param type type of the content
407 * @param priority priority of the content
408 * @param anonymity anonymity-level for the content
409 * @param replication replication-level for the content
410 * @param expiration expiration time for the content
411 * @param msg set to error message
412 * @return GNUNET_OK on success
415 postgres_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size,
416 const void *data, enum GNUNET_BLOCK_Type type,
417 uint32_t priority, uint32_t anonymity,
418 uint32_t replication,
419 struct GNUNET_TIME_Absolute expiration, char **msg)
421 struct Plugin *plugin = cls;
422 GNUNET_HashCode vhash;
424 uint32_t btype = htonl (type);
425 uint32_t bprio = htonl (priority);
426 uint32_t banon = htonl (anonymity);
427 uint32_t brepl = htonl (replication);
428 uint64_t bexpi = GNUNET_TIME_absolute_hton (expiration).abs_value__;
430 const char *paramValues[] = {
431 (const char *) &brepl,
432 (const char *) &btype,
433 (const char *) &bprio,
434 (const char *) &banon,
435 (const char *) &bexpi,
437 (const char *) &vhash,
440 int paramLengths[] = {
446 sizeof (GNUNET_HashCode),
447 sizeof (GNUNET_HashCode),
450 const int paramFormats[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
452 GNUNET_CRYPTO_hash (data, size, &vhash);
454 PQexecPrepared (plugin->dbh, "put", 8, paramValues, paramLengths,
457 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put",
459 return GNUNET_SYSERR;
461 plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
463 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
464 "Stored %u bytes in database\n", (unsigned int) size);
471 * Function invoked to process the result and call
474 * @param plugin global plugin data
475 * @param proc function to call the value (once only).
476 * @param proc_cls closure for proc
477 * @param res result from exec
478 * @param line line number for error messages
481 process_result (struct Plugin *plugin, PluginDatumProcessor proc,
482 void *proc_cls, PGresult * res, int line)
485 enum GNUNET_BLOCK_Type type;
490 struct GNUNET_TIME_Absolute expiration_time;
494 check_result (plugin, res, PGRES_TUPLES_OK, "PQexecPrepared", "select",
498 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
499 "Ending iteration (postgres error)\n");
501 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
505 if (0 == PQntuples (res))
509 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
510 "Ending iteration (no more results)\n");
512 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
516 if ((1 != PQntuples (res)) || (7 != PQnfields (res)) ||
517 (sizeof (uint32_t) != PQfsize (res, 0)) ||
518 (sizeof (uint32_t) != PQfsize (res, 6)))
521 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
525 rowid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 6));
526 if ((sizeof (uint32_t) != PQfsize (res, 0)) ||
527 (sizeof (uint32_t) != PQfsize (res, 1)) ||
528 (sizeof (uint32_t) != PQfsize (res, 2)) ||
529 (sizeof (uint64_t) != PQfsize (res, 3)) ||
530 (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 4)))
534 delete_by_rowid (plugin, rowid);
535 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
539 type = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0));
540 priority = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
541 anonymity = ntohl (*(uint32_t *) PQgetvalue (res, 0, 2));
542 expiration_time.abs_value =
543 GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 3));
544 memcpy (&key, PQgetvalue (res, 0, 4), sizeof (GNUNET_HashCode));
545 size = PQgetlength (res, 0, 5);
547 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
548 "Found result of size %u bytes and type %u in database\n",
549 (unsigned int) size, (unsigned int) type);
552 proc (proc_cls, &key, size, PQgetvalue (res, 0, 5),
553 (enum GNUNET_BLOCK_Type) type, priority, anonymity, expiration_time,
556 if (iret == GNUNET_NO)
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "Processor asked for item %u to be removed.\n", rowid);
562 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
565 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
566 "Deleting %u bytes from database\n",
567 (unsigned int) size);
569 plugin->env->duc (plugin->env->cls,
570 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
572 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
573 "Deleted %u bytes from database\n", (unsigned int) size);
581 * Iterate over the results for a particular key
585 * @param offset offset of the result (modulo num-results);
586 * specific ordering does not matter for the offset
587 * @param key maybe NULL (to match all entries)
588 * @param vhash hash of the value, maybe NULL (to
589 * match all values that have the right key).
590 * Note that for DBlocks there is no difference
591 * betwen key and vhash, but for other blocks
593 * @param type entries of which type are relevant?
594 * Use 0 for any type.
595 * @param proc function to call on the matching value;
596 * will be called once with a NULL if no value matches
597 * @param proc_cls closure for iter
600 postgres_plugin_get_key (void *cls, uint64_t offset,
601 const GNUNET_HashCode * key,
602 const GNUNET_HashCode * vhash,
603 enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc,
606 struct Plugin *plugin = cls;
607 const int paramFormats[] = { 1, 1, 1, 1, 1 };
609 const char *paramValues[4];
617 GNUNET_assert (key != NULL);
618 paramValues[0] = (const char *) key;
619 paramLengths[0] = sizeof (GNUNET_HashCode);
620 btype = htonl (type);
625 paramValues[1] = (const char *) vhash;
626 paramLengths[1] = sizeof (GNUNET_HashCode);
627 paramValues[2] = (const char *) &btype;
628 paramLengths[2] = sizeof (btype);
629 paramValues[3] = (const char *) &blimit_off;
630 paramLengths[3] = sizeof (blimit_off);
634 PQexecParams (plugin->dbh,
635 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2 AND type=$3",
636 3, NULL, paramValues, paramLengths, paramFormats, 1);
640 paramValues[1] = (const char *) &btype;
641 paramLengths[1] = sizeof (btype);
642 paramValues[2] = (const char *) &blimit_off;
643 paramLengths[2] = sizeof (blimit_off);
647 PQexecParams (plugin->dbh,
648 "SELECT count(*) FROM gn090 WHERE hash=$1 AND type=$2",
649 2, NULL, paramValues, paramLengths, paramFormats, 1);
656 paramValues[1] = (const char *) vhash;
657 paramLengths[1] = sizeof (GNUNET_HashCode);
658 paramValues[2] = (const char *) &blimit_off;
659 paramLengths[2] = sizeof (blimit_off);
663 PQexecParams (plugin->dbh,
664 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2",
665 2, NULL, paramValues, paramLengths, paramFormats, 1);
669 paramValues[1] = (const char *) &blimit_off;
670 paramLengths[1] = sizeof (blimit_off);
674 PQexecParams (plugin->dbh, "SELECT count(*) FROM gn090 WHERE hash=$1",
675 1, NULL, paramValues, paramLengths, paramFormats, 1);
679 check_result (plugin, ret, PGRES_TUPLES_OK, "PQexecParams", pname,
682 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
685 if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) ||
686 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
690 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
693 total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
697 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
700 blimit_off = GNUNET_htonll (offset % total);
702 PQexecPrepared (plugin->dbh, pname, nparams, paramValues, paramLengths,
704 process_result (plugin, proc, proc_cls, ret, __LINE__);
709 * Select a subset of the items in the datastore and call
710 * the given iterator for each of them.
712 * @param cls our "struct Plugin*"
713 * @param offset offset of the result (modulo num-results);
714 * specific ordering does not matter for the offset
715 * @param type entries of which type should be considered?
716 * Use 0 for any type.
717 * @param proc function to call on the matching value;
718 * will be called with a NULL if no value matches
719 * @param proc_cls closure for proc
722 postgres_plugin_get_zero_anonymity (void *cls, uint64_t offset,
723 enum GNUNET_BLOCK_Type type,
724 PluginDatumProcessor proc, void *proc_cls)
726 struct Plugin *plugin = cls;
729 const int paramFormats[] = { 1, 1 };
730 int paramLengths[] = { sizeof (btype), sizeof (boff) };
731 const char *paramValues[] = { (const char *) &btype, (const char *) &boff };
734 btype = htonl ((uint32_t) type);
735 boff = GNUNET_htonll (offset);
737 PQexecPrepared (plugin->dbh, "select_non_anonymous", 2, paramValues,
738 paramLengths, paramFormats, 1);
739 process_result (plugin, proc, proc_cls, ret, __LINE__);
744 * Context for 'repl_iter' function.
752 struct Plugin *plugin;
755 * Function to call for the result (or the NULL).
757 PluginDatumProcessor proc;
767 * Wrapper for the iterator for 'sqlite_plugin_replication_get'.
768 * Decrements the replication counter and calls the original
772 * @param key key for the content
773 * @param size number of bytes in data
774 * @param data content stored
775 * @param type type of the content
776 * @param priority priority of the content
777 * @param anonymity anonymity-level for the content
778 * @param expiration expiration time for the content
779 * @param uid unique identifier for the datum;
780 * maybe 0 if no unique identifier is available
782 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
783 * (continue on call to "next", of course),
784 * GNUNET_NO to delete the item and continue (if supported)
787 repl_proc (void *cls, const GNUNET_HashCode * key, uint32_t size,
788 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
789 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
792 struct ReplCtx *rc = cls;
793 struct Plugin *plugin = rc->plugin;
799 rc->proc (rc->proc_cls, key, size, data, type, priority, anonymity,
803 boid = htonl ((uint32_t) uid);
804 const char *paramValues[] = {
805 (const char *) &boid,
807 int paramLengths[] = {
810 const int paramFormats[] = { 1 };
812 PQexecPrepared (plugin->dbh, "decrepl", 1, paramValues, paramLengths,
815 check_result (plugin, qret, PGRES_COMMAND_OK, "PQexecPrepared",
816 "decrepl", __LINE__))
817 return GNUNET_SYSERR;
825 * Get a random item for replication. Returns a single, not expired, random item
826 * from those with the highest replication counters. The item's
827 * replication counter is decremented by one IF it was positive before.
828 * Call 'proc' with all values ZERO or NULL if the datastore is empty.
831 * @param proc function to call the value (once only).
832 * @param proc_cls closure for proc
835 postgres_plugin_get_replication (void *cls, PluginDatumProcessor proc,
838 struct Plugin *plugin = cls;
844 rc.proc_cls = proc_cls;
846 PQexecPrepared (plugin->dbh, "select_replication_order", 0, NULL, NULL,
848 process_result (plugin, &repl_proc, &rc, ret, __LINE__);
853 * Get a random item for expiration.
854 * Call 'proc' with all values ZERO or NULL if the datastore is empty.
857 * @param proc function to call the value (once only).
858 * @param proc_cls closure for proc
861 postgres_plugin_get_expiration (void *cls, PluginDatumProcessor proc,
864 struct Plugin *plugin = cls;
866 const int paramFormats[] = { 1 };
867 int paramLengths[] = { sizeof (btime) };
868 const char *paramValues[] = { (const char *) &btime };
871 btime = GNUNET_htonll (GNUNET_TIME_absolute_get ().abs_value);
873 PQexecPrepared (plugin->dbh, "select_expiration_order", 1, paramValues,
874 paramLengths, paramFormats, 1);
875 process_result (plugin, proc, proc_cls, ret, __LINE__);
880 * Update the priority for a particular key in the datastore. If
881 * the expiration time in value is different than the time found in
882 * the datastore, the higher value should be kept. For the
883 * anonymity level, the lower value is to be used. The specified
884 * priority should be added to the existing priority, ignoring the
887 * Note that it is possible for multiple values to match this put.
888 * In that case, all of the respective values are updated.
890 * @param cls our "struct Plugin*"
891 * @param uid unique identifier of the datum
892 * @param delta by how much should the priority
893 * change? If priority + delta < 0 the
894 * priority should be set to 0 (never go
896 * @param expire new expiration time should be the
897 * MAX of any existing expiration time and
899 * @param msg set to error message
900 * @return GNUNET_OK on success
903 postgres_plugin_update (void *cls, uint64_t uid, int delta,
904 struct GNUNET_TIME_Absolute expire, char **msg)
906 struct Plugin *plugin = cls;
908 int32_t bdelta = (int32_t) htonl ((uint32_t) delta);
909 uint32_t boid = htonl ((uint32_t) uid);
910 uint64_t bexpire = GNUNET_TIME_absolute_hton (expire).abs_value__;
912 const char *paramValues[] = {
913 (const char *) &bdelta,
914 (const char *) &bexpire,
915 (const char *) &boid,
917 int paramLengths[] = {
922 const int paramFormats[] = { 1, 1, 1 };
925 PQexecPrepared (plugin->dbh, "update", 3, paramValues, paramLengths,
928 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "update",
930 return GNUNET_SYSERR;
940 postgres_plugin_drop (void *cls)
942 struct Plugin *plugin = cls;
944 pq_exec (plugin, "DROP TABLE gn090", __LINE__);
949 * Entry point for the plugin.
951 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
952 * @return our "struct Plugin*"
955 libgnunet_plugin_datastore_postgres_init (void *cls)
957 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
958 struct GNUNET_DATASTORE_PluginFunctions *api;
959 struct Plugin *plugin;
961 plugin = GNUNET_malloc (sizeof (struct Plugin));
963 if (GNUNET_OK != init_connection (plugin))
965 GNUNET_free (plugin);
968 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
970 api->estimate_size = &postgres_plugin_estimate_size;
971 api->put = &postgres_plugin_put;
972 api->update = &postgres_plugin_update;
973 api->get_key = &postgres_plugin_get_key;
974 api->get_replication = &postgres_plugin_get_replication;
975 api->get_expiration = &postgres_plugin_get_expiration;
976 api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity;
977 api->drop = &postgres_plugin_drop;
978 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "datastore-postgres",
979 _("Postgres database running\n"));
985 * Exit point from the plugin.
986 * @param cls our "struct Plugin*"
987 * @return always NULL
990 libgnunet_plugin_datastore_postgres_done (void *cls)
992 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
993 struct Plugin *plugin = api->cls;
995 PQfinish (plugin->dbh);
996 GNUNET_free (plugin);
1001 /* end of plugin_datastore_postgres.c */