Skip to content

Commit

Permalink
Embed an entire AggregateOptions struct into the ListSearchIndexOptio…
Browse files Browse the repository at this point in the history
…ns struct.
  • Loading branch information
qingyang-hu committed Aug 25, 2023
1 parent 73d6633 commit b0d6b4f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 51 deletions.
68 changes: 28 additions & 40 deletions mongo/integration/search_index_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
})

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions mongo/integration/unified/collection_operation_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions mongo/options/searchindexoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 10 additions & 8 deletions mongo/search_index_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b0d6b4f

Please sign in to comment.