|
1 | 1 | use std::fmt::Display;
|
2 | 2 | use std::fmt::Formatter;
|
3 | 3 |
|
4 |
| -use crate::vote::CommittedLeaderId; |
5 |
| -use crate::LogId; |
6 |
| -use crate::NodeId; |
7 |
| - |
8 |
| -pub trait RaftLogId<NID: NodeId> { |
9 |
| - fn leader_id(&self) -> &CommittedLeaderId<NID> { |
10 |
| - self.get_log_id().committed_leader_id() |
11 |
| - } |
12 |
| - |
13 |
| - fn get_log_id(&self) -> &LogId<NID>; |
14 |
| - |
15 |
| - fn set_log_id(&mut self, log_id: &LogId<NID>); |
16 |
| -} |
17 |
| - |
18 |
| -pub trait LogIdOptionExt { |
19 |
| - fn index(&self) -> Option<u64>; |
20 |
| - fn next_index(&self) -> u64; |
21 |
| -} |
22 |
| - |
23 |
| -impl<NID: NodeId> LogIdOptionExt for Option<LogId<NID>> { |
24 |
| - fn index(&self) -> Option<u64> { |
25 |
| - self.map(|x| x.index) |
26 |
| - } |
27 |
| - |
28 |
| - fn next_index(&self) -> u64 { |
29 |
| - match self { |
30 |
| - None => 0, |
31 |
| - Some(log_id) => log_id.index + 1, |
32 |
| - } |
33 |
| - } |
34 |
| -} |
35 |
| - |
36 |
| -impl<NID: NodeId> LogIdOptionExt for Option<&LogId<NID>> { |
37 |
| - fn index(&self) -> Option<u64> { |
38 |
| - self.map(|x| x.index) |
39 |
| - } |
40 |
| - |
41 |
| - fn next_index(&self) -> u64 { |
42 |
| - match self { |
43 |
| - None => 0, |
44 |
| - Some(log_id) => log_id.index + 1, |
45 |
| - } |
46 |
| - } |
47 |
| -} |
48 |
| - |
49 |
| -pub trait LogIndexOptionExt { |
50 |
| - fn next_index(&self) -> u64; |
51 |
| - fn prev_index(&self) -> Self; |
52 |
| - fn add(&self, v: u64) -> Self; |
53 |
| -} |
54 |
| - |
55 |
| -impl LogIndexOptionExt for Option<u64> { |
56 |
| - fn next_index(&self) -> u64 { |
57 |
| - match self { |
58 |
| - None => 0, |
59 |
| - Some(v) => v + 1, |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| - fn prev_index(&self) -> Self { |
64 |
| - match self { |
65 |
| - None => { |
66 |
| - panic!("None has no previous value"); |
67 |
| - } |
68 |
| - Some(v) => { |
69 |
| - if *v == 0 { |
70 |
| - None |
71 |
| - } else { |
72 |
| - Some(*v - 1) |
73 |
| - } |
74 |
| - } |
75 |
| - } |
76 |
| - } |
77 |
| - |
78 |
| - fn add(&self, v: u64) -> Self { |
79 |
| - Some(self.next_index() + v).prev_index() |
80 |
| - } |
81 |
| -} |
82 |
| - |
83 | 4 | // Everytime a snapshot is created, it is assigned with a globally unique id.
|
84 | 5 | pub type SnapshotId = String;
|
85 | 6 |
|
|
0 commit comments