@@ -7,6 +7,7 @@ use std::thread::panicking;
7
7
use libsqlite3_sys as ffi;
8
8
9
9
use base:: crypto:: { Crypto , Key } ;
10
+ use base:: vio;
10
11
use error:: { Error , Result } ;
11
12
use trans:: Eid ;
12
13
use volume:: address:: Span ;
@@ -117,8 +118,8 @@ fn run_select_blob(stmt: *mut ffi::sqlite3_stmt) -> Result<Vec<u8>> {
117
118
118
119
/// Sqlite Storage
119
120
pub struct SqliteStorage {
120
- is_attached : bool , // attached to sqlite db
121
- filename : CString ,
121
+ is_attached : bool , // attached to sqlite db
122
+ file_path : CString , // database file path
122
123
db : * mut ffi:: sqlite3 ,
123
124
stmts : Vec < * mut ffi:: sqlite3_stmt > ,
124
125
}
@@ -131,10 +132,10 @@ impl SqliteStorage {
131
132
const TBL_ADDRESSES : & ' static str = "addresses" ;
132
133
const TBL_BLOCKS : & ' static str = "blocks" ;
133
134
134
- pub fn new ( filename : & str ) -> Self {
135
+ pub fn new ( file_path : & str ) -> Self {
135
136
SqliteStorage {
136
137
is_attached : false ,
137
- filename : CString :: new ( filename ) . unwrap ( ) ,
138
+ file_path : CString :: new ( file_path ) . unwrap ( ) ,
138
139
db : ptr:: null_mut ( ) ,
139
140
stmts : Vec :: with_capacity ( 14 ) ,
140
141
}
@@ -196,7 +197,7 @@ impl SqliteStorage {
196
197
) ) ?;
197
198
self . prepare_sql ( format ! (
198
199
"
199
- INSERT INTO {}(suffix, data) VALUES (?, ?)
200
+ INSERT OR REPLACE INTO {}(suffix, data) VALUES (?, ?)
200
201
" ,
201
202
Self :: TBL_SUPER_BLOCK
202
203
) ) ?;
@@ -297,7 +298,7 @@ impl Storable for SqliteStorage {
297
298
let mut db: * mut ffi:: sqlite3 = ptr:: null_mut ( ) ;
298
299
let result = unsafe {
299
300
ffi:: sqlite3_open_v2 (
300
- self . filename . as_ptr ( ) ,
301
+ self . file_path . as_ptr ( ) ,
301
302
& mut db,
302
303
ffi:: SQLITE_OPEN_READONLY ,
303
304
ptr:: null ( ) ,
@@ -312,7 +313,7 @@ impl Storable for SqliteStorage {
312
313
fn connect ( & mut self , _force : bool ) -> Result < ( ) > {
313
314
let result = unsafe {
314
315
ffi:: sqlite3_open_v2 (
315
- self . filename . as_ptr ( ) ,
316
+ self . file_path . as_ptr ( ) ,
316
317
& mut self . db ,
317
318
ffi:: SQLITE_OPEN_READWRITE
318
319
| ffi:: SQLITE_OPEN_CREATE
@@ -524,6 +525,23 @@ impl Storable for SqliteStorage {
524
525
fn flush ( & mut self ) -> Result < ( ) > {
525
526
Ok ( ( ) )
526
527
}
528
+
529
+ #[ inline]
530
+ fn destroy ( & mut self ) -> Result < ( ) > {
531
+ if self . prepare_stmts ( ) . is_ok ( ) {
532
+ let stmt = self . stmts [ 0 ] ;
533
+ reset_stmt ( stmt) ?;
534
+ match unsafe { ffi:: sqlite3_step ( stmt) } {
535
+ ffi:: SQLITE_ROW => {
536
+ // repo is locked
537
+ warn ! ( "Destroy an opened repo" ) ;
538
+ }
539
+ _ => { }
540
+ }
541
+ }
542
+ let _ = vio:: remove_file ( self . file_path . to_str ( ) . unwrap ( ) ) ?;
543
+ Ok ( ( ) )
544
+ }
527
545
}
528
546
529
547
impl Drop for SqliteStorage {
@@ -560,7 +578,7 @@ impl Drop for SqliteStorage {
560
578
impl Debug for SqliteStorage {
561
579
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
562
580
f. debug_struct ( "SqliteStorage" )
563
- . field ( "filename " , & self . filename )
581
+ . field ( "file_path " , & self . file_path )
564
582
. finish ( )
565
583
}
566
584
}
0 commit comments