Ejemplo n.º 1
0
 internal BlockCacheSettings(BlockCacheSettings settings)
 {
     BlockSize       = settings.BlockSize;
     ReadCacheSize   = settings.ReadCacheSize;
     LargeReadSize   = settings.LargeReadSize;
     OptimumReadSize = settings.OptimumReadSize;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the BlockCacheStream class.
        /// </summary>
        /// <param name="toWrap">The stream to wrap.</param>
        /// <param name="ownership">Whether to assume ownership of <c>toWrap</c>.</param>
        /// <param name="settings">The cache settings.</param>
        public BlockCacheStream(SparseStream toWrap, Ownership ownership, BlockCacheSettings settings)
        {
            if (!toWrap.CanRead)
            {
                throw new ArgumentException("The wrapped stream does not support reading", nameof(toWrap));
            }

            if (!toWrap.CanSeek)
            {
                throw new ArgumentException("The wrapped stream does not support seeking", nameof(toWrap));
            }

            _wrappedStream = toWrap;
            _ownWrapped    = ownership;
            _settings      = new BlockCacheSettings(settings);

            if (_settings.OptimumReadSize % _settings.BlockSize != 0)
            {
                throw new ArgumentException("Invalid settings, OptimumReadSize must be a multiple of BlockSize",
                                            nameof(settings));
            }

            _readBuffer         = new byte[_settings.OptimumReadSize];
            _blocksInReadBuffer = _settings.OptimumReadSize / _settings.BlockSize;

            int totalBlocks = (int)(_settings.ReadCacheSize / _settings.BlockSize);

            _cache = new BlockCache <Block>(_settings.BlockSize, totalBlocks);
            _stats = new BlockCacheStatistics();
            _stats.FreeReadBlocks = totalBlocks;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the BlockCacheStream class.
        /// </summary>
        /// <param name="toWrap">The stream to wrap.</param>
        /// <param name="ownership">Whether to assume ownership of <c>toWrap</c>.</param>
        /// <param name="settings">The cache settings.</param>
        public BlockCacheStream(SparseStream toWrap, Ownership ownership, BlockCacheSettings settings)
        {
            if (!toWrap.CanRead)
            {
                throw new ArgumentException("The wrapped stream does not support reading", "toWrap");
            }

            if (!toWrap.CanSeek)
            {
                throw new ArgumentException("The wrapped stream does not support seeking", "toWrap");
            }

            _wrappedStream = toWrap;
            _ownWrapped = ownership;
            _settings = new BlockCacheSettings(settings);

            if (_settings.OptimumReadSize % _settings.BlockSize != 0)
            {
                throw new ArgumentException("Invalid settings, OptimumReadSize must be a multiple of BlockSize", "settings");
            }

            _readBuffer = new byte[_settings.OptimumReadSize];
            _blocksInReadBuffer = _settings.OptimumReadSize / _settings.BlockSize;

            int totalBlocks = (int)(_settings.ReadCacheSize / _settings.BlockSize);

            _cache = new BlockCache<Block>(_settings.BlockSize, totalBlocks);
            _stats = new BlockCacheStatistics();
            _stats.FreeReadBlocks = totalBlocks;
        }
Ejemplo n.º 4
0
        public void CachedOverread()
        {
            MemoryStream ms = CreateSequencedMemStream(100, false);
            BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 };
            BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            byte[] buffer = new byte[20];
            cacheStream.Position = 90;
            int numRead = cacheStream.Read(buffer, 0, buffer.Length);

            Assert.AreEqual(10, numRead);
            AssertSequenced(buffer, 0, 10, 90);
            Assert.AreEqual(0, cacheStream.Statistics.LargeReadsIn);
            Assert.AreEqual(0, cacheStream.Statistics.ReadCacheHits);
            Assert.AreEqual(1, cacheStream.Statistics.TotalReadsIn);

            buffer = new byte[buffer.Length];
            cacheStream.Position = 90;
            numRead = cacheStream.Read(buffer, 0, buffer.Length);

            Assert.AreEqual(10, numRead);
            AssertSequenced(buffer, 0, 10, 90);
            Assert.AreEqual(0, cacheStream.Statistics.LargeReadsIn);
            Assert.AreEqual(1, cacheStream.Statistics.ReadCacheHits);
            Assert.AreEqual(2, cacheStream.Statistics.TotalReadsIn);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a new instance.
        /// </summary>
        /// <param name="toWrap">The stream to wrap</param>
        /// <param name="ownership">Whether to assume ownership of <c>toWrap</c></param>
        /// <param name="settings">The cache settings</param>
        public BlockCacheStream(SparseStream toWrap, Ownership ownership, BlockCacheSettings settings)
        {
            if (!toWrap.CanRead)
            {
                throw new ArgumentException("The wrapped stream does not support reading", "toWrap");
            }
            if (!toWrap.CanSeek)
            {
                throw new ArgumentException("The wrapped stream does not support seeking", "toWrap");
            }


            _wrappedStream = toWrap;
            _ownWrapped    = ownership;
            _settings      = new BlockCacheSettings(settings);

            if (_settings.OptimumReadSize % _settings.BlockSize != 0)
            {
                throw new ArgumentException("Invalid settings, OptimumReadSize must be a multiple of BlockSize", "settings");
            }
            _readBuffer         = new byte[_settings.OptimumReadSize];
            _blocksInReadBuffer = _settings.OptimumReadSize / _settings.BlockSize;

            _totalBlocks = (int)(_settings.ReadCacheSize / _settings.BlockSize);

            _stats = new BlockCacheStatistics();
            _stats.FreeReadBlocks = _totalBlocks;

            _blocks     = new Dictionary <long, CacheBlock>();
            _lru        = new LinkedList <CacheBlock>();
            _freeBlocks = new List <CacheBlock>(_totalBlocks);
        }
        public void FailWrite()
        {
            MemoryStream       ms       = CreateSequencedMemStream(100, false);
            BlockCacheSettings settings = new BlockCacheSettings()
            {
                BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30
            };
            BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            byte[] buffer = new byte[25];
            cacheStream.Position = 0;
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 0);

            int freeBefore = cacheStream.Statistics.FreeReadBlocks;

            cacheStream.Position = 11;
            try
            {
                cacheStream.Write(new byte[10], 0, 10);
            }
            catch (NotSupportedException)
            {
                Assert.AreEqual(freeBefore + 2, cacheStream.Statistics.FreeReadBlocks);
            }
        }
        public void Write()
        {
            MemoryStream       ms       = CreateSequencedMemStream(100, true);
            BlockCacheSettings settings = new BlockCacheSettings()
            {
                BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30
            };
            BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            byte[] buffer = new byte[20];
            cacheStream.Position = 10;
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 10);

            cacheStream.Position = 20;
            cacheStream.Write(new byte[10], 0, 10);
            Assert.AreEqual(30, cacheStream.Position);

            cacheStream.Position = 10;
            buffer = new byte[30];
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 0, 10, 10);
            AssertSequenced(buffer, 20, 10, 30);
            Assert.AreEqual(0, buffer[10]);
            Assert.AreEqual(0, buffer[19]);
        }
        public void CacheBlockRecycle()
        {
            MemoryStream       ms       = CreateSequencedMemStream(100, false);
            BlockCacheSettings settings = new BlockCacheSettings()
            {
                BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 50, LargeReadSize = 100
            };
            BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            byte[] buffer = new byte[50];
            cacheStream.Position = 10;
            int numRead = cacheStream.Read(buffer, 0, buffer.Length);

            Assert.AreEqual(50, numRead);
            AssertSequenced(buffer, 10);
            Assert.AreEqual(0, cacheStream.Statistics.LargeReadsIn);
            Assert.AreEqual(0, cacheStream.Statistics.ReadCacheHits);
            Assert.AreEqual(1, cacheStream.Statistics.TotalReadsIn);

            buffer = new byte[40];
            cacheStream.Position = 50;
            numRead = cacheStream.Read(buffer, 0, buffer.Length);

            Assert.AreEqual(40, numRead);
            AssertSequenced(buffer, 50);
            Assert.AreEqual(0, cacheStream.Statistics.LargeReadsIn);
            Assert.AreEqual(1, cacheStream.Statistics.ReadCacheHits);
            Assert.AreEqual(2, cacheStream.Statistics.TotalReadsIn);
        }
        public void UnalignedCachedRead()
        {
            MemoryStream       ms       = CreateSequencedMemStream(100, false);
            BlockCacheSettings settings = new BlockCacheSettings()
            {
                BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30
            };
            BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            byte[] buffer = new byte[20];
            cacheStream.Position = 3;
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 3);
            Assert.AreEqual(0, cacheStream.Statistics.LargeReadsIn);
            Assert.AreEqual(0, cacheStream.Statistics.ReadCacheHits);
            Assert.AreEqual(1, cacheStream.Statistics.TotalReadsIn);

            buffer = new byte[buffer.Length];
            cacheStream.Position = 3;
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 3);
            Assert.AreEqual(0, cacheStream.Statistics.LargeReadsIn);
            Assert.AreEqual(1, cacheStream.Statistics.ReadCacheHits);
            Assert.AreEqual(2, cacheStream.Statistics.TotalReadsIn);
        }
