2
2
use bson:: { self , Bson , oid} ;
3
3
use bson:: spec:: BinarySubtype ;
4
4
5
- use chrono:: { DateTime , UTC } ;
5
+ use chrono:: { DateTime , Utc } ;
6
6
use crypto:: digest:: Digest ;
7
7
use crypto:: md5:: Md5 ;
8
8
@@ -80,7 +80,7 @@ pub struct GfsFile {
80
80
// The filename of the document.
81
81
pub name : Option < String > ,
82
82
// The date the document was first stored in GridFS.
83
- pub upload_date : Option < DateTime < UTC > > ,
83
+ pub upload_date : Option < DateTime < Utc > > ,
84
84
// The content type of the file.
85
85
pub content_type : Option < String > ,
86
86
// Any additional metadata provided by the user.
@@ -195,7 +195,7 @@ impl File {
195
195
if self . mode == Mode :: Write {
196
196
if try!( self . err_description ( ) ) . is_none ( ) {
197
197
if self . doc . upload_date . is_none ( ) {
198
- self . doc . upload_date = Some ( UTC :: now ( ) ) ;
198
+ self . doc . upload_date = Some ( Utc :: now ( ) ) ;
199
199
}
200
200
self . doc . md5 = self . wsum . result_str ( ) ;
201
201
try!( self . gfs . files . insert_one ( self . doc . to_bson ( ) , None ) ) ;
@@ -205,11 +205,15 @@ impl File {
205
205
206
206
let mut opts = IndexOptions :: new ( ) ;
207
207
opts. unique = Some ( true ) ;
208
- try!( self . gfs . chunks . create_index ( doc ! { "files_id" => 1 , "n" => 1 } , Some ( opts) ) ) ;
208
+ try!( self . gfs . chunks . create_index (
209
+ doc ! { "files_id" => 1 , "n" => 1 } ,
210
+ Some ( opts) ,
211
+ ) ) ;
209
212
} else {
210
- try!( self . gfs
211
- . chunks
212
- . delete_many ( doc ! { "files_id" => ( self . doc. id. clone( ) ) } , None ) ) ;
213
+ try!( self . gfs . chunks . delete_many (
214
+ doc ! { "files_id" => ( self . doc. id. clone( ) ) } ,
215
+ None ,
216
+ ) ) ;
213
217
}
214
218
}
215
219
@@ -240,7 +244,8 @@ impl File {
240
244
let mut vec_buf = Vec :: with_capacity ( buf. len ( ) ) ;
241
245
vec_buf. extend ( buf. iter ( ) . cloned ( ) ) ;
242
246
243
- let document = doc ! {
247
+ let document =
248
+ doc ! {
244
249
"_id" => ( try!( oid:: ObjectId :: new( ) ) ) ,
245
250
"files_id" => ( self . doc. id. clone( ) ) ,
246
251
"n" => n,
@@ -322,8 +327,10 @@ impl File {
322
327
}
323
328
} ;
324
329
325
- let result = arc_gfs. chunks
326
- . find_one ( Some ( doc ! { "files_id" => ( id) , "n" => ( next_chunk_num) } ) , None ) ;
330
+ let result = arc_gfs. chunks . find_one (
331
+ Some ( doc ! { "files_id" => ( id) , "n" => ( next_chunk_num) } ) ,
332
+ None ,
333
+ ) ;
327
334
328
335
match result {
329
336
Ok ( Some ( doc) ) => {
@@ -333,8 +340,10 @@ impl File {
333
340
cache. err = None ;
334
341
}
335
342
_ => {
336
- cache. err = Some ( OperationError ( String :: from ( "Chunk contained \
337
- no data.") ) )
343
+ cache. err = Some ( OperationError ( String :: from (
344
+ "Chunk contained \
345
+ no data.",
346
+ ) ) )
338
347
}
339
348
}
340
349
}
@@ -361,7 +370,10 @@ impl io::Write for File {
361
370
362
371
let description = try!( self . err_description ( ) ) ;
363
372
if description. is_some ( ) {
364
- return Err ( io:: Error :: new ( io:: ErrorKind :: Other , OperationError ( description. unwrap ( ) ) ) ) ;
373
+ return Err ( io:: Error :: new (
374
+ io:: ErrorKind :: Other ,
375
+ OperationError ( description. unwrap ( ) ) ,
376
+ ) ) ;
365
377
}
366
378
367
379
let mut data = buf;
@@ -393,16 +405,19 @@ impl io::Write for File {
393
405
394
406
// If over a megabyte is being written at once, wait for the load to reduce.
395
407
while self . doc . chunk_size * self . wpending . load ( Ordering :: SeqCst ) as i32 >=
396
- MEGABYTE as i32 {
408
+ MEGABYTE as i32
409
+ {
397
410
guard = match self . condvar . wait ( guard) {
398
411
Ok ( guard) => guard,
399
412
Err ( _) => return Err ( io:: Error :: new ( io:: ErrorKind :: Other , PoisonLockError ) ) ,
400
413
} ;
401
414
402
415
let description = try!( self . err_description ( ) ) ;
403
416
if description. is_some ( ) {
404
- return Err ( io:: Error :: new ( io:: ErrorKind :: Other ,
405
- OperationError ( description. unwrap ( ) ) ) ) ;
417
+ return Err ( io:: Error :: new (
418
+ io:: ErrorKind :: Other ,
419
+ OperationError ( description. unwrap ( ) ) ,
420
+ ) ) ;
406
421
}
407
422
}
408
423
@@ -423,16 +438,19 @@ impl io::Write for File {
423
438
424
439
// Pending megabyte
425
440
while self . doc . chunk_size * self . wpending . load ( Ordering :: SeqCst ) as i32 >=
426
- MEGABYTE as i32 {
441
+ MEGABYTE as i32
442
+ {
427
443
guard = match self . condvar . wait ( guard) {
428
444
Ok ( guard) => guard,
429
445
Err ( _) => return Err ( io:: Error :: new ( io:: ErrorKind :: Other , PoisonLockError ) ) ,
430
446
} ;
431
447
432
448
let description = try!( self . err_description ( ) ) ;
433
449
if description. is_some ( ) {
434
- return Err ( io:: Error :: new ( io:: ErrorKind :: Other ,
435
- OperationError ( description. unwrap ( ) ) ) ) ;
450
+ return Err ( io:: Error :: new (
451
+ io:: ErrorKind :: Other ,
452
+ OperationError ( description. unwrap ( ) ) ,
453
+ ) ) ;
436
454
}
437
455
}
438
456
@@ -461,7 +479,8 @@ impl io::Write for File {
461
479
462
480
// Pending megabyte
463
481
while self . doc . chunk_size * self . wpending . load ( Ordering :: SeqCst ) as i32 >=
464
- MEGABYTE as i32 {
482
+ MEGABYTE as i32
483
+ {
465
484
guard = match self . condvar . wait ( guard) {
466
485
Ok ( guard) => guard,
467
486
Err ( _) => return Err ( io:: Error :: new ( io:: ErrorKind :: Other , PoisonLockError ) ) ,
@@ -486,7 +505,10 @@ impl io::Write for File {
486
505
487
506
let description = try!( self . err_description ( ) ) ;
488
507
if description. is_some ( ) {
489
- return Err ( io:: Error :: new ( io:: ErrorKind :: Other , OperationError ( description. unwrap ( ) ) ) ) ;
508
+ return Err ( io:: Error :: new (
509
+ io:: ErrorKind :: Other ,
510
+ OperationError ( description. unwrap ( ) ) ,
511
+ ) ) ;
490
512
}
491
513
492
514
Ok ( ( ) )
@@ -613,7 +635,8 @@ impl GfsFile {
613
635
614
636
/// Converts a GfsFile into a bson document.
615
637
pub fn to_bson ( & self ) -> bson:: Document {
616
- let mut doc = doc ! {
638
+ let mut doc =
639
+ doc ! {
617
640
"_id" => ( self . id. clone( ) ) ,
618
641
"chunkSize" => ( self . chunk_size) ,
619
642
"length" => ( self . len) ,
@@ -622,19 +645,27 @@ impl GfsFile {
622
645
} ;
623
646
624
647
if self . name . is_some ( ) {
625
- doc. insert ( "filename" ,
626
- Bson :: String ( self . name . as_ref ( ) . unwrap ( ) . to_owned ( ) ) ) ;
648
+ doc. insert (
649
+ "filename" ,
650
+ Bson :: String ( self . name . as_ref ( ) . unwrap ( ) . to_owned ( ) ) ,
651
+ ) ;
627
652
}
628
653
629
654
if self . content_type . is_some ( ) {
630
- doc. insert ( "contentType" ,
631
- Bson :: String ( self . content_type . as_ref ( ) . unwrap ( ) . to_owned ( ) ) ) ;
655
+ doc. insert (
656
+ "contentType" ,
657
+ Bson :: String ( self . content_type . as_ref ( ) . unwrap ( ) . to_owned ( ) ) ,
658
+ ) ;
632
659
}
633
660
634
661
if self . metadata . is_some ( ) {
635
- doc. insert ( "metadata" ,
636
- Bson :: Binary ( BinarySubtype :: Generic ,
637
- self . metadata . as_ref ( ) . unwrap ( ) . clone ( ) ) ) ;
662
+ doc. insert (
663
+ "metadata" ,
664
+ Bson :: Binary (
665
+ BinarySubtype :: Generic ,
666
+ self . metadata . as_ref ( ) . unwrap ( ) . clone ( ) ,
667
+ ) ,
668
+ ) ;
638
669
}
639
670
640
671
doc
@@ -647,7 +678,9 @@ impl CachedChunk {
647
678
CachedChunk {
648
679
n : n,
649
680
data : Vec :: new ( ) ,
650
- err : Some ( Error :: DefaultError ( String :: from ( "Chunk has not yet been initialized" ) ) ) ,
681
+ err : Some ( Error :: DefaultError (
682
+ String :: from ( "Chunk has not yet been initialized" ) ,
683
+ ) ) ,
651
684
}
652
685
}
653
686
}
0 commit comments