aboutsummaryrefslogtreecommitdiff
path: root/src/datacache
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-11-22 15:15:53 +0100
committerChristian Grothoff <christian@grothoff.org>2022-11-22 15:15:53 +0100
commit7299f45d97d1dcc22d8155cfeb95939be6422bf4 (patch)
tree3be91681190f78d4b6173a250639854002266d60 /src/datacache
parent20482f34820b614b07cb41b30217166ec1928810 (diff)
downloadgnunet-7299f45d97d1dcc22d8155cfeb95939be6422bf4.tar.gz
gnunet-7299f45d97d1dcc22d8155cfeb95939be6422bf4.zip
modernize datacache postgres implementation
Diffstat (limited to 'src/datacache')
-rw-r--r--src/datacache/Makefile.am7
-rw-r--r--src/datacache/datacache-0001.sql48
-rw-r--r--src/datacache/datacache-drop.sql25
-rw-r--r--src/datacache/datacache.conf4
-rw-r--r--src/datacache/plugin_datacache_postgres.c85
5 files changed, 121 insertions, 48 deletions
diff --git a/src/datacache/Makefile.am b/src/datacache/Makefile.am
index 4789706ff..2d112b4ca 100644
--- a/src/datacache/Makefile.am
+++ b/src/datacache/Makefile.am
@@ -8,6 +8,13 @@ pkgcfgdir= $(pkgdatadir)/config.d/
8dist_pkgcfg_DATA = \ 8dist_pkgcfg_DATA = \
9 datacache.conf 9 datacache.conf
10 10
11sqldir = $(prefix)/share/gnunet/sql/
12
13sql_DATA = \
14 datacache-0001.sql \
15 datacache-drop.sql
16
17
11if USE_COVERAGE 18if USE_COVERAGE
12 AM_CFLAGS = --coverage -O0 19 AM_CFLAGS = --coverage -O0
13 XLIBS = -lgcov 20 XLIBS = -lgcov
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
18BEGIN;
19
20-- Check patch versioning is in place.
21SELECT _v.register_patch('datacache-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 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
41CREATE INDEX IF NOT EXISTS idx_oid
42 ON gn180dc (oid);
43CREATE INDEX IF NOT EXISTS idx_key
44 ON gn180dc (key);
45CREATE INDEX IF NOT EXISTS idx_dt
46 ON gn180dc (expiration_time);
47
48COMMIT;
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
18BEGIN;
19
20
21SELECT _v.unregister_patch('datacache-0001');
22
23DROP SCHEMA datacache CASCADE;
24
25COMMIT;
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]
2CONFIG = postgres:///gnunet 2CONFIG = postgres:///gnunet
3
4# Where are the SQL files to setup our tables?
5# Important: this MUST end with a "/"!
6SQL_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
66static enum GNUNET_GenericReturnValue 66static enum GNUNET_GenericReturnValue
67init_connection (struct Plugin *plugin) 67init_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);