-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtransactions.go
256 lines (235 loc) · 4.61 KB
/
transactions.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
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
package ddtxn
import (
"flag"
"log"
"sync/atomic"
"time"
)
// I tried keeping a slice of interfaces; the reflection was costly.
// Hard code in random parameter types to re-use for now.
type Query struct {
TXN int
W chan struct {
R *Result
E error
}
T TID
K1 Key
K2 Key
A int32
U1 uint64
U2 uint64
U3 uint64
U4 uint64
U5 uint64
U6 uint64
U7 uint64
S1 string
S2 string
I int
TS time.Time
S time.Time
}
type Result struct {
V Value
}
var Allocate = flag.Bool("allocate", true, "Allocate results")
func IsRead(t int) bool {
if t == D_READ_ONE || t == D_READ_TWO {
return true
}
return false
}
func BuyTxn(t Query, tx ETransaction) (*Result, error) {
var r *Result = nil
err := tx.WriteInt32(t.K1, 1, SUM)
if err != nil {
return nil, err
}
err = tx.WriteInt32(t.K2, t.A, SUM)
if err != nil {
return nil, err
}
if tx.Commit() == 0 {
return r, EABORT
}
return r, nil
}
func BuyAndReadTxn(t Query, tx ETransaction) (*Result, error) {
tx.NoCount()
var r *Result = nil
err := tx.WriteInt32(t.K1, 1, SUM)
if err != nil {
return nil, err
}
err = tx.WriteInt32(t.K2, t.A, SUM)
if err != nil {
return nil, err
}
br, err2 := tx.Read(t.K2)
if err2 != nil {
return r, err2
}
x := br.int_value
if br.dd == true && tx.GetPhase() == SPLIT {
log.Fatalf("should not happen %v\n", t.K2)
}
if tx.Commit() == 0 {
return r, EABORT
}
if *Allocate {
r = &Result{x}
}
return r, nil
}
func ReadOneTxn(t Query, tx ETransaction) (*Result, error) {
var r *Result = nil
v1, err := tx.Read(t.K1)
if err != nil {
return r, err
}
x := v1.int_value
_ = x
if txid := tx.Commit(); txid == 0 {
return r, EABORT
}
if *Allocate {
r = &Result{x}
}
return r, nil
}
func ReadTxn(t Query, tx ETransaction) (*Result, error) {
var r *Result = nil
v1, err := tx.Read(t.K1)
if err != nil {
return r, err
}
x := v1.int_value
_ = x
v1, err = tx.Read(t.K2)
if err != nil {
return r, err
}
y := v1.int_value
_ = y
if txid := tx.Commit(); txid == 0 {
return r, EABORT
}
if *Allocate {
r = &Result{&struct {
val1 int32
val2 int32
}{x, y}}
}
return r, nil
}
// This is special. It does not use the Commit() protocol, instead it
// just performs atomic increments on keys. It is impossible to
// abort, and no stats are kept to indicate this key should be in
// split phase or not. This shouldn't be run in a mix with any other
// transaction types.
func AtomicIncr(t Query, tx ETransaction) (*Result, error) {
br, err := tx.Store().getKey(t.K1, tx.Worker().ld)
if err != nil || br == nil {
log.Fatalf("Why no key?")
}
atomic.AddInt32(&br.int_value, 1)
return nil, nil
}
func IncrTxn(t Query, tx ETransaction) (*Result, error) {
err := tx.WriteInt32(t.K1, 1, SUM)
if err != nil {
return nil, err
}
if tx.Commit() == 0 {
return nil, EABORT
}
return nil, nil
}
func BigIncrTxn(t Query, tx ETransaction) (*Result, error) {
var r *Result = nil
key := [6]Key{}
key[0] = BidKey(t.U1)
key[1] = BidKey(t.U2)
key[2] = BidKey(t.U3)
key[3] = BidKey(t.U4)
key[4] = BidKey(t.U5)
key[5] = BidKey(t.U6)
for z := 0; z < 10; z++ {
for i := 0; i < 6; i++ {
tx.MaybeWrite(key[i])
k, err := tx.Read(key[i])
if err == ESTASH {
return nil, ESTASH
}
if err == ENOKEY {
if err := tx.WriteInt32(key[i], int32(0), SUM); err != nil {
return nil, err
}
} else if err != nil {
return r, EABORT
} else {
_ = k
}
}
}
if err := tx.WriteInt32(ProductKey(int(t.U7)), 1, SUM); err != nil {
return nil, err
}
if tx.Commit() == 0 {
return r, EABORT
}
return r, nil
}
// Version of Big that puts keys in read set (doesn't rely on
// commutativity)
func BigRWTxn(t Query, tx ETransaction) (*Result, error) {
var r *Result = nil
key := [7]Key{}
key[0] = BidKey(t.U1)
key[1] = BidKey(t.U2)
key[2] = BidKey(t.U3)
key[3] = BidKey(t.U4)
key[4] = BidKey(t.U5)
key[5] = BidKey(t.U6)
key[6] = ProductKey(int(t.U7))
for z := 0; z < 10; z++ {
for i := 0; i < 6; i++ {
tx.MaybeWrite(key[i])
k, err := tx.Read(key[i])
if err == ESTASH {
return nil, ESTASH
}
if err == ENOKEY {
if err := tx.WriteInt32(key[i], int32(0), SUM); err != nil {
return nil, err
}
} else if err != nil {
return r, EABORT
} else {
_ = k
}
}
}
tx.MaybeWrite(key[6])
k, err := tx.Read(key[6])
if err == ESTASH {
return nil, ESTASH
}
if err == ENOKEY {
if err := tx.WriteInt32(key[6], int32(0), SUM); err != nil {
return nil, err
}
} else if err != nil {
return r, EABORT
} else {
_ = k
}
if err := tx.WriteInt32(key[6], 1, SUM); err != nil {
return nil, err
}
if tx.Commit() == 0 {
return r, EABORT
}
return r, nil
}