Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-j97 committed Jun 26, 2024
1 parent 47acf87 commit ddc7c97
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 37 deletions.
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@
//! let value_log = ValueLog::open(path, Config::default())?;
//!
//! // Write some data
//! # let index_writer = MockIndexWriter(index.clone());
//! let mut writer = value_log.get_writer(index_writer)?;
//! # let mut index_writer = MockIndexWriter(index.clone());
//! let mut writer = value_log.get_writer()?;
//!
//! for key in ["a", "b", "c", "d", "e"] {
//! let value = key.repeat(10_000);
//! let value = value.as_bytes();
//!
//! writer.write(key.as_bytes(), value)?;
//! let key = key.as_bytes();
//!
//! let handle = writer.get_next_value_handle(key);
//! index_writer.insert_indirect(key, handle, value.len() as u32)?;
//!
//! writer.write(key, value)?;
//! }
//!
//! // Finish writing
Expand Down
6 changes: 6 additions & 0 deletions src/segment/multi_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,23 @@ impl MultiWriter {
}

#[doc(hidden)]
#[must_use]
pub fn get_active_writer(&self) -> &Writer {
// NOTE: initialized in constructor
#[allow(clippy::expect_used)]
self.writers.last().expect("should exist")
}

fn get_active_writer_mut(&mut self) -> &mut Writer {
// NOTE: initialized in constructor
#[allow(clippy::expect_used)]
self.writers.last_mut().expect("should exist")
}

