1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use std::io::{Read, Write};
use std::result;

use persistent_log::{Error, Log};
use {LogIndex, ServerId, Term};

/// This is a `Log` implementation that stores entries in a simple in-memory vector. Other data
/// is stored in a struct. It is chiefly intended for testing.
///
/// # Panic
///
/// No bounds checking is performed and attempted access to non-existing log
/// indexes will panic.
#[derive(Clone, Debug)]
pub struct MemLog {
    current_term: Term,
    voted_for: Option<ServerId>,
    entries: Vec<(Term, Vec<u8>)>,
}

impl MemLog {
    pub fn new() -> MemLog {
        MemLog {
            current_term: Term(0),
            voted_for: None,
            entries: Vec::new(),
        }
    }
}

impl Log for MemLog {
    type Error = Error;

    fn current_term(&self) -> result::Result<Term, Error> {
        Ok(self.current_term)
    }

    fn set_current_term(&mut self, term: Term) -> result::Result<(), Error> {
        self.voted_for = None;
        self.current_term = term;
        Ok(())
    }

    fn inc_current_term(&mut self) -> result::Result<Term, Error> {
        self.voted_for = None;
        self.current_term = self.current_term + 1;
        self.current_term()
    }

    fn voted_for(&self) -> result::Result<Option<ServerId>, Error> {
        Ok(self.voted_for)
    }

    fn set_voted_for(&mut self, address: ServerId) -> result::Result<(), Error> {
        self.voted_for = Some(address);
        Ok(())
    }

    fn latest_log_index(&self) -> result::Result<LogIndex, Error> {
        Ok(LogIndex(self.entries.len() as u64))
    }

    fn latest_log_term(&self) -> result::Result<Term, Error> {
        let len = self.entries.len();
        if len == 0 {
            Ok(Term::from(0))
        } else {
            Ok(self.entries[len - 1].0)
        }
    }

    fn entry<W: Write>(&self, index: LogIndex, buf: Option<W>) -> Result<Term, Error> {
        match self.entries.get((index - 1).as_u64() as usize) {
            Some(&(term, ref bytes)) => {
                if let Some(mut buf) = buf {
                    buf.write_all(&bytes)?;
                };
                Ok(term)
            }
            None => Err(Error::BadIndex),
        }
    }

    fn append_entries<R: Read, I: Iterator<Item = (Term, R)>>(
        &mut self,
        from: LogIndex,
        entries: I,
    ) -> result::Result<(), Self::Error> {
        if self.latest_log_index()? + 1 < from {
            return Err(Error::BadLogIndex);
        }

        // TODO remove vector hack
        let mut entries_vec = Vec::new();
        for (term, mut reader) in entries {
            let mut v = Vec::new();
            reader.read_to_end(&mut v)?;
            entries_vec.push((term, v));
        }
        self.entries.truncate((from - 1).as_u64() as usize);
        self.entries.extend(entries_vec.into_iter());
        Ok(())
    }
}

#[cfg(test)]
mod test {

    use super::*;

    use persistent_log::{append_entries, get_entry, Log};
    use {LogIndex, ServerId, Term};

    #[test]
    fn test_current_term() {
        let mut store = MemLog::new();
        assert_eq!(Term(0), store.current_term().unwrap());
        store.set_voted_for(ServerId::from(0)).unwrap();
        store.set_current_term(Term(42)).unwrap();
        assert_eq!(None, store.voted_for().unwrap());
        assert_eq!(Term(42), store.current_term().unwrap());
        store.inc_current_term().unwrap();
        assert_eq!(Term(43), store.current_term().unwrap());
    }

    #[test]
    fn test_voted_for() {
        let mut store = MemLog::new();
        assert_eq!(None, store.voted_for().unwrap());
        let id = ServerId::from(0);
        store.set_voted_for(id).unwrap();
        assert_eq!(Some(id), store.voted_for().unwrap());
    }

    #[test]
    fn test_append_entries() {
        let mut store = MemLog::new();
        assert_eq!(LogIndex::from(0), store.latest_log_index().unwrap());
        assert_eq!(Term::from(0), store.latest_log_term().unwrap());

        // [0.1, 0.2, 0.3, 1.4]
        append_entries(
            &mut store,
            LogIndex(1),
            &[
                (Term::from(0), &[1]),
                (Term::from(0), &[2]),
                (Term::from(0), &[3]),
                (Term::from(1), &[4]),
            ],
        ).unwrap();
        assert_eq!(LogIndex::from(4), store.latest_log_index().unwrap());
        assert_eq!(Term::from(1), store.latest_log_term().unwrap());

        assert_eq!(
            (Term::from(0), vec![1u8]),
            get_entry(&store, LogIndex::from(1))
        );
        assert_eq!(
            (Term::from(0), vec![2u8]),
            get_entry(&store, LogIndex::from(2))
        );
        assert_eq!(
            (Term::from(0), vec![3u8]),
            get_entry(&store, LogIndex::from(3))
        );
        assert_eq!(
            (Term::from(1), vec![4u8]),
            get_entry(&store, LogIndex::from(4))
        );

        // [0.1, 0.2, 0.3]
        append_entries(&mut store, LogIndex::from(4), &[]).unwrap();
        assert_eq!(LogIndex(3), store.latest_log_index().unwrap());
        assert_eq!(Term::from(0), store.latest_log_term().unwrap());
        assert_eq!(
            (Term::from(0), vec![1u8]),
            get_entry(&store, LogIndex::from(1))
        );
        assert_eq!(
            (Term::from(0), vec![2u8]),
            get_entry(&store, LogIndex::from(2))
        );
        assert_eq!(
            (Term::from(0), vec![3u8]),
            get_entry(&store, LogIndex::from(3))
        );

        // [0.1, 0.2, 2.3, 3.4]
        append_entries(
            &mut store,
            LogIndex::from(3),
            &[(Term(2), &[3]), (Term(3), &[4])],
        ).unwrap();
        assert_eq!(LogIndex(4), store.latest_log_index().unwrap());
        assert_eq!(Term::from(3), store.latest_log_term().unwrap());
        assert_eq!(
            (Term::from(0), vec![1u8]),
            get_entry(&store, LogIndex::from(1))
        );
        assert_eq!(
            (Term::from(0), vec![2u8]),
            get_entry(&store, LogIndex::from(2))
        );
        assert_eq!(
            (Term::from(2), vec![3u8]),
            get_entry(&store, LogIndex::from(3))
        );
        assert_eq!(
            (Term::from(3), vec![4u8]),
            get_entry(&store, LogIndex::from(4))
        );
    }
}

impl Default for MemLog {
    fn default() -> Self {
        MemLog::new()
    }
}