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