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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
use std::fmt::Debug; use std::hash::Hash; use std::io::Write; use byte_string::ByteString; use phf_shared::PhfHash; use comde::{Compress, Compressor}; #[cfg(feature = "xz")] pub type XzMap<K, V> = Map<K, V, comde::xz::XzCompressor>; #[cfg(feature = "xz")] #[macro_export] macro_rules! xz_map { ($ty:ty) => { XzMap::new("::comde::xz::XzDecompressor", stringify!($ty)) }; } #[cfg(feature = "deflate")] pub type DeflateMap<K, V> = Map<K, V, comde::deflate::DeflateCompressor>; #[cfg(feature = "deflate")] #[macro_export] macro_rules! deflate_map { ($ty:ty) => { SnappyMap::new("::comde::deflate::DeflateDecompressor", stringify!($ty)) }; } #[cfg(feature = "snappy")] pub type SnappyMap<K, V> = Map<K, V, comde::snappy::SnappyCompressor>; #[cfg(feature = "snappy")] #[macro_export] macro_rules! snappy_map { ($ty:ty) => { SnappyMap::new("::comde::snappy::SnappyDecompressor", stringify!($ty)) }; } #[cfg(feature = "zstandard")] pub type ZstdMap<K, V> = Map<K, V, comde::zstd::ZstdCompressor>; #[cfg(feature = "zstandard")] #[macro_export] macro_rules! zstd_map { ($ty:ty) => { ZstdMap::new("::comde::zstd::ZstdDecompressor", stringify!($ty)) }; } pub struct Map<K, V, C> where K: Hash + PhfHash + Eq + Debug, V: Compress, C: Compressor, { map: phf_codegen::Map<K>, compressor: C, decompressor_type: &'static str, value_type: &'static str, __value: std::marker::PhantomData<V>, } impl<K, V, C> Map<K, V, C> where K: Hash + PhfHash + Eq + Debug, V: Compress, C: Compressor, { pub fn new(decompressor_type: &'static str, value_type: &'static str) -> Map<K, V, C> { Map { map: phf_codegen::Map::new(), compressor: C::new(), decompressor_type, value_type, __value: std::marker::PhantomData::<V>, } } #[inline] pub fn entry(&mut self, key: K, value: V) -> &mut Map<K, V, C> { let bytes = self.compressor.to_vec(value).unwrap(); self.map .entry(key, &format!("{:?}", ByteString::new(bytes))); self } #[inline] pub fn phf_path(&mut self, path: &str) -> &mut Map<K, V, C> { self.map.phf_path(path); self } #[inline] pub fn build<W: Write>(&self, w: &mut W) -> std::io::Result<()> { w.write(b"::comde::phf::CompressedPhfMap {\n map: ")?; self.map.build(w)?; w.write(b",\n decompressor_ty: std::marker::PhantomData::<")?; w.write(self.decompressor_type.as_bytes())?; w.write(b">,\n value_ty: std::marker::PhantomData::<")?; w.write(self.value_type.as_bytes())?; w.write(b">\n}\n")?; Ok(()) } } #[cfg(test)] mod tests { use super::*; use std::io::Cursor; #[test] #[cfg(feature = "snappy")] fn basic_snappy() { let mut map = snappy_map!(String); map.entry("boop", "this is a string string string string string string this is indeed a string string string".to_string()); let mut out = Cursor::new(vec![]); map.build(&mut out).unwrap(); println!("snappy: {}", String::from_utf8(out.into_inner()).unwrap()); } #[test] #[cfg(feature = "xz")] fn basic_xz() { let mut map = xz_map!(String); map.entry("boop", "this is a string string string string string string this is indeed a string string string".to_string()); let mut out = Cursor::new(vec![]); map.build(&mut out).unwrap(); println!("xz: {}", String::from_utf8(out.into_inner()).unwrap()); } #[test] #[cfg(feature = "deflate")] fn basic_deflate() { let mut map = deflate_map!(String); map.entry("boop", "this is a string string string string string string this is indeed a string string string".to_string()); let mut out = Cursor::new(vec![]); map.build(&mut out).unwrap(); println!("deflate: {}", String::from_utf8(out.into_inner()).unwrap()); } #[test] #[cfg(feature = "zstandard")] fn basic_zstd() { let mut map = zstd_map!(String); map.entry("boop", "this is a string string string string string string this is indeed a string string string".to_string()); let mut out = Cursor::new(vec![]); map.build(&mut out).unwrap(); println!("zstd: {}", String::from_utf8(out.into_inner()).unwrap()); } }