Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
qianbin committed Feb 2, 2024
1 parent c379075 commit bf04c9a
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions muxdb/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package muxdb

import (
"context"
"errors"

"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/trie"
Expand All @@ -22,6 +23,7 @@ type Trie struct {
noFillCache bool
filterKeys []string
missGetCount int
notExist func() (bool, error)
}

// newTrie creates a managed trie.
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit bf04c9a

Please sign in to comment.