@@ -136,6 +136,26 @@ impl Client {
136
136
& self . connections [ ( self . hash_function ) ( key) as usize % connections_count]
137
137
}
138
138
139
+ /// Distributes the input `keys` to the available `connections`.
140
+ ///
141
+ /// This uses the `hash_function` internally, and the returned [`Vec`] matches
142
+ /// the available `connections`.
143
+ fn distribute_keys < ' a > ( & self , keys : & [ & ' a str ] ) -> Result < Vec < Vec < & ' a str > > , MemcacheError > {
144
+ for key in keys {
145
+ check_key_len ( key) ?;
146
+ }
147
+
148
+ let connections_count = self . connections . len ( ) ;
149
+ let mut con_keys = Vec :: new ( ) ;
150
+ con_keys. resize_with ( connections_count, Vec :: new) ;
151
+ for key in keys {
152
+ let connection_index = ( self . hash_function ) ( key) as usize % connections_count;
153
+ con_keys[ connection_index] . push ( * key) ;
154
+ }
155
+
156
+ Ok ( con_keys)
157
+ }
158
+
139
159
/// Set the socket read timeout for TCP connections.
140
160
///
141
161
/// Example:
@@ -247,21 +267,13 @@ impl Client {
247
267
/// assert_eq!(result["foo"], "42");
248
268
/// ```
249
269
pub fn gets < V : FromMemcacheValueExt > ( & self , keys : & [ & str ] ) -> Result < HashMap < String , V > , MemcacheError > {
250
- for key in keys {
251
- check_key_len ( key) ?;
252
- }
253
- let mut con_keys: HashMap < usize , Vec < & str > > = HashMap :: new ( ) ;
254
- let mut result: HashMap < String , V > = HashMap :: new ( ) ;
255
- let connections_count = self . connections . len ( ) ;
270
+ let distributed_keys = self . distribute_keys ( keys) ?;
256
271
257
- for key in keys {
258
- let connection_index = ( self . hash_function ) ( key) as usize % connections_count;
259
- let array = con_keys. entry ( connection_index) . or_default ( ) ;
260
- array. push ( key) ;
261
- }
262
- for ( & connection_index, keys) in con_keys. iter ( ) {
263
- let connection = self . connections [ connection_index] . clone ( ) ;
264
- result. extend ( connection. get ( ) ?. gets ( keys) ?) ;
272
+ let mut result: HashMap < String , V > = HashMap :: new ( ) ;
273
+ for ( connection, keys) in self . connections . iter ( ) . zip ( distributed_keys) {
274
+ if !keys. is_empty ( ) {
275
+ result. extend ( connection. get ( ) ?. gets ( & keys) ?) ;
276
+ }
265
277
}
266
278
Ok ( result)
267
279
}
0 commit comments