-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexecute.go
939 lines (881 loc) · 20.9 KB
/
execute.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
package ddtxn
import (
"flag"
"log"
"math/rand"
"github.com/narula/dlog"
)
var SampleRate = flag.Int64("sr", 500, "Sample every sr transactions\n")
var AlwaysSplit = flag.Bool("split", false, "Split every piece of data\n")
var NoConflictType = flag.Int("noconflict", -1, "Type of operation NOT to record conflicts on")
// Phases
const (
SPLIT = iota
MERGE
JOIN
)
// TODO: Handle writing more than once to a key in one transaction
type WriteKey struct {
key Key
br *BRecord
v Value
op KeyType
locked bool
vint32 int32
ve Entry
}
type ReadKey struct {
key Key
br *BRecord
last uint64
}
type ETransaction interface {
Reset()
Read(k Key) (*BRecord, error)
WriteInt32(k Key, a int32, op KeyType) error
WriteList(k Key, l Entry, op KeyType) error
WriteOO(k Key, a int32, v Value, op KeyType) error
Write(k Key, v Value, op KeyType)
Abort() TID
Commit() TID
SetPhase(int)
GetPhase() int
Store() *Store
Worker() *Worker
// Tell 2PL I am going to read and potentially write this key.
// This is because I don't know how to upgrade locks.
MaybeWrite(k Key)
// Tell Doppel not to count this transaction's reads and writes
// when deciding if records should be split.
NoCount()
// Get a unique key; give it up
UID(rune) uint64
RelinquishKey(uint64, rune)
}
// Tracks execution of transaction.
type OTransaction struct {
padding0 [128]byte
read []ReadKey
w *Worker
s *Store
ls *LocalStore
phase int
writes []WriteKey
maxSeen uint64
t int64 // Used just as a rough count
count bool
sr_rate int64
dummyRecord *BRecord
padding [128]byte
}
func (tx *OTransaction) UID(f rune) uint64 {
return tx.w.NextKey(f)
}
func (tx *OTransaction) RelinquishKey(n uint64, r rune) {
tx.w.GiveBack(n, r)
}
func (tx *OTransaction) NoCount() {
tx.count = false
}
func StartOTransaction(w *Worker) *OTransaction {
tx := &OTransaction{
read: make([]ReadKey, 0, 100),
writes: make([]WriteKey, 0, 100),
w: w,
s: w.store,
ls: w.local_store,
sr_rate: int64(w.ID),
dummyRecord: &BRecord{},
}
return tx
}
func (tx *OTransaction) Reset() {
tx.read = tx.read[:0]
tx.writes = tx.writes[:0]
tx.t++
tx.count = (*SysType == DOPPEL && tx.sr_rate == 0)
if tx.count {
tx.w.Nstats[NSAMPLES]++
tx.sr_rate = *SampleRate + int64(rand.Intn(100)) - int64(tx.w.ID)
} else {
tx.sr_rate--
}
}
func (tx *OTransaction) isSplit(br *BRecord) bool {
if *SysType == DOPPEL {
if tx.phase == SPLIT {
if *AlwaysSplit {
return true
}
if tx.s.any_dd {
if br != nil {
if br.dd {
return true
}
}
}
}
}
return false
}
func (tx *OTransaction) Read(k Key) (*BRecord, error) {
if len(tx.writes) > 0 {
for i := 0; i < len(tx.writes); i++ {
w := &tx.writes[i]
if w.key == k {
// I wrote and read the same piece of data in one
// transaction. This is a strong signal this
// shouldn't be dd. Also I should return this value.
// But I return a pointer to a record (sigh) so use a
// dummy record.
if tx.count {
tx.ls.candidates.ReadWrite(k, w.br)
}
if tx.isSplit(w.br) {
return nil, ESTASH
}
tx.dummyRecord.key_type = w.op
tx.dummyRecord.int_value = w.vint32
tx.dummyRecord.value = w.v
if w.op == LIST {
tx.dummyRecord.entries = tx.dummyRecord.entries[0 : len(w.br.entries)+1]
copy(tx.dummyRecord.entries, w.br.entries)
tx.dummyRecord.entries = append(tx.dummyRecord.entries, w.ve)
}
return tx.dummyRecord, nil
}
}
}
br, err := tx.s.getKey(k, tx.w.ld)
if *CountKeys {
p, r := UndoCKey(k)
if r == 'm' {
tx.w.NKeyAccesses[p]++
}
}
if err == ENOKEY {
// Can't be stashed, right?
n := len(tx.read)
tx.read = tx.read[0 : n+1]
tx.read[n].key = k
tx.read[n].br = nil
tx.read[n].last = 0
return nil, err
} else {
if tx.isSplit(br) {
if tx.count {
tx.ls.candidates.Stash(k)
}
return nil, ESTASH
}
if tx.count {
tx.ls.candidates.Read(k, br)
}
ok, last := br.IsUnlocked()
// if locked abort
// else note the last timestamp, save it, return value
if !ok {
tx.w.Nstats[NLOCKED]++
return nil, EABORT
}
n := len(tx.read)
tx.read = tx.read[0 : n+1]
tx.read[n].key = k
tx.read[n].br = br
tx.read[n].last = last
if last > tx.maxSeen {
tx.maxSeen = last
}
return br, nil
}
log.Fatalf("What")
return nil, nil
}
func (tx *OTransaction) WriteInt32(k Key, a int32, op KeyType) error {
// During the normal phase, Doppel operates just like OCC, for
// ease of exposition. That means it would have to put the key
// into the read set and potentially abort accordingly. Doing so
// here, but not using the value until commit time.
br, err := tx.s.getKey(k, tx.w.ld)
if *CountKeys {
p, r := UndoCKey(k)
if r == 'm' {
tx.w.NKeyAccesses[p]++
}
}
if tx.isSplit(br) {
if tx.count {
tx.ls.candidates.Write(k, br, op)
}
if br.key_type != op {
log.Fatalf("%v Doing a write to a non-%v type: %v", k, op, br.key_type)
}
// Do not need to read-validate
} else {
var last uint64
if br == nil || err == ENOKEY {
last = 0
} else {
var ok bool
ok, last = br.IsUnlocked()
if !ok {
tx.w.Nstats[NLOCKED]++
if tx.count && KeyType(*NoConflictType) != op {
tx.ls.candidates.Conflict(k, br, op)
}
return EABORT
}
}
// Note the last timestamp and save it
n := len(tx.read)
tx.read = tx.read[0 : n+1]
tx.read[n].key = k
tx.read[n].br = br
tx.read[n].last = last
if last > tx.maxSeen {
tx.maxSeen = last
}
}
n := len(tx.writes)
tx.writes = tx.writes[0 : n+1]
tx.writes[n].key = k
tx.writes[n].br = br
tx.writes[n].vint32 = a
tx.writes[n].op = op
tx.writes[n].locked = false
return nil
}
func (tx *OTransaction) Write(k Key, v Value, op KeyType) {
if len(tx.writes) == cap(tx.writes) {
log.Fatalf("Ran out of room\n")
}
n := len(tx.writes)
tx.writes = tx.writes[0 : n+1]
tx.writes[n].key = k
tx.writes[n].br = nil
tx.writes[n].v = v
tx.writes[n].op = op
tx.writes[n].locked = false
}
func (tx *OTransaction) WriteList(k Key, l Entry, op KeyType) error {
if op != LIST {
log.Fatalf("Not a list\n")
}
if len(tx.writes) == cap(tx.writes) {
log.Fatalf("Ran out of room\n")
}
// During the normal phase, Doppel operates just like OCC, for
// ease of exposition. That means it would have to put the key
// into the read set and potentially abort accordingly. Doing so
// here, but not using the value until commit time.
br, err := tx.s.getKey(k, tx.w.ld)
if *CountKeys {
p, r := UndoCKey(k)
if r == 'm' {
tx.w.NKeyAccesses[p]++
}
}
if tx.isSplit(br) {
if tx.count {
tx.ls.candidates.Write(k, br, op)
}
if br.key_type != LIST {
log.Fatalf("%v Doing a write to a non-LIST type: %v", k, br.key_type)
}
// Do not need to read-validate
} else {
var last uint64
if br == nil || err == ENOKEY {
last = 0
} else {
var ok bool
ok, last = br.IsUnlocked()
if !ok {
tx.w.Nstats[NLOCKED]++
if tx.count && KeyType(*NoConflictType) != LIST {
tx.ls.candidates.Conflict(k, br, LIST)
}
return EABORT
}
}
// Note the last timestamp and save it
n := len(tx.read)
tx.read = tx.read[0 : n+1]
tx.read[n].key = k
tx.read[n].br = br
tx.read[n].last = last
if last > tx.maxSeen {
tx.maxSeen = last
}
}
n := len(tx.writes)
tx.writes = tx.writes[0 : n+1]
tx.writes[n].key = k
tx.writes[n].br = br
tx.writes[n].ve = l
tx.writes[n].op = op
tx.writes[n].locked = false
return nil
}
func (tx *OTransaction) WriteOO(k Key, a int32, v Value, op KeyType) error {
if op != OOWRITE {
log.Fatalf("Not an OOWRITE\n")
}
if len(tx.writes) == cap(tx.writes) {
log.Fatalf("Ran out of room\n")
}
// During the normal phase, Doppel operates just like OCC, for
// ease of exposition. That means it would have to put the key
// into the read set and potentially abort accordingly. Doing so
// here, but not using the value until commit time.
br, err := tx.s.getKey(k, tx.w.ld)
if *CountKeys {
p, r := UndoCKey(k)
if r == 'm' {
tx.w.NKeyAccesses[p]++
}
}
if tx.isSplit(br) {
if tx.count {
tx.ls.candidates.Write(k, br, op)
}
if br.key_type != OOWRITE {
log.Fatalf("%v Doing a write to a non-OOWRITE type: %v", k, br.key_type)
}
// Do not need to read-validate
} else {
var last uint64
if br == nil || err == ENOKEY {
last = 0
} else {
var ok bool
ok, last = br.IsUnlocked()
if !ok {
tx.w.Nstats[NLOCKED]++
if tx.count && KeyType(*NoConflictType) != OOWRITE {
tx.ls.candidates.Conflict(k, br, OOWRITE)
}
return EABORT
}
}
// Note the last timestamp and save it
n := len(tx.read)
tx.read = tx.read[0 : n+1]
tx.read[n].key = k
tx.read[n].br = br
tx.read[n].last = last
if last > tx.maxSeen {
tx.maxSeen = last
}
}
n := len(tx.writes)
tx.writes = tx.writes[0 : n+1]
tx.writes[n].key = k
tx.writes[n].br = nil
tx.writes[n].v = v
tx.writes[n].vint32 = a
tx.writes[n].op = op
tx.writes[n].locked = false
return nil
}
func (tx *OTransaction) SetPhase(p int) {
tx.phase = p
}
func (tx *OTransaction) GetPhase() int {
return tx.phase
}
func (tx *OTransaction) Store() *Store {
return tx.s
}
func (tx *OTransaction) Worker() *Worker {
return tx.w
}
func (tx *OTransaction) Abort() TID {
for i, _ := range tx.writes {
if tx.writes[i].locked {
tx.writes[i].br.Unlock(0)
}
}
return 0
}
func (tx *OTransaction) checkOwnership(br *BRecord, last uint64) bool {
for j, _ := range tx.writes {
if tx.writes[j].key == br.key && tx.writes[j].locked {
if br.Own(last) {
return true
}
}
}
return false
}
func (tx *OTransaction) Commit() TID {
// for each write key
// if global get from global store and lock
for i, _ := range tx.writes {
w := &tx.writes[i]
if w.br == nil {
var err error
w.br, err = tx.s.getKey(w.key, tx.w.ld)
if *CountKeys {
p, r := UndoCKey(w.key)
if r == 'm' {
tx.w.NKeyAccesses[p]++
}
}
// Data doesn't exist, create it
if w.br == nil || err == ENOKEY {
if w.br == nil {
dlog.Printf("w.br was nil %v\n", w.key)
} else if err == ENOKEY {
dlog.Printf("err==ENOKEY %v\n", w.key)
}
var err2 error
w.br, err2 = tx.s.CreateLockedKey(w.key, w.op)
if err2 != nil {
// Someone snuck in and created the key
if tx.count && w.op != KeyType(*NoConflictType) {
tx.ls.candidates.Conflict(w.key, w.br, w.op)
}
tx.w.Nstats[NFAIL_VERIFY]++
//dlog.Printf("Fail verify key %v\n", w.key)
return tx.Abort()
}
w.locked = true
continue
}
}
if tx.isSplit(w.br) {
continue
}
// Check last TID
var former uint64
var ok bool
if ok, former = w.br.Lock(); !ok {
tx.w.Nstats[NO_LOCK]++
if tx.count && w.op != KeyType(*NoConflictType) {
tx.ls.candidates.Conflict(w.key, w.br, w.op)
}
return tx.Abort()
}
w.locked = true
if former > tx.maxSeen {
tx.maxSeen = former
}
}
// Get TID higher than anything I've seen
tid := tx.w.commitTID()
if uint64(tid) < tx.maxSeen {
tx.w.resetTID(tx.maxSeen)
tid = tx.w.commitTID()
if uint64(tid) < tx.maxSeen {
log.Fatalf("%v MaxSeen %v, reset TID but %v<%v", tx.w.ID, tx.maxSeen, tid, tx.maxSeen)
}
}
// for each read key
// verify
for i, _ := range tx.read {
rk := &tx.read[i]
var err error
if rk.br == nil {
rk.br, err = tx.s.getKey(rk.key, tx.w.ld)
if *CountKeys {
p, r := UndoCKey(rk.key)
if r == 'm' {
tx.w.NKeyAccesses[p]++
}
}
// Verify it still doesn't exist or I'm the one who
// created and locked it to write
if err == ENOKEY || tx.checkOwnership(rk.br, rk.last) {
continue
}
tx.w.Nstats[NFAIL_VERIFY]++
return tx.Abort()
}
if rk.br.Verify(rk.last) {
continue
}
// It didn't verify, but I might own it because I wrote it
if tx.checkOwnership(rk.br, rk.last) {
continue
}
tx.w.Nstats[NFAIL_VERIFY]++
return tx.Abort()
}
// for each write key
// if dd and split phase, apply locally
// else apply globally and unlock
for i, _ := range tx.writes {
w := &tx.writes[i]
if tx.isSplit(w.br) {
switch w.op {
case SUM:
tx.ls.ApplyInt32(w.key, w.op, w.vint32, w.op)
case MAX:
tx.ls.ApplyInt32(w.key, w.op, w.vint32, w.op)
case LIST:
tx.ls.ApplyList(w.key, w.ve)
case OOWRITE:
tx.ls.ApplyOO(w.key, w.vint32, w.v)
default:
tx.ls.Apply(w.key, w.op, w.v, w.op)
}
} else {
switch w.op {
case SUM:
tx.s.SetInt32(w.br, w.vint32, w.op)
case MAX:
tx.s.SetInt32(w.br, w.vint32, w.op)
case LIST:
tx.s.SetList(w.br, w.ve, w.op)
case OOWRITE:
tx.s.SetOO(w.br, w.vint32, w.v, w.op)
default:
if w.br == nil {
log.Fatalf("How is this nil?\n")
}
tx.s.Set(w.br, w.v, w.op)
}
w.br.Unlock(tid)
}
}
return tid
}
func (tx *OTransaction) MaybeWrite(k Key) {
// no op
}
type Rec struct {
br *BRecord
read bool
v interface{}
vint32 int32
ve Entry
op KeyType
noset bool
key Key
}
// Not threadsafe. Tracks execution of transaction.
type LTransaction struct {
padding0 [128]byte
keys []Rec
w *Worker
s *Store
t int64 // Used just as a rough count
ls *LocalStore
phase int
dummyRecord *BRecord
padding [128]byte
}
func StartLTransaction(w *Worker) *LTransaction {
tx := <ransaction{
keys: make([]Rec, 0, 100),
w: w,
s: w.store,
ls: w.local_store,
dummyRecord: &BRecord{},
}
return tx
}
func (tx *LTransaction) Reset() {
tx.keys = tx.keys[:0]
tx.t++
}
func (tx *LTransaction) UID(f rune) uint64 {
return tx.w.NextKey(f)
}
func (tx *LTransaction) RelinquishKey(n uint64, r rune) {
tx.w.GiveBack(n, r)
}
func (tx *LTransaction) Read(k Key) (*BRecord, error) {
if exists, n := tx.already_exists(k); exists {
if tx.keys[n].noset == true && tx.keys[n].br.exists {
// MaybeWrite(); if the key exists.
return tx.keys[n].br, nil
}
if tx.keys[n].noset == true && tx.keys[n].br.exists == false {
// Doesn't really exist yet; created to lock for read or MaybeWrite()
return nil, ENOKEY
}
if tx.keys[n].br.exists && tx.keys[n].noset == false && tx.keys[n].read == false {
tx.dummyRecord.key_type = tx.keys[n].op
tx.dummyRecord.int_value = tx.keys[n].vint32
tx.dummyRecord.value = tx.keys[n].v
dlog.Printf("Creating dummy record for key %v %v %v %v\n", k, tx.dummyRecord.key_type, tx.dummyRecord.int_value, tx.dummyRecord.value)
if tx.keys[n].op == LIST {
tx.dummyRecord.entries = tx.dummyRecord.entries[0 : len(tx.keys[n].br.entries)+1]
copy(tx.dummyRecord.entries, tx.keys[n].br.entries)
tx.dummyRecord.entries = append(tx.dummyRecord.entries, tx.keys[n].ve)
}
return tx.dummyRecord, nil
}
if tx.keys[n].br.exists && tx.keys[n].noset == false && tx.keys[n].read == true {
return tx.keys[n].br, nil
}
dlog.Printf("Returning ENOKEY for key %v supposedly at slot %v. %v\n", k, n, tx.keys[n])
return nil, ENOKEY
}
br, err := tx.s.getKey(k, tx.w.ld)
if *CountKeys {
p, r := UndoCKey(k)
if r == 'm' {
tx.w.NKeyAccesses[p]++
}
}
n := len(tx.keys)
tx.keys = tx.keys[0 : n+1]
tx.keys[n].read = true
tx.keys[n].noset = false
tx.keys[n].key = k
tx.keys[n].br = br
if err == nil {
br.SRLock()
return br, nil
}
if br, err = tx.s.CreateMuLockedKey(k, WRITE); err == nil {
tx.keys[n].noset = true
tx.keys[n].read = false
tx.keys[n].br = br
br.exists = false
return nil, ENOKEY
}
// Perhaps someone snuck in and created this key already.
if br, err = tx.s.getKey(k, tx.w.ld); err == nil {
br.SRLock()
tx.keys[n].br = br
return br, nil
}
log.Fatalf("Can't create key %v and it's not there now\n", k)
return br, nil
}
// This is when I am reading a key and I might write it later; acquire
// the write lock *before* the read.
func (tx *LTransaction) MaybeWrite(k Key) {
if exists, _ := tx.already_exists(k); exists {
log.Fatalf("Shouldn't already have a lock on this\n")
}
br, err := tx.s.getKey(k, tx.w.ld)
if *CountKeys {
p, r := UndoCKey(k)
if r == 'm' {
tx.w.NKeyAccesses[p]++
}
}
if br == nil || err != nil {
if br, err = tx.s.CreateMuLockedKey(k, WRITE); err != nil {
// Perhaps someone snuck in and created this key already.
if br, err = tx.s.getKey(k, tx.w.ld); err != nil {
log.Fatalf("Can't create key %v and it's not there now\n", k)
}
br.SLock()
} // Created and Locked
br.exists = false
} else {
br.SLock()
}
n := len(tx.keys)
tx.keys = tx.keys[0 : n+1]
tx.keys[n].br = br
tx.keys[n].read = false
tx.keys[n].noset = true
tx.keys[n].key = k
}
func (tx *LTransaction) already_exists(k Key) (bool, int) {
n := len(tx.keys)
for i := 0; i < len(tx.keys); i++ {
if tx.keys[i].key == k {
return true, i
}
}
if len(tx.keys) == cap(tx.keys) {
// TODO: extend
log.Fatalf("Ran out of room\n")
}
return false, n
}
func (tx *LTransaction) make_or_get_key(k Key, op KeyType) *BRecord {
br, err := tx.s.getKey(k, tx.w.ld)
if *CountKeys {
p, r := UndoCKey(k)
if r == 'm' {
tx.w.NKeyAccesses[p]++
}
}
if br != nil && err == nil {
br.SLock()
return br
}
var err2 error
br, err2 = tx.s.CreateMuLockedKey(k, op)
if *CountKeys {
p, r := UndoCKey(k)
if r == 'm' {
tx.w.NKeyAccesses[p]++
}
}
if br == nil || err2 != nil {
br, err = tx.s.getKey(k, tx.w.ld)
if err != nil {
log.Fatalf("Should exist\n")
}
br.SLock()
}
return br
}
func (tx *LTransaction) WriteInt32(k Key, a int32, op KeyType) error {
exists, n := tx.already_exists(k)
if exists {
if tx.keys[n].read == true {
log.Fatalf("Already have read lock on this key; cannot upgrade %v\n", k)
}
// Already locked. TODO: aggregate
tx.keys[n].vint32 = a
tx.keys[n].op = op
tx.keys[n].noset = false
tx.keys[n].key = k
return nil
}
br := tx.make_or_get_key(k, op)
tx.keys = tx.keys[0 : n+1]
tx.keys[n].br = br
tx.keys[n].read = false
tx.keys[n].vint32 = a
tx.keys[n].op = op
tx.keys[n].noset = false
tx.keys[n].key = k
return nil
}
func (tx *LTransaction) Write(k Key, v Value, op KeyType) {
if op == SUM || op == MAX {
tx.WriteInt32(k, v.(int32), op)
return
}
exists, n := tx.already_exists(k)
if exists {
if tx.keys[n].read == true {
log.Fatalf("Already have read lock on this key; cannot upgrade %v\n", k)
}
// Already locked.
tx.keys[n].v = v
tx.keys[n].op = op
tx.keys[n].key = k
return
}
br := tx.make_or_get_key(k, op)
tx.keys = tx.keys[0 : n+1]
tx.keys[n].br = br
tx.keys[n].read = false
tx.keys[n].v = v
tx.keys[n].op = op
tx.keys[n].noset = false
tx.keys[n].key = k
}
func (tx *LTransaction) WriteList(k Key, l Entry, op KeyType) error {
if op != LIST {
log.Fatalf("Not a list\n")
}
exists, n := tx.already_exists(k)
if exists {
if tx.keys[n].read == true {
log.Fatalf("Already have read lock on this key; cannot upgrade %v\n", k)
}
// Already locked. TODO: append
tx.keys[n].ve = l
tx.keys[n].op = op
tx.keys[n].key = k
return nil
}
br := tx.make_or_get_key(k, op)
tx.keys = tx.keys[0 : n+1]
tx.keys[n].br = br
tx.keys[n].read = false
tx.keys[n].ve = l
tx.keys[n].op = op
tx.keys[n].noset = false
tx.keys[n].key = k
return nil
}
func (tx *LTransaction) WriteOO(k Key, a int32, v Value, op KeyType) error {
if op != OOWRITE {
log.Fatalf("Not overwrite \n")
}
exists, n := tx.already_exists(k)
if exists {
if tx.keys[n].read == true {
log.Fatalf("Already have read lock on this key; cannot upgrade %v\n", k)
}
// Already locked.
if a > tx.keys[n].vint32 {
tx.keys[n].v = v
tx.keys[n].vint32 = a
tx.keys[n].op = op
tx.keys[n].key = k
}
return nil
}
br := tx.make_or_get_key(k, op)
tx.keys = tx.keys[0 : n+1]
tx.keys[n].br = br
tx.keys[n].read = false
tx.keys[n].vint32 = a
tx.keys[n].v = v
tx.keys[n].op = op
tx.keys[n].noset = false
tx.keys[n].key = k
return nil
}
func (tx *LTransaction) SetPhase(p int) {
tx.phase = p
}
func (tx *LTransaction) GetPhase() int {
return tx.phase
}
func (tx *LTransaction) Store() *Store {
return tx.s
}
func (tx *LTransaction) Worker() *Worker {
return tx.w
}
func (tx *LTransaction) Abort() TID {
for i := len(tx.keys) - 1; i >= 0; i-- {
if tx.keys[i].read {
tx.keys[i].br.SRUnlock()
} else {
tx.keys[i].br.SUnlock()
}
}
return 0
}
func (tx *LTransaction) Commit() TID {
tid := tx.w.commitTID()
for i := len(tx.keys) - 1; i >= 0; i-- {
// Apply and unlock
if tx.keys[i].read == false {
if tx.keys[i].noset {
// No changes, we write-locked it because we thought
// we *might* write
tx.keys[i].br.SUnlock()
continue
}
switch tx.keys[i].op {
case SUM:
tx.s.SetInt32(tx.keys[i].br, tx.keys[i].vint32, tx.keys[i].op)
case MAX:
tx.s.SetInt32(tx.keys[i].br, tx.keys[i].vint32, tx.keys[i].op)
case LIST:
tx.s.SetList(tx.keys[i].br, tx.keys[i].ve, tx.keys[i].op)
case OOWRITE:
tx.s.SetOO(tx.keys[i].br, tx.keys[i].vint32, tx.keys[i].v, tx.keys[i].op)
default:
tx.s.Set(tx.keys[i].br, tx.keys[i].v, tx.keys[i].op)
}
tx.keys[i].br.SUnlock()
} else {
//fmt.Printf("k: %v\n", tx.keys[i].br.key)
tx.keys[i].br.SRUnlock()
}
}
return tid
}
func (tx *LTransaction) NoCount() {
// noop
}