Compare commits

...

2 Commits

Author SHA1 Message Date
Konstantin Knizhnik
d07101d317 Make clippy happy 2023-03-10 09:52:12 +02:00
Konstantin Knizhnik
89e4fc3c63 Copy block content in block cursor cache 2023-03-10 08:19:20 +02:00
2 changed files with 7 additions and 10 deletions

View File

@@ -74,7 +74,7 @@ where
{ {
reader: R, reader: R,
/// last accessed page /// last accessed page
cache: Option<(u32, R::BlockLease)>, cache: (u32, [u8; PAGE_SZ]),
} }
impl<R> BlockCursor<R> impl<R> BlockCursor<R>
@@ -84,22 +84,20 @@ where
pub fn new(reader: R) -> Self { pub fn new(reader: R) -> Self {
BlockCursor { BlockCursor {
reader, reader,
cache: None, cache: (u32::MAX, [0u8; PAGE_SZ]),
} }
} }
pub fn read_blk(&mut self, blknum: u32) -> Result<&Self, std::io::Error> { pub fn read_blk(&mut self, blknum: u32) -> Result<&Self, std::io::Error> {
// Fast return if this is the same block as before // Fast return if this is the same block as before
if let Some((cached_blk, _buf)) = &self.cache { if self.cache.0 == blknum {
if *cached_blk == blknum { return Ok(self);
return Ok(self);
}
} }
// Read the block from the underlying reader, and cache it // Read the block from the underlying reader, and cache it
self.cache = None;
let buf = self.reader.read_blk(blknum)?; let buf = self.reader.read_blk(blknum)?;
self.cache = Some((blknum, buf)); self.cache.0 = blknum;
self.cache.1[..].copy_from_slice(&buf[..]);
Ok(self) Ok(self)
} }
@@ -112,7 +110,7 @@ where
type Target = [u8; PAGE_SZ]; type Target = [u8; PAGE_SZ];
fn deref(&self) -> &<Self as Deref>::Target { fn deref(&self) -> &<Self as Deref>::Target {
&self.cache.as_ref().unwrap().1 &self.cache.1
} }
} }

View File

@@ -427,7 +427,6 @@ mod tests {
let actual = cursor.read_blob(pos)?; let actual = cursor.read_blob(pos)?;
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
drop(cursor);
// Test a large blob that spans multiple pages // Test a large blob that spans multiple pages
let mut large_data = Vec::new(); let mut large_data = Vec::new();