1- namespace AgDatabaseMove
2- {
3- using System . Collections . Generic ;
4- using System . Linq ;
5- using Exceptions ;
6- using SmoFacade ;
7-
8-
1+ namespace AgDatabaseMove
2+ {
3+ using System . Collections . Generic ;
4+ using System . Linq ;
5+ using Exceptions ;
6+ using SmoFacade ;
7+
8+
99 public interface IBackupChain
1010 {
1111 IEnumerable < BackupMetadata > OrderedBackups { get ; }
@@ -14,11 +14,11 @@ public interface IBackupChain
1414 /// <summary>
1515 /// Encapsulates the logic for determining the order to apply recent backups.
1616 /// </summary>
17- public class BackupChain : IBackupChain
18- {
19- private readonly IList < BackupMetadata > _orderedBackups ;
20-
21- // This also handles any striped backups
17+ public class BackupChain : IBackupChain
18+ {
19+ private readonly IList < BackupMetadata > _orderedBackups ;
20+
21+ // This also handles any striped backups
2222 private BackupChain ( IList < BackupMetadata > recentBackups )
2323 {
2424 if ( recentBackups == null || recentBackups . Count == 0 )
@@ -54,51 +54,53 @@ public BackupChain(Database database) : this(database.MostRecentBackupChain()) {
5454 /// <summary>
5555 /// Backups ordered to have a full restore chain.
5656 /// </summary>
57- public IEnumerable < BackupMetadata > OrderedBackups => _orderedBackups ;
58-
59- private static IEnumerable < BackupMetadata > MostRecentFullBackup ( IEnumerable < BackupMetadata > backups )
60- {
61- var fullBackupsOrdered = backups
62- . Where ( b => b . BackupType == BackupFileTools . BackupType . Full )
63- . OrderByDescending ( d => d . CheckpointLsn ) . ToList ( ) ;
64-
65- if ( ! fullBackupsOrdered . Any ( ) )
66- throw new BackupChainException ( "Could not find any full backups" ) ;
67-
68- var targetCheckpointLsn = fullBackupsOrdered . First ( ) . CheckpointLsn ;
69- // get all the stripes of this backup
70- return fullBackupsOrdered . Where ( fullBackup => fullBackup . CheckpointLsn == targetCheckpointLsn ) ;
71- }
72-
73- private static IEnumerable < BackupMetadata > MostRecentDiffBackup ( IEnumerable < BackupMetadata > backups ,
74- BackupMetadata lastFullBackup )
75- {
76- var diffBackupsOrdered = backups
77- . Where ( b =>
78- b . BackupType == BackupFileTools . BackupType . Diff &&
79- b . DatabaseBackupLsn == lastFullBackup . CheckpointLsn )
80- . OrderByDescending ( b => b . LastLsn ) . ToList ( ) ;
81-
82- if ( ! diffBackupsOrdered . Any ( ) )
83- return new List < BackupMetadata > ( ) ;
84-
85- var targetLastLsn = diffBackupsOrdered . First ( ) . LastLsn ;
86- // get all the stripes of this backup
87- return diffBackupsOrdered . Where ( diffBackup => diffBackup . LastLsn == targetLastLsn ) ;
88- }
89-
90- private static IEnumerable < BackupMetadata > NextLogBackup ( IEnumerable < BackupMetadata > backups ,
91- BackupMetadata prevBackup )
92- {
93- // also gets all the stripes of the next backup
94- return backups . Where ( b => b . BackupType == BackupFileTools . BackupType . Log &&
95- prevBackup . LastLsn >= b . FirstLsn && prevBackup . LastLsn + 1 < b . LastLsn ) ;
96- }
97-
98- private static bool IsValidFilePath ( BackupMetadata meta )
99- {
100- var path = meta . PhysicalDeviceName ;
101- return BackupFileTools . IsValidFileUrl ( path ) || BackupFileTools . IsValidFilePath ( path ) ;
102- }
103- }
104- }
57+ public IEnumerable < BackupMetadata > OrderedBackups => _orderedBackups ;
58+
59+ private static IEnumerable < BackupMetadata > MostRecentFullBackup ( IEnumerable < BackupMetadata > backups )
60+ {
61+ var fullBackupsOrdered = backups
62+ . Where ( b => b . BackupType == BackupFileTools . BackupType . Full )
63+ . OrderByDescending ( d => d . CheckpointLsn ) . ToList ( ) ;
64+
65+ if ( ! fullBackupsOrdered . Any ( ) )
66+ throw new BackupChainException ( "Could not find any full backups" ) ;
67+
68+ var targetCheckpointLsn = fullBackupsOrdered . First ( ) . CheckpointLsn ;
69+ // get all the stripes of this backup
70+ return fullBackupsOrdered . Where ( fullBackup => fullBackup . CheckpointLsn == targetCheckpointLsn ) ;
71+ }
72+
73+ private static IEnumerable < BackupMetadata > MostRecentDiffBackup ( IEnumerable < BackupMetadata > backups ,
74+ BackupMetadata lastFullBackup )
75+ {
76+ var diffBackupsOrdered = backups
77+ . Where ( b =>
78+ b . BackupType == BackupFileTools . BackupType . Diff &&
79+ b . DatabaseBackupLsn == lastFullBackup . CheckpointLsn )
80+ . OrderByDescending ( b => b . LastLsn ) . ToList ( ) ;
81+
82+ if ( ! diffBackupsOrdered . Any ( ) )
83+ return new List < BackupMetadata > ( ) ;
84+
85+ var targetLastLsn = diffBackupsOrdered . First ( ) . LastLsn ;
86+ // get all the stripes of this backup
87+ return diffBackupsOrdered . Where ( diffBackup => diffBackup . LastLsn == targetLastLsn ) ;
88+ }
89+
90+ private static IEnumerable < BackupMetadata > NextLogBackup ( IEnumerable < BackupMetadata > backups ,
91+ BackupMetadata prevBackup )
92+ {
93+ // also gets all the stripes of the next backup
94+ return backups . Where ( b => b . BackupType == BackupFileTools . BackupType . Log &&
95+ prevBackup . LastLsn >= b . FirstLsn &&
96+ prevBackup . LastLsn <= b . LastLsn &&
97+ ! new BackupMetadataEqualityComparer ( ) . EqualsExceptForPhysicalDeviceName ( prevBackup , b ) ) ;
98+ }
99+
100+ private static bool IsValidFilePath ( BackupMetadata meta )
101+ {
102+ var path = meta . PhysicalDeviceName ;
103+ return BackupFileTools . IsValidFileUrl ( path ) || BackupFileTools . IsValidFilePath ( path ) ;
104+ }
105+ }
106+ }
0 commit comments