Skip to content

Commit 8c736ad

Browse files
authored
Merge pull request #119 from divjotarora/legacy279
Implement legacy index enumeration
2 parents 7da8a11 + c9deb6a commit 8c736ad

File tree

3 files changed

+56
-34
lines changed

3 files changed

+56
-34
lines changed

mongo/index_view_internal_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ func getIndexableCollection(t *testing.T) (string, *Collection) {
5252

5353
func TestIndexView_List(t *testing.T) {
5454
t.Parallel()
55-
// TODO(GODRIVER-279) don't skip for legacy servers
56-
skipIfBelow32(t)
5755

5856
if testing.Short() {
5957
t.Skip()
@@ -87,7 +85,6 @@ func TestIndexView_List(t *testing.T) {
8785

8886
func TestIndexView_CreateOne(t *testing.T) {
8987
t.Parallel()
90-
skipIfBelow32(t)
9188

9289
if testing.Short() {
9390
t.Skip()
@@ -129,7 +126,6 @@ func TestIndexView_CreateOne(t *testing.T) {
129126

130127
func TestIndexView_CreateOneWithNameOption(t *testing.T) {
131128
t.Parallel()
132-
skipIfBelow32(t)
133129

134130
if testing.Short() {
135131
t.Skip()
@@ -253,7 +249,6 @@ func TestIndexView_CreateOneWithNilKeys(t *testing.T) {
253249

254250
func TestIndexView_CreateMany(t *testing.T) {
255251
t.Parallel()
256-
skipIfBelow32(t)
257252

258253
if testing.Short() {
259254
t.Skip()
@@ -317,7 +312,6 @@ func TestIndexView_CreateMany(t *testing.T) {
317312

318313
func TestIndexView_DropOne(t *testing.T) {
319314
t.Parallel()
320-
skipIfBelow32(t)
321315

322316
if testing.Short() {
323317
t.Skip()
@@ -368,7 +362,6 @@ func TestIndexView_DropOne(t *testing.T) {
368362

369363
func TestIndexView_DropAll(t *testing.T) {
370364
t.Parallel()
371-
skipIfBelow32(t)
372365

373366
if testing.Short() {
374367
t.Skip()
@@ -419,7 +412,6 @@ func TestIndexView_DropAll(t *testing.T) {
419412

420413
func TestIndexView_CreateIndexesOptioner(t *testing.T) {
421414
t.Parallel()
422-
skipIfBelow32(t)
423415

424416
if testing.Short() {
425417
t.Skip()
@@ -490,7 +482,6 @@ func TestIndexView_CreateIndexesOptioner(t *testing.T) {
490482

491483
func TestIndexView_DropIndexesOptioner(t *testing.T) {
492484
t.Parallel()
493-
skipIfBelow32(t)
494485

495486
if testing.Short() {
496487
t.Skip()

x/mongo/driver/list_indexes.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/mongodb/mongo-go-driver/x/mongo/driver/topology"
1818
"github.com/mongodb/mongo-go-driver/x/mongo/driver/uuid"
1919
"github.com/mongodb/mongo-go-driver/x/network/command"
20+
"github.com/mongodb/mongo-go-driver/x/network/connection"
2021
"github.com/mongodb/mongo-go-driver/x/network/description"
2122
)
2223

@@ -43,6 +44,10 @@ func ListIndexes(
4344
}
4445
defer conn.Close()
4546

47+
if ss.Description().WireVersion.Max < 3 {
48+
return legacyListIndexes(ctx, cmd, ss, conn, opts...)
49+
}
50+
4651
lio := options.MergeListIndexesOptions(opts...)
4752
if lio.BatchSize != nil {
4853
elem := bsonx.Elem{"batchSize", bsonx.Int32(*lio.BatchSize)}
@@ -68,3 +73,31 @@ func ListIndexes(
6873

6974
return c, err
7075
}
76+
77+
func legacyListIndexes(
78+
ctx context.Context,
79+
cmd command.ListIndexes,
80+
ss *topology.SelectedServer,
81+
conn connection.Connection,
82+
opts ...*options.ListIndexesOptions,
83+
) (command.Cursor, error) {
84+
lio := options.MergeListIndexesOptions(opts...)
85+
ns := cmd.NS.DB + "." + cmd.NS.Collection
86+
87+
findCmd := command.Find{
88+
NS: command.NewNamespace(cmd.NS.DB, "system.indexes"),
89+
Filter: bsonx.Doc{
90+
{"ns", bsonx.String(ns)},
91+
},
92+
}
93+
94+
findOpts := options.Find()
95+
if lio.BatchSize != nil {
96+
findOpts.SetBatchSize(*lio.BatchSize)
97+
}
98+
if lio.MaxTime != nil {
99+
findOpts.SetMaxTime(*lio.MaxTime)
100+
}
101+
102+
return legacyFind(ctx, findCmd, nil, ss, conn, findOpts)
103+
}

x/network/integration/list_indexes_test.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,30 @@ import (
1212

1313
"github.com/mongodb/mongo-go-driver/bson"
1414
"github.com/mongodb/mongo-go-driver/internal/testutil"
15+
"github.com/mongodb/mongo-go-driver/mongo/options"
1516
"github.com/mongodb/mongo-go-driver/x/bsonx"
17+
"github.com/mongodb/mongo-go-driver/x/mongo/driver"
18+
"github.com/mongodb/mongo-go-driver/x/mongo/driver/session"
19+
"github.com/mongodb/mongo-go-driver/x/mongo/driver/uuid"
1620
"github.com/mongodb/mongo-go-driver/x/network/command"
1721
"github.com/mongodb/mongo-go-driver/x/network/description"
1822
)
1923

24+
func runCommand(t *testing.T, cmd command.ListIndexes, opts ...*options.ListIndexesOptions) (command.Cursor, error) {
25+
clientID, err := uuid.New()
26+
noerr(t, err)
27+
28+
return driver.ListIndexes(
29+
context.Background(),
30+
cmd,
31+
testutil.Topology(t),
32+
description.WriteSelector(),
33+
clientID,
34+
&session.Pool{},
35+
opts...,
36+
)
37+
}
38+
2039
func TestCommandListIndexes(t *testing.T) {
2140
noerr := func(t *testing.T, err error) {
2241
// t.Helper()
@@ -25,16 +44,10 @@ func TestCommandListIndexes(t *testing.T) {
2544
t.FailNow()
2645
}
2746
}
28-
// TODO(GODRIVER-279) don't skip once legacy index enumeration implemented
29-
skipIfBelow32(t)
3047

3148
t.Run("InvalidDatabaseName", func(t *testing.T) {
32-
server, err := testutil.Topology(t).SelectServer(context.Background(), description.WriteSelector())
33-
noerr(t, err)
34-
conn, err := server.Connection(context.Background())
35-
noerr(t, err)
3649
ns := command.Namespace{DB: "ex", Collection: "space"}
37-
cursor, err := (&command.ListIndexes{NS: ns}).RoundTrip(context.Background(), server.SelectedDescription(), server, conn)
50+
cursor, err := runCommand(t, command.ListIndexes{NS: ns})
3851
noerr(t, err)
3952

4053
indexes := []string{}
@@ -58,12 +71,8 @@ func TestCommandListIndexes(t *testing.T) {
5871
}
5972
})
6073
t.Run("InvalidCollectionName", func(t *testing.T) {
61-
server, err := testutil.Topology(t).SelectServer(context.Background(), description.WriteSelector())
62-
noerr(t, err)
63-
conn, err := server.Connection(context.Background())
64-
noerr(t, err)
6574
ns := command.Namespace{DB: "ex", Collection: testutil.ColName(t)}
66-
cursor, err := (&command.ListIndexes{NS: ns}).RoundTrip(context.Background(), server.SelectedDescription(), server, conn)
75+
cursor, err := runCommand(t, command.ListIndexes{NS: ns})
6776
noerr(t, err)
6877

6978
indexes := []string{}
@@ -87,18 +96,14 @@ func TestCommandListIndexes(t *testing.T) {
8796
}
8897
})
8998
t.Run("SingleBatch", func(t *testing.T) {
90-
server, err := testutil.Topology(t).SelectServer(context.Background(), description.WriteSelector())
91-
noerr(t, err)
92-
conn, err := server.Connection(context.Background())
93-
noerr(t, err)
9499
testutil.AutoDropCollection(t)
95100
testutil.AutoCreateIndexes(t, []string{"a"})
96101
testutil.AutoCreateIndexes(t, []string{"b"})
97102
testutil.AutoCreateIndexes(t, []string{"c"})
98103
testutil.AutoCreateIndexes(t, []string{"d", "e"})
99104

100105
ns := command.NewNamespace(dbName, testutil.ColName(t))
101-
cursor, err := (&command.ListIndexes{NS: ns}).RoundTrip(context.Background(), server.SelectedDescription(), server, conn)
106+
cursor, err := runCommand(t, command.ListIndexes{NS: ns})
102107
noerr(t, err)
103108

104109
indexes := []string{}
@@ -129,20 +134,13 @@ func TestCommandListIndexes(t *testing.T) {
129134
}
130135
})
131136
t.Run("MultipleBatch", func(t *testing.T) {
132-
server, err := testutil.Topology(t).SelectServer(context.Background(), description.WriteSelector())
133-
noerr(t, err)
134-
conn, err := server.Connection(context.Background())
135-
noerr(t, err)
136137
testutil.AutoDropCollection(t)
137138
testutil.AutoCreateIndexes(t, []string{"a"})
138139
testutil.AutoCreateIndexes(t, []string{"b"})
139140
testutil.AutoCreateIndexes(t, []string{"c"})
140141

141142
ns := command.NewNamespace(dbName, testutil.ColName(t))
142-
opts := []bsonx.Elem{
143-
{"batchSize", bsonx.Int32(1)},
144-
}
145-
cursor, err := (&command.ListIndexes{NS: ns, Opts: opts}).RoundTrip(context.Background(), server.SelectedDescription(), server, conn)
143+
cursor, err := runCommand(t, command.ListIndexes{NS: ns}, options.ListIndexes().SetBatchSize(1))
146144
noerr(t, err)
147145

148146
indexes := []string{}

0 commit comments

Comments
 (0)