diff options
author | David Barksdale <amatus.amongus@gmail.com> | 2015-03-21 03:38:29 +0000 |
---|---|---|
committer | David Barksdale <amatus.amongus@gmail.com> | 2015-03-21 03:38:29 +0000 |
commit | c77d4e5c69ac54ffddf5bd60c18bcb0504389311 (patch) | |
tree | bb40b73db6ed428d6ab44ffee91ca0ed6f16b592 /src/datastore/plugin_datastore_sqlite.c | |
parent | ce6f1156a58aafed6563585b3be560ec0b4eabe7 (diff) | |
download | gnunet-c77d4e5c69ac54ffddf5bd60c18bcb0504389311.tar.gz gnunet-c77d4e5c69ac54ffddf5bd60c18bcb0504389311.zip |
Convert datastore plugin API to asynchronous
Diffstat (limited to 'src/datastore/plugin_datastore_sqlite.c')
-rw-r--r-- | src/datastore/plugin_datastore_sqlite.c | 67 |
1 files changed, 44 insertions, 23 deletions
diff --git a/src/datastore/plugin_datastore_sqlite.c b/src/datastore/plugin_datastore_sqlite.c index b27aab8c1..ea00e7df7 100644 --- a/src/datastore/plugin_datastore_sqlite.c +++ b/src/datastore/plugin_datastore_sqlite.c | |||
@@ -470,10 +470,10 @@ delete_by_rowid (struct Plugin *plugin, | |||
470 | * @param anonymity anonymity-level for the content | 470 | * @param anonymity anonymity-level for the content |
471 | * @param replication replication-level for the content | 471 | * @param replication replication-level for the content |
472 | * @param expiration expiration time for the content | 472 | * @param expiration expiration time for the content |
473 | * @param msg set to an error message | 473 | * @param cont continuation called with success or failure status |
474 | * @return #GNUNET_OK on success | 474 | * @param cont_cls continuation closure |
475 | */ | 475 | */ |
476 | static int | 476 | static void |
477 | sqlite_plugin_put (void *cls, | 477 | sqlite_plugin_put (void *cls, |
478 | const struct GNUNET_HashCode *key, | 478 | const struct GNUNET_HashCode *key, |
479 | uint32_t size, | 479 | uint32_t size, |
@@ -483,7 +483,8 @@ sqlite_plugin_put (void *cls, | |||
483 | uint32_t anonymity, | 483 | uint32_t anonymity, |
484 | uint32_t replication, | 484 | uint32_t replication, |
485 | struct GNUNET_TIME_Absolute expiration, | 485 | struct GNUNET_TIME_Absolute expiration, |
486 | char **msg) | 486 | PluginPutCont cont, |
487 | void *cont_cls) | ||
487 | { | 488 | { |
488 | struct Plugin *plugin = cls; | 489 | struct Plugin *plugin = cls; |
489 | int n; | 490 | int n; |
@@ -491,9 +492,13 @@ sqlite_plugin_put (void *cls, | |||
491 | sqlite3_stmt *stmt; | 492 | sqlite3_stmt *stmt; |
492 | struct GNUNET_HashCode vhash; | 493 | struct GNUNET_HashCode vhash; |
493 | uint64_t rvalue; | 494 | uint64_t rvalue; |
495 | char *msg = NULL; | ||
494 | 496 | ||
495 | if (size > MAX_ITEM_SIZE) | 497 | if (size > MAX_ITEM_SIZE) |
496 | return GNUNET_SYSERR; | 498 | { |
499 | cont (cont_cls, key, size, GNUNET_SYSERR, _("Data too large")); | ||
500 | return; | ||
501 | } | ||
497 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", | 502 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", |
498 | "Storing in database block with type %u/key `%s'/priority %u/expiration in %s (%s).\n", | 503 | "Storing in database block with type %u/key `%s'/priority %u/expiration in %s (%s).\n", |
499 | type, | 504 | type, |
@@ -519,13 +524,15 @@ sqlite_plugin_put (void *cls, | |||
519 | SQLITE_TRANSIENT)) || | 524 | SQLITE_TRANSIENT)) || |
520 | (SQLITE_OK != sqlite3_bind_blob (stmt, 9, data, size, SQLITE_TRANSIENT))) | 525 | (SQLITE_OK != sqlite3_bind_blob (stmt, 9, data, size, SQLITE_TRANSIENT))) |
521 | { | 526 | { |
522 | LOG_SQLITE (plugin, msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 527 | LOG_SQLITE (plugin, &msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
523 | "sqlite3_bind_XXXX"); | 528 | "sqlite3_bind_XXXX"); |
524 | if (SQLITE_OK != sqlite3_reset (stmt)) | 529 | if (SQLITE_OK != sqlite3_reset (stmt)) |
525 | LOG_SQLITE (plugin, NULL, | 530 | LOG_SQLITE (plugin, NULL, |
526 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 531 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
527 | "sqlite3_reset"); | 532 | "sqlite3_reset"); |
528 | return GNUNET_SYSERR; | 533 | cont (cont_cls, key, size, GNUNET_SYSERR, msg); |
534 | GNUNET_free_non_null(msg); | ||
535 | return; | ||
529 | } | 536 | } |
530 | n = sqlite3_step (stmt); | 537 | n = sqlite3_step (stmt); |
531 | switch (n) | 538 | switch (n) |
@@ -539,12 +546,12 @@ sqlite_plugin_put (void *cls, | |||
539 | break; | 546 | break; |
540 | case SQLITE_BUSY: | 547 | case SQLITE_BUSY: |
541 | GNUNET_break (0); | 548 | GNUNET_break (0); |
542 | LOG_SQLITE (plugin, msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 549 | LOG_SQLITE (plugin, &msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
543 | "sqlite3_step"); | 550 | "sqlite3_step"); |
544 | ret = GNUNET_SYSERR; | 551 | ret = GNUNET_SYSERR; |
545 | break; | 552 | break; |
546 | default: | 553 | default: |
547 | LOG_SQLITE (plugin, msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 554 | LOG_SQLITE (plugin, &msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
548 | "sqlite3_step"); | 555 | "sqlite3_step"); |
549 | if (SQLITE_OK != sqlite3_reset (stmt)) | 556 | if (SQLITE_OK != sqlite3_reset (stmt)) |
550 | LOG_SQLITE (plugin, NULL, | 557 | LOG_SQLITE (plugin, NULL, |
@@ -552,12 +559,15 @@ sqlite_plugin_put (void *cls, | |||
552 | "sqlite3_reset"); | 559 | "sqlite3_reset"); |
553 | database_shutdown (plugin); | 560 | database_shutdown (plugin); |
554 | database_setup (plugin->env->cfg, plugin); | 561 | database_setup (plugin->env->cfg, plugin); |
555 | return GNUNET_SYSERR; | 562 | cont (cont_cls, key, size, GNUNET_SYSERR, msg); |
563 | GNUNET_free_non_null(msg); | ||
564 | return; | ||
556 | } | 565 | } |
557 | if (SQLITE_OK != sqlite3_reset (stmt)) | 566 | if (SQLITE_OK != sqlite3_reset (stmt)) |
558 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 567 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
559 | "sqlite3_reset"); | 568 | "sqlite3_reset"); |
560 | return ret; | 569 | cont (cont_cls, key, size, ret, msg); |
570 | GNUNET_free_non_null(msg); | ||
561 | } | 571 | } |
562 | 572 | ||
563 | 573 | ||
@@ -581,31 +591,34 @@ sqlite_plugin_put (void *cls, | |||
581 | * @param expire new expiration time should be the | 591 | * @param expire new expiration time should be the |
582 | * MAX of any existing expiration time and | 592 | * MAX of any existing expiration time and |
583 | * this value | 593 | * this value |
584 | * @param msg set to an error message | 594 | * @param cont continuation called with success or failure status |
585 | * @return #GNUNET_OK on success | 595 | * @param cons_cls continuation closure |
586 | */ | 596 | */ |
587 | static int | 597 | static void |
588 | sqlite_plugin_update (void *cls, | 598 | sqlite_plugin_update (void *cls, |
589 | uint64_t uid, | 599 | uint64_t uid, |
590 | int delta, | 600 | int delta, |
591 | struct GNUNET_TIME_Absolute expire, | 601 | struct GNUNET_TIME_Absolute expire, |
592 | char **msg) | 602 | PluginUpdateCont cont, |
603 | void *cont_cls) | ||
593 | { | 604 | { |
594 | struct Plugin *plugin = cls; | 605 | struct Plugin *plugin = cls; |
595 | int n; | 606 | int n; |
607 | char *msg = NULL; | ||
596 | 608 | ||
597 | if ((SQLITE_OK != sqlite3_bind_int (plugin->updPrio, 1, delta)) || | 609 | if ((SQLITE_OK != sqlite3_bind_int (plugin->updPrio, 1, delta)) || |
598 | (SQLITE_OK != sqlite3_bind_int64 (plugin->updPrio, 2, expire.abs_value_us)) | 610 | (SQLITE_OK != sqlite3_bind_int64 (plugin->updPrio, 2, expire.abs_value_us)) |
599 | || (SQLITE_OK != sqlite3_bind_int64 (plugin->updPrio, 3, uid))) | 611 | || (SQLITE_OK != sqlite3_bind_int64 (plugin->updPrio, 3, uid))) |
600 | { | 612 | { |
601 | LOG_SQLITE (plugin, msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 613 | LOG_SQLITE (plugin, &msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
602 | "sqlite3_bind_XXXX"); | 614 | "sqlite3_bind_XXXX"); |
603 | if (SQLITE_OK != sqlite3_reset (plugin->updPrio)) | 615 | if (SQLITE_OK != sqlite3_reset (plugin->updPrio)) |
604 | LOG_SQLITE (plugin, NULL, | 616 | LOG_SQLITE (plugin, NULL, |
605 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 617 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
606 | "sqlite3_reset"); | 618 | "sqlite3_reset"); |
607 | return GNUNET_SYSERR; | 619 | cont (cont_cls, GNUNET_SYSERR, msg); |
608 | 620 | GNUNET_free_non_null(msg); | |
621 | return; | ||
609 | } | 622 | } |
610 | n = sqlite3_step (plugin->updPrio); | 623 | n = sqlite3_step (plugin->updPrio); |
611 | if (SQLITE_OK != sqlite3_reset (plugin->updPrio)) | 624 | if (SQLITE_OK != sqlite3_reset (plugin->updPrio)) |
@@ -615,15 +628,21 @@ sqlite_plugin_update (void *cls, | |||
615 | { | 628 | { |
616 | case SQLITE_DONE: | 629 | case SQLITE_DONE: |
617 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Block updated\n"); | 630 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Block updated\n"); |
618 | return GNUNET_OK; | 631 | cont (cont_cls, GNUNET_OK, NULL); |
632 | return; | ||
619 | case SQLITE_BUSY: | 633 | case SQLITE_BUSY: |
620 | LOG_SQLITE (plugin, msg, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, | 634 | LOG_SQLITE (plugin, &msg, |
635 | GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, | ||
621 | "sqlite3_step"); | 636 | "sqlite3_step"); |
622 | return GNUNET_NO; | 637 | cont (cont_cls, GNUNET_NO, msg); |
638 | GNUNET_free_non_null(msg); | ||
639 | return; | ||
623 | default: | 640 | default: |
624 | LOG_SQLITE (plugin, msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 641 | LOG_SQLITE (plugin, &msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
625 | "sqlite3_step"); | 642 | "sqlite3_step"); |
626 | return GNUNET_SYSERR; | 643 | cont (cont_cls, GNUNET_SYSERR, msg); |
644 | GNUNET_free_non_null(msg); | ||
645 | return; | ||
627 | } | 646 | } |
628 | } | 647 | } |
629 | 648 | ||
@@ -1098,6 +1117,7 @@ sqlite_plugin_get_keys (void *cls, | |||
1098 | { | 1117 | { |
1099 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 1118 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
1100 | "sqlite_prepare"); | 1119 | "sqlite_prepare"); |
1120 | proc (proc_cls, NULL, 0); | ||
1101 | return; | 1121 | return; |
1102 | } | 1122 | } |
1103 | while (SQLITE_ROW == (ret = sqlite3_step (stmt))) | 1123 | while (SQLITE_ROW == (ret = sqlite3_step (stmt))) |
@@ -1111,6 +1131,7 @@ sqlite_plugin_get_keys (void *cls, | |||
1111 | if (SQLITE_DONE != ret) | 1131 | if (SQLITE_DONE != ret) |
1112 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_step"); | 1132 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_step"); |
1113 | sqlite3_finalize (stmt); | 1133 | sqlite3_finalize (stmt); |
1134 | proc (proc_cls, NULL, 0); | ||
1114 | } | 1135 | } |
1115 | 1136 | ||
1116 | 1137 | ||