Beispiel #1
0
        public async Task CreateResults()
        {
            try
            {
                ClearErrors();
                HideResultsWindows();
                ResetCompareWindow();
                ButtonGrid.Visibility = Visibility.Collapsed;

                CompareProgressWindow.Text = "Beginning Comparison.";

                var fileTime        = DateTime.Now.ToFileTime();
                var file1SortedName = _alreadySorted ? _file1Name : _file1Name.Replace(".csv", "_sorted.csv");
                var file2SortedName = _alreadySorted ? _file2Name : _file2Name.Replace(".csv", "_sorted.csv");

                var tempFolder = Path.Combine(Path.GetTempPath(), "UnlimitedCsvCompare");
                if (!Directory.Exists(tempFolder))
                {
                    Directory.CreateDirectory(tempFolder);
                }

                _comparisonTempFile  = Path.Combine(tempFolder, $"comparison_{fileTime}_temp.csv");
                _rowOrphans1TempFile = Path.Combine(tempFolder, $"comparison_orphans1_{fileTime}_temp.csv");
                _rowOrphans2TempFile = Path.Combine(tempFolder, $"comparison_orphans2_{fileTime}_temp.csv");

                // Sort file 1
                if (_alreadySorted)
                {
                    CompareProgressWindow.Text += Environment.NewLine + "Skipping sorting files.";
                }
                else
                {
                    // Figure out if we have room to sort in memory or we need external merge
                    var  memory      = new PerformanceCounter("Memory", "Available MBytes");
                    var  memoryValue = (long)memory.NextValue();
                    long maxFileSize = memoryValue / 10 * 1024 * 1024;

                    CompareProgressWindow.Text += Environment.NewLine + "Sorting File 1.";
                    var sorting1Stopwatch = new Stopwatch();
                    sorting1Stopwatch.Start();

                    if (new FileInfo(_file1Name).Length < maxFileSize)
                    {
                        CompareProgressWindow.Text += Environment.NewLine + "Using quick memory sort for small file.";
                        await Task.Run(() => CsvFileSorter.SortFileInMemory(_file1Name, _identifierColumns, file1SortedName));
                    }
                    else
                    {
                        CompareProgressWindow.Text += Environment.NewLine + "Using slow file sort for large file.";
                        await Task.Run(() => CsvFileSorter.ExternalMergeSort(_file1Name, maxFileSize, _identifierColumns, file1SortedName, tempFolder));
                    }

                    sorting1Stopwatch.Stop();
                    CompareProgressWindow.Text += Environment.NewLine + "Finished sorting File 1 in " + TimeSpanAsEnglish(sorting1Stopwatch.Elapsed);

                    // Sort file 2
                    CompareProgressWindow.Text += Environment.NewLine + "Sorting File 2.";
                    var sorting2Stopwatch = new Stopwatch();
                    sorting2Stopwatch.Start();

                    if (new FileInfo(_file2Name).Length < maxFileSize)
                    {
                        CompareProgressWindow.Text += Environment.NewLine + "Using quick memory sort for small file.";
                        await Task.Run(() => CsvFileSorter.SortFileInMemory(_file2Name, _identifierColumns, file2SortedName));
                    }
                    else
                    {
                        CompareProgressWindow.Text += Environment.NewLine + "Using slow file sort for large file.";
                        await Task.Run(() => CsvFileSorter.ExternalMergeSort(_file2Name, maxFileSize, _identifierColumns, file2SortedName, tempFolder));
                    }

                    sorting2Stopwatch.Stop();
                    CompareProgressWindow.Text += Environment.NewLine + "Finished sorting File 2 in " + TimeSpanAsEnglish(sorting2Stopwatch.Elapsed);
                }

                // Compare line by line.
                CompareProgressWindow.Text += Environment.NewLine + "Comparing the files.";
                var comparisonStopWatch = new Stopwatch();
                comparisonStopWatch.Start();
                var orphans = await Task.Run(() => CsvFileComparer.Compare(file1SortedName, file2SortedName, _identifierColumns, _inclusionColumns, _exclusionColumns, _comparisonTempFile, _rowOrphans1TempFile, _rowOrphans2TempFile, _ignoreCase));

                comparisonStopWatch.Stop();
                _columnOrphans1             = orphans.ColumnOrphans1;
                _columnOrphans2             = orphans.ColumnOrphans2;
                CompareProgressWindow.Text += Environment.NewLine + "Finished comparing the files in " + TimeSpanAsEnglish(comparisonStopWatch.Elapsed);

                ButtonGrid.Visibility = Visibility.Visible;

                if (new FileInfo(_comparisonTempFile).Length +
                    new FileInfo(_rowOrphans1TempFile).Length +
                    new FileInfo(_rowOrphans2TempFile).Length < 10_000_000)
                {
                    // Small results, display results
                    SetupDisplayValues();
                    HideCompareWindow();
                }
                else
                {
                    ErrorLabel.Text       = "Results file is too large for display. Click 'Export' for the comparison results.";
                    ErrorLabel.Visibility = Visibility.Visible;
                }
            }
            catch (DuplicateIdentifierException ex)
            {
                var errorBuilder = new StringBuilder($"Error: A duplicate identifier was found while sorting.{Environment.NewLine}Identifiers:{Environment.NewLine}");
                for (int i = 0; i < ex.IdentifierNames.Count; i++)
                {
                    errorBuilder.AppendLine($"{ex.IdentifierNames[i]} - {ex.IdentifierValues[i]}");
                }

                ErrorLabel.Text       = errorBuilder.ToString();
                ErrorLabel.Visibility = Visibility.Visible;
                StartOverFromSortingButton.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                ErrorLabel.Text       = $"Error: {ex.Message}{Environment.NewLine} Stack Trace: {ex.StackTrace}";
                ErrorLabel.Visibility = Visibility.Visible;
                StartOverFromSortingButton.Visibility = Visibility.Visible;
            }
        }
