Skip to content

Commit 7b2e076

Browse files
author
Divjot Arora
committed
Add mongo/indexopt
GODRIVER-272 Change-Id: Ia89317ff936988515f6591bbda80dbd4707992e5
1 parent d1c02c9 commit 7b2e076

File tree

11 files changed

+1136
-72
lines changed

11 files changed

+1136
-72
lines changed

core/option/options.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,16 @@ var (
188188
_ CountOptioner = (*OptMaxTime)(nil)
189189
_ CountOptioner = (*OptReadConcern)(nil)
190190
_ CountOptioner = (*OptSkip)(nil)
191+
_ CreateIndexesOptioner = (*OptWriteConcern)(nil)
192+
_ CreateIndexesOptioner = (*OptMaxTime)(nil)
191193
_ CursorOptioner = OptBatchSize(0)
192194
_ DeleteOptioner = (*OptCollation)(nil)
193195
_ DeleteOptioner = (*OptWriteConcern)(nil)
194196
_ DistinctOptioner = (*OptCollation)(nil)
195197
_ DistinctOptioner = (*OptMaxTime)(nil)
196198
_ DistinctOptioner = (*OptReadConcern)(nil)
199+
_ DropIndexesOptioner = (*OptWriteConcern)(nil)
200+
_ DropIndexesOptioner = (*OptMaxTime)(nil)
197201
_ FindOneAndDeleteOptioner = (*OptCollation)(nil)
198202
_ FindOneAndDeleteOptioner = (*OptMaxTime)(nil)
199203
_ FindOneAndDeleteOptioner = (*OptProjection)(nil)
@@ -266,6 +270,7 @@ var (
266270
_ ListDatabasesOptioner = OptNameOnly(false)
267271
_ ListCollectionsOptioner = OptNameOnly(false)
268272
_ ListIndexesOptioner = OptBatchSize(0)
273+
_ ListIndexesOptioner = (*OptMaxTime)(nil)
269274
_ ReplaceOptioner = (*OptBypassDocumentValidation)(nil)
270275
_ ReplaceOptioner = (*OptCollation)(nil)
271276
_ ReplaceOptioner = (*OptUpsert)(nil)

mongo/database.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func (db *Database) Name() string {
6060
}
6161

6262
// Collection gets a handle for a given collection in the database.
63+
// TODO: add variadic option
6364
func (db *Database) Collection(name string) *Collection {
6465
return newCollection(db, name)
6566
}

mongo/index_view.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/mongodb/mongo-go-driver/bson"
1010
"github.com/mongodb/mongo-go-driver/core/command"
1111
"github.com/mongodb/mongo-go-driver/core/dispatch"
12-
"github.com/mongodb/mongo-go-driver/core/option"
12+
"github.com/mongodb/mongo-go-driver/mongo/indexopt"
1313
)
1414

1515
// ErrInvalidIndexValue indicates that the index Keys document has a value that isn't either a number or a string.
@@ -33,15 +33,20 @@ type IndexModel struct {
3333
}
3434

3535
// List returns a cursor iterating over all the indexes in the collection.
36-
func (iv IndexView) List(ctx context.Context) (Cursor, error) {
37-
listCmd := command.ListIndexes{NS: iv.coll.namespace()}
36+
func (iv IndexView) List(ctx context.Context, opts ...indexopt.List) (Cursor, error) {
37+
listOpts, err := indexopt.BundleList(opts...).Unbundle(true)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
listCmd := command.ListIndexes{NS: iv.coll.namespace(), Opts: listOpts}
3843

3944
return dispatch.ListIndexes(ctx, listCmd, iv.coll.client.topology, iv.coll.writeSelector)
4045
}
4146

4247
// CreateOne creates a single index in the collection specified by the model.
43-
func (iv IndexView) CreateOne(ctx context.Context, model IndexModel, opts ...option.CreateIndexesOptioner) (string, error) {
44-
names, err := iv.CreateMany(ctx, opts, model)
48+
func (iv IndexView) CreateOne(ctx context.Context, model IndexModel, opts ...indexopt.Create) (string, error) {
49+
names, err := iv.CreateMany(ctx, []IndexModel{model}, opts...)
4550
if err != nil {
4651
return "", err
4752
}
@@ -51,7 +56,7 @@ func (iv IndexView) CreateOne(ctx context.Context, model IndexModel, opts ...opt
5156

5257
// CreateMany creates multiple indexes in the collection specified by the models. The names of the
5358
// creates indexes are returned.
54-
func (iv IndexView) CreateMany(ctx context.Context, opts []option.CreateIndexesOptioner, models ...IndexModel) ([]string, error) {
59+
func (iv IndexView) CreateMany(ctx context.Context, models []IndexModel, opts ...indexopt.Create) ([]string, error) {
5560
names := make([]string, 0, len(models))
5661
indexes := bson.NewArray()
5762

@@ -81,9 +86,14 @@ func (iv IndexView) CreateMany(ctx context.Context, opts []option.CreateIndexesO
8186
indexes.Append(bson.VC.Document(index))
8287
}
8388

84-
cmd := command.CreateIndexes{NS: iv.coll.namespace(), Indexes: indexes, Opts: opts}
89+
createOpts, err := indexopt.BundleCreate(opts...).Unbundle(true)
90+
if err != nil {
91+
return nil, err
92+
}
93+
94+
cmd := command.CreateIndexes{NS: iv.coll.namespace(), Indexes: indexes, Opts: createOpts}
8595

86-
_, err := dispatch.CreateIndexes(ctx, cmd, iv.coll.client.topology, iv.coll.writeSelector)
96+
_, err = dispatch.CreateIndexes(ctx, cmd, iv.coll.client.topology, iv.coll.writeSelector)
8797
if err != nil {
8898
return nil, err
8999
}
@@ -92,19 +102,29 @@ func (iv IndexView) CreateMany(ctx context.Context, opts []option.CreateIndexesO
92102
}
93103

94104
// DropOne drops the index with the given name from the collection.
95-
func (iv IndexView) DropOne(ctx context.Context, name string, opts ...option.DropIndexesOptioner) (bson.Reader, error) {
105+
func (iv IndexView) DropOne(ctx context.Context, name string, opts ...indexopt.Drop) (bson.Reader, error) {
96106
if name == "*" {
97107
return nil, ErrMultipleIndexDrop
98108
}
99109

100-
cmd := command.DropIndexes{NS: iv.coll.namespace(), Index: name, Opts: opts}
110+
dropOpts, err := indexopt.BundleDrop(opts...).Unbundle(true)
111+
if err != nil {
112+
return nil, err
113+
}
114+
115+
cmd := command.DropIndexes{NS: iv.coll.namespace(), Index: name, Opts: dropOpts}
101116

102117
return dispatch.DropIndexes(ctx, cmd, iv.coll.client.topology, iv.coll.writeSelector)
103118
}
104119

105120
// DropAll drops all indexes in the collection.
106-
func (iv IndexView) DropAll(ctx context.Context, opts ...option.DropIndexesOptioner) (bson.Reader, error) {
107-
cmd := command.DropIndexes{NS: iv.coll.namespace(), Index: "*", Opts: opts}
121+
func (iv IndexView) DropAll(ctx context.Context, opts ...indexopt.Drop) (bson.Reader, error) {
122+
dropOpts, err := indexopt.BundleDrop(opts...).Unbundle(true)
123+
if err != nil {
124+
return nil, err
125+
}
126+
127+
cmd := command.DropIndexes{NS: iv.coll.namespace(), Index: "*", Opts: dropOpts}
108128

109129
return dispatch.DropIndexes(ctx, cmd, iv.coll.client.topology, iv.coll.writeSelector)
110130
}

mongo/index_view_internal_test.go

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
"time"
1717

1818
"github.com/mongodb/mongo-go-driver/bson"
19-
"github.com/mongodb/mongo-go-driver/core/option"
2019
"github.com/mongodb/mongo-go-driver/core/writeconcern"
20+
"github.com/mongodb/mongo-go-driver/mongo/indexopt"
2121
"github.com/stretchr/testify/require"
2222
)
2323

@@ -277,17 +277,18 @@ func TestIndexView_CreateMany(t *testing.T) {
277277

278278
indexNames, err := indexView.CreateMany(
279279
context.Background(),
280-
[]option.CreateIndexesOptioner{},
281-
IndexModel{
282-
Keys: bson.NewDocument(
283-
bson.EC.Int32("foo", -1),
284-
),
285-
},
286-
IndexModel{
287-
Keys: bson.NewDocument(
288-
bson.EC.Int32("bar", 1),
289-
bson.EC.Int32("baz", -1),
290-
),
280+
[]IndexModel{
281+
{
282+
Keys: bson.NewDocument(
283+
bson.EC.Int32("foo", -1),
284+
),
285+
},
286+
{
287+
Keys: bson.NewDocument(
288+
bson.EC.Int32("bar", 1),
289+
bson.EC.Int32("baz", -1),
290+
),
291+
},
291292
},
292293
)
293294
require.NoError(t, err)
@@ -341,17 +342,18 @@ func TestIndexView_DropOne(t *testing.T) {
341342

342343
indexNames, err := indexView.CreateMany(
343344
context.Background(),
344-
[]option.CreateIndexesOptioner{},
345-
IndexModel{
346-
Keys: bson.NewDocument(
347-
bson.EC.Int32("foo", -1),
348-
),
349-
},
350-
IndexModel{
351-
Keys: bson.NewDocument(
352-
bson.EC.Int32("bar", 1),
353-
bson.EC.Int32("baz", -1),
354-
),
345+
[]IndexModel{
346+
{
347+
Keys: bson.NewDocument(
348+
bson.EC.Int32("foo", -1),
349+
),
350+
},
351+
{
352+
Keys: bson.NewDocument(
353+
bson.EC.Int32("bar", 1),
354+
bson.EC.Int32("baz", -1),
355+
),
356+
},
355357
},
356358
)
357359
require.NoError(t, err)
@@ -392,17 +394,18 @@ func TestIndexView_DropAll(t *testing.T) {
392394

393395
indexNames, err := indexView.CreateMany(
394396
context.Background(),
395-
[]option.CreateIndexesOptioner{},
396-
IndexModel{
397-
Keys: bson.NewDocument(
398-
bson.EC.Int32("foo", -1),
399-
),
400-
},
401-
IndexModel{
402-
Keys: bson.NewDocument(
403-
bson.EC.Int32("bar", 1),
404-
bson.EC.Int32("baz", -1),
405-
),
397+
[]IndexModel{
398+
{
399+
Keys: bson.NewDocument(
400+
bson.EC.Int32("foo", -1),
401+
),
402+
},
403+
{
404+
Keys: bson.NewDocument(
405+
bson.EC.Int32("bar", 1),
406+
bson.EC.Int32("baz", -1),
407+
),
408+
},
406409
},
407410
)
408411
require.NoError(t, err)
@@ -440,24 +443,28 @@ func TestIndexView_CreateIndexesOptioner(t *testing.T) {
440443
dbName, coll := getIndexableCollection(t)
441444
expectedNS := fmt.Sprintf("IndexView.%s", dbName)
442445
indexView := coll.Indexes()
443-
var opts []option.CreateIndexesOptioner
446+
447+
var opts []indexopt.Create
444448
wc := writeconcern.New(writeconcern.W(1))
445-
optwc := option.OptWriteConcern{WriteConcern: wc}
449+
optwc := indexopt.WriteConcern(wc)
446450
opts = append(opts, optwc)
451+
447452
indexNames, err := indexView.CreateMany(
448453
context.Background(),
449-
opts,
450-
IndexModel{
451-
Keys: bson.NewDocument(
452-
bson.EC.Int32("foo", -1),
453-
),
454-
},
455-
IndexModel{
456-
Keys: bson.NewDocument(
457-
bson.EC.Int32("bar", 1),
458-
bson.EC.Int32("baz", -1),
459-
),
454+
[]IndexModel{
455+
{
456+
Keys: bson.NewDocument(
457+
bson.EC.Int32("foo", -1),
458+
),
459+
},
460+
{
461+
Keys: bson.NewDocument(
462+
bson.EC.Int32("bar", 1),
463+
bson.EC.Int32("baz", -1),
464+
),
465+
},
460466
},
467+
opts...,
461468
)
462469
require.NoError(t, err)
463470
require.NoError(t, err)
@@ -512,23 +519,26 @@ func TestIndexView_DropIndexesOptioner(t *testing.T) {
512519
dbName, coll := getIndexableCollection(t)
513520
expectedNS := fmt.Sprintf("IndexView.%s", dbName)
514521
indexView := coll.Indexes()
515-
var opts []option.DropIndexesOptioner
522+
523+
var opts []indexopt.Drop
516524
wc := writeconcern.New(writeconcern.W(1))
517-
optwc := option.OptWriteConcern{WriteConcern: wc}
525+
optwc := indexopt.WriteConcern(wc)
518526
opts = append(opts, optwc)
527+
519528
indexNames, err := indexView.CreateMany(
520529
context.Background(),
521-
[]option.CreateIndexesOptioner{},
522-
IndexModel{
523-
Keys: bson.NewDocument(
524-
bson.EC.Int32("foo", -1),
525-
),
526-
},
527-
IndexModel{
528-
Keys: bson.NewDocument(
529-
bson.EC.Int32("bar", 1),
530-
bson.EC.Int32("baz", -1),
531-
),
530+
[]IndexModel{
531+
{
532+
Keys: bson.NewDocument(
533+
bson.EC.Int32("foo", -1),
534+
),
535+
},
536+
{
537+
Keys: bson.NewDocument(
538+
bson.EC.Int32("bar", 1),
539+
bson.EC.Int32("baz", -1),
540+
),
541+
},
532542
},
533543
)
534544
require.NoError(t, err)

0 commit comments

Comments
 (0)