aboutsummaryrefslogtreecommitdiff
path: root/src/datacache/plugin_datacache_postgres.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/datacache/plugin_datacache_postgres.c')
-rw-r--r--src/datacache/plugin_datacache_postgres.c78
1 files changed, 49 insertions, 29 deletions
diff --git a/src/datacache/plugin_datacache_postgres.c b/src/datacache/plugin_datacache_postgres.c
index c6ccfb210..6eeeb5873 100644
--- a/src/datacache/plugin_datacache_postgres.c
+++ b/src/datacache/plugin_datacache_postgres.c
@@ -1,21 +1,19 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2006, 2009, 2010, 2012, 2015, 2017 GNUnet e.V. 3 Copyright (C) 2006, 2009, 2010, 2012, 2015, 2017, 2018 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software: you can redistribute it and/or modify it
6 it under the terms of the GNU General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation; either version 3, or (at your 7 by the Free Software Foundation, either version 3 of the License,
8 option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with GNUnet; see the file COPYING. If not, write to the 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/ 17*/
20 18
21/** 19/**
@@ -67,47 +65,57 @@ static int
67init_connection (struct Plugin *plugin) 65init_connection (struct Plugin *plugin)
68{ 66{
69 struct GNUNET_PQ_ExecuteStatement es[] = { 67 struct GNUNET_PQ_ExecuteStatement es[] = {
70 GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS gn090dc (" 68 GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS gn011dc ("
71 " type INTEGER NOT NULL," 69 " type INTEGER NOT NULL,"
70 " prox INTEGER NOT NULL,"
72 " discard_time BIGINT NOT NULL," 71 " discard_time BIGINT NOT NULL,"
73 " key BYTEA NOT NULL," 72 " key BYTEA NOT NULL,"
74 " value BYTEA NOT NULL," 73 " value BYTEA NOT NULL,"
75 " path BYTEA DEFAULT NULL)" 74 " path BYTEA DEFAULT NULL)"
76 "WITH OIDS"), 75 "WITH OIDS"),
77 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_key ON gn090dc (key)"), 76 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_key ON gn011dc (key)"),
78 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_dt ON gn090dc (discard_time)"), 77 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_dt ON gn011dc (discard_time)"),
79 GNUNET_PQ_make_execute ("ALTER TABLE gn090dc ALTER value SET STORAGE EXTERNAL"), 78 GNUNET_PQ_make_execute ("ALTER TABLE gn011dc ALTER value SET STORAGE EXTERNAL"),
80 GNUNET_PQ_make_execute ("ALTER TABLE gn090dc ALTER key SET STORAGE PLAIN"), 79 GNUNET_PQ_make_execute ("ALTER TABLE gn011dc ALTER key SET STORAGE PLAIN"),
81 GNUNET_PQ_EXECUTE_STATEMENT_END 80 GNUNET_PQ_EXECUTE_STATEMENT_END
82 }; 81 };
83 struct GNUNET_PQ_PreparedStatement ps[] = { 82 struct GNUNET_PQ_PreparedStatement ps[] = {
84 GNUNET_PQ_make_prepare ("getkt", 83 GNUNET_PQ_make_prepare ("getkt",
85 "SELECT discard_time,type,value,path FROM gn090dc " 84 "SELECT discard_time,type,value,path FROM gn011dc "
86 "WHERE key=$1 AND type=$2", 85 "WHERE key=$1 AND type=$2",
87 2), 86 2),
88 GNUNET_PQ_make_prepare ("getk", 87 GNUNET_PQ_make_prepare ("getk",
89 "SELECT discard_time,type,value,path FROM gn090dc " 88 "SELECT discard_time,type,value,path FROM gn011dc "
90 "WHERE key=$1", 89 "WHERE key=$1",
91 1), 90 1),
91 GNUNET_PQ_make_prepare ("getex",
92 "SELECT length(value) AS len,oid,key FROM gn011dc"
93 " WHERE discard_time < $1"
94 " ORDER BY discard_time ASC LIMIT 1",
95 1),
92 GNUNET_PQ_make_prepare ("getm", 96 GNUNET_PQ_make_prepare ("getm",
93 "SELECT length(value) AS len,oid,key FROM gn090dc " 97 "SELECT length(value) AS len,oid,key FROM gn011dc"
98 " ORDER BY prox ASC, discard_time ASC LIMIT 1",
99 0),
100 GNUNET_PQ_make_prepare ("getp",
101 "SELECT length(value) AS len,oid,key FROM gn011dc "
94 "ORDER BY discard_time ASC LIMIT 1", 102 "ORDER BY discard_time ASC LIMIT 1",
95 0), 103 0),
96 GNUNET_PQ_make_prepare ("get_random", 104 GNUNET_PQ_make_prepare ("get_random",
97 "SELECT discard_time,type,value,path,key FROM gn090dc " 105 "SELECT discard_time,type,value,path,key FROM gn011dc "
98 "ORDER BY key ASC LIMIT 1 OFFSET $1", 106 "ORDER BY key ASC LIMIT 1 OFFSET $1",
99 1), 107 1),
100 GNUNET_PQ_make_prepare ("get_closest", 108 GNUNET_PQ_make_prepare ("get_closest",
101 "SELECT discard_time,type,value,path,key FROM gn090dc " 109 "SELECT discard_time,type,value,path,key FROM gn011dc "
102 "WHERE key>=$1 ORDER BY key ASC LIMIT $2", 110 "WHERE key>=$1 ORDER BY key ASC LIMIT $2",
103 1), 111 1),
104 GNUNET_PQ_make_prepare ("delrow", 112 GNUNET_PQ_make_prepare ("delrow",
105 "DELETE FROM gn090dc WHERE oid=$1", 113 "DELETE FROM gn011dc WHERE oid=$1",
106 1), 114 1),
107 GNUNET_PQ_make_prepare ("put", 115 GNUNET_PQ_make_prepare ("put",
108 "INSERT INTO gn090dc (type, discard_time, key, value, path) " 116 "INSERT INTO gn011dc (type, prox, discard_time, key, value, path) "
109 "VALUES ($1, $2, $3, $4, $5)", 117 "VALUES ($1, $2, $3, $4, $5, $6)",
110 5), 118 6),
111 GNUNET_PQ_PREPARED_STATEMENT_END 119 GNUNET_PQ_PREPARED_STATEMENT_END
112 }; 120 };
113 121
@@ -141,7 +149,7 @@ init_connection (struct Plugin *plugin)
141 * 149 *
142 * @param cls closure (our `struct Plugin`) 150 * @param cls closure (our `struct Plugin`)
143 * @param key key to store @a data under 151 * @param key key to store @a data under
144 * @param am_closest are we the closest peer? 152 * @param prox proximity of @a key to my PID
145 * @param data_size number of bytes in @a data 153 * @param data_size number of bytes in @a data
146 * @param data data to store 154 * @param data data to store
147 * @param type type of the value 155 * @param type type of the value
@@ -153,7 +161,7 @@ init_connection (struct Plugin *plugin)
153static ssize_t 161static ssize_t
154postgres_plugin_put (void *cls, 162postgres_plugin_put (void *cls,
155 const struct GNUNET_HashCode *key, 163 const struct GNUNET_HashCode *key,
156 int am_closest, 164 uint32_t prox,
157 size_t data_size, 165 size_t data_size,
158 const char *data, 166 const char *data,
159 enum GNUNET_BLOCK_Type type, 167 enum GNUNET_BLOCK_Type type,
@@ -165,6 +173,7 @@ postgres_plugin_put (void *cls,
165 uint32_t type32 = (uint32_t) type; 173 uint32_t type32 = (uint32_t) type;
166 struct GNUNET_PQ_QueryParam params[] = { 174 struct GNUNET_PQ_QueryParam params[] = {
167 GNUNET_PQ_query_param_uint32 (&type32), 175 GNUNET_PQ_query_param_uint32 (&type32),
176 GNUNET_PQ_query_param_uint32 (&prox),
168 GNUNET_PQ_query_param_absolute_time (&discard_time), 177 GNUNET_PQ_query_param_absolute_time (&discard_time),
169 GNUNET_PQ_query_param_auto_from_type (key), 178 GNUNET_PQ_query_param_auto_from_type (key),
170 GNUNET_PQ_query_param_fixed_size (data, data_size), 179 GNUNET_PQ_query_param_fixed_size (data, data_size),
@@ -361,11 +370,22 @@ postgres_plugin_del (void *cls)
361 GNUNET_PQ_query_param_uint32 (&oid), 370 GNUNET_PQ_query_param_uint32 (&oid),
362 GNUNET_PQ_query_param_end 371 GNUNET_PQ_query_param_end
363 }; 372 };
373 struct GNUNET_TIME_Absolute now;
374 struct GNUNET_PQ_QueryParam xparam[] = {
375 GNUNET_PQ_query_param_absolute_time (&now),
376 GNUNET_PQ_query_param_end
377 };
364 378
379 now = GNUNET_TIME_absolute_get ();
365 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh, 380 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
366 "getm", 381 "getex",
367 pempty, 382 xparam,
368 rs); 383 rs);
384 if (0 >= res)
385 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
386 "getm",
387 pempty,
388 rs);
369 if (0 > res) 389 if (0 > res)
370 return GNUNET_SYSERR; 390 return GNUNET_SYSERR;
371 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res) 391 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res)