Esempio n. 1
0
        private bool JoinNextPart()
        {
            // Check for cancellation on every part.
            if (_loader.IsCanceled)
            {
                _loader.UpdateProgress(_status = _status.Cancel());
                Complete(null);
                return(false);
            }

            if (_currentPartIndex >= CacheFilePaths.Count)
            {
                Complete(null);
                return(false);
            }

            // If not cancelled, update progress.
            string cacheFilePath = CacheFilePaths[_currentPartIndex];
            string message       = string.Format(Resources.ChromCacheJoiner_JoinNextPart_Joining_file__0__, cacheFilePath);
            int    percent       = _currentPartIndex * 100 / CacheFilePaths.Count;

            _status = _status.ChangeMessage(message).ChangePercentComplete(percent);
            _loader.UpdateProgress(_status);

            try
            {
                using (var inStream = _loader.StreamManager.CreateStream(cacheFilePath, FileMode.Open, false))
                {
                    if (_fs.Stream == null)
                    {
                        _fs.Stream = _loader.StreamManager.CreateStream(_fs.SafeName, FileMode.Create, true);
                    }

                    ChromatogramCache.RawData rawData;
                    long bytesData = ChromatogramCache.LoadStructs(inStream, out rawData);

                    // If joining, then format version should have already been checked.
                    Assume.IsTrue(ChromatogramCache.IsVersionCurrent(rawData.FormatVersion) ||
                                  // WatersCacheTest uses older format partial caches
                                  rawData.FormatVersion == ChromatogramCache.FORMAT_VERSION_CACHE_2);

                    int  offsetFiles       = _listCachedFiles.Count;
                    int  offsetTransitions = _listTransitions.Count;
                    int  offsetPeaks       = _peakCount;
                    int  offsetScores      = _scoreCount;
                    long offsetPoints      = _fs.Stream.Position;

                    // Scan ids
                    long offsetScanIds = _fsScans.Stream.Position;
                    _listCachedFiles.AddRange(rawData.ChromCacheFiles.Select(f => f.RelocateScanIds(f.LocationScanIds + offsetScanIds)));
                    if (rawData.CountBytesScanIds > 0)
                    {
                        inStream.Seek(rawData.LocationScanIds, SeekOrigin.Begin);
                        inStream.TransferBytes(_fsScans.Stream, rawData.CountBytesScanIds);
                    }

                    _peakCount += rawData.ChromatogramPeaks.Length;
                    rawData.ChromatogramPeaks.WriteArray(block => ChromPeak.WriteArray(_fsPeaks.FileStream.SafeFileHandle, block));
                    _listTransitions.AddRange(rawData.ChromTransitions);
                    // Initialize the score types the first time through
                    if (_scoreTypesCount == -1)
                    {
                        _listScoreTypes.AddRange(rawData.ScoreTypes);
                        _scoreTypesCount = _listScoreTypes.Count;
                    }
                    else if (!ArrayUtil.EqualsDeep(_listScoreTypes, rawData.ScoreTypes))
                    {
                        // If the existing score types in the caches are not the same, the caches cannot be joined
                        if (_listScoreTypes.Intersect(rawData.ScoreTypes).Count() != _listScoreTypes.Count)
                        {
                            throw new InvalidDataException("Data cache files with different score types cannot be joined.");    // Not L10N
                        }
                    }
                    _scoreCount += rawData.Scores.Length;
                    rawData.Scores.WriteArray(block => PrimitiveArrays.Write(_fsScores.Stream, block));

                    for (int i = 0; i < rawData.ChromatogramEntries.Length; i++)
                    {
                        rawData.RecalcEntry(i,
                                            offsetFiles,
                                            offsetTransitions,
                                            offsetPeaks,
                                            offsetScores,
                                            offsetPoints,
                                            _dictTextIdToByteIndex,
                                            _listTextIdBytes);
                    }
                    _listGroups.AddRange(rawData.ChromatogramEntries);

                    inStream.Seek(0, SeekOrigin.Begin);

                    long copyBytes = bytesData;
                    while (copyBytes > 0)
                    {
                        int read = inStream.Read(_buffer, 0, (int)Math.Min(_buffer.Length, copyBytes));
                        _fs.Stream.Write(_buffer, 0, read);
                        copyBytes -= read;
                    }

                    _currentPartIndex++;
                    return(true);
                }
            }
            catch (InvalidDataException x)
            {
                Complete(x);
            }
            catch (IOException x)
            {
                Complete(x);
            }
            catch (Exception x)
            {
                Complete(new Exception(String.Format(Resources.ChromCacheJoiner_JoinNextPart_Failed_to_create_cache__0__, CachePath), x));
            }
            return(false);
        }