-
Notifications
You must be signed in to change notification settings - Fork 199
/
snapshotTrieStorageManager.go
74 lines (59 loc) · 2.04 KB
/
snapshotTrieStorageManager.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
package trie
import (
"fmt"
"github.com/ElrondNetwork/elrond-go/errors"
)
type snapshotTrieStorageManager struct {
*trieStorageManager
mainSnapshotStorer snapshotPruningStorer
epoch uint32
}
func newSnapshotTrieStorageManager(tsm *trieStorageManager, epoch uint32) (*snapshotTrieStorageManager, error) {
storer, ok := tsm.mainStorer.(snapshotPruningStorer)
if !ok {
return nil, fmt.Errorf("invalid storer type")
}
return &snapshotTrieStorageManager{
trieStorageManager: tsm,
mainSnapshotStorer: storer,
epoch: epoch,
}, nil
}
// Get checks all the storers for the given key, and returns it if it is found
func (stsm *snapshotTrieStorageManager) Get(key []byte) ([]byte, error) {
stsm.storageOperationMutex.Lock()
defer stsm.storageOperationMutex.Unlock()
if stsm.closed {
log.Debug("snapshotTrieStorageManager get context closing", "key", key)
return nil, errors.ErrContextClosing
}
val, err := stsm.mainSnapshotStorer.GetFromOldEpochsWithoutAddingToCache(key)
if isClosingError(err) {
return nil, err
}
if len(val) != 0 {
return val, nil
}
return stsm.getFromOtherStorers(key)
}
// Put adds the given value to the main storer
func (stsm *snapshotTrieStorageManager) Put(key, data []byte) error {
stsm.storageOperationMutex.Lock()
defer stsm.storageOperationMutex.Unlock()
if stsm.closed {
log.Debug("snapshotTrieStorageManager put context closing", "key", key, "data", data)
return errors.ErrContextClosing
}
log.Trace("put hash in snapshot storer", "hash", key, "epoch", stsm.epoch)
return stsm.mainSnapshotStorer.PutInEpochWithoutCache(key, data, stsm.epoch)
}
// GetFromLastEpoch searches only the last epoch storer for the given key
func (stsm *snapshotTrieStorageManager) GetFromLastEpoch(key []byte) ([]byte, error) {
stsm.storageOperationMutex.Lock()
defer stsm.storageOperationMutex.Unlock()
if stsm.closed {
log.Debug("snapshotTrieStorageManager getFromLastEpoch context closing", "key", key)
return nil, errors.ErrContextClosing
}
return stsm.mainSnapshotStorer.GetFromLastEpoch(key)
}