-
Notifications
You must be signed in to change notification settings - Fork 199
/
testTriePruningStorer.go
81 lines (71 loc) · 2.4 KB
/
testTriePruningStorer.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
package state
import (
"sync"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/storage"
"github.com/ElrondNetwork/elrond-go/storage/memorydb"
storageMock "github.com/ElrondNetwork/elrond-go/storage/mock"
"github.com/ElrondNetwork/elrond-go/storage/pruning"
"github.com/ElrondNetwork/elrond-go/storage/storageUnit"
"github.com/ElrondNetwork/elrond-go/testscommon"
)
// CreateTestingTriePruningStorer creates a new trie pruning storer that is used for testing
func CreateTestingTriePruningStorer(coordinator sharding.Coordinator, notifier pruning.EpochStartNotifier) (storage.Storer, *persisterMap, error) {
cacheConf := storageUnit.CacheConfig{
Capacity: 10,
Type: "LRU",
Shards: 3,
}
dbConf := storageUnit.DBConfig{
FilePath: "path/Epoch_0/Shard_1",
Type: "LvlDBSerial",
BatchDelaySeconds: 500,
MaxBatchSize: 1,
MaxOpenFiles: 1000,
}
persistersMap := NewPersistersMap()
persisterFactory := &storageMock.PersisterFactoryStub{
CreateCalled: func(path string) (storage.Persister, error) {
return persistersMap.GetPersister(path), nil
},
}
args := &pruning.StorerArgs{
PruningEnabled: true,
Identifier: "id",
ShardCoordinator: coordinator,
PathManager: &testscommon.PathManagerStub{},
CacheConf: cacheConf,
DbPath: dbConf.FilePath,
PersisterFactory: persisterFactory,
NumOfEpochsToKeep: 4,
NumOfActivePersisters: 4,
Notifier: notifier,
OldDataCleanerProvider: &testscommon.OldDataCleanerProviderStub{},
CustomDatabaseRemover: &testscommon.CustomDatabaseRemoverStub{},
MaxBatchSize: 10,
}
tps, err := pruning.NewTriePruningStorer(args)
return tps, persistersMap, err
}
type persisterMap struct {
persisters map[string]storage.Persister
mutex sync.Mutex
}
// NewPersistersMap returns a new persisterMap
func NewPersistersMap() *persisterMap {
return &persisterMap{
persisters: make(map[string]storage.Persister),
mutex: sync.Mutex{},
}
}
// GetPersister returns the persister for the given path, or creates a new persister if it does not exist
func (pm *persisterMap) GetPersister(path string) storage.Persister {
pm.mutex.Lock()
defer pm.mutex.Unlock()
persister, exists := pm.persisters[path]
if !exists {
persister = memorydb.New()
pm.persisters[path] = persister
}
return persister
}