Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GODRIVER-2701 Implement a variant of DropIndex that gets keys rather than name #1683

Merged
merged 24 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8962c41
GODRIVER-2701 TEST
timothy-kim-mongo Jun 18, 2024
fe7cf9f
GODRIVER-2701
timothy-kim-mongo Jun 20, 2024
3ad9b8a
GODRIVER-2701 PR
timothy-kim-mongo Jun 20, 2024
57dfd93
GODRIVER-2701 PR
timothy-kim-mongo Jun 20, 2024
a49d3be
GODRIVER-2701 PR
timothy-kim-mongo Jun 20, 2024
56850c7
GODRIVER-2701 PR
timothy-kim-mongo Jun 20, 2024
7e5f157
GODRIVER-2701 PR (Changes to remove generic implementation)
timothy-kim-mongo Jun 20, 2024
5cefa42
GODRIVER-2701 PR Changes
timothy-kim-mongo Jul 1, 2024
d1c21b1
GODRIVER-2701 PR Changes
timothy-kim-mongo Jul 2, 2024
b9defa0
GODRIVER-2701 PR Changes
timothy-kim-mongo Jul 3, 2024
7d7005e
Merge branch 'v1' into GODRIVER-2701
timothy-kim-mongo Jul 3, 2024
5da92b2
GODRIVER-2701 PR
timothy-kim-mongo Jul 3, 2024
be0318c
GODRIVER-2701 PR
timothy-kim-mongo Jul 3, 2024
2f7be52
Merge branch 'GODRIVER-2701' of https://github.com/timothy-kim-mongo/…
timothy-kim-mongo Jul 3, 2024
b9da319
GODRIVER-2701 PR
timothy-kim-mongo Jul 3, 2024
bdd81e0
GODRIVER-2701 PR
timothy-kim-mongo Jul 3, 2024
b4e000e
GODRIVER-2701 PR
timothy-kim-mongo Jul 8, 2024
8769041
GODRIVER-2701 PR
timothy-kim-mongo Jul 8, 2024
abbec79
GODRIVER_2701 PR
timothy-kim-mongo Jul 10, 2024
95f571f
GODRIVER-2701 PR Changes
timothy-kim-mongo Jul 15, 2024
90a7719
GODRIVER-2701 PR Changes
timothy-kim-mongo Jul 16, 2024
bc773fd
GODRIVER-2701 Fix Conflicts
timothy-kim-mongo Jul 16, 2024
23ab5c7
GODRIVER-2701 PR
timothy-kim-mongo Jul 17, 2024
563698b
Merge branch 'v1' into GODRIVER-2701
timothy-kim-mongo Jul 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions mongo/index_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (iv IndexView) createOptionsDoc(opts *options.IndexOptions) (bsoncore.Docum
return optsDoc, nil
}

func (iv IndexView) drop(ctx context.Context, name string, opts ...*options.DropIndexesOptions) (bson.Raw, error) {
func (iv IndexView) drop(ctx context.Context, index any, opts ...*options.DropIndexesOptions) (bson.Raw, error) {
if ctx == nil {
ctx = context.Background()
}
Expand Down Expand Up @@ -397,8 +397,7 @@ func (iv IndexView) drop(ctx context.Context, name string, opts ...*options.Drop

// TODO(GODRIVER-3038): This operation should pass CSE to the DropIndexes
// Crypt setter to be applied to the operation.
op := operation.NewDropIndexes(name).
Session(sess).WriteConcern(wc).CommandMonitor(iv.coll.client.monitor).
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).
Expand Down Expand Up @@ -436,6 +435,20 @@ func (iv IndexView) DropOne(ctx context.Context, name string, opts ...*options.D
return iv.drop(ctx, name, opts...)
}

// DropWithKey drops a collection index by key using the dropIndexes operation. 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.
//
// This function is useful to drop an index using its key specification instead of its name.
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
}

return iv.drop(ctx, doc, opts...)
}

