1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 20:17:45 +00:00

Add dynamic buffer adjustment, fix test comment

This commit is contained in:
electricboogie 2021-04-25 15:13:27 -05:00
parent ab594b7b4c
commit 733949b2e7
3 changed files with 22 additions and 16 deletions

View file

@ -174,13 +174,23 @@ where
{
let mut total_read = 0;
let mut chunk = Vec::new();
// Initial buffer is specified by user
let mut adjusted_buffer_size = self.buffer_bytes;
// make the initial chunks on disk
for seq in unsorted {
total_read += seq.get_size();
let seq_size = seq.get_size();
total_read += seq_size;
// Grow buffer size for a Line larger than buffer
adjusted_buffer_size =
if adjusted_buffer_size < seq_size {
seq_size
} else {
adjusted_buffer_size
};
chunk.push(seq);
if total_read >= self.buffer_bytes {
if total_read >= adjusted_buffer_size {
super::sort_by(&mut chunk, &self.settings);
self.write_chunk(
&iter.tmp_dir.path().join(iter.chunks.to_string()),
@ -247,10 +257,7 @@ where
let line_s = line?;
bytes_read += line_s.len() + 1;
// This is where the bad stuff happens usually
let deserialized: Line = match serde_json::from_str(&line_s) {
Ok(x) => x,
Err(err) => panic!("JSON read error: {}", err),
};
let deserialized: Line = serde_json::from_str(&line_s).expect("JSON read error: ");
total_read += deserialized.get_size();
vec.push_back(deserialized);
if total_read > max_bytes {

View file

@ -141,13 +141,12 @@ impl GlobalSettings {
.expect("Error parsing buffer size: ");
let suf_usize: usize = match suf_str.to_uppercase().as_str() {
// SI Units
"K" => 1000usize,
"M" => 1000000usize,
"G" => 1000000000usize,
"T" => 1000000000000usize,
"P" => 1000000000000000usize,
"E" => 1000000000000000000usize,
_ => 1usize,
"K" => 1024usize,
"M" => 1024000usize,
"G" => 1024000000usize,
"T" => 1024000000000usize,
// GNU regards empty human numeric value as 1024 bytes
_ => 1024usize,
};
num_usize * suf_usize
}

View file

@ -59,13 +59,13 @@ fn test_human_numeric_whitespace() {
test_helper("human-numeric-whitespace", "-h");
}
// This tests the ext sort feature, but it also tests where
// serde might fail when reading back JSON if it finds a null value
// This tests where serde often fails when reading back JSON
// if it finds a null value
#[test]
fn test_extsort_as64_bailout() {
new_ucmd!()
.arg("-g")
.arg("-S 10K")
.arg("-S 5K")
.arg("multiple_decimals_general.txt")
.succeeds()
.stdout_is_fixture("multiple_decimals_general.expected");