aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/datastore/Makefile.am9
-rw-r--r--src/datastore/datastore-0001.sql49
-rw-r--r--src/datastore/datastore-drop.sql25
-rw-r--r--src/datastore/datastore.conf.in1
-rw-r--r--src/datastore/plugin_datastore_postgres.c113
-rw-r--r--src/include/gnunet_datastore_plugin.h22
-rw-r--r--src/namecache/namecache.conf.in3
-rw-r--r--src/namestore/namestore.conf.in2
8 files changed, 132 insertions, 92 deletions
diff --git a/src/datastore/Makefile.am b/src/datastore/Makefile.am
index 07ae004b3..b73a0497b 100644
--- a/src/datastore/Makefile.am
+++ b/src/datastore/Makefile.am
@@ -10,6 +10,12 @@ libexecdir= $(pkglibdir)/libexec/
10pkgcfg_DATA = \ 10pkgcfg_DATA = \
11 datastore.conf 11 datastore.conf
12 12
13sqldir = $(prefix)/share/gnunet/sql/
14
15sql_DATA = \
16 datastore-0001.sql \
17 datastore-drop.sql
18
13if USE_COVERAGE 19if USE_COVERAGE
14 AM_CFLAGS = --coverage -O0 20 AM_CFLAGS = --coverage -O0
15 XLIBS = -lgcov 21 XLIBS = -lgcov
@@ -318,4 +324,5 @@ EXTRA_DIST = \
318 test_plugin_datastore_data_mysql.conf \ 324 test_plugin_datastore_data_mysql.conf \
319 test_datastore_api_data_postgres.conf \ 325 test_datastore_api_data_postgres.conf \
320 perf_plugin_datastore_data_postgres.conf \ 326 perf_plugin_datastore_data_postgres.conf \
321 test_plugin_datastore_data_postgres.conf 327 test_plugin_datastore_data_postgres.conf \
328 $(sql_DATA)
diff --git a/src/datastore/datastore-0001.sql b/src/datastore/datastore-0001.sql
new file mode 100644
index 000000000..0d4758be2
--- /dev/null
+++ b/src/datastore/datastore-0001.sql
@@ -0,0 +1,49 @@
1--
2-- This file is part of GNUnet
3-- Copyright (C) 2014--2022 GNUnet e.V.
4--
5-- GNUnet is free software; you can redistribute it and/or modify it under the
6-- terms of the GNU General Public License as published by the Free Software
7-- Foundation; either version 3, or (at your option) any later version.
8--
9-- GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11-- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12--
13-- You should have received a copy of the GNU General Public License along with
14-- GNUnet; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15--
16
17-- Everything in one big transaction
18BEGIN;
19
20-- Check patch versioning is in place.
21SELECT _v.register_patch('datastore-0001', NULL, NULL);
22
23-------------------- Schema ----------------------------
24
25CREATE SCHEMA datastore;
26COMMENT ON SCHEMA datastore IS 'gnunet-datastore data';
27
28SET search_path TO datastore;
29
30CREATE TABLE IF NOT EXISTS gn090 (
31 repl INTEGER NOT NULL DEFAULT 0,
32 type INTEGER NOT NULL DEFAULT 0,
33 prio INTEGER NOT NULL DEFAULT 0,
34 anonLevel INTEGER NOT NULL DEFAULT 0,
35 expire BIGINT NOT NULL DEFAULT 0,
36 rvalue BIGINT NOT NULL DEFAULT 0,
37 hash BYTEA NOT NULL DEFAULT '',
38 vhash BYTEA NOT NULL DEFAULT '',
39 value BYTEA NOT NULL DEFAULT '',
40 oid BIGINT GENERATED BY DEFAULT AS IDENTITY);
41
42CREATE INDEX IF NOT EXISTS oid_hash ON gn090 (oid);
43CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash);
44CREATE INDEX IF NOT EXISTS idx_prio_anon ON gn090 (prio,anonLevel);
45CREATE INDEX IF NOT EXISTS idx_prio_hash_anon ON gn090 (prio,hash,anonLevel);
46CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn090 (repl,rvalue);
47CREATE INDEX IF NOT EXISTS idx_expire_hash ON gn090 (expire,hash);
48
49COMMIT;
diff --git a/src/datastore/datastore-drop.sql b/src/datastore/datastore-drop.sql
new file mode 100644
index 000000000..67fee303d
--- /dev/null
+++ b/src/datastore/datastore-drop.sql
@@ -0,0 +1,25 @@
1--
2-- This file is part of GNUnet
3-- Copyright (C) 2014--2022 GNUnet e.V.
4--
5-- GNUnet is free software; you can redistribute it and/or modify it under the
6-- terms of the GNU General Public License as published by the Free Software
7-- Foundation; either version 3, or (at your option) any later version.
8--
9-- GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11-- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12--
13-- You should have received a copy of the GNU General Public License along with
14-- GNUnet; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15--
16
17-- Everything in one big transaction
18BEGIN;
19
20
21SELECT _v.unregister_patch('datastore-0001');
22
23DROP SCHEMA datastore CASCADE;
24
25COMMIT;
diff --git a/src/datastore/datastore.conf.in b/src/datastore/datastore.conf.in
index 21d24bb52..bcd495c8f 100644
--- a/src/datastore/datastore.conf.in
+++ b/src/datastore/datastore.conf.in
@@ -18,6 +18,7 @@ FILENAME = $GNUNET_DATA_HOME/datastore/sqlite.db
18 18
19[datastore-postgres] 19[datastore-postgres]
20CONFIG = postgres:///gnunet 20CONFIG = postgres:///gnunet
21SQL_DIR = ${DATADIR}/sql/
21 22
22[datastore-mysql] 23[datastore-mysql]
23DATABASE = gnunet 24DATABASE = gnunet
diff --git a/src/datastore/plugin_datastore_postgres.c b/src/datastore/plugin_datastore_postgres.c
index e49564dd9..5fcacc17b 100644
--- a/src/datastore/plugin_datastore_postgres.c
+++ b/src/datastore/plugin_datastore_postgres.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2009-2017 GNUnet e.V. 3 Copyright (C) 2009-2017, 2022 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
@@ -64,102 +64,67 @@ struct Plugin
64 * @param plugin global context 64 * @param plugin global context
65 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 65 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
66 */ 66 */
67static int 67static enum GNUNET_GenericReturnValue
68init_connection (struct Plugin *plugin) 68init_connection (struct Plugin *plugin)
69{ 69{
70 struct GNUNET_PQ_ExecuteStatement es[] = {
71 /* FIXME: PostgreSQL does not have unsigned integers! This is ok for the type column because
72 * we only test equality on it and can cast it to/from uint32_t. For repl, prio, and anonLevel
73 * we do math or inequality tests, so we can't handle the entire range of uint32_t.
74 * This will also cause problems for expiration times after 294247-01-10-04:00:54 UTC.
75 */
76 GNUNET_PQ_make_try_execute (
77 "CREATE SEQUENCE IF NOT EXISTS gn090_oid_seq"),
78 GNUNET_PQ_make_execute ("CREATE TABLE IF NOT EXISTS gn090 ("
79 " repl INTEGER NOT NULL DEFAULT 0,"
80 " type INTEGER NOT NULL DEFAULT 0,"
81 " prio INTEGER NOT NULL DEFAULT 0,"
82 " anonLevel INTEGER NOT NULL DEFAULT 0,"
83 " expire BIGINT NOT NULL DEFAULT 0,"
84 " rvalue BIGINT NOT NULL DEFAULT 0,"
85 " hash BYTEA NOT NULL DEFAULT '',"
86 " vhash BYTEA NOT NULL DEFAULT '',"
87 " value BYTEA NOT NULL DEFAULT '',"
88 " oid OID NOT NULL DEFAULT nextval('gn090_oid_seq'))"),
89 GNUNET_PQ_make_try_execute (
90 "ALTER SEQUENCE gn090_oid_seq OWNED BY gn090.oid"),
91 GNUNET_PQ_make_try_execute (
92 "CREATE INDEX IF NOT EXISTS oid_hash ON gn090 (oid)"),
93 GNUNET_PQ_make_try_execute (
94 "CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash)"),
95 GNUNET_PQ_make_try_execute (
96 "CREATE INDEX IF NOT EXISTS idx_prio ON gn090 (prio)"),
97 GNUNET_PQ_make_try_execute (
98 "CREATE INDEX IF NOT EXISTS idx_expire ON gn090 (expire)"),
99 GNUNET_PQ_make_try_execute (
100 "CREATE INDEX IF NOT EXISTS idx_prio_anon ON gn090 (prio,anonLevel)"),
101 GNUNET_PQ_make_try_execute (
102 "CREATE INDEX IF NOT EXISTS idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)"),
103 GNUNET_PQ_make_try_execute (
104 "CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn090 (repl,rvalue)"),
105 GNUNET_PQ_make_try_execute (
106 "CREATE INDEX IF NOT EXISTS idx_expire_hash ON gn090 (expire,hash)"),
107 GNUNET_PQ_make_execute (
108 "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL"),
109 GNUNET_PQ_make_execute ("ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN"),
110 GNUNET_PQ_make_execute ("ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN"),
111 GNUNET_PQ_EXECUTE_STATEMENT_END
112 };
113
114#define RESULT_COLUMNS "repl, type, prio, anonLevel, expire, hash, value, oid" 70#define RESULT_COLUMNS "repl, type, prio, anonLevel, expire, hash, value, oid"
115 struct GNUNET_PQ_PreparedStatement ps[] = { 71 struct GNUNET_PQ_PreparedStatement ps[] = {
116 GNUNET_PQ_make_prepare ("get", 72 GNUNET_PQ_make_prepare ("get",
117 "SELECT " RESULT_COLUMNS " FROM gn090" 73 "SELECT " RESULT_COLUMNS
74 " FROM datastore.gn090"
118 " WHERE oid >= $1::bigint AND" 75 " WHERE oid >= $1::bigint AND"
119 " (rvalue >= $2 OR 0 = $3::smallint) AND" 76 " (rvalue >= $2 OR 0 = $3::smallint) AND"
120 " (hash = $4 OR 0 = $5::smallint) AND" 77 " (hash = $4 OR 0 = $5::smallint) AND"
121 " (type = $6 OR 0 = $7::smallint)" 78 " (type = $6 OR 0 = $7::smallint)"
122 " ORDER BY oid ASC LIMIT 1"), 79 " ORDER BY oid ASC LIMIT 1"),
123 GNUNET_PQ_make_prepare ("put", 80 GNUNET_PQ_make_prepare ("put",
124 "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " 81 "INSERT INTO datastore.gn090"
82 " (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
125 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)"), 83 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)"),
126 GNUNET_PQ_make_prepare ("update", 84 GNUNET_PQ_make_prepare ("update",
127 "UPDATE gn090" 85 "UPDATE datastore.gn090"
128 " SET prio = prio + $1," 86 " SET prio = prio + $1,"
129 " repl = repl + $2," 87 " repl = repl + $2,"
130 " expire = GREATEST(expire, $3)" 88 " expire = GREATEST(expire, $3)"
131 " WHERE hash = $4 AND vhash = $5"), 89 " WHERE hash = $4 AND vhash = $5"),
132 GNUNET_PQ_make_prepare ("decrepl", 90 GNUNET_PQ_make_prepare ("decrepl",
133 "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) " 91 "UPDATE datastore.gn090"
134 "WHERE oid = $1"), 92 " SET repl = GREATEST (repl - 1, 0)"
93 " WHERE oid = $1"),
135 GNUNET_PQ_make_prepare ("select_non_anonymous", 94 GNUNET_PQ_make_prepare ("select_non_anonymous",
136 "SELECT " RESULT_COLUMNS " FROM gn090 " 95 "SELECT " RESULT_COLUMNS
137 "WHERE anonLevel = 0 AND type = $1 AND oid >= $2::bigint " 96 " FROM datastore.gn090"
138 "ORDER BY oid ASC LIMIT 1"), 97 " WHERE anonLevel = 0 AND type = $1 AND oid >= $2::bigint"
98 " ORDER BY oid ASC LIMIT 1"),
139 GNUNET_PQ_make_prepare ("select_expiration_order", 99 GNUNET_PQ_make_prepare ("select_expiration_order",
140 "(SELECT " RESULT_COLUMNS " FROM gn090 " 100 "(SELECT " RESULT_COLUMNS
141 "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " 101 " FROM datastore.gn090"
102 " WHERE expire < $1 ORDER BY prio ASC LIMIT 1) "
142 "UNION " 103 "UNION "
143 "(SELECT " RESULT_COLUMNS " FROM gn090 " 104 "(SELECT " RESULT_COLUMNS
144 "ORDER BY prio ASC LIMIT 1) " 105 " FROM datastore.gn090"
145 "ORDER BY expire ASC LIMIT 1"), 106 " ORDER BY prio ASC LIMIT 1)"
107 " ORDER BY expire ASC LIMIT 1"),
146 GNUNET_PQ_make_prepare ("select_replication_order", 108 GNUNET_PQ_make_prepare ("select_replication_order",
147 "SELECT " RESULT_COLUMNS " FROM gn090 " 109 "SELECT " RESULT_COLUMNS
148 "ORDER BY repl DESC,RANDOM() LIMIT 1"), 110 " FROM datastore.gn090"
111 " ORDER BY repl DESC,RANDOM() LIMIT 1"),
149 GNUNET_PQ_make_prepare ("delrow", 112 GNUNET_PQ_make_prepare ("delrow",
150 "DELETE FROM gn090 " 113 "DELETE FROM datastore.gn090"
151 "WHERE oid=$1"), 114 " WHERE oid=$1"),
152 GNUNET_PQ_make_prepare ("remove", 115 GNUNET_PQ_make_prepare ("remove",
153 "DELETE FROM gn090" 116 "DELETE FROM datastore.gn090"
154 " WHERE hash = $1 AND" 117 " WHERE hash = $1 AND"
155 " value = $2"), 118 " value = $2"),
156 GNUNET_PQ_make_prepare ("get_keys", 119 GNUNET_PQ_make_prepare ("get_keys",
157 "SELECT hash FROM gn090"), 120 "SELECT hash"
121 " FROM datastore.gn090"),
158 GNUNET_PQ_make_prepare ("estimate_size", 122 GNUNET_PQ_make_prepare ("estimate_size",
159 "SELECT CASE WHEN NOT EXISTS" 123 "SELECT CASE WHEN NOT EXISTS"
160 " (SELECT 1 FROM gn090)" 124 " (SELECT 1 FROM datastore.gn090)"
161 " THEN 0" 125 " THEN 0"
162 " ELSE (SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090)" 126 " ELSE (SELECT SUM(LENGTH(value))+256*COUNT(*)"
127 " FROM datastore.gn090)"
163 "END AS total"), 128 "END AS total"),
164 GNUNET_PQ_PREPARED_STATEMENT_END 129 GNUNET_PQ_PREPARED_STATEMENT_END
165 }; 130 };
@@ -167,8 +132,8 @@ init_connection (struct Plugin *plugin)
167 132
168 plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg, 133 plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg,
169 "datastore-postgres", 134 "datastore-postgres",
135 "datastore-",
170 NULL, 136 NULL,
171 es,
172 ps); 137 ps);
173 if (NULL == plugin->dbh) 138 if (NULL == plugin->dbh)
174 return GNUNET_SYSERR; 139 return GNUNET_SYSERR;
@@ -389,7 +354,7 @@ process_result (void *cls,
389 for (unsigned int i = 0; i < num_results; i++) 354 for (unsigned int i = 0; i < num_results; i++)
390 { 355 {
391 int iret; 356 int iret;
392 uint32_t rowid; 357 uint64_t rowid;
393 uint32_t utype; 358 uint32_t utype;
394 uint32_t anonymity; 359 uint32_t anonymity;
395 uint32_t replication; 360 uint32_t replication;
@@ -406,7 +371,7 @@ process_result (void *cls,
406 GNUNET_PQ_result_spec_absolute_time ("expire", &expiration_time), 371 GNUNET_PQ_result_spec_absolute_time ("expire", &expiration_time),
407 GNUNET_PQ_result_spec_auto_from_type ("hash", &key), 372 GNUNET_PQ_result_spec_auto_from_type ("hash", &key),
408 GNUNET_PQ_result_spec_variable_size ("value", &data, &size), 373 GNUNET_PQ_result_spec_variable_size ("value", &data, &size),
409 GNUNET_PQ_result_spec_uint32 ("oid", &rowid), 374 GNUNET_PQ_result_spec_uint64 ("oid", &rowid),
410 GNUNET_PQ_result_spec_end 375 GNUNET_PQ_result_spec_end
411 }; 376 };
412 377
@@ -439,7 +404,7 @@ process_result (void *cls,
439 if (iret == GNUNET_NO) 404 if (iret == GNUNET_NO)
440 { 405 {
441 struct GNUNET_PQ_QueryParam param[] = { 406 struct GNUNET_PQ_QueryParam param[] = {
442 GNUNET_PQ_query_param_uint32 (&rowid), 407 GNUNET_PQ_query_param_uint64 (&rowid),
443 GNUNET_PQ_query_param_end 408 GNUNET_PQ_query_param_end
444 }; 409 };
445 410
@@ -635,9 +600,8 @@ repl_proc (void *cls,
635 struct ReplCtx *rc = cls; 600 struct ReplCtx *rc = cls;
636 struct Plugin *plugin = rc->plugin; 601 struct Plugin *plugin = rc->plugin;
637 int ret; 602 int ret;
638 uint32_t oid = (uint32_t) uid;
639 struct GNUNET_PQ_QueryParam params[] = { 603 struct GNUNET_PQ_QueryParam params[] = {
640 GNUNET_PQ_query_param_uint32 (&oid), 604 GNUNET_PQ_query_param_uint64 (&uid),
641 GNUNET_PQ_query_param_end 605 GNUNET_PQ_query_param_end
642 }; 606 };
643 enum GNUNET_DB_QueryStatus qret; 607 enum GNUNET_DB_QueryStatus qret;
@@ -940,9 +904,6 @@ libgnunet_plugin_datastore_postgres_init (void *cls)
940 api->get_keys = &postgres_plugin_get_keys; 904 api->get_keys = &postgres_plugin_get_keys;
941 api->drop = &postgres_plugin_drop; 905 api->drop = &postgres_plugin_drop;
942 api->remove_key = &postgres_plugin_remove_key; 906 api->remove_key = &postgres_plugin_remove_key;
943 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
944 "datastore-postgres",
945 _ ("Postgres database running\n"));
946 return api; 907 return api;
947} 908}
948 909
diff --git a/src/include/gnunet_datastore_plugin.h b/src/include/gnunet_datastore_plugin.h
index 3de08b788..4d1a7ff67 100644
--- a/src/include/gnunet_datastore_plugin.h
+++ b/src/include/gnunet_datastore_plugin.h
@@ -101,17 +101,17 @@ struct GNUNET_DATASTORE_PluginEnvironment
101 * @return #GNUNET_OK to keep the item 101 * @return #GNUNET_OK to keep the item
102 * #GNUNET_NO to delete the item 102 * #GNUNET_NO to delete the item
103 */ 103 */
104typedef int 104typedef enum GNUNET_GenericReturnValue
105(*PluginDatumProcessor) (void *cls, 105(*PluginDatumProcessor)(void *cls,
106 const struct GNUNET_HashCode *key, 106 const struct GNUNET_HashCode *key,
107 uint32_t size, 107 uint32_t size,
108 const void *data, 108 const void *data,
109 enum GNUNET_BLOCK_Type type, 109 enum GNUNET_BLOCK_Type type,
110 uint32_t priority, 110 uint32_t priority,
111 uint32_t anonymity, 111 uint32_t anonymity,
112 uint32_t replication, 112 uint32_t replication,
113 struct GNUNET_TIME_Absolute expiration, 113 struct GNUNET_TIME_Absolute expiration,
114 uint64_t uid); 114 uint64_t uid);
115 115
116 116
117/** 117/**
diff --git a/src/namecache/namecache.conf.in b/src/namecache/namecache.conf.in
index cf1340846..6bf75454b 100644
--- a/src/namecache/namecache.conf.in
+++ b/src/namecache/namecache.conf.in
@@ -24,7 +24,6 @@ FILENAME = $GNUNET_DATA_HOME/namecache/flat.db
24 24
25[namecache-postgres] 25[namecache-postgres]
26CONFIG = postgres:///gnunet 26CONFIG = postgres:///gnunet
27TEMPORARY_TABLE = NO 27SQL_DIR = ${DATADIR}/sql/
28
29 28
30 29
diff --git a/src/namestore/namestore.conf.in b/src/namestore/namestore.conf.in
index 697d07612..d817f3f95 100644
--- a/src/namestore/namestore.conf.in
+++ b/src/namestore/namestore.conf.in
@@ -27,8 +27,6 @@ FILENAME = $GNUNET_DATA_HOME/namestore/sqlite.db
27[namestore-postgres] 27[namestore-postgres]
28# How to connect to the database 28# How to connect to the database
29CONFIG = postgres:///gnunet 29CONFIG = postgres:///gnunet
30# Use temporary tables
31TEMPORARY_TABLE = NO
32# Use asynchronous commit (SET synchronous_commit TO OFF). 30# Use asynchronous commit (SET synchronous_commit TO OFF).
33ASYNC_COMMIT = NO 31ASYNC_COMMIT = NO
34INIT_ON_CONNECT = YES 32INIT_ON_CONNECT = YES