Skip to content
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

refactor: remove reduant boxing in trigger #253

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/compaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ pub(crate) mod tests {
where
R: Record + Send,
{
let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);

let mutable: Mutable<R> = Mutable::new(option, trigger, fs, schema.clone()).await?;

Expand Down
12 changes: 6 additions & 6 deletions src/inmem/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ where
{
pub(crate) data: SkipMap<Timestamped<<R::Schema as Schema>::Key>, Option<R>>,
wal: Option<Mutex<WalFile<Box<dyn DynWrite>, R>>>,
pub(crate) trigger: Arc<Box<dyn Trigger<R> + Send + Sync>>,
pub(crate) trigger: Arc<dyn Trigger<R>>,

pub(super) schema: Arc<R::Schema>,
}
Expand All @@ -48,7 +48,7 @@ where
{
pub async fn new(
option: &DbOption,
trigger: Arc<Box<dyn Trigger<R> + Send + Sync>>,
trigger: Arc<dyn Trigger<R>>,
fs: &Arc<dyn DynFs>,
schema: Arc<R::Schema>,
) -> Result<Self, fusio::Error> {
Expand Down Expand Up @@ -119,7 +119,7 @@ where
.map_err(|e| DbError::WalWrite(Box::new(e)))?;
}

let is_exceeded = self.trigger.item(&value);
let is_exceeded = self.trigger.check_if_exceed(&value);
self.data.insert(timestamped_key, value);

Ok(is_exceeded)
Expand Down Expand Up @@ -240,7 +240,7 @@ mod tests {
);
fs.create_dir_all(&option.wal_dir_path()).await.unwrap();

let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);
let mem_table = Mutable::<Test>::new(&option, trigger, &fs, Arc::new(TestSchema {}))
.await
.unwrap();
Expand Down Expand Up @@ -293,7 +293,7 @@ mod tests {
);
fs.create_dir_all(&option.wal_dir_path()).await.unwrap();

let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);

let mutable = Mutable::<String>::new(&option, trigger, &fs, Arc::new(StringSchema))
.await
Expand Down Expand Up @@ -389,7 +389,7 @@ mod tests {
let fs = Arc::new(TokioFs) as Arc<dyn DynFs>;
fs.create_dir_all(&option.wal_dir_path()).await.unwrap();

let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);

let schema = Arc::new(schema);

Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ where
pub immutables: Vec<(Option<FileId>, Immutable<<R::Schema as Schema>::Columns>)>,
compaction_tx: Sender<CompactTask>,
recover_wal_ids: Option<Vec<FileId>>,
trigger: Arc<Box<dyn Trigger<R> + Send + Sync>>,
trigger: Arc<dyn Trigger<R>>,
record_schema: Arc<R::Schema>,
}

