aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/plugin_namestore_sqlite.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-03-17 16:31:42 +0000
committerChristian Grothoff <christian@grothoff.org>2012-03-17 16:31:42 +0000
commit3db4f1b870b1e296a6da49bc541b613e2742b20e (patch)
treeab8ad49755929ba66f6a19a6b955d1c0a2cf6f1c /src/namestore/plugin_namestore_sqlite.c
parent6c18851f371c3c8b5d1d22c97d3a5937e52be69c (diff)
downloadgnunet-3db4f1b870b1e296a6da49bc541b613e2742b20e.tar.gz
gnunet-3db4f1b870b1e296a6da49bc541b613e2742b20e.zip
-removing duplicate code
Diffstat (limited to 'src/namestore/plugin_namestore_sqlite.c')
-rw-r--r--src/namestore/plugin_namestore_sqlite.c193
1 files changed, 79 insertions, 114 deletions
diff --git a/src/namestore/plugin_namestore_sqlite.c b/src/namestore/plugin_namestore_sqlite.c
index 98f89b3da..39cd185fc 100644
--- a/src/namestore/plugin_namestore_sqlite.c
+++ b/src/namestore/plugin_namestore_sqlite.c
@@ -545,6 +545,83 @@ namestore_sqlite_put_records (void *cls,
545 return GNUNET_SYSERR; 545 return GNUNET_SYSERR;
546 } 546 }
547} 547}
548
549
550/**
551 * The given 'sqlite' statement has been prepared to be run.
552 * It will return a record which should be given to the iterator.
553 * Runs the statement and parses the returned record.
554 *
555 * @param plugin plugin context
556 * @param stmt to run (and then clean up)
557 * @param iter iterator to call with the result
558 * @param iter_cls closure for 'iter'
559 * @return GNUNET_OK on success, GNUNET_NO if there were no results, GNUNET_SYSERR on error
560 */
561static int
562get_record_and_call_iterator (struct Plugin *plugin,
563 sqlite3_stmt *stmt,
564 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
565{
566 int ret;
567 int sret;
568 unsigned int record_count;
569 size_t data_size;
570 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
571 const struct GNUNET_CRYPTO_RsaSignature *sig;
572 struct GNUNET_TIME_Absolute expiration;
573 const char *data;
574 const char *name;
575
576 ret = GNUNET_NO;
577 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
578 {
579 ret = GNUNET_YES;
580 zone_key = sqlite3_column_blob (stmt, 0);
581 name = (const char*) sqlite3_column_text (stmt, 1);
582 record_count = sqlite3_column_int (stmt, 2);
583 data_size = sqlite3_column_bytes (stmt, 3);
584 data = sqlite3_column_blob (stmt, 3);
585 expiration.abs_value = (uint64_t) sqlite3_column_int64 (stmt, 4);
586 sig = sqlite3_column_blob (stmt, 5);
587
588 if ( (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) != sqlite3_column_bytes (stmt, 0)) ||
589 (sizeof (struct GNUNET_CRYPTO_RsaSignature) != sqlite3_column_bytes (stmt, 5)) )
590 {
591 GNUNET_break (0);
592 ret = GNUNET_SYSERR;
593 }
594 else
595 {
596 struct GNUNET_NAMESTORE_RecordData rd[record_count];
597
598 if (GNUNET_OK !=
599 GNUNET_NAMESTORE_records_deserialize (data_size, data,
600 record_count, rd))
601 {
602 GNUNET_break (0);
603 ret = GNUNET_SYSERR;
604 record_count = 0;
605 }
606 else
607 {
608 iter (iter_cls, zone_key, expiration, name,
609 record_count, rd, sig);
610 }
611 }
612 }
613 else
614 {
615 if (SQLITE_DONE != sret)
616 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
617 iter (iter_cls, NULL, GNUNET_TIME_UNIT_ZERO_ABS, NULL, 0, NULL, NULL);
618 }
619 if (SQLITE_OK != sqlite3_reset (stmt))
620 LOG_SQLITE (plugin,
621 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
622 "sqlite3_reset");
623 return ret;
624}
548 625
549 626
550/** 627/**
@@ -570,8 +647,6 @@ namestore_sqlite_iterate_records (void *cls,
570 sqlite3_stmt *stmt; 647 sqlite3_stmt *stmt;
571 GNUNET_HashCode name_hase; 648 GNUNET_HashCode name_hase;
572 unsigned int boff; 649 unsigned int boff;
573 int ret;
574 int sret;
575 650
576 if (NULL == zone) 651 if (NULL == zone)
577 if (NULL == name) 652 if (NULL == name)
@@ -630,62 +705,8 @@ namestore_sqlite_iterate_records (void *cls,
630 "sqlite3_reset"); 705 "sqlite3_reset");
631 return GNUNET_SYSERR; 706 return GNUNET_SYSERR;
632 } 707 }
633 ret = GNUNET_NO;
634 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
635 {
636 unsigned int record_count;
637 size_t data_size;
638 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
639 const struct GNUNET_CRYPTO_RsaSignature *sig;
640 struct GNUNET_TIME_Absolute expiration;
641 const char *data;
642 const char *name;
643
644 ret = GNUNET_YES;
645 zone_key = sqlite3_column_blob (stmt, 0);
646 name = (const char*) sqlite3_column_text (stmt, 1);
647 record_count = sqlite3_column_int (stmt, 2);
648 data_size = sqlite3_column_bytes (stmt, 3);
649 data = sqlite3_column_blob (stmt, 3);
650 expiration.abs_value = (uint64_t) sqlite3_column_int64 (stmt, 4);
651 sig = sqlite3_column_blob (stmt, 5);
652
653 if ( (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) != sqlite3_column_bytes (stmt, 0)) ||
654 (sizeof (struct GNUNET_CRYPTO_RsaSignature) != sqlite3_column_bytes (stmt, 5)) )
655 {
656 GNUNET_break (0);
657 ret = GNUNET_SYSERR;
658 }
659 else
660 {
661 struct GNUNET_NAMESTORE_RecordData rd[record_count];
662 708
663 if (GNUNET_OK != 709 return get_record_and_call_iterator (plugin, stmt, iter, iter_cls);
664 GNUNET_NAMESTORE_records_deserialize (data_size, data,
665 record_count, rd))
666 {
667 GNUNET_break (0);
668 ret = GNUNET_SYSERR;
669 record_count = 0;
670 }
671 else
672 {
673 iter (iter_cls, zone_key, expiration, name,
674 record_count, rd, sig);
675 }
676 }
677 }
678 else
679 {
680 if (SQLITE_DONE != sret)
681 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
682 iter (iter_cls, NULL, GNUNET_TIME_UNIT_ZERO_ABS, NULL, 0, NULL, NULL);
683 }
684 if (SQLITE_OK != sqlite3_reset (stmt))
685 LOG_SQLITE (plugin,
686 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
687 "sqlite3_reset");
688 return ret;
689} 710}
690 711
691 712
@@ -708,8 +729,6 @@ namestore_sqlite_zone_to_name (void *cls,
708{ 729{
709 struct Plugin *plugin = cls; 730 struct Plugin *plugin = cls;
710 sqlite3_stmt *stmt; 731 sqlite3_stmt *stmt;
711 int ret;
712 int sret;
713 732
714 stmt = plugin->zone_to_name; 733 stmt = plugin->zone_to_name;
715 if ( (SQLITE_OK != sqlite3_bind_blob (stmt, 1, 734 if ( (SQLITE_OK != sqlite3_bind_blob (stmt, 1,
@@ -727,62 +746,8 @@ namestore_sqlite_zone_to_name (void *cls,
727 "sqlite3_reset"); 746 "sqlite3_reset");
728 return GNUNET_SYSERR; 747 return GNUNET_SYSERR;
729 } 748 }
730 ret = GNUNET_NO;
731 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
732 {
733 unsigned int record_count;
734 size_t data_size;
735 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
736 const struct GNUNET_CRYPTO_RsaSignature *sig;
737 struct GNUNET_TIME_Absolute expiration;
738 const char *data;
739 const char *name;
740
741 ret = GNUNET_YES;
742 zone_key = sqlite3_column_blob (stmt, 0);
743 name = (const char*) sqlite3_column_text (stmt, 1);
744 record_count = sqlite3_column_int (stmt, 2);
745 data_size = sqlite3_column_bytes (stmt, 3);
746 data = sqlite3_column_blob (stmt, 3);
747 expiration.abs_value = (uint64_t) sqlite3_column_int64 (stmt, 4);
748 sig = sqlite3_column_blob (stmt, 5);
749
750 if ( (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) != sqlite3_column_bytes (stmt, 0)) ||
751 (sizeof (struct GNUNET_CRYPTO_RsaSignature) != sqlite3_column_bytes (stmt, 5)) )
752 {
753 GNUNET_break (0);
754 ret = GNUNET_SYSERR;
755 }
756 else
757 {
758 struct GNUNET_NAMESTORE_RecordData rd[record_count];
759 749
760 if (GNUNET_OK != 750 return get_record_and_call_iterator (plugin, stmt, iter, iter_cls);
761 GNUNET_NAMESTORE_records_deserialize (data_size, data,
762 record_count, rd))
763 {
764 GNUNET_break (0);
765 ret = GNUNET_SYSERR;
766 record_count = 0;
767 }
768 else
769 {
770 iter (iter_cls, zone_key, expiration, name,
771 record_count, rd, sig);
772 }
773 }
774 }
775 else
776 {
777 if (SQLITE_DONE != sret)
778 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
779 iter (iter_cls, NULL, GNUNET_TIME_UNIT_ZERO_ABS, NULL, 0, NULL, NULL);
780 }
781 if (SQLITE_OK != sqlite3_reset (stmt))
782 LOG_SQLITE (plugin,
783 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
784 "sqlite3_reset");
785 return ret;
786} 751}
787 752
788 753