/// <summary> /// Creates a stream /// </summary> /// <param name="streamId">Stream Id</param> public StorageStream CreateStream(Guid streamId, int tag = 0) { CheckClosed(); if (SystemStreamId.IsSystemStreamId(streamId)) { throw new InvalidStreamIdException(); } if (ContainsStream(streamId)) { throw new StreamExistsException(); } StartTransaction(); try { streamTable.Add(streamId, tag); CommitTransaction(); streamsCreatedDuringTransaction.Add(streamId); } catch { RollbackTransaction(); throw; } return(OpenStream(streamId)); }
internal void StreamChanged(StorageStreamChangeType changeType, StorageStream stream) { if (!SystemStreamId.IsSystemStreamId(stream.StreamId) || stream.StreamId == SystemStreamId.EmptySpace) { switch (changeType) { case StorageStreamChangeType.SegmentsAndMetadata: if (!streamsChangedDuringTransaction.Contains(stream)) { streamsChangedDuringTransaction.Add(stream); } break; case StorageStreamChangeType.Closing: if (streamsChangedDuringTransaction.Contains(stream)) { streamsChangedDuringTransaction.Remove(stream); } openedStreams.Remove(stream.StreamId); //e.Stream.Changed -= StorageStream_Changed; break; } } }
/// <summary> /// Gets all of the stream Id's /// </summary> /// <param name="tag">If specified, only streams with specified tag are returned</param> public IEnumerable <Guid> GetStreams(int?tag) { return(streamTable.Get() .Where(x => !SystemStreamId.IsSystemStreamId(x.StreamId)) .Where(x => !tag.HasValue || x.Tag == tag.Value) .Select(x => x.StreamId) .ToList()); }
/// <summary> /// Checks if storage contains specified stream /// </summary> public bool ContainsStream(Guid streamId) { CheckClosed(); if (SystemStreamId.IsSystemStreamId(streamId)) { throw new InvalidStreamIdException(); } return(streamTable.Contains(streamId)); }
/// <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> /// Gets areas where specified stream segments are located /// </summary> public List <SegmentExtent> GetStreamExtents(Guid streamId) { CheckClosed(); if (SystemStreamId.IsSystemStreamId(streamId)) { throw new InvalidStreamIdException(); } StorageStream stream = OpenStream(streamId); return(stream.GetStreamExtents()); }
/// <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; } }