// DropAll executes a dropIndexes operation to drop all indexes 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.
Expand Down
82 changes: 82 additions & 0 deletions mongo/integration/index_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,88 @@ func TestIndexView(t *testing.T) {
}
assert.Nil(mt, cursor.Err(), "cursor error: %v", cursor.Err())
})
mt.Run("drop with key", func(mt *mtest.T) {
tests := []struct {
name string
models []mongo.IndexModel
index any
want string
}{
{
name: "custom index name and unique indexes",
models: []mongo.IndexModel{
{
Keys: bson.D{{"username", int32(1)}},
Options: options.Index().SetUnique(true).SetName("myidx"),
},
},
index: bson.D{{"username", int32(1)}},
want: "myidx",
},
{
name: "normal generated index name",
models: []mongo.IndexModel{
{
Keys: bson.D{{"foo", int32(-1)}},
},
},
index: bson.D{{"foo", int32(-1)}},
want: "foo_-1",
},
{
name: "compound index",
models: []mongo.IndexModel{
{
Keys: bson.D{{"foo", int32(1)}, {"bar", int32(1)}},
},
},
index: bson.D{{"foo", int32(1)}, {"bar", int32(1)}},
want: "foo_1_bar_1",
},
{
name: "text index",
models: []mongo.IndexModel{
{
Keys: bson.D{{"plot1", "text"}, {"plot2", "text"}},
},
},
// Key is automatically set to Full Text Search for any text index
index: bson.D{{"_fts", "text"}, {"_ftsx", int32(1)}},
want: "plot1_text_plot2_text",
},
}

for _, test := range tests {
mt.Run(test.name, func(mt *mtest.T) {
iv := mt.Coll.Indexes()
indexNames, err := iv.CreateMany(context.Background(), test.models)

s, _ := test.index.(bson.D)
for _, name := range indexNames {
verifyIndexExists(mt, iv, index{
Key: s,
Name: name,
})
}

assert.NoError(mt, err)
assert.Equal(mt, len(test.models), len(indexNames), "expected %v index names, got %v", len(test.models), len(indexNames))

_, err = iv.DropWithKey(context.Background(), test.index)
timothy-kim-mongo marked this conversation as resolved.
Show resolved Hide resolved
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, test.want, idx.Name, "found index %v after dropping", test.want)
}
assert.Nil(mt, cursor.Err(), "cursor error: %v", cursor.Err())
})
}
})
mt.Run("drop all", func(mt *mtest.T) {
iv := mt.Coll.Indexes()
names, err := iv.CreateMany(context.Background(), []mongo.IndexModel{
Expand Down
21 changes: 14 additions & 7 deletions x/mongo/driver/operation/drop_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// DropIndexes performs an dropIndexes operation.
type DropIndexes struct {
authenticator driver.Authenticator
index *string
index any
maxTime *time.Duration
session *session.Client
clock *session.ClusterClock
Expand Down Expand Up @@ -66,9 +66,9 @@ func buildDropIndexesResult(response bsoncore.Document) (DropIndexesResult, erro
}

// NewDropIndexes constructs and returns a new DropIndexes.
func NewDropIndexes(index string) *DropIndexes {
func NewDropIndexes(index any) *DropIndexes {
return &DropIndexes{
index: &index,
index: index,
}
}

Expand Down Expand Up @@ -109,19 +109,26 @@ func (di *DropIndexes) Execute(ctx context.Context) error {

func (di *DropIndexes) command(dst []byte, _ description.SelectedServer) ([]byte, error) {
dst = bsoncore.AppendStringElement(dst, "dropIndexes", di.collection)
if di.index != nil {
dst = bsoncore.AppendStringElement(dst, "index", *di.index)

switch di.index.(type) {
case string:
dst = bsoncore.AppendStringElement(dst, "index", di.index.(string))
case bsoncore.Document:
if di.index != nil {
dst = bsoncore.AppendDocumentElement(dst, "index", di.index.(bsoncore.Document))
}
}

return dst, nil
}

// Index specifies the name of the index to drop. If '*' is specified, all indexes will be dropped.
func (di *DropIndexes) Index(index string) *DropIndexes {
func (di *DropIndexes) Index(index any) *DropIndexes {
if di == nil {
di = new(DropIndexes)
}

di.index = &index
di.index = index
return di
}

Expand Down
Loading