1
- // TODO(RUST-1395) Remove these allows.
2
- #![ allow( dead_code, unused_variables, missing_docs) ]
1
+ //! Contains the functionality for GridFS operations.
3
2
4
3
mod download;
5
- pub mod options;
4
+ pub ( crate ) mod options;
6
5
mod upload;
7
6
8
7
use std:: sync:: { atomic:: AtomicBool , Arc } ;
@@ -20,13 +19,13 @@ use crate::{
20
19
} ;
21
20
22
21
pub use download:: GridFsDownloadStream ;
23
- pub use options:: * ;
22
+ pub ( crate ) use options:: * ;
24
23
pub use upload:: GridFsUploadStream ;
25
24
26
- pub const DEFAULT_BUCKET_NAME : & str = "fs" ;
27
- pub const DEFAULT_CHUNK_SIZE_BYTES : u32 = 255 * 1024 ;
25
+ const DEFAULT_BUCKET_NAME : & str = "fs" ;
26
+ const DEFAULT_CHUNK_SIZE_BYTES : u32 = 255 * 1024 ;
28
27
29
- // Contained in a "chunks" collection for each user file
28
+ /// A model for the documents stored in the chunks collection.
30
29
#[ derive( Debug , Deserialize , Serialize ) ]
31
30
pub ( crate ) struct Chunk < ' a > {
32
31
#[ serde( rename = "_id" ) ]
@@ -37,45 +36,57 @@ pub(crate) struct Chunk<'a> {
37
36
data : RawBinaryRef < ' a > ,
38
37
}
39
38
40
- /// A collection in which information about stored files is stored. There will be one files
41
- /// collection document per stored file .
39
+ /// A model for the documents stored in a GridFS bucket's files
40
+ /// collection.
42
41
#[ derive( Clone , Debug , Deserialize , Serialize ) ]
43
42
#[ serde( rename_all = "camelCase" ) ]
44
43
#[ skip_serializing_none]
45
44
#[ non_exhaustive]
46
45
pub struct FilesCollectionDocument {
46
+ /// The file's unique identifier.
47
47
#[ serde( rename = "_id" ) ]
48
48
pub id : Bson ,
49
+
50
+ /// The length of the file in bytes.
49
51
pub length : u64 ,
50
- pub chunk_size : u32 ,
52
+
53
+ /// The size of the file's chunks in bytes.
54
+ #[ serde( rename = "chunkSize" ) ]
55
+ pub chunk_size_bytes : u32 ,
56
+
57
+ /// The time at which the file was uploaded.
51
58
pub upload_date : DateTime ,
59
+
60
+ /// The name of the file.
52
61
pub filename : Option < String > ,
62
+
63
+ /// User-provided metadata associated with the file.
53
64
pub metadata : Option < Document > ,
54
65
}
55
66
56
67
impl FilesCollectionDocument {
57
68
/// Returns the total number of chunks expected to be in the file.
58
69
fn n ( & self ) -> u32 {
59
- Self :: n_from_vals ( self . length , self . chunk_size )
70
+ Self :: n_from_vals ( self . length , self . chunk_size_bytes )
60
71
}
61
72
62
- fn n_from_vals ( length : u64 , chunk_size : u32 ) -> u32 {
63
- let chunk_size = chunk_size as u64 ;
64
- let n = length / chunk_size + u64:: from ( length % chunk_size != 0 ) ;
73
+ fn n_from_vals ( length : u64 , chunk_size_bytes : u32 ) -> u32 {
74
+ let chunk_size_bytes = chunk_size_bytes as u64 ;
75
+ let n = length / chunk_size_bytes + u64:: from ( length % chunk_size_bytes != 0 ) ;
65
76
n as u32
66
77
}
67
78
68
79
/// Returns the expected length of a chunk given its index.
69
80
fn expected_chunk_length ( & self , n : u32 ) -> u32 {
70
- Self :: expected_chunk_length_from_vals ( self . length , self . chunk_size , n)
81
+ Self :: expected_chunk_length_from_vals ( self . length , self . chunk_size_bytes , n)
71
82
}
72
83
73
- fn expected_chunk_length_from_vals ( length : u64 , chunk_size : u32 , n : u32 ) -> u32 {
74
- let remainder = length % ( chunk_size as u64 ) ;
75
- if n == Self :: n_from_vals ( length, chunk_size ) - 1 && remainder != 0 {
84
+ fn expected_chunk_length_from_vals ( length : u64 , chunk_size_bytes : u32 , n : u32 ) -> u32 {
85
+ let remainder = length % ( chunk_size_bytes as u64 ) ;
86
+ if n == Self :: n_from_vals ( length, chunk_size_bytes ) - 1 && remainder != 0 {
76
87
remainder as u32
77
88
} else {
78
- chunk_size
89
+ chunk_size_bytes
79
90
}
80
91
}
81
92
}
@@ -89,7 +100,15 @@ struct GridFsBucketInner {
89
100
created_indexes : AtomicBool ,
90
101
}
91
102
92
- /// Struct for storing GridFS managed files within a [`Database`].
103
+ /// A `GridFsBucket` provides the functionality for storing and retrieving binary BSON data that
104
+ /// exceeds the 16 MiB size limit of a MongoDB document. Users may upload and download large amounts
105
+ /// of data, called files, to the bucket. When a file is uploaded, its contents are divided into
106
+ /// chunks and stored in a chunks collection. A corresponding [`FilesCollectionDocument`] is also
107
+ /// stored in a files collection. When a user downloads a file, the bucket finds and returns the
108
+ /// data stored in its chunks.
109
+ ///
110
+ /// `GridFsBucket` uses [`std::sync::Arc`] internally, so it can be shared safely across threads or
111
+ /// async tasks.
93
112
#[ derive( Debug , Clone ) ]
94
113
pub struct GridFsBucket {
95
114
inner : Arc < GridFsBucketInner > ,
@@ -142,41 +161,42 @@ impl GridFsBucket {
142
161
self . inner . files . client ( )
143
162
}
144
163
145
- /// Gets the read concern of the [`GridFsBucket`] .
164
+ /// Gets the read concern of the bucket .
146
165
pub fn read_concern ( & self ) -> Option < & ReadConcern > {
147
166
self . inner . options . read_concern . as_ref ( )
148
167
}
149
168
150
- /// Gets the write concern of the [`GridFsBucket`] .
169
+ /// Gets the write concern of the bucket .
151
170
pub fn write_concern ( & self ) -> Option < & WriteConcern > {
152
171
self . inner . options . write_concern . as_ref ( )
153
172
}
154
173
155
- /// Gets the selection criteria of the [`GridFsBucket`] .
174
+ /// Gets the selection criteria of the bucket .
156
175
pub fn selection_criteria ( & self ) -> Option < & SelectionCriteria > {
157
176
self . inner . options . selection_criteria . as_ref ( )
158
177
}
159
178
160
- /// Gets the chunk size in bytes for the [`GridFsBucket`] .
179
+ /// Gets the chunk size in bytes for the bucket .
161
180
fn chunk_size_bytes ( & self ) -> u32 {
162
181
self . inner
163
182
. options
164
183
. chunk_size_bytes
165
184
. unwrap_or ( DEFAULT_CHUNK_SIZE_BYTES )
166
185
}
167
186
168
- /// Gets a handle to the files collection for the [`GridFsBucket`] .
187
+ /// Gets a handle to the files collection for the bucket .
169
188
pub ( crate ) fn files ( & self ) -> & Collection < FilesCollectionDocument > {
170
189
& self . inner . files
171
190
}
172
191
173
- /// Gets a handle to the chunks collection for the [`GridFsBucket`] .
192
+ /// Gets a handle to the chunks collection for the bucket .
174
193
pub ( crate ) fn chunks ( & self ) -> & Collection < Chunk < ' static > > {
175
194
& self . inner . chunks
176
195
}
177
196
178
- /// Deletes the [`FilesCollectionDocument`] with the given `id `and its associated chunks from
179
- /// this bucket.
197
+ /// Deletes the [`FilesCollectionDocument`] with the given `id` and its associated chunks from
198
+ /// this bucket. This method returns an error if the `id` does not match any files in the
199
+ /// bucket.
180
200
pub async fn delete ( & self , id : Bson ) -> Result < ( ) > {
181
201
let delete_result = self
182
202
. files ( )
@@ -209,7 +229,8 @@ impl GridFsBucket {
209
229
self . files ( ) . find ( filter, find_options) . await
210
230
}
211
231
212
- /// Renames the file with the given 'id' to the provided `new_filename`.
232
+ /// Renames the file with the given 'id' to the provided `new_filename`. This method returns an
233
+ /// error if the `id` does not match any files in the bucket.
213
234
pub async fn rename ( & self , id : Bson , new_filename : impl AsRef < str > ) -> Result < ( ) > {
214
235
self . files ( )
215
236
. update_one (
@@ -222,7 +243,7 @@ impl GridFsBucket {
222
243
Ok ( ( ) )
223
244
}
224
245
225
- /// Drops all of the files and their associated chunks in this bucket.
246
+ /// Removes all of the files and their associated chunks from this bucket.
226
247
pub async fn drop ( & self ) -> Result < ( ) > {
227
248
self . files ( ) . drop ( None ) . await ?;
228
249
self . chunks ( ) . drop ( None ) . await ?;
0 commit comments