Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // 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 | |
| 7 | package options |
| 8 | |
| 9 | import ( |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // CreateIndexesOptions represents all possible options for the create() function. |
| 14 | type 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. |
| 19 | func CreateIndexes() *CreateIndexesOptions { |
| 20 | return &CreateIndexesOptions{} |
| 21 | } |
| 22 | |
| 23 | // SetMaxTime specifies the maximum amount of time to allow the query to run. |
| 24 | func (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. |
| 31 | func 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. |
| 46 | type DropIndexesOptions struct { |
| 47 | MaxTime *time.Duration |
| 48 | } |
| 49 | |
| 50 | // DropIndexes creates a new DropIndexesOptions instance. |
| 51 | func DropIndexes() *DropIndexesOptions { |
| 52 | return &DropIndexesOptions{} |
| 53 | } |
| 54 | |
| 55 | // SetMaxTime specifies the maximum amount of time to allow the query to run. |
| 56 | func (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. |
| 63 | func 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. |
| 78 | type ListIndexesOptions struct { |
| 79 | BatchSize *int32 |
| 80 | MaxTime *time.Duration |
| 81 | } |
| 82 | |
| 83 | // ListIndexes creates a new ListIndexesOptions instance. |
| 84 | func ListIndexes() *ListIndexesOptions { |
| 85 | return &ListIndexesOptions{} |
| 86 | } |
| 87 | |
| 88 | // SetBatchSize specifies the number of documents to return in every batch. |
| 89 | func (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. |
| 95 | func (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. |
| 102 | func 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. |
| 117 | type 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 |
| 139 | func 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 |
| 145 | func (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. |
| 151 | func (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. |
| 160 | func (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. |
| 167 | func (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 |
| 174 | func (i *IndexOptions) SetStorageEngine(engine interface{}) *IndexOptions { |
| 175 | i.StorageEngine = engine |
| 176 | return i |
| 177 | } |
| 178 | |
| 179 | // SetUnique forces the index to be unique. |
| 180 | func (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. |
| 186 | func (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. |
| 193 | func (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. |
| 199 | func (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. |
| 207 | func (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. |
| 213 | func (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. |
| 221 | func (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. |
| 227 | func (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. |
| 233 | func (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. |
| 239 | func (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. |
| 245 | func (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. |
| 252 | func (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 |
| 259 | func (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. |
| 265 | func 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 | } |