diff options
-rw-r--r-- | src/datacache/Makefile.am | 10 | ||||
-rw-r--r-- | src/datacache/datacache-0001.sql | 48 | ||||
-rw-r--r-- | src/datacache/datacache-drop.sql | 25 | ||||
-rw-r--r-- | src/datacache/datacache.conf | 4 | ||||
-rw-r--r-- | src/datacache/plugin_datacache_postgres.c | 85 | ||||
-rw-r--r-- | src/datastore/Makefile.am | 9 | ||||
-rw-r--r-- | src/datastore/datastore-0001.sql | 49 | ||||
-rw-r--r-- | src/datastore/datastore-drop.sql | 25 | ||||
-rw-r--r-- | src/datastore/datastore.conf.in | 1 | ||||
-rw-r--r-- | src/datastore/plugin_datastore_postgres.c | 113 | ||||
-rw-r--r-- | src/include/gnunet_datastore_plugin.h | 22 | ||||
-rw-r--r-- | src/namecache/Makefile.am | 10 | ||||
-rw-r--r-- | src/namecache/namecache-0001.sql | 42 | ||||
-rw-r--r-- | src/namecache/namecache-drop.sql | 25 | ||||
-rw-r--r-- | src/namecache/namecache.conf.in | 3 | ||||
-rw-r--r-- | src/namecache/plugin_namecache_postgres.c | 85 | ||||
-rw-r--r-- | src/namestore/Makefile.am | 5 | ||||
-rw-r--r-- | src/namestore/namestore.conf.in | 2 |
18 files changed, 362 insertions, 201 deletions
diff --git a/src/datacache/Makefile.am b/src/datacache/Makefile.am index 4789706ff..14778e0a3 100644 --- a/src/datacache/Makefile.am +++ b/src/datacache/Makefile.am | |||
@@ -8,6 +8,13 @@ pkgcfgdir= $(pkgdatadir)/config.d/ | |||
8 | dist_pkgcfg_DATA = \ | 8 | dist_pkgcfg_DATA = \ |
9 | datacache.conf | 9 | datacache.conf |
10 | 10 | ||
11 | sqldir = $(prefix)/share/gnunet/sql/ | ||
12 | |||
13 | sql_DATA = \ | ||
14 | datacache-0001.sql \ | ||
15 | datacache-drop.sql | ||
16 | |||
17 | |||
11 | if USE_COVERAGE | 18 | if USE_COVERAGE |
12 | AM_CFLAGS = --coverage -O0 | 19 | AM_CFLAGS = --coverage -O0 |
13 | XLIBS = -lgcov | 20 | XLIBS = -lgcov |
@@ -195,4 +202,5 @@ EXTRA_DIST = \ | |||
195 | test_datacache_data_heap.conf \ | 202 | test_datacache_data_heap.conf \ |
196 | perf_datacache_data_heap.conf \ | 203 | perf_datacache_data_heap.conf \ |
197 | test_datacache_data_postgres.conf \ | 204 | test_datacache_data_postgres.conf \ |
198 | perf_datacache_data_postgres.conf | 205 | perf_datacache_data_postgres.conf \ |
206 | $(sql_DATA) | ||
diff --git a/src/datacache/datacache-0001.sql b/src/datacache/datacache-0001.sql new file mode 100644 index 000000000..6567de3c2 --- /dev/null +++ b/src/datacache/datacache-0001.sql | |||
@@ -0,0 +1,48 @@ | |||
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 | ||
18 | BEGIN; | ||
19 | |||
20 | -- Check patch versioning is in place. | ||
21 | SELECT _v.register_patch('datacache-0001', NULL, NULL); | ||
22 | |||
23 | -------------------- Schema ---------------------------- | ||
24 | |||
25 | CREATE SCHEMA datacache; | ||
26 | COMMENT ON SCHEMA datacache IS 'gnunet-datacache data'; | ||
27 | |||
28 | SET search_path TO datacache; | ||
29 | |||
30 | CREATE TABLE IF NOT EXISTS gn180dc ( | ||
31 | oid BIGINT GENERATED BY DEFAULT AS IDENTITY, | ||
32 | type INT4 NOT NULL, | ||
33 | ro INT4 NOT NULL, | ||
34 | prox INT4 NOT NULL, | ||
35 | expiration_time INT8 NOT NULL, | ||
36 | key BYTEA NOT NULL CHECK(LENGTH(key)=64), | ||
37 | trunc BYTEA NOT NULL CHECK(LENGTH(trunc)=32), | ||
38 | value BYTEA NOT NULL, | ||
39 | path BYTEA DEFAULT NULL); | ||
40 | |||
41 | CREATE INDEX IF NOT EXISTS idx_oid | ||
42 | ON gn180dc (oid); | ||
43 | CREATE INDEX IF NOT EXISTS idx_key | ||
44 | ON gn180dc (key); | ||
45 | CREATE INDEX IF NOT EXISTS idx_dt | ||
46 | ON gn180dc (expiration_time); | ||
47 | |||
48 | COMMIT; | ||
diff --git a/src/datacache/datacache-drop.sql b/src/datacache/datacache-drop.sql new file mode 100644 index 000000000..2dd84bca8 --- /dev/null +++ b/src/datacache/datacache-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 | ||
18 | BEGIN; | ||
19 | |||
20 | |||
21 | SELECT _v.unregister_patch('datacache-0001'); | ||
22 | |||
23 | DROP SCHEMA datacache CASCADE; | ||
24 | |||
25 | COMMIT; | ||
diff --git a/src/datacache/datacache.conf b/src/datacache/datacache.conf index f9c718eb0..ed5c3da06 100644 --- a/src/datacache/datacache.conf +++ b/src/datacache/datacache.conf | |||
@@ -1,2 +1,6 @@ | |||
1 | [datacache-postgres] | 1 | [datacache-postgres] |
2 | CONFIG = postgres:///gnunet | 2 | CONFIG = postgres:///gnunet |
3 | |||
4 | # Where are the SQL files to setup our tables? | ||
5 | # Important: this MUST end with a "/"! | ||
6 | SQL_DIR = $DATADIR/sql/ \ No newline at end of file | ||
diff --git a/src/datacache/plugin_datacache_postgres.c b/src/datacache/plugin_datacache_postgres.c index df5fc4454..8bfd04aea 100644 --- a/src/datacache/plugin_datacache_postgres.c +++ b/src/datacache/plugin_datacache_postgres.c | |||
@@ -66,64 +66,45 @@ struct Plugin | |||
66 | static enum GNUNET_GenericReturnValue | 66 | static enum GNUNET_GenericReturnValue |
67 | init_connection (struct Plugin *plugin) | 67 | init_connection (struct Plugin *plugin) |
68 | { | 68 | { |
69 | struct GNUNET_PQ_ExecuteStatement es[] = { | ||
70 | GNUNET_PQ_make_try_execute ( | ||
71 | "CREATE TEMPORARY SEQUENCE IF NOT EXISTS gn180dc_oid_seq"), | ||
72 | GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS gn180dc (" | ||
73 | " oid OID NOT NULL DEFAULT nextval('gn180dc_oid_seq')," | ||
74 | " type INT4 NOT NULL," | ||
75 | " ro INT4 NOT NULL," | ||
76 | " prox INT4 NOT NULL," | ||
77 | " expiration_time INT8 NOT NULL," | ||
78 | " key BYTEA NOT NULL CHECK(LENGTH(key)=64)," | ||
79 | " trunc BYTEA NOT NULL CHECK(LENGTH(trunc)=32)," | ||
80 | " value BYTEA NOT NULL," | ||
81 | " path BYTEA DEFAULT NULL)"), | ||
82 | GNUNET_PQ_make_try_execute ( | ||
83 | "ALTER SEQUENCE gnu011dc_oid_seq OWNED BY gn180dc.oid"), | ||
84 | GNUNET_PQ_make_try_execute ( | ||
85 | "CREATE INDEX IF NOT EXISTS idx_oid ON gn180dc (oid)"), | ||
86 | GNUNET_PQ_make_try_execute ( | ||
87 | "CREATE INDEX IF NOT EXISTS idx_key ON gn180dc (key)"), | ||
88 | GNUNET_PQ_make_try_execute ( | ||
89 | "CREATE INDEX IF NOT EXISTS idx_dt ON gn180dc (expiration_time)"), | ||
90 | GNUNET_PQ_make_execute ( | ||
91 | "ALTER TABLE gn180dc ALTER value SET STORAGE EXTERNAL"), | ||
92 | GNUNET_PQ_make_execute ("ALTER TABLE gn180dc ALTER key SET STORAGE PLAIN"), | ||
93 | GNUNET_PQ_EXECUTE_STATEMENT_END | ||
94 | }; | ||
95 | struct GNUNET_PQ_PreparedStatement ps[] = { | 69 | struct GNUNET_PQ_PreparedStatement ps[] = { |
96 | GNUNET_PQ_make_prepare ("getkt", | 70 | GNUNET_PQ_make_prepare ("getkt", |
97 | "SELECT expiration_time,type,ro,value,trunc,path FROM gn180dc " | 71 | "SELECT expiration_time,type,ro,value,trunc,path" |
98 | "WHERE key=$1 AND type=$2 AND expiration_time >= $3"), | 72 | " FROM datacache.gn180dc" |
73 | " WHERE key=$1 AND type=$2 AND expiration_time >= $3"), | ||
99 | GNUNET_PQ_make_prepare ("getk", | 74 | GNUNET_PQ_make_prepare ("getk", |
100 | "SELECT expiration_time,type,ro,value,trunc,path FROM gn180dc " | 75 | "SELECT expiration_time,type,ro,value,trunc,path" |
101 | "WHERE key=$1 AND expiration_time >= $2"), | 76 | " FROM datacache.gn180dc" |
77 | " WHERE key=$1 AND expiration_time >= $2"), | ||
102 | GNUNET_PQ_make_prepare ("getex", | 78 | GNUNET_PQ_make_prepare ("getex", |
103 | "SELECT LENGTH(value) AS len,oid,key FROM gn180dc" | 79 | "SELECT LENGTH(value) AS len,oid,key" |
80 | " FROM datacache.gn180dc" | ||
104 | " WHERE expiration_time < $1" | 81 | " WHERE expiration_time < $1" |
105 | " ORDER BY expiration_time ASC LIMIT 1"), | 82 | " ORDER BY expiration_time ASC LIMIT 1"), |
106 | GNUNET_PQ_make_prepare ("getm", | 83 | GNUNET_PQ_make_prepare ("getm", |
107 | "SELECT LENGTH(value) AS len,oid,key FROM gn180dc" | 84 | "SELECT LENGTH(value) AS len,oid,key" |
85 | " FROM datacache.gn180dc" | ||
108 | " ORDER BY prox ASC, expiration_time ASC LIMIT 1"), | 86 | " ORDER BY prox ASC, expiration_time ASC LIMIT 1"), |
109 | GNUNET_PQ_make_prepare ("get_closest", | 87 | GNUNET_PQ_make_prepare ("get_closest", |
110 | "(SELECT expiration_time,type,ro,value,trunc,path,key FROM gn180dc" | 88 | "(SELECT expiration_time,type,ro,value,trunc,path,key" |
89 | " FROM datacache.gn180dc" | ||
111 | " WHERE key >= $1" | 90 | " WHERE key >= $1" |
112 | " AND expiration_time >= $2" | 91 | " AND expiration_time >= $2" |
113 | " AND ( (type = $3) OR ( 0 = $3) )" | 92 | " AND ( (type = $3) OR ( 0 = $3) )" |
114 | " ORDER BY key ASC" | 93 | " ORDER BY key ASC" |
115 | " LIMIT $4)" | 94 | " LIMIT $4)" |
116 | " UNION " | 95 | " UNION " |
117 | "(SELECT expiration_time,type,ro,value,trunc,path,key FROM gn180dc" | 96 | "(SELECT expiration_time,type,ro,value,trunc,path,key" |
97 | " FROM datacache.gn180dc" | ||
118 | " WHERE key <= $1" | 98 | " WHERE key <= $1" |
119 | " AND expiration_time >= $2" | 99 | " AND expiration_time >= $2" |
120 | " AND ( (type = $3) OR ( 0 = $3) )" | 100 | " AND ( (type = $3) OR ( 0 = $3) )" |
121 | " ORDER BY key DESC" | 101 | " ORDER BY key DESC" |
122 | " LIMIT $4)"), | 102 | " LIMIT $4)"), |
123 | GNUNET_PQ_make_prepare ("delrow", | 103 | GNUNET_PQ_make_prepare ("delrow", |
124 | "DELETE FROM gn180dc WHERE oid=$1"), | 104 | "DELETE FROM datacache.gn180dc" |
105 | " WHERE oid=$1"), | ||
125 | GNUNET_PQ_make_prepare ("put", | 106 | GNUNET_PQ_make_prepare ("put", |
126 | "INSERT INTO gn180dc" | 107 | "INSERT INTO datacache.gn180dc" |
127 | " (type, ro, prox, expiration_time, key, value, trunc, path) " | 108 | " (type, ro, prox, expiration_time, key, value, trunc, path) " |
128 | "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"), | 109 | "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"), |
129 | GNUNET_PQ_PREPARED_STATEMENT_END | 110 | GNUNET_PQ_PREPARED_STATEMENT_END |
@@ -131,8 +112,8 @@ init_connection (struct Plugin *plugin) | |||
131 | 112 | ||
132 | plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg, | 113 | plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg, |
133 | "datacache-postgres", | 114 | "datacache-postgres", |
115 | "datacache-", | ||
134 | NULL, | 116 | NULL, |
135 | es, | ||
136 | ps); | 117 | ps); |
137 | if (NULL == plugin->dbh) | 118 | if (NULL == plugin->dbh) |
138 | return GNUNET_SYSERR; | 119 | return GNUNET_SYSERR; |
@@ -165,9 +146,12 @@ postgres_plugin_put (void *cls, | |||
165 | GNUNET_PQ_query_param_fixed_size (block->data, | 146 | GNUNET_PQ_query_param_fixed_size (block->data, |
166 | block->data_size), | 147 | block->data_size), |
167 | GNUNET_PQ_query_param_auto_from_type (&block->trunc_peer), | 148 | GNUNET_PQ_query_param_auto_from_type (&block->trunc_peer), |
168 | GNUNET_PQ_query_param_fixed_size (block->put_path, | 149 | (0 == block->put_path_length) |
169 | block->put_path_length | 150 | ? GNUNET_PQ_query_param_null () |
170 | * sizeof(struct GNUNET_DHT_PathElement)), | 151 | : GNUNET_PQ_query_param_fixed_size ( |
152 | block->put_path, | ||
153 | block->put_path_length | ||
154 | * sizeof(struct GNUNET_DHT_PathElement)), | ||
171 | GNUNET_PQ_query_param_end | 155 | GNUNET_PQ_query_param_end |
172 | }; | 156 | }; |
173 | enum GNUNET_DB_QueryStatus ret; | 157 | enum GNUNET_DB_QueryStatus ret; |
@@ -226,8 +210,8 @@ handle_results (void *cls, | |||
226 | uint32_t bro32; | 210 | uint32_t bro32; |
227 | void *data; | 211 | void *data; |
228 | struct GNUNET_DATACACHE_Block block; | 212 | struct GNUNET_DATACACHE_Block block; |
229 | void *path; | 213 | void *path = NULL; |
230 | size_t path_size; | 214 | size_t path_size = 0; |
231 | struct GNUNET_PQ_ResultSpec rs[] = { | 215 | struct GNUNET_PQ_ResultSpec rs[] = { |
232 | GNUNET_PQ_result_spec_absolute_time ("expiration_time", | 216 | GNUNET_PQ_result_spec_absolute_time ("expiration_time", |
233 | &block.expiration_time), | 217 | &block.expiration_time), |
@@ -240,9 +224,11 @@ handle_results (void *cls, | |||
240 | &block.data_size), | 224 | &block.data_size), |
241 | GNUNET_PQ_result_spec_auto_from_type ("trunc", | 225 | GNUNET_PQ_result_spec_auto_from_type ("trunc", |
242 | &block.trunc_peer), | 226 | &block.trunc_peer), |
243 | GNUNET_PQ_result_spec_variable_size ("path", | 227 | GNUNET_PQ_result_spec_allow_null ( |
244 | &path, | 228 | GNUNET_PQ_result_spec_variable_size ("path", |
245 | &path_size), | 229 | &path, |
230 | &path_size), | ||
231 | NULL), | ||
246 | GNUNET_PQ_result_spec_end | 232 | GNUNET_PQ_result_spec_end |
247 | }; | 233 | }; |
248 | 234 | ||
@@ -351,12 +337,12 @@ postgres_plugin_del (void *cls) | |||
351 | GNUNET_PQ_query_param_end | 337 | GNUNET_PQ_query_param_end |
352 | }; | 338 | }; |
353 | uint32_t size; | 339 | uint32_t size; |
354 | uint32_t oid; | 340 | uint64_t oid; |
355 | struct GNUNET_HashCode key; | 341 | struct GNUNET_HashCode key; |
356 | struct GNUNET_PQ_ResultSpec rs[] = { | 342 | struct GNUNET_PQ_ResultSpec rs[] = { |
357 | GNUNET_PQ_result_spec_uint32 ("len", | 343 | GNUNET_PQ_result_spec_uint32 ("len", |
358 | &size), | 344 | &size), |
359 | GNUNET_PQ_result_spec_uint32 ("oid", | 345 | GNUNET_PQ_result_spec_uint64 ("oid", |
360 | &oid), | 346 | &oid), |
361 | GNUNET_PQ_result_spec_auto_from_type ("key", | 347 | GNUNET_PQ_result_spec_auto_from_type ("key", |
362 | &key), | 348 | &key), |
@@ -364,7 +350,7 @@ postgres_plugin_del (void *cls) | |||
364 | }; | 350 | }; |
365 | enum GNUNET_DB_QueryStatus res; | 351 | enum GNUNET_DB_QueryStatus res; |
366 | struct GNUNET_PQ_QueryParam dparam[] = { | 352 | struct GNUNET_PQ_QueryParam dparam[] = { |
367 | GNUNET_PQ_query_param_uint32 (&oid), | 353 | GNUNET_PQ_query_param_uint64 (&oid), |
368 | GNUNET_PQ_query_param_end | 354 | GNUNET_PQ_query_param_end |
369 | }; | 355 | }; |
370 | struct GNUNET_TIME_Absolute now; | 356 | struct GNUNET_TIME_Absolute now; |
@@ -617,6 +603,9 @@ libgnunet_plugin_datacache_postgres_done (void *cls) | |||
617 | struct GNUNET_DATACACHE_PluginFunctions *api = cls; | 603 | struct GNUNET_DATACACHE_PluginFunctions *api = cls; |
618 | struct Plugin *plugin = api->cls; | 604 | struct Plugin *plugin = api->cls; |
619 | 605 | ||
606 | GNUNET_break (GNUNET_OK == | ||
607 | GNUNET_PQ_exec_sql (plugin->dbh, | ||
608 | "datacache-drop")); | ||
620 | GNUNET_PQ_disconnect (plugin->dbh); | 609 | GNUNET_PQ_disconnect (plugin->dbh); |
621 | GNUNET_free (plugin); | 610 | GNUNET_free (plugin); |
622 | GNUNET_free (api); | 611 | GNUNET_free (api); |
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/ | |||
10 | pkgcfg_DATA = \ | 10 | pkgcfg_DATA = \ |
11 | datastore.conf | 11 | datastore.conf |
12 | 12 | ||
13 | sqldir = $(prefix)/share/gnunet/sql/ | ||
14 | |||
15 | sql_DATA = \ | ||
16 | datastore-0001.sql \ | ||
17 | datastore-drop.sql | ||
18 | |||
13 | if USE_COVERAGE | 19 | if 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 | ||
18 | BEGIN; | ||
19 | |||
20 | -- Check patch versioning is in place. | ||
21 | SELECT _v.register_patch('datastore-0001', NULL, NULL); | ||
22 | |||
23 | -------------------- Schema ---------------------------- | ||
24 | |||
25 | CREATE SCHEMA datastore; | ||
26 | COMMENT ON SCHEMA datastore IS 'gnunet-datastore data'; | ||
27 | |||
28 | SET search_path TO datastore; | ||
29 | |||
30 | CREATE 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 | |||
42 | CREATE INDEX IF NOT EXISTS oid_hash ON gn090 (oid); | ||
43 | CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash); | ||
44 | CREATE INDEX IF NOT EXISTS idx_prio_anon ON gn090 (prio,anonLevel); | ||
45 | CREATE INDEX IF NOT EXISTS idx_prio_hash_anon ON gn090 (prio,hash,anonLevel); | ||
46 | CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn090 (repl,rvalue); | ||
47 | CREATE INDEX IF NOT EXISTS idx_expire_hash ON gn090 (expire,hash); | ||
48 | |||
49 | COMMIT; | ||
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 | ||
18 | BEGIN; | ||
19 | |||
20 | |||
21 | SELECT _v.unregister_patch('datastore-0001'); | ||
22 | |||
23 | DROP SCHEMA datastore CASCADE; | ||
24 | |||
25 | COMMIT; | ||
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] |
20 | CONFIG = postgres:///gnunet | 20 | CONFIG = postgres:///gnunet |
21 | SQL_DIR = ${DATADIR}/sql/ | ||
21 | 22 | ||
22 | [datastore-mysql] | 23 | [datastore-mysql] |
23 | DATABASE = gnunet | 24 | DATABASE = 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 | */ |
67 | static int | 67 | static enum GNUNET_GenericReturnValue |
68 | init_connection (struct Plugin *plugin) | 68 | init_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 | */ |
104 | typedef int | 104 | typedef 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/Makefile.am b/src/namecache/Makefile.am index 272f16b77..f283d3f64 100644 --- a/src/namecache/Makefile.am +++ b/src/namecache/Makefile.am | |||
@@ -10,6 +10,13 @@ libexecdir= $(pkglibdir)/libexec/ | |||
10 | pkgcfg_DATA = \ | 10 | pkgcfg_DATA = \ |
11 | namecache.conf | 11 | namecache.conf |
12 | 12 | ||
13 | sqldir = $(prefix)/share/gnunet/sql/ | ||
14 | |||
15 | sql_DATA = \ | ||
16 | namecache-0001.sql \ | ||
17 | namecache-drop.sql | ||
18 | |||
19 | |||
13 | if USE_COVERAGE | 20 | if USE_COVERAGE |
14 | AM_CFLAGS = --coverage -O0 | 21 | AM_CFLAGS = --coverage -O0 |
15 | XLIBS = -lgcov | 22 | XLIBS = -lgcov |
@@ -168,4 +175,5 @@ EXTRA_DIST = \ | |||
168 | test_namecache_api.conf \ | 175 | test_namecache_api.conf \ |
169 | test_plugin_namecache_sqlite.conf \ | 176 | test_plugin_namecache_sqlite.conf \ |
170 | test_plugin_namecache_postgres.conf \ | 177 | test_plugin_namecache_postgres.conf \ |
171 | test_plugin_namecache_flat.conf | 178 | test_plugin_namecache_flat.conf \ |
179 | $(sql_DATA) | ||
diff --git a/src/namecache/namecache-0001.sql b/src/namecache/namecache-0001.sql new file mode 100644 index 000000000..8509b078f --- /dev/null +++ b/src/namecache/namecache-0001.sql | |||
@@ -0,0 +1,42 @@ | |||
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 | ||
18 | BEGIN; | ||
19 | |||
20 | -- Check patch versioning is in place. | ||
21 | SELECT _v.register_patch('namecache-0001', NULL, NULL); | ||
22 | |||
23 | -------------------- Schema ---------------------------- | ||
24 | |||
25 | CREATE SCHEMA datacache; | ||
26 | COMMENT ON SCHEMA datacache IS 'gnunet-datacache data'; | ||
27 | |||
28 | SET search_path TO datacache; | ||
29 | |||
30 | CREATE TABLE IF NOT EXISTS ns096blocks ( | ||
31 | query BYTEA NOT NULL DEFAULT '', | ||
32 | block BYTEA NOT NULL DEFAULT '', | ||
33 | expiration_time BIGINT NOT NULL DEFAULT 0); | ||
34 | |||
35 | CREATE INDEX ir_query_hash | ||
36 | ON ns096blocks (query,expiration_time); | ||
37 | |||
38 | CREATE INDEX ir_block_expiration | ||
39 | ON ns096blocks (expiration_time); | ||
40 | |||
41 | |||
42 | COMMIT; | ||
diff --git a/src/namecache/namecache-drop.sql b/src/namecache/namecache-drop.sql new file mode 100644 index 000000000..197ee78c1 --- /dev/null +++ b/src/namecache/namecache-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 | ||
18 | BEGIN; | ||
19 | |||
20 | |||
21 | SELECT _v.unregister_patch('namecache-0001'); | ||
22 | |||
23 | DROP SCHEMA namecache CASCADE; | ||
24 | |||
25 | COMMIT; | ||
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] |
26 | CONFIG = postgres:///gnunet | 26 | CONFIG = postgres:///gnunet |
27 | TEMPORARY_TABLE = NO | 27 | SQL_DIR = ${DATADIR}/sql/ |
28 | |||
29 | 28 | ||
30 | 29 | ||
diff --git a/src/namecache/plugin_namecache_postgres.c b/src/namecache/plugin_namecache_postgres.c index d6d1730ce..cdbe248b6 100644 --- a/src/namecache/plugin_namecache_postgres.c +++ b/src/namecache/plugin_namecache_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-2013, 2016, 2017 GNUnet e.V. | 3 | * Copyright (C) 2009-2013, 2016, 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 |
@@ -56,63 +56,34 @@ struct Plugin | |||
56 | * @param plugin the plugin context (state for this module) | 56 | * @param plugin the plugin context (state for this module) |
57 | * @return #GNUNET_OK on success | 57 | * @return #GNUNET_OK on success |
58 | */ | 58 | */ |
59 | static int | 59 | static enum GNUNET_GenericReturnValue |
60 | database_setup (struct Plugin *plugin) | 60 | database_setup (struct Plugin *plugin) |
61 | { | 61 | { |
62 | struct GNUNET_PQ_ExecuteStatement es_temporary = | 62 | struct GNUNET_PQ_PreparedStatement ps[] = { |
63 | GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS ns096blocks (" | 63 | GNUNET_PQ_make_prepare ("cache_block", |
64 | " query BYTEA NOT NULL DEFAULT ''," | 64 | "INSERT INTO namecache.ns096blocks" |
65 | " block BYTEA NOT NULL DEFAULT ''," | 65 | " (query, block, expiration_time)" |
66 | " expiration_time BIGINT NOT NULL DEFAULT 0" | 66 | " VALUES" |
67 | ")"); | 67 | " ($1, $2, $3)"), |
68 | struct GNUNET_PQ_ExecuteStatement es_default = | 68 | GNUNET_PQ_make_prepare ("expire_blocks", |
69 | GNUNET_PQ_make_execute ("CREATE TABLE IF NOT EXISTS ns096blocks (" | 69 | "DELETE FROM namecache.ns096blocks" |
70 | " query BYTEA NOT NULL DEFAULT ''," | 70 | " WHERE expiration_time<$1"), |
71 | " block BYTEA NOT NULL DEFAULT ''," | 71 | GNUNET_PQ_make_prepare ("delete_block", |
72 | " expiration_time BIGINT NOT NULL DEFAULT 0" | 72 | "DELETE FROM namecache.ns096blocks" |
73 | ")"); | 73 | " WHERE query=$1 AND expiration_time<=$2"), |
74 | const struct GNUNET_PQ_ExecuteStatement *cr; | 74 | GNUNET_PQ_make_prepare ("lookup_block", |
75 | 75 | "SELECT block" | |
76 | if (GNUNET_YES == | 76 | " FROM namecache.ns096blocks" |
77 | GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg, | 77 | " WHERE query=$1" |
78 | " ORDER BY expiration_time DESC LIMIT 1"), | ||
79 | GNUNET_PQ_PREPARED_STATEMENT_END | ||
80 | }; | ||
81 | |||
82 | plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg, | ||
78 | "namecache-postgres", | 83 | "namecache-postgres", |
79 | "TEMPORARY_TABLE")) | 84 | "namecache-", |
80 | { | 85 | NULL, |
81 | cr = &es_temporary; | 86 | ps); |
82 | } | ||
83 | else | ||
84 | { | ||
85 | cr = &es_default; | ||
86 | } | ||
87 | { | ||
88 | struct GNUNET_PQ_ExecuteStatement es[] = { | ||
89 | *cr, | ||
90 | GNUNET_PQ_make_try_execute ( | ||
91 | "CREATE INDEX ir_query_hash ON ns096blocks (query,expiration_time)"), | ||
92 | GNUNET_PQ_make_try_execute ( | ||
93 | "CREATE INDEX ir_block_expiration ON ns096blocks (expiration_time)"), | ||
94 | GNUNET_PQ_EXECUTE_STATEMENT_END | ||
95 | }; | ||
96 | struct GNUNET_PQ_PreparedStatement ps[] = { | ||
97 | GNUNET_PQ_make_prepare ("cache_block", | ||
98 | "INSERT INTO ns096blocks (query, block, expiration_time) VALUES " | ||
99 | "($1, $2, $3)"), | ||
100 | GNUNET_PQ_make_prepare ("expire_blocks", | ||
101 | "DELETE FROM ns096blocks WHERE expiration_time<$1"), | ||
102 | GNUNET_PQ_make_prepare ("delete_block", | ||
103 | "DELETE FROM ns096blocks WHERE query=$1 AND expiration_time<=$2"), | ||
104 | GNUNET_PQ_make_prepare ("lookup_block", | ||
105 | "SELECT block FROM ns096blocks WHERE query=$1" | ||
106 | " ORDER BY expiration_time DESC LIMIT 1"), | ||
107 | GNUNET_PQ_PREPARED_STATEMENT_END | ||
108 | }; | ||
109 | |||
110 | plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg, | ||
111 | "namecache-postgres", | ||
112 | NULL, | ||
113 | es, | ||
114 | ps); | ||
115 | } | ||
116 | if (NULL == plugin->dbh) | 87 | if (NULL == plugin->dbh) |
117 | return GNUNET_SYSERR; | 88 | return GNUNET_SYSERR; |
118 | return GNUNET_OK; | 89 | return GNUNET_OK; |
@@ -174,7 +145,7 @@ delete_old_block (struct Plugin *plugin, | |||
174 | * @param block block to cache | 145 | * @param block block to cache |
175 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR | 146 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR |
176 | */ | 147 | */ |
177 | static int | 148 | static enum GNUNET_GenericReturnValue |
178 | namecache_postgres_cache_block (void *cls, | 149 | namecache_postgres_cache_block (void *cls, |
179 | const struct GNUNET_GNSRECORD_Block *block) | 150 | const struct GNUNET_GNSRECORD_Block *block) |
180 | { | 151 | { |
@@ -222,7 +193,7 @@ namecache_postgres_cache_block (void *cls, | |||
222 | * @param iter_cls closure for @a iter | 193 | * @param iter_cls closure for @a iter |
223 | * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error | 194 | * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error |
224 | */ | 195 | */ |
225 | static int | 196 | static enum GNUNET_GenericReturnValue |
226 | namecache_postgres_lookup_block (void *cls, | 197 | namecache_postgres_lookup_block (void *cls, |
227 | const struct GNUNET_HashCode *query, | 198 | const struct GNUNET_HashCode *query, |
228 | GNUNET_NAMECACHE_BlockCallback iter, | 199 | GNUNET_NAMECACHE_BlockCallback iter, |
diff --git a/src/namestore/Makefile.am b/src/namestore/Makefile.am index b4d2b0ff2..98ee228e1 100644 --- a/src/namestore/Makefile.am +++ b/src/namestore/Makefile.am | |||
@@ -538,5 +538,6 @@ EXTRA_DIST = \ | |||
538 | test_plugin_namestore_sqlite.conf \ | 538 | test_plugin_namestore_sqlite.conf \ |
539 | test_plugin_namestore_postgres.conf \ | 539 | test_plugin_namestore_postgres.conf \ |
540 | test_hostkey \ | 540 | test_hostkey \ |
541 | example_zonefile \ | 541 | example_zonefile \ |
542 | $(check_SCRIPTS) | 542 | $(check_SCRIPTS) \ |
543 | $(sql_DATA) | ||
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 |
29 | CONFIG = postgres:///gnunet | 29 | CONFIG = postgres:///gnunet |
30 | # Use temporary tables | ||
31 | TEMPORARY_TABLE = NO | ||
32 | # Use asynchronous commit (SET synchronous_commit TO OFF). | 30 | # Use asynchronous commit (SET synchronous_commit TO OFF). |
33 | ASYNC_COMMIT = NO | 31 | ASYNC_COMMIT = NO |
34 | INIT_ON_CONNECT = YES | 32 | INIT_ON_CONNECT = YES |