-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from enmand/feat/asyncdb-async-std
feat: add async-std as an optional runtime for AsyncDB
- Loading branch information
Showing
9 changed files
with
263 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "asyncdb-async-std" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
async-std = { version = "1.12.0", features = ["attributes"] } | ||
rusty-leveldb = { path = "../../", features = ["asyncdb-async-std"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use rusty_leveldb::{AsyncDB, Options, Status, StatusCode}; | ||
|
||
#[async_std::main] | ||
async fn main() { | ||
let adb = AsyncDB::new("testdb", Options::default()).unwrap(); | ||
|
||
adb.put("Hello".as_bytes().to_owned(), "World".as_bytes().to_owned()) | ||
.await | ||
.expect("put()"); | ||
|
||
let r = adb.get("Hello".as_bytes().to_owned()).await; | ||
assert_eq!(r, Ok(Some("World".as_bytes().to_owned()))); | ||
|
||
let snapshot = adb.get_snapshot().await.expect("get_snapshot()"); | ||
|
||
adb.delete("Hello".as_bytes().to_owned()) | ||
.await | ||
.expect("delete()"); | ||
|
||
// A snapshot allows us to travel back in time before the deletion. | ||
let r2 = adb.get_at(snapshot, "Hello".as_bytes().to_owned()).await; | ||
assert_eq!(r2, Ok(Some("World".as_bytes().to_owned()))); | ||
|
||
// Once dropped, a snapshot cannot be used anymore. | ||
adb.drop_snapshot(snapshot).await.expect("drop_snapshot()"); | ||
|
||
let r3 = adb.get_at(snapshot, "Hello".as_bytes().to_owned()).await; | ||
assert_eq!( | ||
r3, | ||
Err(Status { | ||
code: StatusCode::AsyncError, | ||
err: "Unknown snapshot reference: this is a bug".to_string() | ||
}) | ||
); | ||
|
||
adb.flush().await.expect("flush()"); | ||
adb.close().await.expect("close()"); | ||
} |
6 changes: 3 additions & 3 deletions
6
examples/asyncdb/Cargo.toml → examples/asyncdb-tokio/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
[package] | ||
name = "asyncdb" | ||
name = "asyncdb-tokio" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
tokio = { version = "1.21", features = ["rt", "macros" ] } | ||
rusty-leveldb = { path = "../../", features = ["async"] } | ||
tokio = { version = "1.21", features = ["rt", "macros"] } | ||
rusty-leveldb = { path = "../../", features = ["asyncdb-tokio"] } |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::path::Path; | ||
use std::sync::Arc; | ||
|
||
use async_std::channel; | ||
use async_std::task::{spawn_blocking, JoinHandle}; | ||
|
||
use crate::asyncdb::{ReceiverExt, Request, Response, CHANNEL_BUFFER_SIZE}; | ||
use crate::{Options, Result, Status, StatusCode, DB}; | ||
|
||
pub(crate) struct Message { | ||
pub(crate) req: Request, | ||
pub(crate) resp_channel: channel::Sender<Response>, | ||
} | ||
/// `AsyncDB` makes it easy to use LevelDB in a async-std runtime. | ||
/// The methods follow very closely the main API (see `DB` type). Iteration is not yet implemented. | ||
#[derive(Clone)] | ||
pub struct AsyncDB { | ||
jh: Arc<JoinHandle<()>>, | ||
send: channel::Sender<Message>, | ||
} | ||
|
||
impl AsyncDB { | ||
/// Create a new or open an existing database. | ||
pub fn new<P: AsRef<Path>>(name: P, opts: Options) -> Result<AsyncDB> { | ||
let db = DB::open(name, opts)?; | ||
|
||
let (send, recv) = channel::bounded(CHANNEL_BUFFER_SIZE); | ||
let jh = spawn_blocking(move || AsyncDB::run_server(db, recv)); | ||
Ok(AsyncDB { | ||
jh: Arc::new(jh), | ||
send, | ||
}) | ||
} | ||
|
||
pub(crate) async fn process_request(&self, req: Request) -> Result<Response> { | ||
let (tx, rx) = channel::bounded(1); | ||
|
||
let m = Message { | ||
req, | ||
resp_channel: tx, | ||
}; | ||
if let Err(e) = self.send.send(m).await { | ||
return Err(Status { | ||
code: StatusCode::AsyncError, | ||
err: e.to_string(), | ||
}); | ||
} | ||
let resp = rx.recv().await; | ||
match resp { | ||
Err(e) => Err(Status { | ||
code: StatusCode::AsyncError, | ||
err: e.to_string(), | ||
}), | ||
Ok(r) => Ok(r), | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn send_response_result(ch: channel::Sender<Response>, result: Result<()>) { | ||
if let Err(e) = result { | ||
ch.try_send(Response::Error(e)).ok(); | ||
} else { | ||
ch.try_send(Response::OK).ok(); | ||
} | ||
} | ||
|
||
pub(crate) fn send_response(ch: channel::Sender<Response>, res: Response) { | ||
ch.send_blocking(res).ok(); | ||
} | ||
|
||
impl<T> ReceiverExt<T> for channel::Receiver<T> { | ||
fn blocking_recv(&mut self) -> Option<T> { | ||
self.recv_blocking().ok() | ||
} | ||
|
||
fn close(&mut self) { | ||
channel::Receiver::close(self); | ||
} | ||
} |
Oops, something went wrong.