@@ -1028,27 +1028,108 @@ async fn txn_scan_reverse() -> Result<()> {
10281028 init ( ) . await ?;
10291029 let client = TransactionClient :: new_with_config ( pd_addrs ( ) , Default :: default ( ) ) . await ?;
10301030
1031- let k1 = b"a1" . to_vec ( ) ;
1032- let k2 = b"a2" . to_vec ( ) ;
1033- let v1 = b"b1" . to_vec ( ) ;
1034- let v2 = b"b2" . to_vec ( ) ;
1035-
1036- let reverse_resp = vec ! [
1037- ( Key :: from( k2. clone( ) ) , v2. clone( ) ) ,
1038- ( Key :: from( k1. clone( ) ) , v1. clone( ) ) ,
1039- ] ;
1031+ let k1 = b"k1" . to_vec ( ) ;
1032+ let k2 = b"k2" . to_vec ( ) ;
1033+ let k3 = b"k3" . to_vec ( ) ;
1034+
1035+ let v1 = b"v1" . to_vec ( ) ;
1036+ let v2 = b"v2" . to_vec ( ) ;
1037+ let v3 = b"v3" . to_vec ( ) ;
10401038
10411039 // Pessimistic option is not stable in this case. Use optimistic options instead.
10421040 let option = TransactionOptions :: new_optimistic ( ) . drop_check ( tikv_client:: CheckLevel :: Warn ) ;
10431041 let mut t = client. begin_with_options ( option. clone ( ) ) . await ?;
1044- t. put ( k1. clone ( ) , v1) . await ?;
1045- t. put ( k2. clone ( ) , v2) . await ?;
1042+ t. put ( k1. clone ( ) , v1. clone ( ) ) . await ?;
1043+ t. put ( k2. clone ( ) , v2. clone ( ) ) . await ?;
1044+ t. put ( k3. clone ( ) , v3. clone ( ) ) . await ?;
1045+ t. commit ( ) . await ?;
1046+
1047+ let mut t2 = client. begin_with_options ( option) . await ?;
1048+ {
1049+ // For [k1, k3]:
1050+ let bound_range: BoundRange = ( k1. clone ( ) ..=k3. clone ( ) ) . into ( ) ;
1051+ let resp = t2
1052+ . scan_reverse ( bound_range, 3 )
1053+ . await ?
1054+ . map ( |kv| ( kv. 0 , kv. 1 ) )
1055+ . collect :: < Vec < ( Key , Vec < u8 > ) > > ( ) ;
1056+ assert_eq ! (
1057+ resp,
1058+ vec![
1059+ ( Key :: from( k3. clone( ) ) , v3. clone( ) ) ,
1060+ ( Key :: from( k2. clone( ) ) , v2. clone( ) ) ,
1061+ ( Key :: from( k1. clone( ) ) , v1. clone( ) ) ,
1062+ ]
1063+ ) ;
1064+ }
1065+ {
1066+ // For [k1, k3):
1067+ let bound_range: BoundRange = ( k1. clone ( ) ..k3. clone ( ) ) . into ( ) ;
1068+ let resp = t2
1069+ . scan_reverse ( bound_range, 3 )
1070+ . await ?
1071+ . map ( |kv| ( kv. 0 , kv. 1 ) )
1072+ . collect :: < Vec < ( Key , Vec < u8 > ) > > ( ) ;
1073+ assert_eq ! (
1074+ resp,
1075+ vec![
1076+ ( Key :: from( k2. clone( ) ) , v2. clone( ) ) ,
1077+ ( Key :: from( k1. clone( ) ) , v1) ,
1078+ ]
1079+ ) ;
1080+ }
1081+ {
1082+ // For (k1, k3):
1083+ let mut start_key = k1. clone ( ) ;
1084+ start_key. push ( 0 ) ;
1085+ let bound_range: BoundRange = ( start_key..k3) . into ( ) ;
1086+ let resp = t2
1087+ . scan_reverse ( bound_range, 3 )
1088+ . await ?
1089+ . map ( |kv| ( kv. 0 , kv. 1 ) )
1090+ . collect :: < Vec < ( Key , Vec < u8 > ) > > ( ) ;
1091+ assert_eq ! ( resp, vec![ ( Key :: from( k2) , v2) , ] ) ;
1092+ }
1093+ t2. commit ( ) . await ?;
1094+
1095+ Ok ( ( ) )
1096+ }
1097+
1098+ #[ tokio:: test]
1099+ #[ serial]
1100+ async fn txn_scan_reverse_multi_regions ( ) -> Result < ( ) > {
1101+ init ( ) . await ?;
1102+ let client = TransactionClient :: new_with_config ( pd_addrs ( ) , Default :: default ( ) ) . await ?;
1103+
1104+ // Keys in `keys` should locate in different regions. See `init()` for boundary of regions.
1105+ let keys: Vec < Key > = vec ! [
1106+ 0x00000000_u32 . to_be_bytes( ) . to_vec( ) ,
1107+ 0x40000000_u32 . to_be_bytes( ) . to_vec( ) ,
1108+ 0x80000000_u32 . to_be_bytes( ) . to_vec( ) ,
1109+ 0xC0000000_u32 . to_be_bytes( ) . to_vec( ) ,
1110+ ]
1111+ . into_iter ( )
1112+ . map ( Into :: into)
1113+ . collect ( ) ;
1114+ let values: Vec < Vec < u8 > > = ( 0 ..keys. len ( ) )
1115+ . map ( |i| format ! ( "v{}" , i) . into_bytes ( ) )
1116+ . collect ( ) ;
1117+ let bound_range: BoundRange =
1118+ ( keys. first ( ) . unwrap ( ) . clone ( ) ..=keys. last ( ) . unwrap ( ) . clone ( ) ) . into ( ) ;
1119+
1120+ // Pessimistic option is not stable in this case. Use optimistic options instead.
1121+ let option = TransactionOptions :: new_optimistic ( ) . drop_check ( tikv_client:: CheckLevel :: Warn ) ;
1122+ let mut t = client. begin_with_options ( option. clone ( ) ) . await ?;
1123+ let mut reverse_resp = Vec :: with_capacity ( keys. len ( ) ) ;
1124+ for ( k, v) in keys. into_iter ( ) . zip ( values. into_iter ( ) ) . rev ( ) {
1125+ t. put ( k. clone ( ) , v. clone ( ) ) . await ?;
1126+ reverse_resp. push ( ( k, v) ) ;
1127+ }
10461128 t. commit ( ) . await ?;
10471129
10481130 let mut t2 = client. begin_with_options ( option) . await ?;
1049- let bound_range: BoundRange = ( k1..=k2) . into ( ) ;
10501131 let resp = t2
1051- . scan_reverse ( bound_range, 2 )
1132+ . scan_reverse ( bound_range, 100 )
10521133 . await ?
10531134 . map ( |kv| ( kv. 0 , kv. 1 ) )
10541135 . collect :: < Vec < ( Key , Vec < u8 > ) > > ( ) ;
0 commit comments