@@ -3,7 +3,7 @@ use std::num::NonZeroU64;
33use std:: path:: { Path , PathBuf } ;
44use std:: sync:: atomic:: { AtomicBool , Ordering } ;
55use std:: sync:: Arc ;
6- use std:: time:: Instant ;
6+ use std:: time:: { Duration , Instant } ;
77
88use dashmap:: DashMap ;
99use libsql_sys:: ffi:: Sqlite3DbHeader ;
@@ -25,6 +25,9 @@ use crate::replication::storage::{ReplicateFromStorage as _, StorageReplicator};
2525use crate :: segment:: list:: SegmentList ;
2626use crate :: segment:: Segment ;
2727use crate :: segment:: { current:: CurrentSegment , sealed:: SealedSegment } ;
28+ use crate :: segment_swap_strategy:: duration:: DurationSwapStrategy ;
29+ use crate :: segment_swap_strategy:: frame_count:: FrameCountSwapStrategy ;
30+ use crate :: segment_swap_strategy:: SegmentSwapStrategy ;
2831use crate :: shared_wal:: { SharedWal , SwapLog } ;
2932use crate :: storage:: { OnStoreCallback , Storage } ;
3033use crate :: transaction:: TxGuard ;
@@ -337,6 +340,17 @@ where
337340
338341 let ( new_frame_notifier, _) = tokio:: sync:: watch:: channel ( next_frame_no. get ( ) - 1 ) ;
339342
343+ // FIXME: make swap strategy configurable
344+ // This strategy will perform a swap if either the wal is bigger than 20k frames, or older
345+ // than 10 minutes, or if the frame count is greater than a 1000 and the wal was last
346+ // swapped more than 30 secs ago
347+ let swap_strategy = Box :: new (
348+ DurationSwapStrategy :: new ( Duration :: from_secs ( 5 * 60 ) )
349+ . or ( FrameCountSwapStrategy :: new ( 20_000 ) )
350+ . or ( FrameCountSwapStrategy :: new ( 1000 )
351+ . and ( DurationSwapStrategy :: new ( Duration :: from_secs ( 30 ) ) ) ) ,
352+ ) ;
353+
340354 let shared = Arc :: new ( SharedWal {
341355 current,
342356 wal_lock : Default :: default ( ) ,
@@ -352,8 +366,8 @@ where
352366 ) ) ,
353367 shutdown : false . into ( ) ,
354368 checkpoint_notifier : self . checkpoint_notifier . clone ( ) ,
355- max_segment_size : 1000 . into ( ) ,
356369 io : self . io . clone ( ) ,
370+ swap_strategy,
357371 } ) ;
358372
359373 self . opened
0 commit comments