-
Notifications
You must be signed in to change notification settings - Fork 2
/
tx.go
385 lines (361 loc) · 10.7 KB
/
tx.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
package myjson
import (
"context"
"time"
"github.com/autom8ter/myjson/errors"
"github.com/autom8ter/myjson/kv"
"github.com/autom8ter/myjson/util"
"github.com/dop251/goja"
"github.com/samber/lo"
"github.com/segmentio/ksuid"
)
// TxFunc is a function executed against a transaction - if the function returns an error, all changes will be rolled back.
// Otherwise, the changes will be commited to the database
type TxFunc func(ctx context.Context, tx Tx) error
// ForEachFunc returns false to stop scanning and an error if one occurred
type ForEachFunc func(d *Document) (bool, error)
type transaction struct {
db *defaultDB
tx kv.Tx
isBatch bool
cdc []CDC
vm *goja.Runtime
docs map[string]struct{}
}
func (t *transaction) Commit(ctx context.Context) error {
if err := t.tx.Commit(ctx); err != nil {
return err
}
t.cdc = []CDC{}
return nil
}
func (t *transaction) Rollback(ctx context.Context) error {
if err := t.tx.Rollback(ctx); err != nil {
return err
}
t.cdc = []CDC{}
return nil
}
func (t *transaction) Update(ctx context.Context, collection string, id string, update map[string]any) error {
schema, ctx := t.db.getSchema(ctx, collection)
if schema == nil {
return errors.New(errors.Validation, "tx: unsupported collection: %s", collection)
}
doc, err := NewDocumentFrom(update)
if err != nil {
return errors.Wrap(err, 0, "tx: failed to update")
}
if err := schema.SetPrimaryKey(doc, id); err != nil {
return errors.Wrap(err, 0, "tx: failed to set primary key")
}
if err := t.persistCommand(ctx, &persistCommand{
Collection: collection,
Action: UpdateAction,
Document: doc,
Timestamp: time.Now().UnixNano(),
Metadata: ExtractMetadata(ctx),
}); err != nil {
return errors.Wrap(err, 0, "tx: failed to commit update")
}
return nil
}
func (t *transaction) Create(ctx context.Context, collection string, document *Document) (string, error) {
c, ctx := t.db.getSchema(ctx, collection)
if c == nil {
return "", errors.New(errors.Validation, "tx: unsupported collection: %s", collection)
}
var id = c.GetPrimaryKey(document)
if id == "" {
id = ksuid.New().String()
err := c.SetPrimaryKey(document, id)
if err != nil {
return "", err
}
}
if err := t.persistCommand(ctx, &persistCommand{
Collection: collection,
Action: CreateAction,
Document: document,
Timestamp: time.Now().UnixNano(),
Metadata: ExtractMetadata(ctx),
}); err != nil {
return "", errors.Wrap(err, 0, "tx: failed to commit create")
}
return id, nil
}
func (t *transaction) Set(ctx context.Context, collection string, document *Document) error {
schema, ctx := t.db.getSchema(ctx, collection)
if schema == nil {
return errors.New(errors.Validation, "tx: unsupported collection: %s", collection)
}
if err := t.persistCommand(ctx, &persistCommand{
Collection: collection,
Action: SetAction,
Document: document,
Timestamp: time.Now().UnixNano(),
Metadata: ExtractMetadata(ctx),
}); err != nil {
return errors.Wrap(err, 0, "tx: failed to commit set")
}
return nil
}
func (t *transaction) Delete(ctx context.Context, collection string, id string) error {
schema, ctx := t.db.getSchema(ctx, collection)
if schema == nil {
return errors.New(errors.Validation, "tx: unsupported collection: %s", collection)
}
d, _ := NewDocumentFrom(map[string]any{
t.db.GetSchema(ctx, collection).PrimaryKey(): id,
})
if err := t.persistCommand(ctx, &persistCommand{
Collection: collection,
Action: DeleteAction,
Document: d,
Timestamp: time.Now().UnixNano(),
Metadata: ExtractMetadata(ctx),
}); err != nil {
return errors.Wrap(err, 0, "tx: failed to commit delete")
}
return nil
}
func (t *transaction) Query(ctx context.Context, collection string, query Query) (Page, error) {
if len(query.Select) == 0 {
query.Select = append(query.Select, Select{Field: "*"})
}
if err := query.Validate(ctx); err != nil {
return Page{}, err
}
schema, ctx := t.db.getSchema(ctx, collection)
if schema == nil {
return Page{}, errors.New(errors.Validation, "tx: unsupported collection: %s", collection)
}
allow, err := t.authorizeQuery(ctx, schema, &query)
if err != nil {
return Page{}, err
}
if !allow {
return Page{}, errors.New(errors.Forbidden, "not authorized: %s/%s", collection, QueryAction)
}
if isAggregateQuery(query) {
return t.aggregate(ctx, collection, query)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
now := time.Now()
var results Documents
fullScan := true
match, err := t.queryScan(ctx, collection, query.Where, query.Join, func(d *Document) (bool, error) {
results = append(results, d)
if query.Page == 0 && len(query.OrderBy) == 0 && query.Limit > 0 && len(results) >= query.Limit {
fullScan = false
return false, nil
}
return true, nil
})
if err != nil {
return Page{}, err
}
results = orderByDocs(results, query.OrderBy)
if fullScan && query.Limit > 0 && query.Page > 0 {
results = lo.Slice(results, query.Limit*query.Page, (query.Limit*query.Page)+query.Limit)
}
if query.Limit > 0 && len(results) > query.Limit {
results = results[:query.Limit]
}
if len(query.Select) > 0 && query.Select[0].Field != "*" {
for _, result := range results {
err := selectDocument(result, query.Select)
if err != nil {
return Page{}, err
}
}
}
return Page{
Documents: results,
NextPage: query.Page + 1,
Count: len(results),
Stats: PageStats{
ExecutionTime: time.Since(now),
Explain: &match,
},
}, nil
}
func (t *transaction) Get(ctx context.Context, collection string, id string) (*Document, error) {
c, ctx := t.db.getSchema(ctx, collection)
if c == nil {
return nil, errors.New(errors.Validation, "tx: unsupported collection: %s", collection)
}
results, err := t.Query(ctx, collection, Query{Where: []Where{{Field: c.PrimaryKey(), Op: WhereOpEq, Value: id}}, Limit: 1})
if err != nil {
return nil, errors.Wrap(err, errors.NotFound, "%s not found", id)
}
if results.Count == 0 {
return nil, errors.New(errors.NotFound, "%s not found", id)
}
return results.Documents[0], nil
}
func (t *transaction) Cmd(ctx context.Context, cmd TxCmd) TxResponse {
switch {
case cmd.Commit != nil:
if err := t.Commit(ctx); err != nil {
return TxResponse{Commit: &struct{}{}, Error: errors.Extract(err)}
}
return TxResponse{Commit: &struct{}{}}
case cmd.Rollback != nil:
if err := t.Rollback(ctx); err != nil {
return TxResponse{Rollback: &struct{}{}, Error: errors.Extract(err)}
}
return TxResponse{Rollback: &struct{}{}}
case cmd.Query != nil:
results, err := t.Query(ctx, cmd.Query.Collection, cmd.Query.Query)
if err != nil {
return TxResponse{Error: errors.Extract(err)}
}
return TxResponse{
Query: &results,
}
case cmd.Create != nil:
_, err := t.Create(ctx, cmd.Create.Collection, cmd.Create.Document)
if err != nil {
return TxResponse{Error: errors.Extract(err)}
}
return TxResponse{
Create: cmd.Create.Document,
}
case cmd.Set != nil:
err := t.Set(ctx, cmd.Set.Collection, cmd.Set.Document)
if err != nil {
return TxResponse{Error: errors.Extract(err)}
}
return TxResponse{
Set: cmd.Set.Document,
}
case cmd.Delete != nil:
err := t.Delete(ctx, cmd.Delete.Collection, cmd.Delete.ID)
if err != nil {
return TxResponse{Error: errors.Extract(err)}
}
return TxResponse{
Delete: &struct{}{},
}
case cmd.Get != nil:
doc, err := t.Get(ctx, cmd.Get.Collection, cmd.Get.ID)
if err != nil {
return TxResponse{Error: errors.Extract(err)}
}
return TxResponse{
Get: doc,
}
case cmd.Update != nil:
err := t.Update(ctx, cmd.Update.Collection, cmd.Update.ID, cmd.Update.Update)
if err != nil {
return TxResponse{Error: errors.Extract(err)}
}
doc, err := t.Get(ctx, cmd.Update.Collection, cmd.Update.ID)
if err != nil {
return TxResponse{Error: errors.Extract(err)}
}
return TxResponse{
Update: doc,
}
case cmd.TimeTravel != nil:
doc, err := t.TimeTravel(ctx, cmd.TimeTravel.Collection, cmd.TimeTravel.ID, cmd.TimeTravel.Timestamp)
if err != nil {
return TxResponse{Error: errors.Extract(err)}
}
return TxResponse{
TimeTravel: doc,
}
case cmd.Revert != nil:
if err := t.Revert(ctx, cmd.Revert.Collection, cmd.Revert.ID, cmd.Revert.Timestamp); err != nil {
return TxResponse{Error: errors.Extract(err)}
}
return TxResponse{
Revert: &struct{}{},
}
}
return TxResponse{Error: errors.Extract(errors.New(errors.Validation, "tx: unsupported command"))}
}
// aggregate performs aggregations against the collection
func (t *transaction) aggregate(ctx context.Context, collection string, query Query) (Page, error) {
c, ctx := t.db.getSchema(ctx, collection)
if c == nil {
return Page{}, errors.New(errors.Validation, "tx: unsupported collection: %s", collection)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
now := time.Now()
var results Documents
match, err := t.queryScan(ctx, collection, query.Where, query.Join, func(d *Document) (bool, error) {
results = append(results, d)
return true, nil
})
if err != nil {
return Page{}, err
}
var reduced Documents
for _, values := range groupByDocs(results, query.GroupBy) {
value, err := aggregateDocs(values, query.Select)
if err != nil {
return Page{}, err
}
reduced = append(reduced, value)
}
reduced, err = docsHaving(query.Having, reduced)
if err != nil {
return Page{}, errors.Wrap(err, errors.Internal, "")
}
reduced = orderByDocs(reduced, query.OrderBy)
if query.Limit > 0 && query.Page > 0 {
reduced = lo.Slice(reduced, query.Limit*query.Page, (query.Limit*query.Page)+query.Limit)
}
if query.Limit > 0 && len(reduced) > query.Limit {
reduced = reduced[:query.Limit]
}
return Page{
Documents: reduced,
NextPage: query.Page + 1,
Count: len(reduced),
Stats: PageStats{
ExecutionTime: time.Since(now),
Explain: &match,
},
}, nil
}
func docsHaving(where []Where, results Documents) (Documents, error) {
if len(where) > 0 {
for i, document := range results {
pass, err := document.Where(where)
if err != nil {
return nil, err
}
if pass {
results = util.RemoveElement(i, results)
}
}
}
return results, nil
}
func (t *transaction) ForEach(ctx context.Context, collection string, opts ForEachOpts, fn ForEachFunc) (Explain, error) {
pass, err := t.authorizeQuery(ctx, t.db.GetSchema(ctx, collection), &Query{
Where: opts.Where,
Join: opts.Join,
})
if err != nil {
return Explain{}, err
}
if !pass {
return Explain{}, errors.New(errors.Forbidden, "not authorized: %s", QueryAction)
}
return t.queryScan(ctx, collection, opts.Where, opts.Join, fn)
}
func (t *transaction) Close(ctx context.Context) {
t.tx.Close(ctx)
t.cdc = []CDC{}
}
func (t *transaction) CDC() []CDC {
return t.cdc
}
func (t *transaction) DB() Database {
return t.db
}