forked from kaspanet/kaspad
-
Notifications
You must be signed in to change notification settings - Fork 3
/
multiset_store.go
93 lines (75 loc) · 2.84 KB
/
multiset_store.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 multisetstore
import (
"github.com/golang/protobuf/proto"
"github.com/ixbaseANT/gord/domain/consensus/database/serialization"
"github.com/ixbaseANT/gord/domain/consensus/model"
"github.com/ixbaseANT/gord/domain/consensus/model/externalapi"
"github.com/ixbaseANT/gord/domain/consensus/utils/lrucache"
"github.com/ixbaseANT/gord/util/staging"
)
var bucketName = []byte("multisets")
// multisetStore represents a store of Multisets
type multisetStore struct {
shardID model.StagingShardID
cache *lrucache.LRUCache
bucket model.DBBucket
}
// New instantiates a new MultisetStore
func New(prefixBucket model.DBBucket, cacheSize int, preallocate bool) model.MultisetStore {
return &multisetStore{
shardID: staging.GenerateShardingID(),
cache: lrucache.New(cacheSize, preallocate),
bucket: prefixBucket.Bucket(bucketName),
}
}
// Stage stages the given multiset for the given blockHash
func (ms *multisetStore) Stage(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash, multiset model.Multiset) {
stagingShard := ms.stagingShard(stagingArea)
stagingShard.toAdd[*blockHash] = multiset.Clone()
}
func (ms *multisetStore) IsStaged(stagingArea *model.StagingArea) bool {
return ms.stagingShard(stagingArea).isStaged()
}
// Get gets the multiset associated with the given blockHash
func (ms *multisetStore) Get(dbContext model.DBReader, stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (model.Multiset, error) {
stagingShard := ms.stagingShard(stagingArea)
if multiset, ok := stagingShard.toAdd[*blockHash]; ok {
return multiset.Clone(), nil
}
if multiset, ok := ms.cache.Get(blockHash); ok {
return multiset.(model.Multiset).Clone(), nil
}
multisetBytes, err := dbContext.Get(ms.hashAsKey(blockHash))
if err != nil {
return nil, err
}
multiset, err := ms.deserializeMultiset(multisetBytes)
if err != nil {
return nil, err
}
ms.cache.Add(blockHash, multiset)
return multiset.Clone(), nil
}
// Delete deletes the multiset associated with the given blockHash
func (ms *multisetStore) Delete(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) {
stagingShard := ms.stagingShard(stagingArea)
if _, ok := stagingShard.toAdd[*blockHash]; ok {
delete(stagingShard.toAdd, *blockHash)
return
}
stagingShard.toDelete[*blockHash] = struct{}{}
}
func (ms *multisetStore) hashAsKey(hash *externalapi.DomainHash) model.DBKey {
return ms.bucket.Key(hash.ByteSlice())
}
func (ms *multisetStore) serializeMultiset(multiset model.Multiset) ([]byte, error) {
return proto.Marshal(serialization.MultisetToDBMultiset(multiset))
}
func (ms *multisetStore) deserializeMultiset(multisetBytes []byte) (model.Multiset, error) {
dbMultiset := &serialization.DbMultiset{}
err := proto.Unmarshal(multisetBytes, dbMultiset)
if err != nil {
return nil, err
}
return serialization.DBMultisetToMultiset(dbMultiset)
}