Skip to content

Commit f41ebe3

Browse files
committed
Implement async API for random-access-storage
This commit moves the API from a sync call into async functions using async_trait. We would like to use async operations on hypercore, and it would be nice to have the underlying storage using async-io when possible. As discussed on #97, we could change the API to use async functions on the 'ram' trait. Async Trait methods are still unstable, but possible using the 'async-trait' crate through a macro. This PR moves the API to use async-fns. Tests are passing.
1 parent 5c6a73e commit f41ebe3

File tree

15 files changed

+368
-299
lines changed

15 files changed

+368
-299
lines changed

Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@ memory-pager = "0.9.0"
2828
merkle-tree-stream = "0.12.0"
2929
pretty-hash = "0.4.1"
3030
rand = "0.7.3"
31-
random-access-disk = "1.1.0"
32-
random-access-memory = "1.2.0"
33-
random-access-storage = "3.0.0"
31+
random-access-disk = "2.0.0"
32+
random-access-memory = "2.0.0"
33+
random-access-storage = "4.0.0"
3434
sha2 = "0.8.1"
3535
sleep-parser = "0.8.0"
3636
sparse-bitfield = "0.11.0"
3737
tree-index = "0.6.0"
3838
bitfield-rle = "0.1.1"
39+
futures = "0.3.4"
40+
async-std = "1.5.0"
3941

4042
[dev-dependencies]
4143
quickcheck = "0.9.2"
4244
data-encoding = "2.2.0"
4345
remove_dir_all = "0.5.2"
4446
tempfile = "3.1.0"
45-
async-std = "1.5.0"
47+
async-std = { version = "1.5.0", features = ["attributes"] }

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ WIP. Secure, distributed, append-only log structure. Adapted from
1010

1111
## Usage
1212
```rust
13-
let mut feed = hypercore::open("./feed.db")?;
13+
let mut feed = hypercore::open("./feed.db").await?;
1414

15-
feed.append(b"hello")?;
16-
feed.append(b"world")?;
15+
feed.append(b"hello").await?;
16+
feed.append(b"world").await?;
1717

18-
assert_eq!(feed.get(0)?, Some(b"hello".to_vec()));
19-
assert_eq!(feed.get(1)?, Some(b"world".to_vec()));
18+
assert_eq!(feed.get(0).await?, Some(b"hello".to_vec()));
19+
assert_eq!(feed.get(1).await?, Some(b"world".to_vec()));
2020
```
2121

2222
## Installation

benches/bench.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,51 @@ use anyhow::Error;
55
use random_access_memory::RandomAccessMemory;
66
use test::Bencher;
77

8-
use hypercore::{Feed, Storage, Store};
8+
use hypercore::{Feed, Storage};
99

10-
fn create_feed(page_size: usize) -> Result<Feed<RandomAccessMemory>, Error> {
11-
let create = |_store: Store| Ok(RandomAccessMemory::new(page_size));
12-
let storage = Storage::new(create)?;
13-
Ok(Feed::with_storage(storage)?)
10+
async fn create_feed(page_size: usize) -> Result<Feed<RandomAccessMemory>, Error> {
11+
let storage =
12+
Storage::new(|_| Box::pin(async move { Ok(RandomAccessMemory::new(page_size)) })).await?;
13+
Feed::with_storage(storage).await
1414
}
1515

1616
#[bench]
1717
fn create(b: &mut Bencher) {
1818
b.iter(|| {
19-
create_feed(1024).unwrap();
19+
async_std::task::block_on(async {
20+
create_feed(1024).await.unwrap();
21+
});
2022
});
2123
}
2224

2325
#[bench]
2426
fn write(b: &mut Bencher) {
25-
let mut feed = create_feed(1024).unwrap();
26-
let data = Vec::from("hello");
27-
b.iter(|| {
28-
feed.append(&data).unwrap();
27+
async_std::task::block_on(async {
28+
let mut feed = create_feed(1024).await.unwrap();
29+
let data = Vec::from("hello");
30+
b.iter(|| {
31+
async_std::task::block_on(async {
32+
feed.append(&data).await.unwrap();
33+
});
34+
});
2935
});
3036
}
3137

3238
#[bench]
3339
fn read(b: &mut Bencher) {
34-
let mut feed = create_feed(1024).unwrap();
35-
let data = Vec::from("hello");
36-
for _ in 0..1000 {
37-
feed.append(&data).unwrap();
38-
}
40+
async_std::task::block_on(async {
41+
let mut feed = create_feed(1024).await.unwrap();
42+
let data = Vec::from("hello");
43+
for _ in 0..1000 {
44+
feed.append(&data).await.unwrap();
45+
}
3946

40-
let mut i = 0;
41-
b.iter(|| {
42-
feed.get(i).unwrap();
43-
i += 1;
47+
let mut i = 0;
48+
b.iter(|| {
49+
async_std::task::block_on(async {
50+
feed.get(i).await.unwrap();
51+
i += 1;
52+
});
53+
});
4454
});
4555
}

examples/async.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ use std::fmt::Debug;
55

66
async fn append<T>(feed: &mut Feed<T>, content: &[u8])
77
where
8-
T: RandomAccess<Error = Box<dyn std::error::Error + Send + Sync>> + Debug,
8+
T: RandomAccess<Error = Box<dyn std::error::Error + Send + Sync>> + Debug + Send,
99
{
10-
feed.append(content).unwrap();
10+
feed.append(content).await.unwrap();
1111
}
1212

1313
async fn print<T>(feed: &mut Feed<T>)
1414
where
15-
T: RandomAccess<Error = Box<dyn std::error::Error + Send + Sync>> + Debug,
15+
T: RandomAccess<Error = Box<dyn std::error::Error + Send + Sync>> + Debug + Send,
1616
{
17-
println!("{:?}", feed.get(0));
18-
println!("{:?}", feed.get(1));
17+
println!("{:?}", feed.get(0).await);
18+
println!("{:?}", feed.get(1).await);
1919
}
2020

2121
fn main() {

examples/main.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use hypercore::Feed;
22

3-
fn main() {
3+
#[async_std::main]
4+
async fn main() {
45
let mut feed = Feed::default();
56

6-
feed.append(b"hello").unwrap();
7-
feed.append(b"world").unwrap();
7+
feed.append(b"hello").await.unwrap();
8+
feed.append(b"world").await.unwrap();
89

9-
println!("{:?}", feed.get(0)); // prints "hello"
10-
println!("{:?}", feed.get(1)); // prints "world"
10+
println!("{:?}", feed.get(0).await); // prints "hello"
11+
println!("{:?}", feed.get(1).await); // prints "world"
1112
}

0 commit comments

Comments
 (0)