1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
use std::collections::hash_map::RandomState; use std::io::{prelude::*, Result, Seek, SeekFrom}; use xz2::{read::XzDecoder, write::XzEncoder}; use crate::hash_map::CompressedHashMap; use crate::{com::ByteCount, Compressor, Decompress, Decompressor}; #[cfg(feature = "with-phf")] use crate::phf::CompressedPhfMap; pub type XzHashMap<K, V> = CompressedHashMap<K, V, RandomState, XzCompressor, XzDecompressor>; #[cfg(feature = "with-phf")] pub type XzPhfMap<K, V> = CompressedPhfMap<K, V, XzCompressor>; #[derive(Debug, Copy, Clone)] pub struct XzDecompressor; impl Decompressor for XzDecompressor { fn new() -> Self { XzDecompressor } fn copy<R: Read, W: Write>(&self, source: R, mut dest: W) -> Result<u64> { let mut decoder = XzDecoder::new(source); std::io::copy(&mut decoder, &mut dest) } fn from_reader<R: Read, V: Decompress>(&self, reader: R) -> Result<V> where Self: Sized, { let decoder = XzDecoder::new(reader); V::from_reader(decoder) } } #[derive(Debug, Copy, Clone)] pub struct XzCompressor; impl Compressor for XzCompressor { fn new() -> Self { XzCompressor } fn compress<W: Write + Seek, R: Read>( &self, writer: &mut W, reader: &mut R, ) -> Result<ByteCount> { let start = writer.seek(SeekFrom::Current(0))?; let mut encoder = XzEncoder::new(writer, 9); let read = std::io::copy(reader, &mut encoder)?; let end = encoder.finish()?.seek(SeekFrom::Current(0))?; Ok(ByteCount { read, write: end - start, }) } } #[cfg(test)] mod tests { use super::*; #[test] fn basic() { let mut map = XzHashMap::<String, String>::new(); map.insert("foo".into(), "bar".into()); assert_eq!("bar".to_string(), map.get("foo").unwrap()); assert_ne!("bap".to_string(), map.get("foo").unwrap()); } #[test] #[cfg(feature = "with-phf")] fn with_phf() {} }