Beispiel #1
0
        protected override Result <BlockCollection> ZipConvert(BlockCollection readerCollection)
        {
            int bufferSize  = FileInformation.BufferSize;
            int blocksCount = readerCollection.Blocks.Count;
            int chunks      = blocksCount / threadsLimit;

            var threads = new Thread[threadsLimit];
            var decompressedCollection = new BlockCollection();
            var errors = new List <string>();

            try {
                for (var i = 0; i < threadsLimit; i++)
                {
                    long chunkStart = i * chunks;
                    long chunkEnd   = chunkStart + chunks;
                    if (i == threadsLimit - 1)
                    {
                        chunkEnd += blocksCount % threadsLimit;
                    }

                    threads[i] = new Thread(
                        () => {
                        Result <IReadOnlyList <Block> > blocksResult = GetDecompressedBlocksByChunkResult(chunkStart, chunkEnd, bufferSize, readerCollection);

                        if (blocksResult.Success)
                        {
                            decompressedCollection.SetBlocks(blocksResult.Value);
                        }
                        else
                        {
                            Monitor.Enter(mLock);
                            errors.Add(blocksResult.Error);
                            Monitor.Exit(mLock);
                        }
                    })
                    {
                        IsBackground = true,
                        Priority     = ThreadPriority.AboveNormal
                    };

                    threads[i].Start();
                }

                foreach (Thread thread in threads)
                {
                    thread.Join();
                }

                return(errors.Any()
                    ? Result.Fail <BlockCollection>(string.Join("\n", errors))
                    : Result.Ok(decompressedCollection.GetSortedCollection()));
            } catch (System.Exception ex) {
                return(Result.Fail <BlockCollection>(StreamException.GetErrorText(ex)));
            }
        }