aboutsummaryrefslogtreecommitdiff
path: root/src/namecache
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2018-12-11 20:31:46 +0100
committerChristian Grothoff <christian@grothoff.org>2018-12-11 20:31:46 +0100
commit174c0ad98ec6a0b565efe7f73196ec7844a046ea (patch)
tree2df47b09e0cc002b6f66cd0b6f4d4ea46d33df56 /src/namecache
parentc1dd4d02762d51d1ac4078407cda2796aff8faf4 (diff)
downloadgnunet-174c0ad98ec6a0b565efe7f73196ec7844a046ea.tar.gz
gnunet-174c0ad98ec6a0b565efe7f73196ec7844a046ea.zip
fix #5500
Diffstat (limited to 'src/namecache')
-rw-r--r--src/namecache/plugin_namecache_sqlite.c152
1 files changed, 33 insertions, 119 deletions
diff --git a/src/namecache/plugin_namecache_sqlite.c b/src/namecache/plugin_namecache_sqlite.c
index b355d37ba..84794e1ac 100644
--- a/src/namecache/plugin_namecache_sqlite.c
+++ b/src/namecache/plugin_namecache_sqlite.c
@@ -90,74 +90,10 @@ struct Plugin
90 */ 90 */
91 sqlite3_stmt *expire_blocks; 91 sqlite3_stmt *expire_blocks;
92 92
93
94}; 93};
95 94
96 95
97/** 96/**
98 * @brief Prepare a SQL statement
99 *
100 * @param dbh handle to the database
101 * @param zSql SQL statement, UTF-8 encoded
102 * @param ppStmt set to the prepared statement
103 * @return 0 on success
104 */
105static int
106sq_prepare (sqlite3 *dbh,
107 const char *zSql,
108 sqlite3_stmt **ppStmt)
109{
110 char *dummy;
111 int result;
112
113 result = sqlite3_prepare_v2 (dbh,
114 zSql,
115 strlen (zSql),
116 ppStmt,
117 (const char **) &dummy);
118 LOG (GNUNET_ERROR_TYPE_DEBUG,
119 "Prepared `%s' / %p: %d\n",
120 zSql,
121 *ppStmt,
122 result);
123 return result;
124}
125
126
127/**
128 * Create our database indices.
129 *
130 * @param dbh handle to the database
131 */
132static void
133create_indices (sqlite3 * dbh)
134{
135 /* create indices */
136 if ( (SQLITE_OK !=
137 sqlite3_exec (dbh,
138 "CREATE INDEX IF NOT EXISTS ir_query_hash ON ns096blocks (query,expiration_time)",
139 NULL, NULL, NULL)) ||
140 (SQLITE_OK !=
141 sqlite3_exec (dbh,
142 "CREATE INDEX IF NOT EXISTS ir_block_expiration ON ns096blocks (expiration_time)",
143 NULL, NULL, NULL)) )
144 LOG (GNUNET_ERROR_TYPE_ERROR,
145 "Failed to create indices: %s\n",
146 sqlite3_errmsg (dbh));
147}
148
149
150#if 0
151#define CHECK(a) GNUNET_break(a)
152#define ENULL NULL
153#else
154#define ENULL &e
155#define ENULL_DEFINED 1
156#define CHECK(a) if (! (a)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
157#endif
158
159
160/**
161 * Initialize the database connections and associated 97 * Initialize the database connections and associated
162 * data structures (create tables and indices 98 * data structures (create tables and indices
163 * as needed as well). 99 * as needed as well).
@@ -177,14 +113,30 @@ database_setup (struct Plugin *plugin)
177 GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=EXCLUSIVE"), 113 GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=EXCLUSIVE"),
178 GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"), 114 GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"),
179 GNUNET_SQ_make_try_execute ("PRAGMA journal_mode=WAL"), 115 GNUNET_SQ_make_try_execute ("PRAGMA journal_mode=WAL"),
116 GNUNET_SQ_make_execute ("CREATE TABLE IF NOT EXISTS ns096blocks ("
117 " query BLOB NOT NULL,"
118 " block BLOB NOT NULL,"
119 " expiration_time INT8 NOT NULL"
120 ")"),
121 GNUNET_SQ_make_execute ("CREATE INDEX IF NOT EXISTS ir_query_hash "
122 "ON ns096blocks (query,expiration_time)"),
123 GNUNET_SQ_make_execute ("CREATE INDEX IF NOT EXISTS ir_block_expiration "
124 "ON ns096blocks (expiration_time)"),
180 GNUNET_SQ_EXECUTE_STATEMENT_END 125 GNUNET_SQ_EXECUTE_STATEMENT_END
181 }; 126 };
182 127 struct GNUNET_SQ_PrepareStatement ps[] = {
183 sqlite3_stmt *stmt; 128 GNUNET_SQ_make_prepare ("INSERT INTO ns096blocks (query,block,expiration_time) VALUES (?, ?, ?)",
129 &plugin->cache_block),
130 GNUNET_SQ_make_prepare ("DELETE FROM ns096blocks WHERE expiration_time<?",
131 &plugin->expire_blocks),
132 GNUNET_SQ_make_prepare ("DELETE FROM ns096blocks WHERE query=? AND expiration_time<=?",
133 &plugin->delete_block),
134 GNUNET_SQ_make_prepare ("SELECT block FROM ns096blocks WHERE query=? "
135 "ORDER BY expiration_time DESC LIMIT 1",
136 &plugin->lookup_block),
137 GNUNET_SQ_PREPARE_END
138 };
184 char *afsdir; 139 char *afsdir;
185#if ENULL_DEFINED
186 char *e;
187#endif
188 140
189 if (GNUNET_OK != 141 if (GNUNET_OK !=
190 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, 142 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
@@ -230,59 +182,21 @@ database_setup (struct Plugin *plugin)
230 plugin->fn); 182 plugin->fn);
231 return GNUNET_SYSERR; 183 return GNUNET_SYSERR;
232 } 184 }
233 CHECK (SQLITE_OK == 185 GNUNET_break (SQLITE_OK ==
234 sqlite3_busy_timeout (plugin->dbh, 186 sqlite3_busy_timeout (plugin->dbh,
235 BUSY_TIMEOUT_MS)); 187 BUSY_TIMEOUT_MS));
236 188
237 189 if (GNUNET_OK !=
238 /* Create tables */ 190 GNUNET_SQ_prepare (plugin->dbh,
239 CHECK (SQLITE_OK == 191 ps))
240 sq_prepare (plugin->dbh,
241 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns096blocks'",
242 &stmt));
243 if ( (sqlite3_step (stmt) == SQLITE_DONE) &&
244 (SQLITE_OK !=
245 sqlite3_exec (plugin->dbh,
246 "CREATE TABLE ns096blocks ("
247 " query BLOB NOT NULL,"
248 " block BLOB NOT NULL,"
249 " expiration_time INT8 NOT NULL"
250 ")",
251 NULL, NULL, NULL)) )
252 {
253 LOG_SQLITE (plugin,
254 GNUNET_ERROR_TYPE_ERROR,
255 "sqlite3_exec");
256 sqlite3_finalize (stmt);
257 return GNUNET_SYSERR;
258 }
259 sqlite3_finalize (stmt);
260 create_indices (plugin->dbh);
261
262 if ( (SQLITE_OK !=
263 sq_prepare (plugin->dbh,
264 "INSERT INTO ns096blocks (query,block,expiration_time) VALUES (?, ?, ?)",
265 &plugin->cache_block)) ||
266 (SQLITE_OK !=
267 sq_prepare (plugin->dbh,
268 "DELETE FROM ns096blocks WHERE expiration_time<?",
269 &plugin->expire_blocks)) ||
270 (SQLITE_OK !=
271 sq_prepare (plugin->dbh,
272 "DELETE FROM ns096blocks WHERE query=? AND expiration_time<=?",
273 &plugin->delete_block)) ||
274 (SQLITE_OK !=
275 sq_prepare (plugin->dbh,
276 "SELECT block FROM ns096blocks WHERE query=? "
277 "ORDER BY expiration_time DESC LIMIT 1",
278 &plugin->lookup_block) )
279 )
280 { 192 {
281 LOG_SQLITE (plugin, 193 GNUNET_break (0);
282 GNUNET_ERROR_TYPE_ERROR, 194 LOG (GNUNET_ERROR_TYPE_ERROR,
283 "precompiling"); 195 _("Failed to setup database at `%s'\n"),
196 plugin->fn);
284 return GNUNET_SYSERR; 197 return GNUNET_SYSERR;
285 } 198 }
199
286 return GNUNET_OK; 200 return GNUNET_OK;
287} 201}
288 202