Skip to content

Commit ae4ca85

Browse files
committed
GODRIVER-50: Support maxTimeMS for createIndexes and
dropIndexes commands
1 parent 2a25967 commit ae4ca85

File tree

2 files changed

+154
-32
lines changed

2 files changed

+154
-32
lines changed

mongo/index_view.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +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/options"
1213
)
1314

1415
// ErrInvalidIndexValue indicates that the index Keys document has a value that isn't either a number or a string.
@@ -39,8 +40,8 @@ func (iv IndexView) List(ctx context.Context) (Cursor, error) {
3940
}
4041

4142
// CreateOne creates a single index in the collection specified by the model.
42-
func (iv IndexView) CreateOne(ctx context.Context, model IndexModel) (string, error) {
43-
names, err := iv.CreateMany(ctx, model)
43+
func (iv IndexView) CreateOne(ctx context.Context, model IndexModel, opts ...options.CreateIndexesOptioner) (string, error) {
44+
names, err := iv.CreateMany(ctx, opts, model)
4445
if err != nil {
4546
return "", err
4647
}
@@ -50,7 +51,7 @@ func (iv IndexView) CreateOne(ctx context.Context, model IndexModel) (string, er
5051

5152
// CreateMany creates multiple indexes in the collection specified by the models. The names of the
5253
// creates indexes are returned.
53-
func (iv IndexView) CreateMany(ctx context.Context, models ...IndexModel) ([]string, error) {
54+
func (iv IndexView) CreateMany(ctx context.Context, opts []options.CreateIndexesOptioner, models ...IndexModel) ([]string, error) {
5455
names := make([]string, 0, len(models))
5556
indexes := bson.NewArray()
5657

@@ -76,7 +77,7 @@ func (iv IndexView) CreateMany(ctx context.Context, models ...IndexModel) ([]str
7677
indexes.Append(bson.VC.Document(index))
7778
}
7879

79-
cmd := command.CreateIndexes{NS: iv.coll.namespace(), Indexes: indexes}
80+
cmd := command.CreateIndexes{NS: iv.coll.namespace(), Indexes: indexes, Opts: opts}
8081

8182
_, err := dispatch.CreateIndexes(ctx, cmd, iv.coll.client.topology, iv.coll.writeSelector)
8283
if err != nil {
@@ -87,19 +88,19 @@ func (iv IndexView) CreateMany(ctx context.Context, models ...IndexModel) ([]str
8788
}
8889

8990
// DropOne drops the index with the given name from the collection.
90-
func (iv IndexView) DropOne(ctx context.Context, name string) (bson.Reader, error) {
91+
func (iv IndexView) DropOne(ctx context.Context, name string, opts ...options.DropIndexesOptioner) (bson.Reader, error) {
9192
if name == "*" {
9293
return nil, ErrMultipleIndexDrop
9394
}
9495

95-
cmd := command.DropIndexes{NS: iv.coll.namespace(), Index: name}
96+
cmd := command.DropIndexes{NS: iv.coll.namespace(), Index: name, Opts: opts}
9697

9798
return dispatch.DropIndexes(ctx, cmd, iv.coll.client.topology, iv.coll.writeSelector)
9899
}
99100

100101
// DropAll drops all indexes in the collection.
101-
func (iv IndexView) DropAll(ctx context.Context) (bson.Reader, error) {
102-
cmd := command.DropIndexes{NS: iv.coll.namespace(), Index: "*"}
102+
func (iv IndexView) DropAll(ctx context.Context, opts ...options.DropIndexesOptioner) (bson.Reader, error) {
103+
cmd := command.DropIndexes{NS: iv.coll.namespace(), Index: "*", Opts: opts}
103104

104105
return dispatch.DropIndexes(ctx, cmd, iv.coll.client.topology, iv.coll.writeSelector)
105106
}

mongo/index_view_internal_test.go

Lines changed: 145 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ import (
1616
"time"
1717

1818
"github.com/mongodb/mongo-go-driver/bson"
19+
"github.com/mongodb/mongo-go-driver/core/options"
20+
"github.com/mongodb/mongo-go-driver/core/writeconcern"
1921
"github.com/stretchr/testify/require"
2022
)
2123

2224
var seed = time.Now().UnixNano()
2325

26+
type index struct {
27+
Key map[string]int
28+
NS string
29+
Name string
30+
}
31+
2432
func getIndexableCollection(t *testing.T) (string, *Collection) {
2533
atomic.AddInt64(&seed, 1)
2634
rand.Seed(atomic.LoadInt64(&seed))
@@ -60,11 +68,7 @@ func TestIndexView_List(t *testing.T) {
6068
require.NoError(t, err)
6169

6270
found := false
63-
var index struct {
64-
Key map[string]int
65-
NS string
66-
Name string
67-
}
71+
var index = index{}
6872

6973
for cursor.Next(context.Background()) {
7074
err := cursor.Decode(&index)
@@ -107,11 +111,7 @@ func TestIndexView_CreateOne(t *testing.T) {
107111
require.NoError(t, err)
108112

109113
found := false
110-
var index struct {
111-
Key map[string]int
112-
NS string
113-
Name string
114-
}
114+
var index = index{}
115115

116116
for cursor.Next(context.Background()) {
117117
err := cursor.Decode(&index)
@@ -142,6 +142,7 @@ func TestIndexView_CreateMany(t *testing.T) {
142142

143143
indexNames, err := indexView.CreateMany(
144144
context.Background(),
145+
[]options.CreateIndexesOptioner{},
145146
IndexModel{
146147
Keys: bson.NewDocument(
147148
bson.EC.Int32("foo", -1),
@@ -166,11 +167,7 @@ func TestIndexView_CreateMany(t *testing.T) {
166167

167168
fooFound := false
168169
barBazFound := false
169-
var index struct {
170-
Key map[string]int
171-
NS string
172-
Name string
173-
}
170+
var index = index{}
174171

175172
for cursor.Next(context.Background()) {
176173
err := cursor.Decode(&index)
@@ -209,6 +206,7 @@ func TestIndexView_DropOne(t *testing.T) {
209206

210207
indexNames, err := indexView.CreateMany(
211208
context.Background(),
209+
[]options.CreateIndexesOptioner{},
212210
IndexModel{
213211
Keys: bson.NewDocument(
214212
bson.EC.Int32("foo", -1),
@@ -234,11 +232,7 @@ func TestIndexView_DropOne(t *testing.T) {
234232
cursor, err := indexView.List(context.Background())
235233
require.NoError(t, err)
236234

237-
var index struct {
238-
Key map[string]int
239-
NS string
240-
Name string
241-
}
235+
var index = index{}
242236

243237
for cursor.Next(context.Background()) {
244238
err := cursor.Decode(&index)
@@ -263,6 +257,7 @@ func TestIndexView_DropAll(t *testing.T) {
263257

264258
indexNames, err := indexView.CreateMany(
265259
context.Background(),
260+
[]options.CreateIndexesOptioner{},
266261
IndexModel{
267262
Keys: bson.NewDocument(
268263
bson.EC.Int32("foo", -1),
@@ -287,16 +282,142 @@ func TestIndexView_DropAll(t *testing.T) {
287282
cursor, err := indexView.List(context.Background())
288283
require.NoError(t, err)
289284

290-
var index struct {
291-
Key map[string]int
292-
NS string
293-
Name string
285+
var index = index{}
286+
287+
for cursor.Next(context.Background()) {
288+
err := cursor.Decode(&index)
289+
require.NoError(t, err)
290+
291+
require.Equal(t, expectedNS, index.NS)
292+
require.NotEqual(t, indexNames[0], index.Name)
293+
require.NotEqual(t, indexNames[1], index.Name)
294294
}
295+
require.NoError(t, cursor.Err())
296+
}
297+
298+
func TestIndexView_CreateIndexesOptioner(t *testing.T) {
299+
t.Parallel()
300+
301+
if testing.Short() {
302+
t.Skip()
303+
}
304+
305+
dbName, coll := getIndexableCollection(t)
306+
expectedNS := fmt.Sprintf("IndexView.%s", dbName)
307+
indexView := coll.Indexes()
308+
var opts []options.CreateIndexesOptioner
309+
wc := writeconcern.New(writeconcern.W(25))
310+
elem, err := wc.MarshalBSONElement()
311+
require.NoError(t, err)
312+
optwc := options.OptWriteConcern{WriteConcern: elem, Acknowledged: wc.Acknowledged()}
313+
opts = append(opts, optwc)
314+
indexNames, err := indexView.CreateMany(
315+
context.Background(),
316+
opts,
317+
IndexModel{
318+
Keys: bson.NewDocument(
319+
bson.EC.Int32("foo", -1),
320+
),
321+
},
322+
IndexModel{
323+
Keys: bson.NewDocument(
324+
bson.EC.Int32("bar", 1),
325+
bson.EC.Int32("baz", -1),
326+
),
327+
},
328+
)
329+
require.NoError(t, err)
330+
require.NoError(t, err)
331+
332+
require.Len(t, indexNames, 2)
333+
334+
fooName := indexNames[0]
335+
barBazName := indexNames[1]
336+
337+
cursor, err := indexView.List(context.Background())
338+
require.NoError(t, err)
339+
340+
fooFound := false
341+
barBazFound := false
342+
var index = index{}
295343

296344
for cursor.Next(context.Background()) {
297345
err := cursor.Decode(&index)
298346
require.NoError(t, err)
299347

348+
require.Equal(t, expectedNS, index.NS)
349+
350+
if index.Name == fooName {
351+
require.Len(t, index.Key, 1)
352+
require.Equal(t, -1, index.Key["foo"])
353+
fooFound = true
354+
}
355+
356+
if index.Name == barBazName {
357+
require.Len(t, index.Key, 2)
358+
require.Equal(t, 1, index.Key["bar"])
359+
require.Equal(t, -1, index.Key["baz"])
360+
barBazFound = true
361+
}
362+
}
363+
require.NoError(t, cursor.Err())
364+
require.True(t, fooFound)
365+
require.True(t, barBazFound)
366+
defer func() {
367+
_, err := indexView.DropAll(context.Background())
368+
require.NoError(t, err)
369+
}()
370+
}
371+
372+
func TestIndexView_DropIndexesOptioner(t *testing.T) {
373+
t.Parallel()
374+
375+
if testing.Short() {
376+
t.Skip()
377+
}
378+
379+
dbName, coll := getIndexableCollection(t)
380+
expectedNS := fmt.Sprintf("IndexView.%s", dbName)
381+
indexView := coll.Indexes()
382+
var opts []options.DropIndexesOptioner
383+
wc := writeconcern.New(writeconcern.W(25))
384+
elem, err := wc.MarshalBSONElement()
385+
require.NoError(t, err)
386+
optwc := options.OptWriteConcern{WriteConcern: elem, Acknowledged: wc.Acknowledged()}
387+
opts = append(opts, optwc)
388+
indexNames, err := indexView.CreateMany(
389+
context.Background(),
390+
[]options.CreateIndexesOptioner{},
391+
IndexModel{
392+
Keys: bson.NewDocument(
393+
bson.EC.Int32("foo", -1),
394+
),
395+
},
396+
IndexModel{
397+
Keys: bson.NewDocument(
398+
bson.EC.Int32("bar", 1),
399+
bson.EC.Int32("baz", -1),
400+
),
401+
},
402+
)
403+
require.NoError(t, err)
404+
405+
require.Len(t, indexNames, 2)
406+
407+
_, err = indexView.DropAll(
408+
context.Background(),
409+
opts...,
410+
)
411+
require.NoError(t, err)
412+
413+
cursor, err := indexView.List(context.Background())
414+
require.NoError(t, err)
415+
416+
var index = index{}
417+
418+
for cursor.Next(context.Background()) {
419+
err := cursor.Decode(&index)
420+
require.NoError(t, err)
300421
require.Equal(t, expectedNS, index.NS)
301422
require.NotEqual(t, indexNames[0], index.Name)
302423
require.NotEqual(t, indexNames[1], index.Name)

0 commit comments

Comments
 (0)