@@ -767,28 +767,32 @@ impl LogBatch {
767767 & mut self ,
768768 compression_threshold : usize ,
769769 compression_level : Option < usize > ,
770- ) -> Result < usize > {
770+ ) -> Result < ( usize , f64 ) > {
771771 let _t = StopWatch :: new ( perf_context ! ( log_populating_duration) ) ;
772772 debug_assert ! ( self . buf_state == BufState :: Open ) ;
773773 if self . is_empty ( ) {
774774 self . buf_state = BufState :: Encoded ( self . buf . len ( ) , 0 ) ;
775- return Ok ( 0 ) ;
775+ return Ok ( ( 0 , 0.0 ) ) ;
776776 }
777777 self . buf_state = BufState :: Incomplete ;
778778
779779 // entries
780- let ( header_offset, compression_type) = if compression_threshold > 0
780+ let ( header_offset, compression_type, compression_ratio ) = if compression_threshold > 0
781781 && self . buf . len ( ) >= LOG_BATCH_HEADER_LEN + compression_threshold
782782 {
783783 let buf_len = self . buf . len ( ) ;
784- lz4:: append_compress_block (
784+ let compression_ratio = lz4:: append_compress_block (
785785 & mut self . buf ,
786786 LOG_BATCH_HEADER_LEN ,
787787 compression_level. unwrap_or ( lz4:: DEFAULT_LZ4_COMPRESSION_LEVEL ) ,
788788 ) ?;
789- ( buf_len - LOG_BATCH_HEADER_LEN , CompressionType :: Lz4 )
789+ (
790+ buf_len - LOG_BATCH_HEADER_LEN ,
791+ CompressionType :: Lz4 ,
792+ compression_ratio,
793+ )
790794 } else {
791- ( 0 , CompressionType :: None )
795+ ( 0 , CompressionType :: None , 0.0 )
792796 } ;
793797
794798 // checksum
@@ -830,7 +834,7 @@ impl LogBatch {
830834 }
831835
832836 self . buf_state = BufState :: Encoded ( header_offset, footer_roffset - LOG_BATCH_HEADER_LEN ) ;
833- Ok ( self . buf . len ( ) - header_offset)
837+ Ok ( ( self . buf . len ( ) - header_offset, compression_ratio ) )
834838 }
835839
836840 /// Make preparations for the write of `LogBatch`.
@@ -1328,7 +1332,7 @@ mod tests {
13281332 offset : 0 ,
13291333 } ;
13301334 let old_approximate_size = batch. approximate_size ( ) ;
1331- let len = batch. finish_populate ( usize:: from ( compress) , None ) . unwrap ( ) ;
1335+ let ( len, _ ) = batch. finish_populate ( usize:: from ( compress) , None ) . unwrap ( ) ;
13321336 assert ! ( old_approximate_size >= len) ;
13331337 assert_eq ! ( batch. approximate_size( ) , len) ;
13341338 let mut batch_handle = mocked_file_block_handle;
@@ -1493,7 +1497,7 @@ mod tests {
14931497 batch1. merge ( & mut batch2) . unwrap ( ) ;
14941498 assert ! ( batch2. is_empty( ) ) ;
14951499
1496- let len = batch1. finish_populate ( 0 , None ) . unwrap ( ) ;
1500+ let ( len, _ ) = batch1. finish_populate ( 0 , None ) . unwrap ( ) ;
14971501 batch1. prepare_write ( & file_context) . unwrap ( ) ;
14981502 let encoded = batch1. encoded_bytes ( ) ;
14991503 assert_eq ! ( len, encoded. len( ) ) ;
@@ -1549,7 +1553,8 @@ mod tests {
15491553 offset : 0 ,
15501554 } ;
15511555 let buf_len = batch. buf . len ( ) ;
1552- let len = batch. finish_populate ( 1 , None ) . unwrap ( ) ;
1556+ let ( len, compression_ratio) = batch. finish_populate ( 1 , None ) . unwrap ( ) ;
1557+ assert ! ( compression_ratio == 0.0 ) ;
15531558 assert ! ( len == 0 ) ;
15541559 assert_eq ! ( batch. buf_state, BufState :: Encoded ( buf_len, 0 ) ) ;
15551560 let file_context = LogFileContext :: new ( mocked_file_block_handles. id , Version :: V2 ) ;
@@ -1671,7 +1676,8 @@ mod tests {
16711676 } ,
16721677 ] ;
16731678 let old_approximate_size = batch. approximate_size ( ) ;
1674- let len = batch. finish_populate ( 1 , None ) . unwrap ( ) ;
1679+ let ( len, compression_ratio) = batch. finish_populate ( 1 , None ) . unwrap ( ) ;
1680+ assert ! ( compression_ratio > 0.0 ) ;
16751681 assert ! ( old_approximate_size >= len) ;
16761682 assert_eq ! ( batch. approximate_size( ) , len) ;
16771683 let checksum = batch. item_batch . checksum ;
0 commit comments