From c439cb11a908ee5405ed0a3a721e9c8c0e299ad2 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 2 Jun 2022 23:14:10 +0200 Subject: Sqlite iter with unsafe code --- src/garage/repair.rs | 57 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 20 deletions(-) (limited to 'src/garage/repair.rs') diff --git a/src/garage/repair.rs b/src/garage/repair.rs index 04d9ee72..762a8300 100644 --- a/src/garage/repair.rs +++ b/src/garage/repair.rs @@ -66,18 +66,10 @@ impl Repair { async fn repair_versions(&self, must_exit: &watch::Receiver) -> Result<(), Error> { let mut pos = vec![]; - while let Some(item) = self - .garage - .version_table - .data - .store - .range((Bound::Excluded(pos), Bound::Unbounded))? - .next() - { - let (item_key, item_bytes) = item?; - pos = item_key.to_vec(); + while let Some((item_key, item_bytes)) = self.get_next_version_after(&pos)? { + pos = item_key; - let version = rmp_serde::decode::from_read_ref::<_, Version>(item_bytes.as_ref())?; + let version = rmp_serde::decode::from_read_ref::<_, Version>(&item_bytes)?; if version.deleted.get() { continue; } @@ -113,22 +105,30 @@ impl Repair { Ok(()) } - async fn repair_block_ref(&self, must_exit: &watch::Receiver) -> Result<(), Error> { - let mut pos = vec![]; - - while let Some(item) = self + fn get_next_version_after(&self, pos: &[u8]) -> Result, Vec)>, Error> { + match self .garage - .block_ref_table + .version_table .data .store - .range((Bound::Excluded(pos), Bound::Unbounded))? + .range::<&[u8], _>((Bound::Excluded(pos), Bound::Unbounded))? .next() { - let (item_key, item_bytes) = item?; + None => Ok(None), + Some(item) => { + let (item_key, item_bytes) = item?; + Ok(Some((item_key.into_owned(), item_bytes.into_owned()))) + } + } + } - pos = item_key.to_vec(); + async fn repair_block_ref(&self, must_exit: &watch::Receiver) -> Result<(), Error> { + let mut pos = vec![]; - let block_ref = rmp_serde::decode::from_read_ref::<_, BlockRef>(item_bytes.as_ref())?; + while let Some((item_key, item_bytes)) = self.get_next_block_ref_after(&pos)? { + pos = item_key; + + let block_ref = rmp_serde::decode::from_read_ref::<_, BlockRef>(&item_bytes)?; if block_ref.deleted.get() { continue; } @@ -160,4 +160,21 @@ impl Repair { } Ok(()) } + + fn get_next_block_ref_after(&self, pos: &[u8]) -> Result, Vec)>, Error> { + match self + .garage + .block_ref_table + .data + .store + .range::<&[u8], _>((Bound::Excluded(pos), Bound::Unbounded))? + .next() + { + None => Ok(None), + Some(item) => { + let (item_key, item_bytes) = item?; + Ok(Some((item_key.into_owned(), item_bytes.into_owned()))) + } + } + } } -- cgit v1.2.3