예제 #1
0
        protected void Complete(Exception x)
        {
            lock (this)
            {
                ChromatogramCache result = null;
                try
                {
                    if (x == null && !_status.IsFinal)
                    {
                        long locationScanIds = 0, countBytesScanIds = 0;
                        if (_fs.Stream != null)
                        {
                            try
                            {
                                locationScanIds   = _fs.Stream.Position;
                                countBytesScanIds = _fsScans.Stream.Position;

                                _listGroups.Sort();
                                var listChromGroupHeaderInfos = ReadOnlyList.Create(_listGroups.Count,
                                                                                    i => _listGroups[i].ChromGroupHeaderInfo);
                                ChromatogramCache.WriteStructs(CacheFormat,
                                                               _fs.Stream,
                                                               _fsScans.Stream,
                                                               _fsPeaks.Stream,
                                                               _fsScores.Stream,
                                                               _listCachedFiles,
                                                               listChromGroupHeaderInfos,
                                                               _listTransitions,
                                                               _listTextIdBytes,
                                                               _listScoreTypes,
                                                               _scoreCount,
                                                               _peakCount);

                                _loader.StreamManager.Finish(_fs.Stream);
                                _fs.Stream = null;
                                // Allow cancelation right up to the final commit
                                if (!_loader.IsCanceled)
                                {
                                    _fs.Commit(_destinationStream);
                                }
                                else
                                {
                                    _loader.UpdateProgress(_status = _status.Cancel());
                                }
                            }
                            catch (Exception xWrite)
                            {
                                throw new IOException(TextUtil.LineSeparate(string.Format(Resources.ChromCacheWriter_Complete_Failure_attempting_to_write_the_file__0_, _fs.RealName), xWrite.Message));
                            }
                        }

                        if (!_status.IsCanceled)
                        {
                            // Create stream identifier, but do not open.  The stream will be opened
                            // the first time the document uses it.
                            var readStream = _loader.StreamManager.CreatePooledStream(CachePath, false);

                            // DebugLog.Info("{0}. {1} - created", readStream.GlobalIndex, CachePath);

                            _fsPeaks.Stream.Seek(0, SeekOrigin.Begin);
                            _fsScores.Stream.Seek(0, SeekOrigin.Begin);
                            var arrayCachFiles = _listCachedFiles.ToArray();
                            _listCachedFiles = null;
                            var arrayChromEntries = BlockedArray <ChromGroupHeaderInfo> .Convert(_listGroups,
                                                                                                 entry => entry.ChromGroupHeaderInfo);

                            _listGroups = null;
                            var arrayTransitions = _listTransitions.ToBlockedArray();
                            _listTransitions = null;
                            var peakSerializer = CacheFormat.ChromPeakSerializer();
                            var chromPeaks     = new BlockedArray <ChromPeak>(
                                count => peakSerializer.ReadArray(_fsPeaks.FileStream, count), _peakCount,
                                ChromPeak.SizeOf, ChromPeak.DEFAULT_BLOCK_SIZE);
                            var scores = new BlockedArray <float>(
                                count => PrimitiveArrays.Read <float>(_fsScores.FileStream, count), _scoreCount,
                                sizeof(float), ChromatogramCache.DEFAULT_SCORES_BLOCK_SIZE);
                            var textIdBytes = _listTextIdBytes.ToArray();
                            _listTextIdBytes = null;

                            var rawData = new ChromatogramCache.RawData(CacheFormat)
                            {
                                ChromCacheFiles     = arrayCachFiles,
                                ChromatogramEntries = arrayChromEntries,
                                ChromTransitions    = arrayTransitions,
                                ChromatogramPeaks   = chromPeaks,
                                ScoreTypes          = _listScoreTypes.ToArray(),
                                Scores            = scores,
                                TextIdBytes       = textIdBytes,
                                CountBytesScanIds = countBytesScanIds,
                                LocationScanIds   = locationScanIds
                            };
                            result  = new ChromatogramCache(CachePath, rawData, readStream);
                            _status = _status.Complete();
                            _loader.UpdateProgress(_status);
                        }
                    }
                }
                catch (Exception x2)
                {
                    x = x2;
                }
                finally
                {
                    Dispose();
                }

                try
                {
                    _completed(result, x == null ? _status : _status.ChangeErrorException(x));
                }
                catch (Exception x2)
                {
                    _completed(null, _status.ChangeErrorException(x2));
                }
            }
        }