@@ -21,6 +21,7 @@ import (
2121 "errors"
2222 "fmt"
2323 "io"
24+ "os"
2425 "time"
2526
2627 "github.com/ethereum/go-ethereum/common"
@@ -51,11 +52,24 @@ const journalVersion uint64 = 3
5152
5253// loadJournal tries to parse the layer journal from the disk.
5354func (db * Database ) loadJournal (diskRoot common.Hash ) (layer , error ) {
54- journal := rawdb .ReadTrieJournal (db .diskdb )
55- if len (journal ) == 0 {
56- return nil , errMissJournal
55+ var reader io.Reader
56+ if db .config .Journal != "" && common .FileExist (db .config .Journal ) {
57+ log .Info ("Load pathdb disklayer journal" , "path" , db .config .Journal )
58+ // If a journal file is specified, read it from there
59+ f , err := os .OpenFile (db .config .Journal , os .O_RDONLY , 0644 )
60+ if err != nil {
61+ return nil , fmt .Errorf ("failed to read journal file %s: %w" , db .config .Journal , err )
62+ }
63+ defer f .Close ()
64+ reader = f
65+ } else {
66+ journal := rawdb .ReadTrieJournal (db .diskdb )
67+ if len (journal ) == 0 {
68+ return nil , errMissJournal
69+ }
70+ reader = bytes .NewReader (journal )
5771 }
58- r := rlp .NewStream (bytes . NewReader ( journal ) , 0 )
72+ r := rlp .NewStream (reader , 0 )
5973
6074 // Firstly, resolve the first element as the journal version
6175 version , err := r .Uint64 ()
@@ -315,8 +329,21 @@ func (db *Database) Journal(root common.Hash) error {
315329 if db .readOnly {
316330 return errDatabaseReadOnly
317331 }
332+
333+ var journal io.Writer
334+ // Store the journal into the database and return
335+ if db .config .Journal != "" {
336+ file , err := os .OpenFile (db .config .Journal , os .O_WRONLY | os .O_CREATE , 0644 )
337+ if err != nil {
338+ return fmt .Errorf ("failed to open journal file %s: %w" , db .config .Journal , err )
339+ }
340+ defer file .Close ()
341+ journal = file
342+ } else {
343+ journal = new (bytes.Buffer )
344+ }
345+
318346 // Firstly write out the metadata of journal
319- journal := new (bytes.Buffer )
320347 if err := rlp .Encode (journal , journalVersion ); err != nil {
321348 return err
322349 }
@@ -333,11 +360,20 @@ func (db *Database) Journal(root common.Hash) error {
333360 if err := l .journal (journal ); err != nil {
334361 return err
335362 }
363+
336364 // Store the journal into the database and return
337- rawdb .WriteTrieJournal (db .diskdb , journal .Bytes ())
365+ var size int
366+ if db .config .Journal == "" {
367+ data := journal .(* bytes.Buffer )
368+ size = data .Len ()
369+ rawdb .WriteTrieJournal (db .diskdb , data .Bytes ())
370+ } else {
371+ stat , _ := journal .(* os.File ).Stat ()
372+ size = int (stat .Size ())
373+ }
338374
339375 // Set the db in read only mode to reject all following mutations
340376 db .readOnly = true
341- log .Info ("Persisted dirty state to disk" , "size" , common .StorageSize (journal . Len () ), "elapsed" , common .PrettyDuration (time .Since (start )))
377+ log .Info ("Persisted dirty state to disk" , "size" , common .StorageSize (size ), "elapsed" , common .PrettyDuration (time .Since (start )))
342378 return nil
343379}
0 commit comments