Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
qianbin committed Feb 3, 2024
1 parent 5c9e18a commit 1ec73f8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
39 changes: 34 additions & 5 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 @@ -16,11 +17,13 @@ const leafFilterLen = 8

// Trie is the managed trie.
type Trie struct {
name string
back *backend
trie *trie.Trie
noFillCache bool
filterKeys []string
name string
back *backend
trie *trie.Trie
noFillCache bool
filterKeys []string
missGetCount int
notExist func() (bool, error)
}

// newTrie creates a managed trie.
Expand All @@ -43,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 @@ -61,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 @@ -111,9 +124,25 @@ func (t *Trie) DefinitelyNotExist(key []byte) (bool, error) {
// Get returns the value for key stored in the trie.
// The value bytes must not be modified by the caller.
func (t *Trie) Get(key []byte) ([]byte, []byte, error) {
if t.missGetCount > 200 {
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 {
t.missGetCount++
}
return v, m, nil
}
}
Expand Down
8 changes: 4 additions & 4 deletions trie/hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ func (h *hasher) store(n node, db DatabaseWriter, path []byte) (node, error) {
// short node is stored when only when it's the root node
//
// This is a very significant improvement compared to maindb-v3. Short-nodes are embedded
// in full-nodes whenever possible. Doing this can save huge storage space, because the
// 32-byte hash value of the short-node is omitted, and most short-nodes themselves are small,
// only slightly larger than 32 bytes.
if isRoot {
// in full-nodes whenever possible. Doing this can save huge storage space.
//
// For a hash-skipped trie, a short-node is stored as standalone node, since
if isRoot || h.skipHash {
h.buf = n.encode(h.buf[:0], h.skipHash)
if err := db.Put(path, h.newVer, h.buf); err != nil {
return nil, err
Expand Down

0 comments on commit 1ec73f8

Please sign in to comment.