/// <summary> /// Opens a stream /// </summary> /// <param name="streamId">Stream Id</param> public StorageStream OpenStream(Guid streamId) { CheckClosed(); if (SystemStreamId.IsSystemStreamId(streamId)) { throw new InvalidStreamIdException(); } StartTransaction(); try { StorageStream tmpStream = null; WeakReference <StorageStream> streamRef; // Check if stream is already opened if (openedStreams.TryGetValue(streamId, out streamRef)) { if (!streamRef.TryGetTarget(out tmpStream)) { tmpStream = null; openedStreams.Remove(streamId); } } // Open stream if (tmpStream == null) { var streamMetadata = streamTable.Get(streamId); if (streamMetadata == null) { throw new StreamNotFoundException(); } tmpStream = new StorageStream(streamMetadata, this); //tmpStream.Changed += StorageStream_Changed; openedStreams.Add(streamId, new WeakReference <StorageStream>(tmpStream)); } tmpStream.Position = 0; CommitTransaction(); return(tmpStream); } catch { RollbackTransaction(); throw; } }
/// <summary> /// Opens the storage /// </summary> private void OpenStorage() { StartTransaction(); try { // For metadata assume block size of 512 because blockSize is unknown at this point. // 512 is the smallest block size so it will work as long as storage metadata is not // longer than 512 bytes storageMetadataStream = new StorageStream(new StorageStreamMetadata(null) { FirstSegmentPosition = 0, InitializedLength = 512 - Segment.StructureSize, Length = 512 - Segment.StructureSize, StreamId = SystemStreamId.StorageMetadata, StreamTableIndex = -1 }, this); StorageMetadata = StorageMetadata.Load(storageMetadataStream); streamTableStreamMetadata = new StorageStreamMetadata(storageMetadataStream) { FirstSegmentPosition = blockSize, StreamId = SystemStreamId.StreamTable, StreamTableIndex = -1 }; streamTableStream = new StorageStream(streamTableStreamMetadata, this); streamTable = new StreamTable(streamTableStream); var freeSpaceStreamMetadata = streamTable.Get(SystemStreamId.EmptySpace); FreeSpaceStream = new StorageStream(freeSpaceStreamMetadata, this); CommitTransaction(); } catch { RollbackTransaction(); throw; } }