@@ -5,7 +5,7 @@ use std::{convert::TryFrom, time::Duration};
55use serde:: { de:: Error , ser, Deserialize , Deserializer , Serialize , Serializer } ;
66
77use crate :: {
8- bson:: { doc, oid :: ObjectId , Binary , Bson , Document , JavaScriptCodeWithScope , Regex } ,
8+ bson:: { doc, Binary , Bson , Document , JavaScriptCodeWithScope , Regex } ,
99 error:: { ErrorKind , Result } ,
1010} ;
1111
@@ -31,11 +31,6 @@ pub(crate) fn get_u64(val: &Bson) -> Option<u64> {
3131 }
3232}
3333
34- pub ( crate ) fn add_id ( doc : & mut Document ) {
35- doc. entry ( "_id" . to_string ( ) )
36- . or_insert_with ( || Bson :: ObjectId ( ObjectId :: new ( ) ) ) ;
37- }
38-
3934pub ( crate ) fn to_bson_array ( docs : & [ Document ] ) -> Bson {
4035 Bson :: Array ( docs. iter ( ) . map ( |doc| Bson :: Document ( doc. clone ( ) ) ) . collect ( ) )
4136}
@@ -171,7 +166,7 @@ where
171166 . ok_or_else ( || D :: Error :: custom ( format ! ( "could not deserialize u64 from {:?}" , bson) ) )
172167}
173168
174- pub fn doc_size_bytes ( doc : & Document ) -> usize {
169+ pub fn doc_size_bytes ( doc : & Document ) -> u64 {
175170 //
176171 // * i32 length prefix (4 bytes)
177172 // * for each element:
@@ -182,19 +177,19 @@ pub fn doc_size_bytes(doc: &Document) -> usize {
182177 // * null terminator (1 byte)
183178 4 + doc
184179 . into_iter ( )
185- . map ( |( key, val) | 1 + key. len ( ) + 1 + size_bytes ( val) )
186- . sum :: < usize > ( )
180+ . map ( |( key, val) | 1 + key. len ( ) as u64 + 1 + size_bytes ( val) )
181+ . sum :: < u64 > ( )
187182 + 1
188183}
189184
190- pub fn size_bytes ( val : & Bson ) -> usize {
185+ pub fn size_bytes ( val : & Bson ) -> u64 {
191186 match val {
192187 Bson :: Double ( _) => 8 ,
193188 //
194189 // * length prefix (4 bytes)
195190 // * number of UTF-8 bytes
196191 // * null terminator (1 byte)
197- Bson :: String ( s) => 4 + s. len ( ) + 1 ,
192+ Bson :: String ( s) => 4 + s. len ( ) as u64 + 1 ,
198193 // An array is serialized as a document with the keys "0", "1", "2", etc., so the size of
199194 // an array is:
200195 //
@@ -210,7 +205,7 @@ pub fn size_bytes(val: &Bson) -> usize {
210205 . iter ( )
211206 . enumerate ( )
212207 . map ( |( i, val) | 1 + num_decimal_digits ( i) + 1 + size_bytes ( val) )
213- . sum :: < usize > ( )
208+ . sum :: < u64 > ( )
214209 + 1
215210 }
216211 Bson :: Document ( doc) => doc_size_bytes ( doc) ,
@@ -220,21 +215,21 @@ pub fn size_bytes(val: &Bson) -> usize {
220215 // * number of UTF-8 bytes
221216 // * null terminator (1 byte)
222217 Bson :: RegularExpression ( Regex { pattern, options } ) => {
223- pattern. len ( ) + 1 + options. len ( ) + 1
218+ pattern. len ( ) as u64 + 1 + options. len ( ) as u64 + 1
224219 }
225220 //
226221 // * length prefix (4 bytes)
227222 // * number of UTF-8 bytes
228223 // * null terminator (1 byte)
229- Bson :: JavaScriptCode ( code) => 4 + code. len ( ) + 1 ,
224+ Bson :: JavaScriptCode ( code) => 4 + code. len ( ) as u64 + 1 ,
230225 //
231226 // * i32 length prefix (4 bytes)
232227 // * i32 length prefix for code (4 bytes)
233228 // * number of UTF-8 bytes in code
234229 // * null terminator for code (1 byte)
235230 // * length of document
236231 Bson :: JavaScriptCodeWithScope ( JavaScriptCodeWithScope { code, scope } ) => {
237- 4 + 4 + code. len ( ) + 1 + doc_size_bytes ( scope)
232+ 4 + 4 + code. len ( ) as u64 + 1 + doc_size_bytes ( scope)
238233 }
239234 Bson :: Int32 ( _) => 4 ,
240235 Bson :: Int64 ( _) => 8 ,
@@ -243,14 +238,14 @@ pub fn size_bytes(val: &Bson) -> usize {
243238 // * i32 length prefix (4 bytes)
244239 // * subtype (1 byte)
245240 // * number of bytes
246- Bson :: Binary ( Binary { bytes, .. } ) => 4 + 1 + bytes. len ( ) ,
241+ Bson :: Binary ( Binary { bytes, .. } ) => 4 + 1 + bytes. len ( ) as u64 ,
247242 Bson :: ObjectId ( _) => 12 ,
248243 Bson :: DateTime ( _) => 8 ,
249244 //
250245 // * i32 length prefix (4 bytes)
251246 // * subtype (1 byte)
252247 // * number of UTF-8 bytes
253- Bson :: Symbol ( s) => 4 + 1 + s. len ( ) ,
248+ Bson :: Symbol ( s) => 4 + 1 + s. len ( ) as u64 ,
254249 Bson :: Decimal128 ( ..) => 128 / 8 ,
255250 Bson :: Undefined | Bson :: MaxKey | Bson :: MinKey => 0 ,
256251 // DbPointer doesn't have public details exposed by the BSON library, but it comprises of a
@@ -267,7 +262,19 @@ pub fn size_bytes(val: &Bson) -> usize {
267262 }
268263}
269264
270- fn num_decimal_digits ( n : usize ) -> usize {
265+ /// The size in bytes of the provided document's entry in a BSON array at the given index.
266+ pub ( crate ) fn array_entry_size_bytes ( index : usize , doc : & Document ) -> u64 {
267+ //
268+ // * type (1 byte)
269+ // * number of decimal digits in key
270+ // * null terminator for the key (1 byte)
271+ // * size of value
272+ 1 + num_decimal_digits ( index) + 1 + doc_size_bytes ( & doc)
273+ }
274+
275+ /// The number of digits in `n` in base 10.
276+ /// Useful for calculating the size of an array entry in BSON.
277+ fn num_decimal_digits ( n : usize ) -> u64 {
271278 let mut digits = 1 ;
272279 let mut curr = 10 ;
273280
@@ -332,6 +339,6 @@ mod test {
332339 let mut serialized_bytes = Vec :: new ( ) ;
333340 doc. to_writer ( & mut serialized_bytes) . unwrap ( ) ;
334341
335- assert_eq ! ( size_bytes, serialized_bytes. len( ) ) ;
342+ assert_eq ! ( size_bytes, serialized_bytes. len( ) as u64 ) ;
336343 }
337344}
0 commit comments