-
Notifications
You must be signed in to change notification settings - Fork 197
/
trackableDataTrie.go
93 lines (78 loc) · 2.59 KB
/
trackableDataTrie.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package state
import (
"github.com/ElrondNetwork/elrond-go/data"
)
// TrackableDataTrie wraps a PatriciaMerkelTrie adding modifying data capabilities
type TrackableDataTrie struct {
originalData map[string][]byte
dirtyData map[string][]byte
tr data.Trie
}
// NewTrackableDataTrie returns an instance of DataTrieTracker
func NewTrackableDataTrie(tr data.Trie) *TrackableDataTrie {
return &TrackableDataTrie{
tr: tr,
originalData: make(map[string][]byte),
dirtyData: make(map[string][]byte),
}
}
// ClearDataCaches empties the dirtyData map and original map
func (tdaw *TrackableDataTrie) ClearDataCaches() {
tdaw.dirtyData = make(map[string][]byte)
tdaw.originalData = make(map[string][]byte)
}
// DirtyData returns the map of (key, value) pairs that contain the data needed to be saved in the data trie
func (tdaw *TrackableDataTrie) DirtyData() map[string][]byte {
return tdaw.dirtyData
}
// OriginalValue returns the value for a key stored in originalData map which is acting like a cache
func (tdaw *TrackableDataTrie) OriginalValue(key []byte) []byte {
return tdaw.originalData[string(key)]
}
// RetrieveValue fetches the value from a particular key searching the account data store
// The search starts with dirty map, continues with original map and ends with the trie
// Data must have been retrieved from its trie
func (tdaw *TrackableDataTrie) RetrieveValue(key []byte) ([]byte, error) {
strKey := string(key)
//search in dirty data cache
data, found := tdaw.dirtyData[strKey]
if found {
return data, nil
}
//search in original data cache
data, found = tdaw.originalData[strKey]
if found {
return data, nil
}
//ok, not in cache, retrieve from trie
if tdaw.tr == nil {
return nil, ErrNilTrie
}
data, err := tdaw.tr.Get(key)
if err != nil {
return nil, err
}
//got the value, put it originalData cache as the next fetch will run faster
tdaw.originalData[string(key)] = data
return data, nil
}
// SaveKeyValue stores in dirtyData the data keys "touched"
// It does not care if the data is really dirty as calling this check here will be sub-optimal
func (tdaw *TrackableDataTrie) SaveKeyValue(key []byte, value []byte) {
tdaw.dirtyData[string(key)] = value
}
// SetDataTrie sets the internal data trie
func (tdaw *TrackableDataTrie) SetDataTrie(tr data.Trie) {
tdaw.tr = tr
}
// DataTrie sets the internal data trie
func (tdaw *TrackableDataTrie) DataTrie() data.Trie {
return tdaw.tr
}
// IsInterfaceNil returns true if there is no value under the interface
func (tdaw *TrackableDataTrie) IsInterfaceNil() bool {
if tdaw == nil {
return true
}
return false
}