-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
245 lines (223 loc) · 7 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
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
package dalgo2datastore
import (
"cloud.google.com/go/datastore"
"context"
"fmt"
"github.com/dal-go/dalgo/dal"
"github.com/strongo/log"
)
func (db database) RunReadonlyTransaction(ctx context.Context, f dal.ROTxWorker, options ...dal.TransactionOption) error {
_, err := db.runInTransaction(ctx, append(options, dal.TxWithReadonly()), func(tx transaction) error {
return f(ctx, tx)
})
if err != nil {
return err
}
return nil
}
func (db database) RunReadwriteTransaction(ctx context.Context, f dal.RWTxWorker, options ...dal.TransactionOption) error {
_, err := db.runInTransaction(ctx, options, func(tx transaction) error {
return f(ctx, tx)
})
return err
}
func (db database) runInTransaction(c context.Context, opts []dal.TransactionOption, f func(tx transaction) error) (cmt *datastore.Commit, err error) {
var tx transaction
tx.db = db
tx.QueryExecutor = db.QueryExecutor
tx.dalgoTxOptions = dal.NewTransactionOptions(opts...)
var dsTxOptions []datastore.TransactionOption
//tx.datastoreTxOptions.XG = tx.dalgoTxOptions.IsCrossGroup()
if tx.dalgoTxOptions.IsReadonly() {
dsTxOptions = append(dsTxOptions, datastore.ReadOnly)
}
if tx.dalgoTxOptions.IsCrossGroup() {
dsTxOptions = append(dsTxOptions, datastore.MaxAttempts(tx.dalgoTxOptions.Attempts()))
}
return db.client.RunInTransaction(c, func(datastoreTx *datastore.Transaction) error {
tx.datastoreTx = datastoreTx
if err := f(tx); err != nil {
return err
}
//if _, err := datastoreTx.Commit(); err != nil {
// return err
//}
return nil
}, dsTxOptions...)
}
var _ dal.Transaction = (*transaction)(nil)
var _ dal.ReadwriteTransaction = (*transaction)(nil)
type partialKey struct {
dalgo *dal.Key
pending *datastore.PendingKey
}
type transaction struct {
db database
dalgoTxOptions dal.TransactionOptions
datastoreTx *datastore.Transaction
pendingKeys []partialKey
dal.QueryExecutor
}
func (tx transaction) InsertMulti(_ context.Context, _ []dal.Record, _ ...dal.InsertOption) error {
//TODO implement me
panic("implement me")
}
// ID returns empty string as datastore doesn't support long-lasting transactions
func (tx transaction) ID() string {
return ""
}
func (tx transaction) Update(_ context.Context, _ *dal.Key, _ []dal.Update, _ ...dal.Precondition) error {
return dal.ErrNotSupported
}
func (tx transaction) UpdateMulti(_ context.Context, _ []*dal.Key, _ []dal.Update, _ ...dal.Precondition) error {
return dal.ErrNotSupported
}
func (tx transaction) Options() dal.TransactionOptions {
return tx.dalgoTxOptions
}
func (tx transaction) Set(ctx context.Context, record dal.Record) error {
data := record.Data()
log.Debugf(ctx, "data: %+v", data)
if data == nil {
panic("record.Data() == nil")
}
if key, isIncomplete, err := getDatastoreKey(record.Key()); err != nil {
return err
} else if isIncomplete {
log.Errorf(ctx, "database.Update() called for incomplete key, will insert.")
panic("not implemented")
//return gaeDb.Insert(ctx, record, dal.NewInsertOptions(dal.WithRandomStringID(5)))
} else if _, err = Put(ctx, tx.db.client, key, data); err != nil {
return fmt.Errorf("failed to update %s: %w", key2str(key), err)
}
return nil
}
func (tx transaction) SetMultiOld(ctx context.Context, records []dal.Record) (err error) { // TODO: Rename to PutMulti?
keys := make([]*datastore.Key, len(records))
values := make([]any, len(records))
insertedIndexes := make([]int, 0, len(records))
for i, record := range records {
if record == nil {
panic(fmt.Sprintf("records[%v] is nil: %v", i, record))
}
isIncomplete := false
if keys[i], isIncomplete, err = getDatastoreKey(record.Key()); err != nil {
return
} else if isIncomplete {
insertedIndexes = append(insertedIndexes, i)
}
if values[i] = record.Data(); values[i] == nil {
return fmt.Errorf("records[%d].Data() == nil", i)
}
}
// logKeys(ctx, "database.SetMulti", keys)
if keys, err = PutMulti(ctx, tx.db.client, keys, values); err != nil {
switch err := err.(type) {
case datastore.MultiError:
if len(err) == len(records) {
for i, e := range err {
if err != nil {
records[i].SetError(e)
}
}
return nil
}
}
return
}
for _, i := range insertedIndexes {
setRecordID(keys[i], records[i])
//records[i].SetData(values[i]) // it seems useless but covers case when .Data() returned newly created object without storing inside record
}
return
}
//func (t transaction) Update(ctx context.Context, key *dal.Key, updates []dal.Update, preconditions ...dal.Precondition) error {
// //TODO implement me
// panic("implement me")
//}
//
//func (t transaction) SetMulti(c context.Context, keys []*dal.Key, updates []dal.Update, preconditions ...dal.Precondition) error {
// //TODO implement me
// panic("implement me")
//}
//
//func (t transaction) Select(ctx context.Context, query dal.Select) (dal.Reader, error) {
// panic("implement me")
//}
//func (t transaction) Insert(ctx context.Context, record dal.Record, opts ...dal.InsertOption) error {
// options := dal.NewInsertOptions(opts...)
// idGenerator := options.IDGenerator()
// key := record.Key()
// if key.ID == nil {
// key.ID = idGenerator(ctx, record)
// }
// dr := t.dtb.doc(key)
// data := record.Data()
// return t.tx.Create(dr, data)
//}
//
//func (t transaction) Upsert(_ context.Context, record dal.Record) error {
// dr := t.dtb.doc(record.Key())
// return t.tx.Set(dr, record.Data())
//}
//
//func (t transaction) Get(_ context.Context, record dal.Record) error {
// key := record.Key()
// docRef := t.dtb.doc(key)
// docSnapshot, err := t.tx.Get(docRef)
// return docSnapshotToRecord(err, docSnapshot, record, func(ds *firestore.DocumentSnapshot, p interface{}) error {
// return ds.DataTo(p)
// })
//}
//
//func (t transaction) Set(ctx context.Context, record dal.Record) error {
// dr := t.dtb.doc(record.Key())
// return t.tx.Set(dr, record.Data())
//}
//
//func (t transaction) Delete(ctx context.Context, key *dal.Key) error {
// dr := t.dtb.doc(key)
// return t.tx.Delete(dr)
//}
//
//func (t transaction) GetMulti(ctx context.Context, records []dal.Record) error {
// dr := make([]*firestore.DocumentRef, len(records))
// for i, r := range records {
// dr[i] = t.dtb.doc(r.Key())
// }
// ds, err := t.tx.GetAll(dr)
// if err != nil {
// return err
// }
// for i, d := range ds {
// err = docSnapshotToRecord(nil, d, records[i], func(ds *firestore.DocumentSnapshot, p interface{}) error {
// return ds.DataTo(p)
// })
// if err != nil {
// return err
// }
// }
// return nil
//}
//
//func (t transaction) SetMulti(ctx context.Context, records []dal.Record) error {
// for _, record := range records { // TODO: can we do this in parallel?
// doc := t.dtb.doc(record.Key())
// _, err := doc.Set(ctx, record.Data())
// if err != nil {
// record.SetError(err)
// return err
// }
// }
// return nil
//}
//
//func (t transaction) DeleteMulti(_ context.Context, keys []*dal.Key) error {
// for _, k := range keys {
// dr := t.dtb.doc(k)
// if err := t.tx.Delete(dr); err != nil {
// return fmt.Errorf("failed to delete record: %w", err)
// }
// }
// return nil
//}