Repository navigation
Expand file tree
/
Copy pathFolderDiffExecutionStrategy.cs
More file actions
204 lines (181 loc) · 8.9 KB
/
Copy pathFolderDiffExecutionStrategy.cs
File metadata and controls
204 lines (181 loc) · 8.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FolderDiffIL4DotNet.Core.Diagnostics;
using FolderDiffIL4DotNet.Models;
namespace FolderDiffIL4DotNet.Services
{
/// <summary>
/// Determines discovery criteria and parallelism policy for folder diffs.
/// フォルダ差分の探索条件と並列実行ポリシーを決定します。
/// </summary>
public sealed class FolderDiffExecutionStrategy : IFolderDiffExecutionStrategy
{
/// <summary>
/// Maximum parallelism when network-share optimisation is active. Limited to 8
/// because NAS/SMB servers typically throttle or error with too many simultaneous
/// connections; empirical testing shows 8 concurrent workers as a stable upper bound.
/// ネットワーク最適化時の自動並列度上限。NAS/SMB 環境では同時接続数が多すぎるとスロットリングや
/// エラーが発生するため、実測で安定する 8 並列を上限としています。
/// </summary>
private const int MAX_PARALLEL_NETWORK_LIMIT = 8;
/// <summary>
/// Multiplier applied to <see cref="Environment.ProcessorCount"/> for local I/O-bound
/// workloads. File hashing and IL comparison are I/O-dominant, so using twice the core
/// count keeps the CPU busy while other threads wait on disk reads.
/// ローカル I/O バウンドワークロード用の <see cref="Environment.ProcessorCount"/> 倍率。
/// ファイルハッシュや IL 比較はディスク I/O が支配的なため、コア数の2倍を使用して
/// 他スレッドのディスク読み取り待ち中も CPU を稼働させます。
/// </summary>
private const int IO_BOUND_MULTIPLIER = 2;
private readonly IReadOnlyConfigSettings _config;
private readonly FileDiffResultLists _fileDiffResultLists;
private readonly IFileSystemService _fileSystem;
private readonly string _oldFolderAbsolutePath;
private readonly string _newFolderAbsolutePath;
private readonly bool _optimizeForNetworkShares;
private readonly HashSet<string> _ignoredExtensions;
private readonly StringComparer _pathComparer;
/// <summary>
/// Initializes a new instance of <see cref="FolderDiffExecutionStrategy"/>.
/// <see cref="FolderDiffExecutionStrategy"/> の新しいインスタンスを初期化します。
/// </summary>
public FolderDiffExecutionStrategy(
IReadOnlyConfigSettings config,
DiffExecutionContext executionContext,
FileDiffResultLists fileDiffResultLists,
IFileSystemService fileSystem)
{
ArgumentNullException.ThrowIfNull(config);
ArgumentNullException.ThrowIfNull(executionContext);
ArgumentNullException.ThrowIfNull(fileDiffResultLists);
ArgumentNullException.ThrowIfNull(fileSystem);
_config = config;
_fileDiffResultLists = fileDiffResultLists;
_fileSystem = fileSystem;
_oldFolderAbsolutePath = executionContext.OldFolderAbsolutePath;
_newFolderAbsolutePath = executionContext.NewFolderAbsolutePath;
_optimizeForNetworkShares = executionContext.OptimizeForNetworkShares;
_ignoredExtensions = new HashSet<string>(_config.IgnoredExtensions ?? Enumerable.Empty<string>(), StringComparer.OrdinalIgnoreCase);
_pathComparer = DetermineRelativePathComparer(_oldFolderAbsolutePath, _newFolderAbsolutePath);
}
/// <inheritdoc />
public List<string> EnumerateIncludedFiles(string rootFolderAbsolutePath, FileDiffResultLists.IgnoredFileLocation locationFlag)
{
var includedFiles = new List<string>();
foreach (var fileAbsolutePath in _fileSystem.EnumerateFiles(rootFolderAbsolutePath, "*", SearchOption.AllDirectories))
{
if (_ignoredExtensions.Contains(Path.GetExtension(fileAbsolutePath)))
{
if (_config.ShouldIncludeIgnoredFiles)
{
var relativePath = Path.GetRelativePath(rootFolderAbsolutePath, fileAbsolutePath);
_fileDiffResultLists.RecordIgnoredFile(relativePath, locationFlag);
}
continue;
}
includedFiles.Add(fileAbsolutePath);
}
return includedFiles;
}
/// <inheritdoc />
public int ComputeUnionFileCount(IReadOnlyCollection<string> oldFilesAbsolutePath, IReadOnlyCollection<string> newFilesAbsolutePath)
{
var oldRelativePathSet = new HashSet<string>(
oldFilesAbsolutePath.Select(path => Path.GetRelativePath(_oldFolderAbsolutePath, path)),
_pathComparer);
var newRelativePathSet = new HashSet<string>(
newFilesAbsolutePath.Select(path => Path.GetRelativePath(_newFolderAbsolutePath, path)),
_pathComparer);
oldRelativePathSet.UnionWith(newRelativePathSet);
return oldRelativePathSet.Count;
}
/// <inheritdoc />
public int DetermineMaxParallel()
{
if (_config.MaxParallelism <= 0)
{
if (_optimizeForNetworkShares)
{
return Math.Min(Environment.ProcessorCount, MAX_PARALLEL_NETWORK_LIMIT);
}
// File comparison is I/O-bound: use ProcessorCount × 2 so threads that
// block on disk I/O leave room for others to keep the CPU busy.
// ファイル比較は I/O バウンド: ディスク I/O 待ちスレッドの隙間を
// 他スレッドで埋めるため、ProcessorCount × 2 を使用。
return Environment.ProcessorCount * IO_BOUND_MULTIPLIER;
}
return _config.MaxParallelism;
}
/// <inheritdoc />
public int CountDotNetAssemblyCandidates(IEnumerable<string> oldFilesAbsolutePath, IEnumerable<string> newFilesAbsolutePath, Action<double>? progressCallback = null)
{
var distinctFiles = oldFilesAbsolutePath
.Concat(newFilesAbsolutePath)
.Distinct(_pathComparer)
.ToList();
int total = distinctFiles.Count;
int dotNetCount = 0;
for (int i = 0; i < total; i++)
{
if (DotNetDetector.IsDotNetExecutable(distinctFiles[i]))
{
dotNetCount++;
}
progressCallback?.Invoke(total > 0 ? Math.Min((double)(i + 1) * 100.0 / total, 100.0) : 100.0);
}
return dotNetCount;
}
private static StringComparer DetermineRelativePathComparer(string oldFolderAbsolutePath, string newFolderAbsolutePath)
{
return IsCaseInsensitiveDirectory(oldFolderAbsolutePath) && IsCaseInsensitiveDirectory(newFolderAbsolutePath)
? StringComparer.OrdinalIgnoreCase
: StringComparer.Ordinal;
}
private static bool IsCaseInsensitiveDirectory(string directoryPath)
{
if (OperatingSystem.IsWindows())
{
return true;
}
try
{
string fullPath = Path.GetFullPath(directoryPath);
if (!Directory.Exists(fullPath))
{
return OperatingSystem.IsMacOS();
}
string trimmedPath = fullPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
string? parentPath = Path.GetDirectoryName(trimmedPath);
string directoryName = Path.GetFileName(trimmedPath);
string alternateName = ToggleAsciiCase(directoryName);
if (string.IsNullOrEmpty(parentPath) || alternateName == directoryName)
{
return OperatingSystem.IsMacOS();
}
return Directory.Exists(Path.Combine(parentPath, alternateName));
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException or NotSupportedException)
{
return OperatingSystem.IsMacOS();
}
}
private static string ToggleAsciiCase(string value)
{
for (int i = 0; i < value.Length; i++)
{
char ch = value[i];
if (ch is >= 'a' and <= 'z')
{
return value[..i] + char.ToUpperInvariant(ch) + value[(i + 1)..];
}
if (ch is >= 'A' and <= 'Z')
{
return value[..i] + char.ToLowerInvariant(ch) + value[(i + 1)..];
}
}
return value;
}
}
}