blob: 2fda698ce5dfd343a0a2c5e1b572ce65de96c386 [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package options
8
9import (
10 "time"
11)
12
13// CreateIndexesOptions represents all possible options for the create() function.
14type CreateIndexesOptions struct {
15 MaxTime *time.Duration // The maximum amount of time to allow the query to run.
16}
17
18// CreateIndexes creates a new CreateIndexesOptions instance.
19func CreateIndexes() *CreateIndexesOptions {
20 return &CreateIndexesOptions{}
21}
22
23// SetMaxTime specifies the maximum amount of time to allow the query to run.
24func (c *CreateIndexesOptions) SetMaxTime(d time.Duration) *CreateIndexesOptions {
25 c.MaxTime = &d
26 return c
27}
28
29// MergeCreateIndexesOptions combines the given *CreateIndexesOptions into a single *CreateIndexesOptions in a last one
30// wins fashion.
31func MergeCreateIndexesOptions(opts ...*CreateIndexesOptions) *CreateIndexesOptions {
32 c := CreateIndexes()
33 for _, opt := range opts {
34 if opt == nil {
35 continue
36 }
37 if opt.MaxTime != nil {
38 c.MaxTime = opt.MaxTime
39 }
40 }
41
42 return c
43}
44
45// DropIndexesOptions represents all possible options for the create() function.
46type DropIndexesOptions struct {
47 MaxTime *time.Duration
48}
49
50// DropIndexes creates a new DropIndexesOptions instance.
51func DropIndexes() *DropIndexesOptions {
52 return &DropIndexesOptions{}
53}
54
55// SetMaxTime specifies the maximum amount of time to allow the query to run.
56func (d *DropIndexesOptions) SetMaxTime(duration time.Duration) *DropIndexesOptions {
57 d.MaxTime = &duration
58 return d
59}
60
61// MergeDropIndexesOptions combines the given *DropIndexesOptions into a single *DropIndexesOptions in a last one
62// wins fashion.
63func MergeDropIndexesOptions(opts ...*DropIndexesOptions) *DropIndexesOptions {
64 c := DropIndexes()
65 for _, opt := range opts {
66 if opt == nil {
67 continue
68 }
69 if opt.MaxTime != nil {
70 c.MaxTime = opt.MaxTime
71 }
72 }
73
74 return c
75}
76
77// ListIndexesOptions represents all possible options for the create() function.
78type ListIndexesOptions struct {
79 BatchSize *int32
80 MaxTime *time.Duration
81}
82
83// ListIndexes creates a new ListIndexesOptions instance.
84func ListIndexes() *ListIndexesOptions {
85 return &ListIndexesOptions{}
86}
87
88// SetBatchSize specifies the number of documents to return in every batch.
89func (l *ListIndexesOptions) SetBatchSize(i int32) *ListIndexesOptions {
90 l.BatchSize = &i
91 return l
92}
93
94// SetMaxTime specifies the maximum amount of time to allow the query to run.
95func (l *ListIndexesOptions) SetMaxTime(d time.Duration) *ListIndexesOptions {
96 l.MaxTime = &d
97 return l
98}
99
100// MergeListIndexesOptions combines the given *ListIndexesOptions into a single *ListIndexesOptions in a last one
101// wins fashion.
102func MergeListIndexesOptions(opts ...*ListIndexesOptions) *ListIndexesOptions {
103 c := ListIndexes()
104 for _, opt := range opts {
105 if opt == nil {
106 continue
107 }
108 if opt.MaxTime != nil {
109 c.MaxTime = opt.MaxTime
110 }
111 }
112
113 return c
114}
115
116// IndexOptions represents all possible options to configure a new index.
117type IndexOptions struct {
118 Background *bool
119 ExpireAfterSeconds *int32
120 Name *string
121 Sparse *bool
122 StorageEngine interface{}
123 Unique *bool
124 Version *int32
125 DefaultLanguage *string
126 LanguageOverride *string
127 TextVersion *int32
128 Weights interface{}
129 SphereVersion *int32
130 Bits *int32
131 Max *float64
132 Min *float64
133 BucketSize *int32
134 PartialFilterExpression interface{}
135 Collation *Collation
136}
137
138// Index creates a new *IndexOptions
139func Index() *IndexOptions {
140 return &IndexOptions{}
141}
142
143// SetBackground sets the background option. If true, the server will create the index in the background and not block
144// other tasks
145func (i *IndexOptions) SetBackground(background bool) *IndexOptions {
146 i.Background = &background
147 return i
148}
149
150// SetExpireAfterSeconds specifies the number of seconds for a document to remain in a collection.
151func (i *IndexOptions) SetExpireAfterSeconds(seconds int32) *IndexOptions {
152 i.ExpireAfterSeconds = &seconds
153 return i
154}
155
156// SetName specifies a name for the index.
157// If not set, a name will be generated in the format "[field]_[direction]".
158// If multiple indexes are created for the same key pattern with different collations, a name must be provided to avoid
159// ambiguity.
160func (i *IndexOptions) SetName(name string) *IndexOptions {
161 i.Name = &name
162 return i
163}
164
165// SetSparse sets the sparse option.
166// If true, the index will only reference documents with the specified field in the index.
167func (i *IndexOptions) SetSparse(sparse bool) *IndexOptions {
168 i.Sparse = &sparse
169 return i
170}
171
172// SetStorageEngine specifies the storage engine to use.
173// Valid for server versions >= 3.0
174func (i *IndexOptions) SetStorageEngine(engine interface{}) *IndexOptions {
175 i.StorageEngine = engine
176 return i
177}
178
179// SetUnique forces the index to be unique.
180func (i *IndexOptions) SetUnique(unique bool) *IndexOptions {
181 i.Unique = &unique
182 return i
183}
184
185// SetVersion specifies the index version number, either 0 or 1.
186func (i *IndexOptions) SetVersion(version int32) *IndexOptions {
187 i.Version = &version
188 return i
189}
190
191// SetDefaultLanguage specifies the default language for text indexes.
192// If not set, this will default to english.
193func (i *IndexOptions) SetDefaultLanguage(language string) *IndexOptions {
194 i.DefaultLanguage = &language
195 return i
196}
197
198// SetLanguageOverride specifies the field in the document to override the language.
199func (i *IndexOptions) SetLanguageOverride(override string) *IndexOptions {
200 i.LanguageOverride = &override
201 return i
202}
203
204// SetTextVersion specifies the text index version number.
205// MongoDB version 2.4 can only support version 1.
206// MongoDB versions 2.6 and higher can support versions 1 or 2.
207func (i *IndexOptions) SetTextVersion(version int32) *IndexOptions {
208 i.TextVersion = &version
209 return i
210}
211
212// SetWeights specifies fields in the index and their corresponding weight values.
213func (i *IndexOptions) SetWeights(weights interface{}) *IndexOptions {
214 i.Weights = weights
215 return i
216}
217
218// SetSphereVersion specifies the 2dsphere index version number.
219// MongoDB version 2.4 can only support version 1.
220// MongoDB versions 2.6 and higher can support versions 1 or 2.
221func (i *IndexOptions) SetSphereVersion(version int32) *IndexOptions {
222 i.SphereVersion = &version
223 return i
224}
225
226// SetBits specifies the precision of the stored geo hash in the 2d index, from 1 to 32.
227func (i *IndexOptions) SetBits(bits int32) *IndexOptions {
228 i.Bits = &bits
229 return i
230}
231
232// SetMax specifies the maximum boundary for latitude and longitude in the 2d index.
233func (i *IndexOptions) SetMax(max float64) *IndexOptions {
234 i.Max = &max
235 return i
236}
237
238// SetMin specifies the minimum boundary for latitude and longitude in the 2d index.
239func (i *IndexOptions) SetMin(min float64) *IndexOptions {
240 i.Min = &min
241 return i
242}
243
244// SetBucketSize specifies number of units within which to group the location values in a geo haystack index.
245func (i *IndexOptions) SetBucketSize(bucketSize int32) *IndexOptions {
246 i.BucketSize = &bucketSize
247 return i
248}
249
250// SetPartialFilterExpression specifies a filter for use in a partial index. Only documents that match the filter
251// expression are included in the index.
252func (i *IndexOptions) SetPartialFilterExpression(expression interface{}) *IndexOptions {
253 i.PartialFilterExpression = expression
254 return i
255}
256
257// SetCollation specifies a Collation to use for the operation.
258// Valid for server versions >= 3.4
259func (i *IndexOptions) SetCollation(collation *Collation) *IndexOptions {
260 i.Collation = collation
261 return i
262}
263
264// MergeIndexOptions combines the given *IndexOptions into a single *IndexOptions in a last one wins fashion.
265func MergeIndexOptions(opts ...*IndexOptions) *IndexOptions {
266 i := Index()
267
268 for _, opt := range opts {
269 if opt.Background != nil {
270 i.Background = opt.Background
271 }
272 if opt.ExpireAfterSeconds != nil {
273 i.ExpireAfterSeconds = opt.ExpireAfterSeconds
274 }
275 if opt.Name != nil {
276 i.Name = opt.Name
277 }
278 if opt.Sparse != nil {
279 i.Sparse = opt.Sparse
280 }
281 if opt.StorageEngine != nil {
282 i.StorageEngine = opt.StorageEngine
283 }
284 if opt.Unique != nil {
285 i.Unique = opt.Unique
286 }
287 if opt.Version != nil {
288 i.Version = opt.Version
289 }
290 if opt.DefaultLanguage != nil {
291 i.DefaultLanguage = opt.DefaultLanguage
292 }
293 if opt.LanguageOverride != nil {
294 i.LanguageOverride = opt.LanguageOverride
295 }
296 if opt.TextVersion != nil {
297 i.TextVersion = opt.TextVersion
298 }
299 if opt.Weights != nil {
300 i.Weights = opt.Weights
301 }
302 if opt.SphereVersion != nil {
303 i.SphereVersion = opt.SphereVersion
304 }
305 if opt.Bits != nil {
306 i.Bits = opt.Bits
307 }
308 if opt.Max != nil {
309 i.Max = opt.Max
310 }
311 if opt.Min != nil {
312 i.Min = opt.Min
313 }
314 if opt.BucketSize != nil {
315 i.BucketSize = opt.BucketSize
316 }
317 if opt.PartialFilterExpression != nil {
318 i.PartialFilterExpression = opt.PartialFilterExpression
319 }
320 if opt.Collation != nil {
321 i.Collation = opt.Collation
322 }
323 }
324
325 return i
326}