Expand All @@ -487,7 +487,7 @@ where
record_schema: Arc<R::Schema>,
manager: &StoreManager,
) -> Result<Self, DbError<R>> {
let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);
let mut schema = DbStorage {
mutable: Mutable::new(
&option,
Expand Down Expand Up @@ -1169,7 +1169,7 @@ pub(crate) mod tests {
option: Arc<DbOption>,
fs: &Arc<dyn DynFs>,
) -> Result<(crate::DbStorage<Test>, Receiver<CompactTask>), fusio::Error> {
let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);

let mutable = Mutable::new(&option, trigger.clone(), fs, Arc::new(TestSchema {})).await?;

Expand Down Expand Up @@ -1211,7 +1211,7 @@ pub(crate) mod tests {
.unwrap();

let immutables = {
let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);

let mutable: Mutable<Test> =
Mutable::new(&option, trigger.clone(), fs, Arc::new(TestSchema)).await?;
Expand Down Expand Up @@ -1648,7 +1648,7 @@ pub(crate) mod tests {

let (task_tx, _task_rx) = bounded(1);

let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);
let schema: crate::DbStorage<Test> = crate::DbStorage {
mutable: Mutable::new(&option, trigger.clone(), &fs, Arc::new(TestSchema))
.await
Expand Down Expand Up @@ -1718,7 +1718,7 @@ pub(crate) mod tests {

let (task_tx, _task_rx) = bounded(1);

let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);
let schema: crate::DbStorage<DynRecord> = crate::DbStorage {
mutable: Mutable::new(
&option,
Expand Down
2 changes: 1 addition & 1 deletion src/stream/mem_projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ mod tests {

fs.create_dir_all(&option.wal_dir_path()).await.unwrap();

let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);

let mutable = Mutable::<Test>::new(&option, trigger, &fs, Arc::new(TestSchema {}))
.await
Expand Down
10 changes: 5 additions & 5 deletions src/stream/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod tests {

fs.create_dir_all(&option.wal_dir_path()).await.unwrap();

let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);

let m1 = Mutable::<String>::new(&option, trigger, &fs, Arc::new(StringSchema))
.await
Expand All @@ -194,7 +194,7 @@ mod tests {
.await
.unwrap();

let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);

let m2 = Mutable::<String>::new(&option, trigger, &fs, Arc::new(StringSchema))
.await
Expand All @@ -209,7 +209,7 @@ mod tests {
.await
.unwrap();

let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);

let m3 = Mutable::<String>::new(&option, trigger, &fs, Arc::new(StringSchema))
.await
Expand Down Expand Up @@ -281,7 +281,7 @@ mod tests {

fs.create_dir_all(&option.wal_dir_path()).await.unwrap();

let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);

let m1 = Mutable::<String>::new(&option, trigger, &fs, Arc::new(StringSchema))
.await
Expand Down Expand Up @@ -372,7 +372,7 @@ mod tests {

fs.create_dir_all(&option.wal_dir_path()).await.unwrap();

let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);

let m1 = Mutable::<String>::new(&option, trigger, &fs, Arc::new(StringSchema))
.await
Expand Down
2 changes: 1 addition & 1 deletion src/stream/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ mod tests {

fs.create_dir_all(&option.wal_dir_path()).await.unwrap();

let trigger = Arc::new(TriggerFactory::create(option.trigger_type));
let trigger = TriggerFactory::create(option.trigger_type);

let m1 = Mutable::<Test>::new(&option, trigger, &fs, Arc::new(TestSchema {}))
.await
Expand Down
46 changes: 24 additions & 22 deletions src/trigger.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use std::{
fmt,
fmt::Debug,
marker::PhantomData,
sync::atomic::{AtomicUsize, Ordering},
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};

use crate::record::Record;

pub trait Trigger<R: Record>: fmt::Debug {
fn item(&self, item: &Option<R>) -> bool;
pub trait Trigger<R: Record>: Send + Sync {
fn check_if_exceed(&self, item: &Option<R>) -> bool;

fn reset(&self);
}

#[derive(Debug)]
pub struct SizeOfMemTrigger<R> {
threshold: usize,
Expand All @@ -30,7 +32,7 @@ impl<T> SizeOfMemTrigger<T> {
}

impl<R: Record> Trigger<R> for SizeOfMemTrigger<R> {
fn item(&self, item: &Option<R>) -> bool {
fn check_if_exceed(&self, item: &Option<R>) -> bool {
let size = item.as_ref().map_or(0, R::size);
self.current_size.fetch_add(size, Ordering::SeqCst) + size >= self.threshold
}
Expand Down Expand Up @@ -58,7 +60,7 @@ impl<T> LengthTrigger<T> {
}

impl<R: Record> Trigger<R> for LengthTrigger<R> {
fn item(&self, _: &Option<R>) -> bool {
fn check_if_exceed(&self, _: &Option<R>) -> bool {
self.count.fetch_add(1, Ordering::SeqCst) + 1 >= self.threshold
}

Expand All @@ -78,10 +80,10 @@ pub(crate) struct TriggerFactory<R> {
}

impl<R: Record> TriggerFactory<R> {
pub fn create(trigger_type: TriggerType) -> Box<dyn Trigger<R> + Send + Sync> {
pub fn create(trigger_type: TriggerType) -> Arc<dyn Trigger<R>> {
match trigger_type {
TriggerType::SizeOfMem(threshold) => Box::new(SizeOfMemTrigger::new(threshold)),
TriggerType::Length(threshold) => Box::new(LengthTrigger::new(threshold)),
TriggerType::SizeOfMem(threshold) => Arc::new(SizeOfMemTrigger::new(threshold)),
TriggerType::Length(threshold) => Arc::new(LengthTrigger::new(threshold)),
}
}
}
Expand All @@ -106,19 +108,19 @@ mod tests {
assert_eq!(record_size, 8);

assert!(
!trigger.item(&record),
!trigger.check_if_exceed(&record),
"Trigger should not be exceeded after 1 record"
);

trigger.item(&record);
trigger.check_if_exceed(&record);
assert!(
trigger.item(&record),
trigger.check_if_exceed(&record),
"Trigger should be exceeded after 2 records"
);

trigger.reset();
assert!(
!trigger.item(&record),
!trigger.check_if_exceed(&record),
"Trigger should not be exceeded after reset"
);
}
Expand All @@ -135,19 +137,19 @@ mod tests {
});

assert!(
!trigger.item(&record),
!trigger.check_if_exceed(&record),
"Trigger should not be exceeded after 1 record"
);

trigger.item(&record);
trigger.check_if_exceed(&record);
assert!(
trigger.item(&record),
trigger.check_if_exceed(&record),
"Trigger should be exceeded after 2 records"
);

trigger.reset();
assert!(
!trigger.item(&record),
!trigger.check_if_exceed(&record),
"Trigger should not be exceeded after reset"
);
}
Expand All @@ -156,23 +158,23 @@ mod tests {
let size_of_mem_trigger = TriggerFactory::<Test>::create(TriggerType::SizeOfMem(9));
let length_trigger = TriggerFactory::<Test>::create(TriggerType::Length(2));

assert!(!size_of_mem_trigger.item(&Some(Test {
assert!(!size_of_mem_trigger.check_if_exceed(&Some(Test {
vstring: "test".to_string(),
vu32: 0,
vbool: None
})));
assert!(size_of_mem_trigger.item(&Some(Test {
assert!(size_of_mem_trigger.check_if_exceed(&Some(Test {
vstring: "test".to_string(),
vu32: 0,
vbool: None
})));

assert!(!length_trigger.item(&Some(Test {
assert!(!length_trigger.check_if_exceed(&Some(Test {
vstring: "test".to_string(),
vu32: 1,
vbool: Some(true)
})));
assert!(length_trigger.item(&Some(Test {
assert!(length_trigger.check_if_exceed(&Some(Test {
vstring: "test".to_string(),
vu32: 1,
vbool: Some(true)
Expand Down
Loading