From bf04c9a50f3ede02e551bb9fc94f3f04f594a835 Mon Sep 17 00:00:00 2001 From: qianbin Date: Fri, 2 Feb 2024 15:09:59 +0800 Subject: [PATCH] temp --- muxdb/trie.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/muxdb/trie.go b/muxdb/trie.go index 7ac76e0d9..8f8676404 100644 --- a/muxdb/trie.go +++ b/muxdb/trie.go @@ -7,6 +7,7 @@ package muxdb import ( "context" + "errors" "github.com/vechain/thor/v2/thor" "github.com/vechain/thor/v2/trie" @@ -22,6 +23,7 @@ type Trie struct { noFillCache bool filterKeys []string missGetCount int + notExist func() (bool, error) } // newTrie creates a managed trie. @@ -44,6 +46,8 @@ func newTrie( return t } +var noKeyErr = errors.New("no key") + // newDatabase creates a database instance for low-level trie construction. func (t *Trie) newDatabaseReader() trie.DatabaseReader { var keyBuf []byte @@ -62,6 +66,14 @@ func (t *Trie) newDatabaseReader() trie.DatabaseReader { } }() + if f := t.notExist; f != nil { + t.notExist = nil + if notExist, err := f(); err != nil { + return nil, err + } else if notExist { + return nil, noKeyErr + } + } // query in db snapshot := t.back.Store.Snapshot() defer snapshot.Release() @@ -113,13 +125,19 @@ func (t *Trie) DefinitelyNotExist(key []byte) (bool, error) { // The value bytes must not be modified by the caller. func (t *Trie) Get(key []byte) ([]byte, []byte, error) { if t.missGetCount > 200 { - if notExist, err := t.DefinitelyNotExist(key); err != nil { - return nil, nil, err - } else if notExist { - return nil, nil, nil + t.notExist = func() (bool, error) { + return t.DefinitelyNotExist(key) } + defer func() { + t.notExist = nil + }() } if v, m, err := t.trie.Get(key); err != nil { + if err, ok := err.(*trie.MissingNodeError); ok { + if err.Err == noKeyErr { + return nil, nil, nil + } + } return nil, nil, err } else { if len(v) == 0 {