Esempio n. 1
0
        /// <summary>
        /// The returned 'Sector' is invalidated at the next call.
        /// </summary>
        public Sector Read(SectorIndex index)
        {
            _memoryStream.Seek(index.ByteIndex(_sectorSizeInBytes), SeekOrigin.Begin);
            _sector.Read(_memoryStream, index.ByteIndex(_sectorSizeInBytes));

            return(_sector);
        }
Esempio n. 2
0
        /// <remarks>
        /// The returned 'Sector' is invalidated at the next call.
        /// </remarks>
        public Sector Read(SectorIndex index)
        {
            if (!index.IsWithinRange(_firstNotAllocated))
            {
                throw new ArgumentOutOfRangeException(
                          $"Try to read from index {index} out of total {_firstNotAllocated.Value} sectors.");
            }

            _storeStream.Seek(index.ByteIndex(_sectorSizeInBytes), SeekOrigin.Begin);
            _sector.Read(_storeStream, index.ByteIndex(_sectorSizeInBytes));

            return(_sector);
        }
Esempio n. 3
0
        /// <summary>
        /// Write to hot storage first, then regular storage secondly.
        /// </summary>
        public void Write(SectorIndex index, Sector source)
        {
            if (!index.IsWithinRange(_firstNotAllocated))
            {
                throw new ArgumentOutOfRangeException(
                          $"Try to write to index {index.Value} out of total {_firstNotAllocated.Value} sectors.");
            }

            _hotStream.Seek(0, SeekOrigin.Begin);
            source.Write(_hotStream, index.ByteIndex(_sectorSizeInBytes));

            _storeStream.Seek(index.ByteIndex(_sectorSizeInBytes), SeekOrigin.Begin);
            source.Write(_storeStream, index.ByteIndex(_sectorSizeInBytes));
        }
Esempio n. 4
0
        public SectorIndex AllocateSectors(int count)
        {
            if (count <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            var firstNewlyAllocated = _firstNotAllocated;

            _firstNotAllocated = _firstNotAllocated.GetNext(count);

            _storeStream.SetLength(_storeStream.Length + _sectorSizeInBytes * count);

            _sector.Clear();

            for (var idx = firstNewlyAllocated;
                 idx.IsWithinRange(_firstNotAllocated);
                 idx = idx.GetNext())
            {
                _sector.Write(_storeStream, idx.ByteIndex(_sectorSizeInBytes));
            }

            Debug.Assert(_storeStream.Length == _firstNotAllocated.ByteIndex(_sectorSizeInBytes));

            return(firstNewlyAllocated);
        }
Esempio n. 5
0
        public bool TryRead(SectorIndex index, out Sector sector)
        {
            sector = null;

            // upper layer saved -1 as sectorIndex if child sector doesn't exist.
            if (index.IsUnknown())
            {
                return(false);
            }

            var position = _storeStream.Seek(index.ByteIndex(_sectorSizeInBytes), SeekOrigin.Begin);

            // seek a position beyond file length
            if (position == _storeStream.Length)
            {
                return(false);
            }

            _sector.Read(_storeStream, index.ByteIndex(_sectorSizeInBytes));
            sector = _sector;
            return(true);
        }
Esempio n. 6
0
 public void Write(SectorIndex index, Sector source)
 {
     _memoryStream.Seek(index.ByteIndex(_sectorSizeInBytes), SeekOrigin.Begin);
     source.Write(_memoryStream, index.ByteIndex(_sectorSizeInBytes));
 }