From 1949427bd4b249ef0b3228ff7b2f3ff65f763b06 Mon Sep 17 00:00:00 2001 From: Philipp Korber Date: Mon, 7 Aug 2023 05:09:21 +0200 Subject: [PATCH] use IterAsTuple::chunks more and chunks.len() => chunks.element_count() --- web-api/src/storage/postgres.rs | 17 +++++++++-------- web-api/src/storage/utils.rs | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/web-api/src/storage/postgres.rs b/web-api/src/storage/postgres.rs index 1a5e422b0..deba16bfd 100644 --- a/web-api/src/storage/postgres.rs +++ b/web-api/src/storage/postgres.rs @@ -166,8 +166,8 @@ impl Database { let mut builder = QueryBuilder::new("DELETE FROM document WHERE document_id IN "); let all = ids.into_iter(); let mut deleted = Vec::with_capacity(all.len()); - let mut ids = all.clone(); - while let Ok(ids) = IterAsTuple::new(ids.by_ref().take(Self::BIND_LIMIT)) { + let mut chunks = IterAsTuple::chunks(Self::BIND_LIMIT, all.clone()); + while let Some(ids) = chunks.next() { deleted.extend( builder .reset() @@ -227,7 +227,7 @@ impl Database { WHERE document_id IN ", ); let mut chunks = IterAsTuple::chunks(Self::BIND_LIMIT, ids); - let mut documents = Vec::with_capacity(chunks.len()); + let mut documents = Vec::with_capacity(chunks.element_count()); while let Some(ids) = chunks.next() { documents.extend( builder @@ -267,8 +267,9 @@ impl Database { include_snippet.then_some(", snippet").unwrap_or_default(), )); let mut documents = Vec::with_capacity(scores.len()); - let mut ids = scores.keys().map(SnippetId::document_id); - while let Ok(ids) = IterAsTuple::new(ids.by_ref().take(Self::BIND_LIMIT)) { + let mut chunks = + IterAsTuple::chunks(Self::BIND_LIMIT, scores.keys().map(SnippetId::document_id)); + while let Some(ids) = chunks.next() { documents.extend( builder .reset() @@ -320,9 +321,9 @@ impl Database { FROM document WHERE document_id IN ", ); - let mut ids = ids.into_iter(); - let mut documents = Vec::with_capacity(ids.len()); - while let Ok(ids) = IterAsTuple::new(ids.by_ref().take(Self::BIND_LIMIT)) { + let mut chunks = IterAsTuple::chunks(Self::BIND_LIMIT, ids); + let mut documents = Vec::with_capacity(chunks.element_count()); + while let Some(ids) = chunks.next() { documents.extend( builder .reset() diff --git a/web-api/src/storage/utils.rs b/web-api/src/storage/utils.rs index ec909eb2c..eeca29e74 100644 --- a/web-api/src/storage/utils.rs +++ b/web-api/src/storage/utils.rs @@ -175,7 +175,7 @@ where IterAsTuple::new(self.iter.by_ref().take(self.chunk_size)).ok() } - pub(super) fn len(&self) -> usize + pub(super) fn element_count(&self) -> usize where I: ExactSizeIterator, {