Skip to content

Commit

Permalink
GODRIVER-2701 PR Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
timothy-kim-mongo committed Jul 2, 2024
1 parent 5cefa42 commit d1c21b1
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 146 deletions.
44 changes: 11 additions & 33 deletions mongo/index_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,42 +403,20 @@ func (iv IndexView) drop(ctx context.Context, index any, opts ...*options.DropIn

// TODO(GODRIVER-3038): This operation should pass CSE to the DropIndexes
// Crypt setter to be applied to the operation.
op := operation.NewDropIndexes(index).Session(sess).WriteConcern(wc).CommandMonitor(iv.coll.client.monitor).
ServerSelector(selector).ClusterClock(iv.coll.client.clock).
Database(iv.coll.db.name).Collection(iv.coll.name).
Deployment(iv.coll.client.deployment).ServerAPI(iv.coll.client.serverAPI).
Timeout(iv.coll.client.timeout).MaxTime(dio.MaxTime)

var results operation.DropIndexesResult

switch indexVal := index.(type) {
case string:
// Convert index to string
op := operation.NewDropIndexes(indexVal).Session(sess).WriteConcern(wc).CommandMonitor(iv.coll.client.monitor).
ServerSelector(selector).ClusterClock(iv.coll.client.clock).
Database(iv.coll.db.name).Collection(iv.coll.name).
Deployment(iv.coll.client.deployment).ServerAPI(iv.coll.client.serverAPI).
Timeout(iv.coll.client.timeout).MaxTime(dio.MaxTime)
err = op.Execute(ctx)
if err != nil {
return nil, replaceErrors(err)
}
results = op.Result()
case bsoncore.Document:
// Convert index to bsoncore.Document
op := operation.NewDropIndexes(indexVal).Session(sess).WriteConcern(wc).CommandMonitor(iv.coll.client.monitor).
ServerSelector(selector).ClusterClock(iv.coll.client.clock).
Database(iv.coll.db.name).Collection(iv.coll.name).
Deployment(iv.coll.client.deployment).ServerAPI(iv.coll.client.serverAPI).
Timeout(iv.coll.client.timeout).MaxTime(dio.MaxTime)
err = op.Execute(ctx)
if err != nil {
return nil, replaceErrors(err)
}
results = op.Result()
default:
// Handle unexpected type
return nil, ErrInvalidIndexType
err = op.Execute(ctx)
if err != nil {
return nil, replaceErrors(err)
}

// TODO: it's weird to return a bson.Raw here because we have to convert the result back to BSON
ridx, res := bsoncore.AppendDocumentStart(nil)
res = bsoncore.AppendInt32Element(res, "nIndexesWas", results.NIndexesWas)
res = bsoncore.AppendInt32Element(res, "nIndexesWas", op.Result().NIndexesWas)
res, _ = bsoncore.AppendDocumentEnd(res, ridx)
return res, nil
}
Expand All @@ -462,12 +440,12 @@ func (iv IndexView) DropOne(ctx context.Context, name string, opts ...*options.D
return iv.drop(ctx, name, opts...)
}

// DropKeyOne executes a dropIndexes operation to drop an index on the collection. If the operation succeeds, this returns
// DropWithKey executes a dropIndexes operation to drop an index on the collection. If the operation succeeds, this returns
// a BSON document in the form {nIndexesWas: <int32>}. The "nIndexesWas" field in the response contains the number of
// indexes that existed prior to the drop.
//
// The key parameter should be the keySpecDocument of the index to drop.
func (iv IndexView) DropKeyOne(ctx context.Context, keySpecDocument interface{}, opts ...*options.DropIndexesOptions) (bson.Raw, error) {
func (iv IndexView) DropWithKey(ctx context.Context, keySpecDocument interface{}, opts ...*options.DropIndexesOptions) (bson.Raw, error) {
doc, err := marshal(keySpecDocument, iv.coll.bsonOpts, iv.coll.registry)
if err != nil {
return nil, err
Expand Down
118 changes: 96 additions & 22 deletions mongo/integration/index_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/google/go-cmp/cmp"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/internal/require"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/mongo/options"
Expand Down Expand Up @@ -607,36 +608,109 @@ func TestIndexView(t *testing.T) {
}
assert.Nil(mt, cursor.Err(), "cursor error: %v", cursor.Err())
})
mt.Run("dropKey one", func(mt *mtest.T) {
iv := mt.Coll.Indexes()
indexNames, err := iv.CreateMany(context.Background(), []mongo.IndexModel{
mt.Run("drop with key", func(mt *mtest.T) {
tests := []struct {
name string
models []mongo.IndexModel
index any
}{
{
Keys: bson.Raw(bsoncore.NewDocumentBuilder().AppendInt32("_id", 1).Build()),
name: "custom index name and unique indexes",
models: []mongo.IndexModel{
{
Keys: bson.D{{"_id", 1}},
},
{
Keys: bson.D{{"username", 1}},
Options: options.Index().SetUnique(true).SetName("myidx"),
},
},
index: bson.D{{"username", 1}},
},
{
Keys: bson.Raw(bsoncore.NewDocumentBuilder().AppendInt32("username", 1).Build()),
Options: options.Index().SetUnique(true).SetName("myidx"),
name: "normal generated index name",
models: []mongo.IndexModel{
{
Keys: bson.D{{"_id", 1}},
},
{
Keys: bson.D{{"foo", -1}},
},
},
index: bson.D{{"foo", -1}},
},
{
name: "compound index",
models: []mongo.IndexModel{
{
Keys: bson.D{{"_id", 1}},
},
{
Keys: bson.D{{"foo", 1}, {"bar", 1}},
},
},
index: bson.D{{"foo", 1}, {"bar", 1}},
},
/*{
name: "clustered indexes",
models: []mongo.IndexModel{
{
Keys: bson.Raw(bsoncore.NewDocumentBuilder().AppendInt32("foo", 1).Build()),
},
{
Keys: map[string]interface{}{"key": bson.D{{"_id", 1}}},
},
},
index: map[string]interface{}{"key": bson.D{{"_id", 1}}},
},*/
{
name: "text index",
models: []mongo.IndexModel{
{
Keys: bson.D{{"_id", 1}},
},
{
Keys: bson.D{{"plot", "text"}},
},
},
index: map[string]interface{}{"_fts": "text", "_ftsx": 1},
//index: map[string]interface{}{"plot": "text"},
},
})

key := map[string]interface{}{
"username": 1,
}
assert.Nil(mt, err, "CreateMany error: %v", err)
assert.Equal(mt, 2, len(indexNames), "expected 2 index names, got %v", len(indexNames))

_, err = iv.DropKeyOne(context.Background(), key)
assert.Nil(mt, err, "DropOne error: %v", err)
for _, test := range tests {
mt.Run(test.name, func(mt *mtest.T) {
iv := mt.Coll.Indexes()
indexNames, err := iv.CreateMany(context.Background(), test.models)

cursor, err := iv.List(context.Background())
assert.Nil(mt, err, "List error: %v", err)
for cursor.Next(context.Background()) {
var idx index
err = cursor.Decode(&idx)
assert.Nil(mt, err, "Decode error: %v (document %v)", err, cursor.Current)
assert.NotEqual(mt, indexNames[1], idx.Name, "found index %v after dropping", indexNames[1])
assert.NoError(mt, err)
assert.Equal(mt, 2, len(indexNames), "expected 2 index names, got %v", len(indexNames))

// List existing indexes
cursor, err := mt.Coll.Indexes().List(context.Background())
require.NoError(t, err)

var indexes []bson.M
err = cursor.All(context.Background(), &indexes)
require.NoError(t, err)

t.Logf("Existing indexes: %v", indexes)
//////

_, err = iv.DropWithKey(context.Background(), test.index)
assert.Nil(mt, err, "DropOne error: %v", err)

cursor, err = iv.List(context.Background())
assert.Nil(mt, err, "List error: %v", err)
for cursor.Next(context.Background()) {
var idx index
err = cursor.Decode(&idx)
assert.Nil(mt, err, "Decode error: %v (document %v)", err, cursor.Current)
assert.NotEqual(mt, indexNames[1], idx.Name, "found index %v after dropping", indexNames[1])
}
assert.Nil(mt, cursor.Err(), "cursor error: %v", cursor.Err())
})
}
assert.Nil(mt, cursor.Err(), "cursor error: %v", cursor.Err())
})
mt.Run("drop all", func(mt *mtest.T) {
iv := mt.Coll.Indexes()
Expand Down
54 changes: 0 additions & 54 deletions mongo/integration/main.go

This file was deleted.

Loading

0 comments on commit d1c21b1

Please sign in to comment.