@@ -68,12 +68,13 @@ pub struct RowSizeConfig {
6868#[ derive( Default , serde:: Deserialize , serde:: Serialize , Debug , PartialEq , Eq ) ]
6969pub enum SizeUnits {
7070 Bytes ,
71- #[ default]
7271 KB ,
7372 MB ,
7473 GB ,
7574 TB ,
76- // TODO 5: Add an auto option to use smallest non fractional (would need to use str instead of f32)
75+ #[ default]
76+ /// Is output as a String because it includes the unit and is the largest unit where the output value is >= 1
77+ Auto ,
7778}
7879impl SizeUnits {
7980 pub ( crate ) fn convert ( & self , row_size_in_bytes : usize ) -> serde_json:: Value {
@@ -83,6 +84,26 @@ impl SizeUnits {
8384 SizeUnits :: MB => 1024.0 * 1024.0 ,
8485 SizeUnits :: GB => 1024.0 * 1024.0 * 1024.0 ,
8586 SizeUnits :: TB => 1024.0 * 1024.0 * 1024.0 * 1024.0 ,
87+ SizeUnits :: Auto => {
88+ // TODO 5: Rewrite with shifts for performance
89+ // Special handling see doc string for explanation
90+ let units = [ "Bytes" , "KB" , "MB" , "GB" , "TB" ] ;
91+ let mut last_index = 0 ;
92+ let mut scalar = 1.0f64 ;
93+ let row_size_in_bytes = row_size_in_bytes as f64 ;
94+ for i in 1 ..units. len ( ) {
95+ let new_scalar = scalar * 1024.0 ;
96+ if ( row_size_in_bytes / new_scalar) >= 1.0 {
97+ last_index = i;
98+ scalar = new_scalar;
99+ } else {
100+ // Last was as correct unit
101+ break ;
102+ }
103+ }
104+ let result = row_size_in_bytes / scalar;
105+ return format ! ( "{result:0>9.4} {}" , units[ last_index] ) . into ( ) ;
106+ }
86107 } ;
87108 let result = row_size_in_bytes as f64 / scalar;
88109 result. into ( )
@@ -147,7 +168,7 @@ impl Default for DataDisplayOptions {
147168 row_idx_field_name : Some ( "row#" . to_string ( ) ) ,
148169 row_size_config : Some ( RowSizeConfig {
149170 field_name : "row_size" . to_string ( ) ,
150- units : SizeUnits :: Bytes ,
171+ units : SizeUnits :: Auto ,
151172 } ) ,
152173 row_parse_error_handling : Default :: default ( ) ,
153174 level_conversion : Some ( Default :: default ( ) ) ,
0 commit comments