-
Notifications
You must be signed in to change notification settings - Fork 1
/
operation.go
628 lines (516 loc) · 18.3 KB
/
operation.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
package arangom
import (
"context"
"encoding/json"
"fmt"
"github.com/arangodb/go-driver"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
const (
OperationKindAQLExecute OperationKind = iota + 1 // operation to execute an AQL query
OperationKindCollectionCreate // operation to create a collection
OperationKindCollectionUpdate // operation to update a collection
OperationKindCollectionDelete // operation to delete a collection
OperationKindGraphCreate // operation to create a graph
OperationKindGraphAddVertex // operation to add a vertex collection to a graph
OperationKindGraphRemoveVertex // operation to remove a vertex collection from a graph
OperationKindGraphAddEdge // operation to add an edge definition to a graph
OperationKindGraphRemoveEdge // operation to remove an edge definition from a graph
OperationKindGraphDelete // operation to delete a graph
OperationKindViewCreate // operation to create a view
OperationKindViewUpdate // operation to update a view
OperationKindViewDelete // operation to delete a view
OperationKindFulltextIndexCreate // operation to create a fulltext index
OperationKindGeoSpatialIndexCreate // operation to create a geospatial index
OperationKindHashIndexCreate // operation to create a hash index
OperationKindInvertedIndexCreate // operation to create an inverted index
OperationKindPersistentIndexCreate // operation to create a persistent index
OperationKindSkipListIndexCreate // operation to create a skiplist index
OperationKindTTLIndexCreate // operation to create a TTL index
OperationKindZKDIndexCreate // operation to create a ZKD index
OperationKindIndexDelete // operation to delete an index
OperationKindAnalyzerCreate // operation to delete an index
OperationKindAnalyzerDelete // operation to delete an index
)
var (
// ErrInvalidOperationKind is returned when an invalid operation kind is
// specified.
ErrInvalidOperationKind = fmt.Errorf("invalid operation kind")
// operationMap is a map of operation names to operation kinds.
operationMap = map[string]OperationKind{
"executeAQL": OperationKindAQLExecute,
"createCollection": OperationKindCollectionCreate,
"updateCollection": OperationKindCollectionUpdate,
"deleteCollection": OperationKindCollectionDelete,
"createGraph": OperationKindGraphCreate,
"addVertexToGraph": OperationKindGraphAddVertex,
"removeVertexFromGraph": OperationKindGraphRemoveVertex,
"addEdgeToGraph": OperationKindGraphAddEdge,
"removeEdgeFromGraph": OperationKindGraphRemoveEdge,
"deleteGraph": OperationKindGraphDelete,
"createView": OperationKindViewCreate,
"updateView": OperationKindViewUpdate,
"deleteView": OperationKindViewDelete,
"createFulltextIndex": OperationKindFulltextIndexCreate,
"createGeoSpatialIndex": OperationKindGeoSpatialIndexCreate,
"createHashIndex": OperationKindHashIndexCreate,
"createInvertedIndex": OperationKindInvertedIndexCreate,
"createPersistentIndex": OperationKindPersistentIndexCreate,
"createSkipListIndex": OperationKindSkipListIndexCreate,
"createTTLIndex": OperationKindTTLIndexCreate,
"createZKDIndex": OperationKindZKDIndexCreate,
"deleteIndex": OperationKindIndexDelete,
"createAnalyzer": OperationKindAnalyzerCreate,
"deleteAnalyzer": OperationKindAnalyzerDelete,
}
// operationKindMap is a map of operation kinds to operations.
operationKindMap = map[OperationKind]func(o *Operation) OperationFn{
OperationKindAQLExecute: ExecuteAQLOperation,
OperationKindCollectionCreate: CreateCollectionOperation,
OperationKindCollectionUpdate: UpdateCollectionOperation,
OperationKindCollectionDelete: DeleteCollectionOperation,
OperationKindGraphCreate: CreateGraphOperation,
OperationKindGraphAddVertex: AddVertexOperation,
OperationKindGraphRemoveVertex: RemoveVertexOperation,
OperationKindGraphAddEdge: AddEdgeOperation,
OperationKindGraphRemoveEdge: RemoveEdgeOperation,
OperationKindGraphDelete: DeleteGraphOperation,
OperationKindViewCreate: CreateViewOperation,
OperationKindViewUpdate: UpdateViewOperation,
OperationKindViewDelete: DeleteViewOperation,
OperationKindFulltextIndexCreate: CreateFulltextIndexOperation,
OperationKindGeoSpatialIndexCreate: CreateGeoSpatialIndexOperation,
OperationKindHashIndexCreate: CreateHashIndexOperation,
OperationKindInvertedIndexCreate: CreateInvertedIndexOperation,
OperationKindPersistentIndexCreate: CreatePersistentIndexOperation,
OperationKindSkipListIndexCreate: CreateSkipListIndexOperation,
OperationKindTTLIndexCreate: CreateTTLIndexOperation,
OperationKindZKDIndexCreate: CreateZKDIndexOperation,
OperationKindIndexDelete: DeleteIndexOperation,
OperationKindAnalyzerCreate: CreateAnalyzerOperation,
OperationKindAnalyzerDelete: DeleteAnalyzerOperation,
}
)
// OperationFn runs an operation on a database.
type OperationFn func(ctx context.Context, db driver.Database) error
// OperationKind is the kind of operation to run.
type OperationKind int
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (o *OperationKind) UnmarshalYAML(value *yaml.Node) error {
if kind, ok := operationMap[value.Value]; ok {
*o = kind
return nil
}
return errors.Wrap(ErrInvalidOperationKind, value.Value)
}
// Operation is an operation to run in a migration.
type Operation struct {
Kind OperationKind `yaml:"kind"`
Collection string `yaml:"collection"`
Options map[string]any `yaml:"options"`
}
// GetOperationFn returns the operation function for the operation kind.
func (o *Operation) GetOperationFn() (OperationFn, error) {
if op, ok := operationKindMap[o.Kind]; ok && op != nil {
return op(o), nil
}
return nil, ErrInvalidOperationKind
}
// convertToOperationOptions converts a map of options to a struct using JSON.
// This is used to convert the options from the YAML file to the options for
// the ArangoDB driver. Since the ArangoDB driver uses a JSON tag for the
// options, we can use JSON to convert the options from the YAML file to the
// options for the ArangoDB driver.
func convertToOperationOptions(opts map[string]any, dst any) error {
b, err := json.Marshal(opts)
if err != nil {
return err
}
return json.Unmarshal(b, dst)
}
// ExecuteAQLOperation executes an AQL query.
func ExecuteAQLOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type aqlOpts struct {
Query string `json:"query"`
BindVars map[string]any `json:"bindVars"`
}
opts := aqlOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
_, err := db.Query(ctx, opts.Query, opts.BindVars)
return err
}
}
// CreateCollectionOperation creates a collection.
func CreateCollectionOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
opts := driver.CreateCollectionOptions{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
_, err := db.CreateCollection(ctx, o.Collection, &opts)
return err
}
}
// UpdateCollectionOperation updates a collection.
func UpdateCollectionOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
opts := driver.SetCollectionPropertiesOptions{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
coll, err := db.Collection(ctx, o.Collection)
if err != nil {
return err
}
return coll.SetProperties(ctx, opts)
}
}
// DeleteCollectionOperation removes a collection.
func DeleteCollectionOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
coll, err := db.Collection(ctx, o.Collection)
if err != nil {
return err
}
return coll.Remove(ctx)
}
}
// CreateGraphOperation creates a graph.
func CreateGraphOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
opts := driver.CreateGraphOptions{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
_, err := db.CreateGraphV2(ctx, o.Collection, &opts)
return err
}
}
// AddVertexOperation adds a vertex collection to a graph.
func AddVertexOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type addVertexOpts struct {
driver.CreateVertexCollectionOptions
Collection string `json:"collection"`
}
opts := addVertexOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
graph, err := db.Graph(ctx, o.Collection)
if err != nil {
return err
}
_, err = graph.CreateVertexCollectionWithOptions(ctx, opts.Collection, opts.CreateVertexCollectionOptions)
return err
}
}
// RemoveVertexOperation removes a vertex collection from a graph.
func RemoveVertexOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type removeVertexOpts struct {
Collection string `json:"collection"`
}
opts := removeVertexOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
graph, err := db.Graph(ctx, o.Collection)
if err != nil {
return err
}
vertex, err := graph.VertexCollection(ctx, opts.Collection)
if err != nil {
return err
}
return vertex.Remove(ctx)
}
}
// AddEdgeOperation adds an edge definition to a graph.
func AddEdgeOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type addEdgeOpts struct {
driver.CreateEdgeCollectionOptions
Collection string `json:"collection"`
Constraints driver.VertexConstraints `json:"constraints"`
}
opts := addEdgeOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
graph, err := db.Graph(ctx, o.Collection)
if err != nil {
return err
}
_, err = graph.CreateEdgeCollectionWithOptions(ctx, opts.Collection, opts.Constraints, opts.CreateEdgeCollectionOptions)
return err
}
}
// RemoveEdgeOperation removes an edge definition from a graph.
func RemoveEdgeOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type removeEdgeOpts struct {
Collection string `json:"collection"`
}
opts := removeEdgeOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
graph, err := db.Graph(ctx, o.Collection)
if err != nil {
return err
}
edge, _, err := graph.EdgeCollection(ctx, opts.Collection)
if err != nil {
return err
}
return edge.Remove(ctx)
}
}
// DeleteGraphOperation deletes a graph.
func DeleteGraphOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
graph, err := db.Graph(ctx, o.Collection)
if err != nil {
return err
}
return graph.Remove(ctx)
}
}
// CreateViewOperation creates a search view.
func CreateViewOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
opts := driver.ArangoSearchViewProperties{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
_, err := db.CreateArangoSearchView(ctx, o.Collection, &opts)
return err
}
}
// UpdateViewOperation updates an existing search view.
func UpdateViewOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
opts := driver.ArangoSearchViewProperties{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
view, err := db.View(ctx, o.Collection)
if err != nil {
return err
}
searchView, err := view.ArangoSearchView()
if err != nil {
return err
}
return searchView.SetProperties(ctx, opts)
}
}
// DeleteViewOperation removes a search view.
func DeleteViewOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
view, err := db.View(ctx, o.Collection)
if err != nil {
return err
}
return view.Remove(ctx)
}
}
func CreateFulltextIndexOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type fulltextIndexOpts struct {
driver.EnsureFullTextIndexOptions
Fields []string `json:"fields"`
}
opts := fulltextIndexOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
coll, err := db.Collection(ctx, o.Collection)
if err != nil {
return err
}
_, _, err = coll.EnsureFullTextIndex(ctx, opts.Fields, &opts.EnsureFullTextIndexOptions) //nolint:staticcheck
return err
}
}
func CreateGeoSpatialIndexOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type geoSpatialIndexOpts struct {
driver.EnsureGeoIndexOptions
Fields []string `json:"fields"`
}
opts := geoSpatialIndexOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
coll, err := db.Collection(ctx, o.Collection)
if err != nil {
return err
}
_, _, err = coll.EnsureGeoIndex(ctx, opts.Fields, &opts.EnsureGeoIndexOptions)
return err
}
}
func CreateHashIndexOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type hashIndexOpts struct {
driver.EnsureHashIndexOptions
Fields []string `json:"fields"`
}
opts := hashIndexOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
coll, err := db.Collection(ctx, o.Collection)
if err != nil {
return err
}
_, _, err = coll.EnsureHashIndex(ctx, opts.Fields, &opts.EnsureHashIndexOptions)
return err
}
}
func CreateInvertedIndexOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
opts := driver.InvertedIndexOptions{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
coll, err := db.Collection(ctx, o.Collection)
if err != nil {
return err
}
_, _, err = coll.EnsureInvertedIndex(ctx, &opts)
return err
}
}
func CreatePersistentIndexOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type persistentIndexOpts struct {
driver.EnsurePersistentIndexOptions
Fields []string `json:"fields"`
}
opts := persistentIndexOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
coll, err := db.Collection(ctx, o.Collection)
if err != nil {
return err
}
_, _, err = coll.EnsurePersistentIndex(ctx, opts.Fields, &opts.EnsurePersistentIndexOptions)
return err
}
}
func CreateSkipListIndexOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type skipListIndexOpts struct {
driver.EnsureSkipListIndexOptions
Fields []string `json:"fields"`
}
opts := skipListIndexOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
coll, err := db.Collection(ctx, o.Collection)
if err != nil {
return err
}
_, _, err = coll.EnsureSkipListIndex(ctx, opts.Fields, &opts.EnsureSkipListIndexOptions)
return err
}
}
func CreateTTLIndexOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type ttlIndexOpts struct {
driver.EnsureTTLIndexOptions
Field string `json:"field"`
ExpireAfter int `json:"expireAfter"`
}
opts := ttlIndexOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
coll, err := db.Collection(ctx, o.Collection)
if err != nil {
return err
}
_, _, err = coll.EnsureTTLIndex(ctx, opts.Field, opts.ExpireAfter, &opts.EnsureTTLIndexOptions)
return err
}
}
func CreateZKDIndexOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type zkdIndexOpts struct {
driver.EnsureZKDIndexOptions
Fields []string `json:"fields"`
}
opts := zkdIndexOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
coll, err := db.Collection(ctx, o.Collection)
if err != nil {
return err
}
_, _, err = coll.EnsureZKDIndex(ctx, opts.Fields, &opts.EnsureZKDIndexOptions)
return err
}
}
func DeleteIndexOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type deleteIndexOpts struct {
Name string `json:"name"`
}
opts := deleteIndexOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
coll, err := db.Collection(ctx, o.Collection)
if err != nil {
return err
}
index, err := coll.Index(ctx, opts.Name)
if err != nil {
return err
}
return index.Remove(ctx)
}
}
func CreateAnalyzerOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
opts := driver.ArangoSearchAnalyzerDefinition{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
found, _, err := db.EnsureAnalyzer(ctx, opts)
if err != nil {
return err
}
if found {
return fmt.Errorf("analyzer '%s' already exists", opts.Name)
}
return nil
}
}
func DeleteAnalyzerOperation(o *Operation) OperationFn {
return func(ctx context.Context, db driver.Database) error {
type deleteAnalyzerOpts struct {
Name string `json:"name"`
Force bool `json:"force"`
}
opts := deleteAnalyzerOpts{}
if err := convertToOperationOptions(o.Options, &opts); err != nil {
return err
}
analyzer, err := db.Analyzer(ctx, opts.Name)
if err != nil {
return err
}
return analyzer.Remove(ctx, opts.Force)
}
}