PathEnumeratorContext
using System;
using System.Collections.Generic;
using System.Linq;
namespace Relativity.Transfer
{
public sealed class PathEnumeratorContext
{
public const double DefaultProgressRateSeconds = 0.25;
public const bool DefaultSyncBatchTotals = false;
public const bool DefaultValidateTotalFileCount = false;
public const int MaxRemoteDegreeOfParallelism = 2;
public const int MaxLocalDegreeOfParallelism = 8;
private static readonly object SyncRoot = new object();
private string targetPath;
private DateTime? searchProgressTimestamp;
public ClientConfiguration Configuration { get; }
public IList<string> FileNames { get; }
public int MaxDegreeOfDirectoryParallelism { get; set; }
public int MaxDegreeOfFileParallelism { get; set; }
public int MaxDegreeOfRemotePagingParallelism { get; set; }
public PathEnumeratorOption Option { get; set; }
public bool PreserveFolders { get; set; }
public double ProgressRateSeconds { get; set; }
public IList<string> SearchPaths { get; }
public bool SyncBatchTotals { get; set; }
public string TargetPath {
get {
return targetPath;
}
set {
targetPath = value;
IsTargetPathUnix = PathHelper.IsUnixPath(value);
}
}
public bool ValidateTotalFileCount { get; set; }
internal bool IsTargetPathUnix { get; set; }
public event EventHandler<EnumeratedPathErrorEventArgs> PathError;
public event EventHandler<EnumeratedPathsEventArgs> Progress;
public event EventHandler<SerializedPathsEventArgs> Serialized;
public PathEnumeratorContext()
: this(string.Empty)
{
}
public PathEnumeratorContext(string searchPath)
: this(searchPath, string.Empty)
{
}
public PathEnumeratorContext(string searchPath, string targetPath)
: this(searchPath, targetPath, PathEnumeratorOption.TopDirectoryOnly)
{
}
public PathEnumeratorContext(string searchPath, string targetPath, PathEnumeratorOption option)
: this(new ClientConfiguration(), new string[1] {
searchPath
}, targetPath, option)
{
}
public PathEnumeratorContext(IEnumerable<string> searchPaths)
: this(searchPaths, string.Empty)
{
}
public PathEnumeratorContext(IEnumerable<string> searchPaths, string targetPath)
: this(searchPaths, targetPath, PathEnumeratorOption.TopDirectoryOnly)
{
}
public PathEnumeratorContext(IEnumerable<string> searchPaths, string targetPath, PathEnumeratorOption option)
: this(new ClientConfiguration(), searchPaths, targetPath, option)
{
}
public PathEnumeratorContext(ClientConfiguration clientConfiguration)
: this(clientConfiguration, string.Empty)
{
}
public PathEnumeratorContext(ClientConfiguration clientConfiguration, string searchPath)
: this(clientConfiguration, searchPath, string.Empty)
{
}
public PathEnumeratorContext(ClientConfiguration clientConfiguration, string searchPath, string targetPath)
: this(clientConfiguration, searchPath, targetPath, PathEnumeratorOption.TopDirectoryOnly)
{
}
public PathEnumeratorContext(ClientConfiguration clientConfiguration, string searchPath, string targetPath, PathEnumeratorOption option)
: this(clientConfiguration, new string[1] {
searchPath
}, targetPath, option)
{
}
public PathEnumeratorContext(ClientConfiguration clientConfiguration, IEnumerable<string> searchPaths)
: this(clientConfiguration, searchPaths, string.Empty)
{
}
public PathEnumeratorContext(ClientConfiguration clientConfiguration, IEnumerable<string> searchPaths, string targetPath)
: this(clientConfiguration, searchPaths, targetPath, PathEnumeratorOption.TopDirectoryOnly)
{
}
public PathEnumeratorContext(ClientConfiguration clientConfiguration, IEnumerable<string> searchPaths, string targetPath, PathEnumeratorOption option)
{
if (clientConfiguration == null)
throw new ArgumentNullException("clientConfiguration");
if (searchPaths == null)
throw new ArgumentNullException("searchPaths");
Configuration = clientConfiguration;
FileNames = new List<string>();
MaxDegreeOfDirectoryParallelism = Math.Min(Environment.ProcessorCount, 8);
MaxDegreeOfFileParallelism = Math.Min(Environment.ProcessorCount, 8);
MaxDegreeOfRemotePagingParallelism = Math.Min(Environment.ProcessorCount, 2);
PreserveFolders = true;
ProgressRateSeconds = 0.25;
Option = option;
SearchPaths = new List<string>();
foreach (string item in from x in searchPaths
where !string.IsNullOrEmpty(x)
select x) {
SearchPaths.Add(item);
}
SyncBatchTotals = false;
TargetPath = targetPath;
ValidateTotalFileCount = false;
}
internal void PublishSerializedPaths(SerializedBatch batch)
{
this.Serialized?.Invoke(this, new SerializedPathsEventArgs(batch));
}
internal void PublishPathError(string path, Exception error)
{
this.PathError?.Invoke(this, new EnumeratedPathErrorEventArgs(path, error));
}
internal void PublishProgress(bool force, PathSearchStorage storage)
{
if (force || ShouldPublishProgress()) {
double searchRateFps = PathEnumeratorHelper.CalculateSearchRateFps(storage.TotalFiles, (DateTime.Now - storage.StartTime).TotalSeconds);
this.Progress?.Invoke(this, new EnumeratedPathsEventArgs(storage.TotalDirectories, storage.TotalBytes, storage.TotalFiles, storage.PathErrors.Count, searchRateFps));
lock (SyncRoot) {
searchProgressTimestamp = DateTime.Now;
}
}
}
private bool ShouldPublishProgress()
{
lock (SyncRoot) {
if (searchProgressTimestamp.HasValue && !(ProgressRateSeconds <= 0)) {
double totalSeconds = (DateTime.Now - searchProgressTimestamp.Value).TotalSeconds;
return totalSeconds >= ProgressRateSeconds;
}
return true;
}
}
}
}