Skip to content

Commit de64ae4

Browse files
committed
Split SqliteStore into SqliteStore and SqliteStoreInner
.. where the former holds the latter in an `Arc` that can be used in async/`Future` contexts more easily.
1 parent d0081ef commit de64ae4

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

src/io/sqlite_store/mod.rs

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ const SCHEMA_USER_VERSION: u16 = 2;
3939
///
4040
/// [SQLite]: https://sqlite.org
4141
pub struct SqliteStore {
42-
connection: Arc<Mutex<Connection>>,
43-
data_dir: PathBuf,
44-
kv_table_name: String,
42+
inner: Arc<SqliteStoreInner>,
4543
}
4644

4745
impl SqliteStore {
@@ -53,6 +51,50 @@ impl SqliteStore {
5351
/// Similarly, the given `kv_table_name` will be used or default to [`DEFAULT_KV_TABLE_NAME`].
5452
pub fn new(
5553
data_dir: PathBuf, db_file_name: Option<String>, kv_table_name: Option<String>,
54+
) -> io::Result<Self> {
55+
let inner = Arc::new(SqliteStoreInner::new(data_dir, db_file_name, kv_table_name)?);
56+
Ok(Self { inner })
57+
}
58+
59+
/// Returns the data directory.
60+
pub fn get_data_dir(&self) -> PathBuf {
61+
self.inner.data_dir.clone()
62+
}
63+
}
64+
65+
impl KVStoreSync for SqliteStore {
66+
fn read(
67+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
68+
) -> io::Result<Vec<u8>> {
69+
self.inner.read_internal(primary_namespace, secondary_namespace, key)
70+
}
71+
72+
fn write(
73+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec<u8>,
74+
) -> io::Result<()> {
75+
self.inner.write_internal(primary_namespace, secondary_namespace, key, buf)
76+
}
77+
78+
fn remove(
79+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool,
80+
) -> io::Result<()> {
81+
self.inner.remove_internal(primary_namespace, secondary_namespace, key, lazy)
82+
}
83+
84+
fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> io::Result<Vec<String>> {
85+
self.inner.list_internal(primary_namespace, secondary_namespace)
86+
}
87+
}
88+
89+
struct SqliteStoreInner {
90+
connection: Arc<Mutex<Connection>>,
91+
data_dir: PathBuf,
92+
kv_table_name: String,
93+
}
94+
95+
impl SqliteStoreInner {
96+
fn new(
97+
data_dir: PathBuf, db_file_name: Option<String>, kv_table_name: Option<String>,
5698
) -> io::Result<Self> {
5799
let db_file_name = db_file_name.unwrap_or(DEFAULT_SQLITE_DB_FILE_NAME.to_string());
58100
let kv_table_name = kv_table_name.unwrap_or(DEFAULT_KV_TABLE_NAME.to_string());
@@ -124,11 +166,6 @@ impl SqliteStore {
124166
Ok(Self { connection, data_dir, kv_table_name })
125167
}
126168

127-
/// Returns the data directory.
128-
pub fn get_data_dir(&self) -> PathBuf {
129-
self.data_dir.clone()
130-
}
131-
132169
fn read_internal(
133170
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
134171
) -> io::Result<Vec<u8>> {
@@ -287,30 +324,6 @@ impl SqliteStore {
287324
}
288325
}
289326

290-
impl KVStoreSync for SqliteStore {
291-
fn read(
292-
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
293-
) -> io::Result<Vec<u8>> {
294-
self.read_internal(primary_namespace, secondary_namespace, key)
295-
}
296-
297-
fn write(
298-
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec<u8>,
299-
) -> io::Result<()> {
300-
self.write_internal(primary_namespace, secondary_namespace, key, buf)
301-
}
302-
303-
fn remove(
304-
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool,
305-
) -> io::Result<()> {
306-
self.remove_internal(primary_namespace, secondary_namespace, key, lazy)
307-
}
308-
309-
fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> io::Result<Vec<String>> {
310-
self.list_internal(primary_namespace, secondary_namespace)
311-
}
312-
}
313-
314327
#[cfg(test)]
315328
mod tests {
316329
use super::*;
@@ -320,7 +333,7 @@ mod tests {
320333

321334
impl Drop for SqliteStore {
322335
fn drop(&mut self) {
323-
match fs::remove_dir_all(&self.data_dir) {
336+
match fs::remove_dir_all(&self.inner.data_dir) {
324337
Err(e) => println!("Failed to remove test store directory: {}", e),
325338
_ => {},
326339
}

0 commit comments

Comments
 (0)