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

Add dummy cache for inmem ops #883

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion muxdb/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// backend is the backend of the trie.
type backend struct {
Store kv.Store
Cache *cache
Cache Cache
HistPtnFactor, DedupedPtnFactor uint32
CachedNodeTTL uint16
}
Expand Down
39 changes: 27 additions & 12 deletions muxdb/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import (
"github.com/vechain/thor/v2/trie"
)

type Cache interface {
AddNodeBlob(keyBuf *[]byte, name string, path []byte, ver trie.Version, blob []byte, isCommitting bool)
GetNodeBlob(keyBuf *[]byte, name string, path []byte, ver trie.Version, peek bool) []byte
AddRootNode(name string, n trie.Node)
GetRootNode(name string, ver trie.Version) trie.Node
}

// cache is the cache layer for trie.
type cache struct {
queriedNodes *directcache.Cache // caches recently queried node blobs.
Expand All @@ -34,7 +41,7 @@ type cache struct {
}

// newCache creates a cache object with the given cache size.
func newCache(sizeMB int, rootTTL uint32) *cache {
func newCache(sizeMB int, rootTTL uint32) Cache {
sizeBytes := sizeMB * 1024 * 1024
cache := &cache{
queriedNodes: directcache.New(sizeBytes / 4),
Expand Down Expand Up @@ -65,10 +72,6 @@ func (c *cache) log() {

// AddNodeBlob adds encoded node blob into the cache.
func (c *cache) AddNodeBlob(keyBuf *[]byte, name string, path []byte, ver trie.Version, blob []byte, isCommitting bool) {
if c == nil {
return
}

// the version part
v := binary.AppendUvarint((*keyBuf)[:0], uint64(ver.Major))
v = binary.AppendUvarint(v, uint64(ver.Minor))
Expand All @@ -89,9 +92,6 @@ func (c *cache) AddNodeBlob(keyBuf *[]byte, name string, path []byte, ver trie.V

// GetNodeBlob returns the cached node blob.
func (c *cache) GetNodeBlob(keyBuf *[]byte, name string, path []byte, ver trie.Version, peek bool) []byte {
if c == nil {
return nil
}
// the version part
v := binary.AppendUvarint((*keyBuf)[:0], uint64(ver.Major))
v = binary.AppendUvarint(v, uint64(ver.Minor))
Expand Down Expand Up @@ -130,7 +130,7 @@ func (c *cache) GetNodeBlob(keyBuf *[]byte, name string, path []byte, ver trie.V

// AddRootNode add the root node into the cache.
func (c *cache) AddRootNode(name string, n trie.Node) {
if c == nil || n == nil {
if n == nil {
return
}
c.roots.lock.Lock()
Expand All @@ -151,9 +151,6 @@ func (c *cache) AddRootNode(name string, n trie.Node) {

// GetRootNode returns the cached root node.
func (c *cache) GetRootNode(name string, ver trie.Version) trie.Node {
if c == nil {
return nil
}
c.roots.lock.RLock()
defer c.roots.lock.RUnlock()

Expand Down Expand Up @@ -200,3 +197,21 @@ func (cs *cacheStats) ShouldLog(msg string) (func(), bool) {
cs.flag.Store(flag)
}, cs.flag.Load() != flag
}

type dummyCache struct{}

// AddNodeBlob is a no-op.
func (*dummyCache) AddNodeBlob(_ *[]byte, _ string, _ []byte, _ trie.Version, _ []byte, _ bool) {}

// GetNodeBlob always returns nil.
func (*dummyCache) GetNodeBlob(_ *[]byte, _ string, _ []byte, _ trie.Version, _ bool) []byte {
return nil
}

// AddRootNode is a no-op.
func (*dummyCache) AddRootNode(_ string, _ trie.Node) {}

// GetRootNode always returns nil.
func (*dummyCache) GetRootNode(_ string, _ trie.Version) trie.Node {
return nil
}
2 changes: 1 addition & 1 deletion muxdb/muxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func NewMem() *MuxDB {
engine: engine,
trieBackend: &backend{
Store: engine,
Cache: nil,
Cache: &dummyCache{},
HistPtnFactor: 1,
DedupedPtnFactor: 1,
CachedNodeTTL: 32,
Expand Down
2 changes: 1 addition & 1 deletion muxdb/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newTestBackend() *backend {
engine := newTestEngine()
return &backend{
Store: engine,
Cache: nil,
Cache: &dummyCache{},
HistPtnFactor: 1,
DedupedPtnFactor: 1,
CachedNodeTTL: 100,
Expand Down