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"
30 #include <postgresql/libpq-fe.h>
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 * @brief Get a database handle
67 * @param plugin global context
68 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
71 init_connection (struct Plugin *plugin)
75 plugin->dbh = GNUNET_POSTGRES_connect (plugin->env->cfg, "datastore-postgres");
76 if (NULL == plugin->dbh)
81 "CREATE TABLE gn090 (" " repl INTEGER NOT NULL DEFAULT 0,"
82 " type INTEGER NOT NULL DEFAULT 0,"
83 " prio INTEGER NOT NULL DEFAULT 0,"
84 " anonLevel INTEGER NOT NULL DEFAULT 0,"
85 " expire BIGINT NOT NULL DEFAULT 0,"
86 " rvalue BIGINT NOT NULL DEFAULT 0,"
87 " hash BYTEA NOT NULL DEFAULT '',"
88 " vhash BYTEA NOT NULL DEFAULT '',"
89 " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS");
91 ((PQresultStatus (ret) != PGRES_COMMAND_OK) &&
92 (0 != strcmp ("42P07", /* duplicate table */
97 (void) GNUNET_POSTGRES_check_result (plugin->dbh,
102 PQfinish (plugin->dbh);
104 return GNUNET_SYSERR;
107 if (PQresultStatus (ret) == PGRES_COMMAND_OK)
110 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_hash ON gn090 (hash)")) ||
112 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)")) ||
114 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_prio ON gn090 (prio)")) ||
116 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_expire ON gn090 (expire)")) ||
118 GNUNET_POSTGRES_exec (plugin->dbh,
119 "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)")) ||
121 GNUNET_POSTGRES_exec (plugin->dbh,
122 "CREATE INDEX idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)")) ||
124 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)")) ||
126 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_expire_hash ON gn090 (expire,hash)")))
129 PQfinish (plugin->dbh);
131 return GNUNET_SYSERR;
138 "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL");
140 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
142 PQfinish (plugin->dbh);
144 return GNUNET_SYSERR;
147 ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN");
149 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
151 PQfinish (plugin->dbh);
153 return GNUNET_SYSERR;
156 ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN");
158 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
160 PQfinish (plugin->dbh);
162 return GNUNET_SYSERR;
166 GNUNET_POSTGRES_prepare (plugin->dbh, "getvt",
167 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
168 "WHERE hash=$1 AND vhash=$2 AND type=$3 "
169 "ORDER BY oid ASC LIMIT 1 OFFSET $4", 4)) ||
171 GNUNET_POSTGRES_prepare (plugin->dbh, "gett",
172 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
173 "WHERE hash=$1 AND type=$2 "
174 "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3)) ||
176 GNUNET_POSTGRES_prepare (plugin->dbh, "getv",
177 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
178 "WHERE hash=$1 AND vhash=$2 "
179 "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3)) ||
181 GNUNET_POSTGRES_prepare (plugin->dbh, "get",
182 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
183 "WHERE hash=$1 " "ORDER BY oid ASC LIMIT 1 OFFSET $2", 2)) ||
185 GNUNET_POSTGRES_prepare (plugin->dbh, "put",
186 "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
187 "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", 9)) ||
189 GNUNET_POSTGRES_prepare (plugin->dbh, "update",
190 "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END "
191 "WHERE oid = $3", 3)) ||
193 GNUNET_POSTGRES_prepare (plugin->dbh, "decrepl",
194 "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) "
195 "WHERE oid = $1", 1)) ||
197 GNUNET_POSTGRES_prepare (plugin->dbh, "select_non_anonymous",
198 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
199 "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2",
202 GNUNET_POSTGRES_prepare (plugin->dbh, "select_expiration_order",
203 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
204 "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " "UNION "
205 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
206 "ORDER BY prio ASC LIMIT 1) " "ORDER BY expire ASC LIMIT 1",
209 GNUNET_POSTGRES_prepare (plugin->dbh, "select_replication_order",
210 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
211 "ORDER BY repl DESC,RANDOM() LIMIT 1", 0)) ||
213 GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1)) ||
215 GNUNET_POSTGRES_prepare (plugin->dbh, "get_keys", "SELECT hash FROM gn090", 0)))
217 PQfinish (plugin->dbh);
219 return GNUNET_SYSERR;
226 * Get an estimate of how much space the database is
229 * @param cls our `struct Plugin *`
230 * @return number of bytes used on disk
232 static unsigned long long
233 postgres_plugin_estimate_size (void *cls)
235 struct Plugin *plugin = cls;
236 unsigned long long total;
240 PQexecParams (plugin->dbh,
241 "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090", 0,
242 NULL, NULL, NULL, NULL, 1);
244 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_TUPLES_OK, "PQexecParams", "get_size"))
248 if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) )
254 if (PQgetlength (ret, 0, 0) != sizeof (unsigned long long))
256 GNUNET_break (0 == PQgetlength (ret, 0, 0));
260 total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
267 * Store an item in the datastore.
269 * @param cls closure with the `struct Plugin`
270 * @param key key for the item
271 * @param size number of bytes in data
272 * @param data content stored
273 * @param type type of the content
274 * @param priority priority of the content
275 * @param anonymity anonymity-level for the content
276 * @param replication replication-level for the content
277 * @param expiration expiration time for the content
278 * @param msg set to error message
279 * @return #GNUNET_OK on success
282 postgres_plugin_put (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
283 const void *data, enum GNUNET_BLOCK_Type type,
284 uint32_t priority, uint32_t anonymity,
285 uint32_t replication,
286 struct GNUNET_TIME_Absolute expiration, char **msg)
288 struct Plugin *plugin = cls;
289 struct GNUNET_HashCode vhash;
291 uint32_t btype = htonl (type);
292 uint32_t bprio = htonl (priority);
293 uint32_t banon = htonl (anonymity);
294 uint32_t brepl = htonl (replication);
295 uint64_t bexpi = GNUNET_TIME_absolute_hton (expiration).abs_value_us__;
297 const char *paramValues[] = {
298 (const char *) &brepl,
299 (const char *) &btype,
300 (const char *) &bprio,
301 (const char *) &banon,
302 (const char *) &bexpi,
304 (const char *) &vhash,
307 int paramLengths[] = {
313 sizeof (struct GNUNET_HashCode),
314 sizeof (struct GNUNET_HashCode),
317 const int paramFormats[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
319 GNUNET_CRYPTO_hash (data, size, &vhash);
321 PQexecPrepared (plugin->dbh, "put", 8, paramValues, paramLengths,
324 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put"))
325 return GNUNET_SYSERR;
327 plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
328 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
329 "Stored %u bytes in database\n", (unsigned int) size);
335 * Function invoked to process the result and call
338 * @param plugin global plugin data
339 * @param proc function to call the value (once only).
340 * @param proc_cls closure for proc
341 * @param res result from exec
342 * @param filename filename for error messages
343 * @param line line number for error messages
346 process_result (struct Plugin *plugin, PluginDatumProcessor proc,
347 void *proc_cls, PGresult * res,
348 const char *filename, int line)
351 enum GNUNET_BLOCK_Type type;
356 struct GNUNET_TIME_Absolute expiration_time;
357 struct GNUNET_HashCode key;
360 GNUNET_POSTGRES_check_result_ (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared", "select",
363 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
364 "Ending iteration (postgres error)\n");
365 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
369 if (0 == PQntuples (res))
372 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
373 "Ending iteration (no more results)\n");
374 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
378 if ((1 != PQntuples (res)) || (7 != PQnfields (res)) ||
379 (sizeof (uint32_t) != PQfsize (res, 0)) ||
380 (sizeof (uint32_t) != PQfsize (res, 6)))
383 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
387 rowid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 6));
388 if ((sizeof (uint32_t) != PQfsize (res, 0)) ||
389 (sizeof (uint32_t) != PQfsize (res, 1)) ||
390 (sizeof (uint32_t) != PQfsize (res, 2)) ||
391 (sizeof (uint64_t) != PQfsize (res, 3)) ||
392 (sizeof (struct GNUNET_HashCode) != PQgetlength (res, 0, 4)))
396 GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", rowid);
397 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
401 type = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0));
402 priority = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
403 anonymity = ntohl (*(uint32_t *) PQgetvalue (res, 0, 2));
404 expiration_time.abs_value_us =
405 GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 3));
406 memcpy (&key, PQgetvalue (res, 0, 4), sizeof (struct GNUNET_HashCode));
407 size = PQgetlength (res, 0, 5);
408 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
409 "Found result of size %u bytes and type %u in database\n",
410 (unsigned int) size, (unsigned int) type);
412 proc (proc_cls, &key, size, PQgetvalue (res, 0, 5),
413 (enum GNUNET_BLOCK_Type) type, priority, anonymity, expiration_time,
416 if (iret == GNUNET_NO)
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Processor asked for item %u to be removed.\n", rowid);
420 if (GNUNET_OK == GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", rowid))
422 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
423 "Deleting %u bytes from database\n",
424 (unsigned int) size);
425 plugin->env->duc (plugin->env->cls,
426 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
427 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
428 "Deleted %u bytes from database\n", (unsigned int) size);
435 * Iterate over the results for a particular key
438 * @param cls closure with the 'struct Plugin'
439 * @param offset offset of the result (modulo num-results);
440 * specific ordering does not matter for the offset
441 * @param key maybe NULL (to match all entries)
442 * @param vhash hash of the value, maybe NULL (to
443 * match all values that have the right key).
444 * Note that for DBlocks there is no difference
445 * betwen key and vhash, but for other blocks
447 * @param type entries of which type are relevant?
448 * Use 0 for any type.
449 * @param proc function to call on the matching value;
450 * will be called once with a NULL if no value matches
451 * @param proc_cls closure for iter
454 postgres_plugin_get_key (void *cls, uint64_t offset,
455 const struct GNUNET_HashCode * key,
456 const struct GNUNET_HashCode * vhash,
457 enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc,
460 struct Plugin *plugin = cls;
461 const int paramFormats[] = { 1, 1, 1, 1, 1 };
463 const char *paramValues[4];
471 GNUNET_assert (key != NULL);
472 paramValues[0] = (const char *) key;
473 paramLengths[0] = sizeof (struct GNUNET_HashCode);
474 btype = htonl (type);
479 paramValues[1] = (const char *) vhash;
480 paramLengths[1] = sizeof (struct GNUNET_HashCode);
481 paramValues[2] = (const char *) &btype;
482 paramLengths[2] = sizeof (btype);
483 paramValues[3] = (const char *) &blimit_off;
484 paramLengths[3] = sizeof (blimit_off);
488 PQexecParams (plugin->dbh,
489 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2 AND type=$3",
490 3, NULL, paramValues, paramLengths, paramFormats, 1);
494 paramValues[1] = (const char *) &btype;
495 paramLengths[1] = sizeof (btype);
496 paramValues[2] = (const char *) &blimit_off;
497 paramLengths[2] = sizeof (blimit_off);
501 PQexecParams (plugin->dbh,
502 "SELECT count(*) FROM gn090 WHERE hash=$1 AND type=$2",
503 2, NULL, paramValues, paramLengths, paramFormats, 1);
510 paramValues[1] = (const char *) vhash;
511 paramLengths[1] = sizeof (struct GNUNET_HashCode);
512 paramValues[2] = (const char *) &blimit_off;
513 paramLengths[2] = sizeof (blimit_off);
517 PQexecParams (plugin->dbh,
518 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2",
519 2, NULL, paramValues, paramLengths, paramFormats, 1);
523 paramValues[1] = (const char *) &blimit_off;
524 paramLengths[1] = sizeof (blimit_off);
528 PQexecParams (plugin->dbh, "SELECT count(*) FROM gn090 WHERE hash=$1",
529 1, NULL, paramValues, paramLengths, paramFormats, 1);
533 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_TUPLES_OK, "PQexecParams", pname))
535 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
538 if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) ||
539 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
543 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
546 total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
550 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
553 blimit_off = GNUNET_htonll (offset % total);
555 PQexecPrepared (plugin->dbh, pname, nparams, paramValues, paramLengths,
557 process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__);
562 * Select a subset of the items in the datastore and call
563 * the given iterator for each of them.
565 * @param cls our "struct Plugin*"
566 * @param offset offset of the result (modulo num-results);
567 * specific ordering does not matter for the offset
568 * @param type entries of which type should be considered?
569 * Use 0 for any type.
570 * @param proc function to call on the matching value;
571 * will be called with a NULL if no value matches
572 * @param proc_cls closure for proc
575 postgres_plugin_get_zero_anonymity (void *cls, uint64_t offset,
576 enum GNUNET_BLOCK_Type type,
577 PluginDatumProcessor proc, void *proc_cls)
579 struct Plugin *plugin = cls;
582 const int paramFormats[] = { 1, 1 };
583 int paramLengths[] = { sizeof (btype), sizeof (boff) };
584 const char *paramValues[] = { (const char *) &btype, (const char *) &boff };
587 btype = htonl ((uint32_t) type);
588 boff = GNUNET_htonll (offset);
590 PQexecPrepared (plugin->dbh, "select_non_anonymous", 2, paramValues,
591 paramLengths, paramFormats, 1);
592 process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__);
597 * Context for 'repl_iter' function.
605 struct Plugin *plugin;
608 * Function to call for the result (or the NULL).
610 PluginDatumProcessor proc;
620 * Wrapper for the iterator for 'sqlite_plugin_replication_get'.
621 * Decrements the replication counter and calls the original
624 * @param cls closure with the 'struct ReplCtx*'
625 * @param key key for the content
626 * @param size number of bytes in data
627 * @param data content stored
628 * @param type type of the content
629 * @param priority priority of the content
630 * @param anonymity anonymity-level for the content
631 * @param expiration expiration time for the content
632 * @param uid unique identifier for the datum;
633 * maybe 0 if no unique identifier is available
635 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
636 * (continue on call to "next", of course),
637 * GNUNET_NO to delete the item and continue (if supported)
640 repl_proc (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
641 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
642 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
645 struct ReplCtx *rc = cls;
646 struct Plugin *plugin = rc->plugin;
652 rc->proc (rc->proc_cls, key, size, data, type, priority, anonymity,
656 boid = htonl ((uint32_t) uid);
657 const char *paramValues[] = {
658 (const char *) &boid,
660 int paramLengths[] = {
663 const int paramFormats[] = { 1 };
665 PQexecPrepared (plugin->dbh, "decrepl", 1, paramValues, paramLengths,
668 GNUNET_POSTGRES_check_result (plugin->dbh, qret, PGRES_COMMAND_OK, "PQexecPrepared",
670 return GNUNET_SYSERR;
678 * Get a random item for replication. Returns a single, not expired, random item
679 * from those with the highest replication counters. The item's
680 * replication counter is decremented by one IF it was positive before.
681 * Call 'proc' with all values ZERO or NULL if the datastore is empty.
683 * @param cls closure with the 'struct Plugin'
684 * @param proc function to call the value (once only).
685 * @param proc_cls closure for proc
688 postgres_plugin_get_replication (void *cls, PluginDatumProcessor proc,
691 struct Plugin *plugin = cls;
697 rc.proc_cls = proc_cls;
699 PQexecPrepared (plugin->dbh, "select_replication_order", 0, NULL, NULL,
701 process_result (plugin, &repl_proc, &rc, ret, __FILE__, __LINE__);
706 * Get a random item for expiration.
707 * Call 'proc' with all values ZERO or NULL if the datastore is empty.
709 * @param cls closure with the 'struct Plugin'
710 * @param proc function to call the value (once only).
711 * @param proc_cls closure for proc
714 postgres_plugin_get_expiration (void *cls, PluginDatumProcessor proc,
717 struct Plugin *plugin = cls;
719 const int paramFormats[] = { 1 };
720 int paramLengths[] = { sizeof (btime) };
721 const char *paramValues[] = { (const char *) &btime };
724 btime = GNUNET_htonll (GNUNET_TIME_absolute_get ().abs_value_us);
726 PQexecPrepared (plugin->dbh, "select_expiration_order", 1, paramValues,
727 paramLengths, paramFormats, 1);
728 process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__);
733 * Update the priority for a particular key in the datastore. If
734 * the expiration time in value is different than the time found in
735 * the datastore, the higher value should be kept. For the
736 * anonymity level, the lower value is to be used. The specified
737 * priority should be added to the existing priority, ignoring the
740 * Note that it is possible for multiple values to match this put.
741 * In that case, all of the respective values are updated.
743 * @param cls our "struct Plugin*"
744 * @param uid unique identifier of the datum
745 * @param delta by how much should the priority
746 * change? If priority + delta < 0 the
747 * priority should be set to 0 (never go
749 * @param expire new expiration time should be the
750 * MAX of any existing expiration time and
752 * @param msg set to error message
753 * @return GNUNET_OK on success
756 postgres_plugin_update (void *cls, uint64_t uid, int delta,
757 struct GNUNET_TIME_Absolute expire, char **msg)
759 struct Plugin *plugin = cls;
761 int32_t bdelta = (int32_t) htonl ((uint32_t) delta);
762 uint32_t boid = htonl ((uint32_t) uid);
763 uint64_t bexpire = GNUNET_TIME_absolute_hton (expire).abs_value_us__;
765 const char *paramValues[] = {
766 (const char *) &bdelta,
767 (const char *) &bexpire,
768 (const char *) &boid,
770 int paramLengths[] = {
775 const int paramFormats[] = { 1, 1, 1 };
778 PQexecPrepared (plugin->dbh, "update", 3, paramValues, paramLengths,
781 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "update"))
782 return GNUNET_SYSERR;
790 * Get all of the keys in the datastore.
792 * @param cls closure with the 'struct Plugin'
793 * @param proc function to call on each key
794 * @param proc_cls closure for proc
797 postgres_plugin_get_keys (void *cls,
798 PluginKeyProcessor proc,
801 struct Plugin *plugin = cls;
804 struct GNUNET_HashCode key;
807 res = PQexecPrepared (plugin->dbh, "get_keys", 0, NULL, NULL, NULL, 1);
808 ret = PQntuples (res);
811 if (sizeof (struct GNUNET_HashCode) != PQgetlength (res, i, 0))
813 memcpy (&key, PQgetvalue (res, i, 0), sizeof (struct GNUNET_HashCode));
814 proc (proc_cls, &key, 1);
825 * @param cls closure with the 'struct Plugin'
828 postgres_plugin_drop (void *cls)
830 struct Plugin *plugin = cls;
832 if (GNUNET_OK != GNUNET_POSTGRES_exec (plugin->dbh, "DROP TABLE gn090"))
833 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "postgres", _("Failed to drop table from database.\n"));
838 * Entry point for the plugin.
840 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
841 * @return our "struct Plugin*"
844 libgnunet_plugin_datastore_postgres_init (void *cls)
846 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
847 struct GNUNET_DATASTORE_PluginFunctions *api;
848 struct Plugin *plugin;
850 plugin = GNUNET_new (struct Plugin);
852 if (GNUNET_OK != init_connection (plugin))
854 GNUNET_free (plugin);
857 api = GNUNET_new (struct GNUNET_DATASTORE_PluginFunctions);
859 api->estimate_size = &postgres_plugin_estimate_size;
860 api->put = &postgres_plugin_put;
861 api->update = &postgres_plugin_update;
862 api->get_key = &postgres_plugin_get_key;
863 api->get_replication = &postgres_plugin_get_replication;
864 api->get_expiration = &postgres_plugin_get_expiration;
865 api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity;
866 api->get_keys = &postgres_plugin_get_keys;
867 api->drop = &postgres_plugin_drop;
868 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "datastore-postgres",
869 _("Postgres database running\n"));
875 * Exit point from the plugin.
876 * @param cls our "struct Plugin*"
877 * @return always NULL
880 libgnunet_plugin_datastore_postgres_done (void *cls)
882 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
883 struct Plugin *plugin = api->cls;
885 PQfinish (plugin->dbh);
886 GNUNET_free (plugin);
891 /* end of plugin_datastore_postgres.c */