@@ -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 ;
@@ -27,6 +27,9 @@ use crate::segment::Segment;
2727use crate :: segment:: { current:: CurrentSegment , sealed:: SealedSegment } ;
2828use crate :: shared_wal:: { SharedWal , SwapLog } ;
2929use crate :: storage:: { OnStoreCallback , Storage } ;
30+ use crate :: swap_strategy:: duration:: DurationSwapStrategy ;
31+ use crate :: swap_strategy:: frame_count:: FrameCountSwapStrategy ;
32+ use crate :: swap_strategy:: SwapStrategy ;
3033use crate :: transaction:: TxGuard ;
3134use crate :: { LibsqlFooter , LIBSQL_PAGE_SIZE } ;
3235use libsql_sys:: name:: NamespaceName ;
@@ -332,6 +335,17 @@ where
332335
333336 let ( new_frame_notifier, _) = tokio:: sync:: watch:: channel ( next_frame_no. get ( ) - 1 ) ;
334337
338+ // FIXME: make swap strategy configurable
339+ // This strategy will perform a swap if either the wal is bigger than 20k frames, or older
340+ // than 10 minutes, or if the frame count is greater than a 1000 and the wal was last
341+ // swapped more than 30 secs ago
342+ let swap_strategy = Box :: new (
343+ DurationSwapStrategy :: new ( Duration :: from_secs ( 5 * 60 ) )
344+ . or ( FrameCountSwapStrategy :: new ( 20_000 ) )
345+ . or ( FrameCountSwapStrategy :: new ( 1000 )
346+ . and ( DurationSwapStrategy :: new ( Duration :: from_secs ( 30 ) ) ) ) ,
347+ ) ;
348+
335349 let shared = Arc :: new ( SharedWal {
336350 current,
337351 wal_lock : Default :: default ( ) ,
@@ -347,8 +361,8 @@ where
347361 ) ) ,
348362 shutdown : false . into ( ) ,
349363 checkpoint_notifier : self . checkpoint_notifier . clone ( ) ,
350- max_segment_size : 1000 . into ( ) ,
351364 io : self . io . clone ( ) ,
365+ swap_strategy,
352366 } ) ;
353367
354368 self . opened
0 commit comments