4
4
#![ allow( dead_code) ]
5
5
6
6
use std:: {
7
- collections:: HashMap ,
7
+ collections:: hash_map :: { self , HashMap } ,
8
8
fmt,
9
- ops:: Deref ,
10
9
sync:: { Arc , RwLock } ,
11
10
time:: Duration ,
12
11
} ;
@@ -107,7 +106,7 @@ impl RpcClientInner {
107
106
}
108
107
109
108
fn locate_key ( & self , key : & Key ) -> impl Future < Output = Result < KeyLocation > > {
110
- self . load_region ( key) . map_ok ( KeyLocation :: new )
109
+ self . load_region ( key)
111
110
}
112
111
113
112
fn kv_client ( & self , context : RegionContext ) -> Result < ( RegionContext , Arc < KvClient > ) > {
@@ -197,7 +196,7 @@ impl RpcClient {
197
196
let peer = location. peer ( ) . expect ( "leader must exist" ) ;
198
197
let store_id = peer. get_store_id ( ) ;
199
198
inner. load_store ( store_id) . map_ok ( |store| RegionContext {
200
- region : location. into_inner ( ) ,
199
+ region : location,
201
200
store,
202
201
} )
203
202
} )
@@ -257,7 +256,6 @@ impl RpcClient {
257
256
let inner = self . inner ( ) ;
258
257
self . group_tasks_by_region ( keys)
259
258
. and_then ( move |task_groups| {
260
- let task_groups = task_groups. into_inner ( ) ;
261
259
let mut tasks = Vec :: with_capacity ( task_groups. len ( ) ) ;
262
260
for ( region, keys) in task_groups. into_iter ( ) {
263
261
let inner = Arc :: clone ( & inner) ;
@@ -302,7 +300,6 @@ impl RpcClient {
302
300
Either :: Right (
303
301
self . group_tasks_by_region ( pairs)
304
302
. and_then ( move |task_groups| {
305
- let task_groups = task_groups. into_inner ( ) ;
306
303
let mut tasks = Vec :: with_capacity ( task_groups. len ( ) ) ;
307
304
for ( region, pairs) in task_groups. into_iter ( ) {
308
305
let inner = Arc :: clone ( & inner) ;
@@ -336,7 +333,6 @@ impl RpcClient {
336
333
let inner = self . inner ( ) ;
337
334
self . group_tasks_by_region ( keys)
338
335
. and_then ( move |task_groups| {
339
- let task_groups = task_groups. into_inner ( ) ;
340
336
let mut tasks = Vec :: with_capacity ( task_groups. len ( ) ) ;
341
337
for ( region, keys) in task_groups. into_iter ( ) {
342
338
let inner = Arc :: clone ( & inner) ;
@@ -374,21 +370,27 @@ impl RpcClient {
374
370
let inner = Arc :: clone ( & self . inner ) ;
375
371
loop_fn ( ( inner, scan) , |( inner, scan) | {
376
372
inner. locate_key ( scan. start_key ( ) ) . and_then ( |location| {
377
- let region = location. into_inner ( ) ;
378
- let cf = scan. cf . clone ( ) ;
379
- Self :: region_context_by_id ( Arc :: clone ( & inner) , region. id )
373
+ let region = location;
374
+ let cf = scan. state . cf . clone ( ) ;
375
+ Self :: region_context_by_id ( Arc :: clone ( & inner) , region. id ( ) )
380
376
. map_ok ( |( region, client) | {
381
377
( scan, region. range ( ) , RawContext :: new ( region, client, cf) )
382
378
} )
383
379
. and_then ( |( mut scan, region_range, context) | {
384
380
let ( start_key, end_key) = scan. range ( ) ;
385
381
context
386
382
. client ( )
387
- . raw_scan ( context, start_key, end_key, scan. limit , scan. key_only )
383
+ . raw_scan (
384
+ context,
385
+ start_key,
386
+ end_key,
387
+ scan. state . limit ,
388
+ scan. state . key_only ,
389
+ )
388
390
. map_ok ( |pairs| ( scan, region_range, pairs) )
389
391
} )
390
392
. map_ok ( |( mut scan, region_range, mut pairs) | {
391
- let limit = scan. limit ;
393
+ let limit = scan. state . limit ;
392
394
scan. result_mut ( ) . append ( & mut pairs) ;
393
395
if scan. result ( ) . len ( ) as u32 >= limit {
394
396
Loop :: Break ( scan. into_inner ( ) )
@@ -424,9 +426,9 @@ impl RpcClient {
424
426
let inner = Arc :: clone ( & self . inner ) ;
425
427
loop_fn ( ( inner, scan) , |( inner, scan) | {
426
428
inner. locate_key ( scan. start_key ( ) ) . and_then ( |location| {
427
- let region = location. into_inner ( ) ;
428
- let cf = scan. clone ( ) ;
429
- Self :: region_context_by_id ( Arc :: clone ( & inner) , region. id )
429
+ let region = location;
430
+ let cf = scan. state . clone ( ) ;
431
+ Self :: region_context_by_id ( Arc :: clone ( & inner) , region. id ( ) )
430
432
. map_ok ( |( region, client) | {
431
433
( scan, region. range ( ) , RawContext :: new ( region, client, cf) )
432
434
} )
@@ -482,9 +484,9 @@ impl RegionContext {
482
484
impl From < RegionContext > for kvrpcpb:: Context {
483
485
fn from ( mut ctx : RegionContext ) -> kvrpcpb:: Context {
484
486
let mut kvctx = kvrpcpb:: Context :: default ( ) ;
485
- kvctx. set_region_id ( ctx. region . id ) ;
486
- kvctx. set_region_epoch ( ctx. region . take_region_epoch ( ) ) ;
487
- kvctx. set_peer ( ctx. region . peer ( ) . expect ( "leader must exist" ) . into_inner ( ) ) ;
487
+ kvctx. set_region_id ( ctx. region . id ( ) ) ;
488
+ kvctx. set_region_epoch ( ctx. region . region . take_region_epoch ( ) ) ;
489
+ kvctx. set_peer ( ctx. region . peer ( ) . expect ( "leader must exist" ) ) ;
488
490
kvctx
489
491
}
490
492
}
@@ -523,25 +525,7 @@ impl TxnContext {
523
525
}
524
526
}
525
527
526
- struct KeyLocation ( Region ) ;
527
-
528
- impl KeyLocation {
529
- fn new ( region : Region ) -> Self {
530
- KeyLocation ( region)
531
- }
532
-
533
- fn into_inner ( self ) -> Region {
534
- self . 0
535
- }
536
- }
537
-
538
- impl Deref for KeyLocation {
539
- type Target = Region ;
540
-
541
- fn deref ( & self ) -> & Self :: Target {
542
- & self . 0
543
- }
544
- }
528
+ type KeyLocation = Region ;
545
529
546
530
trait GroupingTask : Clone + Default + Sized {
547
531
fn key ( & self ) -> & Key ;
@@ -550,36 +534,33 @@ trait GroupingTask: Clone + Default + Sized {
550
534
#[ derive( Default ) ]
551
535
struct GroupedTasks < Task : GroupingTask > ( HashMap < RegionVerId , Vec < Task > > , RegionVerId ) ;
552
536
553
- impl < Task > GroupedTasks < Task >
554
- where
555
- Task : GroupingTask ,
556
- {
537
+ impl < Task : GroupingTask > GroupedTasks < Task > {
557
538
fn new ( ver_id : RegionVerId , task : Task ) -> Self {
558
539
let mut map = HashMap :: with_capacity ( 1 ) ;
559
540
map. insert ( ver_id. clone ( ) , vec ! [ task] ) ;
560
541
GroupedTasks ( map, ver_id)
561
542
}
562
543
544
+ #[ inline]
563
545
fn add ( & mut self , ver_id : RegionVerId , task : Task ) {
564
546
self . 0
565
547
. entry ( ver_id)
566
548
. or_insert_with ( || Vec :: with_capacity ( 1 ) )
567
549
. push ( task)
568
550
}
569
551
570
- fn into_inner ( self ) -> HashMap < RegionVerId , Vec < Task > > {
571
- self . 0
552
+ #[ inline]
553
+ fn len ( & self ) -> usize {
554
+ self . 0 . len ( )
572
555
}
573
556
}
574
557
575
- impl < Task > Deref for GroupedTasks < Task >
576
- where
577
- Task : GroupingTask ,
578
- {
579
- type Target = HashMap < RegionVerId , Vec < Task > > ;
558
+ impl < Task : GroupingTask > IntoIterator for GroupedTasks < Task > {
559
+ type Item = ( RegionVerId , Vec < Task > ) ;
560
+ type IntoIter = hash_map:: IntoIter < RegionVerId , Vec < Task > > ;
580
561
581
- fn deref ( & self ) -> & Self :: Target {
582
- & self . 0
562
+ fn into_iter ( self ) -> hash_map :: IntoIter < RegionVerId , Vec < Task > > {
563
+ self . 0 . into_iter ( )
583
564
}
584
565
}
585
566
@@ -666,15 +647,3 @@ where
666
647
& self . result
667
648
}
668
649
}
669
-
670
- impl < Res , State > Deref for ScanRegionsContext < Res , State >
671
- where
672
- Res : Default ,
673
- State : Sized ,
674
- {
675
- type Target = State ;
676
-
677
- fn deref ( & self ) -> & Self :: Target {
678
- & self . state
679
- }
680
- }
0 commit comments