forked from intervention-engine/fhir
/
mongo_data_access.go
1273 lines (1094 loc) · 40 KB
/
mongo_data_access.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 server
import (
"context"
"fmt"
"net/url"
"runtime"
"strconv"
"strings"
"time"
"github.com/eug48/fhir/models"
"github.com/eug48/fhir/models2"
"github.com/eug48/fhir/search"
"github.com/golang/glog"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
mongowrapper "github.com/opencensus-integrations/gomongowrapper"
)
type mongoDataAccessLayer struct {
client *mongowrapper.WrappedClient
defaultDbName string
enableMultiDB bool
dbSuffix string
Interceptors map[string]InterceptorList
countTotalResults bool
enableCISearches bool
tokenParametersCaseSensitive bool
enableHistory bool
readonly bool
}
type mongoSession struct {
session mongo.Session
context mongo.SessionContext
db *mongowrapper.WrappedDatabase
dal *mongoDataAccessLayer
inTransaction bool
}
func (dal *mongoDataAccessLayer) StartSession(ctx context.Context, customDbName string) DataAccessSession {
opts := options.Session()
opts.SetCausalConsistency(true)
opts.SetDefaultReadConcern(readconcern.Majority())
opts.SetDefaultWriteConcern(writeconcern.New(writeconcern.WMajority(), writeconcern.J(true)))
session, err := dal.client.StartSession(opts)
if err != nil {
panic(errors.Wrap(err, "StartSession failed"))
}
var dbName string
if dal.enableMultiDB && customDbName != "" {
if dal.dbSuffix != "" && !strings.HasSuffix(customDbName, dal.dbSuffix) {
panic(errors.Wrapf(err, "database name (%s) doesn't end with suffix (%s)", customDbName, dal.dbSuffix))
}
dbName = customDbName
} else {
dbName = dal.defaultDbName
}
db := dal.client.Database(dbName)
if db == nil {
panic(errors.Wrap(err, "client.Database failed"))
}
var contextWithSession mongo.SessionContext
wrappedSession := session.(*mongowrapper.WrappedSession) // unwrap - mongo's sessionFromContext wants its own session impl
mongo.WithSession(ctx, wrappedSession.Session, func(sc mongo.SessionContext) error {
// hack to work around this closure-based API
// WithSession just calls the callback and does nothing else.. (at least in MongoDB Go Driver 1.0.3)
contextWithSession = sc
return nil
})
return &mongoSession{
session: session,
context: contextWithSession,
db: db,
inTransaction: false,
dal: dal,
}
}
func (ms *mongoSession) CurrentVersionCollection(resourceType string) *mongowrapper.WrappedCollection {
return ms.db.Collection(models.PluralizeLowerResourceName(resourceType))
}
func (ms *mongoSession) PreviousVersionsCollection(resourceType string) *mongowrapper.WrappedCollection {
return ms.db.Collection(models.PluralizeLowerResourceName(resourceType) + "_prev")
}
func (ms *mongoSession) StartTransaction() error {
if ms.inTransaction {
// sucess if already in a transaction
return nil
}
err := ms.session.StartTransaction()
glog.V(3).Infof("StartTransaction")
if err == nil {
ms.inTransaction = true
}
return errors.Wrap(err, "mongoSession.StartTransaction")
}
func (ms *mongoSession) CommmitIfTransaction() error {
if ms.inTransaction {
glog.V(3).Infof("CommmitTransaction")
err := ms.session.CommitTransaction(ms.context)
ms.inTransaction = false
return errors.Wrap(err, "mongoSession.CommmitIfTransaction")
} else {
return nil
}
}
func (ms *mongoSession) Finish() {
var err error
if ms.inTransaction {
err = ms.session.AbortTransaction(ms.context)
if err == nil {
glog.Warningf("AbortTransaction called from mongoSession.Finish")
ms.inTransaction = false
} else {
commandErr, ok := err.(mongo.CommandError)
if ok && commandErr.Name == "OperationNotSupportedInTransaction" {
// can ignore - occurs if we fail before issuing a command. msg is "Command is not supported as the first command in a transaction"
glog.V(5).Infof("ignoring failed AbortTransaction in mongoSession.Finish (%s %s)", commandErr.Name, commandErr.Message)
} else if ok && commandErr.Name == "NoSuchTransaction" {
/*
after any error in a transaction
AbortTransaction will fail with NoSuchTransaction
since the transaction gets aborted by the error
*/
glog.V(5).Infof("AbortTransaction in mongoSession.Finish failed: %T %v", err, err)
} else {
panic(fmt.Sprintf("AbortTransaction in mongoSession.Finish failed: %T %v", err, err))
}
}
}
ms.session.EndSession(ms.context)
}
// NewMongoDataAccessLayer returns an implementation of DataAccessLayer that is backed by a Mongo database
func NewMongoDataAccessLayer(client *mongowrapper.WrappedClient, defaultDbName string, enableMultiDB bool, dbSuffix string, interceptors map[string]InterceptorList, config Config) DataAccessLayer {
return &mongoDataAccessLayer{
client: client,
defaultDbName: defaultDbName,
enableMultiDB: enableMultiDB,
dbSuffix: dbSuffix,
Interceptors: interceptors,
countTotalResults: config.CountTotalResults,
enableCISearches: config.EnableCISearches,
tokenParametersCaseSensitive: config.TokenParametersCaseSensitive,
enableHistory: config.EnableHistory,
readonly: config.ReadOnly,
}
}
// InterceptorList is a list of interceptors registered for a given database operation
type InterceptorList []Interceptor
// Interceptor optionally executes functions on a specified resource type before and after
// a database operation involving that resource. To register an interceptor for ALL resource
// types use a "*" as the resourceType.
type Interceptor struct {
ResourceType string
Handler InterceptorHandler
}
// InterceptorHandler is an interface that defines three methods that are executed on a resource
// before the database operation, after the database operation SUCCEEDS, and after the database
// operation FAILS.
type InterceptorHandler interface {
Before(resource interface{})
After(resource interface{})
OnError(err error, resource interface{})
}
// invokeInterceptorsBefore invokes the interceptor list for the given resource type before a database
// operation occurs.
func (ms *mongoSession) invokeInterceptorsBefore(op, resourceType string, resource interface{}) {
for _, interceptor := range ms.dal.Interceptors[op] {
if interceptor.ResourceType == resourceType || interceptor.ResourceType == "*" {
interceptor.Handler.Before(resource)
}
}
}
// invokeInterceptorsAfter invokes the interceptor list for the given resource type after a database
// operation occurs and succeeds.
func (ms *mongoSession) invokeInterceptorsAfter(op, resourceType string, resource interface{}) {
for _, interceptor := range ms.dal.Interceptors[op] {
if interceptor.ResourceType == resourceType || interceptor.ResourceType == "*" {
interceptor.Handler.After(resource)
}
}
}
// invokeInterceptorsOnError invokes the interceptor list for the given resource type after a database
// operation occurs and fails.
func (ms *mongoSession) invokeInterceptorsOnError(op, resourceType string, err error, resource interface{}) {
for _, interceptor := range ms.dal.Interceptors[op] {
if interceptor.ResourceType == resourceType || interceptor.ResourceType == "*" {
interceptor.Handler.OnError(err, resource)
}
}
}
// hasInterceptorsForOpAndType checks if any interceptors are registered for a particular database operation AND resource type
func (ms *mongoSession) hasInterceptorsForOpAndType(op, resourceType string) bool {
if len(ms.dal.Interceptors[op]) > 0 {
for _, interceptor := range ms.dal.Interceptors[op] {
if interceptor.ResourceType == resourceType || interceptor.ResourceType == "*" {
// At least 1 interceptor is registered for this database operation and resource type
return true
}
}
}
return false
}
func (ms *mongoSession) Get(id, resourceType string) (resource *models2.Resource, err error) {
bsonID, err := convertIDToBsonID(id)
if err != nil {
return nil, ErrNotFound
}
collection := ms.CurrentVersionCollection(resourceType)
filter := bson.D{{"_id", bsonID.Hex()}}
var doc bson.D
err = collection.FindOne(ms.context, filter).Decode(&doc)
glog.V(3).Infof("Get %s/%s --> %s (err %+v)", resourceType, id, doc, err)
if err == mongo.ErrNoDocuments && ms.dal.enableHistory {
// check whether this is a deleted record
prevCollection := ms.PreviousVersionsCollection(resourceType)
prevQuery := bson.D{
{"_id._id", bsonID.Hex()},
{"_id._deleted", 1},
}
idOnly := bson.D{{"_id", 1}}
cursor, err := prevCollection.Find(ms.context, prevQuery, options.Find().SetLimit(1).SetProjection(idOnly))
if err != nil {
return nil, errors.Wrap(err, "Get --> prevCollection.Find")
}
deleted := cursor.Next(ms.context)
err = cursor.Err()
glog.V(3).Infof(" deleted version: %t (err %+v)", deleted, err)
if err != nil {
return nil, errors.Wrap(err, "Get --> prevCollection.Find --> cursor error")
}
if deleted {
return nil, ErrDeleted
}
return nil, ErrNotFound
}
if err != nil {
return nil, convertMongoErr(err)
}
resource, err = models2.NewResourceFromBSON(doc)
return
}
func (ms *mongoSession) GetVersion(id, versionIdStr, resourceType string) (resource *models2.Resource, err error) {
bsonID, err := convertIDToBsonID(id)
if err != nil {
return nil, ErrNotFound
}
versionIdInt, err := strconv.Atoi(versionIdStr)
if err != nil {
return nil, errors.Wrapf(err, "failed to convert versionId to an integer (%s)", versionIdStr)
}
// First assume versionId is for the current version
curQuery := bson.D{
{"_id", bsonID.Hex()},
{"meta.versionId", versionIdStr},
}
curCollection := ms.CurrentVersionCollection(resourceType)
var result bson.D
err = curCollection.FindOne(ms.context, curQuery).Decode(&result)
// glog.Debugf("GetVersion: curQuery=%+v; err=%+v\n", curQuery, err)
if err == mongo.ErrNoDocuments {
// try to search for previous versions
prevQuery := bson.D{
{"_id._id", bsonID.Hex()},
{"_id._version", int32(versionIdInt)},
}
prevCollection := ms.PreviousVersionsCollection(resourceType)
cur, err := prevCollection.Find(ms.context, prevQuery, options.Find().SetLimit(1))
if err != nil {
return nil, errors.Wrap(err, "GetVersion --> prevCollection.Find")
}
if cur.Next(ms.context) {
var prevDoc bson.Raw
err = cur.Decode(&prevDoc)
if err != nil {
return nil, errors.Wrap(err, "GetVersion --> prevCollection.Find --> Decode")
}
var deleted bool
deleted, resource, err = unmarshalPreviousVersion(&prevDoc)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal previous version")
}
if deleted {
return nil, ErrDeleted
} else {
return resource, nil
}
}
if err := cur.Err(); err != nil {
return nil, errors.Wrap(err, "GetVersion --> prevCollection.Find --> cursor error")
}
return nil, ErrNotFound
} else if err != nil {
return nil, errors.Wrap(convertMongoErr(err), "failed to search for current version")
} else {
resource, err = models2.NewResourceFromBSON(result)
}
return
}
// Convert document stored in one of the _prev collections into a resource
func unmarshalPreviousVersion(rawDoc *bson.Raw) (deleted bool, resource *models2.Resource, err error) {
// glog.Debugf("[unmarshalPreviousVersion] %+v\n", rawDoc)
// first we have to parse the vermongo-style id
idItem, err := rawDoc.IndexErr(0)
if err != nil {
return false, nil, fmt.Errorf("unmarshalPreviousVersion: input empty: %s", err)
}
if idItem.Key() != "_id" {
return false, nil, fmt.Errorf("unmarshalPreviousVersion: first element not an _id")
}
idValue, ok := idItem.Value().DocumentOK()
if !ok {
return false, nil, fmt.Errorf("unmarshalPreviousVersion: _id not a bson dictionary")
}
actualIdVal, err := idValue.LookupErr("_id")
if err != nil {
return false, nil, fmt.Errorf("unmarshalPreviousVersion: _id._id missing")
}
actualId, ok := actualIdVal.StringValueOK()
if !ok {
return false, nil, fmt.Errorf("unmarshalPreviousVersion: _id._id not a string")
}
// check if actually deleted
deletedVal, err := idValue.LookupErr("_deleted")
if err == nil {
deleted, ok := deletedVal.Int32OK()
if ok && deleted > 0 {
return true, nil, nil
}
}
// convert to a bson.D
var doc bson.D
err = bson.Unmarshal(*rawDoc, &doc)
if err != nil {
return false, nil, errors.Wrapf(err, "unmarshalPreviousVersion: unmarshal failed")
}
// replace first element with a string id
doc[0] = bson.E{"_id", actualId}
// convert to JSON
resource, err = models2.NewResourceFromBSON(doc)
if err != nil {
return false, nil, errors.Wrap(err, "unmarshalPreviousVersion: NewResourceFromBSON failed")
}
return false, resource, nil
}
func (ms *mongoSession) Post(resource *models2.Resource) (id string, err error) {
id = primitive.NewObjectID().Hex()
err = convertMongoErr(ms.PostWithID(id, resource))
return
}
func (ms *mongoSession) ConditionalPost(query search.Query, resource *models2.Resource) (httpStatus int, id string, outputResource *models2.Resource, err error) {
existingIds, err := ms.FindIDs(query)
if err != nil {
return
}
if len(existingIds) == 0 {
httpStatus = 201
id = primitive.NewObjectID().Hex()
err = convertMongoErr(ms.PostWithID(id, resource))
if err == nil {
outputResource = resource
}
} else if len(existingIds) == 1 {
httpStatus = 200
id = existingIds[0]
outputResource, err = ms.Get(id, query.Resource)
} else if len(existingIds) > 1 {
httpStatus = 412
}
return
}
func (ms *mongoSession) PostWithID(id string, resource *models2.Resource) error {
bsonID, err := convertIDToBsonID(id)
if err != nil {
return convertMongoErr(err)
}
resource.SetId(bsonID.Hex())
updateResourceMeta(resource, 1)
resourceType := resource.ResourceType()
curCollection := ms.CurrentVersionCollection(resourceType)
ms.invokeInterceptorsBefore("Create", resourceType, resource)
glog.V(3).Infof("PostWithID: inserting %s/%s", resourceType, id)
_, err = curCollection.InsertOne(ms.context, resource)
if err == nil {
ms.invokeInterceptorsAfter("Create", resourceType, resource)
} else {
ms.invokeInterceptorsOnError("Create", resourceType, err, resource)
}
return convertMongoErr(err)
}
func (ms *mongoSession) Put(id string, conditionalVersionId string, resource *models2.Resource) (createdNew bool, err error) {
bsonID, err := convertIDToBsonID(id)
if err != nil {
return false, convertMongoErr(err)
}
resourceType := resource.ResourceType()
curCollection := ms.CurrentVersionCollection(resourceType)
resource.SetId(bsonID.Hex())
if conditionalVersionId != "" {
glog.V(3).Infof("PUT %s/%s (If-Match %s)", resourceType, resource.Id(), conditionalVersionId)
} else {
glog.V(3).Infof("PUT %s/%s", resourceType, resource.Id())
}
var curVersionId *int = nil
var newVersionId = 1
var start time.Time
if ms.dal.enableHistory == false {
if conditionalVersionId != "" {
return false, errors.Errorf("If-Match specified for a conditional put, but version histories are disabled")
}
glog.V(3).Infof(" versionIds: history disabled; new %d", newVersionId)
} else {
// get current version of this document
if glog.V(5) {
start = time.Now()
}
var currentDoc bson.D
var currentDocRaw bson.Raw
currentDocQuery := bson.D{{"_id", bsonID.Hex()}}
if err = curCollection.FindOne(ms.context, currentDocQuery).Decode(¤tDocRaw); err != nil && err != mongo.ErrNoDocuments {
return false, errors.Wrap(convertMongoErr(err), "Put handler: error retrieving current version")
}
if glog.V(5) {
glog.V(5).Infof("get_current_version took %v", time.Since(start))
}
if err == mongo.ErrNoDocuments {
if conditionalVersionId != "" {
return false, ErrConflict{msg: "If-Match specified for a resource that doesn't exist"}
}
glog.V(3).Infof(" versionIds: no current; new %d", newVersionId)
} else {
// unmarshal fully
err = bson.Unmarshal(currentDocRaw, ¤tDoc)
if err != nil {
return false, errors.Wrap(convertMongoErr(err), "Put: error unmarshalling current version")
}
hasVersionId, curVersionIdTemp, curVersionIdStr := getVersionIdFromResource(¤tDocRaw)
if hasVersionId {
newVersionId = curVersionIdTemp + 1
} else {
// for documents created by previous versions not supporting versioning or if it was disabled
newVersionId = 1
curVersionIdTemp = 0
}
curVersionId = &curVersionIdTemp
glog.V(3).Infof(" versionIds: current %d; new %d", *curVersionId, newVersionId)
if conditionalVersionId != "" && conditionalVersionId != curVersionIdStr {
return false, ErrConflict{msg: "If-Match doesn't match current versionId"}
}
// store current document in the previous version collection, adding its versionId to
// its mongo _id like in vermongo (https://github.com/thiloplanz/v7files/wiki/Vermongo)
// i.e. { "_id" : { "_id" : ObjectId("4c78da..."), "_version" : "2" }
setVermongoId(¤tDoc, curVersionIdTemp)
// NOTE: currentDoc._id modified in-place
prevCollection := ms.PreviousVersionsCollection(resourceType)
vermongoIdField := bson.D{currentDoc[0]}
// TODO: figure out why ReplaceOne isn't working (FindOneAndReplace returns a doc even though we don't need it)
res := prevCollection.FindOneAndReplace(ms.context, &vermongoIdField, ¤tDoc, options.FindOneAndReplace().SetUpsert(true).SetReturnDocument(options.Before))
err = res.Err()
// _, err := prevCollection.ReplaceOne(ms.context, &vermongoIdField, ¤tDoc, options.Replace().SetUpsert(true))
// if err != nil {
if err != nil && strings.Contains(err.Error(), "duplicate key") {
return false, ErrConflict{msg: fmt.Sprintf("duplicate key storing previous version for %s/%s", resourceType, id)}
}
if err != nil && err != mongo.ErrNoDocuments {
return false, errors.Wrap(convertMongoErr(err), "failed to store previous version")
}
}
}
updateResourceMeta(resource, newVersionId)
if ms.hasInterceptorsForOpAndType("Update", resourceType) {
oldResource, getError := ms.Get(id, resourceType)
if getError == nil {
ms.invokeInterceptorsBefore("Update", resourceType, oldResource)
}
}
var updated int64
if curVersionId == nil {
var info *mongo.UpdateResult
selector := bson.D{{"_id", bsonID.Hex()}}
if glog.V(5) {
start = time.Now()
}
info, err = curCollection.ReplaceOne(ms.context, selector, resource, options.Replace().SetUpsert(true))
if glog.V(5) {
glog.V(3).Infof(" upsert %#v took %v", selector, time.Since(start))
}
if err != nil {
bson, err2 := resource.GetBSON()
if err2 != nil {
panic(err2)
}
err = errors.Wrapf(err, "PUT handler: failed to upsert new document: %#v --> %s %#v", selector, resource.JsonBytes(), bson)
} else {
updated = info.ModifiedCount
}
} else {
// atomic check-then-update
selector := bson.D{
{"_id", bsonID.Hex()},
{"meta.versionId", strconv.Itoa(*curVersionId)},
}
if *curVersionId == 0 {
// cur doc won't actually have a versionId field
selector[1] = bson.E{"meta.versionId", bson.D{{"$exists", false}}}
}
var updateOneInfo *mongo.UpdateResult
if glog.V(5) {
start = time.Now()
}
updateOneInfo, err = curCollection.ReplaceOne(ms.context, selector, resource)
if glog.V(5) {
glog.V(3).Infof(" update %#v --> %#v (err %#v) took %v", selector, updateOneInfo, err, time.Since(start))
}
if err != nil {
err = errors.Wrap(err, "PUT handler: failed to update current document")
} else if updateOneInfo.ModifiedCount == 0 {
return false, ErrConflict{msg: fmt.Sprintf("conflicting update for %+v", selector)}
}
updated = 1
}
if updated == 0 {
glog.V(3).Infof(" created new")
} else {
glog.V(3).Infof(" updated %d", updated)
}
if err == nil {
createdNew = (updated == 0)
if createdNew {
ms.invokeInterceptorsAfter("Create", resourceType, resource)
} else {
ms.invokeInterceptorsAfter("Update", resourceType, resource)
}
} else {
ms.invokeInterceptorsOnError("Update", resourceType, err, resource)
}
return createdNew, convertMongoErr(err)
}
func getVersionIdFromResource(doc *bson.Raw) (hasVersionId bool, versionIdInt int, versionIdStr string) {
versionId, err := doc.LookupErr("meta", "versionId")
if err == bsoncore.ErrElementNotFound {
return false, -1, ""
} else if err != nil {
panic(errors.Wrap(err, "getVersionIdFromResource LookupErr failed"))
}
hasVersionId = true
var isString bool
versionIdStr, isString = versionId.StringValueOK()
if !isString {
panic(errors.Errorf("meta.versionId is not a BSON string"))
}
versionIdInt, err = strconv.Atoi(versionIdStr)
if err == nil {
return
} else {
panic(errors.Errorf("meta.versionId BSON string is not an integer: %s", versionIdStr))
}
}
// Updates the doc to use a vermongo-like _id (_id: current_id, _version: versionId)
func setVermongoId(doc *bson.D, versionIdInt int) {
idItem := &((*doc)[0])
if idItem == nil || idItem.Key != "_id" {
panic("_id field not first in bson document")
}
newId := bson.D{
{"_id", idItem.Value},
{"_version", int32(versionIdInt)},
}
(*doc)[0] = bson.E{"_id", newId}
}
func (ms *mongoSession) ConditionalPut(query search.Query, conditionalVersionId string, resource *models2.Resource) (id string, createdNew bool, err error) {
if IDs, err := ms.FindIDs(query); err == nil {
switch len(IDs) {
case 0:
id = primitive.NewObjectID().Hex()
case 1:
id = IDs[0]
default:
return "", false, &ErrMultipleMatches{msg: fmt.Sprintf("Multiple matches for %s?%s", query.Resource, query.Query)}
}
} else {
return "", false, err
}
createdNew, err = ms.Put(id, conditionalVersionId, resource)
return id, createdNew, err
}
func (ms *mongoSession) Delete(id, resourceType string) (newVersionId string, err error) {
bsonID, err := convertIDToBsonID(id)
if err != nil {
return "", ErrNotFound
}
curCollection := ms.CurrentVersionCollection(resourceType)
prevCollection := ms.PreviousVersionsCollection(resourceType)
if ms.dal.enableHistory {
newVersionId, err = saveDeletionIntoHistory(resourceType, bsonID.Hex(), curCollection, prevCollection, ms)
if err == mongo.ErrNoDocuments {
return "", ErrNotFound
} else if err != nil {
return "", errors.Wrap(err, "failed to save deletion into history")
}
}
var resource interface{}
var getError error
hasInterceptor := ms.hasInterceptorsForOpAndType("Delete", resourceType)
if hasInterceptor {
// Although this is a delete operation we need to get the resource first so we can
// run any interceptors on the resource before it's deleted.
resource, getError = ms.Get(id, resourceType)
ms.invokeInterceptorsBefore("Delete", resourceType, resource)
}
filter := bson.D{{"_id", bsonID.Hex()}}
deleteInfo, err := curCollection.DeleteOne(ms.context, filter)
glog.V(3).Infof(" deleteInfo: %+v (err %+v)", deleteInfo, err)
if deleteInfo.DeletedCount == 0 && err == nil {
err = mongo.ErrNoDocuments
}
if hasInterceptor {
if err == nil && getError == nil {
ms.invokeInterceptorsAfter("Delete", resourceType, resource)
} else {
ms.invokeInterceptorsOnError("Delete", resourceType, err, resource)
}
}
err = convertMongoErr(err)
return
}
func saveDeletionIntoHistory(resourceType string, id string, curCollection *mongowrapper.WrappedCollection, prevCollection *mongowrapper.WrappedCollection, ms *mongoSession) (newVersionIdStr string, err error) {
// get current version of this document
var currentDoc bson.D
var currentDocRaw bson.Raw
currentDocQuery := bson.D{{"_id", id}}
err = curCollection.FindOne(ms.context, currentDocQuery).Decode(¤tDocRaw)
if err == mongo.ErrNoDocuments {
return "", err
} else if err != nil {
return "", errors.Wrap(convertMongoErr(err), "saveDeletionIntoHistory: error retrieving current version")
} else {
// unmarshal fully
err = bson.Unmarshal(currentDocRaw, ¤tDoc)
if err != nil {
return "", errors.Wrap(convertMongoErr(err), "saveDeletionIntoHistory: error unmarshalling current version")
}
// extract current version
hasVersionId, curVersionId, _ := getVersionIdFromResource(¤tDocRaw)
var newVersionId int
if hasVersionId {
newVersionId = curVersionId + 1
} else {
// document created by previous versions not supporting versioning or if it was disabled
newVersionId = 1
curVersionId = 0
}
newVersionIdStr = strconv.Itoa(newVersionId)
// store current document in the previous version collection, adding its versionId to
// its mongo _id like in vermongo (https://github.com/thiloplanz/v7files/wiki/Vermongo)
// i.e. { "_id" : { "_id" : ObjectId("4c78da..."), "_version" : "2" }
setVermongoId(¤tDoc, curVersionId)
// NOTE: currentDoc._id modified in-place
vermongoIdField := bson.D{currentDoc[0]}
// TODO: figure out why ReplaceOne isn't working (FindOneAndReplace returns a doc even though we don't need it)
res := prevCollection.FindOneAndReplace(ms.context, &vermongoIdField, ¤tDoc, options.FindOneAndReplace().SetUpsert(true).SetReturnDocument(options.Before))
err = res.Err()
if err == mongo.ErrNoDocuments {
err = nil
}
if err != nil && strings.Contains(err.Error(), "duplicate key") {
return "", ErrConflict{msg: fmt.Sprintf("Delete handler: duplicate key storing previous version for %s/%s", resourceType, id)}
}
if err != nil {
return "", errors.Wrap(convertMongoErr(err), "Delete handler: failed to store previous version")
}
// create a deletion record
now := time.Now()
deletionRecord := bson.D{
{"_id", bson.D{
{"_id", id},
{"_version", int32(newVersionId)},
{"_deleted", 1},
}},
{"meta", bson.D{
{"versionId", newVersionIdStr},
{"lastUpdated", now},
}},
}
vermongoIdField = bson.D{deletionRecord[0]}
// TODO: figure out why ReplaceOne isn't working (FindOneAndReplace returns a doc even though we don't need it)
res = prevCollection.FindOneAndReplace(ms.context, &vermongoIdField, &deletionRecord, options.FindOneAndReplace().SetUpsert(true).SetReturnDocument(options.Before))
err = res.Err()
if err == mongo.ErrNoDocuments {
err = nil
}
if err != nil && strings.Contains(err.Error(), "duplicate key") {
return "", ErrConflict{msg: fmt.Sprintf("Delete handler: duplicate key storing deletion marker for %s/%s", resourceType, id)}
}
if err != nil {
return "", errors.Wrap(convertMongoErr(err), "Delete handler: failed to store deletion marker")
}
}
return
}
func (ms *mongoSession) ConditionalDelete(query search.Query) (count int64, err error) {
IDsToDelete, err := ms.FindIDs(query)
if err != nil {
return 0, err
}
// There is the potential here for the delete to fail if the slice of IDs
// is too large (exceeding Mongo's 16MB document size limit).
deleteQuery := bson.D{
{"_id", bson.D{
{"$in", IDsToDelete},
},
},
}
resourceType := query.Resource
curCollection := ms.CurrentVersionCollection(resourceType)
prevCollection := ms.PreviousVersionsCollection(resourceType)
hasInterceptors := ms.hasInterceptorsForOpAndType("Delete", resourceType)
if hasInterceptors || ms.dal.enableHistory {
/* Interceptors for a conditional delete are tricky since an interceptor is only run
AFTER the database operation and only on resources that were SUCCESSFULLY deleted. We use
the following approach:
1. Bulk delete those resources by ID
2. Search again using the SAME query, to verify that those resources were in fact deleted
3. Run the interceptor(s) on all resources that ARE NOT in the second search (since they were truly deleted)
*/
// get the resources that are about to be deleted
bundle, err := ms.Search(url.URL{}, query) // the baseURL argument here does not matter
if err == nil {
for _, elem := range bundle.Entry {
if hasInterceptors {
ms.invokeInterceptorsBefore("Delete", resourceType, elem.Resource)
}
}
for _, elem := range bundle.Entry {
if ms.dal.enableHistory {
id := elem.Resource.Id()
_, err = saveDeletionIntoHistory(resourceType, id, curCollection, prevCollection, ms)
if err != nil {
return count, errors.Wrapf(err, "failed to save deletion into history (%s/%s)", resourceType, id)
}
}
}
// Do the bulk delete by ID.
info, err := curCollection.DeleteMany(ms.context, deleteQuery)
deletedIds := make([]string, len(IDsToDelete))
if info != nil {
count = info.DeletedCount
}
if err != nil {
if hasInterceptors {
for _, elem := range bundle.Entry {
ms.invokeInterceptorsOnError("Delete", resourceType, err, elem.Resource)
}
}
return count, convertMongoErr(err)
} else if hasInterceptors == false {
return count, nil
}
var searchErr error
if count < int64(len(IDsToDelete)) {
// Some but not all resources were removed, so use the original search query
// to see which resources are left.
var failBundle *models2.ShallowBundle
failBundle, searchErr = ms.Search(url.URL{}, query)
deletedIds = setDiff(IDsToDelete, getResourceIdsFromBundle(failBundle))
} else {
// All resources were successfully removed
deletedIds = IDsToDelete
}
if searchErr == nil {
for _, elem := range bundle.Entry {
id := elem.Resource.Id()
if elementInSlice(id, deletedIds) {
// This resource was confirmed deleted
ms.invokeInterceptorsAfter("Delete", resourceType, elem.Resource)
} else {
// This resource was not confirmed deleted, which is an error
resourceErr := fmt.Errorf("ConditionalDelete: failed to delete resource %s with ID %s", resourceType, id)
ms.invokeInterceptorsOnError("Delete", resourceType, resourceErr, elem.Resource)
}
}
}
}
return count, convertMongoErr(err)
} else {
// do the bulk delete the usual way
info, err := curCollection.DeleteMany(ms.context, deleteQuery)
if info != nil {
count = info.DeletedCount
}
return count, convertMongoErr(err)
}
}
func (ms *mongoSession) History(baseURL url.URL, resourceType string, id string) (bundle *models2.ShallowBundle, err error) {
// check id
_, err = convertIDToBsonID(id)
if err != nil {
return nil, ErrNotFound
}
baseURLstr := baseURL.String()
if !strings.HasSuffix(baseURLstr, "/") {
baseURLstr = baseURLstr + "/"
}
fullUrl := baseURLstr + id
curCollection := ms.CurrentVersionCollection(resourceType)
prevCollection := ms.PreviousVersionsCollection(resourceType)
var entryList []models2.ShallowBundleEntryComponent
makeEntryRequest := func(method string) *models.BundleEntryRequestComponent {
return &models.BundleEntryRequestComponent{
Url: resourceType + "/" + id,
Method: method,
}
}
// add current version
var curDoc bson.D
curDocQuery := bson.D{{"_id", id}}
err = curCollection.FindOne(ms.context, curDocQuery).Decode(&curDoc)
if err == nil {
var entry models2.ShallowBundleEntryComponent
entry.FullUrl = fullUrl
entry.Resource, err = models2.NewResourceFromBSON(curDoc)
if err != nil {
return nil, errors.Wrap(err, "History: NewResourceFromBSON failed")
}
entry.Request = makeEntryRequest("PUT")
entryList = append(entryList, entry)
} else if err != mongo.ErrNoDocuments {
return nil, err
}
// sort - oldest versions last
prevDocsQuery := bson.D{{"_id._id", id}}
prevDocsSort := options.Find().SetSort(bson.D{{"_id._version", -1}})
cursor, err := prevCollection.Find(ms.context, prevDocsQuery, prevDocsSort)
if err != nil {
return nil, errors.Wrap(err, "History: prevCollection.Find failed")
}
for cursor.Next(ms.context) {
var prevDocBson bson.Raw
err = cursor.Decode(&prevDocBson)
glog.V(8).Infof("History: decoded prev document: %s", prevDocBson.String())
if err != nil {
return nil, errors.Wrap(err, "History: cursor.Decode failed")
}
var entry models2.ShallowBundleEntryComponent
entry.FullUrl = fullUrl
deleted, resource, err := unmarshalPreviousVersion(&prevDocBson)
if err != nil {
return nil, errors.Wrap(err, "History: unmarshalPreviousVersion failed")
}
if deleted {
entry.Request = makeEntryRequest("DELETE")
} else {
entry.Resource = resource
entry.Request = makeEntryRequest("PUT")
}
entryList = append(entryList, entry)
}
if err := cursor.Err(); err != nil {
return nil, errors.Wrap(err, "History: MongoDB query for previous versions failed")
}
totalDocs := uint32(len(entryList))
if totalDocs == 0 {
return nil, ErrNotFound
}