@@ -17,7 +17,7 @@ use std::{fmt::Debug, sync::Arc, time::Duration};
1717use 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 ) ]
2121pub enum FilterResult {
2222 /// Admittion.
2323 Admit ,
@@ -86,6 +86,8 @@ impl Filter {
8686
8787pub 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}
0 commit comments