/// <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> /// Saves the changes for segments and metadata /// </summary> internal void Save() { CheckClosed(); foreach (var segment in segments) { segment.Save(storage.MasterStream); } metadata.Save(); changeNotified = false; }
/// <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; }