|
| 1 | +// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + config::Config, raw::client::Client, BoundRange, ColumnFamily, Key, KvPair, Result, Value, |
| 5 | +}; |
| 6 | +use futures::executor::block_on; |
| 7 | +use slog::{Drain, Logger}; |
| 8 | +use std::u32; |
| 9 | + |
| 10 | +#[derive(Clone)] |
| 11 | +pub struct SyncClient { |
| 12 | + client: Client, |
| 13 | +} |
| 14 | + |
| 15 | +impl SyncClient { |
| 16 | + /// The synchronous version of RawClient |
| 17 | + /// |
| 18 | + /// # Examples |
| 19 | + /// |
| 20 | + /// ```rust,no_run |
| 21 | + /// # use tikv_client::SyncRawClient; |
| 22 | + /// let client = SyncRawClient::new(vec!["192.168.0.100"], None).unwrap(); |
| 23 | + /// ``` |
| 24 | + pub fn new<S: Into<String>>( |
| 25 | + pd_endpoints: Vec<S>, |
| 26 | + logger: Option<Logger>, |
| 27 | + ) -> Result<SyncClient> { |
| 28 | + Self::new_with_config(pd_endpoints, Config::default(), logger) |
| 29 | + } |
| 30 | + |
| 31 | + /// Create a raw [`SyncClient`] with a custom configuration, and connect to the TiKV cluster. |
| 32 | + /// |
| 33 | + /// # Examples |
| 34 | + /// |
| 35 | + /// ```rust,no_run |
| 36 | + /// # use tikv_client::{Config, SyncRawClient}; |
| 37 | + /// # use std::time::Duration; |
| 38 | + /// let client = SyncRawClient::new_with_config( |
| 39 | + /// vec!["192.168.0.100"], |
| 40 | + /// Config::default().with_timeout(Duration::from_secs(60)), |
| 41 | + /// None, |
| 42 | + /// ).unwrap(); |
| 43 | + /// ``` |
| 44 | + pub fn new_with_config<S: Into<String>>( |
| 45 | + pd_endpoints: Vec<S>, |
| 46 | + config: Config, |
| 47 | + logger: Option<Logger>, |
| 48 | + ) -> Result<SyncClient> { |
| 49 | + let client = block_on(Client::new_with_config(pd_endpoints, config, logger)).unwrap(); |
| 50 | + Ok(SyncClient { client: client }) |
| 51 | + } |
| 52 | + |
| 53 | + pub fn with_cf(&self, cf: ColumnFamily) -> SyncClient { |
| 54 | + SyncClient { |
| 55 | + client: self.client.with_cf(cf), |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + pub fn with_atomic_for_cas(&self) -> SyncClient { |
| 60 | + SyncClient { |
| 61 | + client: self.client.with_atomic_for_cas(), |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// Create a new 'get' request. |
| 66 | + /// |
| 67 | + /// Once resolved this request will result in the fetching of the value associated with the |
| 68 | + /// given key. |
| 69 | + /// |
| 70 | + /// Retuning `Ok(None)` indicates the key does not exist in TiKV. |
| 71 | + /// |
| 72 | + /// # Examples |
| 73 | + /// ```rust,no_run |
| 74 | + /// # use tikv_client::{Value, Config, SyncRawClient}; |
| 75 | + /// # let client = SyncRawClient::new(vec!["192.168.0.100"], None).unwrap(); |
| 76 | + /// let key = "TiKV".to_owned(); |
| 77 | + /// let req = client.get(key); |
| 78 | + /// let result: Option<Value> = req.unwrap(); |
| 79 | + /// ``` |
| 80 | + pub fn get(&self, key: impl Into<Key>) -> Result<Option<Value>> { |
| 81 | + block_on(self.client.get(key)) |
| 82 | + } |
| 83 | + |
| 84 | + /// Create a new 'batch get' request. |
| 85 | + /// |
| 86 | + /// Once resolved this request will result in the fetching of the values associated with the |
| 87 | + /// given keys. |
| 88 | + /// |
| 89 | + /// Non-existent entries will not appear in the result. The order of the keys is not retained in the result. |
| 90 | + /// |
| 91 | + /// # Examples |
| 92 | + /// ```rust,no_run |
| 93 | + /// # use tikv_client::{KvPair, Config, SyncRawClient}; |
| 94 | + /// # let client = SyncRawClient::new(vec!["192.168.0.100"], None).unwrap(); |
| 95 | + /// let keys = vec!["TiKV".to_owned(), "TiDB".to_owned()]; |
| 96 | + /// let req = client.batch_get(keys); |
| 97 | + /// let result: Vec<KvPair> = req.unwrap(); |
| 98 | + /// ``` |
| 99 | + pub fn batch_get(&self, keys: impl IntoIterator<Item = impl Into<Key>>) -> Result<Vec<KvPair>> { |
| 100 | + block_on(self.client.batch_get(keys)) |
| 101 | + } |
| 102 | + |
| 103 | + /// Create a new 'put' request. |
| 104 | + /// |
| 105 | + /// Once resolved this request will result in the setting of the value associated with the given key. |
| 106 | + /// |
| 107 | + /// # Examples |
| 108 | + /// ```rust,no_run |
| 109 | + /// # use tikv_client::{Key, Value, Config, SyncRawClient}; |
| 110 | + /// # let client = SyncRawClient::new(vec!["192.168.0.100"], None).unwrap(); |
| 111 | + /// let key = "TiKV".to_owned(); |
| 112 | + /// let val = "TiKV".to_owned(); |
| 113 | + /// let req = client.put(key, val); |
| 114 | + /// let result: () = req.unwrap(); |
| 115 | + /// ``` |
| 116 | + pub fn put(&self, key: impl Into<Key>, value: impl Into<Value>) -> Result<()> { |
| 117 | + block_on(self.client.put(key, value)) |
| 118 | + } |
| 119 | + |
| 120 | + /// Create a new 'batch put' request. |
| 121 | + /// |
| 122 | + /// Once resolved this request will result in the setting of the values associated with the given keys. |
| 123 | + /// |
| 124 | + /// # Examples |
| 125 | + /// ```rust,no_run |
| 126 | + /// # use tikv_client::{Result, KvPair, Key, Value, Config, SyncRawClient, IntoOwnedRange}; |
| 127 | + /// # let client = SyncRawClient::new(vec!["192.168.0.100"], None).unwrap(); |
| 128 | + /// let kvpair1 = ("PD".to_owned(), "Go".to_owned()); |
| 129 | + /// let kvpair2 = ("TiKV".to_owned(), "Rust".to_owned()); |
| 130 | + /// let iterable = vec![kvpair1, kvpair2]; |
| 131 | + /// let req = client.batch_put(iterable); |
| 132 | + /// let result: () = req.unwrap(); |
| 133 | + /// ``` |
| 134 | + pub fn batch_put(&self, pairs: impl IntoIterator<Item = impl Into<KvPair>>) -> Result<()> { |
| 135 | + block_on(self.client.batch_put(pairs)) |
| 136 | + } |
| 137 | + |
| 138 | + /// Create a new 'delete' request. |
| 139 | + /// |
| 140 | + /// Once resolved this request will result in the deletion of the given key. |
| 141 | + /// |
| 142 | + /// It does not return an error if the key does not exist in TiKV. |
| 143 | + /// |
| 144 | + /// # Examples |
| 145 | + /// ```rust,no_run |
| 146 | + /// # use tikv_client::{Key, Config, SyncRawClient}; |
| 147 | + /// # let client = SyncRawClient::new(vec!["192.168.0.100"], None).unwrap(); |
| 148 | + /// let key = "TiKV".to_owned(); |
| 149 | + /// let req = client.delete(key); |
| 150 | + /// let result: () = req.unwrap(); |
| 151 | + /// ``` |
| 152 | + pub fn delete(&self, key: impl Into<Key>) -> Result<()> { |
| 153 | + block_on(self.client.delete(key)) |
| 154 | + } |
| 155 | + |
| 156 | + /// Create a new 'batch delete' request. |
| 157 | + /// |
| 158 | + /// Once resolved this request will result in the deletion of the given keys. |
| 159 | + /// |
| 160 | + /// It does not return an error if some of the keys do not exist and will delete the others. |
| 161 | + /// |
| 162 | + /// # Examples |
| 163 | + /// ```rust,no_run |
| 164 | + /// # use tikv_client::{Config, SyncRawClient}; |
| 165 | + /// # let client = SyncRawClient::new(vec!["192.168.0.100"], None).unwrap(); |
| 166 | + /// let keys = vec!["TiKV".to_owned(), "TiDB".to_owned()]; |
| 167 | + /// let req = client.batch_delete(keys); |
| 168 | + /// let result: () = req.unwrap(); |
| 169 | + /// ``` |
| 170 | + pub fn batch_delete(&self, keys: impl IntoIterator<Item = impl Into<Key>>) -> Result<()> { |
| 171 | + block_on(self.client.batch_delete(keys)) |
| 172 | + } |
| 173 | + |
| 174 | + /// Create a new 'delete range' request. |
| 175 | + /// |
| 176 | + /// Once resolved this request will result in the deletion of all keys lying in the given range. |
| 177 | + /// |
| 178 | + /// # Examples |
| 179 | + /// ```rust,no_run |
| 180 | + /// # use tikv_client::{Key, Config, SyncRawClient, IntoOwnedRange}; |
| 181 | + /// # let client = SyncRawClient::new(vec!["192.168.0.100"], None).unwrap(); |
| 182 | + /// let inclusive_range = "TiKV"..="TiDB"; |
| 183 | + /// let req = client.delete_range(inclusive_range.into_owned()); |
| 184 | + /// let result: () = req.unwrap(); |
| 185 | + /// ``` |
| 186 | + pub fn delete_range(&self, range: impl Into<BoundRange>) -> Result<()> { |
| 187 | + block_on(self.client.delete_range(range)) |
| 188 | + } |
| 189 | + |
| 190 | + /// Create a new 'scan' request. |
| 191 | + /// |
| 192 | + /// Once resolved this request will result in a `Vec` of key-value pairs that lies in the specified range. |
| 193 | + /// |
| 194 | + /// If the number of eligible key-value pairs are greater than `limit`, |
| 195 | + /// only the first `limit` pairs are returned, ordered by the key. |
| 196 | + /// |
| 197 | + /// |
| 198 | + /// # Examples |
| 199 | + /// ```rust,no_run |
| 200 | + /// # use tikv_client::{KvPair, Config, SyncRawClient, IntoOwnedRange}; |
| 201 | + /// # let client = SyncRawClient::new(vec!["192.168.0.100"], None).unwrap(); |
| 202 | + /// let inclusive_range = "TiKV"..="TiDB"; |
| 203 | + /// let req = client.scan(inclusive_range.into_owned(), 2); |
| 204 | + /// let result: Vec<KvPair> = req.unwrap(); |
| 205 | + /// ``` |
| 206 | + pub fn scan(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<KvPair>> { |
| 207 | + block_on(self.client.scan(range, limit)) |
| 208 | + } |
| 209 | + |
| 210 | + /// Create a new 'scan' request that only returns the keys. |
| 211 | + /// |
| 212 | + /// Once resolved this request will result in a `Vec` of keys that lies in the specified range. |
| 213 | + /// |
| 214 | + /// If the number of eligible keys are greater than `limit`, |
| 215 | + /// only the first `limit` pairs are returned, ordered by the key. |
| 216 | + /// |
| 217 | + /// |
| 218 | + /// # Examples |
| 219 | + /// ```rust,no_run |
| 220 | + /// # use tikv_client::{Key, Config, SyncRawClient, IntoOwnedRange}; |
| 221 | + /// # let client = SyncRawClient::new(vec!["192.168.0.100"], None).unwrap(); |
| 222 | + /// let inclusive_range = "TiKV"..="TiDB"; |
| 223 | + /// let req = client.scan_keys(inclusive_range.into_owned(), 2); |
| 224 | + /// let result: Vec<Key> = req.unwrap(); |
| 225 | + /// ``` |
| 226 | + pub fn scan_keys(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<Key>> { |
| 227 | + block_on(self.client.scan_keys(range, limit)) |
| 228 | + } |
| 229 | + |
| 230 | + /// Create a new 'batch scan' request. |
| 231 | + /// |
| 232 | + /// Once resolved this request will result in a set of scanners over the given keys. |
| 233 | + /// |
| 234 | + /// **Warning**: This method is experimental. The `each_limit` parameter does not work as expected. |
| 235 | + /// It does not limit the number of results returned of each range, |
| 236 | + /// instead it limits the number of results in each region of each range. |
| 237 | + /// As a result, you may get **more than** `each_limit` key-value pairs for each range. |
| 238 | + /// But you should not miss any entries. |
| 239 | + /// |
| 240 | + /// # Examples |
| 241 | + /// ```rust,no_run |
| 242 | + /// # use tikv_client::{Key, Config, SyncRawClient, IntoOwnedRange}; |
| 243 | + /// # let client = SyncRawClient::new(vec!["192.168.0.100"], None).unwrap(); |
| 244 | + /// let inclusive_range1 = "TiDB"..="TiKV"; |
| 245 | + /// let inclusive_range2 = "TiKV"..="TiSpark"; |
| 246 | + /// let iterable = vec![inclusive_range1.into_owned(), inclusive_range2.into_owned()]; |
| 247 | + /// let result = client.batch_scan(iterable, 2); |
| 248 | + /// ``` |
| 249 | + pub fn batch_scan( |
| 250 | + &self, |
| 251 | + ranges: impl IntoIterator<Item = impl Into<BoundRange>>, |
| 252 | + each_limit: u32, |
| 253 | + ) -> Result<Vec<KvPair>> { |
| 254 | + block_on(self.client.batch_scan(ranges, each_limit)) |
| 255 | + } |
| 256 | + |
| 257 | + /// Create a new 'batch scan' request that only returns the keys. |
| 258 | + /// |
| 259 | + /// Once resolved this request will result in a set of scanners over the given keys. |
| 260 | + /// |
| 261 | + /// **Warning**: This method is experimental. |
| 262 | + /// The `each_limit` parameter does not limit the number of results returned of each range, |
| 263 | + /// instead it limits the number of results in each region of each range. |
| 264 | + /// As a result, you may get **more than** `each_limit` key-value pairs for each range, |
| 265 | + /// but you should not miss any entries. |
| 266 | + /// |
| 267 | + /// # Examples |
| 268 | + /// ```rust,no_run |
| 269 | + /// # use tikv_client::{Key, Config, SyncRawClient, IntoOwnedRange}; |
| 270 | + /// # let client = SyncRawClient::new(vec!["192.168.0.100"], None).unwrap(); |
| 271 | + /// let inclusive_range1 = "TiDB"..="TiKV"; |
| 272 | + /// let inclusive_range2 = "TiKV"..="TiSpark"; |
| 273 | + /// let iterable = vec![inclusive_range1.into_owned(), inclusive_range2.into_owned()]; |
| 274 | + /// let result = client.batch_scan(iterable, 2); |
| 275 | + /// ``` |
| 276 | + pub fn batch_scan_keys( |
| 277 | + &self, |
| 278 | + ranges: impl IntoIterator<Item = impl Into<BoundRange>>, |
| 279 | + each_limit: u32, |
| 280 | + ) -> Result<Vec<Key>> { |
| 281 | + block_on(self.client.batch_scan_keys(ranges, each_limit)) |
| 282 | + } |
| 283 | + |
| 284 | + pub fn compare_and_swap( |
| 285 | + &self, |
| 286 | + key: impl Into<Key>, |
| 287 | + previous_value: impl Into<Option<Value>>, |
| 288 | + new_value: impl Into<Value>, |
| 289 | + ) -> Result<(Option<Value>, bool)> { |
| 290 | + block_on(self.client.compare_and_swap(key, previous_value, new_value)) |
| 291 | + } |
| 292 | +} |
0 commit comments