Beispiel #1
0
        public void Count_should_return_count_of_pushed_items()
        {
            int expected   = 44;
            var blockStack = new ConcurrentBlockStack();

            for (int i = 0; i < expected; i++)
            {
                blockStack.Push(new BlockWithPosition(new byte[] { 2, 3, 4 }, 11));
            }
            Assert.AreEqual(expected, blockStack.Count());
        }
Beispiel #2
0
        private void decompressBlockThread()
        {
            if (_lockedThreadsException != null)
            {
                return;
            }

            while (true)
            {
                if (_decompressedBlocks.Count() >= _threadsCount)
                {
                    continue;
                }

                BlockWithPosition block = null;
                while (_readedBlocks.TryPop(out block) == false && _lockedIsFileClosed == false)
                {
                }
                try
                {
                    if (block == null)
                    {
                        break;
                    }
                    block.Block = _blockDecompressor.DecompressBlock(block.Block);
                }
                catch (Exception ex)
                {
                    _lockedThreadsException = ex;
                    break;
                }

                _decompressedBlocks.Push(block);
            }
            lock (_currentDecompressedIndexLocker)
            {
                _currentDecompressedIndex++;
                if (_currentDecompressedIndex == _threadsCount)
                {
                    _lockedIsFileDecompressed = true;
                }
            }
        }
Beispiel #3
0
        private void readBlocksThread()
        {
            if (_lockedThreadsException != null)
            {
                return;
            }

            while (true)
            {
                if (_readedBlocks.Count() >= _threadsCount)
                {
                    continue;
                }

                lock (_readLocker)
                {
                    var block = new BlockWithPosition();
                    try
                    {
                        if (_inputFile.Position >= _inputFile.Length)
                        {
                            _lockedIsFileClosed = true;
                            break;
                        }
                        var blockPosBuf = new byte[8];
                        _inputFile.Read(blockPosBuf);
                        long blockPos    = BitConverter.ToInt64(blockPosBuf);
                        var  blockLenBuf = new byte[4];
                        _inputFile.Read(blockLenBuf);
                        int blockLen = BitConverter.ToInt32(blockLenBuf);

                        block.Position = blockPos;
                        block.Block    = _blockReader.ReadBlock(_inputFile, _inputFile.Position, blockLen);
                    }
                    catch (Exception ex)
                    {
                        _lockedThreadsException = ex;
                        break;
                    }
                    _readedBlocks.Push(block);
                }
            }
        }
Beispiel #4
0
        private void readBlockThread()
        {
            if (_lockedThreadsException != null)
            {
                return;
            }
            while (true)
            {
                if (_readedBlocks.Count() >= _threadsCount)
                {
                    continue;
                }
                using var inputFile = File.OpenRead(_inputFilePath);
                long pos = 0;
                lock (_currentReadIndexLocker)
                {
                    pos = _currentReadIndex * _blockLen;
                    if (pos >= inputFile.Length)
                    {
                        _lockedIsFileClosed = true;
                        break;
                    }
                    _currentReadIndex++;
                }

                byte[] block = null;
                try
                {
                    block = _blockReader.ReadBlock(inputFile, pos, _blockLen);
                }
                catch (Exception ex)
                {
                    _lockedThreadsException = ex;
                    break;
                }
                _readedBlocks.Push(new BlockWithPosition(block, pos));
            }
        }