diff --git a/lib/Cargo.toml b/lib/Cargo.toml index aa241ead..78c3d701 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -64,7 +64,7 @@ url = "2.2.2" uuid = { version = "1.1.2", features = ["serde", "v4"] } [dev-dependencies] -tokio = { version = "1.20.1", features = ["macros"] } +tokio = { version = "1.20.1", features = ["macros", "rt"] } [target.'cfg(target_arch = "wasm32")'.dependencies] surf = { version = "2.3.2", optional = true, default-features = false, features = ["encoding", "wasm-client"] } diff --git a/lib/src/kvs/rocksdb/mod.rs b/lib/src/kvs/rocksdb/mod.rs index 21a9d455..cf39e43f 100644 --- a/lib/src/kvs/rocksdb/mod.rs +++ b/lib/src/kvs/rocksdb/mod.rs @@ -9,10 +9,11 @@ use rocksdb::IteratorMode; use rocksdb::OptimisticTransactionDB; use rocksdb::ReadOptions; use std::ops::Range; +use std::pin::Pin; use std::sync::Arc; pub struct Datastore { - db: rocksdb::OptimisticTransactionDB, + db: Pin>, } pub struct Transaction { @@ -22,13 +23,16 @@ pub struct Transaction { rw: bool, // The distributed datastore transaction tx: Arc>>>, + // the above, supposedly 'static, transaction actually points here, so keep the memory alive + // note that this is dropped last, as it is declared last + _db: Pin>, } impl Datastore { // Open a new database pub async fn new(path: &str) -> Result { Ok(Datastore { - db: OptimisticTransactionDB::open_default(path)?, + db: Arc::pin(OptimisticTransactionDB::open_default(path)?), }) } // Start a new transaction @@ -52,6 +56,7 @@ impl Datastore { ok: false, rw: write, tx: Arc::new(Mutex::new(Some(tx))), + _db: self.db.clone(), }) } } @@ -286,3 +291,18 @@ impl Transaction { Ok(res) } } + +#[cfg(test)] +mod tests { + // https://github.com/surrealdb/surrealdb/issues/76 + #[tokio::test] + async fn soundness() { + let mut transaction = get_transaction().await; + transaction.put("uh", "oh").await.unwrap(); + + async fn get_transaction() -> crate::Transaction { + let datastore = crate::Datastore::new("rocksdb:/tmp/rocks.db").await.unwrap(); + datastore.transaction(true, false).await.unwrap() + } + } +}