You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

indexoptions.go 9.7 kB

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