@@ -37,9 +37,7 @@ const SCHEMA_USER_VERSION: u16 = 2;
37
37
///
38
38
/// [SQLite]: https://sqlite.org
39
39
pub struct SqliteStore {
40
- connection : Arc < Mutex < Connection > > ,
41
- data_dir : PathBuf ,
42
- kv_table_name : String ,
40
+ inner : Arc < SqliteStoreInner > ,
43
41
}
44
42
45
43
impl SqliteStore {
@@ -51,6 +49,50 @@ impl SqliteStore {
51
49
/// Similarly, the given `kv_table_name` will be used or default to [`DEFAULT_KV_TABLE_NAME`].
52
50
pub fn new (
53
51
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 > ,
54
96
) -> io:: Result < Self > {
55
97
let db_file_name = db_file_name. unwrap_or ( DEFAULT_SQLITE_DB_FILE_NAME . to_string ( ) ) ;
56
98
let kv_table_name = kv_table_name. unwrap_or ( DEFAULT_KV_TABLE_NAME . to_string ( ) ) ;
@@ -122,11 +164,6 @@ impl SqliteStore {
122
164
Ok ( Self { connection, data_dir, kv_table_name } )
123
165
}
124
166
125
- /// Returns the data directory.
126
- pub fn get_data_dir ( & self ) -> PathBuf {
127
- self . data_dir . clone ( )
128
- }
129
-
130
167
fn read_internal (
131
168
& self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
132
169
) -> io:: Result < Vec < u8 > > {
@@ -285,30 +322,6 @@ impl SqliteStore {
285
322
}
286
323
}
287
324
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
-
312
325
#[ cfg( test) ]
313
326
mod tests {
314
327
use super :: * ;
@@ -318,7 +331,7 @@ mod tests {
318
331
319
332
impl Drop for SqliteStore {
320
333
fn drop ( & mut self ) {
321
- match fs:: remove_dir_all ( & self . data_dir ) {
334
+ match fs:: remove_dir_all ( & self . inner . data_dir ) {
322
335
Err ( e) => println ! ( "Failed to remove test store directory: {}" , e) ,
323
336
_ => { } ,
324
337
}
0 commit comments