@@ -14,23 +14,21 @@ use crate::quorum::QuorumSet;
14
14
use crate :: MessageSummary ;
15
15
use crate :: NodeId ;
16
16
17
- /// BTreeMap for mapping node-id the node.
18
- pub type NodeMap < NID , N > = BTreeMap < NID , Option < N > > ;
19
17
/// Convert other types into the internal data structure for node infos
20
18
pub trait IntoOptionNodes < NID , N >
21
19
where
22
20
N : Node ,
23
21
NID : NodeId ,
24
22
{
25
- fn into_option_nodes ( self ) -> NodeMap < NID , N > ;
23
+ fn into_option_nodes ( self ) -> BTreeMap < NID , N > ;
26
24
}
27
25
28
26
impl < NID , N > IntoOptionNodes < NID , N > for ( )
29
27
where
30
28
N : Node ,
31
29
NID : NodeId ,
32
30
{
33
- fn into_option_nodes ( self ) -> NodeMap < NID , N > {
31
+ fn into_option_nodes ( self ) -> BTreeMap < NID , N > {
34
32
btreemap ! { }
35
33
}
36
34
}
40
38
N : Node ,
41
39
NID : NodeId ,
42
40
{
43
- fn into_option_nodes ( self ) -> NodeMap < NID , N > {
44
- self . into_iter ( ) . map ( |node_id| ( node_id, None ) ) . collect ( )
41
+ fn into_option_nodes ( self ) -> BTreeMap < NID , N > {
42
+ self . into_iter ( ) . map ( |node_id| ( node_id, N :: default ( ) ) ) . collect ( )
45
43
}
46
44
}
47
45
50
48
N : Node ,
51
49
NID : NodeId ,
52
50
{
53
- fn into_option_nodes ( self ) -> NodeMap < NID , N > {
54
- self . into_iter ( ) . map ( |( node_id, n) | ( node_id, Some ( n) ) ) . collect ( )
55
- }
56
- }
57
-
58
- impl < NID , N > IntoOptionNodes < NID , N > for NodeMap < NID , N >
59
- where
60
- N : Node ,
61
- NID : NodeId ,
62
- {
63
- fn into_option_nodes ( self ) -> NodeMap < NID , N > {
51
+ fn into_option_nodes ( self ) -> BTreeMap < NID , N > {
64
52
self
65
53
}
66
54
}
@@ -84,22 +72,19 @@ where
84
72
/// Additional info of all nodes, e.g., the connecting host and port.
85
73
///
86
74
/// A node-id key that is in `nodes` but is not in `configs` is a **learner**.
87
- /// The values in this map must all be `Some` or `None`.
88
- nodes : BTreeMap < NID , Option < N > > ,
75
+ nodes : BTreeMap < NID , N > ,
89
76
}
90
77
91
- impl < NID , N > TryFrom < BTreeMap < NID , Option < N > > > for Membership < NID , N >
78
+ impl < NID , N > From < BTreeMap < NID , N > > for Membership < NID , N >
92
79
where
93
80
N : Node ,
94
81
NID : NodeId ,
95
82
{
96
- type Error = MissingNodeInfo < NID > ;
97
-
98
- fn try_from ( b : BTreeMap < NID , Option < N > > ) -> Result < Self , Self :: Error > {
83
+ fn from ( b : BTreeMap < NID , N > ) -> Self {
99
84
let member_ids = b. keys ( ) . cloned ( ) . collect :: < BTreeSet < NID > > ( ) ;
100
85
101
- let membership = Membership :: with_nodes ( vec ! [ member_ids] , b ) ? ;
102
- Ok ( membership )
86
+ // Safe unwrap: every node-id in ` member_ids` present in `b`.
87
+ Membership :: with_nodes ( vec ! [ member_ids ] , b ) . unwrap ( )
103
88
}
104
89
}
105
90
@@ -122,9 +107,8 @@ where
122
107
}
123
108
res. push ( format ! ( "{}" , node_id) ) ;
124
109
125
- if let Some ( n) = self . get_node ( node_id) {
126
- res. push ( format ! ( ":{{{}}}" , n) ) ;
127
- }
110
+ let n = self . get_node ( node_id) ;
111
+ res. push ( format ! ( ":{{{}}}" , n) ) ;
128
112
}
129
113
res. push ( "}" . to_string ( ) ) ;
130
114
}
@@ -141,9 +125,8 @@ where
141
125
142
126
res. push ( format ! ( "{}" , learner_id) ) ;
143
127
144
- if let Some ( n) = self . get_node ( learner_id) {
145
- res. push ( format ! ( ":{{{}}}" , n) ) ;
146
- }
128
+ let n = self . get_node ( learner_id) ;
129
+ res. push ( format ! ( ":{{{}}}" , n) ) ;
147
130
}
148
131
res. push ( "]" . to_string ( ) ) ;
149
132
res. join ( "" )
@@ -194,28 +177,14 @@ where
194
177
}
195
178
}
196
179
197
- let has_some = nodes. values ( ) . any ( |x| x. is_some ( ) ) ;
198
- if has_some {
199
- let first_none = nodes. iter ( ) . find ( |( _node_id, v) | v. is_none ( ) ) ;
200
- if let Some ( first_none) = first_none {
201
- return Err ( MissingNodeInfo {
202
- node_id : * first_none. 0 ,
203
- reason : "is None" . to_string ( ) ,
204
- } ) ;
205
- }
206
- }
207
-
208
180
Ok ( Membership { configs, nodes } )
209
181
}
210
182
211
183
/// Extends nodes btreemap with another.
212
184
///
213
185
/// Node that present in `old` will **NOT** be replaced because changing the address of a node potentially breaks
214
186
/// consensus guarantee.
215
- pub ( crate ) fn extend_nodes (
216
- old : BTreeMap < NID , Option < N > > ,
217
- new : & BTreeMap < NID , Option < N > > ,
218
- ) -> BTreeMap < NID , Option < N > > {
187
+ pub ( crate ) fn extend_nodes ( old : BTreeMap < NID , N > , new : & BTreeMap < NID , N > ) -> BTreeMap < NID , N > {
219
188
let mut res = old;
220
189
221
190
for ( k, v) in new. iter ( ) {
@@ -233,7 +202,7 @@ where
233
202
self . configs . len ( ) > 1
234
203
}
235
204
236
- pub ( crate ) fn add_learner ( & self , node_id : NID , node : Option < N > ) -> Result < Self , MissingNodeInfo < NID > > {
205
+ pub ( crate ) fn add_learner ( & self , node_id : NID , node : N ) -> Result < Self , MissingNodeInfo < NID > > {
237
206
let configs = self . configs . clone ( ) ;
238
207
239
208
let nodes = Self :: extend_nodes ( self . nodes . clone ( ) , & btreemap ! { node_id=>node} ) ;
@@ -289,13 +258,12 @@ where
289
258
}
290
259
291
260
/// Get a the node(either voter or learner) by node id.
292
- pub ( crate ) fn get_node ( & self , node_id : & NID ) -> Option < & N > {
293
- let x = self . nodes . get ( node_id) ?;
294
- x. as_ref ( )
261
+ pub ( crate ) fn get_node ( & self , node_id : & NID ) -> & N {
262
+ & self . nodes [ node_id]
295
263
}
296
264
297
265
/// Returns an Iterator of all nodes(voters and learners).
298
- pub fn nodes ( & self ) -> impl Iterator < Item = ( & NID , & Option < N > ) > {
266
+ pub fn nodes ( & self ) -> impl Iterator < Item = ( & NID , & N ) > {
299
267
self . nodes . iter ( )
300
268
}
301
269
0 commit comments