@@ -270,15 +270,16 @@ pub trait Storage: Clone + Send + Sync + 'static {
270270 /// The readable/writeable storage buffer that can be opened by this Storage.
271271 type Blob : Blob ;
272272
273- /// Open an existing blob in a given partition or create a new one.
273+ /// Open an existing blob in a given partition or create a new one, returning
274+ /// the blob and its length.
274275 ///
275276 /// Multiple instances of the same blob can be opened concurrently, however,
276277 /// writing to the same blob concurrently may lead to undefined behavior.
277278 fn open (
278279 & self ,
279280 partition : & str ,
280281 name : & [ u8 ] ,
281- ) -> impl Future < Output = Result < Self :: Blob , Error > > + Send ;
282+ ) -> impl Future < Output = Result < ( Self :: Blob , u64 ) , Error > > + Send ;
282283
283284 /// Remove a blob from a given partition.
284285 ///
@@ -305,9 +306,6 @@ pub trait Storage: Clone + Send + Sync + 'static {
305306/// and writing to both is undefined behavior.
306307#[ allow( clippy:: len_without_is_empty) ]
307308pub trait Blob : Clone + Send + Sync + ' static {
308- /// Get the length of the blob.
309- fn len ( & self ) -> impl Future < Output = Result < u64 , Error > > + Send ;
310-
311309 /// Read from the blob at the given offset.
312310 ///
313311 /// `read_at` does not return the number of bytes read because it
@@ -521,7 +519,7 @@ mod tests {
521519 let name = b"test_blob" ;
522520
523521 // Open a new blob
524- let blob = context
522+ let ( blob, _ ) = context
525523 . open ( partition, name)
526524 . await
527525 . expect ( "Failed to open blob" ) ;
@@ -542,10 +540,6 @@ mod tests {
542540 . expect ( "Failed to read from blob" ) ;
543541 assert_eq ! ( & buffer, data) ;
544542
545- // Get blob length
546- let length = blob. len ( ) . await . expect ( "Failed to get blob length" ) ;
547- assert_eq ! ( length, data. len( ) as u64 ) ;
548-
549543 // Close the blob
550544 blob. close ( ) . await . expect ( "Failed to close blob" ) ;
551545
@@ -557,10 +551,11 @@ mod tests {
557551 assert ! ( blobs. contains( & name. to_vec( ) ) ) ;
558552
559553 // Reopen the blob
560- let blob = context
554+ let ( blob, len ) = context
561555 . open ( partition, name)
562556 . await
563557 . expect ( "Failed to reopen blob" ) ;
558+ assert_eq ! ( len, data. len( ) as u64 ) ;
564559
565560 // Read data part of message back
566561 let mut buffer = vec ! [ 0u8 ; 7 ] ;
@@ -606,7 +601,7 @@ mod tests {
606601 let name = b"test_blob_rw" ;
607602
608603 // Open a new blob
609- let blob = context
604+ let ( blob, _ ) = context
610605 . open ( partition, name)
611606 . await
612607 . expect ( "Failed to open blob" ) ;
@@ -621,10 +616,6 @@ mod tests {
621616 . await
622617 . expect ( "Failed to write data2" ) ;
623618
624- // Assert that length tracks pending data
625- let length = blob. len ( ) . await . expect ( "Failed to get blob length" ) ;
626- assert_eq ! ( length, 10 ) ;
627-
628619 // Read data back
629620 let mut buffer = vec ! [ 0u8 ; 10 ] ;
630621 blob. read_at ( & mut buffer, 0 )
@@ -638,13 +629,9 @@ mod tests {
638629 blob. write_at ( data3, 5 )
639630 . await
640631 . expect ( "Failed to write data3" ) ;
641- let length = blob. len ( ) . await . expect ( "Failed to get blob length" ) ;
642- assert_eq ! ( length, 10 ) ;
643632
644633 // Truncate the blob
645634 blob. truncate ( 5 ) . await . expect ( "Failed to truncate blob" ) ;
646- let length = blob. len ( ) . await . expect ( "Failed to get blob length" ) ;
647- assert_eq ! ( length, 5 ) ;
648635 let mut buffer = vec ! [ 0u8 ; 5 ] ;
649636 blob. read_at ( & mut buffer, 0 )
650637 . await
@@ -654,7 +641,7 @@ mod tests {
654641 // Full read after truncation
655642 let mut buffer = vec ! [ 0u8 ; 10 ] ;
656643 let result = blob. read_at ( & mut buffer, 0 ) . await ;
657- assert ! ( matches! ( result, Err ( Error :: BlobInsufficientLength ) ) ) ;
644+ assert ! ( result. is_err ( ) ) ;
658645
659646 // Close the blob
660647 blob. close ( ) . await . expect ( "Failed to close blob" ) ;
@@ -668,17 +655,17 @@ mod tests {
668655 runner. start ( |context| async move {
669656 let partitions = [ "partition1" , "partition2" , "partition3" ] ;
670657 let name = b"test_blob_rw" ;
658+ let data1 = b"Hello" ;
659+ let data2 = b"World" ;
671660
672661 for ( additional, partition) in partitions. iter ( ) . enumerate ( ) {
673662 // Open a new blob
674- let blob = context
663+ let ( blob, _ ) = context
675664 . open ( partition, name)
676665 . await
677666 . expect ( "Failed to open blob" ) ;
678667
679668 // Write data at different offsets
680- let data1 = b"Hello" ;
681- let data2 = b"World" ;
682669 blob. write_at ( data1, 0 )
683670 . await
684671 . expect ( "Failed to write data1" ) ;
@@ -692,10 +679,11 @@ mod tests {
692679
693680 for ( additional, partition) in partitions. iter ( ) . enumerate ( ) {
694681 // Open a new blob
695- let blob = context
682+ let ( blob, len ) = context
696683 . open ( partition, name)
697684 . await
698685 . expect ( "Failed to open blob" ) ;
686+ assert_eq ! ( len, ( data1. len( ) + data2. len( ) + additional) as u64 ) ;
699687
700688 // Read data back
701689 let mut buffer = vec ! [ 0u8 ; 10 + additional] ;
@@ -720,15 +708,15 @@ mod tests {
720708 let name = b"test_blob_rw" ;
721709
722710 // Open a new blob
723- let blob = context
711+ let ( blob, _ ) = context
724712 . open ( partition, name)
725713 . await
726714 . expect ( "Failed to open blob" ) ;
727715
728716 // Read data past file length (empty file)
729717 let mut buffer = vec ! [ 0u8 ; 10 ] ;
730718 let result = blob. read_at ( & mut buffer, 0 ) . await ;
731- assert ! ( matches! ( result, Err ( Error :: BlobInsufficientLength ) ) ) ;
719+ assert ! ( result. is_err ( ) ) ;
732720
733721 // Write data to the blob
734722 let data = b"Hello, Storage!" ;
@@ -739,7 +727,7 @@ mod tests {
739727 // Read data past file length (non-empty file)
740728 let mut buffer = vec ! [ 0u8 ; 20 ] ;
741729 let result = blob. read_at ( & mut buffer, 0 ) . await ;
742- assert ! ( matches! ( result, Err ( Error :: BlobInsufficientLength ) ) ) ;
730+ assert ! ( result. is_err ( ) ) ;
743731 } )
744732 }
745733
@@ -752,7 +740,7 @@ mod tests {
752740 let name = b"test_blob_rw" ;
753741
754742 // Open a new blob
755- let blob = context
743+ let ( blob, _ ) = context
756744 . open ( partition, name)
757745 . await
758746 . expect ( "Failed to open blob" ) ;
@@ -800,10 +788,6 @@ mod tests {
800788 . expect ( "Failed to read from blob" ) ;
801789 assert_eq ! ( & buffer, data) ;
802790
803- // Get blob length
804- let length = blob. len ( ) . await . expect ( "Failed to get blob length" ) ;
805- assert_eq ! ( length, data. len( ) as u64 ) ;
806-
807791 // Close the blob
808792 blob. close ( ) . await . expect ( "Failed to close blob" ) ;
809793
0 commit comments