Beispiel #2
0
        public async Task CreateResults()
        {
            try
            {
                ClearErrors();
                HideResultsWindows();
                ResetCompareWindow();
                ButtonGrid.Visibility = Visibility.Collapsed;

                CompareProgressWindow.Text = "Beginning Comparison.";

                var fileTime        = DateTime.Now.ToFileTime();
                var file1SortedName = _alreadySorted ? _file1Name : _file1Name.Replace(".csv", "_sorted.csv");
                var file2SortedName = _alreadySorted ? _file2Name : _file2Name.Replace(".csv", "_sorted.csv");

                var tempFolder = Path.Combine(Path.GetTempPath(), "CsvCompare");
                if (!Directory.Exists(tempFolder))
                {
                    Directory.CreateDirectory(tempFolder);
                }

                _comparisonTempFile  = Path.Combine(tempFolder, $"comparison_{fileTime}_temp.csv");
                _rowOrphans1TempFile = Path.Combine(tempFolder, $"comparison_orphans1_{fileTime}_temp.csv");
                _rowOrphans2TempFile = Path.Combine(tempFolder, $"comparison_orphans2_{fileTime}_temp.csv");

                // Sort file 1
                if (_alreadySorted)
                {
                    CompareProgressWindow.Text += Environment.NewLine + "Skipping sorting files.";
                }
                else
                {
                    CompareProgressWindow.Text += Environment.NewLine + "Sorting File 1.";
                    var sorting1Stopwatch = new Stopwatch();
                    sorting1Stopwatch.Start();
                    await Task.Run(() => CsvFileSorter.SortFileInMemory(_file1Name, _identifierColumns, file1SortedName));

                    sorting1Stopwatch.Stop();
                    CompareProgressWindow.Text += Environment.NewLine + "Finished sorting File 1 in " + TimeSpanAsEnglish(sorting1Stopwatch.Elapsed);

                    // Sort file 2
                    CompareProgressWindow.Text += Environment.NewLine + "Sorting File 2.";
                    var sorting2Stopwatch = new Stopwatch();
                    sorting2Stopwatch.Start();
                    await Task.Run(() => CsvFileSorter.SortFileInMemory(_file2Name, _identifierColumns, file2SortedName));

                    sorting2Stopwatch.Stop();
                    CompareProgressWindow.Text += Environment.NewLine + "Finished sorting File 2 in " + TimeSpanAsEnglish(sorting2Stopwatch.Elapsed);
                }

                // Compare line by line.
                CompareProgressWindow.Text += Environment.NewLine + "Comparing the files.";
                var comparisonStopWatch = new Stopwatch();
                comparisonStopWatch.Start();
                var orphans = await Task.Run(() => CsvFileComparer.Compare(file1SortedName, file2SortedName, _identifierColumns, _inclusionColumns, _exclusionColumns, _comparisonTempFile, _rowOrphans1TempFile, _rowOrphans2TempFile, _ignoreCase));

                comparisonStopWatch.Stop();
                _columnOrphans1             = orphans.ColumnOrphans1;
                _columnOrphans2             = orphans.ColumnOrphans2;
                CompareProgressWindow.Text += Environment.NewLine + "Finished comparing the files in " + TimeSpanAsEnglish(comparisonStopWatch.Elapsed);

                ButtonGrid.Visibility = Visibility.Visible;

                if (new FileInfo(_comparisonTempFile).Length +
                    new FileInfo(_rowOrphans1TempFile).Length +
                    new FileInfo(_rowOrphans2TempFile).Length < 10_000_000)
                {
                    // Small results, display results
                    SetupDisplayValues();
                    HideCompareWindow();
                }
                else
                {
                    ErrorLabel.Content    = "Results file is too large for display. Click 'Export' for the comparison results.";
                    ErrorLabel.Visibility = Visibility.Visible;
                }
            }
            catch (Exception ex)
            {
                ErrorLabel.Content    = $"Error: {ex.Message}{Environment.NewLine} Stack Trace: {ex.StackTrace}";
                ErrorLabel.Visibility = Visibility.Visible;
            }
        }