Skip to content

Commit e9f2c57

Browse files
authored
feat: introduce estimated size filter (#1089)
1 parent 5797044 commit e9f2c57

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

foyer-storage/src/filter.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::{fmt::Debug, sync::Arc, time::Duration};
1717
use crate::io::device::statistics::Statistics;
1818

1919
/// Filter result for admission pickers and reinsertion pickers.
20-
#[derive(Debug, Clone, Copy)]
20+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2121
pub enum FilterResult {
2222
/// Admittion.
2323
Admit,
@@ -86,6 +86,8 @@ impl Filter {
8686

8787
pub mod conditions {
8888

89+
use std::ops::{Bound, Range, RangeBounds};
90+
8991
pub use super::*;
9092

9193
/// Admit all entries.
@@ -121,4 +123,52 @@ pub mod conditions {
121123
}
122124
}
123125
}
126+
127+
/// A condition that checks if the estimated size is within a specified range.
128+
#[derive(Debug)]
129+
pub struct EstimatedSize {
130+
range: Range<usize>,
131+
}
132+
133+
impl EstimatedSize {
134+
/// Create a new `EstimatedSize` condition with a specified range.
135+
pub fn new<R: RangeBounds<usize>>(range: R) -> Self {
136+
let start = match range.start_bound() {
137+
Bound::Included(v) => *v,
138+
Bound::Excluded(v) => *v + 1,
139+
Bound::Unbounded => 0,
140+
};
141+
let end = match range.end_bound() {
142+
Bound::Included(v) => *v + 1,
143+
Bound::Excluded(v) => *v,
144+
Bound::Unbounded => usize::MAX,
145+
};
146+
Self { range: start..end }
147+
}
148+
}
149+
150+
impl FilterCondition for EstimatedSize {
151+
fn filter(&self, _: &Arc<Statistics>, _: u64, estimated_size: usize) -> FilterResult {
152+
if self.range.contains(&estimated_size) {
153+
FilterResult::Admit
154+
} else {
155+
FilterResult::Reject
156+
}
157+
}
158+
}
159+
}
160+
161+
#[cfg(test)]
162+
mod tests {
163+
use super::conditions::*;
164+
use crate::Throttle;
165+
166+
#[test]
167+
fn test_estimated_size_condition() {
168+
let condition = EstimatedSize::new(10..20);
169+
let statistics = Arc::new(Statistics::new(Throttle::default()));
170+
assert_eq!(condition.filter(&statistics, 0, 15), FilterResult::Admit);
171+
assert_eq!(condition.filter(&statistics, 0, 5), FilterResult::Reject);
172+
assert_eq!(condition.filter(&statistics, 0, 20), FilterResult::Reject);
173+
}
124174
}

foyer-storage/src/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use crate::{
2626
},
2727
error::{Error, Result},
2828
filter::{
29-
conditions::{AdmitAll, RejectAll},
29+
conditions::{AdmitAll, EstimatedSize, RejectAll},
3030
Filter, FilterCondition, FilterResult,
3131
},
3232
io::{

foyer/src/prelude.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ pub use crate::{
3636
},
3737
storage::{
3838
AdmitAll, Block, BlockEngineBuilder, BlockStatistics, CombinedDeviceBuilder, Compression, Device,
39-
DeviceBuilder, Engine, EngineBuildContext, EngineConfig, EvictionInfo, EvictionPicker, FifoPicker,
40-
FileDeviceBuilder, Filter, FilterCondition, FilterResult, FsDeviceBuilder, InvalidRatioPicker, IoEngine,
41-
IoEngineBuilder, IoError, IoHandle, IoResult, IopsCounter, Load, NoopDeviceBuilder, NoopIoEngine,
39+
DeviceBuilder, Engine, EngineBuildContext, EngineConfig, EstimatedSize, EvictionInfo, EvictionPicker,
40+
FifoPicker, FileDeviceBuilder, Filter, FilterCondition, FilterResult, FsDeviceBuilder, InvalidRatioPicker,
41+
IoEngine, IoEngineBuilder, IoError, IoHandle, IoResult, IopsCounter, Load, NoopDeviceBuilder, NoopIoEngine,
4242
NoopIoEngineBuilder, PartialDeviceBuilder, PsyncIoEngine, PsyncIoEngineBuilder, RawFile, RecoverMode,
4343
RejectAll, Runtime, RuntimeOptions, Statistics, Store, StoreBuilder, Throttle, TokioRuntimeOptions,
4444
},

0 commit comments

Comments
 (0)