public void MakeSureYouCannotPutNegativeNumbersInTheBlock()
        {
            byte[]    bytes            = new byte[4096];
            const int numberOfIntegers = 10;

            var block = new IntegerListConstrained(bytes, 2, numberOfIntegers + 1);

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentOutOfRangeException>(
                delegate
            {
                block[0] = -1;
            });

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentOutOfRangeException>(
                delegate
            {
                block.AddInteger(-2);
            });

            MemoryStream stream = new MemoryStream(bytes, true);
            BinaryWriter writer = new BinaryWriter(stream);

            writer.Write((int)-56);

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentException>(
                delegate
            {
                new IntegerListConstrained(bytes, 2, numberOfIntegers + 1);
            });
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="numberOfBytesToAdd"></param>
        /// <param name="numberOfBlocksCommitted"></param>
        /// <param name="allBlocksAcquired"></param>
        /// <param name="blocksToDistribute"></param>
        /// <exception cref="NoFreeBlocksException"></exception>
        private void PushFreeBlocksToAddressingSystem(int numberOfBytesToAdd, int numberOfBlocksCommitted, List <int> allBlocksAcquired, Stack <int> blocksToDistribute)
        {
            IntegerListConstrained lastSinglyIndirectBlock = null;
            int lastSingleIndirectBlockSize = 0;

            var addressingSystemBlockSizes =
                base.AddressingBlockSizesCalculator.GetSizesOfAddressingBlocksSufficientToStoreItems(numberOfBlocksCommitted);

            lastSingleIndirectBlockSize = addressingSystemBlockSizes.LastSingleIndirectBlockSize;

            if (this.CurrentSize != 0)
            {
                var lastSingleIndirectBlockIndex = base.DoubleIndirectBlocks[base.DoubleIndirectBlocks.Count - 1];

                lastSinglyIndirectBlock = new IntegerListConstrained(
                    base.Disk.ReadAllBytesFromBlock(lastSingleIndirectBlockIndex),
                    lastSingleIndirectBlockSize,
                    base.AddressingSystemParameters.DataBlockReferencesCountInSingleIndirectBlock);
            }

            try
            {
                while (blocksToDistribute.Count != 0)
                {
                    if ((lastSinglyIndirectBlock != null) && (!lastSinglyIndirectBlock.IsFull))
                    {
                        lastSinglyIndirectBlock.AddInteger(blocksToDistribute.Pop());
                    }
                    else
                    {
                        if (lastSinglyIndirectBlock != null)
                        {
                            base.Disk.WriteBytesToBlock(base.DoubleIndirectBlocks[base.DoubleIndirectBlocks.Count - 1],
                                                        lastSinglyIndirectBlock.ToByteArray());
                        }

                        int newBlockIndex = _freeBlockManager.AcquireFreeBlock();
                        allBlocksAcquired.Add(newBlockIndex);

                        base.DoubleIndirectBlocks.AddInteger(newBlockIndex);

                        base.Disk.WriteBytesToBlock(base.DataStreamDefinition.ContentsBlockIndex, base.DoubleIndirectBlocks.ToByteArray());

                        lastSinglyIndirectBlock = new IntegerListConstrained(new byte[0], 0, base.AddressingSystemParameters.DataBlockReferencesCountInSingleIndirectBlock);
                    }
                }

                base.Disk.WriteBytesToBlock(base.DoubleIndirectBlocks[base.DoubleIndirectBlocks.Count - 1], lastSinglyIndirectBlock.ToByteArray());
                base.DataStreamDefinition.StreamLengthInBytes += (numberOfBytesToAdd);
            }
            catch (NoFreeBlocksException)
            {
                _freeBlockManager.MarkBlocksAsFree(allBlocksAcquired);

                throw;
            }
        }
        public void MakeSureYouCannotAddressIntegeresOutsideOfInitializedSpace()
        {
            byte[] bytes = new byte[4096];

            IntegerListConstrained integersList = new IntegerListConstrained(bytes, 0, 24);

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentOutOfRangeException>(
                delegate()
            {
                integersList[1] = 25;
            });
        }
Exemplo n.º 4
0
        private void InitializeSingleIndirectionBlock(int indexOfBlockToCreateIndirectionBlockFrom, int currentCountOfIntegersInBlock, int currentPositionInBlock)
        {
            byte[] blockContents = _disk.ReadAllBytesFromBlock(indexOfBlockToCreateIndirectionBlockFrom);

            _singlyIndirectBlockReferences      = new IntegerListConstrained(blockContents, currentCountOfIntegersInBlock, _addressingSystemParameters.DataBlockReferencesCountInSingleIndirectBlock);
            _singlyIndirectReferencesEnumerator = _singlyIndirectBlockReferences.GetAddressableEnumerator();

            if (currentPositionInBlock != PositionBeforeFirstElement)
            {
                _singlyIndirectReferencesEnumerator.SetPosition(currentPositionInBlock);
            }
        }
Exemplo n.º 5
0
        public DataStreamStructureBuilderImmutable(IVirtualDisk disk, DataStreamDefinition dataStreamDefinition, AddressingSystemParameters addressingSystemParameters, IFileSystemNodeStorage fileSystemNodeStorage, Node governingNode)
        {
            if (disk == null)
            {
                throw new ArgumentNullException("disk");
            }
            if (dataStreamDefinition == null)
            {
                throw new ArgumentNullException("dataStreamDefinition");
            }
            if (addressingSystemParameters == null)
            {
                throw new ArgumentNullException("addressingSystemParameters");
            }
            if (fileSystemNodeStorage == null)
            {
                throw new ArgumentNullException("fileSystemNodeStorage");
            }
            if (governingNode == null)
            {
                throw new ArgumentNullException("governingNode");
            }

            _disk                       = disk;
            _governingNode              = governingNode;
            _fileSystemNodeStorage      = fileSystemNodeStorage;
            _addressingSystemParameters = addressingSystemParameters;
            _dataStreamDefinition       = dataStreamDefinition;

            int numberOfBlocks =
                SpaceRequirementsCalculator.GetNumberOfChunksNeededToStoreData(
                    dataStreamDefinition.StreamLengthInBytes, addressingSystemParameters.BlockSize);

            _addressingBlockSizesCalculator = new AddressingBlockSizesCalculator(addressingSystemParameters.IndirectBlockReferencesCountInDoubleIndirectBlock,
                                                                                 addressingSystemParameters.DataBlockReferencesCountInSingleIndirectBlock);

            _sizesOfLastAddressingBlocks = AddressingBlockSizesCalculator.GetSizesOfAddressingBlocksSufficientToStoreItems(numberOfBlocks);

            int doubleIndirectBlockSize = _sizesOfLastAddressingBlocks.DoubleIndirectBlockSize;

            _doubleIndirectBlocks = new IntegerListConstrained(disk.ReadAllBytesFromBlock(dataStreamDefinition.ContentsBlockIndex), doubleIndirectBlockSize, _addressingSystemParameters.IndirectBlockReferencesCountInDoubleIndirectBlock);

            CalculateAndSetMaximumSize(addressingSystemParameters);
        }
        public void MakeSureTheIntegeresAreBeingProperlyUpdatedAndSerialized()
        {
            byte[]    bytes            = new byte[4096];
            const int numberOfIntegers = 10;

            IntegerListConstrained integersList = new IntegerListConstrained(bytes, numberOfIntegers, numberOfIntegers);

            for (int i = 0; i < numberOfIntegers; i++)
            {
                integersList[i] = i;
                Assert.AreEqual(i, integersList[i]);
            }

            IntegerListConstrained recreatedBlock = new IntegerListConstrained(integersList.ToByteArray(), integersList.Count, integersList.Count);

            for (int i = 0; i < numberOfIntegers; i++)
            {
                Assert.AreEqual(i, recreatedBlock[i]);
            }
        }
        public void MakeSureYouCannotAddIntegersIfTheBlockIsFull()
        {
            byte[]    bytes            = new byte[4096];
            const int numberOfIntegers = 10;

            IntegerListConstrained integersList = new IntegerListConstrained(bytes, numberOfIntegers, numberOfIntegers + 1);

            int initialCount = integersList.Count;

            Assert.AreEqual(numberOfIntegers, initialCount);

            integersList.AddInteger(1234);

            int count = integersList.Count;

            Assert.AreEqual(initialCount + 1, count);

            ExceptionAssert.MakeSureExceptionIsRaisedBy <InvalidOperationException>(
                delegate
            {
                integersList.AddInteger(44);
            });
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="newLength"></param>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="InconsistentDataDetectedException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        private void MakeShorter(int newLength)
        {
            int numberOfBlocksCommitted =
                SpaceRequirementsCalculator.GetNumberOfChunksNeededToStoreData(base.CurrentSize,
                                                                               base.AddressingSystemParameters.BlockSize);

            int numberOfBlocksNeededToStoreNewData =
                SpaceRequirementsCalculator.GetNumberOfChunksNeededToStoreData(newLength,
                                                                               base.AddressingSystemParameters.BlockSize);

            int numberOfBlocksToRemove = numberOfBlocksCommitted - numberOfBlocksNeededToStoreNewData;

            if (numberOfBlocksToRemove == 0) //изменения в пределах одного блока.
            {
                base.DataStreamDefinition.StreamLengthInBytes = newLength;
                base.FileSystemNodeStorage.WriteNode(base.GoverningNode);

                return;
            }

            var oldAddressingSystemBlockSizes =
                base.AddressingBlockSizesCalculator.GetSizesOfAddressingBlocksSufficientToStoreItems(numberOfBlocksCommitted);

            var newAddressingSystemBlockSizes =
                base.AddressingBlockSizesCalculator.GetSizesOfAddressingBlocksSufficientToStoreItems(numberOfBlocksNeededToStoreNewData);

            int doubleIndirectBlockIndexToStopAt = newAddressingSystemBlockSizes.DoubleIndirectBlockSize == 0
                                                       ? 0
                                                       : newAddressingSystemBlockSizes.DoubleIndirectBlockSize;

            for (int i = oldAddressingSystemBlockSizes.DoubleIndirectBlockSize - 1; i >= doubleIndirectBlockIndexToStopAt; i--)
            {
                int singlyIndirectBlockIndex = base.DoubleIndirectBlocks[i];
                int newSize;
                int currentSize;

                if (i == oldAddressingSystemBlockSizes.DoubleIndirectBlockSize - 1)
                {
                    currentSize = oldAddressingSystemBlockSizes.LastSingleIndirectBlockSize;
                }
                else
                {
                    currentSize = base.AddressingSystemParameters.DataBlockReferencesCountInSingleIndirectBlock;
                }

                if (i == doubleIndirectBlockIndexToStopAt) // ок, этот - остается.
                {
                    newSize = newAddressingSystemBlockSizes.LastSingleIndirectBlockSize;
                }
                else
                {
                    newSize = 0;
                }

                var listConstrained =
                    new IntegerListConstrained(base.Disk.ReadAllBytesFromBlock(singlyIndirectBlockIndex), currentSize, base.AddressingSystemParameters.DataBlockReferencesCountInSingleIndirectBlock);

                _freeBlockManager.MarkBlocksAsFree(listConstrained.Shrink(newSize));
            }

            _freeBlockManager.MarkBlocksAsFree(base.DoubleIndirectBlocks.Shrink(newAddressingSystemBlockSizes.DoubleIndirectBlockSize));

            base.Disk.WriteBytesToBlock(base.DataStreamDefinition.ContentsBlockIndex, base.DoubleIndirectBlocks.ToByteArray());

            base.DataStreamDefinition.StreamLengthInBytes = newLength;
            base.FileSystemNodeStorage.WriteNode(base.GoverningNode);
        }