@@ -498,7 +498,11 @@ impl ReadProposalFromDB for DB {
498
498
let proposal_index_option: Option < JsonValue > = Self :: get_proposal_index_as_json ( ) ;
499
499
match proposal_index_option {
500
500
Some ( proposal_index) => {
501
- let next_proposal_id_option: Option < i32 > = Proposal :: get_next_proposal_id ( ) ;
501
+
502
+ //TODO: invoke get_next_proposal_id_from_index() instead
503
+ //let next_proposal_id_option: Option<i32> = Proposal::get_next_proposal_id();
504
+ let next_proposal_id_option: Option < i32 > = Proposal :: get_next_proposal_id_from_index ( ) ;
505
+
502
506
match next_proposal_id_option {
503
507
Some ( next_proposal_id) => {
504
508
let mut all_proposals_vector: Vec < Proposal > = Vec :: new ( ) ;
@@ -778,7 +782,9 @@ impl NewProposal for Proposal {
778
782
fn create ( request_origin : String ) -> Option < Proposal > {
779
783
println ! ( "Creating New Proposal..." ) ;
780
784
//TODO: determine proposal ID
781
- let new_proposal_id: i32 = match Self :: get_next_proposal_id ( ) {
785
+ //TODO: invoke get_next_proposal_id_from_index() instead
786
+ //let new_proposal_id:i32 = match Self::get_next_proposal_id(){
787
+ let new_proposal_id: i32 = match Self :: get_next_proposal_id_from_index ( ) {
782
788
Some ( pid) => pid,
783
789
None => -1
784
790
} ;
@@ -826,6 +832,7 @@ impl NewProposal for Proposal {
826
832
trait ProposalIDGenerator {
827
833
fn parse_filename_for_proposal_id ( filename : & str ) -> Option < i32 > ;
828
834
fn get_next_proposal_id ( ) -> Option < i32 > ;
835
+ fn get_next_proposal_id_from_index ( ) -> Option < i32 > ;
829
836
}
830
837
831
838
impl ProposalIDGenerator for Proposal {
@@ -855,6 +862,7 @@ impl ProposalIDGenerator for Proposal {
855
862
/*
856
863
@name get_next_proposal_id
857
864
@desc generate the next proposal_id from all proposals on disk
865
+ @deprecated due to counting files, instead of accessing index
858
866
*/
859
867
fn get_next_proposal_id ( ) -> Option < i32 > {
860
868
//read all directories
@@ -863,7 +871,7 @@ impl ProposalIDGenerator for Proposal {
863
871
let mut highest_proposal_index: i32 = -1 ;
864
872
//iterate over all proposal files
865
873
while let Some ( v) = iter. next ( ) {
866
- println ! ( "Filename Iter: {}" , v) ;
874
+ // println!("Filename Iter: {}", v);
867
875
//parse file name for proposal id
868
876
let filename_split_vector = v. split ( "/" ) . collect :: < Vec < _ > > ( ) ;
869
877
let last_split_section: & str = filename_split_vector[ filename_split_vector. len ( ) - 1 ] ;
@@ -887,6 +895,36 @@ impl ProposalIDGenerator for Proposal {
887
895
888
896
}
889
897
}
898
+
899
+ /*
900
+ @name get_next_proposal_id_from_index
901
+ @desc determine next proposal id from index
902
+ */
903
+ fn get_next_proposal_id_from_index ( ) -> Option < i32 > {
904
+ let parsed_option: Option < JsonValue > = DB :: get_proposal_index_as_json ( ) ;
905
+ match parsed_option {
906
+ Some ( mut proposal_index) => {
907
+
908
+ let proposals_iter = & proposal_index. clone ( ) [ "proposals" ] ;
909
+ let last_proposal_option = proposals_iter. entries ( ) . last ( ) . clone ( ) ;
910
+ match last_proposal_option {
911
+ Some ( last_proposal) => {
912
+ let last_proposal_json = last_proposal. 1 ;
913
+
914
+ // TODO: error handling
915
+ Some ( last_proposal_json[ "proposal_id" ] . as_i32 ( ) . unwrap ( ) )
916
+
917
+ } ,
918
+ None => {
919
+ None
920
+ }
921
+ }
922
+
923
+ } ,
924
+ None => None
925
+ }
926
+
927
+ }
890
928
}
891
929
892
930
#[ derive( Clone , Debug , PartialEq ) ]
@@ -988,10 +1026,12 @@ impl ProposalValidator for Proposal {
988
1026
//let current_block_id: Option<i64> = Block::get_latest_block_id();
989
1027
990
1028
//TODO: this is better than get_latest_block_id, since this counds block files instead of index length
991
- let current_block_id: Option < i64 > = Block :: get_next_block_id ( ) ;
1029
+ //TODO: invoke get_next_block_id_from_index() instead
1030
+ //let current_block_id: Option<i64> = Block::get_next_block_id();
1031
+ let current_block_id: Option < i64 > = Block :: get_next_block_id_from_index ( ) ;
992
1032
993
1033
let current_block_id_result: i64 = match current_block_id {
994
- Some ( block_id) => {
1034
+ Some ( block_id) => {
995
1035
println ! ( "validate_proposal(), current_block_id, block_id: {}" , block_id) ;
996
1036
// was just block_id, but substracting one since calling current_block_id
997
1037
// block_id
0 commit comments