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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

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

func (db *merkleDB) PrefetchKeys(keys [][]byte) error {
dboehm-avalabs marked this conversation as resolved.
Show resolved Hide resolved
dboehm-avalabs marked this conversation as resolved.
Show resolved Hide resolved
db.commitLock.RLock()
defer db.commitLock.RUnlock()

if db.closed {
return database.ErrClosed
}

tempView, err := newTrieView(db, db, ViewChanges{})
if err != nil {
return err
}
for _, key := range keys {
pathToKey, err := tempView.getPathTo(db.newPath(key))
if err != nil {
return err
}
for _, n := range pathToKey {
dboehm-avalabs marked this conversation as resolved.
Show resolved Hide resolved
if n.hasValue() {
db.valueNodeDB.cacheNode(n.key, n)
} else {
if err := db.intermediateNodeDB.cacheNode(n.key, n); err != nil {
return err
}
}
}
}

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
5 changes: 5 additions & 0 deletions x/merkledb/intermediate_node_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ func (db *intermediateNodeDB) constructDBKey(key Path) []byte {
return addPrefixToKey(db.bufferPool, intermediateNodePrefix, key.Append(1).Bytes())
}

// cacheNode ensures that the key/node combo is within the db's cache
func (db *intermediateNodeDB) cacheNode(key Path, n *node) error {
return db.nodeCache.Put(key, n)
}

func (db *intermediateNodeDB) Put(key Path, n *node) error {
return db.nodeCache.Put(key, n)
}
Expand Down
5 changes: 5 additions & 0 deletions x/merkledb/value_node_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func (db *valueNodeDB) newIteratorWithStartAndPrefix(start, prefix []byte) datab
return i
}

// cacheNode ensures that the key/node combo is within the db's cache
func (db *valueNodeDB) cacheNode(key Path, n *node) {
db.nodeCache.Put(key, n)
}

func (db *valueNodeDB) Close() {
db.closed.Set(true)
}
Expand Down
Loading