-
Notifications
You must be signed in to change notification settings - Fork 4
/
map_tx.go
212 lines (174 loc) · 3.96 KB
/
map_tx.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
205
206
207
208
209
210
211
212
package vermap
import (
"math"
"sync"
"sync/atomic"
"github.com/insolar/assured-ledger/ledger-core/vanilla/longbits"
)
var _ TxMap = &Tx{}
const pendingTxVersion = uint64(math.MaxUint64)
const discardedTxVersion = uint64(0)
const firstTxVersion = uint64(1)
type Tx struct {
container *IncrementalMap
viewVersion uint64
txMark *txMark
options Options
mutex sync.RWMutex
pending map[longbits.ByteString]txEntry
//deleted map[longbits.ByteString]struct{}
}
func (t *Tx) canReplace() bool {
return t.options&CanReplace != 0
}
func (t *Tx) canDelete() bool {
return t.options&CanDelete != 0
}
func (t *Tx) canUpdate() (canUpdate bool, isDiscarded bool) {
v := t.txMark.getVersion()
return v == pendingTxVersion && t.options&CanUpdate != 0, v == discardedTxVersion
}
func (t *Tx) isPending() bool {
v := t.txMark.getVersion()
return v == pendingTxVersion
}
func (t *Tx) ensureUpdate() error {
switch canUpdate, isDiscarded := t.canUpdate(); {
case isDiscarded:
return ErrDiscardedTxn
case !canUpdate:
return ErrReadOnlyTxn
}
return nil
}
func (t *Tx) ensureView() error {
if _, isDiscarded := t.canUpdate(); isDiscarded {
return ErrDiscardedTxn
}
return nil
}
func (t *Tx) Set(k Key, v Value) error {
return t.SetEntry(NewEntry(k, v))
}
func (t *Tx) SetEntry(kv Entry) error {
if err := t.container.validateEntry(kv); err != nil {
return err
}
t.mutex.Lock()
defer t.mutex.Unlock()
if err := t.ensureUpdate(); err != nil {
return err
}
if !t.canReplace() && t.contains(kv.Key) {
return ErrExistingKey
}
if t.pending == nil {
t.pending = make(map[longbits.ByteString]txEntry)
}
t.pending[kv.Key] = txEntry{kv, t.txMark}
//delete(t.deleted, kv.Key)
return nil
}
//func (t *Tx) Delete(k Key) error {
// if err := t.container.validateKey(k); err != nil {
// return err
// }
// if !t.canDelete() {
// return ErrExistingKey
// }
//
// t.mutex.Lock()
// defer t.mutex.Unlock()
//
// if err := t.ensureUpdate(); err != nil {
// return err
// }
//
// delete(t.pending, k)
//
// if _, ok := t.txMarkOf(k); ok {
// if t.deleted == nil {
// t.deleted = make(map[longbits.ByteString]struct{})
// }
// t.deleted[k] = struct{}{}
// }
// return nil
//}
func (t *Tx) GetEntry(k Key) (Entry, error) {
if err := t.container.validateKey(k); err != nil {
return Entry{}, err
}
t.mutex.RLock()
defer t.mutex.RUnlock()
if err := t.ensureView(); err != nil {
return Entry{}, err
}
if v, ok := t.pending[k]; ok {
return v.Entry, nil
}
return t.container.getAsOf(k, t.viewVersion)
}
func (t *Tx) Get(k Key) (Value, error) {
if kv, err := t.GetEntry(k); err != nil {
return nil, err
} else {
return kv.Value, nil
}
}
func (t *Tx) Contains(k Key) bool {
if err := t.container.validateKey(k); err != nil {
return false
}
t.mutex.RLock()
defer t.mutex.RUnlock()
return t.contains(k)
}
func (t *Tx) contains(k Key) bool {
if _, ok := t.pending[k]; ok {
return true
}
_, ok := t.container.markAsOf(k, t.viewVersion)
return ok
}
func (t *Tx) txMarkOf(k Key) (*txMark, bool) {
return t.container.markAsOf(k, t.viewVersion)
}
func (t *Tx) Discard() {
t.mutex.Lock()
defer t.mutex.Unlock()
_ = t.txMark.discard()
}
func (t *Tx) Commit() error {
return t.container.commitTx(t)
}
// a minimal tx portion to avoid retention of whole Tx in memory after commit
type txMark struct {
commitVersion uint64 //atomic
}
func (t *txMark) getVersion() uint64 {
return atomic.LoadUint64(&t.commitVersion)
}
func (t *txMark) getVersionNil() uint64 {
if t == nil {
return firstTxVersion
}
return t.getVersion()
}
func (t *txMark) discard() error {
for {
v := t.getVersion()
if v != pendingTxVersion {
return ErrDiscardedTxn
}
if atomic.CompareAndSwapUint64(&t.commitVersion, v, discardedTxVersion) {
return nil
}
}
}
func (t *txMark) setVersion(version uint64) bool {
switch version {
case pendingTxVersion, discardedTxVersion:
panic("illegal value")
}
return atomic.CompareAndSwapUint64(&t.commitVersion, pendingTxVersion, version)
}