changeset 553:bd8c4d4688d5

use conforming format for snappy compression This finally enables reading/writing compressed leveldbs written by the original implementation. Before this, the incompatible compression format prevented this. Fixes #17
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 30 Jun 2022 21:09:05 -0700
parents f43574f976dc
children 1a7d1eba51bc
files examples/leveldb-tool/src/main.rs src/table_block.rs src/table_builder.rs
diffstat 3 files changed, 3 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/examples/leveldb-tool/src/main.rs	Mon Mar 07 22:00:31 2022 +0100
+++ b/examples/leveldb-tool/src/main.rs	Thu Jun 30 21:09:05 2022 -0700
@@ -59,7 +59,7 @@
     let mut opt = Options::default();
     opt.reuse_logs = false;
     opt.reuse_manifest = false;
-    opt.compression_type = rusty_leveldb::CompressionType::CompressionNone;
+    opt.compression_type = rusty_leveldb::CompressionType::CompressionSnappy;
     let mut db = DB::open("tooldb", opt).unwrap();
 
     match args[1].as_str() {
--- a/src/table_block.rs	Mon Mar 07 22:00:31 2022 +0100
+++ b/src/table_block.rs	Thu Jun 30 21:09:05 2022 -0700
@@ -10,8 +10,6 @@
 
 use crc::crc32::{self, Hasher32};
 use integer_encoding::FixedInt;
-use snap::read::FrameDecoder;
-use std::io::Read;
 
 /// Reads the data for the specified block handle from a file.
 fn read_bytes(f: &dyn RandomAccess, location: &BlockHandle) -> Result<Vec<u8>> {
@@ -76,8 +74,7 @@
         match ctype {
             CompressionType::CompressionNone => Ok(Block::new(opt, buf)),
             CompressionType::CompressionSnappy => {
-                let mut decoded = vec![];
-                FrameDecoder::new(buf.as_slice()).read_to_end(&mut decoded)?;
+                let decoded = snap::raw::Decoder::new().decompress_vec(buf.as_slice())?;
                 Ok(Block::new(opt, decoded))
             }
         }
--- a/src/table_builder.rs	Mon Mar 07 22:00:31 2022 +0100
+++ b/src/table_builder.rs	Thu Jun 30 21:09:05 2022 -0700
@@ -16,7 +16,6 @@
 use crc::crc32;
 use crc::Hasher32;
 use integer_encoding::FixedIntWriter;
-use snap::write::FrameEncoder;
 
 pub const FOOTER_LENGTH: usize = 40;
 pub const FULL_FOOTER_LENGTH: usize = FOOTER_LENGTH + 8;
@@ -202,12 +201,7 @@
     fn write_block(&mut self, block: BlockContents, ctype: CompressionType) -> Result<BlockHandle> {
         let mut data = block;
         if ctype == CompressionType::CompressionSnappy {
-            let mut encoded = vec![];
-            {
-                let mut encoder = FrameEncoder::new(&mut encoded);
-                encoder.write(&data)?;
-            }
-            data = encoded;
+            data = snap::raw::Encoder::new().compress_vec(&data)?;
         }
 
         let mut digest = crc32::Digest::new(crc32::CASTAGNOLI);