/// Returns the [`ValueHandle`] for the next written blob.
///
/// This can be used to index an item into an external `Index`.
#[must_use]
pub fn get_next_value_handle(&self, key: &[u8]) -> ValueHandle {
ValueHandle {
offset: self.offset(key),
Expand Down
24 changes: 17 additions & 7 deletions tests/basic_gc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use test_log::test;
use value_log::{Config, MockIndex, MockIndexWriter, ValueLog};
use value_log::{Config, IndexWriter, MockIndex, MockIndexWriter, ValueLog};

#[test]
fn basic_gc() -> value_log::Result<()> {
Expand All @@ -13,14 +13,19 @@ fn basic_gc() -> value_log::Result<()> {
{
let items = ["a", "b", "c", "d", "e"];

let index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer(index_writer)?;
let mut index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer()?;

for key in &items {
let value = key.repeat(10_000);
let value = value.as_bytes();

writer.write(key.as_bytes(), value)?;
let key = key.as_bytes();

let handle = writer.get_next_value_handle(key);
index_writer.insert_indirect(key, handle, value.len() as u32)?;

writer.write(key, value)?;
}

value_log.register_writer(writer)?;
Expand All @@ -43,14 +48,19 @@ fn basic_gc() -> value_log::Result<()> {
{
let items = ["a", "b", "c", "d", "e"];

let index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer(index_writer)?;
let mut index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer()?;

for key in &items {
let value = key.repeat(10_000);
let value = value.as_bytes();

writer.write(key.as_bytes(), value)?;
let key = key.as_bytes();

let handle = writer.get_next_value_handle(key);
index_writer.insert_indirect(key, handle, value.len() as u32)?;

writer.write(key, value)?;
}

value_log.register_writer(writer)?;
Expand Down
13 changes: 9 additions & 4 deletions tests/basic_kv.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;
use test_log::test;
use value_log::{Config, KeyRange, MockIndex, MockIndexWriter, ValueLog};
use value_log::{Config, IndexWriter, KeyRange, MockIndex, MockIndexWriter, ValueLog};

#[test]
fn basic_kv() -> value_log::Result<()> {
Expand All @@ -14,14 +14,19 @@ fn basic_kv() -> value_log::Result<()> {
let items = ["a", "b", "c", "d", "e"];

{
let index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer(index_writer)?;
let mut index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer()?;

for key in &items {
let value = key.repeat(10_000);
let value = value.as_bytes();

writer.write(key.as_bytes(), value)?;
let key = key.as_bytes();

let handle = writer.get_next_value_handle(key);
index_writer.insert_indirect(key, handle, value.len() as u32)?;

writer.write(key, value)?;
}

value_log.register_writer(writer)?;
Expand Down
20 changes: 14 additions & 6 deletions tests/gc_space_amp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use test_log::test;
use value_log::{Config, MockIndex, MockIndexWriter, ValueLog};
use value_log::{Config, IndexWriter, MockIndex, MockIndexWriter, ValueLog};

#[test]
fn gc_space_amp_target_1() -> value_log::Result<()> {
Expand All @@ -13,22 +13,30 @@ fn gc_space_amp_target_1() -> value_log::Result<()> {
assert_eq!(0.0, value_log.space_amp());
assert_eq!(0.0, value_log.manifest.stale_ratio());

let key = "key";
let key = b"key";
let value = "value".repeat(20_000);

// NOTE: Write a single item 10x
// -> should result in space amp = 10.0x
for x in 1..=10 {
let index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer(index_writer)?;
let mut index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer()?;

writer.write(key.as_bytes(), value.as_bytes())?;
let handle = writer.get_next_value_handle(key);
index_writer.insert_indirect(key, handle, value.len() as u32)?;

writer.write(key, value.as_bytes())?;

{
let key = format!("key{x}");
let value = "value".repeat(5_000);

writer.write(key.as_bytes(), value.as_bytes())?;
let key = key.as_bytes();

let handle = writer.get_next_value_handle(key);
index_writer.insert_indirect(key, handle, value.len() as u32)?;

writer.write(key, value.as_bytes())?;
}

value_log.register_writer(writer)?;
Expand Down
13 changes: 9 additions & 4 deletions tests/recovery.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use test_log::test;
use value_log::{Config, MockIndex, MockIndexWriter, ValueLog};
use value_log::{Config, IndexWriter, MockIndex, MockIndexWriter, ValueLog};

#[test]
fn basic_recovery() -> value_log::Result<()> {
Expand All @@ -14,14 +14,19 @@ fn basic_recovery() -> value_log::Result<()> {
let value_log = ValueLog::open(vl_path, Config::default())?;

{
let index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer(index_writer)?;
let mut index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer()?;

for key in &items {
let value = key.repeat(10_000);
let value = value.as_bytes();

writer.write(key.as_bytes(), value)?;
let key = key.as_bytes();

let handle = writer.get_next_value_handle(key);
index_writer.insert_indirect(key, handle, value.len() as u32)?;

writer.write(key, value)?;
}

value_log.register_writer(writer)?;
Expand Down
11 changes: 5 additions & 6 deletions tests/rollover_index_fail_finish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ use value_log::{Config, IndexWriter, MockIndex, MockIndexWriter, ValueHandle, Va
pub struct DebugIndexWriter;

impl IndexWriter for DebugIndexWriter {
fn insert_direct(&mut self, _: &[u8], _: &[u8]) -> std::io::Result<()> {
Ok(())
}

fn insert_indirect(&mut self, _: &[u8], _: ValueHandle, _: u32) -> std::io::Result<()> {
Ok(())
}
Expand All @@ -30,13 +26,16 @@ fn rollover_index_fail_finish() -> value_log::Result<()> {
let items = ["a", "b", "c", "d", "e"];

{
let index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer(index_writer)?;
let mut index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer()?;

for key in &items {
let value = key.repeat(10_000);
let value = value.as_bytes();

let handle = writer.get_next_value_handle(key.as_bytes());
index_writer.insert_indirect(key.as_bytes(), handle, value.len() as u32)?;

writer.write(key.as_bytes(), value)?;
}

Expand Down
20 changes: 13 additions & 7 deletions tests/space_amp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use test_log::test;
use value_log::{Config, MockIndex, MockIndexWriter, ValueLog};
use value_log::{Config, IndexWriter, MockIndex, MockIndexWriter, ValueLog};

#[test]
fn worst_case_space_amp() -> value_log::Result<()> {
Expand All @@ -13,16 +13,19 @@ fn worst_case_space_amp() -> value_log::Result<()> {
assert_eq!(0.0, value_log.space_amp());
assert_eq!(0.0, value_log.manifest.stale_ratio());

let key = "key";
let key = b"key";
let value = "value".repeat(5_000);

// NOTE: Write a single item 10x
// -> should result in space amp = 10.0x
for x in 1..=10 {
let index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer(index_writer)?;
let mut index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer()?;

writer.write(key.as_bytes(), value.as_bytes())?;
let handle = writer.get_next_value_handle(key);
index_writer.insert_indirect(key, handle, value.len() as u32)?;

writer.write(key, value.as_bytes())?;

value_log.register_writer(writer)?;

Expand Down Expand Up @@ -55,8 +58,11 @@ fn no_overlap_space_amp() -> value_log::Result<()> {
let key = i.to_string();
let value = "afsasfdfasdfsda".repeat(5_000);

let index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer(index_writer)?;
let mut index_writer = MockIndexWriter(index.clone());
let mut writer = value_log.get_writer()?;

let handle = writer.get_next_value_handle(key.as_bytes());
index_writer.insert_indirect(key.as_bytes(), handle, value.len() as u32)?;

writer.write(key.as_bytes(), value.as_bytes())?;
value_log.register_writer(writer)?;
Expand Down

0 comments on commit ddc7c97

Please sign in to comment.