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