From b0d6b4f8c315fb1d559eb359ee5d9e7344514d08 Mon Sep 17 00:00:00 2001 From: Qingyang Hu Date: Fri, 25 Aug 2023 11:45:12 -0400 Subject: [PATCH] Embed an entire AggregateOptions struct into the ListSearchIndexOptions struct. --- mongo/integration/search_index_prose_test.go | 68 ++++++++----------- .../unified/collection_operation_execution.go | 8 ++- mongo/options/searchindexoptions.go | 1 + mongo/search_index_view.go | 18 ++--- 4 files changed, 44 insertions(+), 51 deletions(-) diff --git a/mongo/integration/search_index_prose_test.go b/mongo/integration/search_index_prose_test.go index facc16c60b..281ec46436 100644 --- a/mongo/integration/search_index_prose_test.go +++ b/mongo/integration/search_index_prose_test.go @@ -56,7 +56,7 @@ func TestSearchIndexProse(t *testing.T) { var doc bson.Raw for doc == nil { - cursor, err := view.List(ctx, &index, nil) + cursor, err := view.List(ctx, &index) require.NoError(mt, err, "failed to list") if !cursor.Next(ctx) { @@ -86,29 +86,26 @@ func TestSearchIndexProse(t *testing.T) { view := mt.Coll.SearchIndexes() definition := bson.D{{"mappings", bson.D{{"dynamic", false}}}} - searchName0 := "test-search-index-1" - searchName1 := "test-search-index-2" - models := []mongo.SearchIndexModel{ - { + searchNames := []string{"test-search-index-1", "test-search-index-2"} + models := make([]mongo.SearchIndexModel, len(searchNames)) + for i := range searchNames { + models[i] = mongo.SearchIndexModel{ Definition: definition, - Name: &searchName0, - }, - { - Definition: definition, - Name: &searchName1, - }, + Name: &searchNames[i], + } } indexes, err := view.CreateMany(ctx, models) require.NoError(mt, err, "failed to create index") require.Equal(mt, len(indexes), 2, "expected 2 indexes") - require.Contains(mt, indexes, searchName0) - require.Contains(mt, indexes, searchName1) + for _, searchName := range searchNames { + require.Contains(mt, indexes, searchName) + } getDocument := func(index string) bson.Raw { t.Helper() for { - cursor, err := view.List(ctx, &index, nil) + cursor, err := view.List(ctx, &index) require.NoError(mt, err, "failed to list") if !cursor.Next(ctx) { @@ -123,29 +120,20 @@ func TestSearchIndexProse(t *testing.T) { } var wg sync.WaitGroup - wg.Add(2) - go func() { - defer wg.Done() - - doc := getDocument(searchName0) - require.NotNil(mt, doc, "got empty document") - assert.Equal(mt, searchName0, doc.Lookup("name").StringValue(), "unmatched name") - expected, err := bson.Marshal(definition) - require.NoError(mt, err, "failed to marshal definition") - actual := doc.Lookup("latestDefinition").Value - assert.Equal(mt, expected, actual, "unmatched definition") - }() - go func() { - defer wg.Done() - - doc := getDocument(searchName1) - require.NotNil(mt, doc, "got empty document") - assert.Equal(mt, searchName1, doc.Lookup("name").StringValue(), "unmatched name") - expected, err := bson.Marshal(definition) - require.NoError(mt, err, "failed to marshal definition") - actual := doc.Lookup("latestDefinition").Value - assert.Equal(mt, expected, actual, "unmatched definition") - }() + wg.Add(len(searchNames)) + for i := range searchNames { + go func(name string) { + defer wg.Done() + + doc := getDocument(name) + require.NotNil(mt, doc, "got empty document") + assert.Equal(mt, name, doc.Lookup("name").StringValue(), "unmatched name") + expected, err := bson.Marshal(definition) + require.NoError(mt, err, "failed to marshal definition") + actual := doc.Lookup("latestDefinition").Value + assert.Equal(mt, expected, actual, "unmatched definition") + }(searchNames[i]) + } wg.Wait() }) @@ -169,7 +157,7 @@ func TestSearchIndexProse(t *testing.T) { var doc bson.Raw for doc == nil { - cursor, err := view.List(ctx, &index, nil) + cursor, err := view.List(ctx, &index) require.NoError(mt, err, "failed to list") if !cursor.Next(ctx) { @@ -188,7 +176,7 @@ func TestSearchIndexProse(t *testing.T) { err = view.DropOne(ctx, searchName) require.NoError(mt, err, "failed to drop index") for { - cursor, err := view.List(ctx, &index, nil) + cursor, err := view.List(ctx, &index) require.NoError(mt, err, "failed to list") if !cursor.Next(ctx) { @@ -221,7 +209,7 @@ func TestSearchIndexProse(t *testing.T) { t.Helper() for { - cursor, err := view.List(ctx, &index, nil) + cursor, err := view.List(ctx, &index) require.NoError(mt, err, "failed to list") if !cursor.Next(ctx) { diff --git a/mongo/integration/unified/collection_operation_execution.go b/mongo/integration/unified/collection_operation_execution.go index 08768ce694..6da5a9d401 100644 --- a/mongo/integration/unified/collection_operation_execution.go +++ b/mongo/integration/unified/collection_operation_execution.go @@ -1118,7 +1118,7 @@ func executeListSearchIndexes(ctx context.Context, operation *operation) (*opera } var name *string - var opts []*options.AggregateOptions + var opts []*options.ListSearchIndexesOptions elems, err := operation.Arguments.Elements() if err != nil { @@ -1138,13 +1138,15 @@ func executeListSearchIndexes(ctx context.Context, operation *operation) (*opera if err != nil { return nil, err } - opts = append(opts, &opt) + opts = append(opts, &options.ListSearchIndexesOptions{ + AggregateOpts: &opt, + }) default: return nil, fmt.Errorf("unrecognized listSearchIndexes option %q", key) } } - _, err = coll.SearchIndexes().List(ctx, name, opts) + _, err = coll.SearchIndexes().List(ctx, name, opts...) return newValueResult(bsontype.Null, nil, err), nil } diff --git a/mongo/options/searchindexoptions.go b/mongo/options/searchindexoptions.go index 64e585c49f..5f7b771153 100644 --- a/mongo/options/searchindexoptions.go +++ b/mongo/options/searchindexoptions.go @@ -13,6 +13,7 @@ type CreateSearchIndexesOptions struct { // ListSearchIndexesOptions represents options that can be used to configure a SearchIndexView.List operation. type ListSearchIndexesOptions struct { + AggregateOpts *AggregateOptions } // DropSearchIndexOptions represents options that can be used to configure a SearchIndexView.DropOne operation. diff --git a/mongo/search_index_view.go b/mongo/search_index_view.go index 4d0fa104ff..fdd12d2202 100644 --- a/mongo/search_index_view.go +++ b/mongo/search_index_view.go @@ -38,21 +38,23 @@ type SearchIndexModel struct { // List executes a listSearchIndexes command and returns a cursor over the search indexes in the collection. // -// The aggregateOpts parameter is the aggregation options. +// The name parameter specifies the index name. A nil pointer matches all indexes. // // The opts parameter can be used to specify options for this operation (see the options.ListSearchIndexesOptions // documentation). -func (siv SearchIndexView) List(ctx context.Context, name *string, - aggregateOpts []*options.AggregateOptions, _ ...*options.ListSearchIndexesOptions) (*Cursor, error) { +func (siv SearchIndexView) List(ctx context.Context, name *string, opts ...*options.ListSearchIndexesOptions) (*Cursor, error) { if ctx == nil { ctx = context.Background() } - var index bson.D - if name != nil && len(*name) > 0 { + index := bson.D{} + if name != nil { index = bson.D{{"name", *name}} - } else { - index = bson.D{} + } + + aggregateOpts := make([]*options.AggregateOptions, len(opts)) + for i, opt := range opts { + aggregateOpts[i] = opt.AggregateOpts } return siv.coll.Aggregate(ctx, Pipeline{{{"$listSearchIndexes", index}}}, aggregateOpts...) @@ -92,7 +94,7 @@ func (siv SearchIndexView) CreateMany(ctx context.Context, models []SearchIndexM var iidx int32 iidx, indexes = bsoncore.AppendDocumentElementStart(indexes, strconv.Itoa(i)) - if model.Name != nil && len(*model.Name) > 0 { + if model.Name != nil { indexes = bsoncore.AppendStringElement(indexes, "name", *model.Name) } indexes = bsoncore.AppendDocumentElement(indexes, "definition", definition)