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 )
@@ -49,52 +49,56 @@ public BackupChain(IAgDatabase agDatabase) : this(agDatabase.RecentBackups()) {
4949 /// <summary>
5050 /// Initializes a backup chain from a stand alone database that is not part of an AG.
5151 /// </summary>
52- public BackupChain ( Database database ) : this ( database . RecentBackups ( ) ) { }
52+ public BackupChain ( Database database ) : this ( database . MostRecentBackupChain ( ) ) { }
5353
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 ( ) ) throw new BackupChainException ( "Could not find any full backups" ) ;
66-
67- var targetCheckpointLsn = fullBackupsOrdered . First ( ) . CheckpointLsn ;
68- // get all the stripes of this backup
69- return fullBackupsOrdered . Where ( fullBackup => fullBackup . CheckpointLsn == targetCheckpointLsn ) ;
70- }
71-
72- private static IEnumerable < BackupMetadata > MostRecentDiffBackup ( IEnumerable < BackupMetadata > backups ,
73- BackupMetadata lastFullBackup )
74- {
75- var diffBackupsOrdered = backups
76- . Where ( b => b . BackupType == BackupFileTools . BackupType . Diff &&
77- b . DatabaseBackupLsn == lastFullBackup . CheckpointLsn )
78- . OrderByDescending ( b => b . LastLsn ) . ToList ( ) ;
79-
80- if ( ! diffBackupsOrdered . Any ( ) ) return new List < BackupMetadata > ( ) ;
81- var targetLastLsn = diffBackupsOrdered . First ( ) . LastLsn ;
82- // get all the stripes of this backup
83- return diffBackupsOrdered . Where ( diffBackup => diffBackup . LastLsn == targetLastLsn ) ;
84- }
85-
86- private static IEnumerable < BackupMetadata > NextLogBackup ( IEnumerable < BackupMetadata > backups ,
87- BackupMetadata prevBackup )
88- {
89- // also gets all the stripes of the next backup
90- return backups . Where ( b => b . BackupType == BackupFileTools . BackupType . Log &&
91- prevBackup . LastLsn >= b . FirstLsn && prevBackup . LastLsn + 1 < b . LastLsn ) ;
92- }
93-
94- private static bool IsValidFilePath ( BackupMetadata meta )
95- {
96- var path = meta . PhysicalDeviceName ;
97- return BackupFileTools . IsValidFileUrl ( path ) || BackupFileTools . IsValidFilePath ( path ) ;
98- }
99- }
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+ }
100104}
0 commit comments