void AddFilePart(FilePart filePart) { if (filePart.IsSorted) { _filesToMerge.Enqueue(filePart); return; } if (filePart.FileSize <= 16 * 1024 * 1024) { _filesToSort.Push(filePart); return; } _filesToSplit.Push(filePart); }
async Task Merge(FilePart filePart1, FilePart filePart2) { string path = Path.Combine(_tempDirectory, Path.GetRandomFileName()); int lineCount = 0; using (var sr1 = new StreamReader(filePart1.FileName)) using (var sr2 = new StreamReader(filePart2.FileName)) using (var sw = new StreamWriter(path)) { var linFromFile1 = await sr1.ReadLineAsync(); var linFromFile2 = await sr2.ReadLineAsync(); while ((linFromFile1 == null && linFromFile2 == null) == false) { if (linFromFile1 == null && linFromFile2 != null) { await sw.WriteLineAsync(linFromFile2); lineCount++; linFromFile2 = await sr2.ReadLineAsync(); continue; } if (linFromFile1 != null && linFromFile2 == null) { await sw.WriteLineAsync(linFromFile1); lineCount++; linFromFile1 = await sr1.ReadLineAsync(); continue; } var row1 = Row.Parse(linFromFile1); var row2 = Row.Parse(linFromFile2); var compareResult = row1.CompareTo(row2); if (compareResult == -1) { await sw.WriteLineAsync(linFromFile1); lineCount++; linFromFile1 = await sr1.ReadLineAsync(); continue; } await sw.WriteLineAsync(linFromFile2); lineCount++; linFromFile2 = await sr2.ReadLineAsync(); continue; } } File.Delete(filePart1.FileName); File.Delete(filePart2.FileName); // filePart1 and filePart2 is empty if (lineCount == 0) { File.Delete(path); return; } AddFilePart(new FilePart(path, lineCount, new FileInfo(path).Length, true)); }
public FileSplitResult(FilePart filePart1, FilePart filePart2) { FilePart1 = filePart1; FilePart2 = filePart2; }