Ejemplo n.º 10
0
        public void Bug5203_IncreaseSize()
        {
            MemoryStream ms = new MemoryStream();
            BlockCacheSettings settings = new BlockCacheSettings { BlockSize = 64, LargeReadSize = 128, OptimumReadSize = 64, ReadCacheSize = 1024 };
            BlockCacheStream bcs = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            // Pre-load read cache with a 'short' block
            bcs.Write(new byte[11], 0, 11);
            bcs.Position = 0;
            bcs.Read(new byte[11], 0, 11);

            // Extend stream
            for(int i = 0; i < 20; ++i)
            {
                bcs.Write(new byte[11], 0, 11);
            }

            // Try to read from first block beyond length of original cached short length
            // Bug was throwing exception here
            bcs.Position = 60;
            bcs.Read(new byte[20], 0, 20);
        }
        public void Bug5203_IncreaseSize()
        {
            MemoryStream       ms       = new MemoryStream();
            BlockCacheSettings settings = new BlockCacheSettings {
                BlockSize = 64, LargeReadSize = 128, OptimumReadSize = 64, ReadCacheSize = 1024
            };
            BlockCacheStream bcs = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            // Pre-load read cache with a 'short' block
            bcs.Write(new byte[11], 0, 11);
            bcs.Position = 0;
            bcs.Read(new byte[11], 0, 11);

            // Extend stream
            for (int i = 0; i < 20; ++i)
            {
                bcs.Write(new byte[11], 0, 11);
            }

            // Try to read from first block beyond length of original cached short length
            // Bug was throwing exception here
            bcs.Position = 60;
            bcs.Read(new byte[20], 0, 20);
        }
Ejemplo n.º 12
0
        public void Write()
        {
            MemoryStream ms = CreateSequencedMemStream(100, true);
            BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 };
            BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            byte[] buffer = new byte[20];
            cacheStream.Position = 10;
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 10);

            cacheStream.Position = 20;
            cacheStream.Write(new byte[10], 0, 10);
            Assert.AreEqual(30, cacheStream.Position);

            cacheStream.Position = 10;
            buffer = new byte[30];
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 0, 10, 10);
            AssertSequenced(buffer, 20, 10, 30);
            Assert.AreEqual(0, buffer[10]);
            Assert.AreEqual(0, buffer[19]);
        }
Ejemplo n.º 13
0
        public void FailWrite()
        {
            MemoryStream ms = CreateSequencedMemStream(100, false);
            BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 };
            BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            byte[] buffer = new byte[25];
            cacheStream.Position = 0;
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 0);

            int freeBefore = cacheStream.Statistics.FreeReadBlocks;

            cacheStream.Position = 11;
            try
            {
                cacheStream.Write(new byte[10], 0, 10);
            }
            catch(NotSupportedException)
            {
                Assert.AreEqual(freeBefore + 2, cacheStream.Statistics.FreeReadBlocks);
            }
        }