4
4
#![ allow( dead_code) ]
5
5
6
6
use std:: {
7
- collections:: hash_map :: { self , HashMap } ,
7
+ collections:: { HashMap , VecDeque } ,
8
8
fmt,
9
9
sync:: { Arc , RwLock } ,
10
10
time:: Duration ,
@@ -17,11 +17,11 @@ use grpcio::{EnvBuilder, Environment};
17
17
use kvproto:: kvrpcpb;
18
18
19
19
use crate :: {
20
- compat:: { loop_fn , stream_fn, ClientFutureExt , Loop } ,
20
+ compat:: { stream_fn, ClientFutureExt } ,
21
21
kv:: BoundRange ,
22
22
raw:: ColumnFamily ,
23
23
rpc:: {
24
- pd:: { PdClient , Region , RegionId , RegionVerId , Store , StoreId } ,
24
+ pd:: { PdClient , Region , RegionId , Store , StoreId } ,
25
25
security:: SecurityManager ,
26
26
tikv:: KvClient ,
27
27
} ,
@@ -183,26 +183,24 @@ impl RpcClient {
183
183
cf : Option < ColumnFamily > ,
184
184
) -> impl Future < Output = Result < Vec < KvPair > > > {
185
185
let inner = self . inner . clone ( ) ;
186
- self . group_tasks_by_region ( keys)
187
- . and_then ( move |task_groups| {
188
- let tasks: Vec < _ > = task_groups
189
- . into_iter ( )
190
- . map ( |( region_ver_id, keys) | {
191
- let cf = cf. clone ( ) ;
192
- inner
193
- . clone ( )
194
- . raw_from_id ( region_ver_id. id )
195
- . and_then ( |context| {
196
- context
197
- . client
198
- . raw_batch_get ( context. region , cf, keys. into_iter ( ) )
199
- } )
186
+ self . group_tasks_by_region ( keys) . try_fold (
187
+ Vec :: new ( ) ,
188
+ move |mut result, ( region_id, keys) | {
189
+ let cf = cf. clone ( ) ;
190
+ inner
191
+ . clone ( )
192
+ . raw_from_id ( region_id)
193
+ . and_then ( |context| {
194
+ context
195
+ . client
196
+ . raw_batch_get ( context. region , cf, keys. into_iter ( ) )
200
197
} )
201
- . collect ( ) ;
202
-
203
- future:: try_join_all ( tasks)
204
- } )
205
- . map_ok ( |r| r. into_iter ( ) . flat_map ( |a| a. into_iter ( ) ) . collect ( ) )
198
+ . map_ok ( move |mut pairs| {
199
+ result. append ( & mut pairs) ;
200
+ result
201
+ } )
202
+ } ,
203
+ )
206
204
}
207
205
208
206
pub fn raw_put (
@@ -232,23 +230,15 @@ impl RpcClient {
232
230
future:: Either :: Left ( future:: err ( Error :: empty_value ( ) ) )
233
231
} else {
234
232
let inner = self . inner . clone ( ) ;
235
- future:: Either :: Right (
236
- self . group_tasks_by_region ( pairs)
237
- . and_then ( move |task_groups| {
238
- let tasks: Vec < _ > = task_groups
239
- . into_iter ( )
240
- . map ( |( region, keys) | {
241
- let cf = cf. clone ( ) ;
242
- inner. clone ( ) . raw_from_id ( region. id ) . and_then ( |context| {
243
- context. client . raw_batch_put ( context. region , cf, keys)
244
- } )
245
- } )
246
- . collect ( ) ;
247
-
248
- future:: try_join_all ( tasks)
249
- } )
250
- . map_ok ( |_| ( ) ) ,
251
- )
233
+ future:: Either :: Right ( self . group_tasks_by_region ( pairs) . try_for_each (
234
+ move |( region_id, keys) | {
235
+ let cf = cf. clone ( ) ;
236
+ inner
237
+ . clone ( )
238
+ . raw_from_id ( region_id)
239
+ . and_then ( |context| context. client . raw_batch_put ( context. region , cf, keys) )
240
+ } ,
241
+ ) )
252
242
}
253
243
}
254
244
@@ -270,20 +260,13 @@ impl RpcClient {
270
260
) -> impl Future < Output = Result < ( ) > > {
271
261
let inner = self . inner . clone ( ) ;
272
262
self . group_tasks_by_region ( keys)
273
- . and_then ( move |task_groups| {
274
- let tasks: Vec < _ > = task_groups
275
- . into_iter ( )
276
- . map ( |( region, keys) | {
277
- let cf = cf. clone ( ) ;
278
- inner. clone ( ) . raw_from_id ( region. id ) . and_then ( |context| {
279
- context. client . raw_batch_delete ( context. region , cf, keys)
280
- } )
281
- } )
282
- . collect ( ) ;
283
-
284
- future:: try_join_all ( tasks)
263
+ . try_for_each ( move |( region_id, keys) | {
264
+ let cf = cf. clone ( ) ;
265
+ inner
266
+ . clone ( )
267
+ . raw_from_id ( region_id)
268
+ . and_then ( |context| context. client . raw_batch_delete ( context. region , cf, keys) )
285
269
} )
286
- . map_ok ( |_| ( ) )
287
270
}
288
271
289
272
pub fn raw_scan (
@@ -368,42 +351,31 @@ impl RpcClient {
368
351
fn group_tasks_by_region < Task > (
369
352
& self ,
370
353
tasks : Vec < Task > ,
371
- ) -> impl Future < Output = Result < GroupedTasks < Task > > >
354
+ ) -> impl Stream < Item = Result < ( RegionId , Vec < Task > ) > >
372
355
where
373
356
Task : GroupingTask ,
374
357
{
375
- let result: Option < GroupedTasks < Task > > = None ;
376
358
let inner = self . inner . clone ( ) ;
377
- loop_fn ( ( 0 , tasks, result) , move |( mut index, tasks, mut result) | {
378
- if index == tasks. len ( ) {
379
- future:: Either :: Left ( future:: ok ( Loop :: Break ( result) ) )
359
+ let tasks: VecDeque < Task > = tasks. into ( ) ;
360
+
361
+ stream_fn ( tasks, move |mut tasks| {
362
+ if tasks. is_empty ( ) {
363
+ return Either :: Right ( ready ( Ok ( None ) ) ) ;
380
364
} else {
381
- let inner = Arc :: clone ( & inner) ;
382
- future:: Either :: Right ( inner. get_region ( tasks[ index] . key ( ) ) . map_ok (
383
- move |location| {
384
- while let Some ( item) = tasks. get ( index) {
385
- if !location. contains ( item. key ( ) ) {
386
- break ;
387
- }
388
- let ver_id = location. ver_id ( ) ;
389
- let item = item. clone ( ) ;
390
- if let Some ( ref mut grouped) = result {
391
- grouped. add ( ver_id, item) ;
392
- } else {
393
- result = Some ( GroupedTasks :: new ( ver_id, item) ) ;
394
- }
395
- index += 1 ;
396
- }
397
- if index == tasks. len ( ) {
398
- Loop :: Break ( result)
399
- } else {
400
- Loop :: Continue ( ( index, tasks, result) )
365
+ let inner = inner. clone ( ) ;
366
+ Either :: Left ( inner. get_region ( tasks[ 0 ] . key ( ) ) . map_ok ( move |location| {
367
+ let ver_id = location. ver_id ( ) ;
368
+ let mut grouped = Vec :: new ( ) ;
369
+ while let Some ( task) = tasks. pop_front ( ) {
370
+ if !location. contains ( task. key ( ) ) {
371
+ break ;
401
372
}
402
- } ,
403
- ) )
373
+ grouped. push ( task) ;
374
+ }
375
+ Some ( ( tasks, ( ver_id. id , grouped) ) )
376
+ } ) )
404
377
}
405
378
} )
406
- . map_ok ( |r| r. unwrap_or_default ( ) )
407
379
}
408
380
}
409
381
@@ -463,34 +435,6 @@ trait GroupingTask: Clone + Default + Sized {
463
435
fn key ( & self ) -> & Key ;
464
436
}
465
437
466
- #[ derive( Default ) ]
467
- struct GroupedTasks < Task : GroupingTask > ( HashMap < RegionVerId , Vec < Task > > , RegionVerId ) ;
468
-
469
- impl < Task : GroupingTask > GroupedTasks < Task > {
470
- fn new ( ver_id : RegionVerId , task : Task ) -> Self {
471
- let mut map = HashMap :: with_capacity ( 1 ) ;
472
- map. insert ( ver_id. clone ( ) , vec ! [ task] ) ;
473
- GroupedTasks ( map, ver_id)
474
- }
475
-
476
- #[ inline]
477
- fn add ( & mut self , ver_id : RegionVerId , task : Task ) {
478
- self . 0
479
- . entry ( ver_id)
480
- . or_insert_with ( || Vec :: with_capacity ( 1 ) )
481
- . push ( task)
482
- }
483
- }
484
-
485
- impl < Task : GroupingTask > IntoIterator for GroupedTasks < Task > {
486
- type Item = ( RegionVerId , Vec < Task > ) ;
487
- type IntoIter = hash_map:: IntoIter < RegionVerId , Vec < Task > > ;
488
-
489
- fn into_iter ( self ) -> hash_map:: IntoIter < RegionVerId , Vec < Task > > {
490
- self . 0 . into_iter ( )
491
- }
492
- }
493
-
494
438
impl GroupingTask for Key {
495
439
fn key ( & self ) -> & Key {
496
440
self
0 commit comments