aboutsummaryrefslogtreecommitdiff
path: root/src/namecache/plugin_namecache_postgres.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/namecache/plugin_namecache_postgres.c')
-rw-r--r--src/namecache/plugin_namecache_postgres.c85
1 files changed, 28 insertions, 57 deletions
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,