2 This file is part of GNUnet
3 Copyright (C) 2009-2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 "gnunet_pq_lib.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)
79 /* FIXME: PostgreSQL does not have unsigned integers! This is ok for the type column because
80 * we only test equality on it and can cast it to/from uint32_t. For repl, prio, and anonLevel
81 * we do math or inequality tests, so we can't handle the entire range of uint32_t.
82 * This will also cause problems for expiration times after 294247-01-10-04:00:54 UTC.
86 "CREATE TABLE IF NOT EXISTS gn090 ("
87 " repl INTEGER NOT NULL DEFAULT 0,"
88 " type INTEGER NOT NULL DEFAULT 0,"
89 " prio INTEGER NOT NULL DEFAULT 0,"
90 " anonLevel INTEGER NOT NULL DEFAULT 0,"
91 " expire BIGINT NOT NULL DEFAULT 0,"
92 " rvalue BIGINT NOT NULL DEFAULT 0,"
93 " hash BYTEA NOT NULL DEFAULT '',"
94 " vhash BYTEA NOT NULL DEFAULT '',"
95 " value BYTEA NOT NULL DEFAULT '')"
98 ((PQresultStatus (ret) != PGRES_COMMAND_OK) &&
99 (0 != strcmp ("42P07", /* duplicate table */
102 PG_DIAG_SQLSTATE)))))
104 (void) GNUNET_POSTGRES_check_result (plugin->dbh,
109 PQfinish (plugin->dbh);
111 return GNUNET_SYSERR;
114 if (PQresultStatus (ret) == PGRES_COMMAND_OK)
117 GNUNET_POSTGRES_exec (plugin->dbh,
118 "CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash)")) ||
120 GNUNET_POSTGRES_exec (plugin->dbh,
121 "CREATE INDEX IF NOT EXISTS idx_hash_vhash ON gn090 (hash,vhash)")) ||
123 GNUNET_POSTGRES_exec (plugin->dbh,
124 "CREATE INDEX IF NOT EXISTS idx_prio ON gn090 (prio)")) ||
126 GNUNET_POSTGRES_exec (plugin->dbh,
127 "CREATE INDEX IF NOT EXISTS idx_expire ON gn090 (expire)")) ||
129 GNUNET_POSTGRES_exec (plugin->dbh,
130 "CREATE INDEX IF NOT EXISTS idx_prio_anon ON gn090 (prio,anonLevel)")) ||
132 GNUNET_POSTGRES_exec (plugin->dbh,
133 "CREATE INDEX IF NOT EXISTS idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)")) ||
135 GNUNET_POSTGRES_exec (plugin->dbh,
136 "CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn090 (repl,rvalue)")) ||
138 GNUNET_POSTGRES_exec (plugin->dbh,
139 "CREATE INDEX IF NOT EXISTS idx_expire_hash ON gn090 (expire,hash)")))
142 PQfinish (plugin->dbh);
144 return GNUNET_SYSERR;
151 "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL");
153 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
155 PQfinish (plugin->dbh);
157 return GNUNET_SYSERR;
160 ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN");
162 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
164 PQfinish (plugin->dbh);
166 return GNUNET_SYSERR;
169 ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN");
171 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
173 PQfinish (plugin->dbh);
175 return GNUNET_SYSERR;
179 GNUNET_POSTGRES_prepare (plugin->dbh, "getvt",
180 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
181 "WHERE hash=$1 AND vhash=$2 AND type=$3 "
182 "ORDER BY oid ASC LIMIT 1 OFFSET $4", 4)) ||
184 GNUNET_POSTGRES_prepare (plugin->dbh, "gett",
185 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
186 "WHERE hash=$1 AND type=$2 "
187 "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3)) ||
189 GNUNET_POSTGRES_prepare (plugin->dbh, "getv",
190 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
191 "WHERE hash=$1 AND vhash=$2 "
192 "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3)) ||
194 GNUNET_POSTGRES_prepare (plugin->dbh, "get",
195 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
196 "WHERE hash=$1 " "ORDER BY oid ASC LIMIT 1 OFFSET $2", 2)) ||
198 GNUNET_POSTGRES_prepare (plugin->dbh, "count_getvt",
199 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2 AND type=$3", 3)) ||
201 GNUNET_POSTGRES_prepare (plugin->dbh, "count_gett",
202 "SELECT count(*) FROM gn090 WHERE hash=$1 AND type=$2", 2)) ||
204 GNUNET_POSTGRES_prepare (plugin->dbh, "count_getv",
205 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2", 2)) ||
207 GNUNET_POSTGRES_prepare (plugin->dbh, "count_get",
208 "SELECT count(*) FROM gn090 WHERE hash=$1", 1)) ||
210 GNUNET_POSTGRES_prepare (plugin->dbh, "put",
211 "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
212 "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", 9)) ||
214 GNUNET_POSTGRES_prepare (plugin->dbh, "update",
215 "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END "
216 "WHERE oid = $3", 3)) ||
218 GNUNET_POSTGRES_prepare (plugin->dbh, "decrepl",
219 "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) "
220 "WHERE oid = $1", 1)) ||
222 GNUNET_POSTGRES_prepare (plugin->dbh, "select_non_anonymous",
223 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
224 "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2",
227 GNUNET_POSTGRES_prepare (plugin->dbh, "select_expiration_order",
228 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
229 "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " "UNION "
230 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
231 "ORDER BY prio ASC LIMIT 1) " "ORDER BY expire ASC LIMIT 1",
234 GNUNET_POSTGRES_prepare (plugin->dbh, "select_replication_order",
235 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
236 "ORDER BY repl DESC,RANDOM() LIMIT 1", 0)) ||
238 GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1)) ||
240 GNUNET_POSTGRES_prepare (plugin->dbh, "get_keys", "SELECT hash FROM gn090", 0)))
242 PQfinish (plugin->dbh);
244 return GNUNET_SYSERR;
251 * Get an estimate of how much space the database is
254 * @param cls our `struct Plugin *`
255 * @return number of bytes used on disk
258 postgres_plugin_estimate_size (void *cls, unsigned long long *estimate)
260 struct Plugin *plugin = cls;
261 unsigned long long total;
264 if (NULL == estimate)
267 PQexecParams (plugin->dbh,
268 "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090", 0,
269 NULL, NULL, NULL, NULL, 1);
271 GNUNET_POSTGRES_check_result (plugin->dbh,
280 if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) )
287 if (PQgetlength (ret, 0, 0) != sizeof (unsigned long long))
289 GNUNET_break (0 == PQgetlength (ret, 0, 0));
294 total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
301 * Store an item in the datastore.
303 * @param cls closure with the `struct Plugin`
304 * @param key key for the item
305 * @param size number of bytes in data
306 * @param data content stored
307 * @param type type of the content
308 * @param priority priority of the content
309 * @param anonymity anonymity-level for the content
310 * @param replication replication-level for the content
311 * @param expiration expiration time for the content
312 * @param cont continuation called with success or failure status
313 * @param cont_cls continuation closure
316 postgres_plugin_put (void *cls,
317 const struct GNUNET_HashCode *key,
320 enum GNUNET_BLOCK_Type type,
323 uint32_t replication,
324 struct GNUNET_TIME_Absolute expiration,
328 struct Plugin *plugin = cls;
329 uint32_t utype = type;
330 struct GNUNET_HashCode vhash;
332 struct GNUNET_PQ_QueryParam params[] = {
333 GNUNET_PQ_query_param_uint32 (&replication),
334 GNUNET_PQ_query_param_uint32 (&utype),
335 GNUNET_PQ_query_param_uint32 (&priority),
336 GNUNET_PQ_query_param_uint32 (&anonymity),
337 GNUNET_PQ_query_param_absolute_time (&expiration),
338 GNUNET_PQ_query_param_auto_from_type (key),
339 GNUNET_PQ_query_param_auto_from_type (&vhash),
340 GNUNET_PQ_query_param_fixed_size (data, size),
341 GNUNET_PQ_query_param_end
344 GNUNET_CRYPTO_hash (data, size, &vhash);
345 ret = GNUNET_PQ_exec_prepared (plugin->dbh,
349 GNUNET_POSTGRES_check_result (plugin->dbh,
352 "PQexecPrepared", "put"))
354 cont (cont_cls, key, size,
356 _("Postgress exec failure"));
360 plugin->env->duc (plugin->env->cls,
361 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
362 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
363 "datastore-postgres",
364 "Stored %u bytes in database\n",
365 (unsigned int) size);
366 cont (cont_cls, key, size, GNUNET_OK, NULL);
371 * Function invoked to process the result and call the processor.
373 * @param plugin global plugin data
374 * @param proc function to call the value (once only).
375 * @param proc_cls closure for proc
376 * @param res result from exec
377 * @param filename filename for error messages
378 * @param line line number for error messages
381 process_result (struct Plugin *plugin,
382 PluginDatumProcessor proc,
385 const char *filename, int line)
394 struct GNUNET_TIME_Absolute expiration_time;
395 struct GNUNET_HashCode key;
396 struct GNUNET_PQ_ResultSpec rs[] = {
397 GNUNET_PQ_result_spec_uint32 ("type", &utype),
398 GNUNET_PQ_result_spec_uint32 ("prio", &priority),
399 GNUNET_PQ_result_spec_uint32 ("anonLevel", &anonymity),
400 GNUNET_PQ_result_spec_uint32 ("oid", &rowid),
401 GNUNET_PQ_result_spec_absolute_time ("expire", &expiration_time),
402 GNUNET_PQ_result_spec_auto_from_type ("hash", &key),
403 GNUNET_PQ_result_spec_variable_size ("value", &data, &size),
404 GNUNET_PQ_result_spec_end
408 GNUNET_POSTGRES_check_result_ (plugin->dbh,
415 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
416 "datastore-postgres",
417 "Ending iteration (postgres error)\n");
418 proc (proc_cls, NULL, 0, NULL, 0, 0, 0,
419 GNUNET_TIME_UNIT_ZERO_ABS, 0);
423 if (0 == PQntuples (res))
426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
427 "datastore-postgres",
428 "Ending iteration (no more results)\n");
429 proc (proc_cls, NULL, 0, NULL, 0, 0, 0,
430 GNUNET_TIME_UNIT_ZERO_ABS, 0);
434 if (1 != PQntuples (res))
437 proc (proc_cls, NULL, 0, NULL, 0, 0, 0,
438 GNUNET_TIME_UNIT_ZERO_ABS, 0);
443 GNUNET_PQ_extract_result (res,
449 GNUNET_POSTGRES_delete_by_rowid (plugin->dbh,
452 proc (proc_cls, NULL, 0, NULL, 0, 0, 0,
453 GNUNET_TIME_UNIT_ZERO_ABS, 0);
457 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
458 "datastore-postgres",
459 "Found result of size %u bytes and type %u in database\n",
461 (unsigned int) utype);
462 iret = proc (proc_cls,
466 (enum GNUNET_BLOCK_Type) utype,
472 if (iret == GNUNET_NO)
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 "Processor asked for item %u to be removed.\n",
476 (unsigned int) rowid);
478 GNUNET_POSTGRES_delete_by_rowid (plugin->dbh,
482 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
483 "datastore-postgres",
484 "Deleting %u bytes from database\n",
485 (unsigned int) size);
486 plugin->env->duc (plugin->env->cls,
487 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
488 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
489 "datastore-postgres",
490 "Deleted %u bytes from database\n",
491 (unsigned int) size);
498 * Iterate over the results for a particular key
501 * @param cls closure with the 'struct Plugin'
502 * @param offset offset of the result (modulo num-results);
503 * specific ordering does not matter for the offset
504 * @param key maybe NULL (to match all entries)
505 * @param vhash hash of the value, maybe NULL (to
506 * match all values that have the right key).
507 * Note that for DBlocks there is no difference
508 * betwen key and vhash, but for other blocks
510 * @param type entries of which type are relevant?
511 * Use 0 for any type.
512 * @param proc function to call on the matching value;
513 * will be called once with a NULL if no value matches
514 * @param proc_cls closure for iter
517 postgres_plugin_get_key (void *cls,
519 const struct GNUNET_HashCode *key,
520 const struct GNUNET_HashCode *vhash,
521 enum GNUNET_BLOCK_Type type,
522 PluginDatumProcessor proc,
525 struct Plugin *plugin = cls;
526 uint32_t utype = type;
535 struct GNUNET_PQ_QueryParam params[] = {
536 GNUNET_PQ_query_param_auto_from_type (key),
537 GNUNET_PQ_query_param_auto_from_type (vhash),
538 GNUNET_PQ_query_param_uint32 (&utype),
539 GNUNET_PQ_query_param_end
541 ret = GNUNET_PQ_exec_prepared (plugin->dbh,
547 struct GNUNET_PQ_QueryParam params[] = {
548 GNUNET_PQ_query_param_auto_from_type (key),
549 GNUNET_PQ_query_param_uint32 (&utype),
550 GNUNET_PQ_query_param_end
552 ret = GNUNET_PQ_exec_prepared (plugin->dbh,
561 struct GNUNET_PQ_QueryParam params[] = {
562 GNUNET_PQ_query_param_auto_from_type (key),
563 GNUNET_PQ_query_param_auto_from_type (vhash),
564 GNUNET_PQ_query_param_end
566 ret = GNUNET_PQ_exec_prepared (plugin->dbh,
572 struct GNUNET_PQ_QueryParam params[] = {
573 GNUNET_PQ_query_param_auto_from_type (key),
574 GNUNET_PQ_query_param_end
576 ret = GNUNET_PQ_exec_prepared (plugin->dbh,
583 GNUNET_POSTGRES_check_result (plugin->dbh,
589 proc (proc_cls, NULL, 0, NULL, 0, 0, 0,
590 GNUNET_TIME_UNIT_ZERO_ABS, 0);
593 if ( (PQntuples (ret) != 1) ||
594 (PQnfields (ret) != 1) ||
595 (PQgetlength (ret, 0, 0) != sizeof (uint64_t)))
599 proc (proc_cls, NULL, 0, NULL, 0, 0, 0,
600 GNUNET_TIME_UNIT_ZERO_ABS, 0);
603 total = GNUNET_ntohll (*(const uint64_t *) PQgetvalue (ret, 0, 0));
607 proc (proc_cls, NULL, 0, NULL, 0, 0, 0,
608 GNUNET_TIME_UNIT_ZERO_ABS, 0);
611 limit_off = offset % total;
617 struct GNUNET_PQ_QueryParam params[] = {
618 GNUNET_PQ_query_param_auto_from_type (key),
619 GNUNET_PQ_query_param_auto_from_type (vhash),
620 GNUNET_PQ_query_param_uint32 (&utype),
621 GNUNET_PQ_query_param_uint64 (&limit_off),
622 GNUNET_PQ_query_param_end
624 ret = GNUNET_PQ_exec_prepared (plugin->dbh,
630 struct GNUNET_PQ_QueryParam params[] = {
631 GNUNET_PQ_query_param_auto_from_type (key),
632 GNUNET_PQ_query_param_uint32 (&utype),
633 GNUNET_PQ_query_param_uint64 (&limit_off),
634 GNUNET_PQ_query_param_end
636 ret = GNUNET_PQ_exec_prepared (plugin->dbh,
645 struct GNUNET_PQ_QueryParam params[] = {
646 GNUNET_PQ_query_param_auto_from_type (key),
647 GNUNET_PQ_query_param_auto_from_type (vhash),
648 GNUNET_PQ_query_param_uint64 (&limit_off),
649 GNUNET_PQ_query_param_end
651 ret = GNUNET_PQ_exec_prepared (plugin->dbh,
657 struct GNUNET_PQ_QueryParam params[] = {
658 GNUNET_PQ_query_param_auto_from_type (key),
659 GNUNET_PQ_query_param_uint64 (&limit_off),
660 GNUNET_PQ_query_param_end
662 ret = GNUNET_PQ_exec_prepared (plugin->dbh,
667 process_result (plugin,
676 * Select a subset of the items in the datastore and call
677 * the given iterator for each of them.
679 * @param cls our `struct Plugin *`
680 * @param offset offset of the result (modulo num-results);
681 * specific ordering does not matter for the offset
682 * @param type entries of which type should be considered?
683 * Use 0 for any type.
684 * @param proc function to call on the matching value;
685 * will be called with a NULL if no value matches
686 * @param proc_cls closure for @a proc
689 postgres_plugin_get_zero_anonymity (void *cls,
691 enum GNUNET_BLOCK_Type type,
692 PluginDatumProcessor proc,
695 struct Plugin *plugin = cls;
696 uint32_t utype = type;
697 struct GNUNET_PQ_QueryParam params[] = {
698 GNUNET_PQ_query_param_uint32 (&utype),
699 GNUNET_PQ_query_param_uint64 (&offset),
700 GNUNET_PQ_query_param_end
704 ret = GNUNET_PQ_exec_prepared (plugin->dbh,
705 "select_non_anonymous",
708 process_result (plugin,
716 * Context for #repl_iter() function.
724 struct Plugin *plugin;
727 * Function to call for the result (or the NULL).
729 PluginDatumProcessor proc;
732 * Closure for @e proc.
739 * Wrapper for the iterator for 'sqlite_plugin_replication_get'.
740 * Decrements the replication counter and calls the original
743 * @param cls closure with the `struct ReplCtx *`
744 * @param key key for the content
745 * @param size number of bytes in @a data
746 * @param data content stored
747 * @param type type of the content
748 * @param priority priority of the content
749 * @param anonymity anonymity-level for the content
750 * @param expiration expiration time for the content
751 * @param uid unique identifier for the datum;
752 * maybe 0 if no unique identifier is available
753 * @return #GNUNET_SYSERR to abort the iteration,
754 * #GNUNET_OK to continue
755 * (continue on call to "next", of course),
756 * #GNUNET_NO to delete the item and continue (if supported)
759 repl_proc (void *cls,
760 const struct GNUNET_HashCode *key,
763 enum GNUNET_BLOCK_Type type,
766 struct GNUNET_TIME_Absolute expiration,
769 struct ReplCtx *rc = cls;
770 struct Plugin *plugin = rc->plugin;
772 uint32_t oid = (uint32_t) uid;
773 struct GNUNET_PQ_QueryParam params[] = {
774 GNUNET_PQ_query_param_uint32 (&oid),
775 GNUNET_PQ_query_param_end
779 ret = rc->proc (rc->proc_cls,
788 qret = GNUNET_PQ_exec_prepared (plugin->dbh,
792 GNUNET_POSTGRES_check_result (plugin->dbh,
797 return GNUNET_SYSERR;
804 * Get a random item for replication. Returns a single, not expired,
805 * random item from those with the highest replication counters. The
806 * item's replication counter is decremented by one IF it was positive
807 * before. Call @a proc with all values ZERO or NULL if the datastore
810 * @param cls closure with the `struct Plugin`
811 * @param proc function to call the value (once only).
812 * @param proc_cls closure for @a proc
815 postgres_plugin_get_replication (void *cls,
816 PluginDatumProcessor proc,
819 struct Plugin *plugin = cls;
825 rc.proc_cls = proc_cls;
826 ret = PQexecPrepared (plugin->dbh,
827 "select_replication_order", 0, NULL, NULL,
829 process_result (plugin,
838 * Get a random item for expiration. Call @a proc with all values
839 * ZERO or NULL if the datastore is empty.
841 * @param cls closure with the `struct Plugin`
842 * @param proc function to call the value (once only).
843 * @param proc_cls closure for @a proc
846 postgres_plugin_get_expiration (void *cls,
847 PluginDatumProcessor proc,
850 struct Plugin *plugin = cls;
851 struct GNUNET_TIME_Absolute now;
852 struct GNUNET_PQ_QueryParam params[] = {
853 GNUNET_PQ_query_param_absolute_time (&now),
854 GNUNET_PQ_query_param_end
858 now = GNUNET_TIME_absolute_get ();
859 ret = GNUNET_PQ_exec_prepared (plugin->dbh,
860 "select_expiration_order",
862 process_result (plugin,
870 * Update the priority for a particular key in the datastore. If
871 * the expiration time in value is different than the time found in
872 * the datastore, the higher value should be kept. For the
873 * anonymity level, the lower value is to be used. The specified
874 * priority should be added to the existing priority, ignoring the
877 * Note that it is possible for multiple values to match this put.
878 * In that case, all of the respective values are updated.
880 * @param cls our `struct Plugin *`
881 * @param uid unique identifier of the datum
882 * @param delta by how much should the priority
884 * @param expire new expiration time should be the
885 * MAX of any existing expiration time and
887 * @param cont continuation called with success or failure status
888 * @param cons_cls continuation closure
891 postgres_plugin_update (void *cls,
894 struct GNUNET_TIME_Absolute expire,
895 PluginUpdateCont cont,
898 struct Plugin *plugin = cls;
899 uint32_t oid = (uint32_t) uid;
900 struct GNUNET_PQ_QueryParam params[] = {
901 GNUNET_PQ_query_param_uint32 (&delta),
902 GNUNET_PQ_query_param_absolute_time (&expire),
903 GNUNET_PQ_query_param_uint32 (&oid),
904 GNUNET_PQ_query_param_end
908 ret = GNUNET_PQ_exec_prepared (plugin->dbh,
912 GNUNET_POSTGRES_check_result (plugin->dbh,
931 * Get all of the keys in the datastore.
933 * @param cls closure with the `struct Plugin *`
934 * @param proc function to call on each key
935 * @param proc_cls closure for @a proc
938 postgres_plugin_get_keys (void *cls,
939 PluginKeyProcessor proc,
942 struct Plugin *plugin = cls;
945 struct GNUNET_HashCode key;
948 res = PQexecPrepared (plugin->dbh,
950 0, NULL, NULL, NULL, 1);
951 ret = PQntuples (res);
954 if (sizeof (struct GNUNET_HashCode) !=
955 PQgetlength (res, i, 0))
958 PQgetvalue (res, i, 0),
959 sizeof (struct GNUNET_HashCode));
960 proc (proc_cls, &key, 1);
964 proc (proc_cls, NULL, 0);
971 * @param cls closure with the `struct Plugin *`
974 postgres_plugin_drop (void *cls)
976 struct Plugin *plugin = cls;
979 GNUNET_POSTGRES_exec (plugin->dbh,
981 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
983 _("Failed to drop table from database.\n"));
988 * Entry point for the plugin.
990 * @param cls the `struct GNUNET_DATASTORE_PluginEnvironment*`
991 * @return our `struct Plugin *`
994 libgnunet_plugin_datastore_postgres_init (void *cls)
996 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
997 struct GNUNET_DATASTORE_PluginFunctions *api;
998 struct Plugin *plugin;
1000 plugin = GNUNET_new (struct Plugin);
1002 if (GNUNET_OK != init_connection (plugin))
1004 GNUNET_free (plugin);
1007 api = GNUNET_new (struct GNUNET_DATASTORE_PluginFunctions);
1009 api->estimate_size = &postgres_plugin_estimate_size;
1010 api->put = &postgres_plugin_put;
1011 api->update = &postgres_plugin_update;
1012 api->get_key = &postgres_plugin_get_key;
1013 api->get_replication = &postgres_plugin_get_replication;
1014 api->get_expiration = &postgres_plugin_get_expiration;
1015 api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity;
1016 api->get_keys = &postgres_plugin_get_keys;
1017 api->drop = &postgres_plugin_drop;
1018 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1019 "datastore-postgres",
1020 _("Postgres database running\n"));
1026 * Exit point from the plugin.
1028 * @param cls our `struct Plugin *`
1029 * @return always NULL
1032 libgnunet_plugin_datastore_postgres_done (void *cls)
1034 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1035 struct Plugin *plugin = api->cls;
1037 PQfinish (plugin->dbh);
1038 GNUNET_free (plugin);
1043 /* end of plugin_datastore_postgres.c */