Make RocksDB integration more, if not completely, sound (#77)
Closes #76
This commit is contained in:
parent
960061584d
commit
8907f0aa14
2 changed files with 23 additions and 3 deletions
|
@ -64,7 +64,7 @@ url = "2.2.2"
|
||||||
uuid = { version = "1.1.2", features = ["serde", "v4"] }
|
uuid = { version = "1.1.2", features = ["serde", "v4"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { version = "1.20.1", features = ["macros"] }
|
tokio = { version = "1.20.1", features = ["macros", "rt"] }
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
surf = { version = "2.3.2", optional = true, default-features = false, features = ["encoding", "wasm-client"] }
|
surf = { version = "2.3.2", optional = true, default-features = false, features = ["encoding", "wasm-client"] }
|
||||||
|
|
|
@ -9,10 +9,11 @@ use rocksdb::IteratorMode;
|
||||||
use rocksdb::OptimisticTransactionDB;
|
use rocksdb::OptimisticTransactionDB;
|
||||||
use rocksdb::ReadOptions;
|
use rocksdb::ReadOptions;
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub struct Datastore {
|
pub struct Datastore {
|
||||||
db: rocksdb::OptimisticTransactionDB,
|
db: Pin<Arc<rocksdb::OptimisticTransactionDB>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Transaction {
|
pub struct Transaction {
|
||||||
|
@ -22,13 +23,16 @@ pub struct Transaction {
|
||||||
rw: bool,
|
rw: bool,
|
||||||
// The distributed datastore transaction
|
// The distributed datastore transaction
|
||||||
tx: Arc<Mutex<Option<rocksdb::Transaction<'static, OptimisticTransactionDB>>>>,
|
tx: Arc<Mutex<Option<rocksdb::Transaction<'static, OptimisticTransactionDB>>>>,
|
||||||
|
// 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<Arc<rocksdb::OptimisticTransactionDB>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Datastore {
|
impl Datastore {
|
||||||
// Open a new database
|
// Open a new database
|
||||||
pub async fn new(path: &str) -> Result<Datastore, Error> {
|
pub async fn new(path: &str) -> Result<Datastore, Error> {
|
||||||
Ok(Datastore {
|
Ok(Datastore {
|
||||||
db: OptimisticTransactionDB::open_default(path)?,
|
db: Arc::pin(OptimisticTransactionDB::open_default(path)?),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Start a new transaction
|
// Start a new transaction
|
||||||
|
@ -52,6 +56,7 @@ impl Datastore {
|
||||||
ok: false,
|
ok: false,
|
||||||
rw: write,
|
rw: write,
|
||||||
tx: Arc::new(Mutex::new(Some(tx))),
|
tx: Arc::new(Mutex::new(Some(tx))),
|
||||||
|
_db: self.db.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -286,3 +291,18 @@ impl Transaction {
|
||||||
Ok(res)
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue