Skip to content

Commit 8da2bfe

Browse files
committed
chore: apply review comments
1 parent b0c0cb7 commit 8da2bfe

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

mithril-signer/src/mktree_store_sqlite.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
iter::repeat,
3-
sync::{Arc, RwLock},
4-
};
1+
use std::{iter::repeat, sync::Arc};
52

63
use anyhow::Context;
74
use mithril_common::{
@@ -10,17 +7,17 @@ use mithril_common::{
107
};
118

129
/// A Merkle tree store with Sqlite backend
13-
/// This store is used to store the Merkle tree nodes in a Sqlite database with in memory mode.
14-
/// This store is not suited for proving, it is only used to store the compute the root of a Merkle tree.
15-
/// This store is slower than the in memory store but uses less memory which is important to minimize the signer footprint on the SPO infrastructure.
10+
/// * This store is used to store the Merkle tree nodes in a Sqlite database with in memory mode.
11+
/// * This store is not suited for proving, it is only used to store the compute the root of a Merkle tree.
12+
/// * This store is slower than the in memory store but uses less memory which is important to minimize the signer footprint on the SPO infrastructure.
1613
pub struct MKTreeStoreSqlite {
17-
inner_store: RwLock<sqlite::ConnectionThreadSafe>,
14+
inner_store: sqlite::ConnectionThreadSafe,
1815
}
1916

2017
impl MKTreeStoreSqlite {
2118
fn build() -> StdResult<Self> {
2219
Ok(Self {
23-
inner_store: RwLock::new(Self::create_connection()?),
20+
inner_store: Self::create_connection()?,
2421
})
2522
}
2623

@@ -39,9 +36,8 @@ impl MKTreeStoreSqlite {
3936
}
4037

4138
fn get_element_at_position(&self, position: u64) -> StdResult<Option<Arc<MKTreeNode>>> {
42-
let inner_store = self.inner_store.read().unwrap();
4339
let query = "SELECT element FROM merkle_tree WHERE position = ?";
44-
let mut statement = (*inner_store).prepare(query)?;
40+
let mut statement = self.inner_store.prepare(query)?;
4541
statement.bind((1, position as i64)).unwrap();
4642
let result = if let Ok(sqlite::State::Row) = statement.next() {
4743
Some(Arc::new(MKTreeNode::new(
@@ -59,24 +55,22 @@ impl MKTreeStoreSqlite {
5955
position: u64,
6056
elements: Vec<Arc<MKTreeNode>>,
6157
) -> StdResult<()> {
62-
let inner_store = self.inner_store.read().unwrap();
6358
let values_columns: Vec<&str> = repeat("(?, ?)").take(elements.len()).collect();
64-
let values: Vec<sqlite::Value> =
65-
elements
66-
.into_iter()
67-
.enumerate()
68-
.fold(vec![], |mut vec, (i, elem)| {
69-
vec.append(&mut vec![
70-
sqlite::Value::Integer((position + i as u64) as i64),
71-
sqlite::Value::Binary((**elem).to_vec()),
72-
]);
73-
vec
74-
});
59+
let values: Vec<sqlite::Value> = elements
60+
.into_iter()
61+
.enumerate()
62+
.flat_map(|(i, elem)| {
63+
vec![
64+
sqlite::Value::Integer((position + i as u64) as i64),
65+
sqlite::Value::Binary((**elem).to_vec()),
66+
]
67+
})
68+
.collect();
7569
let query = format!(
7670
"INSERT INTO merkle_tree(position, element) VALUES {}",
7771
values_columns.join(", ")
7872
);
79-
let mut statement = (*inner_store).prepare(query)?;
73+
let mut statement = self.inner_store.prepare(query)?;
8074
statement.bind::<&[(_, sqlite::Value)]>(
8175
values
8276
.into_iter()

0 commit comments

Comments
 (0)