use crate::index::index_writer_config::IndexWriterConfig;
pub trait FlushPolicy: Send + Sync {
fn should_flush(
&self,
num_docs: i32,
ram_bytes_used: usize,
config: &IndexWriterConfig,
) -> bool;
}
pub struct FlushByRamOrCountsPolicy;
impl FlushPolicy for FlushByRamOrCountsPolicy {
fn should_flush(
&self,
num_docs: i32,
ram_bytes_used: usize,
config: &IndexWriterConfig,
) -> bool {
let max_docs = config.max_buffered_docs();
if max_docs > 0 && num_docs >= max_docs {
return true;
}
let ram_limit = config.ram_buffer_size_bytes();
if ram_limit > 0 && ram_bytes_used >= ram_limit {
return true;
}
false
}
}
#[cfg(test)]
mod tests {
use super::*;
struct DocCountFlushPolicy;
impl FlushPolicy for DocCountFlushPolicy {
fn should_flush(
&self,
num_docs: i32,
_ram_bytes_used: usize,
config: &IndexWriterConfig,
) -> bool {
let max = config.max_buffered_docs();
max > 0 && num_docs >= max
}
}
#[test]
fn test_doc_count_flush_disabled() {
let policy = DocCountFlushPolicy;
let config = IndexWriterConfig::new(); assert!(!policy.should_flush(100, 0, &config));
assert!(!policy.should_flush(0, 0, &config));
}
#[test]
fn test_doc_count_flush_triggers() {
let policy = DocCountFlushPolicy;
let config = IndexWriterConfig::new().set_max_buffered_docs(10);
assert!(!policy.should_flush(9, 0, &config));
assert!(policy.should_flush(10, 0, &config));
assert!(policy.should_flush(11, 0, &config));
}
#[test]
fn test_doc_count_flush_threshold_one() {
let policy = DocCountFlushPolicy;
let config = IndexWriterConfig::new().set_max_buffered_docs(1);
assert!(policy.should_flush(1, 0, &config));
}
#[test]
fn test_ram_or_counts_doc_count_trigger() {
let policy = FlushByRamOrCountsPolicy;
let config = IndexWriterConfig::new()
.set_max_buffered_docs(10)
.set_ram_buffer_size_mb(0.0); assert!(!policy.should_flush(9, 0, &config));
assert!(policy.should_flush(10, 0, &config));
}
#[test]
fn test_ram_or_counts_ram_trigger() {
let policy = FlushByRamOrCountsPolicy;
let config = IndexWriterConfig::new(); let limit = config.ram_buffer_size_bytes();
assert!(!policy.should_flush(100, limit - 1, &config));
assert!(policy.should_flush(100, limit, &config));
assert!(policy.should_flush(100, limit + 1, &config));
}
#[test]
fn test_ram_or_counts_both_disabled() {
let policy = FlushByRamOrCountsPolicy;
let config = IndexWriterConfig::new()
.set_max_buffered_docs(-1)
.set_ram_buffer_size_mb(0.0);
assert!(!policy.should_flush(10_000, 100_000_000, &config));
}
#[test]
fn test_ram_or_counts_either_triggers() {
let policy = FlushByRamOrCountsPolicy;
let config = IndexWriterConfig::new()
.set_max_buffered_docs(5)
.set_ram_buffer_size_mb(1.0);
assert!(policy.should_flush(5, 100, &config));
assert!(policy.should_flush(1, 2 * 1024 * 1024, &config));
assert!(!policy.should_flush(4, 100, &config));
}
}