aboutsummaryrefslogtreecommitdiff
path: root/src/namecache
diff options
context:
space:
mode:
Diffstat (limited to 'src/namecache')
-rw-r--r--src/namecache/Makefile.am10
-rw-r--r--src/namecache/namecache-0001.sql42
-rw-r--r--src/namecache/namecache-drop.sql25
-rw-r--r--src/namecache/namecache.conf.in3
-rw-r--r--src/namecache/plugin_namecache_postgres.c85
5 files changed, 105 insertions, 60 deletions
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/
10pkgcfg_DATA = \ 10pkgcfg_DATA = \
11 namecache.conf 11 namecache.conf
12 12
13sqldir = $(prefix)/share/gnunet/sql/
14
15sql_DATA = \
16 namecache-0001.sql \
17 namecache-drop.sql
18
19
13if USE_COVERAGE 20if 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
18BEGIN;
19
20-- Check patch versioning is in place.
21SELECT _v.register_patch('namecache-0001', NULL, NULL);
22
23-------------------- Schema ----------------------------
24
25CREATE SCHEMA datacache;
26COMMENT ON SCHEMA datacache IS 'gnunet-datacache data';
27
28SET search_path TO datacache;
29
30CREATE 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
35CREATE INDEX ir_query_hash
36 ON ns096blocks (query,expiration_time);
37
38CREATE INDEX ir_block_expiration
39 ON ns096blocks (expiration_time);
40
41
42COMMIT;
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
18BEGIN;
19
20
21SELECT _v.unregister_patch('namecache-0001');
22
23DROP SCHEMA namecache CASCADE;
24
25COMMIT;
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/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 */
59static int 59static enum GNUNET_GenericReturnValue
60database_setup (struct Plugin *plugin) 60database_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 */
177static int 148static enum GNUNET_GenericReturnValue
178namecache_postgres_cache_block (void *cls, 149namecache_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 */
225static int 196static enum GNUNET_GenericReturnValue
226namecache_postgres_lookup_block (void *cls, 197namecache_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,