@@ -5,7 +5,7 @@ use std::{convert::TryFrom, time::Duration};
5
5
use serde:: { de:: Error , ser, Deserialize , Deserializer , Serialize , Serializer } ;
6
6
7
7
use crate :: {
8
- bson:: { doc, oid :: ObjectId , Binary , Bson , Document , JavaScriptCodeWithScope , Regex } ,
8
+ bson:: { doc, Binary , Bson , Document , JavaScriptCodeWithScope , Regex } ,
9
9
error:: { ErrorKind , Result } ,
10
10
} ;
11
11
@@ -31,11 +31,6 @@ pub(crate) fn get_u64(val: &Bson) -> Option<u64> {
31
31
}
32
32
}
33
33
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
-
39
34
pub ( crate ) fn to_bson_array ( docs : & [ Document ] ) -> Bson {
40
35
Bson :: Array ( docs. iter ( ) . map ( |doc| Bson :: Document ( doc. clone ( ) ) ) . collect ( ) )
41
36
}
@@ -171,7 +166,7 @@ where
171
166
. ok_or_else ( || D :: Error :: custom ( format ! ( "could not deserialize u64 from {:?}" , bson) ) )
172
167
}
173
168
174
- pub fn doc_size_bytes ( doc : & Document ) -> usize {
169
+ pub fn doc_size_bytes ( doc : & Document ) -> u64 {
175
170
//
176
171
// * i32 length prefix (4 bytes)
177
172
// * for each element:
@@ -182,19 +177,19 @@ pub fn doc_size_bytes(doc: &Document) -> usize {
182
177
// * null terminator (1 byte)
183
178
4 + doc
184
179
. 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 > ( )
187
182
+ 1
188
183
}
189
184
190
- pub fn size_bytes ( val : & Bson ) -> usize {
185
+ pub fn size_bytes ( val : & Bson ) -> u64 {
191
186
match val {
192
187
Bson :: Double ( _) => 8 ,
193
188
//
194
189
// * length prefix (4 bytes)
195
190
// * number of UTF-8 bytes
196
191
// * null terminator (1 byte)
197
- Bson :: String ( s) => 4 + s. len ( ) + 1 ,
192
+ Bson :: String ( s) => 4 + s. len ( ) as u64 + 1 ,
198
193
// An array is serialized as a document with the keys "0", "1", "2", etc., so the size of
199
194
// an array is:
200
195
//
@@ -210,7 +205,7 @@ pub fn size_bytes(val: &Bson) -> usize {
210
205
. iter ( )
211
206
. enumerate ( )
212
207
. map ( |( i, val) | 1 + num_decimal_digits ( i) + 1 + size_bytes ( val) )
213
- . sum :: < usize > ( )
208
+ . sum :: < u64 > ( )
214
209
+ 1
215
210
}
216
211
Bson :: Document ( doc) => doc_size_bytes ( doc) ,
@@ -220,21 +215,21 @@ pub fn size_bytes(val: &Bson) -> usize {
220
215
// * number of UTF-8 bytes
221
216
// * null terminator (1 byte)
222
217
Bson :: RegularExpression ( Regex { pattern, options } ) => {
223
- pattern. len ( ) + 1 + options. len ( ) + 1
218
+ pattern. len ( ) as u64 + 1 + options. len ( ) as u64 + 1
224
219
}
225
220
//
226
221
// * length prefix (4 bytes)
227
222
// * number of UTF-8 bytes
228
223
// * null terminator (1 byte)
229
- Bson :: JavaScriptCode ( code) => 4 + code. len ( ) + 1 ,
224
+ Bson :: JavaScriptCode ( code) => 4 + code. len ( ) as u64 + 1 ,
230
225
//
231
226
// * i32 length prefix (4 bytes)
232
227
// * i32 length prefix for code (4 bytes)
233
228
// * number of UTF-8 bytes in code
234
229
// * null terminator for code (1 byte)
235
230
// * length of document
236
231
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)
238
233
}
239
234
Bson :: Int32 ( _) => 4 ,
240
235
Bson :: Int64 ( _) => 8 ,
@@ -243,14 +238,14 @@ pub fn size_bytes(val: &Bson) -> usize {
243
238
// * i32 length prefix (4 bytes)
244
239
// * subtype (1 byte)
245
240
// * number of bytes
246
- Bson :: Binary ( Binary { bytes, .. } ) => 4 + 1 + bytes. len ( ) ,
241
+ Bson :: Binary ( Binary { bytes, .. } ) => 4 + 1 + bytes. len ( ) as u64 ,
247
242
Bson :: ObjectId ( _) => 12 ,
248
243
Bson :: DateTime ( _) => 8 ,
249
244
//
250
245
// * i32 length prefix (4 bytes)
251
246
// * subtype (1 byte)
252
247
// * number of UTF-8 bytes
253
- Bson :: Symbol ( s) => 4 + 1 + s. len ( ) ,
248
+ Bson :: Symbol ( s) => 4 + 1 + s. len ( ) as u64 ,
254
249
Bson :: Decimal128 ( ..) => 128 / 8 ,
255
250
Bson :: Undefined | Bson :: MaxKey | Bson :: MinKey => 0 ,
256
251
// 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 {
267
262
}
268
263
}
269
264
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 {
271
278
let mut digits = 1 ;
272
279
let mut curr = 10 ;
273
280
@@ -332,6 +339,6 @@ mod test {
332
339
let mut serialized_bytes = Vec :: new ( ) ;
333
340
doc. to_writer ( & mut serialized_bytes) . unwrap ( ) ;
334
341
335
- assert_eq ! ( size_bytes, serialized_bytes. len( ) ) ;
342
+ assert_eq ! ( size_bytes, serialized_bytes. len( ) as u64 ) ;
336
343
}
337
344
}
0 commit comments