@@ -18,7 +18,7 @@ pub struct FileOpLogs {
1818 segments : Vec < Segment > ,
1919}
2020
21- #[ derive( Clone , Debug ) ]
21+ #[ derive( Debug ) ]
2222struct Segment {
2323 path : PathBuf ,
2424 start_index : u64 ,
@@ -43,25 +43,24 @@ impl LookupIndex {
4343}
4444
4545impl Segment {
46- fn new ( path : PathBuf ) -> Self {
47- let file = OpenOptions :: new ( )
46+ fn new ( path : PathBuf , start_index : u64 , end_index : u64 ) -> Self {
47+ let _ = OpenOptions :: new ( )
4848 . create ( true )
4949 . append ( true )
5050 . read ( true )
5151 . open ( & path)
5252 . context ( format ! ( "Failed to create initial segment '{}'" , path. display( ) ) )
5353 . unwrap ( ) ;
54- file. lock ( ) . unwrap ( ) ;
5554
56- Self { path, start_index : 0 , end_index : 0 , size : 0 , lookups : Vec :: new ( ) }
55+ Self { path, start_index, end_index, size : 0 , lookups : Vec :: new ( ) }
5756 }
5857
5958 fn read_operations ( & self ) -> Result < Vec < WriteOperation > > {
6059 let file = OpenOptions :: new ( )
6160 . read ( true )
6261 . open ( & self . path )
6362 . context ( format ! ( "Failed to open segment for reading: {}" , self . path. display( ) ) ) ?;
64- file . lock ( ) . unwrap ( ) ;
63+
6564 let mut reader = BufReader :: new ( file) ;
6665 let mut buf = Vec :: new ( ) ;
6766 reader. read_to_end ( & mut buf) ?;
@@ -228,7 +227,7 @@ impl FileOpLogs {
228227 let active_segment = if segment_paths. is_empty ( ) {
229228 // No segments exist — create initial segment
230229 let segment_path = path. join ( "segment_0.oplog" ) ;
231- Segment :: new ( segment_path)
230+ Segment :: new ( segment_path, 0 , 0 )
232231 } else {
233232 // Segments exist — use the last one as active
234233 Segment :: from_path ( segment_paths. last ( ) . unwrap ( ) ) ?
@@ -243,21 +242,19 @@ impl FileOpLogs {
243242 writer. get_mut ( ) . sync_all ( ) ?;
244243 }
245244
246- // Add to segments list
247- self . segments . push ( self . active_segment . clone ( ) ) ;
245+ let next_index = self . segments . len ( ) + 1 ;
246+ let segment_path = self . path . join ( format ! ( "segment_{next_index}.oplog" ) ) ;
248247
249248 // Create new segment
250- let next_index = self . segments . len ( ) ;
251- let segment_path = self . path . join ( format ! ( "segment_{next_index}.oplog" ) ) ;
252- let _ = OpenOptions :: new ( ) . create ( true ) . append ( true ) . read ( true ) . open ( & segment_path) ?;
249+ let mut seg = Segment :: new (
250+ segment_path,
251+ self . active_segment . end_index + 1 ,
252+ self . active_segment . end_index ,
253+ ) ;
253254
254- self . active_segment = Segment {
255- path : segment_path,
256- start_index : self . active_segment . end_index + 1 ,
257- end_index : self . active_segment . end_index ,
258- size : 0 ,
259- lookups : Vec :: new ( ) ,
260- } ;
255+ std:: mem:: swap ( & mut seg, & mut self . active_segment ) ;
256+
257+ self . segments . push ( seg) ;
261258
262259 Ok ( ( ) )
263260 }
0 commit comments