-
Notifications
You must be signed in to change notification settings - Fork 197
/
trackableDataTrie.go
108 lines (90 loc) · 3.27 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
identifier []byte
}
// NewTrackableDataTrie returns an instance of DataTrieTracker
func NewTrackableDataTrie(identifier []byte, tr data.Trie) *TrackableDataTrie {
return &TrackableDataTrie{
tr: tr,
originalData: make(map[string][]byte),
dirtyData: make(map[string][]byte),
identifier: identifier,
}
}
// 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) {
tailLength := len(key) + len(tdaw.identifier)
//search in dirty data cache
if value, found := tdaw.dirtyData[string(key)]; found {
log.Trace("retrieve value from dirty data", "key", key, "value", value)
return trimValue(value, tailLength)
}
//search in original data cache
if value, found := tdaw.originalData[string(key)]; found {
log.Trace("retrieve value from original data", "key", key, "value", value)
return trimValue(value, tailLength)
}
//ok, not in cache, retrieve from trie
if tdaw.tr == nil {
return nil, ErrNilTrie
}
value, err := tdaw.tr.Get(key)
if err != nil {
return nil, err
}
log.Trace("retrieve value from trie", "key", key, "value", value)
value, _ = trimValue(value, tailLength)
//got the value, put it originalData cache as the next fetch will run faster
tdaw.originalData[string(key)] = value
return value, nil
}
func trimValue(value []byte, tailLength int) ([]byte, error) {
dataLength := len(value) - tailLength
if dataLength < 0 {
return nil, ErrNegativeValue
}
return value[:dataLength], 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) {
var identifier []byte
if len(value) != 0 {
identifier = append(key, tdaw.identifier...)
}
tdaw.dirtyData[string(key)] = append(value, identifier...)
}
// 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 {
return tdaw == nil
}