Skip to content

Commit 6353dbc

Browse files
committed
Migrate to futures 0.3
Signed-off-by: Nick Cameron <nrc@ncameron.org>
1 parent f4a1ccf commit 6353dbc

File tree

17 files changed

+872
-519
lines changed

17 files changed

+872
-519
lines changed

.travis.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ os:
66
# - windows # TODO: https://github.com/pingcap/kvproto/issues/355
77
- osx
88
rust:
9-
- stable
9+
# Requires nightly for now, stable can be re-enabled when 1.36 is stable.
10+
# - stable
1011
- nightly
1112
env:
1213
global:
@@ -27,14 +28,14 @@ addons:
2728
- go
2829

2930
install:
30-
- if [[ $TRAVIS_RUST_VERSION == "stable" && $TRAVIS_OS_NAME == "linux" ]]; then rustup component add rustfmt; fi
31-
- if [[ $TRAVIS_RUST_VERSION == "stable" && $TRAVIS_OS_NAME == "linux" ]]; then rustup component add clippy; fi
31+
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then rustup component add rustfmt; fi
32+
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then rustup component add clippy; fi
3233
- if [[ $TRAVIS_OS_NAME == "windows" ]]; then choco install golang cmake strawberryperl protoc; fi
3334
- if [[ $TRAVIS_OS_NAME == "windows" ]]; then export PATH="$PATH:/c/Go/bin/:/c/Program Files/CMake/bin"; fi
3435

3536
script:
36-
- if [[ $TRAVIS_RUST_VERSION == "stable" && $TRAVIS_OS_NAME == "linux" ]]; then cargo fmt -- --check; fi
37-
- if [[ $TRAVIS_RUST_VERSION == "stable" && $TRAVIS_OS_NAME == "linux" ]]; then cargo clippy -- -D clippy::all; fi
37+
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then cargo fmt -- --check; fi
38+
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then cargo clippy -- -D clippy::all; fi
3839
- cargo test --all -- --nocapture
3940
# For now we only run full integration tests on Linux. Here's why:
4041
# * Docker on OS X is not supported by Travis.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ name = "tikv_client"
1919

2020
[dependencies]
2121
failure = "0.1"
22-
futures = "0.1"
22+
futures-preview = { version = "0.3.0-alpha.15", features = ["compat"] }
2323
fxhash = "0.2"
2424
grpcio = { version = "0.5.0-alpha", features = [ "secure", "prost-codec" ], default-features = false }
2525
lazy_static = "0.2.1"
@@ -40,3 +40,5 @@ features = ["push", "process"]
4040
[dev-dependencies]
4141
clap = "2.32"
4242
tempdir = "0.3"
43+
runtime = "0.3.0-alpha.3"
44+
runtime-tokio = "0.3.0-alpha.3"

examples/raw.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.
22

3+
#![feature(async_await, await_macro)]
4+
35
mod common;
46

57
use crate::common::parse_args;
6-
use futures::future::Future;
78
use tikv_client::{raw::Client, Config, Key, KvPair, Result, Value};
89

910
const KEY: &str = "TiKV";
1011
const VALUE: &str = "Rust";
1112

