@@ -285,23 +285,42 @@ impl<F> CurrentSegment<F> {
285
285
286
286
if let Some ( size_after) = size_after {
287
287
if tx. not_empty ( ) {
288
- if let Some ( offset) = tx. recompute_checksum {
289
- self . recompute_checksum ( offset, tx. next_offset - 1 ) ?;
288
+ let new_checksum = if let Some ( offset) = tx. recompute_checksum {
289
+ self . recompute_checksum ( offset, tx. next_offset - 1 ) ?
290
+ } else {
291
+ tx. current_checksum ( )
292
+ } ;
293
+
294
+ #[ cfg( debug_assertions) ]
295
+ {
296
+ // ensure that file checksum for that transaction is valid
297
+ let from = {
298
+ let header = self . header . lock ( ) ;
299
+ if header. last_commited_frame_no ( ) == 0 {
300
+ 0
301
+ } else {
302
+ ( header. last_commited_frame_no ( ) - header. start_frame_no . get ( ) ) as u32
303
+ }
304
+ } ;
305
+
306
+ self . assert_valid_checksum ( from, tx. next_offset - 1 ) ?;
290
307
}
308
+
291
309
let last_frame_no = tx. next_frame_no - 1 ;
292
310
let mut header = { * self . header . lock ( ) } ;
293
311
header. last_commited_frame_no = last_frame_no. into ( ) ;
294
312
header. size_after = size_after. into ( ) ;
295
313
header. recompute_checksum ( ) ;
296
314
297
315
self . file . write_all_at ( header. as_bytes ( ) , 0 ) ?;
316
+ // todo: sync if sync mode is EXTRA
298
317
// self.file.sync_data().unwrap();
299
318
tx. merge_savepoints ( & self . index ) ;
300
319
// set the header last, so that a transaction does not witness a write before
301
320
// it's actually committed.
302
321
* self . header . lock ( ) = header;
303
322
self . current_checksum
304
- . store ( tx . current_checksum ( ) , Ordering :: Relaxed ) ;
323
+ . store ( new_checksum , Ordering :: Relaxed ) ;
305
324
306
325
tx. is_commited = true ;
307
326
@@ -343,7 +362,7 @@ impl<F> CurrentSegment<F> {
343
362
F : FileExt ,
344
363
B : IoBufMut + Send + ' static ,
345
364
{
346
- let byte_offset = dbg ! ( frame_offset( dbg! ( offset) ) ) ;
365
+ let byte_offset = frame_offset ( offset) ;
347
366
self . file . read_exact_at_async ( buf, byte_offset) . await
348
367
}
349
368
@@ -501,14 +520,14 @@ impl<F> CurrentSegment<F> {
501
520
( stream, replicated_until, db_size)
502
521
}
503
522
504
- fn recompute_checksum ( & self , start_offset : u32 , until_offset : u32 ) -> Result < ( ) >
523
+ fn recompute_checksum ( & self , start_offset : u32 , until_offset : u32 ) -> Result < u32 >
505
524
where F : FileExt
506
525
{
507
526
let mut current_checksum = if start_offset == 0 {
508
527
self . header . lock ( ) . salt . get ( )
509
528
} else {
510
529
// we get the checksum from the frame just before the the start offset
511
- let frame_offset = checked_frame_offset ( start_offset - 1 ) ;
530
+ let frame_offset = checked_frame_offset ( ( start_offset - 1 ) ) ;
512
531
let mut out = U32 :: new ( 0 ) ;
513
532
self . file . read_exact_at ( out. as_bytes_mut ( ) , frame_offset) ?;
514
533
out. get ( )
@@ -522,6 +541,31 @@ impl<F> CurrentSegment<F> {
522
541
self . file . write_all_at ( & current_checksum. to_le_bytes ( ) , frame_offset) ?;
523
542
}
524
543
544
+ Ok ( current_checksum)
545
+ }
546
+
547
+ /// test fuction to ensure checksum integrity
548
+ #[ cfg( debug_assertions) ]
549
+ #[ track_caller]
550
+ fn assert_valid_checksum ( & self , from : u32 , until : u32 ) -> Result < ( ) >
551
+ where F : FileExt
552
+ {
553
+ let mut frame: Box < CheckedFrame > = CheckedFrame :: new_box_zeroed ( ) ;
554
+ let mut current_checksum = if from != 0 {
555
+ let offset = checked_frame_offset ( from - 1 ) ;
556
+ self . file . read_exact_at ( frame. as_bytes_mut ( ) , offset) ?;
557
+ frame. checksum . get ( )
558
+ } else {
559
+ self . header . lock ( ) . salt . get ( )
560
+ } ;
561
+
562
+ for i in from..=until {
563
+ let offset = checked_frame_offset ( i) ;
564
+ self . file . read_exact_at ( frame. as_bytes_mut ( ) , offset) ?;
565
+ current_checksum = frame. frame . checksum ( current_checksum) ;
566
+ assert_eq ! ( current_checksum, frame. checksum. get( ) , "invalid checksum at offset {i}" ) ;
567
+ }
568
+
525
569
Ok ( ( ) )
526
570
}
527
571
}
@@ -915,7 +959,7 @@ mod test {
915
959
let mut inner = self . inner . lock ( ) ;
916
960
if !inner. contains_key ( path) {
917
961
let data = if path. exists ( ) {
918
- std:: fs:: read ( path) . map_err ( |e| dbg ! ( e ) ) ?
962
+ std:: fs:: read ( path) ?
919
963
} else {
920
964
vec ! [ ]
921
965
} ;
0 commit comments