forked from mercari/datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
144 lines (116 loc) · 3.01 KB
/
transaction.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
package clouddatastore
import (
"context"
"errors"
"cloud.google.com/go/datastore"
w "go.mercari.io/datastore"
)
var _ w.Transaction = (*transactionImpl)(nil)
var _ w.Commit = (*commitImpl)(nil)
type contextTransaction struct{}
func getTx(ctx context.Context) *datastore.Transaction {
tx := ctx.Value(contextTransaction{})
if tx != nil {
return tx.(*datastore.Transaction)
}
return nil
}
type transactionImpl struct {
client *datastoreImpl
}
type commitImpl struct {
commit *datastore.Commit
}
func (tx *transactionImpl) Get(key w.Key, dst interface{}) error {
err := tx.GetMulti([]w.Key{key}, []interface{}{dst})
if merr, ok := err.(w.MultiError); ok {
return merr[0]
} else if err != nil {
return err
}
return nil
}
func (tx *transactionImpl) GetMulti(keys []w.Key, dst interface{}) error {
baseTx := getTx(tx.client.ctx)
if tx == nil {
return errors.New("unexpected context")
}
return getMultiOps(tx.client.ctx, keys, dst, func(keys []*datastore.Key, dst []datastore.PropertyList) error {
return baseTx.GetMulti(keys, dst)
})
}
func (tx *transactionImpl) Put(key w.Key, src interface{}) (w.PendingKey, error) {
pKeys, err := tx.PutMulti([]w.Key{key}, []interface{}{src})
if merr, ok := err.(w.MultiError); ok {
return nil, merr[0]
} else if err != nil {
return nil, err
}
return pKeys[0], nil
}
func (tx *transactionImpl) PutMulti(keys []w.Key, src interface{}) ([]w.PendingKey, error) {
baseTx := getTx(tx.client.ctx)
if tx == nil {
return nil, errors.New("unexpected context")
}
_, pKeys, err := putMultiOps(tx.client.ctx, keys, src, func(keys []*datastore.Key, src []datastore.PropertyList) ([]w.Key, []w.PendingKey, error) {
pKeys, err := baseTx.PutMulti(keys, src)
if err != nil {
return nil, nil, err
}
wPKeys := toWrapperPendingKeys(pKeys)
return nil, wPKeys, nil
})
if err != nil {
return nil, err
}
return pKeys, nil
}
func (tx *transactionImpl) Delete(key w.Key) error {
err := tx.DeleteMulti([]w.Key{key})
if merr, ok := err.(w.MultiError); ok {
return merr[0]
} else if err != nil {
return err
}
return nil
}
func (tx *transactionImpl) DeleteMulti(keys []w.Key) error {
baseTx := getTx(tx.client.ctx)
if tx == nil {
return errors.New("unexpected context")
}
return deleteMultiOps(tx.client.ctx, keys, func(keys []*datastore.Key) error {
return baseTx.DeleteMulti(keys)
})
}
func (tx *transactionImpl) Commit() (w.Commit, error) {
baseTx := getTx(tx.client.ctx)
if tx == nil {
return nil, nil
}
commit, err := baseTx.Commit()
if err != nil {
return nil, toWrapperError(err)
}
return &commitImpl{commit}, nil
}
func (tx *transactionImpl) Rollback() error {
baseTx := getTx(tx.client.ctx)
if tx == nil {
return nil
}
err := baseTx.Rollback()
if err != nil {
return toWrapperError(err)
}
return nil
}
func (tx *transactionImpl) Batch() *w.TransactionBatch {
return &w.TransactionBatch{Transaction: tx}
}
func (c *commitImpl) Key(p w.PendingKey) w.Key {
pk := toOriginalPendingKey(p)
key := c.commit.Key(pk)
return toWrapperKey(key)
}