-
Notifications
You must be signed in to change notification settings - Fork 15
/
transaction.go
1135 lines (931 loc) · 24.8 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
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package lungo
import (
"fmt"
"sync"
"time"
"go.mongodb.org/mongo-driver/bson"
"github.com/256dpi/lungo/bsonkit"
"github.com/256dpi/lungo/mongokit"
)
// Opcode defines the type of operation.
type Opcode int
// The available opcodes.
const (
Insert Opcode = iota
Replace
Update
Delete
)
// String returns the opcode name.
func (c Opcode) String() string {
switch c {
case Insert:
return "insert"
case Replace:
return "replace"
case Update:
return "update"
case Delete:
return "delete"
default:
return ""
}
}
// Operation defines a single operation.
type Operation struct {
// The opcode.
Opcode Opcode
// The filter document (replace, update, delete).
Filter bsonkit.Doc
// The insert, update or replacement document.
Document bsonkit.Doc
// The sorting to apply (replace, update, delete).
Sort bsonkit.Doc
// Whether an upsert should be performed (replace, update).
Upsert bool
// The documents to skip (update, delete).
Skip int
// The limit (update, delete).
Limit int
// The array filter conditions (update).
ArrayFilters bsonkit.List
}
// Result describes the outcome of an operation.
type Result struct {
// The list of matched documents.
Matched bsonkit.List
// The list of inserted, replace or updated documents.
Modified bsonkit.List
// The upserted document.
Upserted bsonkit.Doc
// The error that occurred during the operation.
Error error
}
// Transaction buffers multiple changes to a catalog.
type Transaction struct {
catalog *Catalog
dirty bool
mutex sync.RWMutex
}
// NewTransaction creates and returns a new transaction.
func NewTransaction(catalog *Catalog) *Transaction {
return &Transaction{
catalog: catalog,
}
}
// Create will ensure that a namespace for the provided handle exists.
func (t *Transaction) Create(handle Handle) error {
// acquire write lock
t.mutex.Lock()
defer t.mutex.Unlock()
// validate handle
err := handle.Validate(true)
if err != nil {
return err
}
// check access
if handle[0] == Local {
return fmt.Errorf("namespace local.* is read only")
}
// check catalog
if t.catalog.Namespaces[handle] != nil {
return nil
}
// create collection
t.catalog = t.catalog.Clone()
t.catalog.Namespaces[handle] = mongokit.NewCollection(true)
t.dirty = true
return nil
}
// Find will query documents from a namespace. Sort, skip and limit may be
// supplied to modify the result. The returned results will contain the matched
// list of documents.
func (t *Transaction) Find(handle Handle, query, sort bsonkit.Doc, skip, limit int) (*Result, error) {
// acquire read lock
t.mutex.RLock()
defer t.mutex.RUnlock()
// validate handle
err := handle.Validate(true)
if err != nil {
return nil, err
}
// check namespace
if t.catalog.Namespaces[handle] == nil {
return &Result{}, nil
}
// find documents
res, err := t.catalog.Namespaces[handle].Find(query, sort, skip, limit)
if err != nil {
return nil, err
}
return &Result{
Matched: res.Matched,
}, nil
}
// Bulk performs the specified operations in one go. If ordered is true the
// process is aborted on the first error.
func (t *Transaction) Bulk(handle Handle, ops []Operation, ordered bool) ([]Result, error) {
// acquire write lock
t.mutex.Lock()
defer t.mutex.Unlock()
// validate handle
err := handle.Validate(true)
if err != nil {
return nil, err
}
// check access
if handle[0] == Local {
return nil, fmt.Errorf("namespace local.* is read only")
}
// clone catalog
clone := t.catalog.Clone()
// ensure namespace
if clone.Namespaces[handle] == nil {
clone.Namespaces[handle] = mongokit.NewCollection(true)
}
// collect changes
changes := 0
// prepare results
results := make([]Result, 0, len(ops))
// process models
for _, op := range ops {
// clone namespace and oplog for every operation as the collections may
// be left in an undefined state after skipping errors
// clone namespace and oplog
namespace := clone.Namespaces[handle].Clone()
oplog := clone.Namespaces[Oplog].Clone()
// prepare variables
var res *Result
var err error
// run operation
switch op.Opcode {
case Insert:
res, err = t.insert(handle, oplog, namespace, op.Document)
case Replace:
res, err = t.replace(handle, oplog, namespace, op.Filter, op.Document, op.Sort, op.Upsert)
case Update:
res, err = t.update(handle, oplog, namespace, op.Filter, op.Document, op.Sort, op.Upsert, op.Skip, op.Limit, op.ArrayFilters)
case Delete:
res, err = t.delete(handle, oplog, namespace, op.Filter, op.Sort, op.Skip, op.Limit)
default:
return nil, fmt.Errorf("unsupported bulk opcode %q", op.Opcode.String())
}
// check error
if err != nil {
// append error
results = append(results, Result{
Error: err,
})
// stop if ordered
if ordered {
break
} else {
continue
}
}
// replace namespace and oplog
clone.Namespaces[handle] = namespace
clone.Namespaces[Oplog] = oplog
// append result
results = append(results, *res)
// update changes
changes += len(res.Modified)
if res.Upserted != nil {
changes++
} else if op.Opcode == Delete {
changes += len(res.Matched)
}
}
// set catalog and flag
if changes > 0 {
t.catalog = clone
t.dirty = true
}
return results, nil
}
// Insert will insert the specified documents into the namespace. The engine
// will automatically generate an object id per document if it is missing. If
// ordered is enabled the operation is aborted on the first error and the
// result returned. Otherwise, the engine will try to insert all documents. The
// returned results will contain the inserted documents and potential errors.
func (t *Transaction) Insert(handle Handle, list bsonkit.List, ordered bool) (*Result, error) {
// acquire write lock
t.mutex.Lock()
defer t.mutex.Unlock()
// validate handle
err := handle.Validate(true)
if err != nil {
return nil, err
}
// check access
if handle[0] == Local {
return nil, fmt.Errorf("namespace local.* is read only")
}
// clone list
list = bsonkit.CloneList(list)
// clone catalog
clone := t.catalog.Clone()
// ensure namespace
if clone.Namespaces[handle] == nil {
clone.Namespaces[handle] = mongokit.NewCollection(true)
}
// prepare result
result := &Result{}
// insert documents
for _, doc := range list {
// clone namespace and oplog for every insert as the collections may
// be left in an undefined state after skipping errors
// clone namespace and oplog
namespace := clone.Namespaces[handle].Clone()
oplog := clone.Namespaces[Oplog].Clone()
// perform insert
res, err := t.insert(handle, oplog, namespace, doc)
if err != nil {
// set error
if result.Error == nil {
result.Error = err
}
// stop if ordered or continue
if ordered {
break
} else {
continue
}
}
// replace namespace and oplog
clone.Namespaces[handle] = namespace
clone.Namespaces[Oplog] = oplog
// merge result
result.Modified = append(result.Modified, res.Modified...)
}
// set catalog and flag
if len(result.Modified) > 0 {
t.catalog = clone
t.dirty = true
}
return result, nil
}
func (t *Transaction) insert(handle Handle, oplog, namespace *mongokit.Collection, doc bsonkit.Doc) (*Result, error) {
// insert document
res, err := namespace.Insert(doc)
if err != nil {
return nil, err
}
// append oplog
err = t.append(oplog, handle, "insert", doc, nil)
if err != nil {
return nil, err
}
return &Result{
Modified: res.Modified,
}, nil
}
// Replace will replace the first matching document with the specified
// replacement document. If upsert is enabled, it will insert the replacement
// document if it is missing. The returned result will contain the matched
// and modified or upserted document.
func (t *Transaction) Replace(handle Handle, query, sort, repl bsonkit.Doc, upsert bool) (*Result, error) {
// acquire write lock
t.mutex.Lock()
defer t.mutex.Unlock()
// validate handle
err := handle.Validate(true)
if err != nil {
return nil, err
}
// check access
if handle[0] == Local {
return nil, fmt.Errorf("namespace local.* is read only")
}
// check namespace
if t.catalog.Namespaces[handle] == nil && !upsert {
return &Result{}, nil
}
// clone replacement
repl = bsonkit.Clone(repl)
// clone catalog
clone := t.catalog.Clone()
// create or clone namespace
var namespace *mongokit.Collection
if clone.Namespaces[handle] == nil {
namespace = mongokit.NewCollection(true)
clone.Namespaces[handle] = namespace
} else {
namespace = clone.Namespaces[handle].Clone()
clone.Namespaces[handle] = namespace
}
// clone oplog
oplog := clone.Namespaces[Oplog].Clone()
clone.Namespaces[Oplog] = oplog
// perform replace
res, err := t.replace(handle, oplog, namespace, query, repl, sort, upsert)
if err != nil {
return nil, err
}
// set catalog and flag
if len(res.Modified) > 0 || res.Upserted != nil {
t.catalog = clone
t.dirty = true
}
return res, nil
}
func (t *Transaction) replace(handle Handle, oplog, namespace *mongokit.Collection, query, repl, sort bsonkit.Doc, upsert bool) (*Result, error) {
// replace document
res, err := namespace.Replace(query, repl, sort)
if err != nil {
return nil, err
}
// perform upsert
if len(res.Modified) == 0 && upsert {
res, err = namespace.Upsert(query, repl, nil, nil)
if err != nil {
return nil, err
}
// append oplog
err = t.append(oplog, handle, "insert", res.Upserted, nil)
if err != nil {
return nil, err
}
return &Result{
Upserted: res.Upserted,
}, nil
}
// append oplog
if len(res.Modified) > 0 {
err = t.append(oplog, handle, "replace", res.Modified[0], nil)
if err != nil {
return nil, err
}
}
return &Result{
Matched: res.Matched,
Modified: res.Modified,
}, nil
}
// Update will apply the update to all matching document. Sort, skip and limit
// may be supplied to modify the result. If upsert is enabled, it will extract
// constant parts of the query and apply the update and insert the document if
// it is missing. The returned result will contain the matched and modified or
// upserted document.
func (t *Transaction) Update(handle Handle, query, sort, update bsonkit.Doc, skip, limit int, upsert bool, arrayFilters bsonkit.List) (*Result, error) {
// acquire write lock
t.mutex.Lock()
defer t.mutex.Unlock()
// validate handle
err := handle.Validate(true)
if err != nil {
return nil, err
}
// check access
if handle[0] == Local {
return nil, fmt.Errorf("namespace local.* is read only")
}
// check namespace
if t.catalog.Namespaces[handle] == nil && !upsert {
return &Result{}, nil
}
// clone catalog
clone := t.catalog.Clone()
// create or clone namespace
var namespace *mongokit.Collection
if clone.Namespaces[handle] == nil {
namespace = mongokit.NewCollection(true)
clone.Namespaces[handle] = namespace
} else {
namespace = clone.Namespaces[handle].Clone()
clone.Namespaces[handle] = namespace
}
// clone oplog
oplog := clone.Namespaces[Oplog].Clone()
clone.Namespaces[Oplog] = oplog
// perform update
res, err := t.update(handle, oplog, namespace, query, update, sort, upsert, skip, limit, arrayFilters)
if err != nil {
return nil, err
}
// set catalog and flag
if len(res.Modified) > 0 || res.Upserted != nil {
t.catalog = clone
t.dirty = true
}
return res, nil
}
func (t *Transaction) update(handle Handle, oplog, namespace *mongokit.Collection, query, update, sort bsonkit.Doc, upsert bool, skip, limit int, arrayFilters bsonkit.List) (*Result, error) {
// perform update
res, err := namespace.Update(query, update, sort, skip, limit, arrayFilters)
if err != nil {
return nil, err
}
// perform upsert
if len(res.Modified) == 0 && upsert {
res, err = namespace.Upsert(query, nil, update, arrayFilters)
if err != nil {
return nil, err
}
// append oplog
err = t.append(oplog, handle, "insert", res.Upserted, nil)
if err != nil {
return nil, err
}
return &Result{
Upserted: res.Upserted,
}, nil
}
// append oplog
for i, doc := range res.Modified {
err = t.append(oplog, handle, "update", doc, res.Changes[i])
if err != nil {
return nil, err
}
}
return &Result{
Matched: res.Matched,
Modified: res.Modified,
}, nil
}
// Delete will remove all matching documents from the namespace. Sort, skip and
// limit may be supplied to modify the result. The returned result will contain
// the matched documents.
func (t *Transaction) Delete(handle Handle, query, sort bsonkit.Doc, skip, limit int) (*Result, error) {
// acquire write lock
t.mutex.Lock()
defer t.mutex.Unlock()
// validate handle
err := handle.Validate(true)
if err != nil {
return nil, err
}
// check access
if handle[0] == Local {
return nil, fmt.Errorf("namespace local.* is read only")
}
// check namespace
if t.catalog.Namespaces[handle] == nil {
return &Result{}, nil
}
// clone catalog
clone := t.catalog.Clone()
// clone namespace
namespace := clone.Namespaces[handle].Clone()
clone.Namespaces[handle] = namespace
// clone oplog
oplog := clone.Namespaces[Oplog].Clone()
clone.Namespaces[Oplog] = oplog
// perform delete
res, err := t.delete(handle, oplog, namespace, query, sort, skip, limit)
if err != nil {
return nil, err
}
// set catalog and flag
if len(res.Matched) > 0 {
t.catalog = clone
t.dirty = true
}
return res, nil
}
func (t *Transaction) delete(handle Handle, oplog, namespace *mongokit.Collection, query, sort bsonkit.Doc, skip, limit int) (*Result, error) {
// perform delete
res, err := namespace.Delete(query, sort, skip, limit)
if err != nil {
return nil, err
}
// append oplog
for _, doc := range res.Matched {
err = t.append(oplog, handle, "delete", doc, nil)
if err != nil {
return nil, err
}
}
return &Result{
Matched: res.Matched,
}, nil
}
// Drop will return the namespace with the specified handle from the catalog.
// If the second part of the handle is empty, it will drop all namespaces
// matching the first part.
func (t *Transaction) Drop(handle Handle) error {
// acquire write lock
t.mutex.Lock()
defer t.mutex.Unlock()
// validate handle
err := handle.Validate(false)
if err != nil {
return err
}
// check access
if handle[0] == Local {
return fmt.Errorf("namespace local.* is read only")
}
// clone catalog
clone := t.catalog.Clone()
// clone oplog
oplog := clone.Namespaces[Oplog].Clone()
clone.Namespaces[Oplog] = oplog
// collect dropped
dropped := 0
// drop all matching namespaces
for ns := range clone.Namespaces {
if ns == handle || handle[1] == "" && ns[0] == handle[0] {
// delete namespace
delete(clone.Namespaces, ns)
dropped++
// append oplog
err = t.append(oplog, ns, "drop", nil, nil)
if err != nil {
return err
}
}
}
// append oplog if database has been dropped
if handle[1] == "" && dropped > 0 {
err = t.append(oplog, handle, "dropDatabase", nil, nil)
if err != nil {
return err
}
}
// set catalog and flag
if dropped > 0 {
t.catalog = clone
t.dirty = true
}
return nil
}
func (t *Transaction) append(oplog *mongokit.Collection, handle Handle, op string, doc bsonkit.Doc, changes *mongokit.Changes) error {
// get time
now := bsonkit.Now()
// prepare ns
ns := bson.M{"db": handle[0]}
if handle[1] != "" {
ns["coll"] = handle[1]
}
// prepare event
event := bson.M{
"ns": ns,
"_id": bson.M{
"ts": now,
},
"clusterTime": now,
"operationType": op,
}
// add document info
if doc != nil {
// add document key
event["documentKey"] = bson.M{
"_id": bsonkit.Get(doc, "_id"),
}
// add full document
if op == "insert" || op == "replace" || op == "update" {
event["fullDocument"] = *doc
}
}
// add changes
if changes != nil {
// collect updated and removed fields
updated := make(map[string]interface{}, len(changes.Changed))
removed := make([]string, 0, len(changes.Changed))
for path, val := range changes.Changed {
if val == bsonkit.Missing {
removed = append(removed, path)
} else {
updated[path] = val
}
}
// set description
event["updateDescription"] = bson.M{
"updatedFields": updated,
"removedFields": removed,
"truncatedArrays": bson.A{},
}
}
// insert event
_, err := oplog.Insert(bsonkit.MustConvert(event))
if err != nil {
return err
}
return nil
}
// ListDatabases will return a list of all databases in the catalog.
func (t *Transaction) ListDatabases(query bsonkit.Doc) (bsonkit.List, error) {
// acquire read lock
t.mutex.RLock()
defer t.mutex.RUnlock()
// sort namespaces
sort := map[string][]*mongokit.Collection{}
for ns, namespace := range t.catalog.Namespaces {
sort[ns[0]] = append(sort[ns[0]], namespace)
}
// prepare list
var list bsonkit.List
for name, nss := range sort {
// check emptiness
empty := true
for _, ns := range nss {
if len(ns.Documents.List) > 0 {
empty = false
}
}
// add specification
list = append(list, &bson.D{
bson.E{Key: "name", Value: name},
bson.E{Key: "sizeOnDisk", Value: 0},
bson.E{Key: "empty", Value: empty},
})
}
// filter list
list, err := mongokit.Filter(list, query, 0)
if err != nil {
return nil, err
}
return list, nil
}
// ListCollections will return a list of all collections in the specified db.
func (t *Transaction) ListCollections(handle Handle, query bsonkit.Doc) (bsonkit.List, error) {
// acquire read lock
t.mutex.RLock()
defer t.mutex.RUnlock()
// validate handle
err := handle.Validate(false)
if err != nil {
return nil, err
}
// prepare list
list := make(bsonkit.List, 0, len(t.catalog.Namespaces))
// add documents
for ns := range t.catalog.Namespaces {
if ns[0] == handle[0] {
list = append(list, &bson.D{
bson.E{Key: "name", Value: ns[1]},
bson.E{Key: "type", Value: "collection"},
bson.E{Key: "options", Value: bson.D{}},
bson.E{Key: "info", Value: bson.D{
bson.E{Key: "uuid", Value: ns.String()},
bson.E{Key: "readOnly", Value: false},
}},
bson.E{Key: "idIndex", Value: bson.D{
bson.E{Key: "v", Value: 2},
bson.E{Key: "key", Value: bson.D{
bson.E{Key: "_id", Value: 1},
}},
bson.E{Key: "name", Value: "_id_"},
bson.E{Key: "namespace", Value: ns.String()},
}},
})
}
}
// filter list
list, err = mongokit.Filter(list, query, 0)
if err != nil {
return nil, err
}
return list, nil
}
// CountDocuments will return the number of documents in the specified namespace.
func (t *Transaction) CountDocuments(handle Handle) (int, error) {
// acquire read lock
t.mutex.RLock()
defer t.mutex.RUnlock()
// validate handle
err := handle.Validate(true)
if err != nil {
return 0, err
}
// check namespace
namespace, ok := t.catalog.Namespaces[handle]
if !ok {
return 0, nil
}
return len(namespace.Documents.List), nil
}
// ListIndexes will return a list of indexes in the specified namespace.
func (t *Transaction) ListIndexes(handle Handle) (bsonkit.List, error) {
// acquire read lock
t.mutex.RLock()
defer t.mutex.RUnlock()
// validate handle
err := handle.Validate(true)
if err != nil {
return nil, err
}
// check namespace
if t.catalog.Namespaces[handle] == nil {
return nil, nil
}
// get namespace
namespace := t.catalog.Namespaces[handle]
// prepare list
var list bsonkit.List
for name, index := range namespace.Indexes {
// get config
config := index.Config()
// create spec
spec := bson.D{
bson.E{Key: "v", Value: 2},
bson.E{Key: "key", Value: *config.Key},
bson.E{Key: "name", Value: name},
}
// add unique
if config.Unique && name != "_id_" {
spec = append(spec, bson.E{Key: "unique", Value: true})
}
// add partial
if config.Partial != nil {
spec = append(spec, bson.E{Key: "partialFilterExpression", Value: *config.Partial})
}
// add expiry
if config.Expiry > 0 {
spec = append(spec, bson.E{Key: "expireAfterSeconds", Value: int32(config.Expiry / time.Second)})
}
// add specification
list = append(list, &spec)
}
// sort list
bsonkit.Sort(list, []bsonkit.Column{
{Path: "name"},
}, true)
return list, nil
}
// CreateIndex will create the specified index in the specified namespace. It
// is a no-op if an index with the same name and configuration already exists.
func (t *Transaction) CreateIndex(handle Handle, name string, config mongokit.IndexConfig) (string, error) {
// acquire write lock
t.mutex.Lock()
defer t.mutex.Unlock()
// validate handle
err := handle.Validate(true)
if err != nil {
return "", err
}
// check access
if handle[0] == Local {
return "", fmt.Errorf("namespace local.* is read only")
}
// clone catalog
clone := t.catalog.Clone()
// create or clone namespace
var namespace *mongokit.Collection
if clone.Namespaces[handle] == nil {
namespace = mongokit.NewCollection(true)
clone.Namespaces[handle] = namespace
} else {
namespace = clone.Namespaces[handle].Clone()
clone.Namespaces[handle] = namespace
}
// create index
name, err = namespace.CreateIndex(name, config)
if err != nil {
return "", err
}
// set catalog and flag
t.catalog = clone
t.dirty = true
return name, nil
}
// DropIndex will drop the specified index in the specified namespace.
func (t *Transaction) DropIndex(handle Handle, name string) error {
// acquire write lock
t.mutex.Lock()
defer t.mutex.Unlock()
// validate handle
err := handle.Validate(true)
if err != nil {
return err
}
// check access
if handle[0] == Local {
return fmt.Errorf("namespace local.* is read only")
}
// check namespace
if t.catalog.Namespaces[handle] == nil {
return fmt.Errorf("missing namespace %q", handle.String())
}
// clone catalog
clone := t.catalog.Clone()
// clone namespace
namespace := clone.Namespaces[handle].Clone()
clone.Namespaces[handle] = namespace
// drop index
dropped, err := namespace.DropIndex(name)
if err != nil {
return err
}
// set catalog and flag
if len(dropped) > 0 {
t.catalog = clone
t.dirty = true
}
return nil
}
// Dirty will return whether the transaction contains changes.
func (t *Transaction) Dirty() bool {
// acquire read lock
t.mutex.RLock()