Skip to content

Commit a04c71c

Browse files
committed
introduce duration based strategy
1 parent d77cca2 commit a04c71c

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::time::{Duration, Instant};
2+
3+
use parking_lot::Mutex;
4+
5+
use super::SwapStrategy;
6+
7+
/// A wal swap strategy that swaps the current wal if it's older that some duration
8+
pub struct DurationSwapStrategy {
9+
swap_after: Duration,
10+
last_swapped_at: Mutex<Instant>,
11+
}
12+
13+
impl DurationSwapStrategy {
14+
pub fn new(swap_after: Duration) -> Self {
15+
Self { swap_after, last_swapped_at: Mutex::new(Instant::now()) }
16+
}
17+
}
18+
19+
impl SwapStrategy for DurationSwapStrategy {
20+
#[inline(always)]
21+
fn should_swap(&self, _frames_in_wal: usize) -> bool {
22+
let last_swapped_at = self.last_swapped_at.lock();
23+
last_swapped_at.elapsed() >= self.swap_after
24+
}
25+
26+
#[inline(always)]
27+
fn swapped(&self) {
28+
*self.last_swapped_at.lock() = Instant::now();
29+
}
30+
}

0 commit comments

Comments
 (0)