Skip to content

Commit 4bb5eff

Browse files
committed
compressor test
1 parent 856b4de commit 4bb5eff

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ serde = ["dep:serde"]
2626
ahash = "0.8.11"
2727
byteorder = "1.5.0"
2828
log = "0.4.22"
29-
lz4_flex = { version = "0.11.3", optional = true }
3029
min-max-heap = "1.3.0"
3130
miniz_oxide = { version = "0.7.4", optional = true }
3231
path-absolutize = "3.1.1"
@@ -41,6 +40,7 @@ xxhash-rust = { version = "0.8.12", features = ["xxh3"] }
4140
criterion = "0.5.1"
4241
rand = "0.8.5"
4342
test-log = "0.2.16"
43+
lz4_flex = { version = "0.11.3" }
4444

4545
[[bench]]
4646
name = "value_log"

src/segment/multi_writer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl MultiWriter {
5151

5252
/// Sets the compression method
5353
#[must_use]
54+
#[doc(hidden)]
5455
pub fn use_compression(mut self, compressor: Arc<dyn Compressor>) -> Self {
5556
self.compression = Some(compressor.clone());
5657
self.get_active_writer_mut().compression = Some(compressor);
@@ -111,7 +112,7 @@ impl MultiWriter {
111112
Ok(())
112113
}
113114

114-
/// Writes an item
115+
/// Writes an item.
115116
///
116117
/// # Errors
117118
///

tests/compression.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::sync::Arc;
2+
use test_log::test;
3+
use value_log::{Compressor, Config, IndexWriter, MockIndex, MockIndexWriter, ValueLog};
4+
5+
struct Lz4Compressor;
6+
impl Compressor for Lz4Compressor {
7+
fn compress(&self, bytes: &[u8]) -> Result<Vec<u8>, value_log::CompressError> {
8+
Ok(lz4_flex::compress_prepend_size(bytes))
9+
}
10+
11+
fn decompress(&self, bytes: &[u8]) -> Result<Vec<u8>, value_log::DecompressError> {
12+
lz4_flex::decompress_size_prepended(bytes)
13+
.map_err(|e| value_log::DecompressError(e.to_string()))
14+
}
15+
}
16+
17+
#[test]
18+
fn compression() -> value_log::Result<()> {
19+
let folder = tempfile::tempdir()?;
20+
let vl_path = folder.path();
21+
22+
let index = MockIndex::default();
23+
24+
let value_log = ValueLog::open(
25+
vl_path,
26+
Config::default().use_compression(Arc::new(Lz4Compressor)),
27+
)?;
28+
29+
let mut index_writer = MockIndexWriter(index.clone());
30+
let mut writer = value_log.get_writer()?;
31+
32+
let key = "abc";
33+
let value = "verycompressable".repeat(1_000);
34+
35+
let handle = writer.get_next_value_handle();
36+
index_writer.insert_indirect(key.as_bytes(), handle.clone(), value.len() as u32)?;
37+
38+
let written_bytes = writer.write(key, &value)?;
39+
assert!(written_bytes < value.len() as u32);
40+
41+
value_log.register_writer(writer)?;
42+
43+
assert_eq!(
44+
&*value_log.get(&handle)?.expect("value should exist"),
45+
value.as_bytes(),
46+
);
47+
48+
Ok(())
49+
}

0 commit comments

Comments
 (0)