Skip to content

Commit

Permalink
core: refactor api for trie journal
Browse files Browse the repository at this point in the history
  • Loading branch information
jingjunLi committed Apr 9, 2024
1 parent 7da286c commit 9039fcb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ var (
}
JournalFileFlag = &cli.BoolFlag{
Name: "journalfile",
Usage: "Enable the in-memory trie node layers to store to wal file when shutdown in pbss (default = false)",
Usage: "Enable journal file to store the TrieJournal when shutdown in pbss (default = false)",
Value: false,
Category: flags.StateCategory,
}
Expand Down
62 changes: 31 additions & 31 deletions triedb/pathdb/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ type journalStorage struct {
Slots [][]byte
}

// journalDB is a journal implementation that stores the journal in db.
type journalDB struct {
// journalKV is a journal implementation that stores the journal in db as a single kv.
type journalKV struct {
Journal
journalBuf bytes.Buffer // Used for temporary storage in memory, and finally uniformly written to the database during sync.
diskdb ethdb.Database // Persistent storage for matured trie nodes
}

// journalWAL is a journal implementation that stores the journal in a file.
type journalWAL struct {
// journalFile is a journal implementation that stores the journal in a file.
type journalFile struct {
Journal
journalFile string
journalFd *os.File
file string // the file used to store the TrieJournal
fd *os.File // the file's fd
}

// loadJournal tries to parse the layer journal from the disk.
Expand Down Expand Up @@ -512,76 +512,76 @@ type Journal interface {
Size() uint64
}

func newJournal(journalFile string, db ethdb.Database) Journal {
if len(journalFile) == 0 {
return &journalDB{
func newJournal(file string, db ethdb.Database) Journal {
if len(file) == 0 {
return &journalKV{
diskdb: db,
}
} else {
return &journalWAL{
journalFile: journalFile,
return &journalFile{
file: file,
}
}
}

func (db *journalDB) newJournalWriter() io.Writer {
func (db *journalKV) newJournalWriter() io.Writer {
return &db.journalBuf
}

func (db *journalDB) newJournalReader() (*rlp.Stream, error) {
func (db *journalKV) newJournalReader() (*rlp.Stream, error) {
journal := rawdb.ReadTrieJournal(db.diskdb)
if len(journal) == 0 {
return nil, errMissJournal
}
return rlp.NewStream(bytes.NewReader(journal), 0), nil
}

func (db *journalDB) Sync() {
func (db *journalKV) Sync() {
rawdb.WriteTrieJournal(db.diskdb, db.journalBuf.Bytes())
db.journalBuf.Reset()
}

func (db *journalDB) Delete() {
func (db *journalKV) Delete() {
rawdb.DeleteTrieJournal(db.diskdb)
}

func (db *journalDB) Close() {
func (db *journalKV) Close() {
}

func (db *journalDB) Size() uint64 {
func (db *journalKV) Size() uint64 {
return uint64(db.journalBuf.Len())
}

// newJournalWriter creates a new journal writer.
func (wal *journalWAL) newJournalWriter() io.Writer {
func (wal *journalFile) newJournalWriter() io.Writer {
var err error
wal.journalFd, err = os.OpenFile(wal.journalFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
wal.fd, err = os.OpenFile(wal.file, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return nil
}
return wal.journalFd
return wal.fd
}

// newJournalReader creates a new journal reader.
func (wal *journalWAL) newJournalReader() (*rlp.Stream, error) {
func (wal *journalFile) newJournalReader() (*rlp.Stream, error) {
var err error
wal.journalFd, err = os.Open(wal.journalFile)
wal.fd, err = os.Open(wal.file)
if errors.Is(err, fs.ErrNotExist) {
return nil, errMissJournal
}
if err != nil {
return nil, err
}
return rlp.NewStream(wal.journalFd, 0), nil
return rlp.NewStream(wal.fd, 0), nil
}

// Sync flushes the journal writer.
func (wal *journalWAL) Sync() {
func (wal *journalFile) Sync() {
}

// Delete deletes the journal.
func (wal *journalWAL) Delete() {
file := wal.journalFile
func (wal *journalFile) Delete() {
file := wal.file
_, err := os.Stat(file)
if os.IsNotExist(err) {
return
Expand All @@ -593,14 +593,14 @@ func (wal *journalWAL) Delete() {
}

// Close closes the journal.
func (wal *journalWAL) Close() {
wal.journalFd.Close()
func (wal *journalFile) Close() {
wal.fd.Close()
}

// Size returns the size of the journal.
func (wal *journalWAL) Size() uint64 {
if wal.journalFd != nil {
fileInfo, err := wal.journalFd.Stat()
func (wal *journalFile) Size() uint64 {
if wal.fd != nil {
fileInfo, err := wal.fd.Stat()
if err != nil {
log.Crit("Failed to stat journal", "err", err)
}
Expand Down

0 comments on commit 9039fcb

Please sign in to comment.