add async to db

This commit is contained in:
creating2morrow 2023-05-20 04:05:48 -04:00
parent 4682d352c2
commit d83fa927c3

View file

@ -42,6 +42,10 @@ impl Interface {
let handle = env.get_default_db(DbFlags::empty()).unwrap(); let handle = env.get_default_db(DbFlags::empty()).unwrap();
Interface { env, handle } Interface { env, handle }
} }
pub async fn async_open() -> Self {
tokio::time::sleep(std::time::Duration::from_micros(1)).await;
self::Interface::open()
}
/// Write a key-value to LMDB. NEVMES does not currently support /// Write a key-value to LMDB. NEVMES does not currently support
/// ///
/// writing multiple key value pairs. /// writing multiple key value pairs.
@ -60,6 +64,10 @@ impl Interface {
Ok(_) => (), Ok(_) => (),
} }
} }
pub async fn async_write(e: &Environment, h: &DbHandle, k: &str, v: &str) {
tokio::time::sleep(std::time::Duration::from_micros(1)).await;
self::Interface::write(e, h, k, v)
}
/// Read a value from LMDB by passing the key as a static /// Read a value from LMDB by passing the key as a static
/// ///
/// string. If the value does not exist an empty string is /// string. If the value does not exist an empty string is
@ -77,6 +85,10 @@ impl Interface {
} }
r r
} }
pub async fn async_read(e: &Environment, h: &DbHandle, k: &str) -> String {
tokio::time::sleep(std::time::Duration::from_micros(1)).await;
self::Interface::read(e, h, k)
}
/// Delete a value from LMDB by passing the key as a /// Delete a value from LMDB by passing the key as a
/// ///
/// static string. If the value does not exist then an /// static string. If the value does not exist then an
@ -94,6 +106,10 @@ impl Interface {
Ok(_) => (), Ok(_) => (),
} }
} }
pub async fn async_delete(e: &Environment, h: &DbHandle, k: &str) {
tokio::time::sleep(std::time::Duration::from_micros(1)).await;
self::Interface::delete(e, h, k)
}
} }
// Tests // Tests
@ -104,26 +120,38 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn write_and_read_test() { fn async_write_and_read_test() {
let s = Interface::open(); // run and async cleanup so the test doesn't fail when deleting test data
let k = "test-key"; use tokio::runtime::Runtime;
let v = "test-value"; let rt = Runtime::new().expect("Unable to create Runtime for test");
Interface::write(&s.env, &s.handle, k, v); let _enter = rt.enter();
let expected = String::from(v); tokio::spawn(async move {
let actual = Interface::read(&s.env, &s.handle, k); let s = Interface::async_open().await;
assert_eq!(expected, actual); let k = "async-test-key";
Interface::delete(&s.env, &s.handle, &k); let v = "async-test-value";
Interface::async_write(&s.env, &s.handle, k, v).await;
let expected = String::from(v);
let actual = Interface::async_read(&s.env, &s.handle, k).await;
assert_eq!(expected, actual);
Interface::async_delete(&s.env, &s.handle, &k).await;
});
} }
#[test] #[test]
fn write_and_delete_test() { fn async_write_and_delete_test() {
let s = Interface::open(); // run and async cleanup so the test doesn't fail when deleting test data
let k = "test-key"; use tokio::runtime::Runtime;
let v = "test-value"; let rt = Runtime::new().expect("Unable to create Runtime for test");
Interface::write(&s.env, &s.handle, k, v); let _enter = rt.enter();
let expected = utils::empty_string(); tokio::spawn(async move {
Interface::delete(&s.env, &s.handle, &k); let s = Interface::open();
let actual = Interface::read(&s.env, &s.handle, k); let k = "write_and_delete_test_test-key";
assert_eq!(expected, actual); let v = "write_and_delete_test_test-value";
Interface::async_write(&s.env, &s.handle, k, v).await;
let expected = utils::empty_string();
Interface::async_delete(&s.env, &s.handle, &k).await;
let actual = Interface::async_read(&s.env, &s.handle, k).await;
assert_eq!(expected, actual);
});
} }
} }