Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MerkleDB allow warming node cache #2128

Merged
merged 6 commits into from
Oct 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions x/merkledb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,59 @@ func (db *merkleDB) Close() error {
return db.baseDB.Put(cleanShutdownKey, hadCleanShutdown)
}

func (db *merkleDB) PrefetchPaths(keys [][]byte) error {
db.commitLock.RLock()
defer db.commitLock.RUnlock()

if db.closed {
return database.ErrClosed
}

// reuse the view so that it can keep repeated nodes in memory
tempView, err := newTrieView(db, db, ViewChanges{})
if err != nil {
return err
}
for _, key := range keys {
if err := db.prefetchPath(tempView, key); err != nil {
return err
}
}

return nil
}

func (db *merkleDB) PrefetchPath(key []byte) error {
db.commitLock.RLock()
defer db.commitLock.RUnlock()

if db.closed {
return database.ErrClosed
}
tempView, err := newTrieView(db, db, ViewChanges{})
if err != nil {
return err
}

return db.prefetchPath(tempView, key)
}

func (db *merkleDB) prefetchPath(view *trieView, key []byte) error {
pathToKey, err := view.getPathTo(db.newPath(key))
if err != nil {
return err
}
for _, n := range pathToKey {
if n.hasValue() {
db.valueNodeDB.nodeCache.Put(n.key, n)
} else if err := db.intermediateNodeDB.nodeCache.Put(n.key, n); err != nil {
return err
}
}
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

func (db *merkleDB) Get(key []byte) ([]byte, error) {
// this is a duplicate because the database interface doesn't support
// contexts, which are used for tracing
Expand Down