Skip to content

Commit 6580f40

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 222ccdd commit 6580f40

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
@@ -37,9 +37,7 @@ const SCHEMA_USER_VERSION: u16 = 2;
3737
///
3838
/// [SQLite]: https://sqlite.org
3939
pub struct SqliteStore {
40-
connection: Arc<Mutex<Connection>>,
41-
data_dir: PathBuf,
42-
kv_table_name: String,
40+
inner: Arc<SqliteStoreInner>,
4341
}
4442

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

125-
/// Returns the data directory.
126-
pub fn get_data_dir(&self) -> PathBuf {
127-
self.data_dir.clone()
128-
}
129-
130167
fn read_internal(
131168
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
132169
) -> io::Result<Vec<u8>> {
@@ -285,30 +322,6 @@ impl SqliteStore {
285322
}
286323
}
287324

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

319332
impl Drop for SqliteStore {
320333
fn drop(&mut self) {
321-
match fs::remove_dir_all(&self.data_dir) {
334+
match fs::remove_dir_all(&self.inner.data_dir) {
322335
Err(e) => println!("Failed to remove test store directory: {}", e),
323336
_ => {},
324337
}

0 commit comments

Comments
 (0)