diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-03-13 20:18:53 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-03-13 20:18:53 +0100 |
commit | 6e01dc4d91d072e8e58ba0140fb7f4ddd5109358 (patch) | |
tree | e51c30ae5734f74e5634b9b1913514e37fd8b660 /src/peerstore/plugin_peerstore_sqlite.c | |
parent | b7ff267a938269cc5363accb14c0010e992c0184 (diff) | |
download | gnunet-6e01dc4d91d072e8e58ba0140fb7f4ddd5109358.tar.gz gnunet-6e01dc4d91d072e8e58ba0140fb7f4ddd5109358.zip |
indentation only
Diffstat (limited to 'src/peerstore/plugin_peerstore_sqlite.c')
-rw-r--r-- | src/peerstore/plugin_peerstore_sqlite.c | 131 |
1 files changed, 93 insertions, 38 deletions
diff --git a/src/peerstore/plugin_peerstore_sqlite.c b/src/peerstore/plugin_peerstore_sqlite.c index 2cd7e22cf..236be129e 100644 --- a/src/peerstore/plugin_peerstore_sqlite.c +++ b/src/peerstore/plugin_peerstore_sqlite.c | |||
@@ -213,7 +213,8 @@ peerstore_sqlite_expire_records (void *cls, struct GNUNET_TIME_Absolute now, | |||
213 | * called | 213 | * called |
214 | */ | 214 | */ |
215 | static int | 215 | static int |
216 | peerstore_sqlite_iterate_records (void *cls, const char *sub_system, | 216 | peerstore_sqlite_iterate_records (void *cls, |
217 | const char *sub_system, | ||
217 | const struct GNUNET_PeerIdentity *peer, | 218 | const struct GNUNET_PeerIdentity *peer, |
218 | const char *key, | 219 | const char *key, |
219 | GNUNET_PEERSTORE_Processor iter, | 220 | GNUNET_PEERSTORE_Processor iter, |
@@ -333,9 +334,12 @@ peerstore_sqlite_iterate_records (void *cls, const char *sub_system, | |||
333 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR and cont is not called | 334 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR and cont is not called |
334 | */ | 335 | */ |
335 | static int | 336 | static int |
336 | peerstore_sqlite_store_record (void *cls, const char *sub_system, | 337 | peerstore_sqlite_store_record (void *cls, |
338 | const char *sub_system, | ||
337 | const struct GNUNET_PeerIdentity *peer, | 339 | const struct GNUNET_PeerIdentity *peer, |
338 | const char *key, const void *value, size_t size, | 340 | const char *key, |
341 | const void *value, | ||
342 | size_t size, | ||
339 | struct GNUNET_TIME_Absolute expiry, | 343 | struct GNUNET_TIME_Absolute expiry, |
340 | enum GNUNET_PEERSTORE_StoreOption options, | 344 | enum GNUNET_PEERSTORE_StoreOption options, |
341 | GNUNET_PEERSTORE_Continuation cont, | 345 | GNUNET_PEERSTORE_Continuation cont, |
@@ -346,7 +350,10 @@ peerstore_sqlite_store_record (void *cls, const char *sub_system, | |||
346 | 350 | ||
347 | if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options) | 351 | if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options) |
348 | { | 352 | { |
349 | peerstore_sqlite_delete_records (cls, sub_system, peer, key); | 353 | peerstore_sqlite_delete_records (cls, |
354 | sub_system, | ||
355 | peer, | ||
356 | key); | ||
350 | } | 357 | } |
351 | if (SQLITE_OK != | 358 | if (SQLITE_OK != |
352 | sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, | 359 | sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, |
@@ -388,15 +395,25 @@ peerstore_sqlite_store_record (void *cls, const char *sub_system, | |||
388 | * @return 0 on success | 395 | * @return 0 on success |
389 | */ | 396 | */ |
390 | static int | 397 | static int |
391 | sql_exec (sqlite3 * dbh, const char *sql) | 398 | sql_exec (sqlite3 *dbh, |
399 | const char *sql) | ||
392 | { | 400 | { |
393 | int result; | 401 | int result; |
394 | 402 | ||
395 | result = sqlite3_exec (dbh, sql, NULL, NULL, NULL); | 403 | result = sqlite3_exec (dbh, |
396 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Executed `%s' / %d\n", sql, result); | 404 | sql, |
397 | if (result != SQLITE_OK) | 405 | NULL, |
398 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Error executing SQL query: %s\n %s\n"), | 406 | NULL, |
399 | sqlite3_errmsg (dbh), sql); | 407 | NULL); |
408 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
409 | "Executed `%s' / %d\n", | ||
410 | sql, | ||
411 | result); | ||
412 | if (SQLITE_OK != result) | ||
413 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
414 | _("Error executing SQL query: %s\n %s\n"), | ||
415 | sqlite3_errmsg (dbh), | ||
416 | sql); | ||
400 | return result; | 417 | return result; |
401 | } | 418 | } |
402 | 419 | ||
@@ -410,17 +427,28 @@ sql_exec (sqlite3 * dbh, const char *sql) | |||
410 | * @return 0 on success | 427 | * @return 0 on success |
411 | */ | 428 | */ |
412 | static int | 429 | static int |
413 | sql_prepare (sqlite3 * dbh, const char *sql, sqlite3_stmt ** stmt) | 430 | sql_prepare (sqlite3 *dbh, |
431 | const char *sql, | ||
432 | sqlite3_stmt ** stmt) | ||
414 | { | 433 | { |
415 | char *tail; | 434 | char *tail; |
416 | int result; | 435 | int result; |
417 | 436 | ||
418 | result = | 437 | result = sqlite3_prepare_v2 (dbh, |
419 | sqlite3_prepare_v2 (dbh, sql, strlen (sql), stmt, (const char **) &tail); | 438 | sql, |
420 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Prepared `%s' / %p: %d\n", sql, *stmt, result); | 439 | strlen (sql), |
421 | if (result != SQLITE_OK) | 440 | stmt, |
422 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Error preparing SQL query: %s\n %s\n"), | 441 | (const char **) &tail); |
423 | sqlite3_errmsg (dbh), sql); | 442 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
443 | "Prepared `%s' / %p: %d\n", | ||
444 | sql, | ||
445 | *stmt, | ||
446 | result); | ||
447 | if (SQLITE_OK != result) | ||
448 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
449 | _("Error preparing SQL query: %s\n %s\n"), | ||
450 | sqlite3_errmsg (dbh), | ||
451 | sql); | ||
424 | return result; | 452 | return result; |
425 | } | 453 | } |
426 | 454 | ||
@@ -429,8 +457,10 @@ sql_prepare (sqlite3 * dbh, const char *sql, sqlite3_stmt ** stmt) | |||
429 | * sqlite3 custom function for comparison of uint64_t values | 457 | * sqlite3 custom function for comparison of uint64_t values |
430 | * since it is not supported by default | 458 | * since it is not supported by default |
431 | */ | 459 | */ |
432 | void | 460 | static void |
433 | sqlite3_lessthan (sqlite3_context * ctx, int dummy, sqlite3_value ** values) | 461 | sqlite3_lessthan (sqlite3_context *ctx, |
462 | int dummy, | ||
463 | sqlite3_value **values) | ||
434 | { | 464 | { |
435 | uint64_t v1; | 465 | uint64_t v1; |
436 | uint64_t v2; | 466 | uint64_t v2; |
@@ -455,10 +485,13 @@ database_setup (struct Plugin *plugin) | |||
455 | char *filename; | 485 | char *filename; |
456 | 486 | ||
457 | if (GNUNET_OK != | 487 | if (GNUNET_OK != |
458 | GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "peerstore-sqlite", | 488 | GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, |
459 | "FILENAME", &filename)) | 489 | "peerstore-sqlite", |
490 | "FILENAME", | ||
491 | &filename)) | ||
460 | { | 492 | { |
461 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "peerstore-sqlite", | 493 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, |
494 | "peerstore-sqlite", | ||
462 | "FILENAME"); | 495 | "FILENAME"); |
463 | return GNUNET_SYSERR; | 496 | return GNUNET_SYSERR; |
464 | } | 497 | } |
@@ -474,34 +507,52 @@ database_setup (struct Plugin *plugin) | |||
474 | /* filename should be UTF-8-encoded. If it isn't, it's a bug */ | 507 | /* filename should be UTF-8-encoded. If it isn't, it's a bug */ |
475 | plugin->fn = filename; | 508 | plugin->fn = filename; |
476 | /* Open database and precompile statements */ | 509 | /* Open database and precompile statements */ |
477 | if (SQLITE_OK != sqlite3_open (plugin->fn, &plugin->dbh)) | 510 | if (SQLITE_OK != sqlite3_open (plugin->fn, |
511 | &plugin->dbh)) | ||
478 | { | 512 | { |
479 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Unable to initialize SQLite: %s.\n"), | 513 | LOG (GNUNET_ERROR_TYPE_ERROR, |
514 | _("Unable to initialize SQLite: %s.\n"), | ||
480 | sqlite3_errmsg (plugin->dbh)); | 515 | sqlite3_errmsg (plugin->dbh)); |
481 | return GNUNET_SYSERR; | 516 | return GNUNET_SYSERR; |
482 | } | 517 | } |
483 | sql_exec (plugin->dbh, "PRAGMA temp_store=MEMORY"); | 518 | sql_exec (plugin->dbh, |
484 | sql_exec (plugin->dbh, "PRAGMA synchronous=OFF"); | 519 | "PRAGMA temp_store=MEMORY"); |
485 | sql_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF"); | 520 | sql_exec (plugin->dbh, |
486 | sql_exec (plugin->dbh, "PRAGMA auto_vacuum=INCREMENTAL"); | 521 | "PRAGMA synchronous=OFF"); |
487 | sql_exec (plugin->dbh, "PRAGMA encoding=\"UTF-8\""); | 522 | sql_exec (plugin->dbh, |
488 | sql_exec (plugin->dbh, "PRAGMA page_size=4096"); | 523 | "PRAGMA legacy_file_format=OFF"); |
489 | sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS); | 524 | sql_exec (plugin->dbh, |
525 | "PRAGMA auto_vacuum=INCREMENTAL"); | ||
526 | sql_exec (plugin->dbh, | ||
527 | "PRAGMA encoding=\"UTF-8\""); | ||
528 | sql_exec (plugin->dbh, | ||
529 | "PRAGMA page_size=4096"); | ||
530 | sqlite3_busy_timeout (plugin->dbh, | ||
531 | BUSY_TIMEOUT_MS); | ||
490 | /* Create tables */ | 532 | /* Create tables */ |
491 | sql_exec (plugin->dbh, | 533 | sql_exec (plugin->dbh, |
492 | "CREATE TABLE IF NOT EXISTS peerstoredata (\n" | 534 | "CREATE TABLE IF NOT EXISTS peerstoredata (\n" |
493 | " sub_system TEXT NOT NULL,\n" " peer_id BLOB NOT NULL,\n" | 535 | " sub_system TEXT NOT NULL,\n" " peer_id BLOB NOT NULL,\n" |
494 | " key TEXT NOT NULL,\n" " value BLOB NULL,\n" | 536 | " key TEXT NOT NULL,\n" " value BLOB NULL,\n" |
495 | " expiry sqlite3_uint64 NOT NULL" ");"); | 537 | " expiry sqlite3_uint64 NOT NULL" ");"); |
496 | sqlite3_create_function (plugin->dbh, "UINT64_LT", 2, SQLITE_UTF8, NULL, | 538 | sqlite3_create_function (plugin->dbh, |
497 | &sqlite3_lessthan, NULL, NULL); | 539 | "UINT64_LT", |
540 | 2, | ||
541 | SQLITE_UTF8, | ||
542 | NULL, | ||
543 | &sqlite3_lessthan, | ||
544 | NULL, | ||
545 | NULL); | ||
498 | /* Create Indices */ | 546 | /* Create Indices */ |
499 | if (SQLITE_OK != | 547 | if (SQLITE_OK != |
500 | sqlite3_exec (plugin->dbh, | 548 | sqlite3_exec (plugin->dbh, |
501 | "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)", | 549 | "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)", |
502 | NULL, NULL, NULL)) | 550 | NULL, |
551 | NULL, | ||
552 | NULL)) | ||
503 | { | 553 | { |
504 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Unable to create indices: %s.\n"), | 554 | LOG (GNUNET_ERROR_TYPE_ERROR, |
555 | _("Unable to create indices: %s.\n"), | ||
505 | sqlite3_errmsg (plugin->dbh)); | 556 | sqlite3_errmsg (plugin->dbh)); |
506 | return GNUNET_SYSERR; | 557 | return GNUNET_SYSERR; |
507 | } | 558 | } |
@@ -573,7 +624,9 @@ libgnunet_plugin_peerstore_sqlite_init (void *cls) | |||
573 | 624 | ||
574 | if (NULL != plugin.cfg) | 625 | if (NULL != plugin.cfg) |
575 | return NULL; /* can only initialize once! */ | 626 | return NULL; /* can only initialize once! */ |
576 | memset (&plugin, 0, sizeof (struct Plugin)); | 627 | memset (&plugin, |
628 | 0, | ||
629 | sizeof (struct Plugin)); | ||
577 | plugin.cfg = cfg; | 630 | plugin.cfg = cfg; |
578 | if (GNUNET_OK != database_setup (&plugin)) | 631 | if (GNUNET_OK != database_setup (&plugin)) |
579 | { | 632 | { |
@@ -585,7 +638,8 @@ libgnunet_plugin_peerstore_sqlite_init (void *cls) | |||
585 | api->store_record = &peerstore_sqlite_store_record; | 638 | api->store_record = &peerstore_sqlite_store_record; |
586 | api->iterate_records = &peerstore_sqlite_iterate_records; | 639 | api->iterate_records = &peerstore_sqlite_iterate_records; |
587 | api->expire_records = &peerstore_sqlite_expire_records; | 640 | api->expire_records = &peerstore_sqlite_expire_records; |
588 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is running\n"); | 641 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
642 | "Sqlite plugin is running\n"); | ||
589 | return api; | 643 | return api; |
590 | } | 644 | } |
591 | 645 | ||
@@ -605,7 +659,8 @@ libgnunet_plugin_peerstore_sqlite_done (void *cls) | |||
605 | database_shutdown (plugin); | 659 | database_shutdown (plugin); |
606 | plugin->cfg = NULL; | 660 | plugin->cfg = NULL; |
607 | GNUNET_free (api); | 661 | GNUNET_free (api); |
608 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is finished\n"); | 662 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
663 | "Sqlite plugin is finished\n"); | ||
609 | return NULL; | 664 | return NULL; |
610 | } | 665 | } |
611 | 666 | ||