-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwtxn.go
115 lines (100 loc) · 2.6 KB
/
rwtxn.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
// package sdk defined spanner sdk for tikv transaction
package sdk
type rwTxn struct {
transaction
}
func beginRWTxn() (*rwTxn, error) {
return nil, nil
}
func (txn *rwTxn) Get(key []byte) ([]byte, error) {
lg.Debug("call get in rw transection")
// get TS if not exist, and start heartbeat with transaction nodes
if err := txn.setTxnIDNX(); err != nil {
return nil, err
}
// locate key from PD
_, rleader, err := pdClient.GetRegion(txn.ctx, key)
if err != nil {
return nil, err
}
linfo, err := pdClient.GetStore(txn.ctx, rleader.GetId())
if err != nil {
return nil, err
}
// call tikv get RO/RW with TS
kvcli, err := NewTiKV(linfo.GetAddress())
if err != nil {
return nil, err
}
txn.pm.AddNode(kvcli, rleader.GetId())
v, err := kvcli.Get(txn.ctx, txn.txnid, key, txn.txnid, false)
if err != nil {
return nil, err
}
txn.store.SetOp(key, v, OpRead)
return v, nil
}
func (txn *rwTxn) Set(key, value []byte) error {
lg.Debug("call set in rw transection")
// get TS if not exist, and start heartbeat with transaction nodes
if err := txn.setTxnIDNX(); err != nil {
return err
}
// locate key from PD
_, rleader, err := pdClient.GetRegion(txn.ctx, key)
if err != nil {
return err
}
linfo, err := pdClient.GetStore(txn.ctx, rleader.GetId())
if err != nil {
return err
}
// call tikv get RO/RW with TS
kvcli, err := NewTiKV(linfo.GetAddress())
if err != nil {
return err
}
txn.pm.AddNode(rleader.GetId(), kvcli)
txn.store.SetOp(key, value, OpPut)
return nil
}
func (txn *rwTxn) Delete(key []byte) error {
lg.Debug("call set in rw transection")
// get TS if not exist, and start heartbeat with transaction nodes
if err := txn.setTxnIDNX(); err != nil {
return err
}
// locate key from PD
_, rleader, err := pdClient.GetRegion(txn.ctx, key)
if err != nil {
return err
}
linfo, err := pdClient.GetStore(txn.ctx, rleader.GetId())
if err != nil {
return err
}
// call tikv get RO/RW with TS
kvcli, err := NewTiKV(linfo.GetAddress())
if err != nil {
return err
}
txn.pm.AddNode(rleader.GetId(), kvcli)
txn.store.SetOp(key, nil, OpDelete)
return nil
}
func (txn *rwTxn) Rollback() error {
return ErrNotImplemented
}
func (txn *rwTxn) Commit() error {
lg.Debug("call commit in rw transection")
if err := txn.setTxnIDNX(); err != nil {
return err
}
kvclis, kvids := txn.pm.AllNodes()
//Commit(ctx context.Context, txnID uint64, operations []Operation, coordinatorId []byte, participants [][]byte)
_, err := kvclis[0].Commit(txn.ctx, txn.txnid, txn.store.GetAllOperations(), kvids[0], kvids[1:])
return err
}
func (txn *rwTxn) Close() error {
return ErrNotImplemented
}