@@ -68,7 +68,6 @@ impl KVStore for SqliteStore {
68
68
let sql =
69
69
format ! ( "SELECT value FROM {} WHERE namespace=:namespace AND key=:key;" , KV_TABLE_NAME ) ;
70
70
71
- let msg = format ! ( "Failed to read from key: {}/{}" , namespace, key) ;
72
71
let res = self
73
72
. connection
74
73
. lock ( )
@@ -81,23 +80,25 @@ impl KVStore for SqliteStore {
81
80
} ,
82
81
|row| row. get ( 0 ) ,
83
82
)
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
+ } ) ?;
85
87
Ok ( Cursor :: new ( res) )
86
88
}
87
89
88
90
fn write ( & self , namespace : & str , key : & str , buf : & [ u8 ] ) -> std:: io:: Result < ( ) > {
89
91
let mut locked_conn = self . connection . lock ( ) . unwrap ( ) ;
90
92
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
+ } ) ?;
95
97
96
98
let sql = format ! (
97
99
"INSERT OR REPLACE INTO {} (namespace, key, value) VALUES (:namespace, :key, :data);" ,
98
100
KV_TABLE_NAME
99
101
) ;
100
- let msg = format ! ( "Failed to write to key: {}/{}" , namespace, key) ;
101
102
sql_tx
102
103
. execute (
103
104
& sql,
@@ -107,17 +108,21 @@ impl KVStore for SqliteStore {
107
108
":data" : buf,
108
109
} ,
109
110
)
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
+ } ) ?;
114
120
115
121
Ok ( ( ) )
116
122
}
117
123
118
124
fn remove ( & self , namespace : & str , key : & str ) -> std:: io:: Result < bool > {
119
125
let sql = format ! ( "DELETE FROM {} WHERE namespace=:namespace AND key=:key;" , KV_TABLE_NAME ) ;
120
- let msg = format ! ( "Failed to delete key: {}/{}" , namespace, key) ;
121
126
let changes = self
122
127
. connection
123
128
. lock ( )
@@ -129,7 +134,10 @@ impl KVStore for SqliteStore {
129
134
":key" : key,
130
135
} ,
131
136
)
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
+ } ) ?;
133
141
134
142
let was_present = changes != 0 ;
135
143
@@ -140,21 +148,25 @@ impl KVStore for SqliteStore {
140
148
let locked_conn = self . connection . lock ( ) . unwrap ( ) ;
141
149
142
150
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
+ } ) ?;
147
155
148
156
let mut keys = Vec :: new ( ) ;
149
157
150
- let msg = format ! ( "Failed to retrieve queried rows" ) ;
151
158
let rows_iter = stmt
152
159
. 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
+ } ) ?;
154
164
155
165
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
+ } ) ?) ;
158
170
}
159
171
160
172
Ok ( keys)
@@ -163,20 +175,21 @@ impl KVStore for SqliteStore {
163
175
164
176
impl KVStorePersister for SqliteStore {
165
177
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) ;
167
178
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)
169
181
} ) ?;
170
182
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
+ } ) ?;
175
187
let namespace = parent_directory. display ( ) . to_string ( ) ;
176
188
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
+ } ) ?;
180
193
let key = dest_without_namespace. display ( ) . to_string ( ) ;
181
194
182
195
self . write ( & namespace, & key, & object. encode ( ) ) ?;
0 commit comments