@@ -3,6 +3,7 @@ use crate::utils::check_namespace_key_validity;
33
44use lightning:: util:: persist:: KVStore ;
55use lightning:: util:: string:: PrintableString ;
6+ use lightning:: io;
67
78use rusqlite:: { named_params, Connection } ;
89
@@ -35,41 +36,49 @@ impl SqliteStore {
3536 /// given `db_file_name` (or the default to [`DEFAULT_SQLITE_DB_FILE_NAME`] if set to `None`).
3637 ///
3738 /// Similarly, the given `kv_table_name` will be used or default to [`DEFAULT_KV_TABLE_NAME`].
38- pub fn new ( data_dir : PathBuf , db_file_name : Option < String > , kv_table_name : Option < String > ) -> Self {
39+ pub fn new ( data_dir : PathBuf , db_file_name : Option < String > , kv_table_name : Option < String > ) -> io :: Result < Self > {
3940 let db_file_name = db_file_name. unwrap_or ( DEFAULT_SQLITE_DB_FILE_NAME . to_string ( ) ) ;
4041 let kv_table_name = kv_table_name. unwrap_or ( DEFAULT_KV_TABLE_NAME . to_string ( ) ) ;
4142
42- fs:: create_dir_all ( data_dir. clone ( ) ) . unwrap_or_else ( |_| {
43- panic ! ( "Failed to create database destination directory: {}" , data_dir. display( ) )
44- } ) ;
43+ fs:: create_dir_all ( data_dir. clone ( ) ) . map_err ( |e| {
44+ let msg = format ! ( "Failed to create database destination directory {}: {}" ,
45+ data_dir. display( ) , e) ;
46+ io:: Error :: new ( io:: ErrorKind :: Other , msg)
47+ } ) ?;
4548 let mut db_file_path = data_dir. clone ( ) ;
4649 db_file_path. push ( db_file_name) ;
4750
48- let connection = Connection :: open ( db_file_path. clone ( ) ) . unwrap_or_else ( |_| {
49- panic ! ( "Failed to open/create database file: {}" , db_file_path. display( ) )
50- } ) ;
51+ let connection = Connection :: open ( db_file_path. clone ( ) ) . map_err ( |e| {
52+ let msg = format ! ( "Failed to open/create database file {}: {}" ,
53+ db_file_path. display( ) , e) ;
54+ io:: Error :: new ( io:: ErrorKind :: Other , msg)
55+ } ) ?;
5156
52- connection
53- . pragma ( Some ( rusqlite :: DatabaseName :: Main ) , "user_version" , SCHEMA_USER_VERSION , |_| {
57+ connection. pragma ( Some ( rusqlite :: DatabaseName :: Main ) ,
58+ "user_version" , SCHEMA_USER_VERSION , |_| {
5459 Ok ( ( ) )
55- } )
56- . unwrap_or_else ( |_| panic ! ( "Failed to set PRAGMA user_version" ) ) ;
60+ } ) . map_err ( |e| {
61+ let msg = format ! ( "Failed to set PRAGMA user_version: {}" , e) ;
62+ io:: Error :: new ( io:: ErrorKind :: Other , msg)
63+ } ) ?;
5764
5865 let sql = format ! (
5966 "CREATE TABLE IF NOT EXISTS {} (
6067 namespace TEXT NOT NULL,
6168 sub_namespace TEXT DEFAULT \" \" NOT NULL,
6269 key TEXT NOT NULL CHECK (key <> ''),
6370 value BLOB, PRIMARY KEY ( namespace, sub_namespace, key )
64- );" ,
71+ );" ,
6572 kv_table_name
6673 ) ;
67- connection
68- . execute ( & sql, [ ] )
69- . unwrap_or_else ( |_| panic ! ( "Failed to create table: {}" , kv_table_name) ) ;
74+
75+ connection. execute ( & sql, [ ] ) . map_err ( |e| {
76+ let msg = format ! ( "Failed to create table {}: {}" , kv_table_name, e) ;
77+ io:: Error :: new ( io:: ErrorKind :: Other , msg)
78+ } ) ?;
7079
7180 let connection = Arc :: new ( Mutex :: new ( connection) ) ;
72- Self { connection, data_dir, kv_table_name }
81+ Ok ( Self { connection, data_dir, kv_table_name } )
7382 }
7483
7584 /// Returns the data directory.
@@ -231,16 +240,16 @@ mod tests {
231240 fn read_write_remove_list_persist ( ) {
232241 let mut temp_path = std:: env:: temp_dir ( ) ;
233242 temp_path. push ( "read_write_remove_list_persist" ) ;
234- let store = SqliteStore :: new ( temp_path, Some ( "test_db" . to_string ( ) ) , Some ( "test_table" . to_string ( ) ) ) ;
243+ let store = SqliteStore :: new ( temp_path, Some ( "test_db" . to_string ( ) ) , Some ( "test_table" . to_string ( ) ) ) . unwrap ( ) ;
235244 do_read_write_remove_list_persist ( & store) ;
236245 }
237246
238247 #[ test]
239248 fn test_sqlite_store ( ) {
240249 let mut temp_path = std:: env:: temp_dir ( ) ;
241250 temp_path. push ( "test_sqlite_store" ) ;
242- let store_0 = SqliteStore :: new ( temp_path. clone ( ) , Some ( "test_db_0" . to_string ( ) ) , Some ( "test_table" . to_string ( ) ) ) ;
243- let store_1 = SqliteStore :: new ( temp_path, Some ( "test_db_1" . to_string ( ) ) , Some ( "test_table" . to_string ( ) ) ) ;
251+ let store_0 = SqliteStore :: new ( temp_path. clone ( ) , Some ( "test_db_0" . to_string ( ) ) , Some ( "test_table" . to_string ( ) ) ) . unwrap ( ) ;
252+ let store_1 = SqliteStore :: new ( temp_path, Some ( "test_db_1" . to_string ( ) ) , Some ( "test_table" . to_string ( ) ) ) . unwrap ( ) ;
244253 do_test_store ( & store_0, & store_1)
245254 }
246255}
@@ -252,8 +261,8 @@ pub mod bench {
252261
253262 /// Bench!
254263 pub fn bench_sends ( bench : & mut Criterion ) {
255- let store_a = super :: SqliteStore :: new ( "bench_sqlite_store_a" . into ( ) , None , None ) ;
256- let store_b = super :: SqliteStore :: new ( "bench_sqlite_store_b" . into ( ) , None , None ) ;
264+ let store_a = super :: SqliteStore :: new ( "bench_sqlite_store_a" . into ( ) , None , None ) . unwrap ( ) ;
265+ let store_b = super :: SqliteStore :: new ( "bench_sqlite_store_b" . into ( ) , None , None ) . unwrap ( ) ;
257266 lightning:: ln:: channelmanager:: bench:: bench_two_sends (
258267 bench, "bench_sqlite_persisted_sends" , store_a, store_b) ;
259268 }
0 commit comments