diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-03-10 22:13:18 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-03-10 22:13:27 +0100 |
commit | cd3d159df3150d46a6de7d39bcefb16f03ed5d3b (patch) | |
tree | 41081437d7f27b1abe5568f4f65202bec3f2f4e6 /src | |
parent | 87798f3ef0f7db096f6ce0a2cb4145dd3a857941 (diff) | |
download | gnunet-cd3d159df3150d46a6de7d39bcefb16f03ed5d3b.tar.gz gnunet-cd3d159df3150d46a6de7d39bcefb16f03ed5d3b.zip |
use libgnunetsq throughout plugin_datastore_sqlite, fix situation where continuation was not called if no data was ready for replication
Diffstat (limited to 'src')
-rw-r--r-- | src/datastore/plugin_datastore_sqlite.c | 676 |
1 files changed, 449 insertions, 227 deletions
diff --git a/src/datastore/plugin_datastore_sqlite.c b/src/datastore/plugin_datastore_sqlite.c index 9c67d242e..4e65f9d14 100644 --- a/src/datastore/plugin_datastore_sqlite.c +++ b/src/datastore/plugin_datastore_sqlite.c | |||
@@ -128,6 +128,46 @@ struct Plugin | |||
128 | sqlite3_stmt *insertContent; | 128 | sqlite3_stmt *insertContent; |
129 | 129 | ||
130 | /** | 130 | /** |
131 | * Precompiled SQL for selection | ||
132 | */ | ||
133 | sqlite3_stmt *count_key; | ||
134 | |||
135 | /** | ||
136 | * Precompiled SQL for selection | ||
137 | */ | ||
138 | sqlite3_stmt *count_key_vhash; | ||
139 | |||
140 | /** | ||
141 | * Precompiled SQL for selection | ||
142 | */ | ||
143 | sqlite3_stmt *count_key_type; | ||
144 | |||
145 | /** | ||
146 | * Precompiled SQL for selection | ||
147 | */ | ||
148 | sqlite3_stmt *count_key_vhash_type; | ||
149 | |||
150 | /** | ||
151 | * Precompiled SQL for selection | ||
152 | */ | ||
153 | sqlite3_stmt *get_key; | ||
154 | |||
155 | /** | ||
156 | * Precompiled SQL for selection | ||
157 | */ | ||
158 | sqlite3_stmt *get_key_vhash; | ||
159 | |||
160 | /** | ||
161 | * Precompiled SQL for selection | ||
162 | */ | ||
163 | sqlite3_stmt *get_key_type; | ||
164 | |||
165 | /** | ||
166 | * Precompiled SQL for selection | ||
167 | */ | ||
168 | sqlite3_stmt *get_key_vhash_type; | ||
169 | |||
170 | /** | ||
131 | * Should the database be dropped on shutdown? | 171 | * Should the database be dropped on shutdown? |
132 | */ | 172 | */ |
133 | int drop_on_shutdown; | 173 | int drop_on_shutdown; |
@@ -151,11 +191,17 @@ sq_prepare (sqlite3 *dbh, | |||
151 | char *dummy; | 191 | char *dummy; |
152 | int result; | 192 | int result; |
153 | 193 | ||
154 | result = | 194 | result = sqlite3_prepare_v2 (dbh, |
155 | sqlite3_prepare_v2 (dbh, zSql, strlen (zSql), ppStmt, | 195 | zSql, |
156 | (const char **) &dummy); | 196 | strlen (zSql), |
157 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", | 197 | ppStmt, |
158 | "Prepared `%s' / %p: %d\n", zSql, *ppStmt, result); | 198 | (const char **) &dummy); |
199 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
200 | "sqlite", | ||
201 | "Prepared `%s' / %p: %d\n", | ||
202 | zSql, | ||
203 | *ppStmt, | ||
204 | result); | ||
159 | return result; | 205 | return result; |
160 | } | 206 | } |
161 | 207 | ||
@@ -311,80 +357,134 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
311 | * we do math or inequality tests, so we can't handle the entire range of uint32_t. | 357 | * we do math or inequality tests, so we can't handle the entire range of uint32_t. |
312 | * This will also cause problems for expiration times after 294247-01-10-04:00:54 UTC. | 358 | * This will also cause problems for expiration times after 294247-01-10-04:00:54 UTC. |
313 | */ | 359 | */ |
314 | if ((sqlite3_step (stmt) == SQLITE_DONE) && | 360 | if ( (SQLITE_DONE == |
315 | (sqlite3_exec | 361 | sqlite3_step (stmt)) && |
316 | (plugin->dbh, | 362 | (SQLITE_OK != |
317 | "CREATE TABLE gn090 (" " repl INT4 NOT NULL DEFAULT 0," | 363 | sqlite3_exec (plugin->dbh, |
318 | " type INT4 NOT NULL DEFAULT 0," " prio INT4 NOT NULL DEFAULT 0," | 364 | "CREATE TABLE gn090 (" " repl INT4 NOT NULL DEFAULT 0," |
319 | " anonLevel INT4 NOT NULL DEFAULT 0," | 365 | " type INT4 NOT NULL DEFAULT 0," " prio INT4 NOT NULL DEFAULT 0," |
320 | " expire INT8 NOT NULL DEFAULT 0," " rvalue INT8 NOT NULL," | 366 | " anonLevel INT4 NOT NULL DEFAULT 0," |
321 | " hash TEXT NOT NULL DEFAULT ''," " vhash TEXT NOT NULL DEFAULT ''," | 367 | " expire INT8 NOT NULL DEFAULT 0," " rvalue INT8 NOT NULL," |
322 | " value BLOB NOT NULL DEFAULT '')", NULL, NULL, NULL) != SQLITE_OK)) | 368 | " hash TEXT NOT NULL DEFAULT ''," " vhash TEXT NOT NULL DEFAULT ''," |
369 | " value BLOB NOT NULL DEFAULT '')", | ||
370 | NULL, | ||
371 | NULL, | ||
372 | NULL)) ) | ||
323 | { | 373 | { |
324 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec"); | 374 | LOG_SQLITE (plugin, |
375 | GNUNET_ERROR_TYPE_ERROR, | ||
376 | "sqlite3_exec"); | ||
325 | sqlite3_finalize (stmt); | 377 | sqlite3_finalize (stmt); |
326 | return GNUNET_SYSERR; | 378 | return GNUNET_SYSERR; |
327 | } | 379 | } |
328 | sqlite3_finalize (stmt); | 380 | sqlite3_finalize (stmt); |
329 | create_indices (plugin->dbh); | 381 | create_indices (plugin->dbh); |
330 | 382 | ||
331 | if ((sq_prepare | 383 | if ( (SQLITE_OK != |
332 | (plugin->dbh, | 384 | sq_prepare (plugin->dbh, |
333 | "UPDATE gn090 " | 385 | "UPDATE gn090 " |
334 | "SET prio = prio + ?, expire = MAX(expire,?) WHERE _ROWID_ = ?", | 386 | "SET prio = prio + ?, expire = MAX(expire,?) WHERE _ROWID_ = ?", |
335 | &plugin->updPrio) != SQLITE_OK) || | 387 | &plugin->updPrio)) || |
336 | (sq_prepare | 388 | (SQLITE_OK != |
337 | (plugin->dbh, | 389 | sq_prepare (plugin->dbh, |
338 | "UPDATE gn090 " "SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?", | 390 | "UPDATE gn090 " "SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?", |
339 | &plugin->updRepl) != SQLITE_OK) || | 391 | &plugin->updRepl)) || |
340 | (sq_prepare | 392 | (SQLITE_OK != |
341 | (plugin->dbh, | 393 | sq_prepare (plugin->dbh, |
342 | "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ " "FROM gn090 " | 394 | "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ " "FROM gn090 " |
343 | #if SQLITE_VERSION_NUMBER >= 3007000 | 395 | #if SQLITE_VERSION_NUMBER >= 3007000 |
344 | "INDEXED BY idx_repl_rvalue " | 396 | "INDEXED BY idx_repl_rvalue " |
345 | #endif | 397 | #endif |
346 | "WHERE repl=?2 AND " " (rvalue>=?1 OR " | 398 | "WHERE repl=?2 AND " " (rvalue>=?1 OR " |
347 | " NOT EXISTS (SELECT 1 FROM gn090 " | 399 | " NOT EXISTS (SELECT 1 FROM gn090 " |
348 | #if SQLITE_VERSION_NUMBER >= 3007000 | 400 | #if SQLITE_VERSION_NUMBER >= 3007000 |
349 | "INDEXED BY idx_repl_rvalue " | 401 | "INDEXED BY idx_repl_rvalue " |
350 | #endif | 402 | #endif |
351 | "WHERE repl=?2 AND rvalue>=?1 LIMIT 1) ) " | 403 | "WHERE repl=?2 AND rvalue>=?1 LIMIT 1) ) " |
352 | "ORDER BY rvalue ASC LIMIT 1", &plugin->selRepl) != SQLITE_OK) || | 404 | "ORDER BY rvalue ASC LIMIT 1", |
353 | (sq_prepare (plugin->dbh, "SELECT MAX(repl) FROM gn090" | 405 | &plugin->selRepl)) || |
406 | (SQLITE_OK != | ||
407 | sq_prepare (plugin->dbh, | ||
408 | "SELECT MAX(repl) FROM gn090" | ||
354 | #if SQLITE_VERSION_NUMBER >= 3007000 | 409 | #if SQLITE_VERSION_NUMBER >= 3007000 |
355 | " INDEXED BY idx_repl_rvalue" | 410 | " INDEXED BY idx_repl_rvalue" |
356 | #endif | 411 | #endif |
357 | "", &plugin->maxRepl) != SQLITE_OK) || | 412 | "", |
358 | (sq_prepare | 413 | &plugin->maxRepl)) || |
359 | (plugin->dbh, | 414 | (SQLITE_OK != |
360 | "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ " "FROM gn090 " | 415 | sq_prepare (plugin->dbh, |
416 | "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ " "FROM gn090 " | ||
361 | #if SQLITE_VERSION_NUMBER >= 3007000 | 417 | #if SQLITE_VERSION_NUMBER >= 3007000 |
362 | "INDEXED BY idx_expire " | 418 | "INDEXED BY idx_expire " |
363 | #endif | 419 | #endif |
364 | "WHERE NOT EXISTS (SELECT 1 FROM gn090 WHERE expire < ?1 LIMIT 1) OR (expire < ?1) " | 420 | "WHERE NOT EXISTS (SELECT 1 FROM gn090 WHERE expire < ?1 LIMIT 1) OR (expire < ?1) " |
365 | "ORDER BY expire ASC LIMIT 1", &plugin->selExpi) != SQLITE_OK) || | 421 | "ORDER BY expire ASC LIMIT 1", |
366 | (sq_prepare | 422 | &plugin->selExpi)) || |
367 | (plugin->dbh, | 423 | (SQLITE_OK != |
368 | "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ " "FROM gn090 " | 424 | sq_prepare (plugin->dbh, |
425 | "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ " "FROM gn090 " | ||
369 | #if SQLITE_VERSION_NUMBER >= 3007000 | 426 | #if SQLITE_VERSION_NUMBER >= 3007000 |
370 | "INDEXED BY idx_anon_type_hash " | 427 | "INDEXED BY idx_anon_type_hash " |
371 | #endif | 428 | #endif |
372 | "WHERE (anonLevel = 0 AND type=?1) " | 429 | "WHERE (anonLevel = 0 AND type=?1) " |
373 | "ORDER BY hash DESC LIMIT 1 OFFSET ?2", | 430 | "ORDER BY hash DESC LIMIT 1 OFFSET ?2", |
374 | &plugin->selZeroAnon) != SQLITE_OK) || | 431 | &plugin->selZeroAnon)) || |
375 | (sq_prepare | 432 | (SQLITE_OK != |
376 | (plugin->dbh, | 433 | sq_prepare (plugin->dbh, |
377 | "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " | 434 | "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " |
378 | "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", | 435 | "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", |
379 | &plugin->insertContent) != SQLITE_OK) || | 436 | &plugin->insertContent)) || |
380 | (sq_prepare | 437 | (SQLITE_OK != |
381 | (plugin->dbh, "DELETE FROM gn090 WHERE _ROWID_ = ?", | 438 | sq_prepare (plugin->dbh, |
382 | &plugin->delRow) != SQLITE_OK)) | 439 | "SELECT count(*) FROM gn090 WHERE hash=?", |
440 | &plugin->count_key)) || | ||
441 | (SQLITE_OK != | ||
442 | sq_prepare (plugin->dbh, | ||
443 | "SELECT count(*) FROM gn090 WHERE hash=? AND vhash=?", | ||
444 | &plugin->count_key_vhash)) || | ||
445 | (SQLITE_OK != | ||
446 | sq_prepare (plugin->dbh, | ||
447 | "SELECT count(*) FROM gn090 WHERE hash=? AND type=?", | ||
448 | &plugin->count_key_type)) || | ||
449 | (SQLITE_OK != | ||
450 | sq_prepare (plugin->dbh, | ||
451 | "SELECT count(*) FROM gn090 WHERE hash=? AND vhash=? AND type=?", | ||
452 | &plugin->count_key_vhash_type)) || | ||
453 | (SQLITE_OK != | ||
454 | sq_prepare (plugin->dbh, | ||
455 | "SELECT type, prio, anonLevel, expire, hash, value, _ROWID_ FROM gn090 " | ||
456 | "WHERE hash=?" | ||
457 | "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET ?", | ||
458 | &plugin->get_key)) || | ||
459 | (SQLITE_OK != | ||
460 | sq_prepare (plugin->dbh, | ||
461 | "SELECT type, prio, anonLevel, expire, hash, value, _ROWID_ FROM gn090 " | ||
462 | "WHERE hash=? AND vhash=?" | ||
463 | "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET ?", | ||
464 | &plugin->get_key_vhash)) || | ||
465 | (SQLITE_OK != | ||
466 | sq_prepare (plugin->dbh, | ||
467 | "SELECT type, prio, anonLevel, expire, hash, value, _ROWID_ FROM gn090 " | ||
468 | "WHERE hash=? AND type=?" | ||
469 | "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET ?", | ||
470 | &plugin->get_key_type)) || | ||
471 | (SQLITE_OK != | ||
472 | sq_prepare (plugin->dbh, | ||
473 | "SELECT type, prio, anonLevel, expire, hash, value, _ROWID_ FROM gn090 " | ||
474 | "WHERE hash=? AND vhash=? AND type=?" | ||
475 | "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET ?", | ||
476 | &plugin->get_key_vhash_type)) || | ||
477 | (SQLITE_OK != | ||
478 | sq_prepare (plugin->dbh, | ||
479 | "DELETE FROM gn090 WHERE _ROWID_ = ?", | ||
480 | &plugin->delRow)) | ||
481 | ) | ||
383 | { | 482 | { |
384 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "precompiling"); | 483 | LOG_SQLITE (plugin, |
484 | GNUNET_ERROR_TYPE_ERROR, | ||
485 | "precompiling"); | ||
385 | return GNUNET_SYSERR; | 486 | return GNUNET_SYSERR; |
386 | } | 487 | } |
387 | |||
388 | return GNUNET_OK; | 488 | return GNUNET_OK; |
389 | } | 489 | } |
390 | 490 | ||
@@ -399,51 +499,74 @@ static void | |||
399 | database_shutdown (struct Plugin *plugin) | 499 | database_shutdown (struct Plugin *plugin) |
400 | { | 500 | { |
401 | int result; | 501 | int result; |
402 | |||
403 | #if SQLITE_VERSION_NUMBER >= 3007000 | 502 | #if SQLITE_VERSION_NUMBER >= 3007000 |
404 | sqlite3_stmt *stmt; | 503 | sqlite3_stmt *stmt; |
405 | #endif | 504 | #endif |
406 | 505 | ||
407 | if (plugin->delRow != NULL) | 506 | if (NULL != plugin->delRow) |
408 | sqlite3_finalize (plugin->delRow); | 507 | sqlite3_finalize (plugin->delRow); |
409 | if (plugin->updPrio != NULL) | 508 | if (NULL != plugin->updPrio) |
410 | sqlite3_finalize (plugin->updPrio); | 509 | sqlite3_finalize (plugin->updPrio); |
411 | if (plugin->updRepl != NULL) | 510 | if (NULL != plugin->updRepl) |
412 | sqlite3_finalize (plugin->updRepl); | 511 | sqlite3_finalize (plugin->updRepl); |
413 | if (plugin->selRepl != NULL) | 512 | if (NULL != plugin->selRepl) |
414 | sqlite3_finalize (plugin->selRepl); | 513 | sqlite3_finalize (plugin->selRepl); |
415 | if (plugin->maxRepl != NULL) | 514 | if (NULL != plugin->maxRepl) |
416 | sqlite3_finalize (plugin->maxRepl); | 515 | sqlite3_finalize (plugin->maxRepl); |
417 | if (plugin->selExpi != NULL) | 516 | if (NULL != plugin->selExpi) |
418 | sqlite3_finalize (plugin->selExpi); | 517 | sqlite3_finalize (plugin->selExpi); |
419 | if (plugin->selZeroAnon != NULL) | 518 | if (NULL != plugin->selZeroAnon) |
420 | sqlite3_finalize (plugin->selZeroAnon); | 519 | sqlite3_finalize (plugin->selZeroAnon); |
421 | if (plugin->insertContent != NULL) | 520 | if (NULL != plugin->insertContent) |
422 | sqlite3_finalize (plugin->insertContent); | 521 | sqlite3_finalize (plugin->insertContent); |
522 | if (NULL != plugin->count_key) | ||
523 | sqlite3_finalize (plugin->count_key); | ||
524 | if (NULL != plugin->count_key_vhash) | ||
525 | sqlite3_finalize (plugin->count_key_vhash); | ||
526 | if (NULL != plugin->count_key_type) | ||
527 | sqlite3_finalize (plugin->count_key_type); | ||
528 | if (NULL != plugin->count_key_vhash_type) | ||
529 | sqlite3_finalize (plugin->count_key_vhash_type); | ||
530 | if (NULL != plugin->count_key) | ||
531 | sqlite3_finalize (plugin->get_key); | ||
532 | if (NULL != plugin->count_key_vhash) | ||
533 | sqlite3_finalize (plugin->get_key_vhash); | ||
534 | if (NULL != plugin->count_key_type) | ||
535 | sqlite3_finalize (plugin->get_key_type); | ||
536 | if (NULL != plugin->count_key_vhash_type) | ||
537 | sqlite3_finalize (plugin->get_key_vhash_type); | ||
423 | result = sqlite3_close (plugin->dbh); | 538 | result = sqlite3_close (plugin->dbh); |
424 | #if SQLITE_VERSION_NUMBER >= 3007000 | 539 | #if SQLITE_VERSION_NUMBER >= 3007000 |
425 | if (result == SQLITE_BUSY) | 540 | if (result == SQLITE_BUSY) |
426 | { | 541 | { |
427 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "sqlite", | 542 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, |
428 | _ | 543 | "sqlite", |
429 | ("Tried to close sqlite without finalizing all prepared statements.\n")); | 544 | _("Tried to close sqlite without finalizing all prepared statements.\n")); |
430 | stmt = sqlite3_next_stmt (plugin->dbh, NULL); | 545 | stmt = sqlite3_next_stmt (plugin->dbh, |
431 | while (stmt != NULL) | 546 | NULL); |
547 | while (NULL != stmt) | ||
432 | { | 548 | { |
433 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", | 549 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
434 | "Closing statement %p\n", stmt); | 550 | "sqlite", |
551 | "Closing statement %p\n", | ||
552 | stmt); | ||
435 | result = sqlite3_finalize (stmt); | 553 | result = sqlite3_finalize (stmt); |
436 | if (result != SQLITE_OK) | 554 | if (result != SQLITE_OK) |
437 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "sqlite", | 555 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, |
438 | "Failed to close statement %p: %d\n", stmt, result); | 556 | "sqlite", |
439 | stmt = sqlite3_next_stmt (plugin->dbh, NULL); | 557 | "Failed to close statement %p: %d\n", |
558 | stmt, | ||
559 | result); | ||
560 | stmt = sqlite3_next_stmt (plugin->dbh, | ||
561 | NULL); | ||
440 | } | 562 | } |
441 | result = sqlite3_close (plugin->dbh); | 563 | result = sqlite3_close (plugin->dbh); |
442 | } | 564 | } |
443 | #endif | 565 | #endif |
444 | if (SQLITE_OK != result) | 566 | if (SQLITE_OK != result) |
445 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close"); | 567 | LOG_SQLITE (plugin, |
446 | 568 | GNUNET_ERROR_TYPE_ERROR, | |
569 | "sqlite3_close"); | ||
447 | GNUNET_free_non_null (plugin->fn); | 570 | GNUNET_free_non_null (plugin->fn); |
448 | } | 571 | } |
449 | 572 | ||
@@ -515,9 +638,10 @@ sqlite_plugin_put (void *cls, | |||
515 | { | 638 | { |
516 | uint64_t rvalue; | 639 | uint64_t rvalue; |
517 | struct GNUNET_HashCode vhash; | 640 | struct GNUNET_HashCode vhash; |
641 | uint32_t type32 = (uint32_t) type; | ||
518 | struct GNUNET_SQ_QueryParam params[] = { | 642 | struct GNUNET_SQ_QueryParam params[] = { |
519 | GNUNET_SQ_query_param_uint32 (&replication), | 643 | GNUNET_SQ_query_param_uint32 (&replication), |
520 | GNUNET_SQ_query_param_uint32 (&type), | 644 | GNUNET_SQ_query_param_uint32 (&type32), |
521 | GNUNET_SQ_query_param_uint32 (&priority), | 645 | GNUNET_SQ_query_param_uint32 (&priority), |
522 | GNUNET_SQ_query_param_uint32 (&anonymity), | 646 | GNUNET_SQ_query_param_uint32 (&anonymity), |
523 | GNUNET_SQ_query_param_absolute_time (&expiration), | 647 | GNUNET_SQ_query_param_absolute_time (&expiration), |
@@ -687,74 +811,88 @@ execute_get (struct Plugin *plugin, | |||
687 | { | 811 | { |
688 | int n; | 812 | int n; |
689 | struct GNUNET_TIME_Absolute expiration; | 813 | struct GNUNET_TIME_Absolute expiration; |
690 | unsigned long long rowid; | 814 | uint32_t type; |
691 | unsigned int size; | 815 | uint32_t priority; |
816 | uint32_t anonymity; | ||
817 | uint64_t rowid; | ||
818 | void *value; | ||
819 | size_t value_size; | ||
820 | struct GNUNET_HashCode key; | ||
692 | int ret; | 821 | int ret; |
822 | struct GNUNET_SQ_ResultSpec rs[] = { | ||
823 | GNUNET_SQ_result_spec_uint32 (&type), | ||
824 | GNUNET_SQ_result_spec_uint32 (&priority), | ||
825 | GNUNET_SQ_result_spec_uint32 (&anonymity), | ||
826 | GNUNET_SQ_result_spec_absolute_time (&expiration), | ||
827 | GNUNET_SQ_result_spec_auto_from_type (&key), | ||
828 | GNUNET_SQ_result_spec_variable_size (&value, | ||
829 | &value_size), | ||
830 | GNUNET_SQ_result_spec_uint64 (&rowid), | ||
831 | GNUNET_SQ_result_spec_end | ||
832 | }; | ||
693 | 833 | ||
694 | n = sqlite3_step (stmt); | 834 | n = sqlite3_step (stmt); |
695 | switch (n) | 835 | switch (n) |
696 | { | 836 | { |
697 | case SQLITE_ROW: | 837 | case SQLITE_ROW: |
698 | size = sqlite3_column_bytes (stmt, 5); | 838 | if (GNUNET_OK != |
699 | rowid = sqlite3_column_int64 (stmt, 6); | 839 | GNUNET_SQ_extract_result (stmt, |
700 | if (sqlite3_column_bytes (stmt, 4) != sizeof (struct GNUNET_HashCode)) | 840 | rs)) |
701 | { | 841 | { |
702 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "sqlite", | 842 | GNUNET_break (0); |
703 | _("Invalid data in database. Trying to fix (by deletion).\n")); | ||
704 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
705 | LOG_SQLITE (plugin, | ||
706 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
707 | "sqlite3_reset"); | ||
708 | if ( (GNUNET_OK == delete_by_rowid (plugin, rowid)) && | ||
709 | (NULL != plugin->env->duc) ) | ||
710 | plugin->env->duc (plugin->env->cls, | ||
711 | -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); | ||
712 | break; | 843 | break; |
713 | } | 844 | } |
714 | expiration.abs_value_us = sqlite3_column_int64 (stmt, 3); | 845 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
715 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", | 846 | "sqlite", |
716 | "Found reply in database with expiration %s\n", | 847 | "Found reply in database with expiration %s\n", |
717 | GNUNET_STRINGS_absolute_time_to_string (expiration)); | 848 | GNUNET_STRINGS_absolute_time_to_string (expiration)); |
718 | ret = proc (proc_cls, sqlite3_column_blob (stmt, 4) /* key */ , | 849 | ret = proc (proc_cls, |
719 | size, sqlite3_column_blob (stmt, 5) /* data */ , | 850 | &key, |
720 | sqlite3_column_int (stmt, 0) /* type */ , | 851 | value_size, |
721 | sqlite3_column_int (stmt, 1) /* priority */ , | 852 | value, |
722 | sqlite3_column_int (stmt, 2) /* anonymity */ , | 853 | type, |
723 | expiration, rowid); | 854 | priority, |
724 | if (SQLITE_OK != sqlite3_reset (stmt)) | 855 | anonymity, |
856 | expiration, | ||
857 | rowid); | ||
858 | if (SQLITE_OK != | ||
859 | sqlite3_reset (stmt)) | ||
725 | LOG_SQLITE (plugin, | 860 | LOG_SQLITE (plugin, |
726 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 861 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
727 | "sqlite3_reset"); | 862 | "sqlite3_reset"); |
728 | if ( (GNUNET_NO == ret) && | 863 | if ( (GNUNET_NO == ret) && |
729 | (GNUNET_OK == delete_by_rowid (plugin, rowid)) && | 864 | (GNUNET_OK == delete_by_rowid (plugin, |
865 | rowid)) && | ||
730 | (NULL != plugin->env->duc) ) | 866 | (NULL != plugin->env->duc) ) |
731 | plugin->env->duc (plugin->env->cls, | 867 | plugin->env->duc (plugin->env->cls, |
732 | -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); | 868 | -(value_size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); |
733 | return; | 869 | return; |
734 | case SQLITE_DONE: | 870 | case SQLITE_DONE: |
735 | /* database must be empty */ | 871 | /* database must be empty */ |
736 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
737 | LOG_SQLITE (plugin, | ||
738 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
739 | "sqlite3_reset"); | ||
740 | break; | 872 | break; |
741 | case SQLITE_BUSY: | 873 | case SQLITE_BUSY: |
742 | case SQLITE_ERROR: | 874 | case SQLITE_ERROR: |
743 | case SQLITE_MISUSE: | 875 | case SQLITE_MISUSE: |
744 | default: | 876 | default: |
745 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 877 | LOG_SQLITE (plugin, |
878 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
746 | "sqlite3_step"); | 879 | "sqlite3_step"); |
747 | if (SQLITE_OK != sqlite3_reset (stmt)) | 880 | if (SQLITE_OK != |
881 | sqlite3_reset (stmt)) | ||
748 | LOG_SQLITE (plugin, | 882 | LOG_SQLITE (plugin, |
749 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 883 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
750 | "sqlite3_reset"); | 884 | "sqlite3_reset"); |
751 | GNUNET_break (0); | 885 | GNUNET_break (0); |
886 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | ||
752 | database_shutdown (plugin); | 887 | database_shutdown (plugin); |
753 | database_setup (plugin->env->cfg, plugin); | 888 | database_setup (plugin->env->cfg, |
754 | break; | 889 | plugin); |
890 | return; | ||
755 | } | 891 | } |
756 | if (SQLITE_OK != sqlite3_reset (stmt)) | 892 | if (SQLITE_OK != |
757 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 893 | sqlite3_reset (stmt)) |
894 | LOG_SQLITE (plugin, | ||
895 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
758 | "sqlite3_reset"); | 896 | "sqlite3_reset"); |
759 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | 897 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
760 | } | 898 | } |
@@ -774,7 +912,8 @@ execute_get (struct Plugin *plugin, | |||
774 | * @param proc_cls closure for @a proc | 912 | * @param proc_cls closure for @a proc |
775 | */ | 913 | */ |
776 | static void | 914 | static void |
777 | sqlite_plugin_get_zero_anonymity (void *cls, uint64_t offset, | 915 | sqlite_plugin_get_zero_anonymity (void *cls, |
916 | uint64_t offset, | ||
778 | enum GNUNET_BLOCK_Type type, | 917 | enum GNUNET_BLOCK_Type type, |
779 | PluginDatumProcessor proc, | 918 | PluginDatumProcessor proc, |
780 | void *proc_cls) | 919 | void *proc_cls) |
@@ -826,97 +965,130 @@ sqlite_plugin_get_key (void *cls, | |||
826 | void *proc_cls) | 965 | void *proc_cls) |
827 | { | 966 | { |
828 | struct Plugin *plugin = cls; | 967 | struct Plugin *plugin = cls; |
968 | uint32_t type32 = (uint32_t) type; | ||
829 | int ret; | 969 | int ret; |
830 | int total; | 970 | int total; |
831 | int limit_off; | 971 | uint32_t limit_off; |
832 | unsigned int sqoff; | 972 | struct GNUNET_SQ_QueryParam count_params_key[] = { |
833 | sqlite3_stmt *stmt; | 973 | GNUNET_SQ_query_param_auto_from_type (key), |
834 | char scratch[256]; | 974 | GNUNET_SQ_query_param_end |
835 | 975 | }; | |
836 | GNUNET_assert (proc != NULL); | 976 | struct GNUNET_SQ_QueryParam count_params_key_vhash[] = { |
837 | GNUNET_assert (key != NULL); | 977 | GNUNET_SQ_query_param_auto_from_type (key), |
838 | GNUNET_snprintf (scratch, sizeof (scratch), | 978 | GNUNET_SQ_query_param_auto_from_type (vhash), |
839 | "SELECT count(*) FROM gn090 WHERE hash=?%s%s", | 979 | GNUNET_SQ_query_param_end |
840 | vhash == NULL ? "" : " AND vhash=?", | 980 | }; |
841 | type == 0 ? "" : " AND type=?"); | 981 | struct GNUNET_SQ_QueryParam count_params_key_type[] = { |
842 | if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK) | 982 | GNUNET_SQ_query_param_auto_from_type (key), |
983 | GNUNET_SQ_query_param_uint32 (&type32), | ||
984 | GNUNET_SQ_query_param_end | ||
985 | }; | ||
986 | struct GNUNET_SQ_QueryParam count_params_key_vhash_type[] = { | ||
987 | GNUNET_SQ_query_param_auto_from_type (key), | ||
988 | GNUNET_SQ_query_param_auto_from_type (vhash), | ||
989 | GNUNET_SQ_query_param_uint32 (&type32), | ||
990 | GNUNET_SQ_query_param_end | ||
991 | }; | ||
992 | struct GNUNET_SQ_QueryParam get_params_key[] = { | ||
993 | GNUNET_SQ_query_param_auto_from_type (key), | ||
994 | GNUNET_SQ_query_param_uint32 (&limit_off), | ||
995 | GNUNET_SQ_query_param_end | ||
996 | }; | ||
997 | struct GNUNET_SQ_QueryParam get_params_key_vhash[] = { | ||
998 | GNUNET_SQ_query_param_auto_from_type (key), | ||
999 | GNUNET_SQ_query_param_auto_from_type (vhash), | ||
1000 | GNUNET_SQ_query_param_uint32 (&limit_off), | ||
1001 | GNUNET_SQ_query_param_end | ||
1002 | }; | ||
1003 | struct GNUNET_SQ_QueryParam get_params_key_type[] = { | ||
1004 | GNUNET_SQ_query_param_auto_from_type (key), | ||
1005 | GNUNET_SQ_query_param_uint32 (&type32), | ||
1006 | GNUNET_SQ_query_param_uint32 (&limit_off), | ||
1007 | GNUNET_SQ_query_param_end | ||
1008 | }; | ||
1009 | struct GNUNET_SQ_QueryParam get_params_key_vhash_type[] = { | ||
1010 | GNUNET_SQ_query_param_auto_from_type (key), | ||
1011 | GNUNET_SQ_query_param_auto_from_type (vhash), | ||
1012 | GNUNET_SQ_query_param_uint32 (&type32), | ||
1013 | GNUNET_SQ_query_param_uint32 (&limit_off), | ||
1014 | GNUNET_SQ_query_param_end | ||
1015 | }; | ||
1016 | struct GNUNET_SQ_QueryParam *count_params; | ||
1017 | sqlite3_stmt *count_stmt; | ||
1018 | struct GNUNET_SQ_QueryParam *get_params; | ||
1019 | sqlite3_stmt *get_stmt; | ||
1020 | |||
1021 | if (NULL == vhash) | ||
843 | { | 1022 | { |
844 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 1023 | if (GNUNET_BLOCK_TYPE_ANY == type) |
845 | "sqlite_prepare"); | 1024 | { |
846 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | 1025 | count_params = count_params_key; |
847 | return; | 1026 | count_stmt = plugin->count_key; |
1027 | get_params = get_params_key; | ||
1028 | get_stmt = plugin->get_key; | ||
1029 | } | ||
1030 | else | ||
1031 | { | ||
1032 | count_params = count_params_key_type; | ||
1033 | count_stmt = plugin->count_key_type; | ||
1034 | get_params = get_params_key_type; | ||
1035 | get_stmt = plugin->get_key_type; | ||
1036 | } | ||
848 | } | 1037 | } |
849 | sqoff = 1; | 1038 | else |
850 | ret = | 1039 | { |
851 | sqlite3_bind_blob (stmt, sqoff++, key, sizeof (struct GNUNET_HashCode), | 1040 | if (GNUNET_BLOCK_TYPE_ANY == type) |
852 | SQLITE_TRANSIENT); | 1041 | { |
853 | if ((vhash != NULL) && (ret == SQLITE_OK)) | 1042 | count_params = count_params_key_vhash; |
854 | ret = | 1043 | count_stmt = plugin->count_key_vhash; |
855 | sqlite3_bind_blob (stmt, sqoff++, vhash, sizeof (struct GNUNET_HashCode), | 1044 | get_params = get_params_key_vhash; |
856 | SQLITE_TRANSIENT); | 1045 | get_stmt = plugin->get_key_vhash; |
857 | if ((type != 0) && (ret == SQLITE_OK)) | 1046 | } |
858 | ret = sqlite3_bind_int (stmt, sqoff++, type); | 1047 | else |
859 | if (SQLITE_OK != ret) | 1048 | { |
1049 | count_params = count_params_key_vhash_type; | ||
1050 | count_stmt = plugin->count_key_vhash_type; | ||
1051 | get_params = get_params_key_vhash_type; | ||
1052 | get_stmt = plugin->get_key_vhash_type; | ||
1053 | } | ||
1054 | } | ||
1055 | if (GNUNET_OK != | ||
1056 | GNUNET_SQ_bind (count_stmt, | ||
1057 | count_params)) | ||
860 | { | 1058 | { |
861 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_bind"); | ||
862 | sqlite3_finalize (stmt); | ||
863 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | 1059 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
864 | return; | 1060 | return; |
865 | } | 1061 | } |
866 | ret = sqlite3_step (stmt); | 1062 | ret = sqlite3_step (count_stmt); |
867 | if (ret != SQLITE_ROW) | 1063 | if (ret != SQLITE_ROW) |
868 | { | 1064 | { |
869 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 1065 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
870 | "sqlite_step"); | 1066 | "sqlite_step"); |
871 | sqlite3_finalize (stmt); | 1067 | sqlite3_reset (count_stmt); |
872 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | 1068 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
873 | return; | 1069 | return; |
874 | } | 1070 | } |
875 | total = sqlite3_column_int (stmt, 0); | 1071 | total = sqlite3_column_int (count_stmt, |
876 | sqlite3_finalize (stmt); | 1072 | 0); |
1073 | sqlite3_reset (count_stmt); | ||
877 | if (0 == total) | 1074 | if (0 == total) |
878 | { | 1075 | { |
879 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | 1076 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
880 | return; | 1077 | return; |
881 | } | 1078 | } |
882 | limit_off = (int) (offset % total); | 1079 | limit_off = (uint32_t) (offset % total); |
883 | if (limit_off < 0) | 1080 | if (GNUNET_OK != |
884 | limit_off += total; | 1081 | GNUNET_SQ_bind (get_stmt, |
885 | GNUNET_snprintf (scratch, sizeof (scratch), | 1082 | get_params)) |
886 | "SELECT type, prio, anonLevel, expire, hash, value, _ROWID_ " | ||
887 | "FROM gn090 WHERE hash=?%s%s " | ||
888 | "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET ?", | ||
889 | vhash == NULL ? "" : " AND vhash=?", | ||
890 | type == 0 ? "" : " AND type=?"); | ||
891 | if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK) | ||
892 | { | ||
893 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
894 | "sqlite_prepare"); | ||
895 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | ||
896 | return; | ||
897 | } | ||
898 | sqoff = 1; | ||
899 | ret = sqlite3_bind_blob (stmt, sqoff++, key, | ||
900 | sizeof (struct GNUNET_HashCode), | ||
901 | SQLITE_TRANSIENT); | ||
902 | if ((vhash != NULL) && (ret == SQLITE_OK)) | ||
903 | ret = sqlite3_bind_blob (stmt, sqoff++, vhash, | ||
904 | sizeof (struct GNUNET_HashCode), | ||
905 | SQLITE_TRANSIENT); | ||
906 | if ((type != 0) && (ret == SQLITE_OK)) | ||
907 | ret = sqlite3_bind_int (stmt, sqoff++, type); | ||
908 | if (ret == SQLITE_OK) | ||
909 | ret = sqlite3_bind_int64 (stmt, sqoff++, limit_off); | ||
910 | if (ret != SQLITE_OK) | ||
911 | { | 1083 | { |
912 | LOG_SQLITE (plugin, | ||
913 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
914 | "sqlite_bind"); | ||
915 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | 1084 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
916 | return; | 1085 | return; |
917 | } | 1086 | } |
918 | execute_get (plugin, stmt, proc, proc_cls); | 1087 | execute_get (plugin, |
919 | sqlite3_finalize (stmt); | 1088 | get_stmt, |
1089 | proc, | ||
1090 | proc_cls); | ||
1091 | sqlite3_reset (get_stmt); | ||
920 | } | 1092 | } |
921 | 1093 | ||
922 | 1094 | ||
@@ -980,13 +1152,17 @@ repl_proc (void *cls, | |||
980 | struct ReplCtx *rc = cls; | 1152 | struct ReplCtx *rc = cls; |
981 | int ret; | 1153 | int ret; |
982 | 1154 | ||
1155 | if (GNUNET_SYSERR == rc->have_uid) | ||
1156 | rc->have_uid = GNUNET_NO; | ||
983 | ret = rc->proc (rc->proc_cls, | 1157 | ret = rc->proc (rc->proc_cls, |
984 | key, | 1158 | key, |
985 | size, data, | 1159 | size, |
1160 | data, | ||
986 | type, | 1161 | type, |
987 | priority, | 1162 | priority, |
988 | anonymity, | 1163 | anonymity, |
989 | expiration, uid); | 1164 | expiration, |
1165 | uid); | ||
990 | if (NULL != key) | 1166 | if (NULL != key) |
991 | { | 1167 | { |
992 | rc->uid = uid; | 1168 | rc->uid = uid; |
@@ -1007,7 +1183,8 @@ repl_proc (void *cls, | |||
1007 | * @param proc_cls closure for @a proc | 1183 | * @param proc_cls closure for @a proc |
1008 | */ | 1184 | */ |
1009 | static void | 1185 | static void |
1010 | sqlite_plugin_get_replication (void *cls, PluginDatumProcessor proc, | 1186 | sqlite_plugin_get_replication (void *cls, |
1187 | PluginDatumProcessor proc, | ||
1011 | void *proc_cls) | 1188 | void *proc_cls) |
1012 | { | 1189 | { |
1013 | struct Plugin *plugin = cls; | 1190 | struct Plugin *plugin = cls; |
@@ -1027,12 +1204,11 @@ sqlite_plugin_get_replication (void *cls, PluginDatumProcessor proc, | |||
1027 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 1204 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
1028 | "datastore-sqlite", | 1205 | "datastore-sqlite", |
1029 | "Getting random block based on replication order.\n"); | 1206 | "Getting random block based on replication order.\n"); |
1030 | rc.have_uid = GNUNET_NO; | 1207 | if (SQLITE_ROW != |
1031 | rc.proc = proc; | 1208 | sqlite3_step (plugin->maxRepl)) |
1032 | rc.proc_cls = proc_cls; | ||
1033 | if (SQLITE_ROW != sqlite3_step (plugin->maxRepl)) | ||
1034 | { | 1209 | { |
1035 | if (SQLITE_OK != sqlite3_reset (plugin->maxRepl)) | 1210 | if (SQLITE_OK != |
1211 | sqlite3_reset (plugin->maxRepl)) | ||
1036 | LOG_SQLITE (plugin, | 1212 | LOG_SQLITE (plugin, |
1037 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 1213 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
1038 | "sqlite3_reset"); | 1214 | "sqlite3_reset"); |
@@ -1040,11 +1216,15 @@ sqlite_plugin_get_replication (void *cls, PluginDatumProcessor proc, | |||
1040 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | 1216 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
1041 | return; | 1217 | return; |
1042 | } | 1218 | } |
1043 | repl = sqlite3_column_int (plugin->maxRepl, 0); | 1219 | repl = sqlite3_column_int (plugin->maxRepl, |
1044 | if (SQLITE_OK != sqlite3_reset (plugin->maxRepl)) | 1220 | 0); |
1045 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 1221 | if (SQLITE_OK != |
1222 | sqlite3_reset (plugin->maxRepl)) | ||
1223 | LOG_SQLITE (plugin, | ||
1224 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
1046 | "sqlite3_reset"); | 1225 | "sqlite3_reset"); |
1047 | rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX); | 1226 | rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, |
1227 | UINT64_MAX); | ||
1048 | if (GNUNET_OK != | 1228 | if (GNUNET_OK != |
1049 | GNUNET_SQ_bind (plugin->selRepl, | 1229 | GNUNET_SQ_bind (plugin->selRepl, |
1050 | params_sel_repl)) | 1230 | params_sel_repl)) |
@@ -1052,7 +1232,13 @@ sqlite_plugin_get_replication (void *cls, PluginDatumProcessor proc, | |||
1052 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | 1232 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
1053 | return; | 1233 | return; |
1054 | } | 1234 | } |
1055 | execute_get (plugin, plugin->selRepl, &repl_proc, &rc); | 1235 | rc.have_uid = GNUNET_SYSERR; |
1236 | rc.proc = proc; | ||
1237 | rc.proc_cls = proc_cls; | ||
1238 | execute_get (plugin, | ||
1239 | plugin->selRepl, | ||
1240 | &repl_proc, | ||
1241 | &rc); | ||
1056 | if (GNUNET_YES == rc.have_uid) | 1242 | if (GNUNET_YES == rc.have_uid) |
1057 | { | 1243 | { |
1058 | if (GNUNET_OK != | 1244 | if (GNUNET_OK != |
@@ -1062,15 +1248,22 @@ sqlite_plugin_get_replication (void *cls, PluginDatumProcessor proc, | |||
1062 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | 1248 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
1063 | return; | 1249 | return; |
1064 | } | 1250 | } |
1065 | if (SQLITE_DONE != sqlite3_step (plugin->updRepl)) | 1251 | if (SQLITE_DONE != |
1252 | sqlite3_step (plugin->updRepl)) | ||
1066 | LOG_SQLITE (plugin, | 1253 | LOG_SQLITE (plugin, |
1067 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 1254 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
1068 | "sqlite3_step"); | 1255 | "sqlite3_step"); |
1069 | if (SQLITE_OK != sqlite3_reset (plugin->updRepl)) | 1256 | if (SQLITE_OK != |
1257 | sqlite3_reset (plugin->updRepl)) | ||
1070 | LOG_SQLITE (plugin, | 1258 | LOG_SQLITE (plugin, |
1071 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 1259 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
1072 | "sqlite3_reset"); | 1260 | "sqlite3_reset"); |
1073 | } | 1261 | } |
1262 | if (GNUNET_SYSERR == rc.have_uid) | ||
1263 | { | ||
1264 | /* proc was not called at all so far, do it now. */ | ||
1265 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | ||
1266 | } | ||
1074 | } | 1267 | } |
1075 | 1268 | ||
1076 | 1269 | ||
@@ -1094,7 +1287,8 @@ sqlite_plugin_get_expiration (void *cls, PluginDatumProcessor proc, | |||
1094 | GNUNET_SQ_query_param_end | 1287 | GNUNET_SQ_query_param_end |
1095 | }; | 1288 | }; |
1096 | 1289 | ||
1097 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", | 1290 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
1291 | "sqlite", | ||
1098 | "Getting random block based on expiration and priority order.\n"); | 1292 | "Getting random block based on expiration and priority order.\n"); |
1099 | now = GNUNET_TIME_absolute_get (); | 1293 | now = GNUNET_TIME_absolute_get (); |
1100 | stmt = plugin->selExpi; | 1294 | stmt = plugin->selExpi; |
@@ -1131,11 +1325,17 @@ sqlite_plugin_get_keys (void *cls, | |||
1131 | int ret; | 1325 | int ret; |
1132 | 1326 | ||
1133 | GNUNET_assert (NULL != proc); | 1327 | GNUNET_assert (NULL != proc); |
1134 | if (sq_prepare (plugin->dbh, "SELECT hash FROM gn090", &stmt) != SQLITE_OK) | 1328 | if (SQLITE_OK != |
1329 | sq_prepare (plugin->dbh, | ||
1330 | "SELECT hash FROM gn090", | ||
1331 | &stmt)) | ||
1135 | { | 1332 | { |
1136 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 1333 | LOG_SQLITE (plugin, |
1334 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
1137 | "sqlite_prepare"); | 1335 | "sqlite_prepare"); |
1138 | proc (proc_cls, NULL, 0); | 1336 | proc (proc_cls, |
1337 | NULL, | ||
1338 | 0); | ||
1139 | return; | 1339 | return; |
1140 | } | 1340 | } |
1141 | while (SQLITE_ROW == (ret = sqlite3_step (stmt))) | 1341 | while (SQLITE_ROW == (ret = sqlite3_step (stmt))) |
@@ -1143,14 +1343,20 @@ sqlite_plugin_get_keys (void *cls, | |||
1143 | if (GNUNET_OK == | 1343 | if (GNUNET_OK == |
1144 | GNUNET_SQ_extract_result (stmt, | 1344 | GNUNET_SQ_extract_result (stmt, |
1145 | results)) | 1345 | results)) |
1146 | proc (proc_cls, &key, 1); | 1346 | proc (proc_cls, |
1347 | &key, | ||
1348 | 1); | ||
1147 | else | 1349 | else |
1148 | GNUNET_break (0); | 1350 | GNUNET_break (0); |
1149 | } | 1351 | } |
1150 | if (SQLITE_DONE != ret) | 1352 | if (SQLITE_DONE != ret) |
1151 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step"); | 1353 | LOG_SQLITE (plugin, |
1354 | GNUNET_ERROR_TYPE_ERROR, | ||
1355 | "sqlite_step"); | ||
1152 | sqlite3_finalize (stmt); | 1356 | sqlite3_finalize (stmt); |
1153 | proc (proc_cls, NULL, 0); | 1357 | proc (proc_cls, |
1358 | NULL, | ||
1359 | 0); | ||
1154 | } | 1360 | } |
1155 | 1361 | ||
1156 | 1362 | ||
@@ -1176,7 +1382,8 @@ sqlite_plugin_drop (void *cls) | |||
1176 | * @return the size of the database on disk (estimate) | 1382 | * @return the size of the database on disk (estimate) |
1177 | */ | 1383 | */ |
1178 | static void | 1384 | static void |
1179 | sqlite_plugin_estimate_size (void *cls, unsigned long long *estimate) | 1385 | sqlite_plugin_estimate_size (void *cls, |
1386 | unsigned long long *estimate) | ||
1180 | { | 1387 | { |
1181 | struct Plugin *plugin = cls; | 1388 | struct Plugin *plugin = cls; |
1182 | sqlite3_stmt *stmt; | 1389 | sqlite3_stmt *stmt; |
@@ -1191,29 +1398,45 @@ sqlite_plugin_estimate_size (void *cls, unsigned long long *estimate) | |||
1191 | return; | 1398 | return; |
1192 | if (SQLITE_VERSION_NUMBER < 3006000) | 1399 | if (SQLITE_VERSION_NUMBER < 3006000) |
1193 | { | 1400 | { |
1194 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "datastore-sqlite", | 1401 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, |
1195 | _ | 1402 | "datastore-sqlite", |
1196 | ("sqlite version to old to determine size, assuming zero\n")); | 1403 | _("sqlite version to old to determine size, assuming zero\n")); |
1197 | *estimate = 0; | 1404 | *estimate = 0; |
1198 | return; | 1405 | return; |
1199 | } | 1406 | } |
1200 | CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh, "VACUUM", NULL, NULL, ENULL)); | ||
1201 | CHECK (SQLITE_OK == | 1407 | CHECK (SQLITE_OK == |
1202 | sqlite3_exec (plugin->dbh, "PRAGMA auto_vacuum=INCREMENTAL", NULL, | 1408 | sqlite3_exec (plugin->dbh, |
1409 | "VACUUM", | ||
1410 | NULL, | ||
1411 | NULL, | ||
1412 | ENULL)); | ||
1413 | CHECK (SQLITE_OK == | ||
1414 | sqlite3_exec (plugin->dbh, | ||
1415 | "PRAGMA auto_vacuum=INCREMENTAL", | ||
1416 | NULL, | ||
1203 | NULL, ENULL)); | 1417 | NULL, ENULL)); |
1204 | CHECK (SQLITE_OK == sq_prepare (plugin->dbh, "PRAGMA page_count", &stmt)); | 1418 | CHECK (SQLITE_OK == |
1419 | sq_prepare (plugin->dbh, | ||
1420 | "PRAGMA page_count", | ||
1421 | &stmt)); | ||
1205 | if (SQLITE_ROW == sqlite3_step (stmt)) | 1422 | if (SQLITE_ROW == sqlite3_step (stmt)) |
1206 | pages = sqlite3_column_int64 (stmt, 0); | 1423 | pages = sqlite3_column_int64 (stmt, |
1424 | 0); | ||
1207 | else | 1425 | else |
1208 | pages = 0; | 1426 | pages = 0; |
1209 | sqlite3_finalize (stmt); | 1427 | sqlite3_finalize (stmt); |
1210 | CHECK (SQLITE_OK == sq_prepare (plugin->dbh, "PRAGMA page_size", &stmt)); | 1428 | CHECK (SQLITE_OK == |
1211 | CHECK (SQLITE_ROW == sqlite3_step (stmt)); | 1429 | sq_prepare (plugin->dbh, |
1430 | "PRAGMA page_size", | ||
1431 | &stmt)); | ||
1432 | CHECK (SQLITE_ROW == | ||
1433 | sqlite3_step (stmt)); | ||
1212 | page_size = sqlite3_column_int64 (stmt, 0); | 1434 | page_size = sqlite3_column_int64 (stmt, 0); |
1213 | sqlite3_finalize (stmt); | 1435 | sqlite3_finalize (stmt); |
1214 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 1436 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
1215 | _("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"), | 1437 | _("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"), |
1216 | (unsigned long long) pages, (unsigned long long) page_size); | 1438 | (unsigned long long) pages, |
1439 | (unsigned long long) page_size); | ||
1217 | *estimate = pages * page_size; | 1440 | *estimate = pages * page_size; |
1218 | } | 1441 | } |
1219 | 1442 | ||
@@ -1231,9 +1454,11 @@ libgnunet_plugin_datastore_sqlite_init (void *cls) | |||
1231 | struct GNUNET_DATASTORE_PluginEnvironment *env = cls; | 1454 | struct GNUNET_DATASTORE_PluginEnvironment *env = cls; |
1232 | struct GNUNET_DATASTORE_PluginFunctions *api; | 1455 | struct GNUNET_DATASTORE_PluginFunctions *api; |
1233 | 1456 | ||
1234 | if (plugin.env != NULL) | 1457 | if (NULL != plugin.env) |
1235 | return NULL; /* can only initialize once! */ | 1458 | return NULL; /* can only initialize once! */ |
1236 | memset (&plugin, 0, sizeof (struct Plugin)); | 1459 | memset (&plugin, |
1460 | 0, | ||
1461 | sizeof (struct Plugin)); | ||
1237 | plugin.env = env; | 1462 | plugin.env = env; |
1238 | if (GNUNET_OK != database_setup (env->cfg, &plugin)) | 1463 | if (GNUNET_OK != database_setup (env->cfg, &plugin)) |
1239 | { | 1464 | { |
@@ -1251,7 +1476,8 @@ libgnunet_plugin_datastore_sqlite_init (void *cls) | |||
1251 | api->get_zero_anonymity = &sqlite_plugin_get_zero_anonymity; | 1476 | api->get_zero_anonymity = &sqlite_plugin_get_zero_anonymity; |
1252 | api->get_keys = &sqlite_plugin_get_keys; | 1477 | api->get_keys = &sqlite_plugin_get_keys; |
1253 | api->drop = &sqlite_plugin_drop; | 1478 | api->drop = &sqlite_plugin_drop; |
1254 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "sqlite", | 1479 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, |
1480 | "sqlite", | ||
1255 | _("Sqlite database running\n")); | 1481 | _("Sqlite database running\n")); |
1256 | return api; | 1482 | return api; |
1257 | } | 1483 | } |
@@ -1270,13 +1496,12 @@ libgnunet_plugin_datastore_sqlite_done (void *cls) | |||
1270 | struct GNUNET_DATASTORE_PluginFunctions *api = cls; | 1496 | struct GNUNET_DATASTORE_PluginFunctions *api = cls; |
1271 | struct Plugin *plugin = api->cls; | 1497 | struct Plugin *plugin = api->cls; |
1272 | 1498 | ||
1273 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", | 1499 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
1500 | "sqlite", | ||
1274 | "sqlite plugin is done\n"); | 1501 | "sqlite plugin is done\n"); |
1275 | fn = NULL; | 1502 | fn = NULL; |
1276 | if (plugin->drop_on_shutdown) | 1503 | if (plugin->drop_on_shutdown) |
1277 | fn = GNUNET_strdup (plugin->fn); | 1504 | fn = GNUNET_strdup (plugin->fn); |
1278 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", | ||
1279 | "Shutting down database\n"); | ||
1280 | database_shutdown (plugin); | 1505 | database_shutdown (plugin); |
1281 | plugin->env = NULL; | 1506 | plugin->env = NULL; |
1282 | GNUNET_free (api); | 1507 | GNUNET_free (api); |
@@ -1288,9 +1513,6 @@ libgnunet_plugin_datastore_sqlite_done (void *cls) | |||
1288 | fn); | 1513 | fn); |
1289 | GNUNET_free (fn); | 1514 | GNUNET_free (fn); |
1290 | } | 1515 | } |
1291 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
1292 | "sqlite", | ||
1293 | "sqlite plugin is finished\n"); | ||
1294 | return NULL; | 1516 | return NULL; |
1295 | } | 1517 | } |
1296 | 1518 | ||