Update scabbard merkle state for exclusive writes
This change adds the ability to configure the scabbard MerkleState with
an Arc-RwLock-wrapped pool for exclusive write support.
// limitations under the License.
use std::collections::HashMap;
+#[cfg(feature = "sqlite")]
+use std::sync::{Arc, RwLock};
#[cfg(feature = "diesel")]
use diesel::r2d2::{ConnectionManager, Pool};
@@ -44,6 +46,11 @@ pub enum MerkleStateConfig {
pool: Pool<ConnectionManager<diesel::SqliteConnection>>,
tree_name: String,
},
+ #[cfg(feature = "sqlite")]
+ SqliteExclusiveWrites {
+ pool: Arc<RwLock<Pool<ConnectionManager<diesel::SqliteConnection>>>>,
+ tree_name: String,
+ },
}
impl MerkleStateConfig {
@@ -100,6 +107,19 @@ impl MerkleState {
MerkleStateConfig::Sqlite { pool, tree_name } => {
let sqlite_backend = sql::backend::SqliteBackend::from(pool);
+ let state = sql::SqlMerkleStateBuilder::new()
+ .with_backend(sqlite_backend)
+ .with_tree(tree_name)
+ .create_tree_if_necessary()
+ .build()
+ .map_err(|e| InternalError::from_source(Box::new(e)))?;
+
+ Ok(MerkleState::SqlSqlite { state })
+ }
+ #[cfg(feature = "sqlite")]
+ MerkleStateConfig::SqliteExclusiveWrites { pool, tree_name } => {
+ let sqlite_backend = sql::backend::SqliteBackend::from(pool);
+
let state = sql::SqlMerkleStateBuilder::new()
.with_backend(sqlite_backend)
.with_tree(tree_name)
@@ -132,6 +152,17 @@ impl MerkleState {
MerkleStateConfig::Sqlite { pool, tree_name } => {
let sqlite_backend = sql::backend::SqliteBackend::from(pool.clone());
+ // This will fail if the tree does not previously exist.
+ sql::SqlMerkleStateBuilder::new()
+ .with_backend(sqlite_backend)
+ .with_tree(tree_name)
+ .build()
+ .is_ok()
+ }
+ #[cfg(feature = "sqlite")]
+ MerkleStateConfig::SqliteExclusiveWrites { pool, tree_name } => {
+ let sqlite_backend = sql::backend::SqliteBackend::from(pool.clone());
+
// This will fail if the tree does not previously exist.
sql::SqlMerkleStateBuilder::new()
.with_backend(sqlite_backend)
