Пример #1
0
        private DuplicatesResult OnCompareDuplicates(string sourceFile, int fileStartIndex)
        {
            ConcurrentDictionary <string, CompareValue> compareCache = new ConcurrentDictionary <string, CompareValue>(CacheCompareValue);
            var result = new DuplicatesResult();
            var comp   = new FileComparison();

            for (int i = fileStartIndex + 1; i < Files.Count; i++)
            {
                var similar = comp.Similar(compareCache.FirstOrDefault(x => x.Key == sourceFile.ToUpper()).Value, compareCache.FirstOrDefault(x => x.Key == Files[i].ToUpper()).Value);
                if (similar.HasFlag(similarMinValue))
                {
                    result.FileResults.Add(new DuplicatesResult.FileResult
                    {
                        CompareValue = similar,
                        FilePath     = Files[i].ToUpper()
                    });
                }
            }
            CacheCompareValue = new Dictionary <string, CompareValue>(compareCache);
            if (result.FileResults.Count > 0)
            {
                result.FileResults.Add(new DuplicatesResult.FileResult
                {
                    CompareValue = CompareValue.Types.Hash | CompareValue.Types.FileName | CompareValue.Types.Extension | CompareValue.Types.Directory,
                    FilePath     = sourceFile
                });
                return(result);
            }
            return(null);
        }
Пример #2
0
        private void OnPrepareCompareValue(CancellationToken ct, int maxParallelism)
        {
            var comp = new FileComparison();
            ConcurrentDictionary <string, CompareValue> compare    = new ConcurrentDictionary <string, CompareValue>(CacheCompareValue);
            ConcurrentDictionary <string, CompareValue> newCompare = new ConcurrentDictionary <string, CompareValue>();
            int itemCounter = 0;

            PrepareCompareValuesProgress?.Invoke(this, 0);

            ParallelOptions po = new ParallelOptions
            {
                CancellationToken      = ct,
                MaxDegreeOfParallelism = maxParallelism
            };

            try
            {
                Parallel.For(0, Files.Count, po, (int index) =>
                {
                    itemCounter      += 1;
                    var progressValue = Math.Round(((decimal)itemCounter / (decimal)Files.Count * 100), 2);
                    PrepareCompareValuesProgress?.Invoke(this, progressValue);
                    string currentFile = Files[index]?.ToUpper();
                    if (!compare.ContainsKey(currentFile))
                    {
                        var compareValue = comp.CreateCompareValue(currentFile);
                        if (compareValue != null)
                        {
                            compare.GetOrAdd(currentFile, compareValue);
                            newCompare.GetOrAdd(currentFile, compareValue);
                        }
                    }
                    PrepareCompareValuesProgressWithItems?.Invoke(this, new PrepareComareProgressItem(progressValue, new Dictionary <string, CompareValue>(newCompare)));
                });
                PrepareCompareValuesProgress?.Invoke(this, 100);
                PrepareCompareValuesProgressWithItems?.Invoke(this, new PrepareComareProgressItem(100, new Dictionary <string, CompareValue>(newCompare)));
                CacheCompareValue = new Dictionary <string, CompareValue>(compare);
            }
            catch (Exception ex)
            {
                if (ex is AggregateException || ex is ObjectDisposedException || ex is OperationCanceledException)
                {
                    Aborted?.Invoke(this, EventArgs.Empty);
                    return;
                }
                throw;
            }
        }