changeset 104:03950ebabe06

Merge pull request #6 from theli-ua/master Avoid doing 3 io requests when reading a block
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 26 Apr 2020 10:10:09 +0200
parents 41d531d9ee5a (current diff) 8c56c0acf795 (diff)
children 3dc5d8e21f50
files
diffstat 1 files changed, 10 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/table_block.rs	Sat Apr 25 09:40:51 2020 +0200
+++ b/src/table_block.rs	Sun Apr 26 10:10:09 2020 +0200
@@ -44,21 +44,16 @@
     // The block is denoted by offset and length in BlockHandle. A block in an encoded
     // table is followed by 1B compression type and 4B checksum.
     // The checksum refers to the compressed contents.
-    let buf = read_bytes(f, location)?;
-    let compress = read_bytes(
-        f,
-        &BlockHandle::new(
-            location.offset() + location.size(),
-            table_builder::TABLE_BLOCK_COMPRESS_LEN,
-        ),
-    )?;
-    let cksum = read_bytes(
-        f,
-        &BlockHandle::new(
-            location.offset() + location.size() + table_builder::TABLE_BLOCK_COMPRESS_LEN,
-            table_builder::TABLE_BLOCK_CKSUM_LEN,
-        ),
-    )?;
+
+    let block_data_size = location.size();
+    let location = BlockHandle::new(
+        location.offset(),
+        block_data_size + table_builder::TABLE_BLOCK_CKSUM_LEN + table_builder::TABLE_BLOCK_COMPRESS_LEN,
+    );
+    let mut buf = read_bytes(f, &location)?;
+
+    let mut compress = buf.split_off(block_data_size);
+    let cksum = compress.split_off(table_builder::TABLE_BLOCK_COMPRESS_LEN);
 
     if !verify_table_block(&buf, compress[0], unmask_crc(u32::decode_fixed(&cksum))) {
         return err(