12-
fn main() -> Result<()> {
13+
#[runtime::main(runtime_tokio::Tokio)]
14+
async fn main() -> Result<()> {
1315
// You can try running this example by passing your pd endpoints
1416
// (and SSL options if necessary) through command line arguments.
1517
let args = parse_args("raw");
@@ -25,15 +27,14 @@ fn main() -> Result<()> {
2527
// When we first create a client we receive a `Connect` structure which must be resolved before
2628
// the client is actually connected and usable.
2729
let unconnnected_client = Client::new(config);
28-
let client = unconnnected_client.wait()?;
30+
let client = await!(unconnnected_client)?;
2931

3032
// Requests are created from the connected client. These calls return structures which
3133
// implement `Future`. This means the `Future` must be resolved before the action ever takes
3234
// place.
3335
//
3436
// Here we set the key `TiKV` to have the value `Rust` associated with it.
35-
let put_request = client.put(KEY, VALUE);
36-
put_request.wait()?; // Returns a `tikv_client::Error` on failure.
37+
await!(client.put(KEY, VALUE)).unwrap(); // Returns a `tikv_client::Error` on failure.
3738
println!("Put key {:?}, value {:?}.", KEY, VALUE);
3839

3940
// Unlike a standard Rust HashMap all calls take owned values. This is because under the hood
@@ -45,20 +46,17 @@ fn main() -> Result<()> {
4546
//
4647
// It is best to pass a `Vec<u8>` in terms of explictness and speed. `String`s and a few other
4748
// types are supported as well, but it all ends up as `Vec<u8>` in the end.
48-
let value: Option<Value> = client.get(KEY).wait()?;
49+
let value: Option<Value> = await!(client.get(KEY))?;
4950
assert_eq!(value, Some(Value::from(VALUE)));
5051
println!("Get key {:?} returned value {:?}.", Key::from(KEY), value);
5152

5253
// You can also set the `ColumnFamily` used by the request.
5354
// This is *advanced usage* and should have some special considerations.
54-
client.delete(KEY).wait().expect("Could not delete value");
55+
await!(client.delete(KEY)).expect("Could not delete value");
5556
println!("Key: {:?} deleted", Key::from(KEY));
5657

5758
// Here we check if the key has been deleted from the key-value store.
58-
let value: Option<Value> = client
59-
.get(KEY)
60-
.wait()
61-
.expect("Could not get just deleted entry");
59+
let value: Option<Value> = await!(client.get(KEY)).expect("Could not get just deleted entry");
6260
assert!(value.is_none());
6361

6462
// You can ask to write multiple key-values at the same time, it is much more
@@ -68,25 +66,18 @@ fn main() -> Result<()> {
6866
KvPair::from(("k2", "v2")),
6967
KvPair::from(("k3", "v3")),
7068
];
71-
client.batch_put(pairs).wait().expect("Could not put pairs");
69+
await!(client.batch_put(pairs)).expect("Could not put pairs");
7270

7371
// Same thing when you want to retrieve multiple values.
7472
let keys = vec![Key::from("k1"), Key::from("k2")];
75-
let values = client
76-
.batch_get(keys.clone())
77-
.wait()
78-
.expect("Could not get values");
73+
let values = await!(client.batch_get(keys.clone())).expect("Could not get values");
7974
println!("Found values: {:?} for keys: {:?}", values, keys);
8075

8176
// Scanning a range of keys is also possible giving it two bounds
8277
// it will returns all entries between these two.
8378
let start = "k1";
8479
let end = "k2";
85-
let pairs = client
86-
.scan(start..=end, 10)
87-
.key_only()
88-
.wait()
89-
.expect("Could not scan");
80+
let pairs = await!(client.scan(start..=end, 10).key_only()).expect("Could not scan");
9081

9182
let keys: Vec<_> = pairs.into_iter().map(|p| p.key().clone()).collect();
9283
assert_eq!(&keys, &[Key::from("k1"), Key::from("k2")]);

examples/transaction.rs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,72 @@
11
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.
22

3+
#![feature(async_await, await_macro)]
4+
35
mod common;
46

57
use crate::common::parse_args;
6-
use futures::{future, Future, Stream};
8+
use futures::{
9+
future,
10+
prelude::{StreamExt, TryStreamExt},
11+
stream, TryFutureExt,
12+
};
713
use std::ops::RangeBounds;
814
use tikv_client::{
915
transaction::{Client, IsolationLevel},
1016
Config, Key, KvPair, Value,
1117
};
1218

13-
fn puts(client: &Client, pairs: impl IntoIterator<Item = impl Into<KvPair>>) {
19+
async fn puts(client: &Client, pairs: impl IntoIterator<Item = impl Into<KvPair>>) {
1420
let mut txn = client.begin();
15-
let _: Vec<()> = future::join_all(
21+
await!(future::join_all(
1622
pairs
1723
.into_iter()
1824
.map(Into::into)
19-
.map(|p| txn.set(p.key().clone(), p.value().clone())),
20-
)
21-
.wait()
25+
.map(|p| txn.set(p.key().clone(), p.value().clone()))
26+
))
27+
.into_iter()
28+
.collect::<Result<Vec<()>, _>>()
2229
.expect("Could not set key value pairs");
23-
txn.commit().wait().expect("Could not commit transaction");
30+
await!(txn.commit()).expect("Could not commit transaction");
2431
}
2532

26-
fn get(client: &Client, key: Key) -> Value {
33+
async fn get(client: &Client, key: Key) -> Value {
2734
let txn = client.begin();
28-
txn.get(key).wait().expect("Could not get value")
35+
await!(txn.get(key)).expect("Could not get value")
2936
}
3037

31-
fn scan(client: &Client, range: impl RangeBounds<Key>, mut limit: usize) {
32-
client
38+
// Ignore a spurious warning from rustc (https://github.com/rust-lang/rust/issues/60566).
39+
#[allow(unused_mut)]
40+
async fn scan(client: &Client, range: impl RangeBounds<Key>, mut limit: usize) {
41+
await!(client
3342
.begin()
3443
.scan(range)
35-
.take_while(move |_| {
36-
Ok(if limit == 0 {
44+
.into_stream()
45+
.take_while(move |r| {
46+
assert!(r.is_ok(), "Could not scan keys");
47+
future::ready(if limit == 0 {
3748
false
3849
} else {
3950
limit -= 1;
4051
true
4152
})
4253
})
43-
.for_each(|pair| {
44-
println!("{:?}", pair);
45-
Ok(())
46-
})
47-
.wait()
48-
.expect("Could not scan keys");
54+
.for_each(|pair| { future::ready(println!("{:?}", pair)) }));
4955
}
5056

51-
fn dels(client: &Client, keys: impl IntoIterator<Item = Key>) {
57+
async fn dels(client: &Client, keys: impl IntoIterator<Item = Key>) {
5258
let mut txn = client.begin();
5359
txn.set_isolation_level(IsolationLevel::ReadCommitted);
54-
let _: Vec<()> = keys
55-
.into_iter()
56-
.map(|p| {
57-
txn.delete(p).wait().expect("Could not delete key");
58-
})
59-
.collect();
60-
txn.commit().wait().expect("Could not commit transaction");
60+
let _: Vec<()> = await!(stream::iter(keys.into_iter())
61+
.then(|p| txn
62+
.delete(p)
63+
.unwrap_or_else(|e| panic!("error in delete: {:?}", e)))
64+
.collect());
65+
await!(txn.commit()).expect("Could not commit transaction");
6166
}
6267

63-
fn main() {
68+
#[runtime::main(runtime_tokio::Tokio)]
69+
async fn main() {
6470
// You can try running this example by passing your pd endpoints
6571
// (and SSL options if necessary) through command line arguments.
6672
let args = parse_args("txn");
@@ -73,28 +79,26 @@ fn main() {
7379
Config::new(args.pd)
7480
};
7581

76-
let txn = Client::new(config)
77-
.wait()
78-
.expect("Could not connect to tikv");
82+
let txn = await!(Client::new(config)).expect("Could not connect to tikv");
7983

8084
// set
8185
let key1: Key = b"key1".to_vec().into();
8286
let value1: Value = b"value1".to_vec().into();
8387
let key2: Key = b"key2".to_vec().into();
8488
let value2: Value = b"value2".to_vec().into();
85-
puts(&txn, vec![(key1, value1), (key2, value2)]);
89+
await!(puts(&txn, vec![(key1, value1), (key2, value2)]));
8690

8791
// get
8892
let key1: Key = b"key1".to_vec().into();
89-
let value1 = get(&txn, key1.clone());
93+
let value1 = await!(get(&txn, key1.clone()));
9094
println!("{:?}", (key1, value1));
9195

9296
// scan
9397
let key1: Key = b"key1".to_vec().into();
94-
scan(&txn, key1.., 10);
98+
await!(scan(&txn, key1.., 10));
9599

96100
// delete
97101
let key1: Key = b"key1".to_vec().into();
98102
let key2: Key = b"key2".to_vec().into();
99-
dels(&txn, vec![key1, key2]);
103+
await!(dels(&txn, vec![key1, key2]));
100104
}

0 commit comments

Comments
 (0)