forked from mongodb/mongo-go-driver
/
database.go
282 lines (235 loc) · 7.54 KB
/
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
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
// Copyright (C) MongoDB, Inc. 2017-present.
//
// 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
package mongo
import (
"context"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"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"
"go.mongodb.org/mongo-driver/x/mongo/driver"
"go.mongodb.org/mongo-driver/x/network/command"
"go.mongodb.org/mongo-driver/x/network/description"
)
// Database performs operations on a given database.
type Database struct {
client *Client
name string
readConcern *readconcern.ReadConcern
writeConcern *writeconcern.WriteConcern
readPreference *readpref.ReadPref
readSelector description.ServerSelector
writeSelector description.ServerSelector
registry *bsoncodec.Registry
}
func newDatabase(client *Client, name string, opts ...*options.DatabaseOptions) *Database {
dbOpt := options.MergeDatabaseOptions(opts...)
rc := client.readConcern
if dbOpt.ReadConcern != nil {
rc = dbOpt.ReadConcern
}
rp := client.readPreference
if dbOpt.ReadPreference != nil {
rp = dbOpt.ReadPreference
}
wc := client.writeConcern
if dbOpt.WriteConcern != nil {
wc = dbOpt.WriteConcern
}
db := &Database{
client: client,
name: name,
readPreference: rp,
readConcern: rc,
writeConcern: wc,
registry: client.registry,
}
db.readSelector = description.CompositeSelector([]description.ServerSelector{
description.ReadPrefSelector(db.readPreference),
description.LatencySelector(db.client.localThreshold),
})
db.writeSelector = description.CompositeSelector([]description.ServerSelector{
description.WriteSelector(),
description.LatencySelector(db.client.localThreshold),
})
return db
}
// Client returns the Client the database was created from.
func (db *Database) Client() *Client {
return db.client
}
// Name returns the name of the database.
func (db *Database) Name() string {
return db.name
}
// Collection gets a handle for a given collection in the database.
func (db *Database) Collection(name string, opts ...*options.CollectionOptions) *Collection {
return newCollection(db, name, opts...)
}
func (db *Database) processRunCommand(ctx context.Context, cmd interface{}, opts ...*options.RunCmdOptions) (command.Read,
description.ServerSelector, error) {
if ctx == nil {
ctx = context.Background()
}
sess := sessionFromContext(ctx)
runCmd := options.MergeRunCmdOptions(opts...)
if err := db.client.validSession(sess); err != nil {
return command.Read{}, nil, err
}
rp := runCmd.ReadPreference
if rp == nil {
if sess != nil && sess.TransactionRunning() {
rp = sess.CurrentRp // override with transaction read pref if specified
}
if rp == nil {
rp = readpref.Primary() // set to primary if nothing specified in options
}
}
runCmdDoc, err := transformDocument(db.registry, cmd)
if err != nil {
return command.Read{}, nil, err
}
readSelect := description.CompositeSelector([]description.ServerSelector{
description.ReadPrefSelector(rp),
description.LatencySelector(db.client.localThreshold),
})
return command.Read{
DB: db.Name(),
Command: runCmdDoc,
ReadPref: rp,
Session: sess,
Clock: db.client.clock,
}, readSelect, nil
}
// RunCommand runs a command on the database. A user can supply a custom
// context to this method, or nil to default to context.Background().
func (db *Database) RunCommand(ctx context.Context, runCommand interface{}, opts ...*options.RunCmdOptions) *SingleResult {
if ctx == nil {
ctx = context.Background()
}
readCmd, readSelect, err := db.processRunCommand(ctx, runCommand, opts...)
if err != nil {
return &SingleResult{err: err}
}
doc, err := driver.Read(ctx,
readCmd,
db.client.topology,
readSelect,
db.client.id,
db.client.topology.SessionPool,
)
return &SingleResult{err: replaceErrors(err), rdr: doc, reg: db.registry}
}
// RunCommandCursor runs a command on the database and returns a cursor over the resulting reader. A user can supply
// a custom context to this method, or nil to default to context.Background().
func (db *Database) RunCommandCursor(ctx context.Context, runCommand interface{}, opts ...*options.RunCmdOptions) (*Cursor, error) {
if ctx == nil {
ctx = context.Background()
}
readCmd, readSelect, err := db.processRunCommand(ctx, runCommand, opts...)
if err != nil {
return nil, err
}
batchCursor, err := driver.ReadCursor(
ctx,
readCmd,
db.client.topology,
readSelect,
db.client.id,
db.client.topology.SessionPool,
)
if err != nil {
return nil, replaceErrors(err)
}
cursor, err := newCursor(batchCursor, db.registry)
return cursor, replaceErrors(err)
}
// Drop drops this database from mongodb.
func (db *Database) Drop(ctx context.Context) error {
if ctx == nil {
ctx = context.Background()
}
sess := sessionFromContext(ctx)
err := db.client.validSession(sess)
if err != nil {
return err
}
cmd := command.DropDatabase{
DB: db.name,
Session: sess,
Clock: db.client.clock,
}
_, err = driver.DropDatabase(
ctx, cmd,
db.client.topology,
db.writeSelector,
db.client.id,
db.client.topology.SessionPool,
)
if err != nil && !command.IsNotFound(err) {
return replaceErrors(err)
}
return nil
}
// ListCollections list collections from mongodb database.
func (db *Database) ListCollections(ctx context.Context, filter interface{}, opts ...*options.ListCollectionsOptions) (*Cursor, error) {
if ctx == nil {
ctx = context.Background()
}
sess := sessionFromContext(ctx)
err := db.client.validSession(sess)
if err != nil {
return nil, err
}
filterDoc, err := transformDocument(db.registry, filter)
if err != nil {
return nil, err
}
cmd := command.ListCollections{
DB: db.name,
Filter: filterDoc,
ReadPref: readpref.Primary(), // list collections must be run on a primary by default
Session: sess,
Clock: db.client.clock,
}
readSelector := description.CompositeSelector([]description.ServerSelector{
description.ReadPrefSelector(readpref.Primary()),
description.LatencySelector(db.client.localThreshold),
})
batchCursor, err := driver.ListCollections(
ctx, cmd,
db.client.topology,
readSelector,
db.client.id,
db.client.topology.SessionPool,
opts...,
)
if err != nil {
return nil, replaceErrors(err)
}
cursor, err := newCursor(batchCursor, db.registry)
return cursor, replaceErrors(err)
}
// ReadConcern returns the read concern of this database.
func (db *Database) ReadConcern() *readconcern.ReadConcern {
return db.readConcern
}
// ReadPreference returns the read preference of this database.
func (db *Database) ReadPreference() *readpref.ReadPref {
return db.readPreference
}
// WriteConcern returns the write concern of this database.
func (db *Database) WriteConcern() *writeconcern.WriteConcern {
return db.writeConcern
}
// Watch returns a change stream cursor used to receive information of changes to the database. This method is preferred
// to running a raw aggregation with a $changeStream stage because it supports resumability in the case of some errors.
// The database must have read concern majority or no read concern for a change stream to be created successfully.
func (db *Database) Watch(ctx context.Context, pipeline interface{},
opts ...*options.ChangeStreamOptions) (*ChangeStream, error) {
return newDbChangeStream(ctx, db, pipeline, opts...)
}