-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
169 lines (151 loc) · 5.04 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
package dalgo2firestore
import (
"cloud.google.com/go/firestore"
"context"
"errors"
"fmt"
"github.com/dal-go/dalgo/dal"
"time"
)
func (db database) RunReadonlyTransaction(ctx context.Context, f dal.ROTxWorker, options ...dal.TransactionOption) error {
options = append(options, dal.TxWithReadonly())
firestoreTxOptions := createFirestoreTransactionOptions(options)
return db.client.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
return f(ctx, transaction{db: db, tx: tx, QueryExecutor: db.QueryExecutor})
}, firestoreTxOptions...)
}
func (db database) RunReadwriteTransaction(ctx context.Context, f dal.RWTxWorker, options ...dal.TransactionOption) error {
firestoreTxOptions := createFirestoreTransactionOptions(options)
var started time.Time
if Debugf != nil {
started = time.Now()
Debugf(ctx, "RunReadwriteTransaction: firestoreTxOptions: %+v", firestoreTxOptions)
}
err := db.client.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
return f(ctx, transaction{db: db, tx: tx, QueryExecutor: db.QueryExecutor})
}, firestoreTxOptions...)
if Debugf != nil {
Debugf(ctx, "RunReadwriteTransaction() completed in %v, err: %v", time.Until(started), err)
}
return err
}
func createFirestoreTransactionOptions(opts []dal.TransactionOption) (options []firestore.TransactionOption) {
to := dal.NewTransactionOptions(opts...)
if to.IsReadonly() {
options = append(options, firestore.ReadOnly)
}
return
}
var _ dal.Transaction = (*transaction)(nil)
var _ dal.ReadwriteTransaction = (*transaction)(nil)
type transaction struct {
db database
tx *firestore.Transaction
options dal.TransactionOptions
dal.QueryExecutor
}
func (tx transaction) Close(_ context.Context) error {
panic("TODO: implement or remove me")
}
func (tx transaction) ID() string {
return ""
}
func (tx transaction) Options() dal.TransactionOptions {
return tx.options
}
func (tx transaction) Insert(ctx context.Context, record dal.Record, opts ...dal.InsertOption) error {
if Debugf != nil {
Debugf(ctx, "tx.Insert(%v)", record.Key())
}
options := dal.NewInsertOptions(opts...)
idGenerator := options.IDGenerator()
key := record.Key()
if key.ID == nil {
key.ID = idGenerator(ctx, record)
}
dr := tx.db.keyToDocRef(key)
record.SetError(nil) // Mark record as not having an error
data := record.Data()
return tx.tx.Create(dr, data)
}
func (tx transaction) Upsert(ctx context.Context, record dal.Record) error {
if Debugf != nil {
Debugf(ctx, "tx.Upsert(%v)", record.Key())
}
dr := tx.db.keyToDocRef(record.Key())
return tx.tx.Set(dr, record.Data())
}
func (tx transaction) Get(ctx context.Context, record dal.Record) error {
if Debugf != nil {
Debugf(ctx, "tx.Get(%v)", record.Key())
}
key := record.Key()
docRef := tx.db.keyToDocRef(key)
docSnapshot, err := tx.tx.Get(docRef)
return docSnapshotToRecord(err, docSnapshot, record, dataTo)
}
func (tx transaction) Set(ctx context.Context, record dal.Record) error {
if Debugf != nil {
Debugf(ctx, "tx.Set(%v)", record.Key())
}
dr := tx.db.keyToDocRef(record.Key())
return tx.tx.Set(dr, record.Data())
}
func (tx transaction) Delete(ctx context.Context, key *dal.Key) error {
if Debugf != nil {
Debugf(ctx, "tx.Delete(%v)", key)
}
dr := tx.db.keyToDocRef(key)
return tx.tx.Delete(dr)
}
func (tx transaction) GetMulti(ctx context.Context, records []dal.Record) error {
dr := make([]*firestore.DocumentRef, len(records))
for i, r := range records {
dr[i] = tx.db.keyToDocRef(r.Key())
}
logMultiRecords(ctx, "tx.GetMulti", records)
ds, err := tx.tx.GetAll(dr)
if err != nil {
return fmt.Errorf("failed to get %d records by keys: %w", len(records), err)
}
var errs []error
for i, d := range ds {
if err = docSnapshotToRecord(nil, d, records[i], dataTo); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return fmt.Errorf("failed to get %d out of %d records requested by keys: %w", len(errs), len(records), errors.Join(errs...))
}
return nil
}
func (tx transaction) SetMulti(ctx context.Context, records []dal.Record) error {
logMultiRecords(ctx, "SetMulti", records)
for _, record := range records { // TODO: can we do this in parallel?
doc := tx.db.keyToDocRef(record.Key())
record.SetError(nil) // Mark record as not having an error
_, err := doc.Set(ctx, record.Data())
if err != nil {
record.SetError(err)
return err
}
}
return nil
}
func (tx transaction) DeleteMulti(ctx context.Context, keys []*dal.Key) error {
logMultiKeys(ctx, "DeleteMulti", keys)
for _, k := range keys {
dr := tx.db.keyToDocRef(k)
if err := tx.tx.Delete(dr); err != nil {
return fmt.Errorf("failed to deleteByDocRef record: %w", err)
}
}
return nil
}
func (tx transaction) InsertMulti(ctx context.Context, records []dal.Record, opts ...dal.InsertOption) (err error) {
logMultiRecords(ctx, "InsertMulti", records)
_, err = insertMulti(ctx, tx.db, records, func(ctx context.Context, docRef *firestore.DocumentRef, data any) (result *firestore.WriteResult, err error) {
return nil, tx.tx.Create(docRef, data)
}, opts...)
return
}