aboutsummaryrefslogtreecommitdiff
path: root/src/datastore/plugin_datastore_postgres.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/datastore/plugin_datastore_postgres.c')
-rw-r--r--src/datastore/plugin_datastore_postgres.c113
1 files changed, 37 insertions, 76 deletions
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