Skip to content

Commit 8991f81

Browse files
author
Divjot Arora
committed
Add IndexOptions to configure options for creating indexes.
GODRIVER-729 Change-Id: I07dee1b3dbfbb4b9d9537f8aa6109f476a1cb820
1 parent 2fb348b commit 8991f81

File tree

5 files changed

+342
-49
lines changed

5 files changed

+342
-49
lines changed

mongo/index_view.go

Lines changed: 82 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type IndexView struct {
3838
// IndexModel contains information about an index.
3939
type IndexModel struct {
4040
Keys interface{}
41-
Options interface{}
41+
Options *options.IndexOptions
4242
}
4343

4444
// List returns a cursor iterating over all the indexes in the collection.
@@ -100,11 +100,12 @@ func (iv IndexView) CreateMany(ctx context.Context, models []IndexModel, opts ..
100100
}
101101
index := bsonx.Doc{{"key", bsonx.Document(keys)}}
102102
if model.Options != nil {
103-
options, err := transformDocument(iv.coll.registry, model.Options)
103+
optsDoc, err := iv.createOptionsDoc(model.Options)
104104
if err != nil {
105105
return nil, err
106106
}
107-
index = append(index, options...)
107+
108+
index = append(index, optsDoc...)
108109
}
109110
index = index.Set("name", bsonx.String(name))
110111

@@ -140,6 +141,82 @@ func (iv IndexView) CreateMany(ctx context.Context, models []IndexModel, opts ..
140141
return names, nil
141142
}
142143

144+
func (iv IndexView) createOptionsDoc(opts *options.IndexOptions) (bsonx.Doc, error) {
145+
optsDoc := bsonx.Doc{}
146+
if opts.Background != nil {
147+
optsDoc = append(optsDoc, bsonx.Elem{"background", bsonx.Boolean(*opts.Background)})
148+
}
149+
if opts.ExpireAfterSeconds != nil {
150+
optsDoc = append(optsDoc, bsonx.Elem{"expireAfterSeconds", bsonx.Int32(*opts.ExpireAfterSeconds)})
151+
}
152+
if opts.Name != nil {
153+
optsDoc = append(optsDoc, bsonx.Elem{"name", bsonx.String(*opts.Name)})
154+
}
155+
if opts.Sparse != nil {
156+
optsDoc = append(optsDoc, bsonx.Elem{"sparse", bsonx.Boolean(*opts.Sparse)})
157+
}
158+
if opts.StorageEngine != nil {
159+
doc, err := transformDocument(iv.coll.registry, opts.StorageEngine)
160+
if err != nil {
161+
return nil, err
162+
}
163+
164+
optsDoc = append(optsDoc, bsonx.Elem{"storageEngine", bsonx.Document(doc)})
165+
}
166+
if opts.Unique != nil {
167+
optsDoc = append(optsDoc, bsonx.Elem{"unique", bsonx.Boolean(*opts.Unique)})
168+
}
169+
if opts.Version != nil {
170+
optsDoc = append(optsDoc, bsonx.Elem{"v", bsonx.Int32(*opts.Version)})
171+
}
172+
if opts.DefaultLanguage != nil {
173+
optsDoc = append(optsDoc, bsonx.Elem{"default_language", bsonx.String(*opts.DefaultLanguage)})
174+
}
175+
if opts.LanguageOverride != nil {
176+
optsDoc = append(optsDoc, bsonx.Elem{"language_override", bsonx.String(*opts.LanguageOverride)})
177+
}
178+
if opts.TextVersion != nil {
179+
optsDoc = append(optsDoc, bsonx.Elem{"textIndexVersion", bsonx.Int32(*opts.TextVersion)})
180+
}
181+
if opts.Weights != nil {
182+
weightsDoc, err := transformDocument(iv.coll.registry, opts.Weights)
183+
if err != nil {
184+
return nil, err
185+
}
186+
187+
optsDoc = append(optsDoc, bsonx.Elem{"weights", bsonx.Document(weightsDoc)})
188+
}
189+
if opts.SphereVersion != nil {
190+
optsDoc = append(optsDoc, bsonx.Elem{"2dsphereIndexVersion", bsonx.Int32(*opts.SphereVersion)})
191+
}
192+
if opts.Bits != nil {
193+
optsDoc = append(optsDoc, bsonx.Elem{"bits", bsonx.Int32(*opts.Bits)})
194+
}
195+
if opts.Max != nil {
196+
optsDoc = append(optsDoc, bsonx.Elem{"max", bsonx.Double(*opts.Max)})
197+
}
198+
if opts.Min != nil {
199+
optsDoc = append(optsDoc, bsonx.Elem{"min", bsonx.Double(*opts.Min)})
200+
}
201+
if opts.BucketSize != nil {
202+
optsDoc = append(optsDoc, bsonx.Elem{"bucketSize", bsonx.Int32(*opts.BucketSize)})
203+
}
204+
if opts.PartialFilterExpression != nil {
205+
doc, err := transformDocument(iv.coll.registry, opts.PartialFilterExpression)
206+
if err != nil {
207+
return nil, err
208+
}
209+
210+
optsDoc = append(optsDoc, bsonx.Elem{"partialFilterExpression", bsonx.Document(doc)})
211+
}
212+
if opts.Collation != nil {
213+
doc := opts.Collation.ToDocument()
214+
optsDoc = append(optsDoc, bsonx.Elem{"collation", bsonx.Document(doc)})
215+
}
216+
217+
return optsDoc, nil
218+
}
219+
143220
// DropOne drops the index with the given name from the collection.
144221
func (iv IndexView) DropOne(ctx context.Context, name string, opts ...*options.DropIndexesOptions) (bson.Raw, error) {
145222
if name == "*" {
@@ -197,25 +274,8 @@ func (iv IndexView) DropAll(ctx context.Context, opts ...*options.DropIndexesOpt
197274
}
198275

199276
func getOrGenerateIndexName(registry *bsoncodec.Registry, model IndexModel) (string, error) {
200-
if model.Options != nil {
201-
options, err := transformDocument(registry, model.Options)
202-
if err != nil {
203-
return "", err
204-
}
205-
nameVal, err := options.LookupErr("name")
206-
207-
switch err.(type) {
208-
case bsonx.KeyNotFound:
209-
break
210-
case nil:
211-
if nameVal.Type() != bson.TypeString {
212-
return "", ErrNonStringIndexName
213-
}
214-
215-
return nameVal.StringValue(), nil
216-
default:
217-
return "", err
218-
}
277+
if model.Options != nil && model.Options.Name != nil {
278+
return *model.Options.Name, nil
219279
}
220280

221281
name := bytes.NewBufferString("")

mongo/index_view_internal_test.go

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestIndexView_CreateOneWithNameOption(t *testing.T) {
139139
context.Background(),
140140
IndexModel{
141141
Keys: bsonx.Doc{{"foo", bsonx.Int32(-1)}},
142-
Options: NewIndexOptionsBuilder().Name("testname").Build(),
142+
Options: options.Index().SetName("testname"),
143143
},
144144
)
145145
require.NoError(t, err)
@@ -182,31 +182,35 @@ func TestIndexView_CreateOneWithAllOptions(t *testing.T) {
182182
context.Background(),
183183
IndexModel{
184184
Keys: bsonx.Doc{{"foo", bsonx.String("text")}},
185-
Options: NewIndexOptionsBuilder().
186-
Background(false).
187-
ExpireAfterSeconds(10).
188-
Name("a").
189-
Sparse(false).
190-
Unique(false).
191-
Version(1).
192-
DefaultLanguage("english").
193-
LanguageOverride("english").
194-
TextVersion(1).
195-
Weights(bsonx.Doc{}).
196-
SphereVersion(1).
197-
Bits(32).
198-
Max(10).
199-
Min(1).
200-
BucketSize(1).
201-
PartialFilterExpression(bsonx.Doc{}).
202-
StorageEngine(bsonx.Doc{{"wiredTiger", bsonx.Document(bsonx.Doc{{"configString", bsonx.String("block_compressor=zlib")}})}}).
203-
Build(),
185+
Options: options.Index().
186+
SetBackground(false).
187+
SetExpireAfterSeconds(10).
188+
SetName("a").
189+
SetSparse(false).
190+
SetUnique(false).
191+
SetVersion(1).
192+
SetDefaultLanguage("english").
193+
SetLanguageOverride("english").
194+
SetTextVersion(1).
195+
SetWeights(bsonx.Doc{}).
196+
SetSphereVersion(1).
197+
SetBits(2).
198+
SetMax(10).
199+
SetMin(1).
200+
SetBucketSize(1).
201+
SetPartialFilterExpression(bsonx.Doc{}).
202+
SetStorageEngine(bsonx.Doc{
203+
{"wiredTiger", bsonx.Document(bsonx.Doc{
204+
{"configString", bsonx.String("block_compressor=zlib")},
205+
})},
206+
}),
204207
},
205208
)
206209
require.NoError(t, err)
207210
}
208211

209212
func TestIndexView_CreateOneWithCollationOption(t *testing.T) {
213+
skipIfBelow34(t, createTestDatabase(t, nil)) // collation invalid for server versions < 3.4
210214
t.Parallel()
211215

212216
if testing.Short() {
@@ -220,9 +224,9 @@ func TestIndexView_CreateOneWithCollationOption(t *testing.T) {
220224
context.Background(),
221225
IndexModel{
222226
Keys: bsonx.Doc{{"bar", bsonx.String("text")}},
223-
Options: NewIndexOptionsBuilder().
224-
Collation(bsonx.Doc{{"locale", bsonx.String("simple")}}).
225-
Build(),
227+
Options: options.Index().SetCollation(&options.Collation{
228+
Locale: "simple",
229+
}),
226230
},
227231
)
228232
require.NoError(t, err)

0 commit comments

Comments
 (0)