aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/datacache/plugin_datacache_postgres.c60
1 files changed, 41 insertions, 19 deletions
diff --git a/src/datacache/plugin_datacache_postgres.c b/src/datacache/plugin_datacache_postgres.c
index 683c6148a..33b317e87 100644
--- a/src/datacache/plugin_datacache_postgres.c
+++ b/src/datacache/plugin_datacache_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) 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 it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published 6 under the terms of the GNU General Public License as published
@@ -62,47 +62,57 @@ static int
62init_connection (struct Plugin *plugin) 62init_connection (struct Plugin *plugin)
63{ 63{
64 struct GNUNET_PQ_ExecuteStatement es[] = { 64 struct GNUNET_PQ_ExecuteStatement es[] = {
65 GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS gn090dc (" 65 GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS gn011dc ("
66 " type INTEGER NOT NULL," 66 " type INTEGER NOT NULL,"
67 " prox INTEGER NOT NULL,"
67 " discard_time BIGINT NOT NULL," 68 " discard_time BIGINT NOT NULL,"
68 " key BYTEA NOT NULL," 69 " key BYTEA NOT NULL,"
69 " value BYTEA NOT NULL," 70 " value BYTEA NOT NULL,"
70 " path BYTEA DEFAULT NULL)" 71 " path BYTEA DEFAULT NULL)"
71 "WITH OIDS"), 72 "WITH OIDS"),
72 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_key ON gn090dc (key)"), 73 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_key ON gn011dc (key)"),
73 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_dt ON gn090dc (discard_time)"), 74 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_dt ON gn011dc (discard_time)"),
74 GNUNET_PQ_make_execute ("ALTER TABLE gn090dc ALTER value SET STORAGE EXTERNAL"), 75 GNUNET_PQ_make_execute ("ALTER TABLE gn011dc ALTER value SET STORAGE EXTERNAL"),
75 GNUNET_PQ_make_execute ("ALTER TABLE gn090dc ALTER key SET STORAGE PLAIN"), 76 GNUNET_PQ_make_execute ("ALTER TABLE gn011dc ALTER key SET STORAGE PLAIN"),
76 GNUNET_PQ_EXECUTE_STATEMENT_END 77 GNUNET_PQ_EXECUTE_STATEMENT_END
77 }; 78 };
78 struct GNUNET_PQ_PreparedStatement ps[] = { 79 struct GNUNET_PQ_PreparedStatement ps[] = {
79 GNUNET_PQ_make_prepare ("getkt", 80 GNUNET_PQ_make_prepare ("getkt",
80 "SELECT discard_time,type,value,path FROM gn090dc " 81 "SELECT discard_time,type,value,path FROM gn011dc "
81 "WHERE key=$1 AND type=$2", 82 "WHERE key=$1 AND type=$2",
82 2), 83 2),
83 GNUNET_PQ_make_prepare ("getk", 84 GNUNET_PQ_make_prepare ("getk",
84 "SELECT discard_time,type,value,path FROM gn090dc " 85 "SELECT discard_time,type,value,path FROM gn011dc "
85 "WHERE key=$1", 86 "WHERE key=$1",
86 1), 87 1),
88 GNUNET_PQ_make_prepare ("getex",
89 "SELECT length(value) AS len,oid,key FROM gn011dc"
90 " WHERE discard_time < $1"
91 " ORDER BY discard_time ASC LIMIT 1",
92 1),
87 GNUNET_PQ_make_prepare ("getm", 93 GNUNET_PQ_make_prepare ("getm",
88 "SELECT length(value) AS len,oid,key FROM gn090dc " 94 "SELECT length(value) AS len,oid,key FROM gn011dc"
95 " ORDER BY prox ASC, discard_time ASC LIMIT 1",
96 0),
97 GNUNET_PQ_make_prepare ("getp",
98 "SELECT length(value) AS len,oid,key FROM gn011dc "
89 "ORDER BY discard_time ASC LIMIT 1", 99 "ORDER BY discard_time ASC LIMIT 1",
90 0), 100 0),
91 GNUNET_PQ_make_prepare ("get_random", 101 GNUNET_PQ_make_prepare ("get_random",
92 "SELECT discard_time,type,value,path,key FROM gn090dc " 102 "SELECT discard_time,type,value,path,key FROM gn011dc "
93 "ORDER BY key ASC LIMIT 1 OFFSET $1", 103 "ORDER BY key ASC LIMIT 1 OFFSET $1",
94 1), 104 1),
95 GNUNET_PQ_make_prepare ("get_closest", 105 GNUNET_PQ_make_prepare ("get_closest",
96 "SELECT discard_time,type,value,path,key FROM gn090dc " 106 "SELECT discard_time,type,value,path,key FROM gn011dc "
97 "WHERE key>=$1 ORDER BY key ASC LIMIT $2", 107 "WHERE key>=$1 ORDER BY key ASC LIMIT $2",
98 1), 108 1),
99 GNUNET_PQ_make_prepare ("delrow", 109 GNUNET_PQ_make_prepare ("delrow",
100 "DELETE FROM gn090dc WHERE oid=$1", 110 "DELETE FROM gn011dc WHERE oid=$1",
101 1), 111 1),
102 GNUNET_PQ_make_prepare ("put", 112 GNUNET_PQ_make_prepare ("put",
103 "INSERT INTO gn090dc (type, discard_time, key, value, path) " 113 "INSERT INTO gn011dc (type, prox, discard_time, key, value, path) "
104 "VALUES ($1, $2, $3, $4, $5)", 114 "VALUES ($1, $2, $3, $4, $5, $6)",
105 5), 115 6),
106 GNUNET_PQ_PREPARED_STATEMENT_END 116 GNUNET_PQ_PREPARED_STATEMENT_END
107 }; 117 };
108 118
@@ -136,7 +146,7 @@ init_connection (struct Plugin *plugin)
136 * 146 *
137 * @param cls closure (our `struct Plugin`) 147 * @param cls closure (our `struct Plugin`)
138 * @param key key to store @a data under 148 * @param key key to store @a data under
139 * @param am_closest are we the closest peer? 149 * @param prox proximity of @a key to my PID
140 * @param data_size number of bytes in @a data 150 * @param data_size number of bytes in @a data
141 * @param data data to store 151 * @param data data to store
142 * @param type type of the value 152 * @param type type of the value
@@ -148,7 +158,7 @@ init_connection (struct Plugin *plugin)
148static ssize_t 158static ssize_t
149postgres_plugin_put (void *cls, 159postgres_plugin_put (void *cls,
150 const struct GNUNET_HashCode *key, 160 const struct GNUNET_HashCode *key,
151 int am_closest, 161 uint32_t prox,
152 size_t data_size, 162 size_t data_size,
153 const char *data, 163 const char *data,
154 enum GNUNET_BLOCK_Type type, 164 enum GNUNET_BLOCK_Type type,
@@ -160,6 +170,7 @@ postgres_plugin_put (void *cls,
160 uint32_t type32 = (uint32_t) type; 170 uint32_t type32 = (uint32_t) type;
161 struct GNUNET_PQ_QueryParam params[] = { 171 struct GNUNET_PQ_QueryParam params[] = {
162 GNUNET_PQ_query_param_uint32 (&type32), 172 GNUNET_PQ_query_param_uint32 (&type32),
173 GNUNET_PQ_query_param_uint32 (&prox),
163 GNUNET_PQ_query_param_absolute_time (&discard_time), 174 GNUNET_PQ_query_param_absolute_time (&discard_time),
164 GNUNET_PQ_query_param_auto_from_type (key), 175 GNUNET_PQ_query_param_auto_from_type (key),
165 GNUNET_PQ_query_param_fixed_size (data, data_size), 176 GNUNET_PQ_query_param_fixed_size (data, data_size),
@@ -356,11 +367,22 @@ postgres_plugin_del (void *cls)
356 GNUNET_PQ_query_param_uint32 (&oid), 367 GNUNET_PQ_query_param_uint32 (&oid),
357 GNUNET_PQ_query_param_end 368 GNUNET_PQ_query_param_end
358 }; 369 };
370 struct GNUNET_TIME_Absolute now;
371 struct GNUNET_PQ_QueryParam xparam[] = {
372 GNUNET_PQ_query_param_absolute_time (&now),
373 GNUNET_PQ_query_param_end
374 };
359 375
376 now = GNUNET_TIME_absolute_get ();
360 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh, 377 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
361 "getm", 378 "getex",
362 pempty, 379 xparam,
363 rs); 380 rs);
381 if (0 >= res)
382 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
383 "getm",
384 pempty,
385 rs);
364 if (0 > res) 386 if (0 > res)
365 return GNUNET_SYSERR; 387 return GNUNET_SYSERR;
366 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res) 388 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res)