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