File tree Expand file tree Collapse file tree
libsql-wal/src/swap_strategy Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments