@@ -477,45 +477,65 @@ void snapshotRestoreIndex(final TestType testType) throws IOException {
477
477
final Path originalDbPath = testDirectory .resolve ("merkledb-snapshotRestoreIndex-" + testType );
478
478
final KeySerializer keySerializer = testType .dataType ().getKeySerializer ();
479
479
final ValueSerializer valueSerializer = testType .dataType ().getValueSerializer ();
480
- createAndApplyDataSource (originalDbPath , tableName , testType , count , 0 , dataSource -> {
481
- final int tableId = dataSource .getTableId ();
482
- // create some leaves
483
- dataSource .saveRecords (
484
- count ,
485
- count * 2 ,
486
- IntStream .range (0 , count * 2 ).mapToObj (i -> createVirtualInternalRecord (i , i + 1 )),
487
- IntStream .range (count , count * 2 )
488
- .mapToObj (i -> testType .dataType ().createVirtualLeafRecord (i ))
489
- .map (r -> r .toBytes (keySerializer , valueSerializer )),
490
- Stream .empty ());
491
- // create a snapshot
492
- final Path snapshotDbPath =
493
- testDirectory .resolve ("merkledb-snapshotRestoreIndex-" + testType + "_SNAPSHOT" );
494
- dataSource .getDatabase ().snapshot (snapshotDbPath , dataSource );
495
- // close data source
496
- dataSource .close ();
497
-
498
- final MerkleDb snapshotDb = MerkleDb .getInstance (snapshotDbPath , CONFIGURATION );
499
- final MerkleDbPaths snapshotPaths = new MerkleDbPaths (snapshotDb .getTableDir (tableName , tableId ));
500
- // Delete all indices
501
- Files .delete (snapshotPaths .pathToDiskLocationLeafNodesFile );
502
- Files .delete (snapshotPaths .pathToDiskLocationInternalNodesFile );
503
- // There is no way to use MerkleDbPaths to get bucket index file path
504
- Files .deleteIfExists (snapshotPaths .keyToPathDirectory .resolve (tableName + "_bucket_index.ll" ));
505
-
506
- final MerkleDbDataSource snapshotDataSource = snapshotDb .getDataSource (tableName , false );
507
- reinitializeDirectMemoryUsage ();
508
- IntStream .range (0 , count * 2 ).forEach (i -> assertHash (snapshotDataSource , i , i + 1 ));
509
- IntStream .range (count , count * 2 )
510
- .forEach (i ->
511
- assertLeaf (testType , keySerializer , valueSerializer , snapshotDataSource , i , i , i + 1 , i ));
512
- // close data source
513
- snapshotDataSource .close ();
514
-
515
- // check db count
516
- assertEventuallyEquals (
517
- 0L , MerkleDbDataSource ::getCountOfOpenDatabases , Duration .ofSeconds (1 ), "Expected no open dbs" );
518
- });
480
+ final int [] deltas = {-10 , 0 , 10 };
481
+ for (int delta : deltas ) {
482
+ createAndApplyDataSource (originalDbPath , tableName , testType , count + Math .abs (delta ), 0 , dataSource -> {
483
+ final int tableId = dataSource .getTableId ();
484
+ // create some records
485
+ dataSource .saveRecords (
486
+ count - 1 ,
487
+ count * 2 - 2 ,
488
+ IntStream .range (0 , count * 2 - 1 ).mapToObj (i -> createVirtualInternalRecord (i , i + 1 )),
489
+ IntStream .range (count - 1 , count * 2 - 1 )
490
+ .mapToObj (i -> testType .dataType ().createVirtualLeafRecord (i ))
491
+ .map (r -> r .toBytes (keySerializer , valueSerializer )),
492
+ Stream .empty ());
493
+ if (delta != 0 ) {
494
+ // create some more, current leaf path range shifted by delta
495
+ dataSource .saveRecords (
496
+ count - 1 + delta ,
497
+ count * 2 - 2 + 2 * delta ,
498
+ IntStream .range (0 , count * 2 - 1 + 2 * delta )
499
+ .mapToObj (i -> createVirtualInternalRecord (i , i + 1 )),
500
+ IntStream .range (count - 1 + delta , count * 2 - 1 + 2 * delta )
501
+ .mapToObj (i -> testType .dataType ().createVirtualLeafRecord (i ))
502
+ .map (r -> r .toBytes (keySerializer , valueSerializer )),
503
+ Stream .empty ());
504
+ }
505
+ // create a snapshot
506
+ final Path snapshotDbPath =
507
+ testDirectory .resolve ("merkledb-snapshotRestoreIndex-" + testType + "_SNAPSHOT" );
508
+ dataSource .getDatabase ().snapshot (snapshotDbPath , dataSource );
509
+ // close data source
510
+ dataSource .close ();
511
+
512
+ final MerkleDb snapshotDb = MerkleDb .getInstance (snapshotDbPath , CONFIGURATION );
513
+ final MerkleDbPaths snapshotPaths = new MerkleDbPaths (snapshotDb .getTableDir (tableName , tableId ));
514
+ // Delete all indices
515
+ Files .delete (snapshotPaths .pathToDiskLocationLeafNodesFile );
516
+ Files .delete (snapshotPaths .pathToDiskLocationInternalNodesFile );
517
+ // There is no way to use MerkleDbPaths to get bucket index file path
518
+ Files .deleteIfExists (snapshotPaths .keyToPathDirectory .resolve (tableName + "_bucket_index.ll" ));
519
+
520
+ final MerkleDbDataSource snapshotDataSource = snapshotDb .getDataSource (tableName , false );
521
+ reinitializeDirectMemoryUsage ();
522
+ // Check hashes
523
+ IntStream .range (0 , count * 2 - 1 + 2 * delta ).forEach (i -> assertHash (snapshotDataSource , i , i + 1 ));
524
+ assertNullHash (snapshotDataSource , count * 2 + 2 * delta );
525
+ // Check leaves
526
+ IntStream .range (0 , count - 2 + delta ).forEach (i -> assertNullLeaf (snapshotDataSource , i ));
527
+ IntStream .range (count - 1 + delta , count * 2 - 1 + 2 * delta )
528
+ .forEach (i -> assertLeaf (
529
+ testType , keySerializer , valueSerializer , snapshotDataSource , i , i , i + 1 , i ));
530
+ assertNullLeaf (snapshotDataSource , count * 2 + 2 * delta );
531
+ // close data source
532
+ snapshotDataSource .close ();
533
+
534
+ // check db count
535
+ assertEventuallyEquals (
536
+ 0L , MerkleDbDataSource ::getCountOfOpenDatabases , Duration .ofSeconds (1 ), "Expected no open dbs" );
537
+ });
538
+ }
519
539
}
520
540
521
541
@ Test
@@ -787,7 +807,16 @@ public static void assertHash(final MerkleDbDataSource dataSource, final long pa
787
807
try {
788
808
assertEqualsAndPrint (hash (i ), dataSource .loadHash (path ));
789
809
} catch (final Exception e ) {
790
- e .printStackTrace ();
810
+ e .printStackTrace (System .err );
811
+ fail ("Exception should not have been thrown here!" );
812
+ }
813
+ }
814
+
815
+ public static void assertNullHash (final MerkleDbDataSource dataSource , final long path ) {
816
+ try {
817
+ assertNull (dataSource .loadHash (path ));
818
+ } catch (final Exception e ) {
819
+ e .printStackTrace (System .err );
791
820
fail ("Exception should not have been thrown here!" );
792
821
}
793
822
}
@@ -828,6 +857,15 @@ public static void assertLeaf(
828
857
}
829
858
}
830
859
860
+ public static void assertNullLeaf (final MerkleDbDataSource dataSource , final long path ) {
861
+ try {
862
+ assertNull (dataSource .loadLeafRecord (path ));
863
+ } catch (final Exception e ) {
864
+ e .printStackTrace (System .err );
865
+ fail ("Exception should not have been thrown here!" );
866
+ }
867
+ }
868
+
831
869
public static <T > void assertEqualsAndPrint (final T recordA , final T recordB ) {
832
870
assertEquals (
833
871
recordA == null ? null : recordA .toString (),
0 commit comments