aboutsummaryrefslogtreecommitdiff
path: root/src/datastore/plugin_datastore_sqlite.c
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-09-06 22:46:29 +0000
committerng0 <ng0@n0.is>2019-09-06 22:46:29 +0000
commit6e599264ad13e8fc105493d74d7c11d46f8739ed (patch)
tree169bef1ecbade5a659831fb169f3ae6943af127f /src/datastore/plugin_datastore_sqlite.c
parent4f13bc15113021ebf71d5d81e99bc29f8a07fc9c (diff)
downloadgnunet-6e599264ad13e8fc105493d74d7c11d46f8739ed.tar.gz
gnunet-6e599264ad13e8fc105493d74d7c11d46f8739ed.zip
first step to remove plibc
Diffstat (limited to 'src/datastore/plugin_datastore_sqlite.c')
-rw-r--r--src/datastore/plugin_datastore_sqlite.c893
1 files changed, 399 insertions, 494 deletions
diff --git a/src/datastore/plugin_datastore_sqlite.c b/src/datastore/plugin_datastore_sqlite.c
index d548935ea..4bd2b39cb 100644
--- a/src/datastore/plugin_datastore_sqlite.c
+++ b/src/datastore/plugin_datastore_sqlite.c
@@ -1,4 +1,4 @@
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 *
@@ -55,7 +55,17 @@
55 * a failure of the command 'cmd' on file 'filename' 55 * a failure of the command 'cmd' on file 'filename'
56 * with the message given by strerror(errno). 56 * with the message given by strerror(errno).
57 */ 57 */
58#define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0) 58#define LOG_SQLITE(db, level, cmd) \
59 do \
60 { \
61 GNUNET_log_from (level, \
62 "sqlite", \
63 _ ("`%s' failed at %s:%d with error: %s\n"), \
64 cmd, \
65 __FILE__, \
66 __LINE__, \
67 sqlite3_errmsg (db->dbh)); \
68 } while (0)
59 69
60 70
61/** 71/**
@@ -63,8 +73,23 @@
63 * a failure of the command 'cmd' on file 'filename' 73 * a failure of the command 'cmd' on file 'filename'
64 * with the message given by strerror(errno). 74 * with the message given by strerror(errno).
65 */ 75 */
66#define LOG_SQLITE_MSG(db, msg, level, cmd) do { GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0) 76#define LOG_SQLITE_MSG(db, msg, level, cmd) \
67 77 do \
78 { \
79 GNUNET_log_from (level, \
80 "sqlite", \
81 _ ("`%s' failed at %s:%d with error: %s\n"), \
82 cmd, \
83 __FILE__, \
84 __LINE__, \
85 sqlite3_errmsg (db->dbh)); \
86 GNUNET_asprintf (msg, \
87 _ ("`%s' failed at %s:%u with error: %s"), \
88 cmd, \
89 __FILE__, \
90 __LINE__, \
91 sqlite3_errmsg (db->dbh)); \
92 } while (0)
68 93
69 94
70/** 95/**
@@ -141,7 +166,6 @@ struct Plugin
141 * Should the database be dropped on shutdown? 166 * Should the database be dropped on shutdown?
142 */ 167 */
143 int drop_on_shutdown; 168 int drop_on_shutdown;
144
145}; 169};
146 170
147 171
@@ -154,9 +178,7 @@ struct Plugin
154 * @return 0 on success 178 * @return 0 on success
155 */ 179 */
156static int 180static int
157sq_prepare (sqlite3 *dbh, 181sq_prepare (sqlite3 *dbh, const char *zSql, sqlite3_stmt **ppStmt)
158 const char *zSql,
159 sqlite3_stmt **ppStmt)
160{ 182{
161 char *dummy; 183 char *dummy;
162 int result; 184 int result;
@@ -182,37 +204,56 @@ sq_prepare (sqlite3 *dbh,
182 * @param dbh handle to the database 204 * @param dbh handle to the database
183 */ 205 */
184static void 206static void
185create_indices (sqlite3 * dbh) 207create_indices (sqlite3 *dbh)
186{ 208{
187 /* create indices */ 209 /* create indices */
188 if (0 != 210 if (
189 (SQLITE_OK != 211 0 !=
190 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_hash ON gn091 (hash)", 212 (SQLITE_OK !=
191 NULL, NULL, NULL)) + 213 sqlite3_exec (dbh,
214 "CREATE INDEX IF NOT EXISTS idx_hash ON gn091 (hash)",
215 NULL,
216 NULL,
217 NULL)) +
192 (SQLITE_OK != 218 (SQLITE_OK !=
193 sqlite3_exec (dbh, 219 sqlite3_exec (
194 "CREATE INDEX IF NOT EXISTS idx_anon_type ON gn091 (anonLevel ASC,type)", 220 dbh,
195 NULL, NULL, NULL)) + 221 "CREATE INDEX IF NOT EXISTS idx_anon_type ON gn091 (anonLevel ASC,type)",
222 NULL,
223 NULL,
224 NULL)) +
196 (SQLITE_OK != 225 (SQLITE_OK !=
197 sqlite3_exec (dbh, 226 sqlite3_exec (dbh,
198 "CREATE INDEX IF NOT EXISTS idx_expire ON gn091 (expire ASC)", 227 "CREATE INDEX IF NOT EXISTS idx_expire ON gn091 (expire ASC)",
199 NULL, NULL, NULL)) + 228 NULL,
229 NULL,
230 NULL)) +
200 (SQLITE_OK != 231 (SQLITE_OK !=
201 sqlite3_exec (dbh, 232 sqlite3_exec (
202 "CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn091 (repl,rvalue)", 233 dbh,
203 NULL, NULL, NULL)) ) 234 "CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn091 (repl,rvalue)",
204 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "sqlite", 235 NULL,
205 "Failed to create indices: %s\n", sqlite3_errmsg (dbh)); 236 NULL,
237 NULL)))
238 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
239 "sqlite",
240 "Failed to create indices: %s\n",
241 sqlite3_errmsg (dbh));
206} 242}
207 243
208 244
209#if 0 245#if 0
210#define CHECK(a) GNUNET_break(a) 246#define CHECK(a) GNUNET_break (a)
211#define ENULL NULL 247#define ENULL NULL
212#else 248#else
213#define ENULL &e 249#define ENULL &e
214#define ENULL_DEFINED 1 250#define ENULL_DEFINED 1
215#define CHECK(a) if (! (a)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); } 251#define CHECK(a) \
252 if (! (a)) \
253 { \
254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", e); \
255 sqlite3_free (e); \
256 }
216#endif 257#endif
217 258
218 259
@@ -235,21 +276,19 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
235 char *e; 276 char *e;
236#endif 277#endif
237 278
238 if (GNUNET_OK != 279 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
239 GNUNET_CONFIGURATION_get_value_filename (cfg, 280 "datastore-sqlite",
240 "datastore-sqlite", 281 "FILENAME",
241 "FILENAME", 282 &afsdir))
242 &afsdir))
243 { 283 {
244 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 284 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
245 "datastore-sqlite", 285 "datastore-sqlite",
246 "FILENAME"); 286 "FILENAME");
247 return GNUNET_SYSERR; 287 return GNUNET_SYSERR;
248 } 288 }
249 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir)) 289 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
250 { 290 {
251 if (GNUNET_OK != 291 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
252 GNUNET_DISK_directory_create_for_file (afsdir))
253 { 292 {
254 GNUNET_break (0); 293 GNUNET_break (0);
255 GNUNET_free (afsdir); 294 GNUNET_free (afsdir);
@@ -257,48 +296,46 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
257 } 296 }
258 /* database is new or got deleted, reset payload to zero! */ 297 /* database is new or got deleted, reset payload to zero! */
259 if (NULL != plugin->env->duc) 298 if (NULL != plugin->env->duc)
260 plugin->env->duc (plugin->env->cls, 299 plugin->env->duc (plugin->env->cls, 0);
261 0);
262 } 300 }
263 /* 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 */
264 plugin->fn = afsdir; 302 plugin->fn = afsdir;
265 303
266 /* Open database and precompile statements */ 304 /* Open database and precompile statements */
267 if (SQLITE_OK != 305 if (SQLITE_OK != sqlite3_open (plugin->fn, &plugin->dbh))
268 sqlite3_open (plugin->fn, &plugin->dbh))
269 { 306 {
270 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "sqlite", 307 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
271 _("Unable to initialize SQLite: %s.\n"), 308 "sqlite",
309 _ ("Unable to initialize SQLite: %s.\n"),
272 sqlite3_errmsg (plugin->dbh)); 310 sqlite3_errmsg (plugin->dbh));
273 return GNUNET_SYSERR; 311 return GNUNET_SYSERR;
274 } 312 }
275 CHECK (SQLITE_OK == 313 CHECK (
276 sqlite3_exec (plugin->dbh, 314 SQLITE_OK ==
277 "PRAGMA temp_store=MEMORY", NULL, NULL, 315 sqlite3_exec (plugin->dbh, "PRAGMA temp_store=MEMORY", NULL, NULL, ENULL));
278 ENULL)); 316 CHECK (
279 CHECK (SQLITE_OK == 317 SQLITE_OK ==
280 sqlite3_exec (plugin->dbh, 318 sqlite3_exec (plugin->dbh, "PRAGMA synchronous=OFF", NULL, NULL, ENULL));
281 "PRAGMA synchronous=OFF", NULL, NULL, 319 CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh,
282 ENULL)); 320 "PRAGMA legacy_file_format=OFF",
283 CHECK (SQLITE_OK == 321 NULL,
284 sqlite3_exec (plugin->dbh, 322 NULL,
285 "PRAGMA legacy_file_format=OFF", NULL, NULL, 323 ENULL));
286 ENULL)); 324 CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh,
287 CHECK (SQLITE_OK == 325 "PRAGMA auto_vacuum=INCREMENTAL",
288 sqlite3_exec (plugin->dbh, 326 NULL,
289 "PRAGMA auto_vacuum=INCREMENTAL", NULL, 327 NULL,
290 NULL, ENULL)); 328 ENULL));
291 CHECK (SQLITE_OK == 329 CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh,
292 sqlite3_exec (plugin->dbh, 330 "PRAGMA locking_mode=EXCLUSIVE",
293 "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL, 331 NULL,
294 ENULL)); 332 NULL,
295 CHECK (SQLITE_OK == 333 ENULL));
296 sqlite3_exec (plugin->dbh, 334 CHECK (
297 "PRAGMA page_size=4096", NULL, NULL, 335 SQLITE_OK ==
298 ENULL)); 336 sqlite3_exec (plugin->dbh, "PRAGMA page_size=4096", NULL, NULL, ENULL));
299 337
300 CHECK (SQLITE_OK == 338 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
301 sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
302 339
303 340
304 /* 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 */
@@ -312,152 +349,135 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
312 * 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.
313 * 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.
314 */ 351 */
315 if ( (SQLITE_DONE == 352 if ((SQLITE_DONE == sqlite3_step (stmt)) &&
316 sqlite3_step (stmt)) && 353 (SQLITE_OK != sqlite3_exec (plugin->dbh,
317 (SQLITE_OK != 354 "CREATE TABLE gn091 ("
318 sqlite3_exec (plugin->dbh, 355 " repl INT4 NOT NULL DEFAULT 0,"
319 "CREATE TABLE gn091 (" 356 " type INT4 NOT NULL DEFAULT 0,"
320 " repl INT4 NOT NULL DEFAULT 0," 357 " prio INT4 NOT NULL DEFAULT 0,"
321 " type INT4 NOT NULL DEFAULT 0," 358 " anonLevel INT4 NOT NULL DEFAULT 0,"
322 " prio INT4 NOT NULL DEFAULT 0," 359 " expire INT8 NOT NULL DEFAULT 0,"
323 " anonLevel INT4 NOT NULL DEFAULT 0," 360 " rvalue INT8 NOT NULL,"
324 " expire INT8 NOT NULL DEFAULT 0," 361 " hash TEXT NOT NULL DEFAULT '',"
325 " rvalue INT8 NOT NULL," 362 " vhash TEXT NOT NULL DEFAULT '',"
326 " hash TEXT NOT NULL DEFAULT ''," 363 " value BLOB NOT NULL DEFAULT '')",
327 " vhash TEXT NOT NULL DEFAULT ''," 364 NULL,
328 " value BLOB NOT NULL DEFAULT '')", 365 NULL,
329 NULL, 366 NULL)))
330 NULL,
331 NULL)) )
332 { 367 {
333 LOG_SQLITE (plugin, 368 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
334 GNUNET_ERROR_TYPE_ERROR,
335 "sqlite3_exec");
336 sqlite3_finalize (stmt); 369 sqlite3_finalize (stmt);
337 return GNUNET_SYSERR; 370 return GNUNET_SYSERR;
338 } 371 }
339 sqlite3_finalize (stmt); 372 sqlite3_finalize (stmt);
340 create_indices (plugin->dbh); 373 create_indices (plugin->dbh);
341 374
342#define RESULT_COLUMNS "repl, type, prio, anonLevel, expire, hash, value, _ROWID_" 375#define RESULT_COLUMNS \
343 if ( (SQLITE_OK != 376 "repl, type, prio, anonLevel, expire, hash, value, _ROWID_"
344 sq_prepare (plugin->dbh, 377 if (
345 "UPDATE gn091 " 378 (SQLITE_OK != sq_prepare (plugin->dbh,
346 "SET prio = prio + ?, " 379 "UPDATE gn091 "
347 "repl = repl + ?, " 380 "SET prio = prio + ?, "
348 "expire = MAX(expire, ?) " 381 "repl = repl + ?, "
349 "WHERE hash = ? AND vhash = ?", 382 "expire = MAX(expire, ?) "
350 &plugin->update)) || 383 "WHERE hash = ? AND vhash = ?",
351 (SQLITE_OK != 384 &plugin->update)) ||
352 sq_prepare (plugin->dbh, 385 (SQLITE_OK != sq_prepare (plugin->dbh,
353 "UPDATE gn091 " "SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?", 386 "UPDATE gn091 "
354 &plugin->updRepl)) || 387 "SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?",
355 (SQLITE_OK != 388 &plugin->updRepl)) ||
356 sq_prepare (plugin->dbh, 389 (SQLITE_OK != sq_prepare (plugin->dbh,
357 "SELECT " RESULT_COLUMNS " FROM gn091 " 390 "SELECT " RESULT_COLUMNS " FROM gn091 "
358 "WHERE repl=?2 AND " " (rvalue>=?1 OR " 391 "WHERE repl=?2 AND "
359 " NOT EXISTS (SELECT 1 FROM gn091 " 392 " (rvalue>=?1 OR "
360 "WHERE repl=?2 AND rvalue>=?1 LIMIT 1) ) " 393 " NOT EXISTS (SELECT 1 FROM gn091 "
361 "ORDER BY rvalue ASC LIMIT 1", 394 "WHERE repl=?2 AND rvalue>=?1 LIMIT 1) ) "
362 &plugin->selRepl)) || 395 "ORDER BY rvalue ASC LIMIT 1",
363 (SQLITE_OK != 396 &plugin->selRepl)) ||
364 sq_prepare (plugin->dbh, 397 (SQLITE_OK != sq_prepare (plugin->dbh,
365 "SELECT MAX(repl) FROM gn091", 398 "SELECT MAX(repl) FROM gn091",
366 &plugin->maxRepl)) || 399 &plugin->maxRepl)) ||
367 (SQLITE_OK != 400 (SQLITE_OK !=
368 sq_prepare (plugin->dbh, 401 sq_prepare (plugin->dbh,
369 "SELECT " RESULT_COLUMNS " FROM gn091 " 402 "SELECT " RESULT_COLUMNS " FROM gn091 "
370 "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) "
371 "ORDER BY expire ASC LIMIT 1", 404 "ORDER BY expire ASC LIMIT 1",
372 &plugin->selExpi)) || 405 &plugin->selExpi)) ||
373 (SQLITE_OK != 406 (SQLITE_OK != sq_prepare (plugin->dbh,
374 sq_prepare (plugin->dbh, 407 "SELECT " RESULT_COLUMNS " FROM gn091 "
375 "SELECT " RESULT_COLUMNS " FROM gn091 " 408 "WHERE _ROWID_ >= ? AND "
376 "WHERE _ROWID_ >= ? AND " 409 "anonLevel = 0 AND "
377 "anonLevel = 0 AND " 410 "type = ? "
378 "type = ? " 411 "ORDER BY _ROWID_ ASC LIMIT 1",
379 "ORDER BY _ROWID_ ASC LIMIT 1", 412 &plugin->selZeroAnon)) ||
380 &plugin->selZeroAnon)) || 413 (SQLITE_OK !=
381 (SQLITE_OK != 414 sq_prepare (plugin->dbh,
382 sq_prepare (plugin->dbh, 415 "INSERT INTO gn091 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
383 "INSERT INTO gn091 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " 416 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
384 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", 417 &plugin->insertContent)) ||
385 &plugin->insertContent)) || 418 (SQLITE_OK != sq_prepare (plugin->dbh,
386 (SQLITE_OK != 419 "SELECT " RESULT_COLUMNS " FROM gn091 "
387 sq_prepare (plugin->dbh, 420 "WHERE _ROWID_ >= ?1 "
388 "SELECT " RESULT_COLUMNS " FROM gn091 " 421 "ORDER BY _ROWID_ ASC LIMIT 1",
389 "WHERE _ROWID_ >= ?1 " 422 &plugin->get[0])) ||
390 "ORDER BY _ROWID_ ASC LIMIT 1", 423 (SQLITE_OK != sq_prepare (plugin->dbh,
391 &plugin->get[0])) || 424 "SELECT " RESULT_COLUMNS " FROM gn091 "
392 (SQLITE_OK != 425 "WHERE _ROWID_ >= ?1 AND "
393 sq_prepare (plugin->dbh, 426 "type = ?4 "
394 "SELECT " RESULT_COLUMNS " FROM gn091 " 427 "ORDER BY _ROWID_ ASC LIMIT 1",
395 "WHERE _ROWID_ >= ?1 AND " 428 &plugin->get[1])) ||
396 "type = ?4 " 429 (SQLITE_OK != sq_prepare (plugin->dbh,
397 "ORDER BY _ROWID_ ASC LIMIT 1", 430 "SELECT " RESULT_COLUMNS " FROM gn091 "
398 &plugin->get[1])) || 431 "WHERE _ROWID_ >= ?1 AND "
399 (SQLITE_OK != 432 "hash = ?3 "
400 sq_prepare (plugin->dbh, 433 "ORDER BY _ROWID_ ASC LIMIT 1",
401 "SELECT " RESULT_COLUMNS " FROM gn091 " 434 &plugin->get[2])) ||
402 "WHERE _ROWID_ >= ?1 AND " 435 (SQLITE_OK != sq_prepare (plugin->dbh,
403 "hash = ?3 " 436 "SELECT " RESULT_COLUMNS " FROM gn091 "
404 "ORDER BY _ROWID_ ASC LIMIT 1", 437 "WHERE _ROWID_ >= ?1 AND "
405 &plugin->get[2])) || 438 "hash = ?3 AND "
406 (SQLITE_OK != 439 "type = ?4 "
407 sq_prepare (plugin->dbh, 440 "ORDER BY _ROWID_ ASC LIMIT 1",
408 "SELECT " RESULT_COLUMNS " FROM gn091 " 441 &plugin->get[3])) ||
409 "WHERE _ROWID_ >= ?1 AND " 442 (SQLITE_OK != sq_prepare (plugin->dbh,
410 "hash = ?3 AND " 443 "SELECT " RESULT_COLUMNS " FROM gn091 "
411 "type = ?4 " 444 "WHERE _ROWID_ >= ?1 AND "
412 "ORDER BY _ROWID_ ASC LIMIT 1", 445 "rvalue >= ?2 "
413 &plugin->get[3])) || 446 "ORDER BY _ROWID_ ASC LIMIT 1",
414 (SQLITE_OK != 447 &plugin->get[4])) ||
415 sq_prepare (plugin->dbh, 448 (SQLITE_OK != sq_prepare (plugin->dbh,
416 "SELECT " RESULT_COLUMNS " FROM gn091 " 449 "SELECT " RESULT_COLUMNS " FROM gn091 "
417 "WHERE _ROWID_ >= ?1 AND " 450 "WHERE _ROWID_ >= ?1 AND "
418 "rvalue >= ?2 " 451 "rvalue >= ?2 AND "
419 "ORDER BY _ROWID_ ASC LIMIT 1", 452 "type = ?4 "
420 &plugin->get[4])) || 453 "ORDER BY _ROWID_ ASC LIMIT 1",
421 (SQLITE_OK != 454 &plugin->get[5])) ||
422 sq_prepare (plugin->dbh, 455 (SQLITE_OK != sq_prepare (plugin->dbh,
423 "SELECT " RESULT_COLUMNS " FROM gn091 " 456 "SELECT " RESULT_COLUMNS " FROM gn091 "
424 "WHERE _ROWID_ >= ?1 AND " 457 "WHERE _ROWID_ >= ?1 AND "
425 "rvalue >= ?2 AND " 458 "rvalue >= ?2 AND "
426 "type = ?4 " 459 "hash = ?3 "
427 "ORDER BY _ROWID_ ASC LIMIT 1", 460 "ORDER BY _ROWID_ ASC LIMIT 1",
428 &plugin->get[5])) || 461 &plugin->get[6])) ||
429 (SQLITE_OK != 462 (SQLITE_OK != sq_prepare (plugin->dbh,
430 sq_prepare (plugin->dbh, 463 "SELECT " RESULT_COLUMNS " FROM gn091 "
431 "SELECT " RESULT_COLUMNS " FROM gn091 " 464 "WHERE _ROWID_ >= ?1 AND "
432 "WHERE _ROWID_ >= ?1 AND " 465 "rvalue >= ?2 AND "
433 "rvalue >= ?2 AND " 466 "hash = ?3 AND "
434 "hash = ?3 " 467 "type = ?4 "
435 "ORDER BY _ROWID_ ASC LIMIT 1", 468 "ORDER BY _ROWID_ ASC LIMIT 1",
436 &plugin->get[6])) || 469 &plugin->get[7])) ||
437 (SQLITE_OK != 470 (SQLITE_OK != sq_prepare (plugin->dbh,
438 sq_prepare (plugin->dbh, 471 "DELETE FROM gn091 WHERE _ROWID_ = ?",
439 "SELECT " RESULT_COLUMNS " FROM gn091 " 472 &plugin->delRow)) ||
440 "WHERE _ROWID_ >= ?1 AND " 473 (SQLITE_OK != sq_prepare (plugin->dbh,
441 "rvalue >= ?2 AND " 474 "DELETE FROM gn091 "
442 "hash = ?3 AND " 475 "WHERE hash = ? AND "
443 "type = ?4 " 476 "value = ? ",
444 "ORDER BY _ROWID_ ASC LIMIT 1", 477 &plugin->remove)) ||
445 &plugin->get[7])) || 478 false)
446 (SQLITE_OK !=
447 sq_prepare (plugin->dbh,
448 "DELETE FROM gn091 WHERE _ROWID_ = ?",
449 &plugin->delRow)) ||
450 (SQLITE_OK !=
451 sq_prepare (plugin->dbh,
452 "DELETE FROM gn091 "
453 "WHERE hash = ? AND "
454 "value = ? ",
455 &plugin->remove)) ||
456 false)
457 { 479 {
458 LOG_SQLITE (plugin, 480 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "precompiling");
459 GNUNET_ERROR_TYPE_ERROR,
460 "precompiling");
461 return GNUNET_SYSERR; 481 return GNUNET_SYSERR;
462 } 482 }
463 return GNUNET_OK; 483 return GNUNET_OK;
@@ -503,11 +523,12 @@ database_shutdown (struct Plugin *plugin)
503#if SQLITE_VERSION_NUMBER >= 3007000 523#if SQLITE_VERSION_NUMBER >= 3007000
504 if (result == SQLITE_BUSY) 524 if (result == SQLITE_BUSY)
505 { 525 {
506 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 526 GNUNET_log_from (
507 "sqlite", 527 GNUNET_ERROR_TYPE_WARNING,
508 _("Tried to close sqlite without finalizing all prepared statements.\n")); 528 "sqlite",
509 stmt = sqlite3_next_stmt (plugin->dbh, 529 _ (
510 NULL); 530 "Tried to close sqlite without finalizing all prepared statements.\n"));
531 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
511 while (NULL != stmt) 532 while (NULL != stmt)
512 { 533 {
513 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 534 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
@@ -521,16 +542,13 @@ database_shutdown (struct Plugin *plugin)
521 "Failed to close statement %p: %d\n", 542 "Failed to close statement %p: %d\n",
522 stmt, 543 stmt,
523 result); 544 result);
524 stmt = sqlite3_next_stmt (plugin->dbh, 545 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
525 NULL);
526 } 546 }
527 result = sqlite3_close (plugin->dbh); 547 result = sqlite3_close (plugin->dbh);
528 } 548 }
529#endif 549#endif
530 if (SQLITE_OK != result) 550 if (SQLITE_OK != result)
531 LOG_SQLITE (plugin, 551 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
532 GNUNET_ERROR_TYPE_ERROR,
533 "sqlite3_close");
534 GNUNET_free_non_null (plugin->fn); 552 GNUNET_free_non_null (plugin->fn);
535} 553}
536 554
@@ -543,28 +561,22 @@ database_shutdown (struct Plugin *plugin)
543 * @param rid the ID of the row to delete 561 * @param rid the ID of the row to delete
544 */ 562 */
545static int 563static int
546delete_by_rowid (struct Plugin *plugin, 564delete_by_rowid (struct Plugin *plugin, uint64_t rid)
547 uint64_t rid)
548{ 565{
549 struct GNUNET_SQ_QueryParam params[] = { 566 struct GNUNET_SQ_QueryParam params[] = {GNUNET_SQ_query_param_uint64 (&rid),
550 GNUNET_SQ_query_param_uint64 (&rid), 567 GNUNET_SQ_query_param_end};
551 GNUNET_SQ_query_param_end 568
552 }; 569 if (GNUNET_OK != GNUNET_SQ_bind (plugin->delRow, params))
553
554 if (GNUNET_OK !=
555 GNUNET_SQ_bind (plugin->delRow,
556 params))
557 return GNUNET_SYSERR; 570 return GNUNET_SYSERR;
558 if (SQLITE_DONE != sqlite3_step (plugin->delRow)) 571 if (SQLITE_DONE != sqlite3_step (plugin->delRow))
559 { 572 {
560 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 573 LOG_SQLITE (plugin,
574 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
561 "sqlite3_step"); 575 "sqlite3_step");
562 GNUNET_SQ_reset (plugin->dbh, 576 GNUNET_SQ_reset (plugin->dbh, plugin->delRow);
563 plugin->delRow);
564 return GNUNET_SYSERR; 577 return GNUNET_SYSERR;
565 } 578 }
566 GNUNET_SQ_reset (plugin->dbh, 579 GNUNET_SQ_reset (plugin->dbh, plugin->delRow);
567 plugin->delRow);
568 return GNUNET_OK; 580 return GNUNET_OK;
569} 581}
570 582
@@ -603,94 +615,78 @@ sqlite_plugin_put (void *cls,
603 struct GNUNET_HashCode vhash; 615 struct GNUNET_HashCode vhash;
604 char *msg = NULL; 616 char *msg = NULL;
605 617
606 GNUNET_CRYPTO_hash (data, 618 GNUNET_CRYPTO_hash (data, size, &vhash);
607 size,
608 &vhash);
609 619
610 if (!absent) 620 if (! absent)
611 { 621 {
612 struct GNUNET_SQ_QueryParam params[] = { 622 struct GNUNET_SQ_QueryParam params[] =
613 GNUNET_SQ_query_param_uint32 (&priority), 623 {GNUNET_SQ_query_param_uint32 (&priority),
614 GNUNET_SQ_query_param_uint32 (&replication), 624 GNUNET_SQ_query_param_uint32 (&replication),
615 GNUNET_SQ_query_param_absolute_time (&expiration), 625 GNUNET_SQ_query_param_absolute_time (&expiration),
616 GNUNET_SQ_query_param_auto_from_type (key), 626 GNUNET_SQ_query_param_auto_from_type (key),
617 GNUNET_SQ_query_param_auto_from_type (&vhash), 627 GNUNET_SQ_query_param_auto_from_type (&vhash),
618 GNUNET_SQ_query_param_end 628 GNUNET_SQ_query_param_end};
619 }; 629
620 630 if (GNUNET_OK != GNUNET_SQ_bind (plugin->update, params))
621 if (GNUNET_OK !=
622 GNUNET_SQ_bind (plugin->update,
623 params))
624 { 631 {
625 cont (cont_cls, 632 cont (cont_cls, key, size, GNUNET_SYSERR, _ ("sqlite bind failure"));
626 key,
627 size,
628 GNUNET_SYSERR,
629 _("sqlite bind failure"));
630 return; 633 return;
631 } 634 }
632 if (SQLITE_DONE != sqlite3_step (plugin->update)) 635 if (SQLITE_DONE != sqlite3_step (plugin->update))
633 { 636 {
634 LOG_SQLITE_MSG (plugin, &msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 637 LOG_SQLITE_MSG (plugin,
638 &msg,
639 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
635 "sqlite3_step"); 640 "sqlite3_step");
636 cont (cont_cls, 641 cont (cont_cls, key, size, GNUNET_SYSERR, msg);
637 key,
638 size,
639 GNUNET_SYSERR,
640 msg);
641 GNUNET_free_non_null (msg); 642 GNUNET_free_non_null (msg);
642 return; 643 return;
643 } 644 }
644 int changes = sqlite3_changes (plugin->dbh); 645 int changes = sqlite3_changes (plugin->dbh);
645 GNUNET_SQ_reset (plugin->dbh, 646 GNUNET_SQ_reset (plugin->dbh, plugin->update);
646 plugin->update);
647 if (0 != changes) 647 if (0 != changes)
648 { 648 {
649 cont (cont_cls, 649 cont (cont_cls, key, size, GNUNET_NO, NULL);
650 key,
651 size,
652 GNUNET_NO,
653 NULL);
654 return; 650 return;
655 } 651 }
656 } 652 }
657 653
658 uint64_t rvalue; 654 uint64_t rvalue;
659 uint32_t type32 = (uint32_t) type; 655 uint32_t type32 = (uint32_t) type;
660 struct GNUNET_SQ_QueryParam params[] = { 656 struct GNUNET_SQ_QueryParam params[] =
661 GNUNET_SQ_query_param_uint32 (&replication), 657 {GNUNET_SQ_query_param_uint32 (&replication),
662 GNUNET_SQ_query_param_uint32 (&type32), 658 GNUNET_SQ_query_param_uint32 (&type32),
663 GNUNET_SQ_query_param_uint32 (&priority), 659 GNUNET_SQ_query_param_uint32 (&priority),
664 GNUNET_SQ_query_param_uint32 (&anonymity), 660 GNUNET_SQ_query_param_uint32 (&anonymity),
665 GNUNET_SQ_query_param_absolute_time (&expiration), 661 GNUNET_SQ_query_param_absolute_time (&expiration),
666 GNUNET_SQ_query_param_uint64 (&rvalue), 662 GNUNET_SQ_query_param_uint64 (&rvalue),
667 GNUNET_SQ_query_param_auto_from_type (key), 663 GNUNET_SQ_query_param_auto_from_type (key),
668 GNUNET_SQ_query_param_auto_from_type (&vhash), 664 GNUNET_SQ_query_param_auto_from_type (&vhash),
669 GNUNET_SQ_query_param_fixed_size (data, size), 665 GNUNET_SQ_query_param_fixed_size (data, size),
670 GNUNET_SQ_query_param_end 666 GNUNET_SQ_query_param_end};
671 };
672 int n; 667 int n;
673 int ret; 668 int ret;
674 sqlite3_stmt *stmt; 669 sqlite3_stmt *stmt;
675 670
676 if (size > MAX_ITEM_SIZE) 671 if (size > MAX_ITEM_SIZE)
677 { 672 {
678 cont (cont_cls, key, size, GNUNET_SYSERR, _("Data too large")); 673 cont (cont_cls, key, size, GNUNET_SYSERR, _ ("Data too large"));
679 return; 674 return;
680 } 675 }
681 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", 676 GNUNET_log_from (
682 "Storing in database block with type %u/key `%s'/priority %u/expiration in %s (%s).\n", 677 GNUNET_ERROR_TYPE_DEBUG,
683 type, 678 "sqlite",
684 GNUNET_h2s (key), 679 "Storing in database block with type %u/key `%s'/priority %u/expiration in %s (%s).\n",
685 priority, 680 type,
686 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (expiration), 681 GNUNET_h2s (key),
687 GNUNET_YES), 682 priority,
688 GNUNET_STRINGS_absolute_time_to_string (expiration)); 683 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (
684 expiration),
685 GNUNET_YES),
686 GNUNET_STRINGS_absolute_time_to_string (expiration));
689 stmt = plugin->insertContent; 687 stmt = plugin->insertContent;
690 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX); 688 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
691 if (GNUNET_OK != 689 if (GNUNET_OK != GNUNET_SQ_bind (stmt, params))
692 GNUNET_SQ_bind (stmt,
693 params))
694 { 690 {
695 cont (cont_cls, key, size, GNUNET_SYSERR, NULL); 691 cont (cont_cls, key, size, GNUNET_SYSERR, NULL);
696 return; 692 return;
@@ -702,7 +698,8 @@ sqlite_plugin_put (void *cls,
702 if (NULL != plugin->env->duc) 698 if (NULL != plugin->env->duc)
703 plugin->env->duc (plugin->env->cls, 699 plugin->env->duc (plugin->env->cls,
704 size + GNUNET_DATASTORE_ENTRY_OVERHEAD); 700 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
705 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", 701 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
702 "sqlite",
706 "Stored new entry (%u bytes)\n", 703 "Stored new entry (%u bytes)\n",
707 size + GNUNET_DATASTORE_ENTRY_OVERHEAD); 704 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
708 ret = GNUNET_OK; 705 ret = GNUNET_OK;
@@ -710,28 +707,26 @@ sqlite_plugin_put (void *cls,
710 case SQLITE_BUSY: 707 case SQLITE_BUSY:
711 GNUNET_break (0); 708 GNUNET_break (0);
712 LOG_SQLITE_MSG (plugin, 709 LOG_SQLITE_MSG (plugin,
713 &msg, 710 &msg,
714 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 711 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
715 "sqlite3_step"); 712 "sqlite3_step");
716 ret = GNUNET_SYSERR; 713 ret = GNUNET_SYSERR;
717 break; 714 break;
718 default: 715 default:
719 LOG_SQLITE_MSG (plugin, 716 LOG_SQLITE_MSG (plugin,
720 &msg, 717 &msg,
721 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 718 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
722 "sqlite3_step"); 719 "sqlite3_step");
723 GNUNET_SQ_reset (plugin->dbh, 720 GNUNET_SQ_reset (plugin->dbh, stmt);
724 stmt);
725 database_shutdown (plugin); 721 database_shutdown (plugin);
726 database_setup (plugin->env->cfg, plugin); 722 database_setup (plugin->env->cfg, plugin);
727 cont (cont_cls, key, size, GNUNET_SYSERR, msg); 723 cont (cont_cls, key, size, GNUNET_SYSERR, msg);
728 GNUNET_free_non_null(msg); 724 GNUNET_free_non_null (msg);
729 return; 725 return;
730 } 726 }
731 GNUNET_SQ_reset (plugin->dbh, 727 GNUNET_SQ_reset (plugin->dbh, stmt);
732 stmt);
733 cont (cont_cls, key, size, ret, msg); 728 cont (cont_cls, key, size, ret, msg);
734 GNUNET_free_non_null(msg); 729 GNUNET_free_non_null (msg);
735} 730}
736 731
737 732
@@ -761,26 +756,22 @@ execute_get (struct Plugin *plugin,
761 size_t value_size; 756 size_t value_size;
762 struct GNUNET_HashCode key; 757 struct GNUNET_HashCode key;
763 int ret; 758 int ret;
764 struct GNUNET_SQ_ResultSpec rs[] = { 759 struct GNUNET_SQ_ResultSpec rs[] =
765 GNUNET_SQ_result_spec_uint32 (&replication), 760 {GNUNET_SQ_result_spec_uint32 (&replication),
766 GNUNET_SQ_result_spec_uint32 (&type), 761 GNUNET_SQ_result_spec_uint32 (&type),
767 GNUNET_SQ_result_spec_uint32 (&priority), 762 GNUNET_SQ_result_spec_uint32 (&priority),
768 GNUNET_SQ_result_spec_uint32 (&anonymity), 763 GNUNET_SQ_result_spec_uint32 (&anonymity),
769 GNUNET_SQ_result_spec_absolute_time (&expiration), 764 GNUNET_SQ_result_spec_absolute_time (&expiration),
770 GNUNET_SQ_result_spec_auto_from_type (&key), 765 GNUNET_SQ_result_spec_auto_from_type (&key),
771 GNUNET_SQ_result_spec_variable_size (&value, 766 GNUNET_SQ_result_spec_variable_size (&value, &value_size),
772 &value_size), 767 GNUNET_SQ_result_spec_uint64 (&rowid),
773 GNUNET_SQ_result_spec_uint64 (&rowid), 768 GNUNET_SQ_result_spec_end};
774 GNUNET_SQ_result_spec_end
775 };
776 769
777 n = sqlite3_step (stmt); 770 n = sqlite3_step (stmt);
778 switch (n) 771 switch (n)
779 { 772 {
780 case SQLITE_ROW: 773 case SQLITE_ROW:
781 if (GNUNET_OK != 774 if (GNUNET_OK != GNUNET_SQ_extract_result (stmt, rs))
782 GNUNET_SQ_extract_result (stmt,
783 rs))
784 { 775 {
785 GNUNET_break (0); 776 GNUNET_break (0);
786 break; 777 break;
@@ -800,12 +791,9 @@ execute_get (struct Plugin *plugin,
800 expiration, 791 expiration,
801 rowid); 792 rowid);
802 GNUNET_SQ_cleanup_result (rs); 793 GNUNET_SQ_cleanup_result (rs);
803 GNUNET_SQ_reset (plugin->dbh, 794 GNUNET_SQ_reset (plugin->dbh, stmt);
804 stmt); 795 if ((GNUNET_NO == ret) && (GNUNET_OK == delete_by_rowid (plugin, rowid)) &&
805 if ( (GNUNET_NO == ret) && 796 (NULL != plugin->env->duc))
806 (GNUNET_OK == delete_by_rowid (plugin,
807 rowid)) &&
808 (NULL != plugin->env->duc) )
809 plugin->env->duc (plugin->env->cls, 797 plugin->env->duc (plugin->env->cls,
810 -(value_size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); 798 -(value_size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
811 return; 799 return;
@@ -819,20 +807,17 @@ execute_get (struct Plugin *plugin,
819 LOG_SQLITE (plugin, 807 LOG_SQLITE (plugin,
820 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 808 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
821 "sqlite3_step"); 809 "sqlite3_step");
822 if (SQLITE_OK != 810 if (SQLITE_OK != sqlite3_reset (stmt))
823 sqlite3_reset (stmt))
824 LOG_SQLITE (plugin, 811 LOG_SQLITE (plugin,
825 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 812 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
826 "sqlite3_reset"); 813 "sqlite3_reset");
827 GNUNET_break (0); 814 GNUNET_break (0);
828 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 815 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
829 database_shutdown (plugin); 816 database_shutdown (plugin);
830 database_setup (plugin->env->cfg, 817 database_setup (plugin->env->cfg, plugin);
831 plugin);
832 return; 818 return;
833 } 819 }
834 GNUNET_SQ_reset (plugin->dbh, 820 GNUNET_SQ_reset (plugin->dbh, stmt);
835 stmt);
836 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 821 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
837} 822}
838 823
@@ -858,16 +843,14 @@ sqlite_plugin_get_zero_anonymity (void *cls,
858{ 843{
859 struct Plugin *plugin = cls; 844 struct Plugin *plugin = cls;
860 uint32_t type32 = type; 845 uint32_t type32 = type;
861 struct GNUNET_SQ_QueryParam params[] = { 846 struct GNUNET_SQ_QueryParam params[] = {GNUNET_SQ_query_param_uint64 (
862 GNUNET_SQ_query_param_uint64 (&next_uid), 847 &next_uid),
863 GNUNET_SQ_query_param_uint32 (&type32), 848 GNUNET_SQ_query_param_uint32 (
864 GNUNET_SQ_query_param_end 849 &type32),
865 }; 850 GNUNET_SQ_query_param_end};
866 851
867 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY); 852 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
868 if (GNUNET_OK != 853 if (GNUNET_OK != GNUNET_SQ_bind (plugin->selZeroAnon, params))
869 GNUNET_SQ_bind (plugin->selZeroAnon,
870 params))
871 { 854 {
872 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 855 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
873 return; 856 return;
@@ -905,13 +888,12 @@ sqlite_plugin_get_key (void *cls,
905 int use_type = GNUNET_BLOCK_TYPE_ANY != type; 888 int use_type = GNUNET_BLOCK_TYPE_ANY != type;
906 int use_key = NULL != key; 889 int use_key = NULL != key;
907 sqlite3_stmt *stmt = plugin->get[use_rvalue * 4 + use_key * 2 + use_type]; 890 sqlite3_stmt *stmt = plugin->get[use_rvalue * 4 + use_key * 2 + use_type];
908 struct GNUNET_SQ_QueryParam params[] = { 891 struct GNUNET_SQ_QueryParam params[] =
909 GNUNET_SQ_query_param_uint64 (&next_uid), 892 {GNUNET_SQ_query_param_uint64 (&next_uid),
910 GNUNET_SQ_query_param_uint64 (&rvalue), 893 GNUNET_SQ_query_param_uint64 (&rvalue),
911 GNUNET_SQ_query_param_auto_from_type (key), 894 GNUNET_SQ_query_param_auto_from_type (key),
912 GNUNET_SQ_query_param_uint32 (&type32), 895 GNUNET_SQ_query_param_uint32 (&type32),
913 GNUNET_SQ_query_param_end 896 GNUNET_SQ_query_param_end};
914 };
915 897
916 /* SQLite doesn't like it when you try to bind a parameter greater than the 898 /* SQLite doesn't like it when you try to bind a parameter greater than the
917 * last numbered parameter, but unused parameters in the middle are OK. 899 * last numbered parameter, but unused parameters in the middle are OK.
@@ -928,24 +910,18 @@ sqlite_plugin_get_key (void *cls,
928 } 910 }
929 if (random) 911 if (random)
930 { 912 {
931 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 913 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
932 UINT64_MAX);
933 next_uid = 0; 914 next_uid = 0;
934 } 915 }
935 else 916 else
936 rvalue = 0; 917 rvalue = 0;
937 918
938 if (GNUNET_OK != 919 if (GNUNET_OK != GNUNET_SQ_bind (stmt, params))
939 GNUNET_SQ_bind (stmt,
940 params))
941 { 920 {
942 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 921 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
943 return; 922 return;
944 } 923 }
945 execute_get (plugin, 924 execute_get (plugin, stmt, proc, proc_cls);
946 stmt,
947 proc,
948 proc_cls);
949} 925}
950 926
951 927
@@ -1051,37 +1027,27 @@ sqlite_plugin_get_replication (void *cls,
1051 struct ReplCtx rc; 1027 struct ReplCtx rc;
1052 uint64_t rvalue; 1028 uint64_t rvalue;
1053 uint32_t repl; 1029 uint32_t repl;
1054 struct GNUNET_SQ_QueryParam params_sel_repl[] = { 1030 struct GNUNET_SQ_QueryParam params_sel_repl[] =
1055 GNUNET_SQ_query_param_uint64 (&rvalue), 1031 {GNUNET_SQ_query_param_uint64 (&rvalue),
1056 GNUNET_SQ_query_param_uint32 (&repl), 1032 GNUNET_SQ_query_param_uint32 (&repl),
1057 GNUNET_SQ_query_param_end 1033 GNUNET_SQ_query_param_end};
1058 }; 1034 struct GNUNET_SQ_QueryParam params_upd_repl[] =
1059 struct GNUNET_SQ_QueryParam params_upd_repl[] = { 1035 {GNUNET_SQ_query_param_uint64 (&rc.uid), GNUNET_SQ_query_param_end};
1060 GNUNET_SQ_query_param_uint64 (&rc.uid),
1061 GNUNET_SQ_query_param_end
1062 };
1063 1036
1064 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1037 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1065 "datastore-sqlite", 1038 "datastore-sqlite",
1066 "Getting random block based on replication order.\n"); 1039 "Getting random block based on replication order.\n");
1067 if (SQLITE_ROW != 1040 if (SQLITE_ROW != sqlite3_step (plugin->maxRepl))
1068 sqlite3_step (plugin->maxRepl))
1069 { 1041 {
1070 GNUNET_SQ_reset (plugin->dbh, 1042 GNUNET_SQ_reset (plugin->dbh, plugin->maxRepl);
1071 plugin->maxRepl);
1072 /* DB empty */ 1043 /* DB empty */
1073 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 1044 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1074 return; 1045 return;
1075 } 1046 }
1076 repl = sqlite3_column_int (plugin->maxRepl, 1047 repl = sqlite3_column_int (plugin->maxRepl, 0);
1077 0); 1048 GNUNET_SQ_reset (plugin->dbh, plugin->maxRepl);
1078 GNUNET_SQ_reset (plugin->dbh, 1049 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
1079 plugin->maxRepl); 1050 if (GNUNET_OK != GNUNET_SQ_bind (plugin->selRepl, params_sel_repl))
1080 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1081 UINT64_MAX);
1082 if (GNUNET_OK !=
1083 GNUNET_SQ_bind (plugin->selRepl,
1084 params_sel_repl))
1085 { 1051 {
1086 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 1052 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1087 return; 1053 return;
@@ -1089,26 +1055,19 @@ sqlite_plugin_get_replication (void *cls,
1089 rc.have_uid = GNUNET_SYSERR; 1055 rc.have_uid = GNUNET_SYSERR;
1090 rc.proc = proc; 1056 rc.proc = proc;
1091 rc.proc_cls = proc_cls; 1057 rc.proc_cls = proc_cls;
1092 execute_get (plugin, 1058 execute_get (plugin, plugin->selRepl, &repl_proc, &rc);
1093 plugin->selRepl,
1094 &repl_proc,
1095 &rc);
1096 if (GNUNET_YES == rc.have_uid) 1059 if (GNUNET_YES == rc.have_uid)
1097 { 1060 {
1098 if (GNUNET_OK != 1061 if (GNUNET_OK != GNUNET_SQ_bind (plugin->updRepl, params_upd_repl))
1099 GNUNET_SQ_bind (plugin->updRepl,
1100 params_upd_repl))
1101 { 1062 {
1102 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 1063 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1103 return; 1064 return;
1104 } 1065 }
1105 if (SQLITE_DONE != 1066 if (SQLITE_DONE != sqlite3_step (plugin->updRepl))
1106 sqlite3_step (plugin->updRepl))
1107 LOG_SQLITE (plugin, 1067 LOG_SQLITE (plugin,
1108 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 1068 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1109 "sqlite3_step"); 1069 "sqlite3_step");
1110 GNUNET_SQ_reset (plugin->dbh, 1070 GNUNET_SQ_reset (plugin->dbh, plugin->updRepl);
1111 plugin->updRepl);
1112 } 1071 }
1113 if (GNUNET_SYSERR == rc.have_uid) 1072 if (GNUNET_SYSERR == rc.have_uid)
1114 { 1073 {
@@ -1127,25 +1086,24 @@ sqlite_plugin_get_replication (void *cls,
1127 * @param proc_cls closure for @a proc 1086 * @param proc_cls closure for @a proc
1128 */ 1087 */
1129static void 1088static void
1130sqlite_plugin_get_expiration (void *cls, PluginDatumProcessor proc, 1089sqlite_plugin_get_expiration (void *cls,
1090 PluginDatumProcessor proc,
1131 void *proc_cls) 1091 void *proc_cls)
1132{ 1092{
1133 struct Plugin *plugin = cls; 1093 struct Plugin *plugin = cls;
1134 sqlite3_stmt *stmt; 1094 sqlite3_stmt *stmt;
1135 struct GNUNET_TIME_Absolute now; 1095 struct GNUNET_TIME_Absolute now;
1136 struct GNUNET_SQ_QueryParam params[] = { 1096 struct GNUNET_SQ_QueryParam params[] = {GNUNET_SQ_query_param_absolute_time (
1137 GNUNET_SQ_query_param_absolute_time (&now), 1097 &now),
1138 GNUNET_SQ_query_param_end 1098 GNUNET_SQ_query_param_end};
1139 }; 1099
1140 1100 GNUNET_log_from (
1141 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1101 GNUNET_ERROR_TYPE_DEBUG,
1142 "sqlite", 1102 "sqlite",
1143 "Getting random block based on expiration and priority order.\n"); 1103 "Getting random block based on expiration and priority order.\n");
1144 now = GNUNET_TIME_absolute_get (); 1104 now = GNUNET_TIME_absolute_get ();
1145 stmt = plugin->selExpi; 1105 stmt = plugin->selExpi;
1146 if (GNUNET_OK != 1106 if (GNUNET_OK != GNUNET_SQ_bind (stmt, params))
1147 GNUNET_SQ_bind (stmt,
1148 params))
1149 { 1107 {
1150 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 1108 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1151 return; 1109 return;
@@ -1162,52 +1120,35 @@ sqlite_plugin_get_expiration (void *cls, PluginDatumProcessor proc,
1162 * @param proc_cls closure for @a proc 1120 * @param proc_cls closure for @a proc
1163 */ 1121 */
1164static void 1122static void
1165sqlite_plugin_get_keys (void *cls, 1123sqlite_plugin_get_keys (void *cls, PluginKeyProcessor proc, void *proc_cls)
1166 PluginKeyProcessor proc,
1167 void *proc_cls)
1168{ 1124{
1169 struct Plugin *plugin = cls; 1125 struct Plugin *plugin = cls;
1170 struct GNUNET_HashCode key; 1126 struct GNUNET_HashCode key;
1171 struct GNUNET_SQ_ResultSpec results[] = { 1127 struct GNUNET_SQ_ResultSpec results[] =
1172 GNUNET_SQ_result_spec_auto_from_type (&key), 1128 {GNUNET_SQ_result_spec_auto_from_type (&key), GNUNET_SQ_result_spec_end};
1173 GNUNET_SQ_result_spec_end
1174 };
1175 sqlite3_stmt *stmt; 1129 sqlite3_stmt *stmt;
1176 int ret; 1130 int ret;
1177 1131
1178 GNUNET_assert (NULL != proc); 1132 GNUNET_assert (NULL != proc);
1179 if (SQLITE_OK != 1133 if (SQLITE_OK != sq_prepare (plugin->dbh, "SELECT hash FROM gn091", &stmt))
1180 sq_prepare (plugin->dbh,
1181 "SELECT hash FROM gn091",
1182 &stmt))
1183 { 1134 {
1184 LOG_SQLITE (plugin, 1135 LOG_SQLITE (plugin,
1185 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 1136 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1186 "sqlite_prepare"); 1137 "sqlite_prepare");
1187 proc (proc_cls, 1138 proc (proc_cls, NULL, 0);
1188 NULL,
1189 0);
1190 return; 1139 return;
1191 } 1140 }
1192 while (SQLITE_ROW == (ret = sqlite3_step (stmt))) 1141 while (SQLITE_ROW == (ret = sqlite3_step (stmt)))
1193 { 1142 {
1194 if (GNUNET_OK == 1143 if (GNUNET_OK == GNUNET_SQ_extract_result (stmt, results))
1195 GNUNET_SQ_extract_result (stmt, 1144 proc (proc_cls, &key, 1);
1196 results))
1197 proc (proc_cls,
1198 &key,
1199 1);
1200 else 1145 else
1201 GNUNET_break (0); 1146 GNUNET_break (0);
1202 } 1147 }
1203 if (SQLITE_DONE != ret) 1148 if (SQLITE_DONE != ret)
1204 LOG_SQLITE (plugin, 1149 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
1205 GNUNET_ERROR_TYPE_ERROR,
1206 "sqlite_step");
1207 sqlite3_finalize (stmt); 1150 sqlite3_finalize (stmt);
1208 proc (proc_cls, 1151 proc (proc_cls, NULL, 0);
1209 NULL,
1210 0);
1211} 1152}
1212 1153
1213 1154
@@ -1244,21 +1185,14 @@ sqlite_plugin_remove_key (void *cls,
1244 void *cont_cls) 1185 void *cont_cls)
1245{ 1186{
1246 struct Plugin *plugin = cls; 1187 struct Plugin *plugin = cls;
1247 struct GNUNET_SQ_QueryParam params[] = { 1188 struct GNUNET_SQ_QueryParam params[] =
1248 GNUNET_SQ_query_param_auto_from_type (key), 1189 {GNUNET_SQ_query_param_auto_from_type (key),
1249 GNUNET_SQ_query_param_fixed_size (data, size), 1190 GNUNET_SQ_query_param_fixed_size (data, size),
1250 GNUNET_SQ_query_param_end 1191 GNUNET_SQ_query_param_end};
1251 }; 1192
1252 1193 if (GNUNET_OK != GNUNET_SQ_bind (plugin->remove, params))
1253 if (GNUNET_OK !=
1254 GNUNET_SQ_bind (plugin->remove,
1255 params))
1256 { 1194 {
1257 cont (cont_cls, 1195 cont (cont_cls, key, size, GNUNET_SYSERR, "bind failed");
1258 key,
1259 size,
1260 GNUNET_SYSERR,
1261 "bind failed");
1262 return; 1196 return;
1263 } 1197 }
1264 if (SQLITE_DONE != sqlite3_step (plugin->remove)) 1198 if (SQLITE_DONE != sqlite3_step (plugin->remove))
@@ -1266,35 +1200,21 @@ sqlite_plugin_remove_key (void *cls,
1266 LOG_SQLITE (plugin, 1200 LOG_SQLITE (plugin,
1267 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 1201 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1268 "sqlite3_step"); 1202 "sqlite3_step");
1269 GNUNET_SQ_reset (plugin->dbh, 1203 GNUNET_SQ_reset (plugin->dbh, plugin->remove);
1270 plugin->remove); 1204 cont (cont_cls, key, size, GNUNET_SYSERR, "sqlite3_step failed");
1271 cont (cont_cls,
1272 key,
1273 size,
1274 GNUNET_SYSERR,
1275 "sqlite3_step failed");
1276 return; 1205 return;
1277 } 1206 }
1278 int changes = sqlite3_changes (plugin->dbh); 1207 int changes = sqlite3_changes (plugin->dbh);
1279 GNUNET_SQ_reset (plugin->dbh, 1208 GNUNET_SQ_reset (plugin->dbh, plugin->remove);
1280 plugin->remove);
1281 if (0 == changes) 1209 if (0 == changes)
1282 { 1210 {
1283 cont (cont_cls, 1211 cont (cont_cls, key, size, GNUNET_NO, NULL);
1284 key,
1285 size,
1286 GNUNET_NO,
1287 NULL);
1288 return; 1212 return;
1289 } 1213 }
1290 if (NULL != plugin->env->duc) 1214 if (NULL != plugin->env->duc)
1291 plugin->env->duc (plugin->env->cls, 1215 plugin->env->duc (plugin->env->cls,
1292 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); 1216 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
1293 cont (cont_cls, 1217 cont (cont_cls, key, size, GNUNET_OK, NULL);
1294 key,
1295 size,
1296 GNUNET_OK,
1297 NULL);
1298} 1218}
1299 1219
1300 1220
@@ -1306,8 +1226,7 @@ sqlite_plugin_remove_key (void *cls,
1306 * @return the size of the database on disk (estimate) 1226 * @return the size of the database on disk (estimate)
1307 */ 1227 */
1308static void 1228static void
1309sqlite_plugin_estimate_size (void *cls, 1229sqlite_plugin_estimate_size (void *cls, unsigned long long *estimate)
1310 unsigned long long *estimate)
1311{ 1230{
1312 struct Plugin *plugin = cls; 1231 struct Plugin *plugin = cls;
1313 sqlite3_stmt *stmt; 1232 sqlite3_stmt *stmt;
@@ -1322,45 +1241,35 @@ sqlite_plugin_estimate_size (void *cls,
1322 return; 1241 return;
1323 if (SQLITE_VERSION_NUMBER < 3006000) 1242 if (SQLITE_VERSION_NUMBER < 3006000)
1324 { 1243 {
1325 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 1244 GNUNET_log_from (
1326 "datastore-sqlite", 1245 GNUNET_ERROR_TYPE_WARNING,
1327 _("sqlite version to old to determine size, assuming zero\n")); 1246 "datastore-sqlite",
1247 _ ("sqlite version to old to determine size, assuming zero\n"));
1328 *estimate = 0; 1248 *estimate = 0;
1329 return; 1249 return;
1330 } 1250 }
1331 CHECK (SQLITE_OK == 1251 CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh, "VACUUM", NULL, NULL, ENULL));
1332 sqlite3_exec (plugin->dbh, 1252 CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh,
1333 "VACUUM", 1253 "PRAGMA auto_vacuum=INCREMENTAL",
1334 NULL, 1254 NULL,
1335 NULL, 1255 NULL,
1336 ENULL)); 1256 ENULL));
1337 CHECK (SQLITE_OK == 1257 CHECK (SQLITE_OK == sq_prepare (plugin->dbh, "PRAGMA page_count", &stmt));
1338 sqlite3_exec (plugin->dbh,
1339 "PRAGMA auto_vacuum=INCREMENTAL",
1340 NULL,
1341 NULL, ENULL));
1342 CHECK (SQLITE_OK ==
1343 sq_prepare (plugin->dbh,
1344 "PRAGMA page_count",
1345 &stmt));
1346 if (SQLITE_ROW == sqlite3_step (stmt)) 1258 if (SQLITE_ROW == sqlite3_step (stmt))
1347 pages = sqlite3_column_int64 (stmt, 1259 pages = sqlite3_column_int64 (stmt, 0);
1348 0);
1349 else 1260 else
1350 pages = 0; 1261 pages = 0;
1351 sqlite3_finalize (stmt); 1262 sqlite3_finalize (stmt);
1352 CHECK (SQLITE_OK == 1263 CHECK (SQLITE_OK == sq_prepare (plugin->dbh, "PRAGMA page_size", &stmt));
1353 sq_prepare (plugin->dbh, 1264 CHECK (SQLITE_ROW == sqlite3_step (stmt));
1354 "PRAGMA page_size",
1355 &stmt));
1356 CHECK (SQLITE_ROW ==
1357 sqlite3_step (stmt));
1358 page_size = sqlite3_column_int64 (stmt, 0); 1265 page_size = sqlite3_column_int64 (stmt, 0);
1359 sqlite3_finalize (stmt); 1266 sqlite3_finalize (stmt);
1360 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1267 GNUNET_log (
1361 _("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"), 1268 GNUNET_ERROR_TYPE_INFO,
1362 (unsigned long long) pages, 1269 _ (
1363 (unsigned long long) page_size); 1270 "Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"),
1271 (unsigned long long) pages,
1272 (unsigned long long) page_size);
1364 *estimate = pages * page_size; 1273 *estimate = pages * page_size;
1365} 1274}
1366 1275
@@ -1379,10 +1288,8 @@ libgnunet_plugin_datastore_sqlite_init (void *cls)
1379 struct GNUNET_DATASTORE_PluginFunctions *api; 1288 struct GNUNET_DATASTORE_PluginFunctions *api;
1380 1289
1381 if (NULL != plugin.env) 1290 if (NULL != plugin.env)
1382 return NULL; /* can only initialize once! */ 1291 return NULL; /* can only initialize once! */
1383 memset (&plugin, 1292 memset (&plugin, 0, sizeof (struct Plugin));
1384 0,
1385 sizeof (struct Plugin));
1386 plugin.env = env; 1293 plugin.env = env;
1387 if (GNUNET_OK != database_setup (env->cfg, &plugin)) 1294 if (GNUNET_OK != database_setup (env->cfg, &plugin))
1388 { 1295 {
@@ -1402,7 +1309,7 @@ libgnunet_plugin_datastore_sqlite_init (void *cls)
1402 api->remove_key = &sqlite_plugin_remove_key; 1309 api->remove_key = &sqlite_plugin_remove_key;
1403 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, 1310 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1404 "sqlite", 1311 "sqlite",
1405 _("Sqlite database running\n")); 1312 _ ("Sqlite database running\n"));
1406 return api; 1313 return api;
1407} 1314}
1408 1315
@@ -1431,10 +1338,8 @@ libgnunet_plugin_datastore_sqlite_done (void *cls)
1431 GNUNET_free (api); 1338 GNUNET_free (api);
1432 if (NULL != fn) 1339 if (NULL != fn)
1433 { 1340 {
1434 if (0 != UNLINK (fn)) 1341 if (0 != unlink (fn))
1435 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 1342 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1436 "unlink",
1437 fn);
1438 GNUNET_free (fn); 1343 GNUNET_free (fn);
1439 } 1344 }
1440 return NULL; 1345 return NULL;