-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.go
More file actions
330 lines (279 loc) · 6.45 KB
/
Copy pathprotocol.go
File metadata and controls
330 lines (279 loc) · 6.45 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package protocol
import (
"fmt"
sl "github.com/msackman/skiplist"
"log"
"sort"
"strings"
)
// Var
type Var string
func (v Var) String() string {
return string(v)
}
// Vars
type Vars []Var
func (vars Vars) Len() int { return len(vars) }
func (vars Vars) Less(i, j int) bool { return vars[i] < vars[j] }
func (vars Vars) Swap(i, j int) { vars[i], vars[j] = vars[j], vars[i] }
// VarAction
type VarAction struct {
Var
ReadVersion int
WroteValue interface{}
}
func ReadAction(v Var, ver int) *VarAction {
return &VarAction{Var: v, ReadVersion: ver}
}
func WriteAction(v Var, value interface{}) *VarAction {
return &VarAction{Var: v, WroteValue: value}
}
func (va *VarAction) IsRead() bool {
return va.WroteValue == nil
}
func (va *VarAction) IsWrite() bool {
return va.WroteValue != nil
}
func (va *VarAction) String() string {
if va.IsRead() && va.IsWrite() {
return fmt.Sprintf("r(%v)%v w(%v := %v)", va.Var, va.ReadVersion, va.Var, va.WroteValue)
} else if va.IsRead() {
return fmt.Sprintf("r(%v)%v", va.Var, va.ReadVersion)
} else if va.IsWrite() {
return fmt.Sprintf("w(%v := %v)", va.Var, va.WroteValue)
} else {
return fmt.Sprintf("noop(%v)%v", va.Var)
}
}
// VarActions
type VarActions []*VarAction
func (vas VarActions) Len() int { return len(vas) }
func (vas VarActions) Less(i, j int) bool { return vas[i].Var < vas[j].Var }
func (vas VarActions) Swap(i, j int) { vas[i], vas[j] = vas[j], vas[i] }
// Txn
type Txn struct {
ID int
Actions VarActions
VarToActions map[Var]VarActions
}
func NewTxn(id int, actions ...*VarAction) *Txn {
vas := VarActions(make([]*VarAction, len(actions)))
copy(vas, actions)
sort.Sort(vas)
vasMap := make(map[Var]VarActions, len(actions))
for _, va := range vas {
if list, found := vasMap[va.Var]; found {
vasMap[va.Var] = append(list, va)
} else {
vasMap[va.Var] = []*VarAction{va}
}
}
return &Txn{
ID: id,
Actions: vas,
VarToActions: vasMap,
}
}
type TxnContainer interface {
GetTxn() *Txn
}
func (txn *Txn) GetTxn() *Txn {
return txn
}
func (a *Txn) Compare(bC sl.Comparable) sl.Cmp {
if bC == nil {
if a == nil {
return sl.EQ
} else {
return sl.GT
}
} else {
b := bC.(TxnContainer)
switch {
case a == b || (b != nil && a == b.GetTxn()):
return sl.EQ
case a == nil:
return sl.LT
case b == nil || b.GetTxn() == nil:
return sl.GT
default:
switch bTxn := b.GetTxn(); {
case a.ID < bTxn.ID:
return sl.LT
case a.ID > bTxn.ID:
return sl.GT
default:
return sl.EQ
}
}
}
}
func (txn *Txn) String() string {
return fmt.Sprintf("Txn %v %v", txn.ID, txn.Actions)
}
func (txn *Txn) Clone() *Txn {
return NewTxn(txn.ID, txn.Actions...)
}
// Txns
type Txns []*Txn
func (txns Txns) Len() int { return len(txns) }
func (txns Txns) Less(i, j int) bool { return txns[i].ID < txns[j].ID }
func (txns Txns) Swap(i, j int) { txns[i], txns[j] = txns[j], txns[i] }
func (a Txns) Equal(b Txns) bool {
if len(a) == len(b) {
for idx := range a {
if a[idx].Compare(b[idx]) != sl.EQ {
return false
}
}
return true
} else {
return false
}
}
func (txns Txns) String() string {
strs := make([]string, len(txns))
for idx, tr := range txns {
strs[idx] = fmt.Sprintf("%v", tr)
}
return "[" + strings.Join(strs, ",\n\t") + "]\n"
}
// VarVersionValue
type VarVersionValue struct {
Var
Version int
Value interface{}
}
type VarVersionValues []*VarVersionValue
func (vvvs VarVersionValues) Len() int { return len(vvvs) }
func (vvvs VarVersionValues) Swap(i, j int) { vvvs[i], vvvs[j] = vvvs[j], vvvs[i] }
func (vvvs VarVersionValues) Less(i, j int) bool {
return vvvs[i].Var < vvvs[j].Var ||
(vvvs[i].Var == vvvs[j].Var && vvvs[i].Version < vvvs[j].Version)
}
func (vvvs VarVersionValues) Sort() { sort.Sort(vvvs) }
func (vvv *VarVersionValue) Clone() *VarVersionValue {
return &VarVersionValue{
Var: vvv.Var,
Version: vvv.Version,
Value: vvv.Value,
}
}
func (vvv *VarVersionValue) String() string {
return fmt.Sprintf("%v(%v)=%v", vvv.Var, vvv.Version, vvv.Value)
}
func (a *VarVersionValue) Equal(b *VarVersionValue) bool {
if a == b {
return true
}
return a.Var == b.Var && a.Version == b.Version && a.Value == b.Value
}
// HistoryNode
type HistoryNode struct {
CommittedTxn *Txn
Next []*HistoryNode
Previous []*HistoryNode
}
func NewHistoryNode(parent *HistoryNode, txn *Txn) *HistoryNode {
node := &HistoryNode{
CommittedTxn: txn,
}
if parent != nil {
parent.AddEdgeTo(node)
}
return node
}
func (hnA *HistoryNode) Equal(hnB *HistoryNode) bool {
if hnA == hnB {
return true
}
if hnA == nil || hnB == nil {
return false
}
if hnA.CommittedTxn.Compare(hnB.CommittedTxn) == sl.EQ && len(hnA.Next) == len(hnB.Next) {
for _, nextA := range hnA.Next {
found := false
for _, nextB := range hnB.Next {
if found = nextA.Equal(nextB); found {
break
}
}
if !found {
return false
}
}
return true
}
return false
}
func (hn *HistoryNode) AddEdgeTo(nodes ...*HistoryNode) {
for _, node := range nodes {
found := false
for _, to := range hn.Next {
if found = node == to; found {
break
}
}
if !found {
hn.Next = append(hn.Next, node)
node.Previous = append(node.Previous, hn)
}
}
}
func (hn *HistoryNode) RemoveEdgeTo(node *HistoryNode) {
found := false
for idx, n := range hn.Next {
if found = n == node; found {
hn.Next = append(hn.Next[:idx], hn.Next[idx+1:]...)
break
}
}
if !found {
return
}
for idx, n := range node.Previous {
if n == hn {
node.Previous = append(node.Previous[:idx], node.Previous[idx+1:]...)
return
}
}
}
func (hn *HistoryNode) Len() int {
if hn == nil {
return 0
}
switch len(hn.Next) {
case 0:
return 1
case 1:
return 1 + hn.Next[0].Len()
default:
log.Fatal("Len() called on non-serial history")
return -1 // doesn't matter - unreachable
}
}
func (hn *HistoryNode) String() string {
return hn.string("\n") + "\n"
}
func (hn *HistoryNode) string(nl string) string {
nextsStrs := make([]string, len(hn.Next))
for idx, next := range hn.Next {
nextsStrs[idx] = next.string(nl + " ")
}
return fmt.Sprintf("HistoryNode%v Committed Txn:%v %v%v Nexts: [%v %v]",
nl, nl, hn.CommittedTxn,
nl, nl, strings.Join(nextsStrs, ";"))
}
// Outcome
type Outcome int
const (
Commit Outcome = iota
Abort = iota
)
func (o Outcome) String() string {
if o == Commit {
return "Commited"
} else {
return "Aborted"
}
}