@@ -68,7 +68,6 @@ impl KVStore for SqliteStore {
6868 let sql =
6969 format ! ( "SELECT value FROM {} WHERE namespace=:namespace AND key=:key;" , KV_TABLE_NAME ) ;
7070
71- let msg = format ! ( "Failed to read from key: {}/{}" , namespace, key) ;
7271 let res = self
7372 . connection
7473 . lock ( )
@@ -81,23 +80,25 @@ impl KVStore for SqliteStore {
8180 } ,
8281 |row| row. get ( 0 ) ,
8382 )
84- . map_err ( |_| std:: io:: Error :: new ( std:: io:: ErrorKind :: NotFound , msg) ) ?;
83+ . map_err ( |_| {
84+ let msg = format ! ( "Failed to read from key: {}/{}" , namespace, key) ;
85+ std:: io:: Error :: new ( std:: io:: ErrorKind :: NotFound , msg)
86+ } ) ?;
8587 Ok ( Cursor :: new ( res) )
8688 }
8789
8890 fn write ( & self , namespace : & str , key : & str , buf : & [ u8 ] ) -> std:: io:: Result < ( ) > {
8991 let mut locked_conn = self . connection . lock ( ) . unwrap ( ) ;
9092
91- let msg = format ! ( "Failed to start transaction" ) ;
92- let sql_tx = locked_conn
93- . transaction ( )
94- . map_err ( |_| std :: io :: Error :: new ( std :: io :: ErrorKind :: Other , msg ) ) ?;
93+ let sql_tx = locked_conn . transaction ( ) . map_err ( |_| {
94+ let msg = format ! ( "Failed to start transaction" ) ;
95+ std :: io :: Error :: new ( std :: io :: ErrorKind :: Other , msg )
96+ } ) ?;
9597
9698 let sql = format ! (
9799 "INSERT OR REPLACE INTO {} (namespace, key, value) VALUES (:namespace, :key, :data);" ,
98100 KV_TABLE_NAME
99101 ) ;
100- let msg = format ! ( "Failed to write to key: {}/{}" , namespace, key) ;
101102 sql_tx
102103 . execute (
103104 & sql,
@@ -107,17 +108,21 @@ impl KVStore for SqliteStore {
107108 ":data" : buf,
108109 } ,
109110 )
110- . map_err ( |_| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg) ) ?;
111-
112- let msg = format ! ( "Failed to commit transaction" ) ;
113- sql_tx. commit ( ) . map_err ( |_| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg) ) ?;
111+ . map_err ( |_| {
112+ let msg = format ! ( "Failed to write to key: {}/{}" , namespace, key) ;
113+ std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg)
114+ } ) ?;
115+
116+ sql_tx. commit ( ) . map_err ( |_| {
117+ let msg = format ! ( "Failed to commit transaction" ) ;
118+ std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg)
119+ } ) ?;
114120
115121 Ok ( ( ) )
116122 }
117123
118124 fn remove ( & self , namespace : & str , key : & str ) -> std:: io:: Result < bool > {
119125 let sql = format ! ( "DELETE FROM {} WHERE namespace=:namespace AND key=:key;" , KV_TABLE_NAME ) ;
120- let msg = format ! ( "Failed to delete key: {}/{}" , namespace, key) ;
121126 let changes = self
122127 . connection
123128 . lock ( )
@@ -129,7 +134,10 @@ impl KVStore for SqliteStore {
129134 ":key" : key,
130135 } ,
131136 )
132- . map_err ( |_| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg) ) ?;
137+ . map_err ( |_| {
138+ let msg = format ! ( "Failed to delete key: {}/{}" , namespace, key) ;
139+ std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg)
140+ } ) ?;
133141
134142 let was_present = changes != 0 ;
135143
@@ -140,21 +148,25 @@ impl KVStore for SqliteStore {
140148 let locked_conn = self . connection . lock ( ) . unwrap ( ) ;
141149
142150 let sql = format ! ( "SELECT key FROM {} WHERE namespace=:namespace" , KV_TABLE_NAME ) ;
143- let msg = format ! ( "Failed to prepare statement" ) ;
144- let mut stmt = locked_conn
145- . prepare ( & sql )
146- . map_err ( |_| std :: io :: Error :: new ( std :: io :: ErrorKind :: Other , msg ) ) ?;
151+ let mut stmt = locked_conn . prepare ( & sql ) . map_err ( |_| {
152+ let msg = format ! ( "Failed to prepare statement" ) ;
153+ std :: io :: Error :: new ( std :: io :: ErrorKind :: Other , msg )
154+ } ) ?;
147155
148156 let mut keys = Vec :: new ( ) ;
149157
150- let msg = format ! ( "Failed to retrieve queried rows" ) ;
151158 let rows_iter = stmt
152159 . query_map ( named_params ! { ":namespace" : namespace, } , |row| row. get ( 0 ) )
153- . map_err ( |_| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg) ) ?;
160+ . map_err ( |_| {
161+ let msg = format ! ( "Failed to retrieve queried rows" ) ;
162+ std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg)
163+ } ) ?;
154164
155165 for k in rows_iter {
156- let msg = format ! ( "Failed to retrieve queried rows" ) ;
157- keys. push ( k. map_err ( |_| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg) ) ?) ;
166+ keys. push ( k. map_err ( |_| {
167+ let msg = format ! ( "Failed to retrieve queried rows" ) ;
168+ std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg)
169+ } ) ?) ;
158170 }
159171
160172 Ok ( keys)
@@ -163,20 +175,21 @@ impl KVStore for SqliteStore {
163175
164176impl KVStorePersister for SqliteStore {
165177 fn persist < W : Writeable > ( & self , prefixed_key : & str , object : & W ) -> lightning:: io:: Result < ( ) > {
166- let msg = format ! ( "Could not persist file for key {}." , prefixed_key) ;
167178 let dest_file_path = PathBuf :: from_str ( prefixed_key) . map_err ( |_| {
168- lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: InvalidInput , msg. clone ( ) )
179+ let msg = format ! ( "Could not persist file for key {}." , prefixed_key) ;
180+ lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: InvalidInput , msg)
169181 } ) ?;
170182
171- let parent_directory = dest_file_path. parent ( ) . ok_or ( lightning :: io :: Error :: new (
172- lightning :: io :: ErrorKind :: InvalidInput ,
173- msg . clone ( ) ,
174- ) ) ?;
183+ let parent_directory = dest_file_path. parent ( ) . ok_or_else ( || {
184+ let msg = format ! ( "Could not persist file for key {}." , prefixed_key ) ;
185+ lightning :: io :: Error :: new ( lightning :: io :: ErrorKind :: InvalidInput , msg )
186+ } ) ?;
175187 let namespace = parent_directory. display ( ) . to_string ( ) ;
176188
177- let dest_without_namespace = dest_file_path
178- . strip_prefix ( & namespace)
179- . map_err ( |_| lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: InvalidInput , msg) ) ?;
189+ let dest_without_namespace = dest_file_path. strip_prefix ( & namespace) . map_err ( |_| {
190+ let msg = format ! ( "Could not persist file for key {}." , prefixed_key) ;
191+ lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: InvalidInput , msg)
192+ } ) ?;
180193 let key = dest_without_namespace. display ( ) . to_string ( ) ;
181194
182195 self . write ( & namespace, & key, & object. encode ( ) ) ?;
0 commit comments