Skip to content

Commit fe44874

Browse files
mattChiaravallotiDivjot Arora
authored andcommitted
Implement improved Go Driver CRUD API
GODRIVER-586 GODRIVER-505 GODRIVER-349 Co-authored-by: Matthew Chiaravalloti <[email protected]> Change-Id: I6008752862ae100ec9bce052a1c28b3ac8c29eeb
1 parent 7bd7754 commit fe44874

File tree

157 files changed

+4070
-17456
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

157 files changed

+4070
-17456
lines changed

core/command/aggregate.go

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ package command
88

99
import (
1010
"context"
11-
1211
"github.com/mongodb/mongo-go-driver/bson"
1312
"github.com/mongodb/mongo-go-driver/core/description"
14-
"github.com/mongodb/mongo-go-driver/core/option"
1513
"github.com/mongodb/mongo-go-driver/core/readconcern"
1614
"github.com/mongodb/mongo-go-driver/core/readpref"
1715
"github.com/mongodb/mongo-go-driver/core/session"
@@ -25,7 +23,8 @@ import (
2523
type Aggregate struct {
2624
NS Namespace
2725
Pipeline *bson.Array
28-
Opts []option.AggregateOptioner
26+
CursorOpts []*bson.Element
27+
Opts []*bson.Element
2928
ReadPref *readpref.ReadPref
3029
WriteConcern *writeconcern.WriteConcern
3130
ReadConcern *readconcern.ReadConcern
@@ -58,22 +57,14 @@ func (a *Aggregate) encode(desc description.SelectedServer) (*Read, error) {
5857
command.Append(bson.EC.SubDocument("cursor", cursor))
5958

6059
for _, opt := range a.Opts {
61-
switch t := opt.(type) {
62-
case nil, option.OptMaxAwaitTime:
63-
continue
64-
case option.OptBatchSize:
65-
if t == 0 && a.HasDollarOut() {
60+
switch opt.Key() {
61+
case "batchSize":
62+
if opt.Value().Int32() == 0 && a.HasDollarOut() {
6663
continue
6764
}
68-
err := opt.Option(cursor)
69-
if err != nil {
70-
return nil, err
71-
}
65+
cursor.Append(opt)
7266
default:
73-
err := opt.Option(command)
74-
if err != nil {
75-
return nil, err
76-
}
67+
command.Append(opt)
7768
}
7869
}
7970

@@ -135,19 +126,10 @@ func (a *Aggregate) Decode(desc description.SelectedServer, cb CursorBuilder, wm
135126
}
136127

137128
func (a *Aggregate) decode(desc description.SelectedServer, cb CursorBuilder, rdr bson.Raw) *Aggregate {
138-
opts := make([]option.CursorOptioner, 0)
139-
for _, opt := range a.Opts {
140-
curOpt, ok := opt.(option.CursorOptioner)
141-
if !ok {
142-
continue
143-
}
144-
opts = append(opts, curOpt)
145-
}
146-
147129
labels, err := getErrorLabels(&rdr)
148130
a.err = err
149131

150-
res, err := cb.BuildCursor(rdr, a.Session, a.Clock, opts...)
132+
res, err := cb.BuildCursor(rdr, a.Session, a.Clock, a.CursorOpts...)
151133
a.result = res
152134
if err != nil {
153135
a.err = Error{Message: err.Error(), Labels: labels}

core/command/command.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/mongodb/mongo-go-driver/bson/bsoncore"
1616
"github.com/mongodb/mongo-go-driver/bson/bsontype"
1717
"github.com/mongodb/mongo-go-driver/core/description"
18-
"github.com/mongodb/mongo-go-driver/core/option"
1918
"github.com/mongodb/mongo-go-driver/core/readconcern"
2019
"github.com/mongodb/mongo-go-driver/core/result"
2120
"github.com/mongodb/mongo-go-driver/core/session"
@@ -435,7 +434,7 @@ splitInserts:
435434

436435
func encodeBatch(
437436
docs []*bson.Document,
438-
opts []option.Optioner,
437+
opts []*bson.Element,
439438
cmdKind WriteCommandKind,
440439
collName string,
441440
) (*bson.Document, error) {
@@ -463,17 +462,7 @@ func encodeBatch(
463462
vals = append(vals, bson.VC.Document(doc))
464463
}
465464
cmd.Append(bson.EC.ArrayFromElements(docString, vals...))
466-
467-
for _, opt := range opts {
468-
if opt == nil {
469-
continue
470-
}
471-
472-
err := opt.Option(cmd)
473-
if err != nil {
474-
return nil, err
475-
}
476-
}
465+
cmd.Append(opts...)
477466

478467
return cmd, nil
479468
}

core/command/count.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/mongodb/mongo-go-driver/bson"
1414
"github.com/mongodb/mongo-go-driver/core/description"
15-
"github.com/mongodb/mongo-go-driver/core/option"
1615
"github.com/mongodb/mongo-go-driver/core/readconcern"
1716
"github.com/mongodb/mongo-go-driver/core/readpref"
1817
"github.com/mongodb/mongo-go-driver/core/session"
@@ -25,7 +24,7 @@ import (
2524
type Count struct {
2625
NS Namespace
2726
Query *bson.Document
28-
Opts []option.CountOptioner
27+
Opts []*bson.Element
2928
ReadPref *readpref.ReadPref
3029
ReadConcern *readconcern.ReadConcern
3130
Clock *session.ClusterClock
@@ -51,15 +50,7 @@ func (c *Count) encode(desc description.SelectedServer) (*Read, error) {
5150
}
5251

5352
command := bson.NewDocument(bson.EC.String("count", c.NS.Collection), bson.EC.SubDocument("query", c.Query))
54-
for _, opt := range c.Opts {
55-
if opt == nil {
56-
continue
57-
}
58-
err := opt.Option(command)
59-
if err != nil {
60-
return nil, err
61-
}
62-
}
53+
command.Append(c.Opts...)
6354

6455
return &Read{
6556
Clock: c.Clock,

core/command/count_documents.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/mongodb/mongo-go-driver/bson"
1414
"github.com/mongodb/mongo-go-driver/core/description"
15-
"github.com/mongodb/mongo-go-driver/core/option"
1615
"github.com/mongodb/mongo-go-driver/core/readconcern"
1716
"github.com/mongodb/mongo-go-driver/core/readpref"
1817
"github.com/mongodb/mongo-go-driver/core/session"
@@ -25,7 +24,7 @@ import (
2524
type CountDocuments struct {
2625
NS Namespace
2726
Pipeline *bson.Array
28-
Opts []option.CountOptioner
27+
Opts []*bson.Element
2928
ReadPref *readpref.ReadPref
3029
ReadConcern *readconcern.ReadConcern
3130
Clock *session.ClusterClock
@@ -45,22 +44,7 @@ func (c *CountDocuments) Encode(desc description.SelectedServer) (wiremessage.Wi
4544

4645
cursor := bson.NewDocument()
4746
command.Append(bson.EC.SubDocument("cursor", cursor))
48-
for _, opt := range c.Opts {
49-
if opt == nil {
50-
continue
51-
}
52-
//because we already have these options in the pipeline
53-
switch opt.(type) {
54-
case option.OptSkip:
55-
continue
56-
case option.OptLimit:
57-
continue
58-
}
59-
err := opt.Option(command)
60-
if err != nil {
61-
return nil, err
62-
}
63-
}
47+
command.Append(c.Opts...)
6448

6549
return (&Read{DB: c.NS.DB, ReadPref: c.ReadPref, Command: command}).Encode(desc)
6650
}

core/command/create_indexes.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/mongodb/mongo-go-driver/bson"
1313
"github.com/mongodb/mongo-go-driver/core/description"
14-
"github.com/mongodb/mongo-go-driver/core/option"
1514
"github.com/mongodb/mongo-go-driver/core/result"
1615
"github.com/mongodb/mongo-go-driver/core/session"
1716
"github.com/mongodb/mongo-go-driver/core/wiremessage"
@@ -24,7 +23,7 @@ import (
2423
type CreateIndexes struct {
2524
NS Namespace
2625
Indexes *bson.Array
27-
Opts []option.CreateIndexesOptioner
26+
Opts []*bson.Element
2827
WriteConcern *writeconcern.WriteConcern
2928
Clock *session.ClusterClock
3029
Session *session.Client
@@ -48,16 +47,7 @@ func (ci *CreateIndexes) encode(desc description.SelectedServer) (*Write, error)
4847
bson.EC.String("createIndexes", ci.NS.Collection),
4948
bson.EC.Array("indexes", ci.Indexes),
5049
)
51-
52-
for _, opt := range ci.Opts {
53-
if opt == nil {
54-
continue
55-
}
56-
err := opt.Option(cmd)
57-
if err != nil {
58-
return nil, err
59-
}
60-
}
50+
cmd.Append(ci.Opts...)
6151

6252
return &Write{
6353
Clock: ci.Clock,

core/command/cursor.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ package command
88

99
import (
1010
"context"
11-
1211
"github.com/mongodb/mongo-go-driver/bson"
13-
"github.com/mongodb/mongo-go-driver/core/option"
1412
"github.com/mongodb/mongo-go-driver/core/session"
1513
)
1614

@@ -61,7 +59,7 @@ type Cursor interface {
6159

6260
// CursorBuilder is a type that can build a Cursor.
6361
type CursorBuilder interface {
64-
BuildCursor(bson.Raw, *session.Client, *session.ClusterClock, ...option.CursorOptioner) (Cursor, error)
62+
BuildCursor(bson.Raw, *session.Client, *session.ClusterClock, ...*bson.Element) (Cursor, error)
6563
}
6664

6765
type emptyCursor struct{}

core/command/delete.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/mongodb/mongo-go-driver/bson"
1313
"github.com/mongodb/mongo-go-driver/core/description"
14-
"github.com/mongodb/mongo-go-driver/core/option"
1514
"github.com/mongodb/mongo-go-driver/core/result"
1615
"github.com/mongodb/mongo-go-driver/core/session"
1716
"github.com/mongodb/mongo-go-driver/core/wiremessage"
@@ -26,7 +25,7 @@ type Delete struct {
2625
ContinueOnError bool
2726
NS Namespace
2827
Deletes []*bson.Document
29-
Opts []option.DeleteOptioner
28+
Opts []*bson.Element
3029
WriteConcern *writeconcern.WriteConcern
3130
Clock *session.ClusterClock
3231
Session *session.Client
@@ -70,18 +69,13 @@ func (d *Delete) encodeBatch(docs []*bson.Document, desc description.SelectedSer
7069
copyDocs = append(copyDocs, doc.Copy())
7170
}
7271

73-
var options []option.Optioner
72+
var options []*bson.Element
7473
for _, opt := range d.Opts {
75-
switch opt.(type) {
76-
case nil:
77-
continue
78-
case option.OptCollation:
74+
if opt.Key() == "collation" {
7975
for _, doc := range copyDocs {
80-
if err := opt.Option(doc); err != nil {
81-
return nil, err
82-
}
76+
doc.Append(opt)
8377
}
84-
default:
78+
} else {
8579
options = append(options, opt)
8680
}
8781
}

core/command/distinct.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/mongodb/mongo-go-driver/bson"
1313
"github.com/mongodb/mongo-go-driver/core/description"
14-
"github.com/mongodb/mongo-go-driver/core/option"
1514
"github.com/mongodb/mongo-go-driver/core/readconcern"
1615
"github.com/mongodb/mongo-go-driver/core/readpref"
1716
"github.com/mongodb/mongo-go-driver/core/result"
@@ -27,7 +26,7 @@ type Distinct struct {
2726
NS Namespace
2827
Field string
2928
Query *bson.Document
30-
Opts []option.DistinctOptioner
29+
Opts []*bson.Element
3130
ReadPref *readpref.ReadPref
3231
ReadConcern *readconcern.ReadConcern
3332
Clock *session.ClusterClock
@@ -59,15 +58,7 @@ func (d *Distinct) encode(desc description.SelectedServer) (*Read, error) {
5958
command.Append(bson.EC.SubDocument("query", d.Query))
6059
}
6160

62-
for _, opt := range d.Opts {
63-
if opt == nil {
64-
continue
65-
}
66-
err := opt.Option(command)
67-
if err != nil {
68-
return nil, err
69-
}
70-
}
61+
command.Append(d.Opts...)
7162

7263
return &Read{
7364
Clock: d.Clock,

core/command/drop_indexes.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/mongodb/mongo-go-driver/bson"
1313
"github.com/mongodb/mongo-go-driver/core/description"
14-
"github.com/mongodb/mongo-go-driver/core/option"
1514
"github.com/mongodb/mongo-go-driver/core/session"
1615
"github.com/mongodb/mongo-go-driver/core/wiremessage"
1716
"github.com/mongodb/mongo-go-driver/core/writeconcern"
@@ -23,7 +22,7 @@ import (
2322
type DropIndexes struct {
2423
NS Namespace
2524
Index string
26-
Opts []option.DropIndexesOptioner
25+
Opts []*bson.Element
2726
WriteConcern *writeconcern.WriteConcern
2827
Clock *session.ClusterClock
2928
Session *session.Client
@@ -47,16 +46,7 @@ func (di *DropIndexes) encode(desc description.SelectedServer) (*Write, error) {
4746
bson.EC.String("dropIndexes", di.NS.Collection),
4847
bson.EC.String("index", di.Index),
4948
)
50-
51-
for _, opt := range di.Opts {
52-
if opt == nil {
53-
continue
54-
}
55-
err := opt.Option(cmd)
56-
if err != nil {
57-
return nil, err
58-
}
59-
}
49+
cmd.Append(di.Opts...)
6050

6151
return &Write{
6252
Clock: di.Clock,

0 commit comments

Comments
 (0)