From e040ea0f680810bcf2daf5a0a5872f26f52e1bea Mon Sep 17 00:00:00 2001 From: Elijah H Date: Fri, 14 Apr 2023 22:28:04 +0200 Subject: [PATCH 1/6] LowFruit: Add missing ` in documentation --- src/swapvec.rs | 8 ++++---- src/swapveciter.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/swapvec.rs b/src/swapvec.rs index b9f79c8..d507ed8 100644 --- a/src/swapvec.rs +++ b/src/swapvec.rs @@ -23,11 +23,11 @@ pub enum Compression { /// Configure when and how the vector should swap. /// -/// The file creation will happen after max(swap_after, batch_size) +/// The file creation will happen after max(`swap_after`, `batch_size`) /// elements. /// /// Keep in mind, that if the temporary file exists, -/// after ever batch_size elements, at least one write (syscall) +/// after ever `batch_size` elements, at least one write (syscall) /// will happen. #[derive(Debug, Clone)] pub struct SwapVecConfig { @@ -169,7 +169,7 @@ where } /// Check if a file has been created. - /// Is false if element count is below swap_after and below batch_size + /// Is false if element count is below `swap_after` and below `batch_size` pub fn written_to_file(&self) -> bool { self.tempfile.is_some() } @@ -186,7 +186,7 @@ where } } - /// Basically elements pushed // batch_size + /// Basically elements pushed // `batch_size` pub fn batches_written(&self) -> usize { match self.tempfile.as_ref() { None => 0, diff --git a/src/swapveciter.rs b/src/swapveciter.rs index 0ad644c..639e5fa 100644 --- a/src/swapveciter.rs +++ b/src/swapveciter.rs @@ -15,7 +15,7 @@ pub struct CheckedFileRead { pub batch_info_rev: Vec, } -/// Iterator for SwapVec. +/// Iterator for `SwapVec`. /// /// Items might be read from disk, /// so every item is wrapped in a `Result`. From 7542ac164b62f6af44a4db4748d6d6cd8ff262e7 Mon Sep 17 00:00:00 2001 From: Elijah H Date: Fri, 14 Apr 2023 23:20:09 +0200 Subject: [PATCH 2/6] LowFruit: Use `map_or` instead of `map(..).unwrap_or(..)` --- src/swapvec.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/swapvec.rs b/src/swapvec.rs index d507ed8..3a4e774 100644 --- a/src/swapvec.rs +++ b/src/swapvec.rs @@ -128,11 +128,7 @@ impl Deserialize<'a>> Debug for SwapVec { f, "SwapVec {{elements_in_ram: {}, elements_in_file: {}, filedescriptor: {:#?}}}", self.vector.len(), - self.tempfile - .as_ref() - .map(|x| x.batch_info.len()) - .unwrap_or(0) - * self.config.batch_size, + self.tempfile.as_ref().map_or(0, |x| x.batch_info.len()) * self.config.batch_size, self.tempfile.as_ref().map(|x| x.file.as_raw_fd()) ) } From 08ea5c30bcb0cca93decb2a48afacc2cf0a4f64a Mon Sep 17 00:00:00 2001 From: Elijah H Date: Fri, 14 Apr 2023 23:21:30 +0200 Subject: [PATCH 3/6] LowFruit: Remove manual `Result::transpose` * Also replace closure with `Ok` --- src/swapveciter.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/swapveciter.rs b/src/swapveciter.rs index 639e5fa..412d33c 100644 --- a/src/swapveciter.rs +++ b/src/swapveciter.rs @@ -128,15 +128,8 @@ impl Deserialize<'a> + Hash> Iterator for SwapVecIter return Some(Ok(item)); } - let next_in_batch = self.next_in_batch(); - if let Err(err) = next_in_batch { - return Some(Err(err)); - } - if let Ok(Some(item)) = next_in_batch { - return Some(Ok(item)); - } - - // File has been exhausted. - self.last_elements.pop_front().map(|x| Ok(x)) + self.next_in_batch() + .transpose() + .or_else(|| self.last_elements.pop_front().map(Ok)) } } From a1e953706ad5e66f0b350e1d84003ed1fede37b1 Mon Sep 17 00:00:00 2001 From: Elijah H Date: Fri, 14 Apr 2023 23:25:43 +0200 Subject: [PATCH 4/6] LowFruit: Add some whitespace --- src/swapvec.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/swapvec.rs b/src/swapvec.rs index 3a4e774..dca76be 100644 --- a/src/swapvec.rs +++ b/src/swapvec.rs @@ -194,9 +194,11 @@ where if self.vector.len() < self.config.batch_size { return Ok(()); } + if self.tempfile.is_none() && self.vector.len() < self.config.swap_after { return Ok(()); } + // Do action if self.tempfile.is_none() { let tf = tempfile::tempfile()?; From 327c4335aca3b9e0f7cf69440d557f455dd36f7f Mon Sep 17 00:00:00 2001 From: Elijah H Date: Fri, 14 Apr 2023 23:28:52 +0200 Subject: [PATCH 5/6] LowFruit: Use `map_or` instead of `match` --- src/swapvec.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/swapvec.rs b/src/swapvec.rs index dca76be..15edee6 100644 --- a/src/swapvec.rs +++ b/src/swapvec.rs @@ -184,10 +184,7 @@ where /// Basically elements pushed // `batch_size` pub fn batches_written(&self) -> usize { - match self.tempfile.as_ref() { - None => 0, - Some(f) => f.batch_info.len(), - } + self.tempfile.as_ref().map_or(0, |f| f.batch_info.len()) } fn after_push_work(&mut self) -> Result<(), SwapVecError> { From c6d3b0de64cec4c95436d5b40cb29a90c8db5847 Mon Sep 17 00:00:00 2001 From: Elijah H Date: Fri, 14 Apr 2023 23:29:47 +0200 Subject: [PATCH 6/6] LowFruit: Replace `for_each` with equivalent method call --- src/swapvec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/swapvec.rs b/src/swapvec.rs index 15edee6..641a6a6 100644 --- a/src/swapvec.rs +++ b/src/swapvec.rs @@ -210,7 +210,7 @@ where .collect::>(); let mut batch_hash = DefaultHasher::new(); - batch.iter().for_each(|x| x.hash(&mut batch_hash)); + batch.hash(&mut batch_hash); let buffer = bincode::serialize(&batch)?; self.tempfile.as_mut().unwrap().write_all(&buffer)?;