Procházet zdrojové kódy

Remove the index method of log array.

Jing Yang před 4 roky
rodič
revize
fc98b98203

+ 8 - 8
src/log_array.rs

@@ -106,14 +106,6 @@ impl<C> LogArray<C> {
     }
 }
 
-impl<C> std::ops::Index<usize> for LogArray<C> {
-    type Output = LogEntry<C>;
-
-    fn index(&self, index: usize) -> &Self::Output {
-        self.at(index)
-    }
-}
-
 // Mutations
 impl<C> LogArray<C> {
     /// Add a new entry to the Raft log, with term and command. The new index is
@@ -276,6 +268,14 @@ mod tests {
 
     use super::*;
 
+    impl<C> std::ops::Index<usize> for LogArray<C> {
+        type Output = LogEntry<C>;
+
+        fn index(&self, index: usize) -> &Self::Output {
+            self.at(index)
+        }
+    }
+
     fn make_log_array(len: usize) -> LogArray<i32> {
         make_log_array_range(0, len)
     }

+ 2 - 2
src/process_append_entries.rs

@@ -41,7 +41,7 @@ where
 
         if rf.log.start() > args.prev_log_index
             || rf.log.end() <= args.prev_log_index
-            || rf.log[args.prev_log_index].term != args.prev_log_term
+            || rf.log.at(args.prev_log_index).term != args.prev_log_term
         {
             return AppendEntriesReply {
                 term: args.term,
@@ -74,7 +74,7 @@ where
         for (i, entry) in args.entries.iter().enumerate() {
             let index = i + args.prev_log_index + 1;
             if rf.log.end() > index {
-                if rf.log[index].term != entry.term {
+                if rf.log.at(index).term != entry.term {
                     check_or_record!(
                         index > rf.commit_index,
                         ErrorKind::RollbackCommitted(index),

+ 2 - 1
src/process_install_snapshot.rs

@@ -58,7 +58,8 @@ impl<C: Clone + Default + serde::Serialize> Raft<C> {
 
         if args.last_included_index < rf.log.end()
             && args.last_included_index >= rf.log.start()
-            && args.last_included_term == rf.log[args.last_included_index].term
+            && args.last_included_term
+                == rf.log.at(args.last_included_index).term
         {
             // Do nothing if the index and term match the current snapshot.
             if args.last_included_index != rf.log.start() {

+ 3 - 2
src/sync_log_entries.rs

@@ -200,7 +200,8 @@ where
                         matched.sort_unstable();
                         let new_commit_index = matched[mid];
                         if new_commit_index > rf.commit_index
-                            && rf.log[new_commit_index].term == rf.current_term
+                            && rf.log.at(new_commit_index).term
+                                == rf.current_term
                         {
                             // COMMIT_INDEX_INVARIANT, SNAPSHOT_INDEX_INVARIANT:
                             // Index new_commit_index exists in the log array,
@@ -351,7 +352,7 @@ where
         peer_index: usize,
     ) -> AppendEntriesArgs<Command> {
         let prev_log_index = rf.next_index[peer_index] - 1;
-        let prev_log_term = rf.log[prev_log_index].term;
+        let prev_log_term = rf.log.at(prev_log_index).term;
         AppendEntriesArgs {
             term: rf.current_term,
             leader_id: rf.leader_id,