@@ -146,7 +146,9 @@ impl Commitments {
146146 salt : None ,
147147 } ;
148148 db_entry. lock ( ) . replace ( Arc :: new (
149- Db :: open_or_create ( & options) . map_err ( CommitmentError :: CommitmentDb ) ?,
149+ Db :: open_or_create ( & options)
150+ . map ( |db| ( db, options) )
151+ . map_err ( CommitmentError :: CommitmentDb ) ?,
150152 ) ) ;
151153 }
152154
@@ -179,7 +181,8 @@ impl Commitments {
179181
180182 let db_guard = db_entry. lock ( ) ;
181183
182- if let Some ( db) = db_guard. as_ref ( ) {
184+ if let Some ( db_with_options) = db_guard. as_ref ( ) {
185+ let ( db, _options) = db_with_options. as_ref ( ) ;
183186 for ( tag, offset) in tags. into_iter ( ) . zip ( batch_start..) {
184187 tags_with_offset. push ( ( tag, offset. to_le_bytes ( ) ) ) ;
185188 }
@@ -195,6 +198,11 @@ impl Commitments {
195198 . map_err ( CommitmentError :: CommitmentDb ) ?;
196199
197200 tags_with_offset. clear ( ) ;
201+
202+ // Hack: Reopen database to free memory, see
203+ // https://github.com/paritytech/parity-db/issues/93#issuecomment-1241812705
204+ drop ( db_guard) ;
205+ db_entry. reopen ( ) ?;
198206 }
199207 } else {
200208 // Database was already removed, no need to continue
@@ -207,7 +215,9 @@ impl Commitments {
207215 if let Some ( db_entry) = self . get_db_entry ( salt) {
208216 let db_guard = db_entry. lock ( ) ;
209217
210- if let Some ( db) = db_guard. as_ref ( ) {
218+ if let Some ( db_with_options) = db_guard. as_ref ( ) {
219+ let ( db, _options) = db_with_options. as_ref ( ) ;
220+
211221 tags_with_offset. sort_by ( |( tag_a, _) , ( tag_b, _) | tag_a. cmp ( tag_b) ) ;
212222
213223 db. commit (
@@ -216,6 +226,11 @@ impl Commitments {
216226 . map ( |( tag, offset) | ( 0 , tag, Some ( offset. to_vec ( ) ) ) ) ,
217227 )
218228 . map_err ( CommitmentError :: CommitmentDb ) ?;
229+
230+ // Hack: Reopen database to free memory, see
231+ // https://github.com/paritytech/parity-db/issues/93#issuecomment-1241812705
232+ drop ( db_guard) ;
233+ db_entry. reopen ( ) ?;
219234 }
220235 }
221236
@@ -255,13 +270,15 @@ impl Commitments {
255270 let salt = db_entry. salt ( ) ;
256271 let db_guard = db_entry. lock ( ) ;
257272
258- if let Some ( db) = db_guard. as_ref ( ) {
259- db. commit (
260- pieces
261- . iter ( )
262- . map ( |piece| ( 0 , create_tag ( piece, salt) , None ) ) ,
263- )
264- . map_err ( CommitmentError :: CommitmentDb ) ?;
273+ if let Some ( db_with_options) = db_guard. as_ref ( ) {
274+ db_with_options
275+ . 0
276+ . commit (
277+ pieces
278+ . iter ( )
279+ . map ( |piece| ( 0 , create_tag ( piece, salt) , None ) ) ,
280+ )
281+ . map_err ( CommitmentError :: CommitmentDb ) ?;
265282 }
266283 }
267284
@@ -285,19 +302,21 @@ impl Commitments {
285302 let salt = db_entry. salt ( ) ;
286303 let db_guard = db_entry. lock ( ) ;
287304
288- if let Some ( db ) = db_guard. as_ref ( ) {
305+ if let Some ( db_with_options ) = db_guard. as_ref ( ) {
289306 let mut tags_with_offset: Vec < ( Tag , PieceOffset ) > = pieces_with_offsets ( )
290307 . map ( |( piece_offset, piece) | ( create_tag ( piece, salt) , piece_offset) )
291308 . collect ( ) ;
292309
293310 tags_with_offset. sort_by ( |( tag_a, _) , ( tag_b, _) | tag_a. cmp ( tag_b) ) ;
294311
295- db. commit (
296- tags_with_offset
297- . into_iter ( )
298- . map ( |( tag, offset) | ( 0 , tag, Some ( offset. to_le_bytes ( ) . to_vec ( ) ) ) ) ,
299- )
300- . map_err ( CommitmentError :: CommitmentDb ) ?;
312+ db_with_options
313+ . 0
314+ . commit (
315+ tags_with_offset
316+ . into_iter ( )
317+ . map ( |( tag, offset) | ( 0 , tag, Some ( offset. to_le_bytes ( ) . to_vec ( ) ) ) ) ,
318+ )
319+ . map_err ( CommitmentError :: CommitmentDb ) ?;
301320 } ;
302321 }
303322
@@ -326,13 +345,13 @@ impl Commitments {
326345 return Vec :: new ( ) ;
327346 }
328347 } ;
329- let db = match db_guard. as_ref ( ) {
330- Some ( db ) => db ,
348+ let db_with_options = match db_guard. as_ref ( ) {
349+ Some ( db_with_options ) => db_with_options ,
331350 None => {
332351 return Vec :: new ( ) ;
333352 }
334353 } ;
335- let iter = match db . iter ( 0 ) {
354+ let iter = match db_with_options . 0 . iter ( 0 ) {
336355 Ok ( iter) => iter,
337356 Err ( _) => {
338357 return Vec :: new ( ) ;
0 commit comments