File tree Expand file tree Collapse file tree 3 files changed +26
-4
lines changed Expand file tree Collapse file tree 3 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -744,11 +744,11 @@ impl<PdC: PdClient> Client<PdC> {
744
744
}
745
745
746
746
fn assert_non_atomic ( & self ) -> Result < ( ) > {
747
- ( !self . atomic ) . then ( || ( ) ) . ok_or ( Error :: UnsupportedMode )
747
+ ( !self . atomic ) . then_some ( ( ) ) . ok_or ( Error :: UnsupportedMode )
748
748
}
749
749
750
750
fn assert_atomic ( & self ) -> Result < ( ) > {
751
- self . atomic . then ( || ( ) ) . ok_or ( Error :: UnsupportedMode )
751
+ self . atomic . then_some ( ( ) ) . ok_or ( Error :: UnsupportedMode )
752
752
}
753
753
}
754
754
Original file line number Diff line number Diff line change @@ -208,8 +208,7 @@ impl<PdC: PdClient> Transaction<PdC> {
208
208
/// ```
209
209
pub async fn key_exists ( & mut self , key : impl Into < Key > ) -> Result < bool > {
210
210
debug ! ( self . logger, "invoking transactional key_exists request" ) ;
211
- let key = key. into ( ) ;
212
- Ok ( self . scan_keys ( key. clone ( ) ..=key, 1 ) . await ?. next ( ) . is_some ( ) )
211
+ Ok ( self . get ( key) . await ?. is_some ( ) )
213
212
}
214
213
215
214
/// Create a new 'batch get' request.
Original file line number Diff line number Diff line change @@ -919,6 +919,29 @@ async fn txn_scan_reverse() -> Result<()> {
919
919
Ok ( ( ) )
920
920
}
921
921
922
+ #[ tokio:: test]
923
+ #[ serial]
924
+ async fn txn_key_exists ( ) -> Result < ( ) > {
925
+ init ( ) . await ?;
926
+ let client = TransactionClient :: new_with_config ( pd_addrs ( ) , Default :: default ( ) , None ) . await ?;
927
+ let key = "key" . to_owned ( ) ;
928
+ let value = "value" . to_owned ( ) ;
929
+ let mut t1 = client. begin_optimistic ( ) . await ?;
930
+ t1. put ( key. clone ( ) , value. clone ( ) ) . await ?;
931
+ assert ! ( t1. key_exists( key. clone( ) ) . await ?) ;
932
+ t1. commit ( ) . await ?;
933
+
934
+ let mut t2 = client. begin_optimistic ( ) . await ?;
935
+ assert ! ( t2. key_exists( key) . await ?) ;
936
+ t2. commit ( ) . await ?;
937
+
938
+ let not_exists_key = "not_exists_key" . to_owned ( ) ;
939
+ let mut t3 = client. begin_optimistic ( ) . await ?;
940
+ assert ! ( !t3. key_exists( not_exists_key) . await ?) ;
941
+ t3. commit ( ) . await ?;
942
+ Ok ( ( ) )
943
+ }
944
+
922
945
// helper function
923
946
async fn get_u32 ( client : & RawClient , key : Vec < u8 > ) -> Result < u32 > {
924
947
let x = client. get ( key) . await ?. unwrap ( ) ;
You can’t perform that action at this time.
0 commit comments