1- // TODO(RUST-1395) Remove these allows.
2- #![ allow( dead_code, unused_variables, missing_docs) ]
1+ //! Contains the functionality for GridFS operations.
32
43mod download;
5- pub mod options;
4+ pub ( crate ) mod options;
65mod upload;
76
87use std:: sync:: { atomic:: AtomicBool , Arc } ;
@@ -20,13 +19,13 @@ use crate::{
2019} ;
2120
2221pub use download:: GridFsDownloadStream ;
23- pub use options:: * ;
22+ pub ( crate ) use options:: * ;
2423pub use upload:: GridFsUploadStream ;
2524
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 ;
2827
29- // Contained in a "chunks" collection for each user file
28+ /// A model for the documents stored in the chunks collection.
3029#[ derive( Debug , Deserialize , Serialize ) ]
3130pub ( crate ) struct Chunk < ' a > {
3231 #[ serde( rename = "_id" ) ]
@@ -37,45 +36,57 @@ pub(crate) struct Chunk<'a> {
3736 data : RawBinaryRef < ' a > ,
3837}
3938
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.
4241#[ derive( Clone , Debug , Deserialize , Serialize ) ]
4342#[ serde( rename_all = "camelCase" ) ]
4443#[ skip_serializing_none]
4544#[ non_exhaustive]
4645pub struct FilesCollectionDocument {
46+ /// The file's unique identifier.
4747 #[ serde( rename = "_id" ) ]
4848 pub id : Bson ,
49+
50+ /// The length of the file in bytes.
4951 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.
5158 pub upload_date : DateTime ,
59+
60+ /// The name of the file.
5261 pub filename : Option < String > ,
62+
63+ /// User-provided metadata associated with the file.
5364 pub metadata : Option < Document > ,
5465}
5566
5667impl FilesCollectionDocument {
5768 /// Returns the total number of chunks expected to be in the file.
5869 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 )
6071 }
6172
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 ) ;
6576 n as u32
6677 }
6778
6879 /// Returns the expected length of a chunk given its index.
6980 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)
7182 }
7283
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 {
7687 remainder as u32
7788 } else {
78- chunk_size
89+ chunk_size_bytes
7990 }
8091 }
8192}
@@ -89,7 +100,15 @@ struct GridFsBucketInner {
89100 created_indexes : AtomicBool ,
90101}
91102
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.
93112#[ derive( Debug , Clone ) ]
94113pub struct GridFsBucket {
95114 inner : Arc < GridFsBucketInner > ,
@@ -142,41 +161,42 @@ impl GridFsBucket {
142161 self . inner . files . client ( )
143162 }
144163
145- /// Gets the read concern of the [`GridFsBucket`] .
164+ /// Gets the read concern of the bucket .
146165 pub fn read_concern ( & self ) -> Option < & ReadConcern > {
147166 self . inner . options . read_concern . as_ref ( )
148167 }
149168
150- /// Gets the write concern of the [`GridFsBucket`] .
169+ /// Gets the write concern of the bucket .
151170 pub fn write_concern ( & self ) -> Option < & WriteConcern > {
152171 self . inner . options . write_concern . as_ref ( )
153172 }
154173
155- /// Gets the selection criteria of the [`GridFsBucket`] .
174+ /// Gets the selection criteria of the bucket .
156175 pub fn selection_criteria ( & self ) -> Option < & SelectionCriteria > {
157176 self . inner . options . selection_criteria . as_ref ( )
158177 }
159178
160- /// Gets the chunk size in bytes for the [`GridFsBucket`] .
179+ /// Gets the chunk size in bytes for the bucket .
161180 fn chunk_size_bytes ( & self ) -> u32 {
162181 self . inner
163182 . options
164183 . chunk_size_bytes
165184 . unwrap_or ( DEFAULT_CHUNK_SIZE_BYTES )
166185 }
167186
168- /// Gets a handle to the files collection for the [`GridFsBucket`] .
187+ /// Gets a handle to the files collection for the bucket .
169188 pub ( crate ) fn files ( & self ) -> & Collection < FilesCollectionDocument > {
170189 & self . inner . files
171190 }
172191
173- /// Gets a handle to the chunks collection for the [`GridFsBucket`] .
192+ /// Gets a handle to the chunks collection for the bucket .
174193 pub ( crate ) fn chunks ( & self ) -> & Collection < Chunk < ' static > > {
175194 & self . inner . chunks
176195 }
177196
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.
180200 pub async fn delete ( & self , id : Bson ) -> Result < ( ) > {
181201 let delete_result = self
182202 . files ( )
@@ -209,7 +229,8 @@ impl GridFsBucket {
209229 self . files ( ) . find ( filter, find_options) . await
210230 }
211231
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.
213234 pub async fn rename ( & self , id : Bson , new_filename : impl AsRef < str > ) -> Result < ( ) > {
214235 self . files ( )
215236 . update_one (
@@ -222,7 +243,7 @@ impl GridFsBucket {
222243 Ok ( ( ) )
223244 }
224245
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.
226247 pub async fn drop ( & self ) -> Result < ( ) > {
227248 self . files ( ) . drop ( None ) . await ?;
228249 self . chunks ( ) . drop ( None ) . await ?;
0 commit comments