2 This file is part of GNUnet
3 (C) 2009-2013 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 "gnunet_postgres_lib.h"
33 * After how many ms "busy" should a DB operation fail for good?
34 * A low value makes sure that we are more responsive to requests
35 * (especially PUTs). A high value guarantees a higher success
36 * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
38 * The default value of 1s should ensure that users do not experience
39 * huge latencies while at the same time allowing operations to succeed
40 * with reasonable probability.
42 #define BUSY_TIMEOUT GNUNET_TIME_UNIT_SECONDS
46 * Context for all functions in this plugin.
51 * Our execution environment.
53 struct GNUNET_DATASTORE_PluginEnvironment *env;
56 * Native Postgres database handle.
64 * @brief Get a database handle
66 * @param plugin global context
67 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
70 init_connection (struct Plugin *plugin)
74 plugin->dbh = GNUNET_POSTGRES_connect (plugin->env->cfg, "datastore-postgres");
75 if (NULL == plugin->dbh)
80 "CREATE TABLE gn090 (" " repl INTEGER NOT NULL DEFAULT 0,"
81 " type INTEGER NOT NULL DEFAULT 0,"
82 " prio INTEGER NOT NULL DEFAULT 0,"
83 " anonLevel INTEGER NOT NULL DEFAULT 0,"
84 " expire BIGINT NOT NULL DEFAULT 0,"
85 " rvalue BIGINT NOT NULL DEFAULT 0,"
86 " hash BYTEA NOT NULL DEFAULT '',"
87 " vhash BYTEA NOT NULL DEFAULT '',"
88 " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS");
90 ((PQresultStatus (ret) != PGRES_COMMAND_OK) &&
91 (0 != strcmp ("42P07", /* duplicate table */
96 (void) GNUNET_POSTGRES_check_result (plugin->dbh,
101 PQfinish (plugin->dbh);
103 return GNUNET_SYSERR;
106 if (PQresultStatus (ret) == PGRES_COMMAND_OK)
109 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_hash ON gn090 (hash)")) ||
111 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)")) ||
113 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_prio ON gn090 (prio)")) ||
115 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_expire ON gn090 (expire)")) ||
117 GNUNET_POSTGRES_exec (plugin->dbh,
118 "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)")) ||
120 GNUNET_POSTGRES_exec (plugin->dbh,
121 "CREATE INDEX idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)")) ||
123 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)")) ||
125 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_expire_hash ON gn090 (expire,hash)")))
128 PQfinish (plugin->dbh);
130 return GNUNET_SYSERR;
137 "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL");
139 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
141 PQfinish (plugin->dbh);
143 return GNUNET_SYSERR;
146 ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN");
148 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
150 PQfinish (plugin->dbh);
152 return GNUNET_SYSERR;
155 ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN");
157 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
159 PQfinish (plugin->dbh);
161 return GNUNET_SYSERR;
165 GNUNET_POSTGRES_prepare (plugin->dbh, "getvt",
166 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
167 "WHERE hash=$1 AND vhash=$2 AND type=$3 "
168 "ORDER BY oid ASC LIMIT 1 OFFSET $4", 4)) ||
170 GNUNET_POSTGRES_prepare (plugin->dbh, "gett",
171 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
172 "WHERE hash=$1 AND type=$2 "
173 "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3)) ||
175 GNUNET_POSTGRES_prepare (plugin->dbh, "getv",
176 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
177 "WHERE hash=$1 AND vhash=$2 "
178 "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3)) ||
180 GNUNET_POSTGRES_prepare (plugin->dbh, "get",
181 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
182 "WHERE hash=$1 " "ORDER BY oid ASC LIMIT 1 OFFSET $2", 2)) ||
184 GNUNET_POSTGRES_prepare (plugin->dbh, "put",
185 "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
186 "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", 9)) ||
188 GNUNET_POSTGRES_prepare (plugin->dbh, "update",
189 "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END "
190 "WHERE oid = $3", 3)) ||
192 GNUNET_POSTGRES_prepare (plugin->dbh, "decrepl",
193 "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) "
194 "WHERE oid = $1", 1)) ||
196 GNUNET_POSTGRES_prepare (plugin->dbh, "select_non_anonymous",
197 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
198 "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2",
201 GNUNET_POSTGRES_prepare (plugin->dbh, "select_expiration_order",
202 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
203 "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " "UNION "
204 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
205 "ORDER BY prio ASC LIMIT 1) " "ORDER BY expire ASC LIMIT 1",
208 GNUNET_POSTGRES_prepare (plugin->dbh, "select_replication_order",
209 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
210 "ORDER BY repl DESC,RANDOM() LIMIT 1", 0)) ||
212 GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1)) ||
214 GNUNET_POSTGRES_prepare (plugin->dbh, "get_keys", "SELECT hash FROM gn090", 0)))
216 PQfinish (plugin->dbh);
218 return GNUNET_SYSERR;
225 * Get an estimate of how much space the database is
228 * @param cls our `struct Plugin *`
229 * @return number of bytes used on disk
232 postgres_plugin_estimate_size (void *cls, unsigned long long *estimate)
234 struct Plugin *plugin = cls;
235 unsigned long long total;
238 if (NULL == estimate)
241 PQexecParams (plugin->dbh,
242 "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090", 0,
243 NULL, NULL, NULL, NULL, 1);
245 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_TUPLES_OK, "PQexecParams", "get_size"))
250 if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) )
257 if (PQgetlength (ret, 0, 0) != sizeof (unsigned long long))
259 GNUNET_break (0 == PQgetlength (ret, 0, 0));
264 total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
271 * Store an item in the datastore.
273 * @param cls closure with the `struct Plugin`
274 * @param key key for the item
275 * @param size number of bytes in data
276 * @param data content stored
277 * @param type type of the content
278 * @param priority priority of the content
279 * @param anonymity anonymity-level for the content
280 * @param replication replication-level for the content
281 * @param expiration expiration time for the content
282 * @param msg set to error message
283 * @return #GNUNET_OK on success
286 postgres_plugin_put (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
287 const void *data, enum GNUNET_BLOCK_Type type,
288 uint32_t priority, uint32_t anonymity,
289 uint32_t replication,
290 struct GNUNET_TIME_Absolute expiration, char **msg)
292 struct Plugin *plugin = cls;
293 struct GNUNET_HashCode vhash;
295 uint32_t btype = htonl (type);
296 uint32_t bprio = htonl (priority);
297 uint32_t banon = htonl (anonymity);
298 uint32_t brepl = htonl (replication);
299 uint64_t bexpi = GNUNET_TIME_absolute_hton (expiration).abs_value_us__;
301 const char *paramValues[] = {
302 (const char *) &brepl,
303 (const char *) &btype,
304 (const char *) &bprio,
305 (const char *) &banon,
306 (const char *) &bexpi,
308 (const char *) &vhash,
311 int paramLengths[] = {
317 sizeof (struct GNUNET_HashCode),
318 sizeof (struct GNUNET_HashCode),
321 const int paramFormats[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
323 GNUNET_CRYPTO_hash (data, size, &vhash);
325 PQexecPrepared (plugin->dbh, "put", 8, paramValues, paramLengths,
328 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put"))
329 return GNUNET_SYSERR;
331 plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
332 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
333 "Stored %u bytes in database\n", (unsigned int) size);
339 * Function invoked to process the result and call
342 * @param plugin global plugin data
343 * @param proc function to call the value (once only).
344 * @param proc_cls closure for proc
345 * @param res result from exec
346 * @param filename filename for error messages
347 * @param line line number for error messages
350 process_result (struct Plugin *plugin, PluginDatumProcessor proc,
351 void *proc_cls, PGresult * res,
352 const char *filename, int line)
355 enum GNUNET_BLOCK_Type type;
360 struct GNUNET_TIME_Absolute expiration_time;
361 struct GNUNET_HashCode key;
364 GNUNET_POSTGRES_check_result_ (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared", "select",
367 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
368 "Ending iteration (postgres error)\n");
369 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
373 if (0 == PQntuples (res))
376 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
377 "Ending iteration (no more results)\n");
378 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
382 if ((1 != PQntuples (res)) || (7 != PQnfields (res)) ||
383 (sizeof (uint32_t) != PQfsize (res, 0)) ||
384 (sizeof (uint32_t) != PQfsize (res, 6)))
387 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
391 rowid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 6));
392 if ((sizeof (uint32_t) != PQfsize (res, 0)) ||
393 (sizeof (uint32_t) != PQfsize (res, 1)) ||
394 (sizeof (uint32_t) != PQfsize (res, 2)) ||
395 (sizeof (uint64_t) != PQfsize (res, 3)) ||
396 (sizeof (struct GNUNET_HashCode) != PQgetlength (res, 0, 4)))
400 GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", rowid);
401 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
405 type = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0));
406 priority = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
407 anonymity = ntohl (*(uint32_t *) PQgetvalue (res, 0, 2));
408 expiration_time.abs_value_us =
409 GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 3));
410 memcpy (&key, PQgetvalue (res, 0, 4), sizeof (struct GNUNET_HashCode));
411 size = PQgetlength (res, 0, 5);
412 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
413 "Found result of size %u bytes and type %u in database\n",
414 (unsigned int) size, (unsigned int) type);
416 proc (proc_cls, &key, size, PQgetvalue (res, 0, 5),
417 (enum GNUNET_BLOCK_Type) type, priority, anonymity, expiration_time,
420 if (iret == GNUNET_NO)
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Processor asked for item %u to be removed.\n", rowid);
424 if (GNUNET_OK == GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", rowid))
426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
427 "Deleting %u bytes from database\n",
428 (unsigned int) size);
429 plugin->env->duc (plugin->env->cls,
430 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
431 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
432 "Deleted %u bytes from database\n", (unsigned int) size);
439 * Iterate over the results for a particular key
442 * @param cls closure with the 'struct Plugin'
443 * @param offset offset of the result (modulo num-results);
444 * specific ordering does not matter for the offset
445 * @param key maybe NULL (to match all entries)
446 * @param vhash hash of the value, maybe NULL (to
447 * match all values that have the right key).
448 * Note that for DBlocks there is no difference
449 * betwen key and vhash, but for other blocks
451 * @param type entries of which type are relevant?
452 * Use 0 for any type.
453 * @param proc function to call on the matching value;
454 * will be called once with a NULL if no value matches
455 * @param proc_cls closure for iter
458 postgres_plugin_get_key (void *cls, uint64_t offset,
459 const struct GNUNET_HashCode * key,
460 const struct GNUNET_HashCode * vhash,
461 enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc,
464 struct Plugin *plugin = cls;
465 const int paramFormats[] = { 1, 1, 1, 1, 1 };
467 const char *paramValues[4];
475 GNUNET_assert (key != NULL);
476 paramValues[0] = (const char *) key;
477 paramLengths[0] = sizeof (struct GNUNET_HashCode);
478 btype = htonl (type);
483 paramValues[1] = (const char *) vhash;
484 paramLengths[1] = sizeof (struct GNUNET_HashCode);
485 paramValues[2] = (const char *) &btype;
486 paramLengths[2] = sizeof (btype);
487 paramValues[3] = (const char *) &blimit_off;
488 paramLengths[3] = sizeof (blimit_off);
492 PQexecParams (plugin->dbh,
493 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2 AND type=$3",
494 3, NULL, paramValues, paramLengths, paramFormats, 1);
498 paramValues[1] = (const char *) &btype;
499 paramLengths[1] = sizeof (btype);
500 paramValues[2] = (const char *) &blimit_off;
501 paramLengths[2] = sizeof (blimit_off);
505 PQexecParams (plugin->dbh,
506 "SELECT count(*) FROM gn090 WHERE hash=$1 AND type=$2",
507 2, NULL, paramValues, paramLengths, paramFormats, 1);
514 paramValues[1] = (const char *) vhash;
515 paramLengths[1] = sizeof (struct GNUNET_HashCode);
516 paramValues[2] = (const char *) &blimit_off;
517 paramLengths[2] = sizeof (blimit_off);
521 PQexecParams (plugin->dbh,
522 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2",
523 2, NULL, paramValues, paramLengths, paramFormats, 1);
527 paramValues[1] = (const char *) &blimit_off;
528 paramLengths[1] = sizeof (blimit_off);
532 PQexecParams (plugin->dbh, "SELECT count(*) FROM gn090 WHERE hash=$1",
533 1, NULL, paramValues, paramLengths, paramFormats, 1);
537 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_TUPLES_OK, "PQexecParams", pname))
539 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
542 if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) ||
543 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
547 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
550 total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
554 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
557 blimit_off = GNUNET_htonll (offset % total);
559 PQexecPrepared (plugin->dbh, pname, nparams, paramValues, paramLengths,
561 process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__);
566 * Select a subset of the items in the datastore and call
567 * the given iterator for each of them.
569 * @param cls our "struct Plugin*"
570 * @param offset offset of the result (modulo num-results);
571 * specific ordering does not matter for the offset
572 * @param type entries of which type should be considered?
573 * Use 0 for any type.
574 * @param proc function to call on the matching value;
575 * will be called with a NULL if no value matches
576 * @param proc_cls closure for proc
579 postgres_plugin_get_zero_anonymity (void *cls, uint64_t offset,
580 enum GNUNET_BLOCK_Type type,
581 PluginDatumProcessor proc, void *proc_cls)
583 struct Plugin *plugin = cls;
586 const int paramFormats[] = { 1, 1 };
587 int paramLengths[] = { sizeof (btype), sizeof (boff) };
588 const char *paramValues[] = { (const char *) &btype, (const char *) &boff };
591 btype = htonl ((uint32_t) type);
592 boff = GNUNET_htonll (offset);
594 PQexecPrepared (plugin->dbh, "select_non_anonymous", 2, paramValues,
595 paramLengths, paramFormats, 1);
596 process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__);
601 * Context for 'repl_iter' function.
609 struct Plugin *plugin;
612 * Function to call for the result (or the NULL).
614 PluginDatumProcessor proc;
624 * Wrapper for the iterator for 'sqlite_plugin_replication_get'.
625 * Decrements the replication counter and calls the original
628 * @param cls closure with the 'struct ReplCtx*'
629 * @param key key for the content
630 * @param size number of bytes in data
631 * @param data content stored
632 * @param type type of the content
633 * @param priority priority of the content
634 * @param anonymity anonymity-level for the content
635 * @param expiration expiration time for the content
636 * @param uid unique identifier for the datum;
637 * maybe 0 if no unique identifier is available
639 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
640 * (continue on call to "next", of course),
641 * GNUNET_NO to delete the item and continue (if supported)
644 repl_proc (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
645 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
646 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
649 struct ReplCtx *rc = cls;
650 struct Plugin *plugin = rc->plugin;
656 rc->proc (rc->proc_cls, key, size, data, type, priority, anonymity,
660 boid = htonl ((uint32_t) uid);
661 const char *paramValues[] = {
662 (const char *) &boid,
664 int paramLengths[] = {
667 const int paramFormats[] = { 1 };
669 PQexecPrepared (plugin->dbh, "decrepl", 1, paramValues, paramLengths,
672 GNUNET_POSTGRES_check_result (plugin->dbh, qret, PGRES_COMMAND_OK, "PQexecPrepared",
674 return GNUNET_SYSERR;
682 * Get a random item for replication. Returns a single, not expired, random item
683 * from those with the highest replication counters. The item's
684 * replication counter is decremented by one IF it was positive before.
685 * Call 'proc' with all values ZERO or NULL if the datastore is empty.
687 * @param cls closure with the 'struct Plugin'
688 * @param proc function to call the value (once only).
689 * @param proc_cls closure for proc
692 postgres_plugin_get_replication (void *cls, PluginDatumProcessor proc,
695 struct Plugin *plugin = cls;
701 rc.proc_cls = proc_cls;
703 PQexecPrepared (plugin->dbh, "select_replication_order", 0, NULL, NULL,
705 process_result (plugin, &repl_proc, &rc, ret, __FILE__, __LINE__);
710 * Get a random item for expiration.
711 * Call 'proc' with all values ZERO or NULL if the datastore is empty.
713 * @param cls closure with the 'struct Plugin'
714 * @param proc function to call the value (once only).
715 * @param proc_cls closure for proc
718 postgres_plugin_get_expiration (void *cls, PluginDatumProcessor proc,
721 struct Plugin *plugin = cls;
723 const int paramFormats[] = { 1 };
724 int paramLengths[] = { sizeof (btime) };
725 const char *paramValues[] = { (const char *) &btime };
728 btime = GNUNET_htonll (GNUNET_TIME_absolute_get ().abs_value_us);
730 PQexecPrepared (plugin->dbh, "select_expiration_order", 1, paramValues,
731 paramLengths, paramFormats, 1);
732 process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__);
737 * Update the priority for a particular key in the datastore. If
738 * the expiration time in value is different than the time found in
739 * the datastore, the higher value should be kept. For the
740 * anonymity level, the lower value is to be used. The specified
741 * priority should be added to the existing priority, ignoring the
744 * Note that it is possible for multiple values to match this put.
745 * In that case, all of the respective values are updated.
747 * @param cls our "struct Plugin*"
748 * @param uid unique identifier of the datum
749 * @param delta by how much should the priority
750 * change? If priority + delta < 0 the
751 * priority should be set to 0 (never go
753 * @param expire new expiration time should be the
754 * MAX of any existing expiration time and
756 * @param msg set to error message
757 * @return GNUNET_OK on success
760 postgres_plugin_update (void *cls, uint64_t uid, int delta,
761 struct GNUNET_TIME_Absolute expire, char **msg)
763 struct Plugin *plugin = cls;
765 int32_t bdelta = (int32_t) htonl ((uint32_t) delta);
766 uint32_t boid = htonl ((uint32_t) uid);
767 uint64_t bexpire = GNUNET_TIME_absolute_hton (expire).abs_value_us__;
769 const char *paramValues[] = {
770 (const char *) &bdelta,
771 (const char *) &bexpire,
772 (const char *) &boid,
774 int paramLengths[] = {
779 const int paramFormats[] = { 1, 1, 1 };
782 PQexecPrepared (plugin->dbh, "update", 3, paramValues, paramLengths,
785 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "update"))
786 return GNUNET_SYSERR;
794 * Get all of the keys in the datastore.
796 * @param cls closure with the 'struct Plugin'
797 * @param proc function to call on each key
798 * @param proc_cls closure for proc
801 postgres_plugin_get_keys (void *cls,
802 PluginKeyProcessor proc,
805 struct Plugin *plugin = cls;
808 struct GNUNET_HashCode key;
811 res = PQexecPrepared (plugin->dbh, "get_keys", 0, NULL, NULL, NULL, 1);
812 ret = PQntuples (res);
815 if (sizeof (struct GNUNET_HashCode) != PQgetlength (res, i, 0))
817 memcpy (&key, PQgetvalue (res, i, 0), sizeof (struct GNUNET_HashCode));
818 proc (proc_cls, &key, 1);
829 * @param cls closure with the 'struct Plugin'
832 postgres_plugin_drop (void *cls)
834 struct Plugin *plugin = cls;
836 if (GNUNET_OK != GNUNET_POSTGRES_exec (plugin->dbh, "DROP TABLE gn090"))
837 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "postgres", _("Failed to drop table from database.\n"));
842 * Entry point for the plugin.
844 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
845 * @return our "struct Plugin*"
848 libgnunet_plugin_datastore_postgres_init (void *cls)
850 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
851 struct GNUNET_DATASTORE_PluginFunctions *api;
852 struct Plugin *plugin;
854 plugin = GNUNET_new (struct Plugin);
856 if (GNUNET_OK != init_connection (plugin))
858 GNUNET_free (plugin);
861 api = GNUNET_new (struct GNUNET_DATASTORE_PluginFunctions);
863 api->estimate_size = &postgres_plugin_estimate_size;
864 api->put = &postgres_plugin_put;
865 api->update = &postgres_plugin_update;
866 api->get_key = &postgres_plugin_get_key;
867 api->get_replication = &postgres_plugin_get_replication;
868 api->get_expiration = &postgres_plugin_get_expiration;
869 api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity;
870 api->get_keys = &postgres_plugin_get_keys;
871 api->drop = &postgres_plugin_drop;
872 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "datastore-postgres",
873 _("Postgres database running\n"));
879 * Exit point from the plugin.
880 * @param cls our "struct Plugin*"
881 * @return always NULL
884 libgnunet_plugin_datastore_postgres_done (void *cls)
886 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
887 struct Plugin *plugin = api->cls;
889 PQfinish (plugin->dbh);
890 GNUNET_free (plugin);
895 /* end of plugin_datastore_postgres.c */