-
Notifications
You must be signed in to change notification settings - Fork 202
/
dataTriesHolder.go
77 lines (61 loc) · 1.69 KB
/
dataTriesHolder.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
package state
import (
"sync"
"github.com/ElrondNetwork/elrond-go/common"
)
type dataTriesHolder struct {
tries map[string]common.Trie
mutex sync.RWMutex
}
// NewDataTriesHolder creates a new instance of dataTriesHolder
func NewDataTriesHolder() *dataTriesHolder {
return &dataTriesHolder{
tries: make(map[string]common.Trie),
}
}
// Put adds a trie pointer to the tries map
func (dth *dataTriesHolder) Put(key []byte, tr common.Trie) {
dth.mutex.Lock()
dth.tries[string(key)] = tr
dth.mutex.Unlock()
}
// Replace changes a trie pointer to the tries map
func (dth *dataTriesHolder) Replace(key []byte, tr common.Trie) {
dth.Put(key, tr)
}
// Get returns the trie pointer that is stored in the map at the given key
func (dth *dataTriesHolder) Get(key []byte) common.Trie {
dth.mutex.Lock()
defer dth.mutex.Unlock()
return dth.tries[string(key)]
}
// GetAll returns all trie pointers from the map
func (dth *dataTriesHolder) GetAll() []common.Trie {
dth.mutex.Lock()
defer dth.mutex.Unlock()
tries := make([]common.Trie, 0)
for _, trie := range dth.tries {
tries = append(tries, trie)
}
return tries
}
// GetAllTries returns the tries with key value map
func (dth *dataTriesHolder) GetAllTries() map[string]common.Trie {
dth.mutex.Lock()
defer dth.mutex.Unlock()
copyTries := make(map[string]common.Trie, len(dth.tries))
for key, trie := range dth.tries {
copyTries[key] = trie
}
return copyTries
}
// Reset clears the tries map
func (dth *dataTriesHolder) Reset() {
dth.mutex.Lock()
dth.tries = make(map[string]common.Trie)
dth.mutex.Unlock()
}
// IsInterfaceNil returns true if underlying object is nil
func (dth *dataTriesHolder) IsInterfaceNil() bool {
return dth == nil
}