/// <summary> /// Restores a <see cref="Stream"/> instance from particular etag, version and optional properties. /// If properties is <c>null</c> the stream header will be merged, otherwise replaced. /// </summary> /// <param name="partition"> /// The partition in which a stream resides. /// </param> /// <param name="etag"> /// The latest etag /// </param> /// <param name="version"> /// The version of the stream corresponding to <paramref name="etag"/> /// </param> /// <param name="properties"> /// The additional properties for this stream. /// </param> /// <exception cref="ArgumentNullException"> /// If <paramref name="partition"/> is <c>null</c> /// </exception> /// <exception cref="ArgumentNullException"> /// If <paramref name="etag"/> is <c>null</c> /// </exception> /// <exception cref="ArgumentException"> /// If <paramref name="etag"/> resolves to an empty <c>string</c> /// </exception> /// <exception cref="ArgumentException"> /// If <paramref name="version"/> is less than <c>0</c> /// </exception> public static Stream From(Partition partition, string etag, int version, StreamProperties properties = null) { Requires.NotNull(partition, nameof(partition)); Requires.NotNullOrEmpty(etag, nameof(etag)); Requires.GreaterThanOrEqualToZero(version, nameof(version)); return(new Stream(partition, etag, version, properties ?? StreamProperties.None)); }
/// <summary> /// Initiates an asynchronous operation that writes the given array of events to a partition using specified expected version. /// </summary> /// <remarks>For new stream specify expected version as 0</remarks> /// <param name="partition">The partition.</param> /// <param name="options">The stream write options.</param> /// <param name="expectedVersion">The expected version of the stream.</param> /// <param name="events">The events to write.</param> /// <returns> /// The result of the stream write operation containing updated stream header /// </returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="partition"/> is <c>null</c> /// </exception> /// <exception cref="ArgumentNullException"> /// If <paramref name="events"/> is <c>null</c> /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// If <paramref name="expectedVersion"/> is less than 0 /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// If <paramref name="events"/> array is empty /// </exception> /// <exception cref="DuplicateEventException"> /// If event with the given id already exists in a storage /// </exception> /// <exception cref="IncludedOperationConflictException"> /// If included entity operation has conflicts /// </exception> /// <exception cref="ConcurrencyConflictException"> /// If write operation has conflicts /// </exception> public static async Task <StreamWriteResult> WriteAsync(Partition partition, StreamWriteOptions options, int expectedVersion, params EventData[] events) { Requires.NotNull(partition, nameof(partition)); Requires.GreaterThanOrEqualToZero(expectedVersion, nameof(expectedVersion)); var stream = expectedVersion == 0 ? new Stream(partition) : await OpenAsync(partition); if (stream.Version != expectedVersion) { throw ConcurrencyConflictException.StreamChangedOrExists(partition); } return(await WriteAsync(stream, options, events)); }
/// <summary> /// Writes the given array of events to a partition using specified expected version. /// </summary> /// <remarks>For new stream specify expected version as 0</remarks> /// <param name="partition">The partition.</param> /// <param name="expectedVersion">The expected version of the stream.</param> /// <param name="events">The events to write.</param> /// <returns> /// The result of the stream write operation containing updated stream header /// </returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="partition"/> is <c>null</c> /// </exception> /// <exception cref="ArgumentNullException"> /// If <paramref name="events"/> is <c>null</c> /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// If <paramref name="expectedVersion"/> is less than 0 /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// If <paramref name="events"/> array is empty /// </exception> /// <exception cref="DuplicateEventException"> /// If event with the given id already exists in a storage /// </exception> /// <exception cref="IncludedOperationConflictException"> /// If included entity operation has conflicts /// </exception> /// <exception cref="ConcurrencyConflictException"> /// If write operation has conflicts /// </exception> public static StreamWriteResult Write(Partition partition, int expectedVersion, params EventData[] events) { Requires.NotNull(partition, nameof(partition)); Requires.GreaterThanOrEqualToZero(expectedVersion, nameof(expectedVersion)); var stream = expectedVersion == 0 ? new Stream(partition) : Open(partition); if (stream.Version != expectedVersion) { throw ConcurrencyConflictException.StreamChangedOrExists(partition); } return(Write(stream, events)); }