Skip to content

Fix compression format: needs to have the zlib header around deflate. #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ flate2 = { version = "0.2.17", optional = true }
rand = "0.3.15"
rug = "0.6.0"
ieee754 = "0.2.2"
clap = "2.26.2"

[lib]
path = "src/lib.rs"
Expand All @@ -45,3 +46,7 @@ debug=true

[profile.bench]
debug=true

[[example]]
name = "cli"
required-features = ["serialization"]
79 changes: 79 additions & 0 deletions examples/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/// Reads numbers from stdin, one per line, and writes them to a serialized histogram on stdout.

extern crate hdrsample;
extern crate clap;

use std::io::BufRead;

use clap::{App, Arg, SubCommand};

use hdrsample::Histogram;
use hdrsample::serialization::{V2Serializer, V2DeflateSerializer};

fn main() {
let default_max = format!("{}", u64::max_value());
let matches = App::new("hdrsample cli")
.subcommand(SubCommand::with_name("serialize")
.arg(Arg::with_name("min")
.long("min")
.help("Minimum discernible value")
.takes_value(true)
.default_value("1"))
.arg(Arg::with_name("max")
.long("max")
.help("Maximum trackable value")
.takes_value(true)
.default_value(default_max.as_str()))
.arg(Arg::with_name("sigfig")
.long("sigfig")
.help("Number of significant digits")
.takes_value(true)
.default_value("3"))
.arg(Arg::with_name("compression")
.short("c")
.long("compression")
.help("Enable compression"))
.arg(Arg::with_name("resize")
.short("r")
.long("resize")
.help("Enable auto resize")))
.get_matches();

match matches.subcommand_name() {
Some("serialize") => {
let sub_matches = matches.subcommand_matches("serialize").unwrap();
let min = sub_matches.value_of("min").unwrap().parse().unwrap();
let max = sub_matches.value_of("max").unwrap().parse().unwrap();
let sigfig = sub_matches.value_of("sigfig").unwrap().parse().unwrap();

let mut h: Histogram<u64> = Histogram::new_with_bounds(min, max, sigfig).unwrap();

if sub_matches.is_present("resize") {
h.auto(true);
}

serialize(h, sub_matches.is_present("compression"));
},
_ => unreachable!()
}
}

fn serialize(mut h: Histogram<u64>, compression: bool) {
let stdin = std::io::stdin();
let stdin_handle = stdin.lock();

for num in stdin_handle.lines()
.map(|l| l.expect("Should be able to read stdin"))
.map(|s| s.parse().expect("Each line must be a u64")) {
h.record(num).unwrap();
}

let stdout = std::io::stdout();
let mut stdout_handle = stdout.lock();

if compression {
V2DeflateSerializer::new().serialize(&h, &mut stdout_handle).unwrap();
} else {
V2Serializer::new().serialize(&h, &mut stdout_handle).unwrap();
}
}
4 changes: 2 additions & 2 deletions src/serialization/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::{self, Cursor, ErrorKind, Read};
use std::marker::PhantomData;
use std;
use super::byteorder::{BigEndian, ReadBytesExt};
use super::flate2::read::DeflateDecoder;
use super::flate2::read::ZlibDecoder;

/// Errors that can happen during deserialization.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Deserializer {
.ok_or(DeserializeError::UsizeTypeTooSmall)?;

// TODO reuse deflate buf, or switch to lower-level flate2::Decompress
let mut deflate_reader = DeflateDecoder::new(reader.take(payload_len as u64));
let mut deflate_reader = ZlibDecoder::new(reader.take(payload_len as u64));
let inner_cookie = deflate_reader.read_u32::<BigEndian>()?;
if inner_cookie != V2_COOKIE {
return Err(DeserializeError::InvalidCookie);
Expand Down
7 changes: 5 additions & 2 deletions src/serialization/v2_deflate_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::byteorder::{BigEndian, WriteBytesExt};
use super::flate2::Compression;
use std;
use std::io::{ErrorKind, Write};
use super::flate2::write::DeflateEncoder;
use super::flate2::write::ZlibEncoder;

/// Errors that occur during serialization.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand All @@ -23,6 +23,9 @@ impl std::convert::From<std::io::Error> for V2DeflateSerializeError {
}

/// Serializer for the V2 + DEFLATE binary format.
///
/// It's called "deflate" to stay consistent with the naming used in the Java implementation, but
/// it actually uses zlib's wrapper format around plain DEFLATE.
pub struct V2DeflateSerializer {
uncompressed_buf: Vec<u8>,
compressed_buf: Vec<u8>,
Expand Down Expand Up @@ -76,7 +79,7 @@ impl V2DeflateSerializer {

{
// TODO reuse deflate buf, or switch to lower-level flate2::Compress
let mut compressor = DeflateEncoder::new(&mut self.compressed_buf, Compression::Default);
let mut compressor = ZlibEncoder::new(&mut self.compressed_buf, Compression::Default);
compressor.write_all(&self.uncompressed_buf[0..uncompressed_len])?;
let _ = compressor.finish()?;
}
Expand Down
Binary file added tests/data/seq-nums.hist
Binary file not shown.
Binary file added tests/data/seq-nums.histz
Binary file not shown.
Loading