Skip to content

Commit a6765b1

Browse files
committed
add index operations
1 parent 5cc147a commit a6765b1

File tree

6 files changed

+114
-7
lines changed

6 files changed

+114
-7
lines changed

internal/integration/unified/collection_operation_execution.go

Lines changed: 9 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,8 @@ 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+
opts.SetRawData(val.Boolean())
298301
default:
299302
return nil, fmt.Errorf("unrecognized createIndex option %q", key)
300303
}
@@ -307,7 +310,8 @@ func executeCreateIndex(ctx context.Context, operation *operation) (*operationRe
307310
Keys: keys,
308311
Options: indexOpts,
309312
}
310-
name, err := coll.Indexes().CreateOne(ctx, model)
313+
314+
name, err := coll.Indexes().CreateOne(ctx, model, opts)
311315
return newValueResult(bson.TypeString, bsoncore.AppendString(nil, name), err), nil
312316
}
313317

@@ -624,6 +628,8 @@ func executeDropIndex(ctx context.Context, operation *operation) (*operationResu
624628
// ensured an analogue exists, extend "skippedTestDescriptions" to avoid
625629
// this error.
626630
return nil, fmt.Errorf("the maxTimeMS collection option is not supported")
631+
case "rawData":
632+
dropIndexOpts.SetRawData(val.Boolean())
627633
default:
628634
return nil, fmt.Errorf("unrecognized dropIndex option %q", key)
629635
}
@@ -1217,6 +1223,8 @@ func executeListIndexes(ctx context.Context, operation *operation) (*operationRe
12171223
switch key {
12181224
case "batchSize":
12191225
opts.SetBatchSize(val.Int32())
1226+
case "rawData":
1227+
opts.SetRawData(val.Boolean())
12201228
default:
12211229
return nil, fmt.Errorf("unrecognized listIndexes option: %q", key)
12221230
}

mongo/index_view.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ func (iv IndexView) List(ctx context.Context, opts ...options.Lister[options.Lis
101101
op = op.BatchSize(*args.BatchSize)
102102
cursorOpts.BatchSize = *args.BatchSize
103103
}
104+
if args.RawData != nil {
105+
op = op.RawData(*args.RawData)
106+
}
104107

105108
retry := driver.RetryNone
106109
if iv.coll.client.retryReads {
@@ -279,6 +282,9 @@ func (iv IndexView) CreateMany(
279282

280283
op.CommitQuorum(commitQuorum)
281284
}
285+
if args.RawData != nil {
286+
op = op.RawData(*args.RawData)
287+
}
282288

283289
_, err = processWriteError(op.Execute(ctx))
284290
if err != nil {
@@ -376,7 +382,12 @@ func (iv IndexView) createOptionsDoc(opts options.Lister[options.IndexOptions])
376382
return optsDoc, nil
377383
}
378384

379-
func (iv IndexView) drop(ctx context.Context, index any, _ ...options.Lister[options.DropIndexesOptions]) error {
385+
func (iv IndexView) drop(ctx context.Context, index any, opts ...options.Lister[options.DropIndexesOptions]) error {
386+
args, err := mongoutil.NewOptions[options.DropIndexesOptions](opts...)
387+
if err != nil {
388+
return fmt.Errorf("failed to construct options from builder: %w", err)
389+
}
390+
380391
if ctx == nil {
381392
ctx = context.Background()
382393
}
@@ -387,7 +398,7 @@ func (iv IndexView) drop(ctx context.Context, index any, _ ...options.Lister[opt
387398
defer sess.EndSession()
388399
}
389400

390-
err := iv.coll.client.validSession(sess)
401+
err = iv.coll.client.validSession(sess)
391402
if err != nil {
392403
return err
393404
}
@@ -408,6 +419,10 @@ func (iv IndexView) drop(ctx context.Context, index any, _ ...options.Lister[opt
408419
Deployment(iv.coll.client.deployment).ServerAPI(iv.coll.client.serverAPI).
409420
Timeout(iv.coll.client.timeout).Crypt(iv.coll.client.cryptFLE).Authenticator(iv.coll.client.authenticator)
410421

422+
if args.RawData != nil {
423+
op = op.RawData(*args.RawData)
424+
}
425+
411426
err = op.Execute(ctx)
412427
if err != nil {
413428
return wrapErrors(err)

mongo/options/indexoptions.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package options
1212
// See corresponding setter methods for documentation.
1313
type CreateIndexesOptions struct {
1414
CommitQuorum interface{}
15+
RawData *bool
1516
}
1617

1718
// CreateIndexesOptionsBuilder contains options to create indexes. Each option
@@ -119,9 +120,23 @@ func (c *CreateIndexesOptionsBuilder) SetCommitQuorumVotingMembers() *CreateInde
119120
return c
120121
}
121122

123+
// SetRawData sets the value for the RawData field. If true, it allows the CRUD operations to access timeseries
124+
// collections on the bucket-level. This option is only valid for MongoDB versions >= 9.0. The default value is false.
125+
func (c *CreateIndexesOptionsBuilder) SetRawData(rawData bool) *CreateIndexesOptionsBuilder {
126+
c.Opts = append(c.Opts, func(opts *CreateIndexesOptions) error {
127+
opts.RawData = &rawData
128+
129+
return nil
130+
})
131+
132+
return c
133+
}
134+
122135
// DropIndexesOptions represents arguments that can be used to configure
123136
// IndexView.DropOne and IndexView.DropAll operations.
124-
type DropIndexesOptions struct{}
137+
type DropIndexesOptions struct {
138+
RawData *bool
139+
}
125140

126141
// DropIndexesOptionsBuilder contains options to configure dropping indexes.
127142
// Each option can be set through setter functions. See documentation for each
@@ -140,12 +155,25 @@ func (d *DropIndexesOptionsBuilder) List() []func(*DropIndexesOptions) error {
140155
return d.Opts
141156
}
142157

158+
// SetRawData sets the value for the RawData field. If true, it allows the CRUD operations to access timeseries
159+
// collections on the bucket-level. This option is only valid for MongoDB versions >= 9.0. The default value is false.
160+
func (d *DropIndexesOptionsBuilder) SetRawData(rawData bool) *DropIndexesOptionsBuilder {
161+
d.Opts = append(d.Opts, func(opts *DropIndexesOptions) error {
162+
opts.RawData = &rawData
163+
164+
return nil
165+
})
166+
167+
return d
168+
}
169+
143170
// ListIndexesOptions represents arguments that can be used to configure an
144171
// IndexView.List operation.
145172
//
146173
// See corresponding setter methods for documentation.
147174
type ListIndexesOptions struct {
148175
BatchSize *int32
176+
RawData *bool
149177
}
150178

151179
// ListIndexesOptionsBuilder contains options to configure count operations. Each
@@ -177,6 +205,18 @@ func (l *ListIndexesOptionsBuilder) SetBatchSize(i int32) *ListIndexesOptionsBui
177205
return l
178206
}
179207

208+
// SetRawData sets the value for the RawData field. If true, it allows the CRUD operations to access timeseries
209+
// collections on the bucket-level. This option is only valid for MongoDB versions >= 9.0. The default value is false.
210+
func (l *ListIndexesOptionsBuilder) SetRawData(rawData bool) *ListIndexesOptionsBuilder {
211+
l.Opts = append(l.Opts, func(opts *ListIndexesOptions) error {
212+
opts.RawData = &rawData
213+
214+
return nil
215+
})
216+
217+
return l
218+
}
219+
180220
// IndexOptions represents arguments that can be used to configure a new index
181221
// created through the IndexView.CreateOne or IndexView.CreateMany operations.
182222
//

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+
}

0 commit comments

Comments
 (0)