/// <summary> /// Creates a storage /// </summary> private void CreateStorage(Stream stream) { this.MasterStream = new MasterStream(stream, false); // Initialize storage metadata Segment metadataStreamSegment = Segment.Create(0, blockSize, null); metadataStreamSegment.Save(stream); StorageStream metadataStream = new StorageStream(new StorageStreamMetadata(null) { FirstSegmentPosition = 0, InitializedLength = blockSize - Segment.StructureSize, Length = blockSize - Segment.StructureSize, StreamId = SystemStreamId.StorageMetadata, StreamTableIndex = -1 }, this); StorageMetadata storageMetadata = new StorageMetadata("[TmStorage 1.0]"); // Set metadata again because above, stream was not specified storageMetadata.Save(metadataStream); metadataStream.Close(); // Initialize stream table long streamTableSegmentSize = 1000 / ((int)blockSize / StorageStreamMetadata.StructureSize) * blockSize; Segment streamTableSegment = Segment.Create(blockSize, streamTableSegmentSize, null); stream.Position = metadataStreamSegment.DataAreaEnd; streamTableSegment.Save(stream); StorageStream streamTableStream = new StorageStream(new StorageStreamMetadata(null) { FirstSegmentPosition = blockSize, InitializedLength = streamTableSegmentSize - Segment.StructureSize, Length = streamTableSegmentSize - Segment.StructureSize, StreamId = SystemStreamId.StreamTable, StreamTableIndex = -1 }, this); // Initialize empty space stream Segment emptyStreamSegment = Segment.Create(streamTableSegment.DataAreaEnd, long.MaxValue - streamTableSegment.DataAreaEnd, null); stream.Position = streamTableSegment.DataAreaEnd; emptyStreamSegment.Save(stream); // Write empty space stream metadata to stream table StorageStreamMetadata emptySpaceStreamMetadata = new StorageStreamMetadata(streamTableStream) { FirstSegmentPosition = emptyStreamSegment.Location, InitializedLength = emptyStreamSegment.DataAreaSize, Length = emptyStreamSegment.DataAreaSize, StreamId = SystemStreamId.EmptySpace, StreamTableIndex = 0 }; emptySpaceStreamMetadata.Save(); this.MasterStream = null; }
/// <summary> /// Deletes a stream /// </summary> /// <param name="streamId">Stream Id</param> public void DeleteStream(Guid streamId) { CheckClosed(); if (SystemStreamId.IsSystemStreamId(streamId)) { throw new InvalidStreamIdException(); } StartTransaction(); try { // Before deleting, set stream size to zero to deallocate all of the space it occupies StorageStream tmpStream = OpenStream(streamId); tmpStream.SetLength(0); tmpStream.Close(); openedStreams.Remove(streamId); streamTable.Remove(streamId); // Remove stream from list of changed streams tmpStream = streamsChangedDuringTransaction.SingleOrDefault(x => x.StreamId == streamId); if (tmpStream != null) { streamsChangedDuringTransaction.Remove(tmpStream); } // Remove stream from list of created streams if (streamsCreatedDuringTransaction.Contains(streamId)) { streamsCreatedDuringTransaction.Remove(streamId); } CommitTransaction(); } catch { RollbackTransaction(); throw; } }