Пример #1
0
        private void SortFile(
            string sourcepath,
            string inputPath,
            string outputPath,
            int bufferSize,
            int blockSize
            )
        {
            var bufferPath = $"{sourcepath}.{_PassesMade}";

            _fileAdapter.Delete(bufferPath);

            var inputfileFileReader = _fileReader.CreateInstance();
            var bufferFileWriter    = _fileWriter.CreateInstance();
            var outputFileWriter    = _fileWriter.CreateInstance();

            try
            {
                if (_fileAdapter.GetFileInfo(inputPath).Length == 0)
                {
                    if (_PassesMade > 0)
                    {
                        _Status = SortStatus.Cleaning;
                        _fileAdapter.Delete(inputPath);
                    }

                    _Done = true;
                    return;
                }

                _Status = SortStatus.Cleaning;
                GC.Collect();

                inputfileFileReader.BeginRead(inputPath, bufferSize);
                bufferFileWriter.BeginWrite(bufferPath, bufferSize);
                outputFileWriter.BeginWrite(outputPath, bufferSize);

                outputFileWriter.StreamWriter.BaseStream.Position =
                    outputFileWriter.StreamWriter.BaseStream.Length;

                var  output      = new GroupedItem <T> [0];
                long readLength  = 0;
                long totalLength = inputfileFileReader.StreamReader.BaseStream.Length;

                _Status = SortStatus.Reading;

                foreach (var block in inputfileFileReader.ReadBlock(blockSize))
                {
                    readLength += block.Sum(x => x.Length + Environment.NewLine.Length);
                    _RowsRed   += block.Count();
                    _RedPercent = (int)(readLength / (double)totalLength * 100);

                    var grouped   = new GroupedItem <T> [0];
                    var sanitized = block.Select(x => Sanitize(x));

                    if (_PassesMade == 0)
                    {
                        grouped = Group(sanitized);
                    }
                    else
                    {
                        grouped = Deserialize(sanitized);
                    }

                    var sorted = SortBlock(grouped);

                    output = MergeBlocks(
                        bufferFileWriter,
                        blockSize,
                        output,
                        sorted
                        );

                    _Status = SortStatus.Reading;
                }

                WriteUngrouped(outputFileWriter, output);

                inputfileFileReader.EndRead();
                bufferFileWriter.EndWrite();
                outputFileWriter.EndWrite();

                if (_PassesMade > 0)
                {
                    _Status = SortStatus.Cleaning;
                    _fileAdapter.Delete(inputPath);
                }

                _PassesMade++;
                ResetProgressMarkers();

                RunFileSortTask(
                    sourcepath,
                    bufferPath,
                    outputPath,
                    bufferSize,
                    blockSize
                    );
            }
            finally
            {
                inputfileFileReader.EndRead();
                bufferFileWriter.EndWrite();
                outputFileWriter.EndWrite();
            }
        }