summaryrefslogtreecommitdiff
path: root/src/datastore/plugin_datastore_sqlite.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/datastore/plugin_datastore_sqlite.c')
-rw-r--r--src/datastore/plugin_datastore_sqlite.c1445
1 files changed, 724 insertions, 721 deletions
diff --git a/src/datastore/plugin_datastore_sqlite.c b/src/datastore/plugin_datastore_sqlite.c
index 4bd2b39cb..7d8d06529 100644
--- a/src/datastore/plugin_datastore_sqlite.c
+++ b/src/datastore/plugin_datastore_sqlite.c
@@ -1,22 +1,22 @@
1/* 1/*
2 * This file is part of GNUnet 2 * This file is part of GNUnet
3 * Copyright (C) 2009, 2011, 2017 GNUnet e.V. 3 * Copyright (C) 2009, 2011, 2017 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
7 * by the Free Software Foundation, either version 3 of the License, 7 * by the Free Software Foundation, either version 3 of the License,
8 * or (at your 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 * Affero 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 Affero General Public License 15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
20 20
21/** 21/**
22 * @file datastore/plugin_datastore_sqlite.c 22 * @file datastore/plugin_datastore_sqlite.c
@@ -57,15 +57,15 @@
57 */ 57 */
58#define LOG_SQLITE(db, level, cmd) \ 58#define LOG_SQLITE(db, level, cmd) \
59 do \ 59 do \
60 { \ 60 { \
61 GNUNET_log_from (level, \ 61 GNUNET_log_from(level, \
62 "sqlite", \ 62 "sqlite", \
63 _ ("`%s' failed at %s:%d with error: %s\n"), \ 63 _("`%s' failed at %s:%d with error: %s\n"), \
64 cmd, \ 64 cmd, \
65 __FILE__, \ 65 __FILE__, \
66 __LINE__, \ 66 __LINE__, \
67 sqlite3_errmsg (db->dbh)); \ 67 sqlite3_errmsg(db->dbh)); \
68 } while (0) 68 } while (0)
69 69
70 70
71/** 71/**
@@ -75,28 +75,27 @@
75 */ 75 */
76#define LOG_SQLITE_MSG(db, msg, level, cmd) \ 76#define LOG_SQLITE_MSG(db, msg, level, cmd) \
77 do \ 77 do \
78 { \ 78 { \
79 GNUNET_log_from (level, \ 79 GNUNET_log_from(level, \
80 "sqlite", \ 80 "sqlite", \
81 _ ("`%s' failed at %s:%d with error: %s\n"), \ 81 _("`%s' failed at %s:%d with error: %s\n"), \
82 cmd, \ 82 cmd, \
83 __FILE__, \ 83 __FILE__, \
84 __LINE__, \ 84 __LINE__, \
85 sqlite3_errmsg (db->dbh)); \ 85 sqlite3_errmsg(db->dbh)); \
86 GNUNET_asprintf (msg, \ 86 GNUNET_asprintf(msg, \
87 _ ("`%s' failed at %s:%u with error: %s"), \ 87 _("`%s' failed at %s:%u with error: %s"), \
88 cmd, \ 88 cmd, \
89 __FILE__, \ 89 __FILE__, \
90 __LINE__, \ 90 __LINE__, \
91 sqlite3_errmsg (db->dbh)); \ 91 sqlite3_errmsg(db->dbh)); \
92 } while (0) 92 } while (0)
93 93
94 94
95/** 95/**
96 * Context for all functions in this plugin. 96 * Context for all functions in this plugin.
97 */ 97 */
98struct Plugin 98struct Plugin {
99{
100 /** 99 /**
101 * Our execution environment. 100 * Our execution environment.
102 */ 101 */
@@ -178,22 +177,22 @@ struct Plugin
178 * @return 0 on success 177 * @return 0 on success
179 */ 178 */
180static int 179static int
181sq_prepare (sqlite3 *dbh, const char *zSql, sqlite3_stmt **ppStmt) 180sq_prepare(sqlite3 *dbh, const char *zSql, sqlite3_stmt **ppStmt)
182{ 181{
183 char *dummy; 182 char *dummy;
184 int result; 183 int result;
185 184
186 result = sqlite3_prepare_v2 (dbh, 185 result = sqlite3_prepare_v2(dbh,
187 zSql, 186 zSql,
188 strlen (zSql), 187 strlen(zSql),
189 ppStmt, 188 ppStmt,
190 (const char **) &dummy); 189 (const char **)&dummy);
191 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 190 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
192 "sqlite", 191 "sqlite",
193 "Prepared `%s' / %p: %d\n", 192 "Prepared `%s' / %p: %d\n",
194 zSql, 193 zSql,
195 *ppStmt, 194 *ppStmt,
196 result); 195 result);
197 return result; 196 return result;
198} 197}
199 198
@@ -204,56 +203,56 @@ sq_prepare (sqlite3 *dbh, const char *zSql, sqlite3_stmt **ppStmt)
204 * @param dbh handle to the database 203 * @param dbh handle to the database
205 */ 204 */
206static void 205static void
207create_indices (sqlite3 *dbh) 206create_indices(sqlite3 *dbh)
208{ 207{
209 /* create indices */ 208 /* create indices */
210 if ( 209 if (
211 0 != 210 0 !=
212 (SQLITE_OK != 211 (SQLITE_OK !=
213 sqlite3_exec (dbh, 212 sqlite3_exec(dbh,
214 "CREATE INDEX IF NOT EXISTS idx_hash ON gn091 (hash)", 213 "CREATE INDEX IF NOT EXISTS idx_hash ON gn091 (hash)",
215 NULL, 214 NULL,
216 NULL, 215 NULL,
217 NULL)) + 216 NULL)) +
218 (SQLITE_OK != 217 (SQLITE_OK !=
219 sqlite3_exec ( 218 sqlite3_exec(
220 dbh, 219 dbh,
221 "CREATE INDEX IF NOT EXISTS idx_anon_type ON gn091 (anonLevel ASC,type)", 220 "CREATE INDEX IF NOT EXISTS idx_anon_type ON gn091 (anonLevel ASC,type)",
222 NULL, 221 NULL,
223 NULL, 222 NULL,
224 NULL)) + 223 NULL)) +
225 (SQLITE_OK != 224 (SQLITE_OK !=
226 sqlite3_exec (dbh, 225 sqlite3_exec(dbh,
227 "CREATE INDEX IF NOT EXISTS idx_expire ON gn091 (expire ASC)", 226 "CREATE INDEX IF NOT EXISTS idx_expire ON gn091 (expire ASC)",
228 NULL, 227 NULL,
229 NULL, 228 NULL,
230 NULL)) + 229 NULL)) +
231 (SQLITE_OK != 230 (SQLITE_OK !=
232 sqlite3_exec ( 231 sqlite3_exec(
233 dbh, 232 dbh,
234 "CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn091 (repl,rvalue)", 233 "CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn091 (repl,rvalue)",
235 NULL, 234 NULL,
236 NULL, 235 NULL,
237 NULL))) 236 NULL)))
238 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 237 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR,
239 "sqlite", 238 "sqlite",
240 "Failed to create indices: %s\n", 239 "Failed to create indices: %s\n",
241 sqlite3_errmsg (dbh)); 240 sqlite3_errmsg(dbh));
242} 241}
243 242
244 243
245#if 0 244#if 0
246#define CHECK(a) GNUNET_break (a) 245#define CHECK(a) GNUNET_break(a)
247#define ENULL NULL 246#define ENULL NULL
248#else 247#else
249#define ENULL &e 248#define ENULL &e
250#define ENULL_DEFINED 1 249#define ENULL_DEFINED 1
251#define CHECK(a) \ 250#define CHECK(a) \
252 if (! (a)) \ 251 if (!(a)) \
253 { \ 252 { \
254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", e); \ 253 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); \
255 sqlite3_free (e); \ 254 sqlite3_free(e); \
256 } 255 }
257#endif 256#endif
258 257
259 258
@@ -267,219 +266,220 @@ create_indices (sqlite3 *dbh)
267 * @return #GNUNET_OK on success 266 * @return #GNUNET_OK on success
268 */ 267 */
269static int 268static int
270database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg, 269database_setup(const struct GNUNET_CONFIGURATION_Handle *cfg,
271 struct Plugin *plugin) 270 struct Plugin *plugin)
272{ 271{
273 sqlite3_stmt *stmt; 272 sqlite3_stmt *stmt;
274 char *afsdir; 273 char *afsdir;
274
275#if ENULL_DEFINED 275#if ENULL_DEFINED
276 char *e; 276 char *e;
277#endif 277#endif
278 278
279 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, 279 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg,
280 "datastore-sqlite", 280 "datastore-sqlite",
281 "FILENAME", 281 "FILENAME",
282 &afsdir)) 282 &afsdir))
283 {
284 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
285 "datastore-sqlite",
286 "FILENAME");
287 return GNUNET_SYSERR;
288 }
289 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
290 {
291 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
292 { 283 {
293 GNUNET_break (0); 284 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
294 GNUNET_free (afsdir); 285 "datastore-sqlite",
286 "FILENAME");
295 return GNUNET_SYSERR; 287 return GNUNET_SYSERR;
296 } 288 }
297 /* database is new or got deleted, reset payload to zero! */ 289 if (GNUNET_OK != GNUNET_DISK_file_test(afsdir))
298 if (NULL != plugin->env->duc) 290 {
299 plugin->env->duc (plugin->env->cls, 0); 291 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file(afsdir))
300 } 292 {
293 GNUNET_break(0);
294 GNUNET_free(afsdir);
295 return GNUNET_SYSERR;
296 }
297 /* database is new or got deleted, reset payload to zero! */
298 if (NULL != plugin->env->duc)
299 plugin->env->duc(plugin->env->cls, 0);
300 }
301 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */ 301 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
302 plugin->fn = afsdir; 302 plugin->fn = afsdir;
303 303
304 /* Open database and precompile statements */ 304 /* Open database and precompile statements */
305 if (SQLITE_OK != sqlite3_open (plugin->fn, &plugin->dbh)) 305 if (SQLITE_OK != sqlite3_open(plugin->fn, &plugin->dbh))
306 { 306 {
307 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 307 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR,
308 "sqlite", 308 "sqlite",
309 _ ("Unable to initialize SQLite: %s.\n"), 309 _("Unable to initialize SQLite: %s.\n"),
310 sqlite3_errmsg (plugin->dbh)); 310 sqlite3_errmsg(plugin->dbh));
311 return GNUNET_SYSERR; 311 return GNUNET_SYSERR;
312 } 312 }
313 CHECK ( 313 CHECK(
314 SQLITE_OK == 314 SQLITE_OK ==
315 sqlite3_exec (plugin->dbh, "PRAGMA temp_store=MEMORY", NULL, NULL, ENULL)); 315 sqlite3_exec(plugin->dbh, "PRAGMA temp_store=MEMORY", NULL, NULL, ENULL));
316 CHECK ( 316 CHECK(
317 SQLITE_OK == 317 SQLITE_OK ==
318 sqlite3_exec (plugin->dbh, "PRAGMA synchronous=OFF", NULL, NULL, ENULL)); 318 sqlite3_exec(plugin->dbh, "PRAGMA synchronous=OFF", NULL, NULL, ENULL));
319 CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh, 319 CHECK(SQLITE_OK == sqlite3_exec(plugin->dbh,
320 "PRAGMA legacy_file_format=OFF", 320 "PRAGMA legacy_file_format=OFF",
321 NULL, 321 NULL,
322 NULL, 322 NULL,
323 ENULL)); 323 ENULL));
324 CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh, 324 CHECK(SQLITE_OK == sqlite3_exec(plugin->dbh,
325 "PRAGMA auto_vacuum=INCREMENTAL", 325 "PRAGMA auto_vacuum=INCREMENTAL",
326 NULL, 326 NULL,
327 NULL, 327 NULL,
328 ENULL)); 328 ENULL));
329 CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh, 329 CHECK(SQLITE_OK == sqlite3_exec(plugin->dbh,
330 "PRAGMA locking_mode=EXCLUSIVE", 330 "PRAGMA locking_mode=EXCLUSIVE",
331 NULL, 331 NULL,
332 NULL, 332 NULL,
333 ENULL)); 333 ENULL));
334 CHECK ( 334 CHECK(
335 SQLITE_OK == 335 SQLITE_OK ==
336 sqlite3_exec (plugin->dbh, "PRAGMA page_size=4096", NULL, NULL, ENULL)); 336 sqlite3_exec(plugin->dbh, "PRAGMA page_size=4096", NULL, NULL, ENULL));
337 337
338 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS)); 338 CHECK(SQLITE_OK == sqlite3_busy_timeout(plugin->dbh, BUSY_TIMEOUT_MS));
339 339
340 340
341 /* We have to do it here, because otherwise precompiling SQL might fail */ 341 /* We have to do it here, because otherwise precompiling SQL might fail */
342 CHECK (SQLITE_OK == 342 CHECK(SQLITE_OK ==
343 sq_prepare (plugin->dbh, 343 sq_prepare(plugin->dbh,
344 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn091'", 344 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn091'",
345 &stmt)); 345 &stmt));
346 346
347 /* FIXME: SQLite does not have unsigned integers! This is ok for the type column because 347 /* FIXME: SQLite does not have unsigned integers! This is ok for the type column because
348 * we only test equality on it and can cast it to/from uint32_t. For repl, prio, and anonLevel 348 * we only test equality on it and can cast it to/from uint32_t. For repl, prio, and anonLevel
349 * we do math or inequality tests, so we can't handle the entire range of uint32_t. 349 * we do math or inequality tests, so we can't handle the entire range of uint32_t.
350 * This will also cause problems for expiration times after 294247-01-10-04:00:54 UTC. 350 * This will also cause problems for expiration times after 294247-01-10-04:00:54 UTC.
351 */ 351 */
352 if ((SQLITE_DONE == sqlite3_step (stmt)) && 352 if ((SQLITE_DONE == sqlite3_step(stmt)) &&
353 (SQLITE_OK != sqlite3_exec (plugin->dbh, 353 (SQLITE_OK != sqlite3_exec(plugin->dbh,
354 "CREATE TABLE gn091 (" 354 "CREATE TABLE gn091 ("
355 " repl INT4 NOT NULL DEFAULT 0," 355 " repl INT4 NOT NULL DEFAULT 0,"
356 " type INT4 NOT NULL DEFAULT 0," 356 " type INT4 NOT NULL DEFAULT 0,"
357 " prio INT4 NOT NULL DEFAULT 0," 357 " prio INT4 NOT NULL DEFAULT 0,"
358 " anonLevel INT4 NOT NULL DEFAULT 0," 358 " anonLevel INT4 NOT NULL DEFAULT 0,"
359 " expire INT8 NOT NULL DEFAULT 0," 359 " expire INT8 NOT NULL DEFAULT 0,"
360 " rvalue INT8 NOT NULL," 360 " rvalue INT8 NOT NULL,"
361 " hash TEXT NOT NULL DEFAULT ''," 361 " hash TEXT NOT NULL DEFAULT '',"
362 " vhash TEXT NOT NULL DEFAULT ''," 362 " vhash TEXT NOT NULL DEFAULT '',"
363 " value BLOB NOT NULL DEFAULT '')", 363 " value BLOB NOT NULL DEFAULT '')",
364 NULL, 364 NULL,
365 NULL, 365 NULL,
366 NULL))) 366 NULL)))
367 { 367 {
368 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec"); 368 LOG_SQLITE(plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
369 sqlite3_finalize (stmt); 369 sqlite3_finalize(stmt);
370 return GNUNET_SYSERR; 370 return GNUNET_SYSERR;
371 } 371 }
372 sqlite3_finalize (stmt); 372 sqlite3_finalize(stmt);
373 create_indices (plugin->dbh); 373 create_indices(plugin->dbh);
374 374
375#define RESULT_COLUMNS \ 375#define RESULT_COLUMNS \
376 "repl, type, prio, anonLevel, expire, hash, value, _ROWID_" 376 "repl, type, prio, anonLevel, expire, hash, value, _ROWID_"
377 if ( 377 if (
378 (SQLITE_OK != sq_prepare (plugin->dbh, 378 (SQLITE_OK != sq_prepare(plugin->dbh,
379 "UPDATE gn091 " 379 "UPDATE gn091 "
380 "SET prio = prio + ?, " 380 "SET prio = prio + ?, "
381 "repl = repl + ?, " 381 "repl = repl + ?, "
382 "expire = MAX(expire, ?) " 382 "expire = MAX(expire, ?) "
383 "WHERE hash = ? AND vhash = ?", 383 "WHERE hash = ? AND vhash = ?",
384 &plugin->update)) || 384 &plugin->update)) ||
385 (SQLITE_OK != sq_prepare (plugin->dbh, 385 (SQLITE_OK != sq_prepare(plugin->dbh,
386 "UPDATE gn091 " 386 "UPDATE gn091 "
387 "SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?", 387 "SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?",
388 &plugin->updRepl)) || 388 &plugin->updRepl)) ||
389 (SQLITE_OK != sq_prepare (plugin->dbh, 389 (SQLITE_OK != sq_prepare(plugin->dbh,
390 "SELECT " RESULT_COLUMNS " FROM gn091 " 390 "SELECT " RESULT_COLUMNS " FROM gn091 "
391 "WHERE repl=?2 AND " 391 "WHERE repl=?2 AND "
392 " (rvalue>=?1 OR " 392 " (rvalue>=?1 OR "
393 " NOT EXISTS (SELECT 1 FROM gn091 " 393 " NOT EXISTS (SELECT 1 FROM gn091 "
394 "WHERE repl=?2 AND rvalue>=?1 LIMIT 1) ) " 394 "WHERE repl=?2 AND rvalue>=?1 LIMIT 1) ) "
395 "ORDER BY rvalue ASC LIMIT 1", 395 "ORDER BY rvalue ASC LIMIT 1",
396 &plugin->selRepl)) || 396 &plugin->selRepl)) ||
397 (SQLITE_OK != sq_prepare (plugin->dbh, 397 (SQLITE_OK != sq_prepare(plugin->dbh,
398 "SELECT MAX(repl) FROM gn091", 398 "SELECT MAX(repl) FROM gn091",
399 &plugin->maxRepl)) || 399 &plugin->maxRepl)) ||
400 (SQLITE_OK != 400 (SQLITE_OK !=
401 sq_prepare (plugin->dbh, 401 sq_prepare(plugin->dbh,
402 "SELECT " RESULT_COLUMNS " FROM gn091 " 402 "SELECT " RESULT_COLUMNS " FROM gn091 "
403 "WHERE NOT EXISTS (SELECT 1 FROM gn091 WHERE expire < ?1 LIMIT 1) OR (expire < ?1) " 403 "WHERE NOT EXISTS (SELECT 1 FROM gn091 WHERE expire < ?1 LIMIT 1) OR (expire < ?1) "
404 "ORDER BY expire ASC LIMIT 1", 404 "ORDER BY expire ASC LIMIT 1",
405 &plugin->selExpi)) || 405 &plugin->selExpi)) ||
406 (SQLITE_OK != sq_prepare (plugin->dbh, 406 (SQLITE_OK != sq_prepare(plugin->dbh,
407 "SELECT " RESULT_COLUMNS " FROM gn091 " 407 "SELECT " RESULT_COLUMNS " FROM gn091 "
408 "WHERE _ROWID_ >= ? AND " 408 "WHERE _ROWID_ >= ? AND "
409 "anonLevel = 0 AND " 409 "anonLevel = 0 AND "
410 "type = ? " 410 "type = ? "
411 "ORDER BY _ROWID_ ASC LIMIT 1", 411 "ORDER BY _ROWID_ ASC LIMIT 1",
412 &plugin->selZeroAnon)) || 412 &plugin->selZeroAnon)) ||
413 (SQLITE_OK != 413 (SQLITE_OK !=
414 sq_prepare (plugin->dbh, 414 sq_prepare(plugin->dbh,
415 "INSERT INTO gn091 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " 415 "INSERT INTO gn091 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
416 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", 416 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
417 &plugin->insertContent)) || 417 &plugin->insertContent)) ||
418 (SQLITE_OK != sq_prepare (plugin->dbh, 418 (SQLITE_OK != sq_prepare(plugin->dbh,
419 "SELECT " RESULT_COLUMNS " FROM gn091 " 419 "SELECT " RESULT_COLUMNS " FROM gn091 "
420 "WHERE _ROWID_ >= ?1 " 420 "WHERE _ROWID_ >= ?1 "
421 "ORDER BY _ROWID_ ASC LIMIT 1", 421 "ORDER BY _ROWID_ ASC LIMIT 1",
422 &plugin->get[0])) || 422 &plugin->get[0])) ||
423 (SQLITE_OK != sq_prepare (plugin->dbh, 423 (SQLITE_OK != sq_prepare(plugin->dbh,
424 "SELECT " RESULT_COLUMNS " FROM gn091 " 424 "SELECT " RESULT_COLUMNS " FROM gn091 "
425 "WHERE _ROWID_ >= ?1 AND " 425 "WHERE _ROWID_ >= ?1 AND "
426 "type = ?4 " 426 "type = ?4 "
427 "ORDER BY _ROWID_ ASC LIMIT 1", 427 "ORDER BY _ROWID_ ASC LIMIT 1",
428 &plugin->get[1])) || 428 &plugin->get[1])) ||
429 (SQLITE_OK != sq_prepare (plugin->dbh, 429 (SQLITE_OK != sq_prepare(plugin->dbh,
430 "SELECT " RESULT_COLUMNS " FROM gn091 " 430 "SELECT " RESULT_COLUMNS " FROM gn091 "
431 "WHERE _ROWID_ >= ?1 AND " 431 "WHERE _ROWID_ >= ?1 AND "
432 "hash = ?3 " 432 "hash = ?3 "
433 "ORDER BY _ROWID_ ASC LIMIT 1", 433 "ORDER BY _ROWID_ ASC LIMIT 1",
434 &plugin->get[2])) || 434 &plugin->get[2])) ||
435 (SQLITE_OK != sq_prepare (plugin->dbh, 435 (SQLITE_OK != sq_prepare(plugin->dbh,
436 "SELECT " RESULT_COLUMNS " FROM gn091 " 436 "SELECT " RESULT_COLUMNS " FROM gn091 "
437 "WHERE _ROWID_ >= ?1 AND " 437 "WHERE _ROWID_ >= ?1 AND "
438 "hash = ?3 AND " 438 "hash = ?3 AND "
439 "type = ?4 " 439 "type = ?4 "
440 "ORDER BY _ROWID_ ASC LIMIT 1", 440 "ORDER BY _ROWID_ ASC LIMIT 1",
441 &plugin->get[3])) || 441 &plugin->get[3])) ||
442 (SQLITE_OK != sq_prepare (plugin->dbh, 442 (SQLITE_OK != sq_prepare(plugin->dbh,
443 "SELECT " RESULT_COLUMNS " FROM gn091 " 443 "SELECT " RESULT_COLUMNS " FROM gn091 "
444 "WHERE _ROWID_ >= ?1 AND " 444 "WHERE _ROWID_ >= ?1 AND "
445 "rvalue >= ?2 " 445 "rvalue >= ?2 "
446 "ORDER BY _ROWID_ ASC LIMIT 1", 446 "ORDER BY _ROWID_ ASC LIMIT 1",
447 &plugin->get[4])) || 447 &plugin->get[4])) ||
448 (SQLITE_OK != sq_prepare (plugin->dbh, 448 (SQLITE_OK != sq_prepare(plugin->dbh,
449 "SELECT " RESULT_COLUMNS " FROM gn091 " 449 "SELECT " RESULT_COLUMNS " FROM gn091 "
450 "WHERE _ROWID_ >= ?1 AND " 450 "WHERE _ROWID_ >= ?1 AND "
451 "rvalue >= ?2 AND " 451 "rvalue >= ?2 AND "
452 "type = ?4 " 452 "type = ?4 "
453 "ORDER BY _ROWID_ ASC LIMIT 1", 453 "ORDER BY _ROWID_ ASC LIMIT 1",
454 &plugin->get[5])) || 454 &plugin->get[5])) ||
455 (SQLITE_OK != sq_prepare (plugin->dbh, 455 (SQLITE_OK != sq_prepare(plugin->dbh,
456 "SELECT " RESULT_COLUMNS " FROM gn091 " 456 "SELECT " RESULT_COLUMNS " FROM gn091 "
457 "WHERE _ROWID_ >= ?1 AND " 457 "WHERE _ROWID_ >= ?1 AND "
458 "rvalue >= ?2 AND " 458 "rvalue >= ?2 AND "
459 "hash = ?3 " 459 "hash = ?3 "
460 "ORDER BY _ROWID_ ASC LIMIT 1", 460 "ORDER BY _ROWID_ ASC LIMIT 1",
461 &plugin->get[6])) || 461 &plugin->get[6])) ||
462 (SQLITE_OK != sq_prepare (plugin->dbh, 462 (SQLITE_OK != sq_prepare(plugin->dbh,
463 "SELECT " RESULT_COLUMNS " FROM gn091 " 463 "SELECT " RESULT_COLUMNS " FROM gn091 "
464 "WHERE _ROWID_ >= ?1 AND " 464 "WHERE _ROWID_ >= ?1 AND "
465 "rvalue >= ?2 AND " 465 "rvalue >= ?2 AND "
466 "hash = ?3 AND " 466 "hash = ?3 AND "
467 "type = ?4 " 467 "type = ?4 "
468 "ORDER BY _ROWID_ ASC LIMIT 1", 468 "ORDER BY _ROWID_ ASC LIMIT 1",
469 &plugin->get[7])) || 469 &plugin->get[7])) ||
470 (SQLITE_OK != sq_prepare (plugin->dbh, 470 (SQLITE_OK != sq_prepare(plugin->dbh,
471 "DELETE FROM gn091 WHERE _ROWID_ = ?", 471 "DELETE FROM gn091 WHERE _ROWID_ = ?",
472 &plugin->delRow)) || 472 &plugin->delRow)) ||
473 (SQLITE_OK != sq_prepare (plugin->dbh, 473 (SQLITE_OK != sq_prepare(plugin->dbh,
474 "DELETE FROM gn091 " 474 "DELETE FROM gn091 "
475 "WHERE hash = ? AND " 475 "WHERE hash = ? AND "
476 "value = ? ", 476 "value = ? ",
477 &plugin->remove)) || 477 &plugin->remove)) ||
478 false) 478 false)
479 { 479 {
480 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "precompiling"); 480 LOG_SQLITE(plugin, GNUNET_ERROR_TYPE_ERROR, "precompiling");
481 return GNUNET_SYSERR; 481 return GNUNET_SYSERR;
482 } 482 }
483 return GNUNET_OK; 483 return GNUNET_OK;
484} 484}
485 485
@@ -491,65 +491,66 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
491 * @param plugin the plugin context (state for this module) 491 * @param plugin the plugin context (state for this module)
492 */ 492 */
493static void 493static void
494database_shutdown (struct Plugin *plugin) 494database_shutdown(struct Plugin *plugin)
495{ 495{
496 int result; 496 int result;
497
497#if SQLITE_VERSION_NUMBER >= 3007000 498#if SQLITE_VERSION_NUMBER >= 3007000
498 sqlite3_stmt *stmt; 499 sqlite3_stmt *stmt;
499#endif 500#endif
500 501
501 if (NULL != plugin->remove) 502 if (NULL != plugin->remove)
502 sqlite3_finalize (plugin->remove); 503 sqlite3_finalize(plugin->remove);
503 if (NULL != plugin->delRow) 504 if (NULL != plugin->delRow)
504 sqlite3_finalize (plugin->delRow); 505 sqlite3_finalize(plugin->delRow);
505 if (NULL != plugin->update) 506 if (NULL != plugin->update)
506 sqlite3_finalize (plugin->update); 507 sqlite3_finalize(plugin->update);
507 if (NULL != plugin->updRepl) 508 if (NULL != plugin->updRepl)
508 sqlite3_finalize (plugin->updRepl); 509 sqlite3_finalize(plugin->updRepl);
509 if (NULL != plugin->selRepl) 510 if (NULL != plugin->selRepl)
510 sqlite3_finalize (plugin->selRepl); 511 sqlite3_finalize(plugin->selRepl);
511 if (NULL != plugin->maxRepl) 512 if (NULL != plugin->maxRepl)
512 sqlite3_finalize (plugin->maxRepl); 513 sqlite3_finalize(plugin->maxRepl);
513 if (NULL != plugin->selExpi) 514 if (NULL != plugin->selExpi)
514 sqlite3_finalize (plugin->selExpi); 515 sqlite3_finalize(plugin->selExpi);
515 if (NULL != plugin->selZeroAnon) 516 if (NULL != plugin->selZeroAnon)
516 sqlite3_finalize (plugin->selZeroAnon); 517 sqlite3_finalize(plugin->selZeroAnon);
517 if (NULL != plugin->insertContent) 518 if (NULL != plugin->insertContent)
518 sqlite3_finalize (plugin->insertContent); 519 sqlite3_finalize(plugin->insertContent);
519 for (int i = 0; i < 8; ++i) 520 for (int i = 0; i < 8; ++i)
520 if (NULL != plugin->get[i]) 521 if (NULL != plugin->get[i])
521 sqlite3_finalize (plugin->get[i]); 522 sqlite3_finalize(plugin->get[i]);
522 result = sqlite3_close (plugin->dbh); 523 result = sqlite3_close(plugin->dbh);
523#if SQLITE_VERSION_NUMBER >= 3007000 524#if SQLITE_VERSION_NUMBER >= 3007000
524 if (result == SQLITE_BUSY) 525 if (result == SQLITE_BUSY)
525 {
526 GNUNET_log_from (
527 GNUNET_ERROR_TYPE_WARNING,
528 "sqlite",
529 _ (
530 "Tried to close sqlite without finalizing all prepared statements.\n"));
531 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
532 while (NULL != stmt)
533 { 526 {
534 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 527 GNUNET_log_from(
535 "sqlite", 528 GNUNET_ERROR_TYPE_WARNING,
536 "Closing statement %p\n", 529 "sqlite",
537 stmt); 530 _(
538 result = sqlite3_finalize (stmt); 531 "Tried to close sqlite without finalizing all prepared statements.\n"));
539 if (result != SQLITE_OK) 532 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
540 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 533 while (NULL != stmt)
541 "sqlite", 534 {
542 "Failed to close statement %p: %d\n", 535 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
543 stmt, 536 "sqlite",
544 result); 537 "Closing statement %p\n",
545 stmt = sqlite3_next_stmt (plugin->dbh, NULL); 538 stmt);
539 result = sqlite3_finalize(stmt);
540 if (result != SQLITE_OK)
541 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING,
542 "sqlite",
543 "Failed to close statement %p: %d\n",
544 stmt,
545 result);
546 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
547 }
548 result = sqlite3_close(plugin->dbh);
546 } 549 }
547 result = sqlite3_close (plugin->dbh);
548 }
549#endif 550#endif
550 if (SQLITE_OK != result) 551 if (SQLITE_OK != result)
551 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close"); 552 LOG_SQLITE(plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
552 GNUNET_free_non_null (plugin->fn); 553 GNUNET_free_non_null(plugin->fn);
553} 554}
554 555
555 556
@@ -561,22 +562,22 @@ database_shutdown (struct Plugin *plugin)
561 * @param rid the ID of the row to delete 562 * @param rid the ID of the row to delete
562 */ 563 */
563static int 564static int
564delete_by_rowid (struct Plugin *plugin, uint64_t rid) 565delete_by_rowid(struct Plugin *plugin, uint64_t rid)
565{ 566{
566 struct GNUNET_SQ_QueryParam params[] = {GNUNET_SQ_query_param_uint64 (&rid), 567 struct GNUNET_SQ_QueryParam params[] = { GNUNET_SQ_query_param_uint64(&rid),
567 GNUNET_SQ_query_param_end}; 568 GNUNET_SQ_query_param_end };
568 569
569 if (GNUNET_OK != GNUNET_SQ_bind (plugin->delRow, params)) 570 if (GNUNET_OK != GNUNET_SQ_bind(plugin->delRow, params))
570 return GNUNET_SYSERR;
571 if (SQLITE_DONE != sqlite3_step (plugin->delRow))
572 {
573 LOG_SQLITE (plugin,
574 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
575 "sqlite3_step");
576 GNUNET_SQ_reset (plugin->dbh, plugin->delRow);
577 return GNUNET_SYSERR; 571 return GNUNET_SYSERR;
578 } 572 if (SQLITE_DONE != sqlite3_step(plugin->delRow))
579 GNUNET_SQ_reset (plugin->dbh, plugin->delRow); 573 {
574 LOG_SQLITE(plugin,
575 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
576 "sqlite3_step");
577 GNUNET_SQ_reset(plugin->dbh, plugin->delRow);
578 return GNUNET_SYSERR;
579 }
580 GNUNET_SQ_reset(plugin->dbh, plugin->delRow);
580 return GNUNET_OK; 581 return GNUNET_OK;
581} 582}
582 583
@@ -598,135 +599,137 @@ delete_by_rowid (struct Plugin *plugin, uint64_t rid)
598 * @param cont_cls continuation closure 599 * @param cont_cls continuation closure
599 */ 600 */
600static void 601static void
601sqlite_plugin_put (void *cls, 602sqlite_plugin_put(void *cls,
602 const struct GNUNET_HashCode *key, 603 const struct GNUNET_HashCode *key,
603 bool absent, 604 bool absent,
604 uint32_t size, 605 uint32_t size,
605 const void *data, 606 const void *data,
606 enum GNUNET_BLOCK_Type type, 607 enum GNUNET_BLOCK_Type type,
607 uint32_t priority, 608 uint32_t priority,
608 uint32_t anonymity, 609 uint32_t anonymity,
609 uint32_t replication, 610 uint32_t replication,
610 struct GNUNET_TIME_Absolute expiration, 611 struct GNUNET_TIME_Absolute expiration,
611 PluginPutCont cont, 612 PluginPutCont cont,
612 void *cont_cls) 613 void *cont_cls)
613{ 614{
614 struct Plugin *plugin = cls; 615 struct Plugin *plugin = cls;
615 struct GNUNET_HashCode vhash; 616 struct GNUNET_HashCode vhash;
616 char *msg = NULL; 617 char *msg = NULL;
617 618
618 GNUNET_CRYPTO_hash (data, size, &vhash); 619 GNUNET_CRYPTO_hash(data, size, &vhash);
619
620 if (! absent)
621 {
622 struct GNUNET_SQ_QueryParam params[] =
623 {GNUNET_SQ_query_param_uint32 (&priority),
624 GNUNET_SQ_query_param_uint32 (&replication),
625 GNUNET_SQ_query_param_absolute_time (&expiration),
626 GNUNET_SQ_query_param_auto_from_type (key),
627 GNUNET_SQ_query_param_auto_from_type (&vhash),
628 GNUNET_SQ_query_param_end};
629 620
630 if (GNUNET_OK != GNUNET_SQ_bind (plugin->update, params)) 621 if (!absent)
631 {
632 cont (cont_cls, key, size, GNUNET_SYSERR, _ ("sqlite bind failure"));
633 return;
634 }
635 if (SQLITE_DONE != sqlite3_step (plugin->update))
636 {
637 LOG_SQLITE_MSG (plugin,
638 &msg,
639 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
640 "sqlite3_step");
641 cont (cont_cls, key, size, GNUNET_SYSERR, msg);
642 GNUNET_free_non_null (msg);
643 return;
644 }
645 int changes = sqlite3_changes (plugin->dbh);
646 GNUNET_SQ_reset (plugin->dbh, plugin->update);
647 if (0 != changes)
648 { 622 {
649 cont (cont_cls, key, size, GNUNET_NO, NULL); 623 struct GNUNET_SQ_QueryParam params[] =
650 return; 624 { GNUNET_SQ_query_param_uint32(&priority),
625 GNUNET_SQ_query_param_uint32(&replication),
626 GNUNET_SQ_query_param_absolute_time(&expiration),
627 GNUNET_SQ_query_param_auto_from_type(key),
628 GNUNET_SQ_query_param_auto_from_type(&vhash),
629 GNUNET_SQ_query_param_end };
630
631 if (GNUNET_OK != GNUNET_SQ_bind(plugin->update, params))
632 {
633 cont(cont_cls, key, size, GNUNET_SYSERR, _("sqlite bind failure"));
634 return;
635 }
636 if (SQLITE_DONE != sqlite3_step(plugin->update))
637 {
638 LOG_SQLITE_MSG(plugin,
639 &msg,
640 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
641 "sqlite3_step");
642 cont(cont_cls, key, size, GNUNET_SYSERR, msg);
643 GNUNET_free_non_null(msg);
644 return;
645 }
646 int changes = sqlite3_changes(plugin->dbh);
647 GNUNET_SQ_reset(plugin->dbh, plugin->update);
648 if (0 != changes)
649 {
650 cont(cont_cls, key, size, GNUNET_NO, NULL);
651 return;
652 }
651 } 653 }
652 }
653 654
654 uint64_t rvalue; 655 uint64_t rvalue;
655 uint32_t type32 = (uint32_t) type; 656 uint32_t type32 = (uint32_t)type;
656 struct GNUNET_SQ_QueryParam params[] = 657 struct GNUNET_SQ_QueryParam params[] =
657 {GNUNET_SQ_query_param_uint32 (&replication), 658 { GNUNET_SQ_query_param_uint32(&replication),
658 GNUNET_SQ_query_param_uint32 (&type32), 659 GNUNET_SQ_query_param_uint32(&type32),
659 GNUNET_SQ_query_param_uint32 (&priority), 660 GNUNET_SQ_query_param_uint32(&priority),
660 GNUNET_SQ_query_param_uint32 (&anonymity), 661 GNUNET_SQ_query_param_uint32(&anonymity),
661 GNUNET_SQ_query_param_absolute_time (&expiration), 662 GNUNET_SQ_query_param_absolute_time(&expiration),
662 GNUNET_SQ_query_param_uint64 (&rvalue), 663 GNUNET_SQ_query_param_uint64(&rvalue),
663 GNUNET_SQ_query_param_auto_from_type (key), 664 GNUNET_SQ_query_param_auto_from_type(key),
664 GNUNET_SQ_query_param_auto_from_type (&vhash), 665 GNUNET_SQ_query_param_auto_from_type(&vhash),
665 GNUNET_SQ_query_param_fixed_size (data, size), 666 GNUNET_SQ_query_param_fixed_size(data, size),
666 GNUNET_SQ_query_param_end}; 667 GNUNET_SQ_query_param_end };
667 int n; 668 int n;
668 int ret; 669 int ret;
669 sqlite3_stmt *stmt; 670 sqlite3_stmt *stmt;
670 671
671 if (size > MAX_ITEM_SIZE) 672 if (size > MAX_ITEM_SIZE)
672 { 673 {
673 cont (cont_cls, key, size, GNUNET_SYSERR, _ ("Data too large")); 674 cont(cont_cls, key, size, GNUNET_SYSERR, _("Data too large"));
674 return; 675 return;
675 } 676 }
676 GNUNET_log_from ( 677 GNUNET_log_from(
677 GNUNET_ERROR_TYPE_DEBUG, 678 GNUNET_ERROR_TYPE_DEBUG,
678 "sqlite", 679 "sqlite",
679 "Storing in database block with type %u/key `%s'/priority %u/expiration in %s (%s).\n", 680 "Storing in database block with type %u/key `%s'/priority %u/expiration in %s (%s).\n",
680 type, 681 type,
681 GNUNET_h2s (key), 682 GNUNET_h2s(key),
682 priority, 683 priority,
683 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining ( 684 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_remaining(
684 expiration), 685 expiration),
685 GNUNET_YES), 686 GNUNET_YES),
686 GNUNET_STRINGS_absolute_time_to_string (expiration)); 687 GNUNET_STRINGS_absolute_time_to_string(expiration));
687 stmt = plugin->insertContent; 688 stmt = plugin->insertContent;
688 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX); 689 rvalue = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
689 if (GNUNET_OK != GNUNET_SQ_bind (stmt, params)) 690 if (GNUNET_OK != GNUNET_SQ_bind(stmt, params))
690 { 691 {
691 cont (cont_cls, key, size, GNUNET_SYSERR, NULL); 692 cont(cont_cls, key, size, GNUNET_SYSERR, NULL);
692 return; 693 return;
693 } 694 }
694 n = sqlite3_step (stmt); 695 n = sqlite3_step(stmt);
695 switch (n) 696 switch (n)
696 { 697 {
697 case SQLITE_DONE: 698 case SQLITE_DONE:
698 if (NULL != plugin->env->duc) 699 if (NULL != plugin->env->duc)
699 plugin->env->duc (plugin->env->cls, 700 plugin->env->duc(plugin->env->cls,
700 size + GNUNET_DATASTORE_ENTRY_OVERHEAD); 701 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
701 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 702 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
702 "sqlite", 703 "sqlite",
703 "Stored new entry (%u bytes)\n", 704 "Stored new entry (%u bytes)\n",
704 size + GNUNET_DATASTORE_ENTRY_OVERHEAD); 705 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
705 ret = GNUNET_OK; 706 ret = GNUNET_OK;
706 break; 707 break;
707 case SQLITE_BUSY: 708
708 GNUNET_break (0); 709 case SQLITE_BUSY:
709 LOG_SQLITE_MSG (plugin, 710 GNUNET_break(0);
710 &msg, 711 LOG_SQLITE_MSG(plugin,
711 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 712 &msg,
712 "sqlite3_step"); 713 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
713 ret = GNUNET_SYSERR; 714 "sqlite3_step");
714 break; 715 ret = GNUNET_SYSERR;
715 default: 716 break;
716 LOG_SQLITE_MSG (plugin, 717
717 &msg, 718 default:
718 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 719 LOG_SQLITE_MSG(plugin,
719 "sqlite3_step"); 720 &msg,
720 GNUNET_SQ_reset (plugin->dbh, stmt); 721 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
721 database_shutdown (plugin); 722 "sqlite3_step");
722 database_setup (plugin->env->cfg, plugin); 723 GNUNET_SQ_reset(plugin->dbh, stmt);
723 cont (cont_cls, key, size, GNUNET_SYSERR, msg); 724 database_shutdown(plugin);
724 GNUNET_free_non_null (msg); 725 database_setup(plugin->env->cfg, plugin);
725 return; 726 cont(cont_cls, key, size, GNUNET_SYSERR, msg);
726 } 727 GNUNET_free_non_null(msg);
727 GNUNET_SQ_reset (plugin->dbh, stmt); 728 return;
728 cont (cont_cls, key, size, ret, msg); 729 }
729 GNUNET_free_non_null (msg); 730 GNUNET_SQ_reset(plugin->dbh, stmt);
731 cont(cont_cls, key, size, ret, msg);
732 GNUNET_free_non_null(msg);
730} 733}
731 734
732 735
@@ -740,10 +743,10 @@ sqlite_plugin_put (void *cls,
740 * @param proc_cls closure for @a proc 743 * @param proc_cls closure for @a proc
741 */ 744 */
742static void 745static void
743execute_get (struct Plugin *plugin, 746execute_get(struct Plugin *plugin,
744 sqlite3_stmt *stmt, 747 sqlite3_stmt *stmt,
745 PluginDatumProcessor proc, 748 PluginDatumProcessor proc,
746 void *proc_cls) 749 void *proc_cls)
747{ 750{
748 int n; 751 int n;
749 struct GNUNET_TIME_Absolute expiration; 752 struct GNUNET_TIME_Absolute expiration;
@@ -757,68 +760,70 @@ execute_get (struct Plugin *plugin,
757 struct GNUNET_HashCode key; 760 struct GNUNET_HashCode key;
758 int ret; 761 int ret;
759 struct GNUNET_SQ_ResultSpec rs[] = 762 struct GNUNET_SQ_ResultSpec rs[] =
760 {GNUNET_SQ_result_spec_uint32 (&replication), 763 { GNUNET_SQ_result_spec_uint32(&replication),
761 GNUNET_SQ_result_spec_uint32 (&type), 764 GNUNET_SQ_result_spec_uint32(&type),
762 GNUNET_SQ_result_spec_uint32 (&priority), 765 GNUNET_SQ_result_spec_uint32(&priority),
763 GNUNET_SQ_result_spec_uint32 (&anonymity), 766 GNUNET_SQ_result_spec_uint32(&anonymity),
764 GNUNET_SQ_result_spec_absolute_time (&expiration), 767 GNUNET_SQ_result_spec_absolute_time(&expiration),
765 GNUNET_SQ_result_spec_auto_from_type (&key), 768 GNUNET_SQ_result_spec_auto_from_type(&key),
766 GNUNET_SQ_result_spec_variable_size (&value, &value_size), 769 GNUNET_SQ_result_spec_variable_size(&value, &value_size),
767 GNUNET_SQ_result_spec_uint64 (&rowid), 770 GNUNET_SQ_result_spec_uint64(&rowid),
768 GNUNET_SQ_result_spec_end}; 771 GNUNET_SQ_result_spec_end };
769 772
770 n = sqlite3_step (stmt); 773 n = sqlite3_step(stmt);
771 switch (n) 774 switch (n)
772 {
773 case SQLITE_ROW:
774 if (GNUNET_OK != GNUNET_SQ_extract_result (stmt, rs))
775 { 775 {
776 GNUNET_break (0); 776 case SQLITE_ROW:
777 if (GNUNET_OK != GNUNET_SQ_extract_result(stmt, rs))
778 {
779 GNUNET_break(0);
780 break;
781 }
782 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
783 "sqlite",
784 "Found reply in database with expiration %s\n",
785 GNUNET_STRINGS_absolute_time_to_string(expiration));
786 ret = proc(proc_cls,
787 &key,
788 value_size,
789 value,
790 type,
791 priority,
792 anonymity,
793 replication,
794 expiration,
795 rowid);
796 GNUNET_SQ_cleanup_result(rs);
797 GNUNET_SQ_reset(plugin->dbh, stmt);
798 if ((GNUNET_NO == ret) && (GNUNET_OK == delete_by_rowid(plugin, rowid)) &&
799 (NULL != plugin->env->duc))
800 plugin->env->duc(plugin->env->cls,
801 -(value_size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
802 return;
803
804 case SQLITE_DONE:
805 /* database must be empty */
777 break; 806 break;
807
808 case SQLITE_BUSY:
809 case SQLITE_ERROR:
810 case SQLITE_MISUSE:
811 default:
812 LOG_SQLITE(plugin,
813 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
814 "sqlite3_step");
815 if (SQLITE_OK != sqlite3_reset(stmt))
816 LOG_SQLITE(plugin,
817 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
818 "sqlite3_reset");
819 GNUNET_break(0);
820 proc(proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
821 database_shutdown(plugin);
822 database_setup(plugin->env->cfg, plugin);
823 return;
778 } 824 }
779 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 825 GNUNET_SQ_reset(plugin->dbh, stmt);
780 "sqlite", 826 proc(proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
781 "Found reply in database with expiration %s\n",
782 GNUNET_STRINGS_absolute_time_to_string (expiration));
783 ret = proc (proc_cls,
784 &key,
785 value_size,
786 value,
787 type,
788 priority,
789 anonymity,
790 replication,
791 expiration,
792 rowid);
793 GNUNET_SQ_cleanup_result (rs);
794 GNUNET_SQ_reset (plugin->dbh, stmt);
795 if ((GNUNET_NO == ret) && (GNUNET_OK == delete_by_rowid (plugin, rowid)) &&
796 (NULL != plugin->env->duc))
797 plugin->env->duc (plugin->env->cls,
798 -(value_size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
799 return;
800 case SQLITE_DONE:
801 /* database must be empty */
802 break;
803 case SQLITE_BUSY:
804 case SQLITE_ERROR:
805 case SQLITE_MISUSE:
806 default:
807 LOG_SQLITE (plugin,
808 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
809 "sqlite3_step");
810 if (SQLITE_OK != sqlite3_reset (stmt))
811 LOG_SQLITE (plugin,
812 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
813 "sqlite3_reset");
814 GNUNET_break (0);
815 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
816 database_shutdown (plugin);
817 database_setup (plugin->env->cfg, plugin);
818 return;
819 }
820 GNUNET_SQ_reset (plugin->dbh, stmt);
821 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
822} 827}
823 828
824 829
@@ -835,27 +840,27 @@ execute_get (struct Plugin *plugin,
835 * @param proc_cls closure for @a proc 840 * @param proc_cls closure for @a proc
836 */ 841 */
837static void 842static void
838sqlite_plugin_get_zero_anonymity (void *cls, 843sqlite_plugin_get_zero_anonymity(void *cls,
839 uint64_t next_uid, 844 uint64_t next_uid,
840 enum GNUNET_BLOCK_Type type, 845 enum GNUNET_BLOCK_Type type,
841 PluginDatumProcessor proc, 846 PluginDatumProcessor proc,
842 void *proc_cls) 847 void *proc_cls)
843{ 848{
844 struct Plugin *plugin = cls; 849 struct Plugin *plugin = cls;
845 uint32_t type32 = type; 850 uint32_t type32 = type;
846 struct GNUNET_SQ_QueryParam params[] = {GNUNET_SQ_query_param_uint64 ( 851 struct GNUNET_SQ_QueryParam params[] = { GNUNET_SQ_query_param_uint64(
847 &next_uid), 852 &next_uid),
848 GNUNET_SQ_query_param_uint32 ( 853 GNUNET_SQ_query_param_uint32(
849 &type32), 854 &type32),
850 GNUNET_SQ_query_param_end}; 855 GNUNET_SQ_query_param_end };
851 856
852 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY); 857 GNUNET_assert(type != GNUNET_BLOCK_TYPE_ANY);
853 if (GNUNET_OK != GNUNET_SQ_bind (plugin->selZeroAnon, params)) 858 if (GNUNET_OK != GNUNET_SQ_bind(plugin->selZeroAnon, params))
854 { 859 {
855 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 860 proc(proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
856 return; 861 return;
857 } 862 }
858 execute_get (plugin, plugin->selZeroAnon, proc, proc_cls); 863 execute_get(plugin, plugin->selZeroAnon, proc, proc_cls);
859} 864}
860 865
861 866
@@ -873,64 +878,62 @@ sqlite_plugin_get_zero_anonymity (void *cls,
873 * @param proc_cls closure for @a proc 878 * @param proc_cls closure for @a proc
874 */ 879 */
875static void 880static void
876sqlite_plugin_get_key (void *cls, 881sqlite_plugin_get_key(void *cls,
877 uint64_t next_uid, 882 uint64_t next_uid,
878 bool random, 883 bool random,
879 const struct GNUNET_HashCode *key, 884 const struct GNUNET_HashCode *key,
880 enum GNUNET_BLOCK_Type type, 885 enum GNUNET_BLOCK_Type type,
881 PluginDatumProcessor proc, 886 PluginDatumProcessor proc,
882 void *proc_cls) 887 void *proc_cls)
883{ 888{
884 struct Plugin *plugin = cls; 889 struct Plugin *plugin = cls;
885 uint64_t rvalue; 890 uint64_t rvalue;
886 int use_rvalue = random; 891 int use_rvalue = random;
887 uint32_t type32 = (uint32_t) type; 892 uint32_t type32 = (uint32_t)type;
888 int use_type = GNUNET_BLOCK_TYPE_ANY != type; 893 int use_type = GNUNET_BLOCK_TYPE_ANY != type;
889 int use_key = NULL != key; 894 int use_key = NULL != key;
890 sqlite3_stmt *stmt = plugin->get[use_rvalue * 4 + use_key * 2 + use_type]; 895 sqlite3_stmt *stmt = plugin->get[use_rvalue * 4 + use_key * 2 + use_type];
891 struct GNUNET_SQ_QueryParam params[] = 896 struct GNUNET_SQ_QueryParam params[] =
892 {GNUNET_SQ_query_param_uint64 (&next_uid), 897 { GNUNET_SQ_query_param_uint64(&next_uid),
893 GNUNET_SQ_query_param_uint64 (&rvalue), 898 GNUNET_SQ_query_param_uint64(&rvalue),
894 GNUNET_SQ_query_param_auto_from_type (key), 899 GNUNET_SQ_query_param_auto_from_type(key),
895 GNUNET_SQ_query_param_uint32 (&type32), 900 GNUNET_SQ_query_param_uint32(&type32),
896 GNUNET_SQ_query_param_end}; 901 GNUNET_SQ_query_param_end };
897 902
898 /* SQLite doesn't like it when you try to bind a parameter greater than the 903 /* SQLite doesn't like it when you try to bind a parameter greater than the
899 * last numbered parameter, but unused parameters in the middle are OK. 904 * last numbered parameter, but unused parameters in the middle are OK.
900 */ 905 */
901 if (! use_type) 906 if (!use_type)
902 {
903 params[3] = (struct GNUNET_SQ_QueryParam) GNUNET_SQ_query_param_end;
904 if (! use_key)
905 { 907 {
906 params[2] = (struct GNUNET_SQ_QueryParam) GNUNET_SQ_query_param_end; 908 params[3] = (struct GNUNET_SQ_QueryParam)GNUNET_SQ_query_param_end;
907 if (! use_rvalue) 909 if (!use_key)
908 params[1] = (struct GNUNET_SQ_QueryParam) GNUNET_SQ_query_param_end; 910 {
911 params[2] = (struct GNUNET_SQ_QueryParam)GNUNET_SQ_query_param_end;
912 if (!use_rvalue)
913 params[1] = (struct GNUNET_SQ_QueryParam)GNUNET_SQ_query_param_end;
914 }
909 } 915 }
910 }
911 if (random) 916 if (random)
912 { 917 {
913 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX); 918 rvalue = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
914 next_uid = 0; 919 next_uid = 0;
915 } 920 }
916 else 921 else
917 rvalue = 0; 922 rvalue = 0;
918 923
919 if (GNUNET_OK != GNUNET_SQ_bind (stmt, params)) 924 if (GNUNET_OK != GNUNET_SQ_bind(stmt, params))
920 { 925 {
921 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 926 proc(proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
922 return; 927 return;
923 } 928 }
924 execute_get (plugin, stmt, proc, proc_cls); 929 execute_get(plugin, stmt, proc, proc_cls);
925} 930}
926 931
927 932
928/** 933/**
929 * Context for #repl_proc() function. 934 * Context for #repl_proc() function.
930 */ 935 */
931struct ReplCtx 936struct ReplCtx {
932{
933
934 /** 937 /**
935 * Function to call for the result (or the NULL). 938 * Function to call for the result (or the NULL).
936 */ 939 */
@@ -973,37 +976,37 @@ struct ReplCtx
973 * #GNUNET_NO to delete the item 976 * #GNUNET_NO to delete the item
974 */ 977 */
975static int 978static int
976repl_proc (void *cls, 979repl_proc(void *cls,
977 const struct GNUNET_HashCode *key, 980 const struct GNUNET_HashCode *key,
978 uint32_t size, 981 uint32_t size,
979 const void *data, 982 const void *data,
980 enum GNUNET_BLOCK_Type type, 983 enum GNUNET_BLOCK_Type type,
981 uint32_t priority, 984 uint32_t priority,
982 uint32_t anonymity, 985 uint32_t anonymity,
983 uint32_t replication, 986 uint32_t replication,
984 struct GNUNET_TIME_Absolute expiration, 987 struct GNUNET_TIME_Absolute expiration,
985 uint64_t uid) 988 uint64_t uid)
986{ 989{
987 struct ReplCtx *rc = cls; 990 struct ReplCtx *rc = cls;
988 int ret; 991 int ret;
989 992
990 if (GNUNET_SYSERR == rc->have_uid) 993 if (GNUNET_SYSERR == rc->have_uid)
991 rc->have_uid = GNUNET_NO; 994 rc->have_uid = GNUNET_NO;
992 ret = rc->proc (rc->proc_cls, 995 ret = rc->proc(rc->proc_cls,
993 key, 996 key,
994 size, 997 size,
995 data, 998 data,
996 type, 999 type,
997 priority, 1000 priority,
998 anonymity, 1001 anonymity,
999 replication, 1002 replication,
1000 expiration, 1003 expiration,
1001 uid); 1004 uid);
1002 if (NULL != key) 1005 if (NULL != key)
1003 { 1006 {
1004 rc->uid = uid; 1007 rc->uid = uid;
1005 rc->have_uid = GNUNET_YES; 1008 rc->have_uid = GNUNET_YES;
1006 } 1009 }
1007 return ret; 1010 return ret;
1008} 1011}
1009 1012
@@ -1019,61 +1022,61 @@ repl_proc (void *cls,
1019 * @param proc_cls closure for @a proc 1022 * @param proc_cls closure for @a proc
1020 */ 1023 */
1021static void 1024static void
1022sqlite_plugin_get_replication (void *cls, 1025sqlite_plugin_get_replication(void *cls,
1023 PluginDatumProcessor proc, 1026 PluginDatumProcessor proc,
1024 void *proc_cls) 1027 void *proc_cls)
1025{ 1028{
1026 struct Plugin *plugin = cls; 1029 struct Plugin *plugin = cls;
1027 struct ReplCtx rc; 1030 struct ReplCtx rc;
1028 uint64_t rvalue; 1031 uint64_t rvalue;
1029 uint32_t repl; 1032 uint32_t repl;
1030 struct GNUNET_SQ_QueryParam params_sel_repl[] = 1033 struct GNUNET_SQ_QueryParam params_sel_repl[] =
1031 {GNUNET_SQ_query_param_uint64 (&rvalue), 1034 { GNUNET_SQ_query_param_uint64(&rvalue),
1032 GNUNET_SQ_query_param_uint32 (&repl), 1035 GNUNET_SQ_query_param_uint32(&repl),
1033 GNUNET_SQ_query_param_end}; 1036 GNUNET_SQ_query_param_end };
1034 struct GNUNET_SQ_QueryParam params_upd_repl[] = 1037 struct GNUNET_SQ_QueryParam params_upd_repl[] =
1035 {GNUNET_SQ_query_param_uint64 (&rc.uid), GNUNET_SQ_query_param_end}; 1038 { GNUNET_SQ_query_param_uint64(&rc.uid), GNUNET_SQ_query_param_end };
1036 1039
1037 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1040 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
1038 "datastore-sqlite", 1041 "datastore-sqlite",
1039 "Getting random block based on replication order.\n"); 1042 "Getting random block based on replication order.\n");
1040 if (SQLITE_ROW != sqlite3_step (plugin->maxRepl)) 1043 if (SQLITE_ROW != sqlite3_step(plugin->maxRepl))
1041 { 1044 {
1042 GNUNET_SQ_reset (plugin->dbh, plugin->maxRepl); 1045 GNUNET_SQ_reset(plugin->dbh, plugin->maxRepl);
1043 /* DB empty */ 1046 /* DB empty */
1044 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 1047 proc(proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1045 return; 1048 return;
1046 } 1049 }
1047 repl = sqlite3_column_int (plugin->maxRepl, 0); 1050 repl = sqlite3_column_int(plugin->maxRepl, 0);
1048 GNUNET_SQ_reset (plugin->dbh, plugin->maxRepl); 1051 GNUNET_SQ_reset(plugin->dbh, plugin->maxRepl);
1049 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX); 1052 rvalue = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
1050 if (GNUNET_OK != GNUNET_SQ_bind (plugin->selRepl, params_sel_repl)) 1053 if (GNUNET_OK != GNUNET_SQ_bind(plugin->selRepl, params_sel_repl))
1051 { 1054 {
1052 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 1055 proc(proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1053 return; 1056 return;
1054 } 1057 }
1055 rc.have_uid = GNUNET_SYSERR; 1058 rc.have_uid = GNUNET_SYSERR;
1056 rc.proc = proc; 1059 rc.proc = proc;
1057 rc.proc_cls = proc_cls; 1060 rc.proc_cls = proc_cls;
1058 execute_get (plugin, plugin->selRepl, &repl_proc, &rc); 1061 execute_get(plugin, plugin->selRepl, &repl_proc, &rc);
1059 if (GNUNET_YES == rc.have_uid) 1062 if (GNUNET_YES == rc.have_uid)
1060 {
1061 if (GNUNET_OK != GNUNET_SQ_bind (plugin->updRepl, params_upd_repl))
1062 { 1063 {
1063 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 1064 if (GNUNET_OK != GNUNET_SQ_bind(plugin->updRepl, params_upd_repl))
1064 return; 1065 {
1066 proc(proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1067 return;
1068 }
1069 if (SQLITE_DONE != sqlite3_step(plugin->updRepl))
1070 LOG_SQLITE(plugin,
1071 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1072 "sqlite3_step");
1073 GNUNET_SQ_reset(plugin->dbh, plugin->updRepl);
1065 } 1074 }
1066 if (SQLITE_DONE != sqlite3_step (plugin->updRepl))
1067 LOG_SQLITE (plugin,
1068 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1069 "sqlite3_step");
1070 GNUNET_SQ_reset (plugin->dbh, plugin->updRepl);
1071 }
1072 if (GNUNET_SYSERR == rc.have_uid) 1075 if (GNUNET_SYSERR == rc.have_uid)
1073 { 1076 {
1074 /* proc was not called at all so far, do it now. */ 1077 /* proc was not called at all so far, do it now. */
1075 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 1078 proc(proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1076 } 1079 }
1077} 1080}
1078 1081
1079 1082
@@ -1086,29 +1089,29 @@ sqlite_plugin_get_replication (void *cls,
1086 * @param proc_cls closure for @a proc 1089 * @param proc_cls closure for @a proc
1087 */ 1090 */
1088static void 1091static void
1089sqlite_plugin_get_expiration (void *cls, 1092sqlite_plugin_get_expiration(void *cls,
1090 PluginDatumProcessor proc, 1093 PluginDatumProcessor proc,
1091 void *proc_cls) 1094 void *proc_cls)
1092{ 1095{
1093 struct Plugin *plugin = cls; 1096 struct Plugin *plugin = cls;
1094 sqlite3_stmt *stmt; 1097 sqlite3_stmt *stmt;
1095 struct GNUNET_TIME_Absolute now; 1098 struct GNUNET_TIME_Absolute now;
1096 struct GNUNET_SQ_QueryParam params[] = {GNUNET_SQ_query_param_absolute_time ( 1099 struct GNUNET_SQ_QueryParam params[] = { GNUNET_SQ_query_param_absolute_time(
1097 &now), 1100 &now),
1098 GNUNET_SQ_query_param_end}; 1101 GNUNET_SQ_query_param_end };
1099 1102
1100 GNUNET_log_from ( 1103 GNUNET_log_from(
1101 GNUNET_ERROR_TYPE_DEBUG, 1104 GNUNET_ERROR_TYPE_DEBUG,
1102 "sqlite", 1105 "sqlite",
1103 "Getting random block based on expiration and priority order.\n"); 1106 "Getting random block based on expiration and priority order.\n");
1104 now = GNUNET_TIME_absolute_get (); 1107 now = GNUNET_TIME_absolute_get();
1105 stmt = plugin->selExpi; 1108 stmt = plugin->selExpi;
1106 if (GNUNET_OK != GNUNET_SQ_bind (stmt, params)) 1109 if (GNUNET_OK != GNUNET_SQ_bind(stmt, params))
1107 { 1110 {
1108 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 1111 proc(proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1109 return; 1112 return;
1110 } 1113 }
1111 execute_get (plugin, stmt, proc, proc_cls); 1114 execute_get(plugin, stmt, proc, proc_cls);
1112} 1115}
1113 1116
1114 1117
@@ -1120,35 +1123,35 @@ sqlite_plugin_get_expiration (void *cls,
1120 * @param proc_cls closure for @a proc 1123 * @param proc_cls closure for @a proc
1121 */ 1124 */
1122static void 1125static void
1123sqlite_plugin_get_keys (void *cls, PluginKeyProcessor proc, void *proc_cls) 1126sqlite_plugin_get_keys(void *cls, PluginKeyProcessor proc, void *proc_cls)
1124{ 1127{
1125 struct Plugin *plugin = cls; 1128 struct Plugin *plugin = cls;
1126 struct GNUNET_HashCode key; 1129 struct GNUNET_HashCode key;
1127 struct GNUNET_SQ_ResultSpec results[] = 1130 struct GNUNET_SQ_ResultSpec results[] =
1128 {GNUNET_SQ_result_spec_auto_from_type (&key), GNUNET_SQ_result_spec_end}; 1131 { GNUNET_SQ_result_spec_auto_from_type(&key), GNUNET_SQ_result_spec_end };
1129 sqlite3_stmt *stmt; 1132 sqlite3_stmt *stmt;
1130 int ret; 1133 int ret;
1131 1134
1132 GNUNET_assert (NULL != proc); 1135 GNUNET_assert(NULL != proc);
1133 if (SQLITE_OK != sq_prepare (plugin->dbh, "SELECT hash FROM gn091", &stmt)) 1136 if (SQLITE_OK != sq_prepare(plugin->dbh, "SELECT hash FROM gn091", &stmt))
1134 { 1137 {
1135 LOG_SQLITE (plugin, 1138 LOG_SQLITE(plugin,
1136 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 1139 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1137 "sqlite_prepare"); 1140 "sqlite_prepare");
1138 proc (proc_cls, NULL, 0); 1141 proc(proc_cls, NULL, 0);
1139 return; 1142 return;
1140 } 1143 }
1141 while (SQLITE_ROW == (ret = sqlite3_step (stmt))) 1144 while (SQLITE_ROW == (ret = sqlite3_step(stmt)))
1142 { 1145 {
1143 if (GNUNET_OK == GNUNET_SQ_extract_result (stmt, results)) 1146 if (GNUNET_OK == GNUNET_SQ_extract_result(stmt, results))
1144 proc (proc_cls, &key, 1); 1147 proc(proc_cls, &key, 1);
1145 else 1148 else
1146 GNUNET_break (0); 1149 GNUNET_break(0);
1147 } 1150 }
1148 if (SQLITE_DONE != ret) 1151 if (SQLITE_DONE != ret)
1149 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step"); 1152 LOG_SQLITE(plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
1150 sqlite3_finalize (stmt); 1153 sqlite3_finalize(stmt);
1151 proc (proc_cls, NULL, 0); 1154 proc(proc_cls, NULL, 0);
1152} 1155}
1153 1156
1154 1157
@@ -1158,7 +1161,7 @@ sqlite_plugin_get_keys (void *cls, PluginKeyProcessor proc, void *proc_cls)
1158 * @param cls our plugin context 1161 * @param cls our plugin context
1159 */ 1162 */
1160static void 1163static void
1161sqlite_plugin_drop (void *cls) 1164sqlite_plugin_drop(void *cls)
1162{ 1165{
1163 struct Plugin *plugin = cls; 1166 struct Plugin *plugin = cls;
1164 1167
@@ -1177,44 +1180,44 @@ sqlite_plugin_drop (void *cls)
1177 * @param cont_cls continuation closure for @a cont 1180 * @param cont_cls continuation closure for @a cont
1178 */ 1181 */
1179static void 1182static void
1180sqlite_plugin_remove_key (void *cls, 1183sqlite_plugin_remove_key(void *cls,
1181 const struct GNUNET_HashCode *key, 1184 const struct GNUNET_HashCode *key,
1182 uint32_t size, 1185 uint32_t size,
1183 const void *data, 1186 const void *data,
1184 PluginRemoveCont cont, 1187 PluginRemoveCont cont,
1185 void *cont_cls) 1188 void *cont_cls)
1186{ 1189{
1187 struct Plugin *plugin = cls; 1190 struct Plugin *plugin = cls;
1188 struct GNUNET_SQ_QueryParam params[] = 1191 struct GNUNET_SQ_QueryParam params[] =
1189 {GNUNET_SQ_query_param_auto_from_type (key), 1192 { GNUNET_SQ_query_param_auto_from_type(key),
1190 GNUNET_SQ_query_param_fixed_size (data, size), 1193 GNUNET_SQ_query_param_fixed_size(data, size),
1191 GNUNET_SQ_query_param_end}; 1194 GNUNET_SQ_query_param_end };
1192 1195
1193 if (GNUNET_OK != GNUNET_SQ_bind (plugin->remove, params)) 1196 if (GNUNET_OK != GNUNET_SQ_bind(plugin->remove, params))
1194 { 1197 {
1195 cont (cont_cls, key, size, GNUNET_SYSERR, "bind failed"); 1198 cont(cont_cls, key, size, GNUNET_SYSERR, "bind failed");
1196 return; 1199 return;
1197 } 1200 }
1198 if (SQLITE_DONE != sqlite3_step (plugin->remove)) 1201 if (SQLITE_DONE != sqlite3_step(plugin->remove))
1199 { 1202 {
1200 LOG_SQLITE (plugin, 1203 LOG_SQLITE(plugin,
1201 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 1204 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1202 "sqlite3_step"); 1205 "sqlite3_step");
1203 GNUNET_SQ_reset (plugin->dbh, plugin->remove); 1206 GNUNET_SQ_reset(plugin->dbh, plugin->remove);
1204 cont (cont_cls, key, size, GNUNET_SYSERR, "sqlite3_step failed"); 1207 cont(cont_cls, key, size, GNUNET_SYSERR, "sqlite3_step failed");
1205 return; 1208 return;
1206 } 1209 }
1207 int changes = sqlite3_changes (plugin->dbh); 1210 int changes = sqlite3_changes(plugin->dbh);
1208 GNUNET_SQ_reset (plugin->dbh, plugin->remove); 1211 GNUNET_SQ_reset(plugin->dbh, plugin->remove);
1209 if (0 == changes) 1212 if (0 == changes)
1210 { 1213 {
1211 cont (cont_cls, key, size, GNUNET_NO, NULL); 1214 cont(cont_cls, key, size, GNUNET_NO, NULL);
1212 return; 1215 return;
1213 } 1216 }
1214 if (NULL != plugin->env->duc) 1217 if (NULL != plugin->env->duc)
1215 plugin->env->duc (plugin->env->cls, 1218 plugin->env->duc(plugin->env->cls,
1216 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); 1219 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
1217 cont (cont_cls, key, size, GNUNET_OK, NULL); 1220 cont(cont_cls, key, size, GNUNET_OK, NULL);
1218} 1221}
1219 1222
1220 1223
@@ -1226,7 +1229,7 @@ sqlite_plugin_remove_key (void *cls,
1226 * @return the size of the database on disk (estimate) 1229 * @return the size of the database on disk (estimate)
1227 */ 1230 */
1228static void 1231static void
1229sqlite_plugin_estimate_size (void *cls, unsigned long long *estimate) 1232sqlite_plugin_estimate_size(void *cls, unsigned long long *estimate)
1230{ 1233{
1231 struct Plugin *plugin = cls; 1234 struct Plugin *plugin = cls;
1232 sqlite3_stmt *stmt; 1235 sqlite3_stmt *stmt;
@@ -1240,36 +1243,36 @@ sqlite_plugin_estimate_size (void *cls, unsigned long long *estimate)
1240 if (NULL == estimate) 1243 if (NULL == estimate)
1241 return; 1244 return;
1242 if (SQLITE_VERSION_NUMBER < 3006000) 1245 if (SQLITE_VERSION_NUMBER < 3006000)
1243 { 1246 {
1244 GNUNET_log_from ( 1247 GNUNET_log_from(
1245 GNUNET_ERROR_TYPE_WARNING, 1248 GNUNET_ERROR_TYPE_WARNING,
1246 "datastore-sqlite", 1249 "datastore-sqlite",
1247 _ ("sqlite version to old to determine size, assuming zero\n")); 1250 _("sqlite version to old to determine size, assuming zero\n"));
1248 *estimate = 0; 1251 *estimate = 0;
1249 return; 1252 return;
1250 } 1253 }
1251 CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh, "VACUUM", NULL, NULL, ENULL)); 1254 CHECK(SQLITE_OK == sqlite3_exec(plugin->dbh, "VACUUM", NULL, NULL, ENULL));
1252 CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh, 1255 CHECK(SQLITE_OK == sqlite3_exec(plugin->dbh,
1253 "PRAGMA auto_vacuum=INCREMENTAL", 1256 "PRAGMA auto_vacuum=INCREMENTAL",
1254 NULL, 1257 NULL,
1255 NULL, 1258 NULL,
1256 ENULL)); 1259 ENULL));
1257 CHECK (SQLITE_OK == sq_prepare (plugin->dbh, "PRAGMA page_count", &stmt)); 1260 CHECK(SQLITE_OK == sq_prepare(plugin->dbh, "PRAGMA page_count", &stmt));
1258 if (SQLITE_ROW == sqlite3_step (stmt)) 1261 if (SQLITE_ROW == sqlite3_step(stmt))
1259 pages = sqlite3_column_int64 (stmt, 0); 1262 pages = sqlite3_column_int64(stmt, 0);
1260 else 1263 else
1261 pages = 0; 1264 pages = 0;
1262 sqlite3_finalize (stmt); 1265 sqlite3_finalize(stmt);
1263 CHECK (SQLITE_OK == sq_prepare (plugin->dbh, "PRAGMA page_size", &stmt)); 1266 CHECK(SQLITE_OK == sq_prepare(plugin->dbh, "PRAGMA page_size", &stmt));
1264 CHECK (SQLITE_ROW == sqlite3_step (stmt)); 1267 CHECK(SQLITE_ROW == sqlite3_step(stmt));
1265 page_size = sqlite3_column_int64 (stmt, 0); 1268 page_size = sqlite3_column_int64(stmt, 0);
1266 sqlite3_finalize (stmt); 1269 sqlite3_finalize(stmt);
1267 GNUNET_log ( 1270 GNUNET_log(
1268 GNUNET_ERROR_TYPE_INFO, 1271 GNUNET_ERROR_TYPE_INFO,
1269 _ ( 1272 _(
1270 "Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"), 1273 "Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"),
1271 (unsigned long long) pages, 1274 (unsigned long long)pages,
1272 (unsigned long long) page_size); 1275 (unsigned long long)page_size);
1273 *estimate = pages * page_size; 1276 *estimate = pages * page_size;
1274} 1277}
1275 1278
@@ -1281,7 +1284,7 @@ sqlite_plugin_estimate_size (void *cls, unsigned long long *estimate)
1281 * @return NULL on error, othrewise the plugin context 1284 * @return NULL on error, othrewise the plugin context
1282 */ 1285 */
1283void * 1286void *
1284libgnunet_plugin_datastore_sqlite_init (void *cls) 1287libgnunet_plugin_datastore_sqlite_init(void *cls)
1285{ 1288{
1286 static struct Plugin plugin; 1289 static struct Plugin plugin;
1287 struct GNUNET_DATASTORE_PluginEnvironment *env = cls; 1290 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
@@ -1289,14 +1292,14 @@ libgnunet_plugin_datastore_sqlite_init (void *cls)
1289 1292
1290 if (NULL != plugin.env) 1293 if (NULL != plugin.env)
1291 return NULL; /* can only initialize once! */ 1294 return NULL; /* can only initialize once! */
1292 memset (&plugin, 0, sizeof (struct Plugin)); 1295 memset(&plugin, 0, sizeof(struct Plugin));
1293 plugin.env = env; 1296 plugin.env = env;
1294 if (GNUNET_OK != database_setup (env->cfg, &plugin)) 1297 if (GNUNET_OK != database_setup(env->cfg, &plugin))
1295 { 1298 {
1296 database_shutdown (&plugin); 1299 database_shutdown(&plugin);
1297 return NULL; 1300 return NULL;
1298 } 1301 }
1299 api = GNUNET_new (struct GNUNET_DATASTORE_PluginFunctions); 1302 api = GNUNET_new(struct GNUNET_DATASTORE_PluginFunctions);
1300 api->cls = &plugin; 1303 api->cls = &plugin;
1301 api->estimate_size = &sqlite_plugin_estimate_size; 1304 api->estimate_size = &sqlite_plugin_estimate_size;
1302 api->put = &sqlite_plugin_put; 1305 api->put = &sqlite_plugin_put;
@@ -1307,9 +1310,9 @@ libgnunet_plugin_datastore_sqlite_init (void *cls)
1307 api->get_keys = &sqlite_plugin_get_keys; 1310 api->get_keys = &sqlite_plugin_get_keys;
1308 api->drop = &sqlite_plugin_drop; 1311 api->drop = &sqlite_plugin_drop;
1309 api->remove_key = &sqlite_plugin_remove_key; 1312 api->remove_key = &sqlite_plugin_remove_key;
1310 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, 1313 GNUNET_log_from(GNUNET_ERROR_TYPE_INFO,
1311 "sqlite", 1314 "sqlite",
1312 _ ("Sqlite database running\n")); 1315 _("Sqlite database running\n"));
1313 return api; 1316 return api;
1314} 1317}
1315 1318
@@ -1321,27 +1324,27 @@ libgnunet_plugin_datastore_sqlite_init (void *cls)
1321 * @return always NULL 1324 * @return always NULL
1322 */ 1325 */
1323void * 1326void *
1324libgnunet_plugin_datastore_sqlite_done (void *cls) 1327libgnunet_plugin_datastore_sqlite_done(void *cls)
1325{ 1328{
1326 char *fn; 1329 char *fn;
1327 struct GNUNET_DATASTORE_PluginFunctions *api = cls; 1330 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1328 struct Plugin *plugin = api->cls; 1331 struct Plugin *plugin = api->cls;
1329 1332
1330 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1333 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
1331 "sqlite", 1334 "sqlite",
1332 "sqlite plugin is done\n"); 1335 "sqlite plugin is done\n");
1333 fn = NULL; 1336 fn = NULL;
1334 if (plugin->drop_on_shutdown) 1337 if (plugin->drop_on_shutdown)
1335 fn = GNUNET_strdup (plugin->fn); 1338 fn = GNUNET_strdup(plugin->fn);
1336 database_shutdown (plugin); 1339 database_shutdown(plugin);
1337 plugin->env = NULL; 1340 plugin->env = NULL;
1338 GNUNET_free (api); 1341 GNUNET_free(api);
1339 if (NULL != fn) 1342 if (NULL != fn)
1340 { 1343 {
1341 if (0 != unlink (fn)) 1344 if (0 != unlink(fn))
1342 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn); 1345 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1343 GNUNET_free (fn); 1346 GNUNET_free(fn);
1344 } 1347 }
1345 return NULL; 1348 return NULL;
1346} 1349}
1347 1350