-
Notifications
You must be signed in to change notification settings - Fork 8
/
wrapped_database.go
90 lines (74 loc) · 2.72 KB
/
wrapped_database.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
// Copyright 2018, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mongowrapper
import (
"context"
"sync"
"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/readpref"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
)
type WrappedDatabase struct {
mu sync.Mutex
db *mongo.Database
}
func (wd *WrappedDatabase) Client() *WrappedClient {
wd.mu.Lock()
defer wd.mu.Unlock()
cc := wd.db.Client()
if cc == nil {
return nil
}
return &WrappedClient{cc: cc}
}
func (wd *WrappedDatabase) Collection(name string, opts ...*options.CollectionOptions) *WrappedCollection {
if wd.db == nil {
return nil
}
coll := wd.db.Collection(name, opts...)
if coll == nil {
return nil
}
return &WrappedCollection{coll: coll}
}
func (wd *WrappedDatabase) Drop(ctx context.Context) error {
ctx, span := roundtripTrackingSpan(ctx, "go.mongodb.org/mongo-driver.Database.Drop")
defer span.end(ctx)
err := wd.db.Drop(ctx)
if err != nil {
span.setError(err)
}
return err
}
func (wd *WrappedDatabase) ListCollections(ctx context.Context, filter interface{}, opts ...*options.ListCollectionsOptions) (*mongo.Cursor, error) {
ctx, span := roundtripTrackingSpan(ctx, "go.mongodb.org/mongo-driver.Database.ListCollections")
defer span.end(ctx)
cur, err := wd.db.ListCollections(ctx, filter, opts...)
if err != nil {
span.setError(err)
}
return cur, err
}
func (wd *WrappedDatabase) Name() string { return wd.db.Name() }
func (wd *WrappedDatabase) ReadConcern() *readconcern.ReadConcern { return wd.db.ReadConcern() }
func (wd *WrappedDatabase) ReadPreference() *readpref.ReadPref { return wd.db.ReadPreference() }
func (wd *WrappedDatabase) RunCommand(ctx context.Context, runCommand interface{}, opts ...*options.RunCmdOptions) *mongo.SingleResult {
ctx, span := roundtripTrackingSpan(ctx, "go.mongodb.org/mongo-driver.Database.RunCommand")
defer span.end(ctx)
return wd.db.RunCommand(ctx, runCommand, opts...)
}
func (wd *WrappedDatabase) WriteConcern() *writeconcern.WriteConcern { return wd.db.WriteConcern() }
func (wd *WrappedDatabase) Database() *mongo.Database { return wd.db }