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.

gridfsoptions.go 8.2 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. "go.mongodb.org/mongo-driver/bson"
  10. "go.mongodb.org/mongo-driver/bson/bsoncodec"
  11. "go.mongodb.org/mongo-driver/mongo/readconcern"
  12. "go.mongodb.org/mongo-driver/mongo/readpref"
  13. "go.mongodb.org/mongo-driver/mongo/writeconcern"
  14. )
  15. // DefaultName is the default name for a GridFS bucket.
  16. var DefaultName = "fs"
  17. // DefaultChunkSize is the default size of each file chunk in bytes.
  18. var DefaultChunkSize int32 = 255 * 1024 // 255 KiB
  19. // DefaultRevision is the default revision number for a download by name operation.
  20. var DefaultRevision int32 = -1
  21. // BucketOptions represents all possible options to configure a GridFS bucket.
  22. type BucketOptions struct {
  23. Name *string // The bucket name. Defaults to "fs".
  24. ChunkSizeBytes *int32 // The chunk size in bytes. Defaults to 255KB.
  25. WriteConcern *writeconcern.WriteConcern // The write concern for the bucket. Defaults to the write concern of the database.
  26. ReadConcern *readconcern.ReadConcern // The read concern for the bucket. Defaults to the read concern of the database.
  27. ReadPreference *readpref.ReadPref // The read preference for the bucket. Defaults to the read preference of the database.
  28. }
  29. // GridFSBucket creates a new *BucketOptions
  30. func GridFSBucket() *BucketOptions {
  31. return &BucketOptions{
  32. Name: &DefaultName,
  33. ChunkSizeBytes: &DefaultChunkSize,
  34. }
  35. }
  36. // SetName sets the name for the bucket. Defaults to "fs" if not set.
  37. func (b *BucketOptions) SetName(name string) *BucketOptions {
  38. b.Name = &name
  39. return b
  40. }
  41. // SetChunkSizeBytes sets the chunk size in bytes for the bucket. Defaults to 255KB if not set.
  42. func (b *BucketOptions) SetChunkSizeBytes(i int32) *BucketOptions {
  43. b.ChunkSizeBytes = &i
  44. return b
  45. }
  46. // SetWriteConcern sets the write concern for the bucket.
  47. func (b *BucketOptions) SetWriteConcern(wc *writeconcern.WriteConcern) *BucketOptions {
  48. b.WriteConcern = wc
  49. return b
  50. }
  51. // SetReadConcern sets the read concern for the bucket.
  52. func (b *BucketOptions) SetReadConcern(rc *readconcern.ReadConcern) *BucketOptions {
  53. b.ReadConcern = rc
  54. return b
  55. }
  56. // SetReadPreference sets the read preference for the bucket.
  57. func (b *BucketOptions) SetReadPreference(rp *readpref.ReadPref) *BucketOptions {
  58. b.ReadPreference = rp
  59. return b
  60. }
  61. // MergeBucketOptions combines the given *BucketOptions into a single *BucketOptions.
  62. // If the name or chunk size is not set in any of the given *BucketOptions, the resulting *BucketOptions will have
  63. // name "fs" and chunk size 255KB.
  64. func MergeBucketOptions(opts ...*BucketOptions) *BucketOptions {
  65. b := GridFSBucket()
  66. for _, opt := range opts {
  67. if opt == nil {
  68. continue
  69. }
  70. if opt.Name != nil {
  71. b.Name = opt.Name
  72. }
  73. if opt.ChunkSizeBytes != nil {
  74. b.ChunkSizeBytes = opt.ChunkSizeBytes
  75. }
  76. if opt.WriteConcern != nil {
  77. b.WriteConcern = opt.WriteConcern
  78. }
  79. if opt.ReadConcern != nil {
  80. b.ReadConcern = opt.ReadConcern
  81. }
  82. if opt.ReadPreference != nil {
  83. b.ReadPreference = opt.ReadPreference
  84. }
  85. }
  86. return b
  87. }
  88. // UploadOptions represents all possible options for a GridFS upload operation. If a registry is nil, bson.DefaultRegistry
  89. // will be used when converting the Metadata interface to BSON.
  90. type UploadOptions struct {
  91. ChunkSizeBytes *int32 // Chunk size in bytes. Defaults to the chunk size of the bucket.
  92. Metadata interface{} // User data for the 'metadata' field of the files collection document.
  93. Registry *bsoncodec.Registry // The registry to use for converting filters. Defaults to bson.DefaultRegistry.
  94. }
  95. // GridFSUpload creates a new *UploadOptions
  96. func GridFSUpload() *UploadOptions {
  97. return &UploadOptions{Registry: bson.DefaultRegistry}
  98. }
  99. // SetChunkSizeBytes sets the chunk size in bytes for the upload. Defaults to 255KB if not set.
  100. func (u *UploadOptions) SetChunkSizeBytes(i int32) *UploadOptions {
  101. u.ChunkSizeBytes = &i
  102. return u
  103. }
  104. // SetMetadata specfies the metadata for the upload.
  105. func (u *UploadOptions) SetMetadata(doc interface{}) *UploadOptions {
  106. u.Metadata = doc
  107. return u
  108. }
  109. // MergeUploadOptions combines the given *UploadOptions into a single *UploadOptions.
  110. // If the chunk size is not set in any of the given *UploadOptions, the resulting *UploadOptions will have chunk size
  111. // 255KB.
  112. func MergeUploadOptions(opts ...*UploadOptions) *UploadOptions {
  113. u := GridFSUpload()
  114. for _, opt := range opts {
  115. if opt == nil {
  116. continue
  117. }
  118. if opt.ChunkSizeBytes != nil {
  119. u.ChunkSizeBytes = opt.ChunkSizeBytes
  120. }
  121. if opt.Metadata != nil {
  122. u.Metadata = opt.Metadata
  123. }
  124. if opt.Registry != nil {
  125. u.Registry = opt.Registry
  126. }
  127. }
  128. return u
  129. }
  130. // NameOptions represents all options that can be used for a GridFS download by name operation.
  131. type NameOptions struct {
  132. Revision *int32 // Which revision (documents with the same filename and different uploadDate). Defaults to -1 (the most recent revision).
  133. }
  134. // GridFSName creates a new *NameOptions
  135. func GridFSName() *NameOptions {
  136. return &NameOptions{}
  137. }
  138. // SetRevision specifies which revision of the file to retrieve. Defaults to -1.
  139. // * Revision numbers are defined as follows:
  140. // * 0 = the original stored file
  141. // * 1 = the first revision
  142. // * 2 = the second revision
  143. // * etc…
  144. // * -2 = the second most recent revision
  145. // * -1 = the most recent revision
  146. func (n *NameOptions) SetRevision(r int32) *NameOptions {
  147. n.Revision = &r
  148. return n
  149. }
  150. // MergeNameOptions combines the given *NameOptions into a single *NameOptions in a last one wins fashion.
  151. func MergeNameOptions(opts ...*NameOptions) *NameOptions {
  152. n := GridFSName()
  153. n.Revision = &DefaultRevision
  154. for _, opt := range opts {
  155. if opt == nil {
  156. continue
  157. }
  158. if opt.Revision != nil {
  159. n.Revision = opt.Revision
  160. }
  161. }
  162. return n
  163. }
  164. // GridFSFindOptions represents all options for a GridFS find operation.
  165. type GridFSFindOptions struct {
  166. BatchSize *int32
  167. Limit *int32
  168. MaxTime *time.Duration
  169. NoCursorTimeout *bool
  170. Skip *int32
  171. Sort interface{}
  172. }
  173. // GridFSFind creates a new GridFSFindOptions instance.
  174. func GridFSFind() *GridFSFindOptions {
  175. return &GridFSFindOptions{}
  176. }
  177. // SetBatchSize sets the number of documents to return in each batch.
  178. func (f *GridFSFindOptions) SetBatchSize(i int32) *GridFSFindOptions {
  179. f.BatchSize = &i
  180. return f
  181. }
  182. // SetLimit specifies a limit on the number of results.
  183. // A negative limit implies that only 1 batch should be returned.
  184. func (f *GridFSFindOptions) SetLimit(i int32) *GridFSFindOptions {
  185. f.Limit = &i
  186. return f
  187. }
  188. // SetMaxTime specifies the max time to allow the query to run.
  189. func (f *GridFSFindOptions) SetMaxTime(d time.Duration) *GridFSFindOptions {
  190. f.MaxTime = &d
  191. return f
  192. }
  193. // SetNoCursorTimeout specifies whether or not cursors should time out after a period of inactivity.
  194. func (f *GridFSFindOptions) SetNoCursorTimeout(b bool) *GridFSFindOptions {
  195. f.NoCursorTimeout = &b
  196. return f
  197. }
  198. // SetSkip specifies the number of documents to skip before returning.
  199. func (f *GridFSFindOptions) SetSkip(i int32) *GridFSFindOptions {
  200. f.Skip = &i
  201. return f
  202. }
  203. // SetSort specifies the order in which to return documents.
  204. func (f *GridFSFindOptions) SetSort(sort interface{}) *GridFSFindOptions {
  205. f.Sort = sort
  206. return f
  207. }
  208. // MergeGridFSFindOptions combines the argued GridFSFindOptions into a single GridFSFindOptions in a last-one-wins fashion
  209. func MergeGridFSFindOptions(opts ...*GridFSFindOptions) *GridFSFindOptions {
  210. fo := GridFSFind()
  211. for _, opt := range opts {
  212. if opt == nil {
  213. continue
  214. }
  215. if opt.BatchSize != nil {
  216. fo.BatchSize = opt.BatchSize
  217. }
  218. if opt.Limit != nil {
  219. fo.Limit = opt.Limit
  220. }
  221. if opt.MaxTime != nil {
  222. fo.MaxTime = opt.MaxTime
  223. }
  224. if opt.NoCursorTimeout != nil {
  225. fo.NoCursorTimeout = opt.NoCursorTimeout
  226. }
  227. if opt.Skip != nil {
  228. fo.Skip = opt.Skip
  229. }
  230. if opt.Sort != nil {
  231. fo.Sort = opt.Sort
  232. }
  233. }
  234. return fo
  235. }