-
Notifications
You must be signed in to change notification settings - Fork 5
/
mvview.go
204 lines (171 loc) · 5.33 KB
/
mvview.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package block_stm
import (
"io"
"cosmossdk.io/store/cachekv"
"cosmossdk.io/store/tracekv"
storetypes "cosmossdk.io/store/types"
)
var (
_ storetypes.KVStore = (*GMVMemoryView[[]byte])(nil)
_ storetypes.ObjKVStore = (*GMVMemoryView[any])(nil)
_ MVView = (*GMVMemoryView[[]byte])(nil)
_ MVView = (*GMVMemoryView[any])(nil)
)
// GMVMemoryView[V] wraps `MVMemory` for execution of a single transaction.
type GMVMemoryView[V any] struct {
storage storetypes.GKVStore[V]
mvData *GMVData[V]
scheduler *Scheduler
store int
txn TxnIndex
readSet *ReadSet
writeSet *GMemDB[V]
}
func NewMVView(store int, storage storetypes.Store, mvData MVStore, scheduler *Scheduler, txn TxnIndex) MVView {
switch data := mvData.(type) {
case *GMVData[any]:
return NewGMVMemoryView(store, storage.(storetypes.ObjKVStore), data, scheduler, txn)
case *GMVData[[]byte]:
return NewGMVMemoryView(store, storage.(storetypes.KVStore), data, scheduler, txn)
default:
panic("unsupported value type")
}
}
func NewGMVMemoryView[V any](store int, storage storetypes.GKVStore[V], mvData *GMVData[V], scheduler *Scheduler, txn TxnIndex) *GMVMemoryView[V] {
return &GMVMemoryView[V]{
store: store,
storage: storage,
mvData: mvData,
scheduler: scheduler,
txn: txn,
readSet: new(ReadSet),
}
}
func (s *GMVMemoryView[V]) init() {
if s.writeSet == nil {
s.writeSet = NewGMemDBNonConcurrent(s.mvData.isZero, s.mvData.valueLen)
}
}
func (s *GMVMemoryView[V]) waitFor(txn TxnIndex) {
cond := s.scheduler.WaitForDependency(s.txn, txn)
if cond != nil {
cond.Wait()
}
}
func (s *GMVMemoryView[V]) ApplyWriteSet(version TxnVersion) Locations {
if s.writeSet == nil || s.writeSet.Len() == 0 {
return nil
}
newLocations := make([]Key, 0, s.writeSet.Len())
s.writeSet.Scan(func(key Key, value V) bool {
s.mvData.Write(key, value, version)
newLocations = append(newLocations, key)
return true
})
return newLocations
}
func (s *GMVMemoryView[V]) ReadSet() *ReadSet {
return s.readSet
}
func (s *GMVMemoryView[V]) Get(key []byte) V {
if s.writeSet != nil {
if value, found := s.writeSet.OverlayGet(key); found {
// value written by this txn
// nil value means deleted
return value
}
}
for {
value, version, estimate := s.mvData.Read(key, s.txn)
if estimate {
// read ESTIMATE mark, wait for the blocking txn to finish
s.waitFor(version.Index)
continue
}
// record the read version, invalid version is ⊥.
// if not found, record version ⊥ when reading from storage.
s.readSet.Reads = append(s.readSet.Reads, ReadDescriptor{key, version})
if !version.Valid() {
return s.storage.Get(key)
}
return value
}
}
func (s *GMVMemoryView[V]) Has(key []byte) bool {
return !s.mvData.isZero(s.Get(key))
}
func (s *GMVMemoryView[V]) Set(key []byte, value V) {
if s.mvData.isZero(value) {
panic("nil value is not allowed")
}
s.init()
s.writeSet.OverlaySet(key, value)
}
func (s *GMVMemoryView[V]) Delete(key []byte) {
var empty V
s.init()
s.writeSet.OverlaySet(key, empty)
}
func (s *GMVMemoryView[V]) Iterator(start, end []byte) storetypes.GIterator[V] {
return s.iterator(IteratorOptions{Start: start, End: end, Ascending: true})
}
func (s *GMVMemoryView[V]) ReverseIterator(start, end []byte) storetypes.GIterator[V] {
return s.iterator(IteratorOptions{Start: start, End: end, Ascending: false})
}
func (s *GMVMemoryView[V]) iterator(opts IteratorOptions) storetypes.GIterator[V] {
mvIter := s.mvData.Iterator(opts, s.txn, s.waitFor)
var parentIter, wsIter storetypes.GIterator[V]
if s.writeSet == nil {
wsIter = NewNoopIterator[V](opts.Start, opts.End, opts.Ascending)
} else {
wsIter = s.writeSet.iterator(opts.Start, opts.End, opts.Ascending)
}
if opts.Ascending {
parentIter = s.storage.Iterator(opts.Start, opts.End)
} else {
parentIter = s.storage.ReverseIterator(opts.Start, opts.End)
}
onClose := func(iter storetypes.GIterator[V]) {
reads := mvIter.Reads()
var stopKey Key
if iter.Valid() {
stopKey = iter.Key()
// if the iterator is not exhausted, the merge iterator may have read one more key which is not observed by
// the caller, in that case we remove that read descriptor.
if len(reads) > 0 {
lastRead := reads[len(reads)-1].Key
if BytesBeyond(lastRead, stopKey, opts.Ascending) {
reads = reads[:len(reads)-1]
}
}
}
s.readSet.Iterators = append(s.readSet.Iterators, IteratorDescriptor{
IteratorOptions: opts,
Stop: stopKey,
Reads: reads,
})
}
// three-way merge iterator
return NewCacheMergeIterator(
NewCacheMergeIterator(parentIter, mvIter, opts.Ascending, nil, s.mvData.isZero),
wsIter,
opts.Ascending,
onClose,
s.mvData.isZero,
)
}
// CacheWrap implements types.Store.
func (s *GMVMemoryView[V]) CacheWrap() storetypes.CacheWrap {
return cachekv.NewGStore(s, s.mvData.isZero, s.mvData.valueLen)
}
// CacheWrapWithTrace implements types.Store.
func (s *GMVMemoryView[V]) CacheWrapWithTrace(w io.Writer, tc storetypes.TraceContext) storetypes.CacheWrap {
if store, ok := any(s).(*GMVMemoryView[[]byte]); ok {
return cachekv.NewGStore(tracekv.NewStore(store, w, tc), store.mvData.isZero, store.mvData.valueLen)
}
return s.CacheWrap()
}
// GetStoreType implements types.Store.
func (s *GMVMemoryView[V]) GetStoreType() storetypes.StoreType {
return s.storage.GetStoreType()
}