@@ -17,60 +17,48 @@ impl OperationLogs {
1717 pub fn new_ondisk ( ) -> Self {
1818 Self :: OnDisk ( FileOpLogs :: new ( ENV . dir . clone ( ) ) . unwrap ( ) )
1919 }
20- /// Append one or more `WriteOperation`s to the log.
21- pub ( crate ) fn write_many ( & mut self , ops : Vec < WriteOperation > ) -> anyhow:: Result < ( ) > {
20+ fn get_mut ( & mut self ) -> & mut dyn TWriteAheadLog {
21+ match self {
22+ OperationLogs :: Memory ( m) => m as & mut dyn TWriteAheadLog ,
23+ OperationLogs :: OnDisk ( f) => f as & mut dyn TWriteAheadLog ,
24+ }
25+ }
26+ fn get ( & self ) -> & dyn TWriteAheadLog {
2227 match self {
23- OperationLogs :: Memory ( memory_op_logs ) => memory_op_logs . write_many ( ops ) ,
24- OperationLogs :: OnDisk ( file_op_logs ) => file_op_logs . write_many ( ops ) ,
28+ OperationLogs :: Memory ( m ) => m as & dyn TWriteAheadLog ,
29+ OperationLogs :: OnDisk ( f ) => f as & dyn TWriteAheadLog ,
2530 }
2631 }
2732
33+ /// Append one or more `WriteOperation`s to the log.
34+ pub ( crate ) fn write_many ( & mut self , ops : Vec < WriteOperation > ) -> anyhow:: Result < ( ) > {
35+ self . get_mut ( ) . write_many ( ops)
36+ }
37+
2838 /// Retrieve logs that fall between the current 'commit' index and target 'log' index.
2939 /// This is NOT async as it is expected to be infallible and an in-memory operation.
3040 pub ( crate ) fn range ( & self , start_exclusive : u64 , end_inclusive : u64 ) -> Vec < WriteOperation > {
31- match self {
32- OperationLogs :: Memory ( memory_op_logs) => {
33- memory_op_logs. range ( start_exclusive, end_inclusive)
34- } ,
35- OperationLogs :: OnDisk ( file_op_logs) => {
36- file_op_logs. range ( start_exclusive, end_inclusive)
37- } ,
38- }
41+ self . get ( ) . range ( start_exclusive, end_inclusive)
3942 }
4043
4144 /// Replays all logged operations from the beginning of the WAL, calling the provided callback `f` for each operation.
4245 /// The callback `f(WriteOperation)` receives each operation in the order it was appended.
43- pub ( crate ) fn replay < F > ( & mut self , f : F ) -> anyhow:: Result < ( ) >
44- where
45- F : FnMut ( WriteOperation ) + Send ,
46- {
47- match self {
48- OperationLogs :: Memory ( memory_op_logs) => memory_op_logs. replay ( f) ,
49- OperationLogs :: OnDisk ( file_op_logs) => file_op_logs. replay ( f) ,
50- }
46+ pub ( crate ) fn replay ( & mut self , f : & mut dyn FnMut ( WriteOperation ) ) -> anyhow:: Result < ( ) > {
47+ self . get_mut ( ) . replay ( f)
5148 }
5249
5350 /// Retrieves the log at a given index.
5451 pub ( crate ) fn read_at ( & mut self , at : u64 ) -> Option < WriteOperation > {
55- match self {
56- OperationLogs :: Memory ( memory_op_logs) => memory_op_logs. read_at ( at) ,
57- OperationLogs :: OnDisk ( file_op_logs) => file_op_logs. read_at ( at) ,
58- }
52+ self . get_mut ( ) . read_at ( at)
5953 }
6054
6155 /// Returns true if there are no logs. Otherwise, returns false.
6256 pub ( crate ) fn is_empty ( & self ) -> bool {
63- match self {
64- OperationLogs :: Memory ( memory_op_logs) => memory_op_logs. is_empty ( ) ,
65- OperationLogs :: OnDisk ( file_op_logs) => file_op_logs. is_empty ( ) ,
66- }
57+ self . get ( ) . is_empty ( )
6758 }
6859
6960 /// Truncate logs that are positioned after `log_index`.
7061 pub ( crate ) fn truncate_after ( & mut self , log_index : u64 ) {
71- match self {
72- OperationLogs :: Memory ( memory_op_logs) => memory_op_logs. truncate_after ( log_index) ,
73- OperationLogs :: OnDisk ( file_op_logs) => file_op_logs. truncate_after ( log_index) ,
74- }
62+ self . get_mut ( ) . truncate_after ( log_index)
7563 }
7664}
0 commit comments