Skip to content

Commit 9170086

Browse files
committed
add index operations
1 parent 46708b2 commit 9170086

File tree

7 files changed

+167
-7
lines changed

7 files changed

+167
-7
lines changed

internal/integration/unified/collection_operation_execution.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ func executeCreateIndex(ctx context.Context, operation *operation) (*operationRe
241241

242242
var keys bson.Raw
243243
indexOpts := options.Index()
244+
opts := options.CreateIndexes()
244245

245246
elems, err := operation.Arguments.Elements()
246247
if err != nil {
@@ -295,6 +296,11 @@ func executeCreateIndex(ctx context.Context, operation *operation) (*operationRe
295296
indexOpts.SetWeights(val.Document())
296297
case "wildcardProjection":
297298
indexOpts.SetWildcardProjection(val.Document())
299+
case "rawData":
300+
err = xoptions.SetInternalCreateIndexesOptions(opts, key, val.Boolean())
301+
if err != nil {
302+
return nil, err
303+
}
298304
default:
299305
return nil, fmt.Errorf("unrecognized createIndex option %q", key)
300306
}
@@ -307,7 +313,8 @@ func executeCreateIndex(ctx context.Context, operation *operation) (*operationRe
307313
Keys: keys,
308314
Options: indexOpts,
309315
}
310-
name, err := coll.Indexes().CreateOne(ctx, model)
316+
317+
name, err := coll.Indexes().CreateOne(ctx, model, opts)
311318
return newValueResult(bson.TypeString, bsoncore.AppendString(nil, name), err), nil
312319
}
313320

@@ -624,6 +631,11 @@ func executeDropIndex(ctx context.Context, operation *operation) (*operationResu
624631
// ensured an analogue exists, extend "skippedTestDescriptions" to avoid
625632
// this error.
626633
return nil, fmt.Errorf("the maxTimeMS collection option is not supported")
634+
case "rawData":
635+
err = xoptions.SetInternalDropIndexesOptions(dropIndexOpts, key, val.Boolean())
636+
if err != nil {
637+
return nil, err
638+
}
627639
default:
628640
return nil, fmt.Errorf("unrecognized dropIndex option %q", key)
629641
}
@@ -1217,6 +1229,11 @@ func executeListIndexes(ctx context.Context, operation *operation) (*operationRe
12171229
switch key {
12181230
case "batchSize":
12191231
opts.SetBatchSize(val.Int32())
1232+
case "rawData":
1233+
err = xoptions.SetInternalListIndexesOptions(opts, key, val.Boolean())
1234+
if err != nil {
1235+
return nil, err
1236+
}
12201237
default:
12211238
return nil, fmt.Errorf("unrecognized listIndexes option: %q", key)
12221239
}

mongo/index_view.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strconv"
1515

1616
"go.mongodb.org/mongo-driver/v2/internal/mongoutil"
17+
"go.mongodb.org/mongo-driver/v2/internal/optionsutil"
1718
"go.mongodb.org/mongo-driver/v2/internal/serverselector"
1819
"go.mongodb.org/mongo-driver/v2/mongo/options"
1920
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
@@ -101,6 +102,11 @@ func (iv IndexView) List(ctx context.Context, opts ...options.Lister[options.Lis
101102
op = op.BatchSize(*args.BatchSize)
102103
cursorOpts.BatchSize = *args.BatchSize
103104
}
105+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
106+
if rawData, ok := rawDataOpt.(bool); ok {
107+
op = op.RawData(rawData)
108+
}
109+
}
104110

105111
retry := driver.RetryNone
106112
if iv.coll.client.retryReads {
@@ -279,6 +285,11 @@ func (iv IndexView) CreateMany(
279285

280286
op.CommitQuorum(commitQuorum)
281287
}
288+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
289+
if rawData, ok := rawDataOpt.(bool); ok {
290+
op = op.RawData(rawData)
291+
}
292+
}
282293

283294
_, err = processWriteError(op.Execute(ctx))
284295
if err != nil {
@@ -376,7 +387,12 @@ func (iv IndexView) createOptionsDoc(opts options.Lister[options.IndexOptions])
376387
return optsDoc, nil
377388
}
378389

379-
func (iv IndexView) drop(ctx context.Context, index any, _ ...options.Lister[options.DropIndexesOptions]) error {
390+
func (iv IndexView) drop(ctx context.Context, index any, opts ...options.Lister[options.DropIndexesOptions]) error {
391+
args, err := mongoutil.NewOptions[options.DropIndexesOptions](opts...)
392+
if err != nil {
393+
return fmt.Errorf("failed to construct options from builder: %w", err)
394+
}
395+
380396
if ctx == nil {
381397
ctx = context.Background()
382398
}
@@ -387,7 +403,7 @@ func (iv IndexView) drop(ctx context.Context, index any, _ ...options.Lister[opt
387403
defer sess.EndSession()
388404
}
389405

390-
err := iv.coll.client.validSession(sess)
406+
err = iv.coll.client.validSession(sess)
391407
if err != nil {
392408
return err
393409
}
@@ -408,6 +424,12 @@ func (iv IndexView) drop(ctx context.Context, index any, _ ...options.Lister[opt
408424
Deployment(iv.coll.client.deployment).ServerAPI(iv.coll.client.serverAPI).
409425
Timeout(iv.coll.client.timeout).Crypt(iv.coll.client.cryptFLE).Authenticator(iv.coll.client.authenticator)
410426

427+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
428+
if rawData, ok := rawDataOpt.(bool); ok {
429+
op = op.RawData(rawData)
430+
}
431+
}
432+
411433
err = op.Execute(ctx)
412434
if err != nil {
413435
return wrapErrors(err)

mongo/options/indexoptions.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66

77
package options
88

9+
import "go.mongodb.org/mongo-driver/v2/internal/optionsutil"
10+
911
// CreateIndexesOptions represents arguments that can be used to configure
1012
// IndexView.CreateOne and IndexView.CreateMany operations.
1113
//
1214
// See corresponding setter methods for documentation.
1315
type CreateIndexesOptions struct {
1416
CommitQuorum interface{}
17+
18+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
19+
// release.
20+
Internal optionsutil.Options
1521
}
1622

1723
// CreateIndexesOptionsBuilder contains options to create indexes. Each option
@@ -121,7 +127,11 @@ func (c *CreateIndexesOptionsBuilder) SetCommitQuorumVotingMembers() *CreateInde
121127

122128
// DropIndexesOptions represents arguments that can be used to configure
123129
// IndexView.DropOne and IndexView.DropAll operations.
124-
type DropIndexesOptions struct{}
130+
type DropIndexesOptions struct {
131+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
132+
// release.
133+
Internal optionsutil.Options
134+
}
125135

126136
// DropIndexesOptionsBuilder contains options to configure dropping indexes.
127137
// Each option can be set through setter functions. See documentation for each
@@ -146,6 +156,10 @@ func (d *DropIndexesOptionsBuilder) List() []func(*DropIndexesOptions) error {
146156
// See corresponding setter methods for documentation.
147157
type ListIndexesOptions struct {
148158
BatchSize *int32
159+
160+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
161+
// release.
162+
Internal optionsutil.Options
149163
}
150164

151165
// ListIndexesOptionsBuilder contains options to configure count operations. Each

x/mongo/driver/operation/create_indexes.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type CreateIndexes struct {
3838
result CreateIndexesResult
3939
serverAPI *driver.ServerAPIOptions
4040
timeout *time.Duration
41+
rawData *bool
4142
}
4243

4344
// CreateIndexesResult represents a createIndexes result returned by the server.
@@ -133,6 +134,10 @@ func (ci *CreateIndexes) command(dst []byte, desc description.SelectedServer) ([
133134
if ci.indexes != nil {
134135
dst = bsoncore.AppendArrayElement(dst, "indexes", ci.indexes)
135136
}
137+
// Set rawData for 8.2+ servers.
138+
if ci.rawData != nil && desc.WireVersion != nil && driverutil.VersionRangeIncludes(*desc.WireVersion, 27) {
139+
dst = bsoncore.AppendBooleanElement(dst, "rawData", *ci.rawData)
140+
}
136141
return dst, nil
137142
}
138143

@@ -277,3 +282,13 @@ func (ci *CreateIndexes) Authenticator(authenticator driver.Authenticator) *Crea
277282
ci.authenticator = authenticator
278283
return ci
279284
}
285+
286+
// RawData sets the rawData to access timeseries data in the compressed format.
287+
func (ci *CreateIndexes) RawData(rawData bool) *CreateIndexes {
288+
if ci == nil {
289+
ci = new(CreateIndexes)
290+
}
291+
292+
ci.rawData = &rawData
293+
return ci
294+
}

x/mongo/driver/operation/drop_indexes.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type DropIndexes struct {
3737
result DropIndexesResult
3838
serverAPI *driver.ServerAPIOptions
3939
timeout *time.Duration
40+
rawData *bool
4041
}
4142

4243
// DropIndexesResult represents a dropIndexes result returned by the server.
@@ -104,7 +105,7 @@ func (di *DropIndexes) Execute(ctx context.Context) error {
104105

105106
}
106107

107-
func (di *DropIndexes) command(dst []byte, _ description.SelectedServer) ([]byte, error) {
108+
func (di *DropIndexes) command(dst []byte, desc description.SelectedServer) ([]byte, error) {
108109
dst = bsoncore.AppendStringElement(dst, "dropIndexes", di.collection)
109110

110111
switch t := di.index.(type) {
@@ -115,6 +116,10 @@ func (di *DropIndexes) command(dst []byte, _ description.SelectedServer) ([]byte
115116
dst = bsoncore.AppendDocumentElement(dst, "index", t)
116117
}
117118
}
119+
// Set rawData for 8.2+ servers.
120+
if di.rawData != nil && desc.WireVersion != nil && driverutil.VersionRangeIncludes(*desc.WireVersion, 27) {
121+
dst = bsoncore.AppendBooleanElement(dst, "rawData", *di.rawData)
122+
}
118123

119124
return dst, nil
120125
}
@@ -248,3 +253,13 @@ func (di *DropIndexes) Authenticator(authenticator driver.Authenticator) *DropIn
248253
di.authenticator = authenticator
249254
return di
250255
}
256+
257+
// RawData sets the rawData to access timeseries data in the compressed format.
258+
func (di *DropIndexes) RawData(rawData bool) *DropIndexes {
259+
if di == nil {
260+
di = new(DropIndexes)
261+
}
262+
263+
di.rawData = &rawData
264+
return di
265+
}

x/mongo/driver/operation/list_indexes.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type ListIndexes struct {
3434
crypt driver.Crypt
3535
serverAPI *driver.ServerAPIOptions
3636
timeout *time.Duration
37+
rawData *bool
3738

3839
result driver.CursorResponse
3940
}
@@ -91,16 +92,19 @@ func (li *ListIndexes) Execute(ctx context.Context) error {
9192

9293
}
9394

94-
func (li *ListIndexes) command(dst []byte, _ description.SelectedServer) ([]byte, error) {
95+
func (li *ListIndexes) command(dst []byte, desc description.SelectedServer) ([]byte, error) {
9596
dst = bsoncore.AppendStringElement(dst, "listIndexes", li.collection)
9697
cursorIdx, cursorDoc := bsoncore.AppendDocumentStart(nil)
9798

9899
if li.batchSize != nil {
99-
100100
cursorDoc = bsoncore.AppendInt32Element(cursorDoc, "batchSize", *li.batchSize)
101101
}
102102
cursorDoc, _ = bsoncore.AppendDocumentEnd(cursorDoc, cursorIdx)
103103
dst = bsoncore.AppendDocumentElement(dst, "cursor", cursorDoc)
104+
// Set rawData for 8.2+ servers.
105+
if li.rawData != nil && desc.WireVersion != nil && driverutil.VersionRangeIncludes(*desc.WireVersion, 27) {
106+
dst = bsoncore.AppendBooleanElement(dst, "rawData", *li.rawData)
107+
}
104108

105109
return dst, nil
106110
}
@@ -235,3 +239,13 @@ func (li *ListIndexes) Authenticator(authenticator driver.Authenticator) *ListIn
235239
li.authenticator = authenticator
236240
return li
237241
}
242+
243+
// RawData sets the rawData to access timeseries data in the compressed format.
244+
func (li *ListIndexes) RawData(rawData bool) *ListIndexes {
245+
if li == nil {
246+
li = new(ListIndexes)
247+
}
248+
249+
li.rawData = &rawData
250+
return li
251+
}

x/mongo/driver/xoptions/options.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,27 @@ func SetInternalCountOptions(a *options.CountOptionsBuilder, key string, option
128128
return nil
129129
}
130130

131+
// SetInternalCreateIndexesOptions sets internal options for CreateIndexesOptions.
132+
func SetInternalCreateIndexesOptions(a *options.CreateIndexesOptionsBuilder, key string, option any) error {
133+
typeErrFunc := func(t string) error {
134+
return fmt.Errorf("unexpected type for %q: %T is not %s", key, option, t)
135+
}
136+
switch key {
137+
case "rawData":
138+
b, ok := option.(bool)
139+
if !ok {
140+
return typeErrFunc("bool")
141+
}
142+
a.Opts = append(a.Opts, func(opts *options.CreateIndexesOptions) error {
143+
opts.Internal = optionsutil.WithValue(opts.Internal, key, b)
144+
return nil
145+
})
146+
default:
147+
return fmt.Errorf("unsupported option: %q", key)
148+
}
149+
return nil
150+
}
151+
131152
// SetInternalDeleteOneOptions sets internal options for DeleteOneOptions.
132153
func SetInternalDeleteOneOptions(a *options.DeleteOneOptionsBuilder, key string, option any) error {
133154
typeErrFunc := func(t string) error {
@@ -191,6 +212,27 @@ func SetInternalDistinctOptions(a *options.DistinctOptionsBuilder, key string, o
191212
return nil
192213
}
193214

215+
// SetInternalDropIndexesOptions sets internal options for DropIndexesOptions.
216+
func SetInternalDropIndexesOptions(a *options.DropIndexesOptionsBuilder, key string, option any) error {
217+
typeErrFunc := func(t string) error {
218+
return fmt.Errorf("unexpected type for %q: %T is not %s", key, option, t)
219+
}
220+
switch key {
221+
case "rawData":
222+
b, ok := option.(bool)
223+
if !ok {
224+
return typeErrFunc("bool")
225+
}
226+
a.Opts = append(a.Opts, func(opts *options.DropIndexesOptions) error {
227+
opts.Internal = optionsutil.WithValue(opts.Internal, key, b)
228+
return nil
229+
})
230+
default:
231+
return fmt.Errorf("unsupported option: %q", key)
232+
}
233+
return nil
234+
}
235+
194236
// SetInternalEstimatedDocumentCountOptions sets internal options for EstimatedDocumentCountOptions.
195237
func SetInternalEstimatedDocumentCountOptions(a *options.EstimatedDocumentCountOptionsBuilder, key string, option any) error {
196238
typeErrFunc := func(t string) error {
@@ -380,6 +422,27 @@ func SetInternalListCollectionsOptions(a *options.ListCollectionsOptionsBuilder,
380422
return nil
381423
}
382424

425+
// SetInternalListIndexesOptions sets internal options for ListIndexesOptions.
426+
func SetInternalListIndexesOptions(a *options.ListIndexesOptionsBuilder, key string, option any) error {
427+
typeErrFunc := func(t string) error {
428+
return fmt.Errorf("unexpected type for %q: %T is not %s", key, option, t)
429+
}
430+
switch key {
431+
case "rawData":
432+
b, ok := option.(bool)
433+
if !ok {
434+
return typeErrFunc("bool")
435+
}
436+
a.Opts = append(a.Opts, func(opts *options.ListIndexesOptions) error {
437+
opts.Internal = optionsutil.WithValue(opts.Internal, key, b)
438+
return nil
439+
})
440+
default:
441+
return fmt.Errorf("unsupported option: %q", key)
442+
}
443+
return nil
444+
}
445+
383446
// SetInternalReplaceOptions sets internal options for ReplaceOptions.
384447
func SetInternalReplaceOptions(a *options.ReplaceOptionsBuilder, key string, option any) error {
385448
typeErrFunc := func(t string) error {

0 commit comments

Comments
 (0)