private async IAsyncEnumerable <ResolvedEvent> ReadInternal( ReadReq request, UserCredentials userCredentials, [EnumeratorCancellation] CancellationToken cancellationToken) { if (request.Options.CountOptionCase == ReadReq.Types.Options.CountOptionOneofCase.Count && request.Options.Count <= 0) { throw new ArgumentOutOfRangeException("count"); } if (request.Options.Filter == null) { request.Options.NoFilter = new ReadReq.Types.Empty(); } request.Options.UuidOption = new ReadReq.Types.Options.Types.UUIDOption { Structured = new ReadReq.Types.Empty() }; using var call = _client.Read( request, RequestMetadata.Create(userCredentials), cancellationToken: cancellationToken); await foreach (var e in call.ResponseStream .ReadAllAsync(cancellationToken) .Select(ConvertToResolvedEvent) .WithCancellation(cancellationToken) .ConfigureAwait(false)) { yield return(e); } ResolvedEvent ConvertToResolvedEvent(ReadResp response) => new ResolvedEvent( ConvertToEventRecord(response.Event.Event), ConvertToEventRecord(response.Event.Link), response.Event.PositionCase switch { ReadResp.Types.ReadEvent.PositionOneofCase.CommitPosition => new Position( response.Event.CommitPosition, 0).ToInt64().commitPosition, ReadResp.Types.ReadEvent.PositionOneofCase.NoPosition => null, _ => throw new InvalidOperationException() });
public override async Task Read( ReadReq request, IServerStreamWriter <ReadResp> responseStream, ServerCallContext context) { var options = request.Options; var countOptionsCase = options.CountOptionCase; var streamOptionsCase = options.StreamOptionCase; var readDirection = options.ReadDirection; var filterOptionsCase = options.FilterOptionCase; var uuidOptionsCase = options.UuidOption.ContentCase; var user = context.GetHttpContext().User; var requiresLeader = GetRequiresLeader(context.RequestHeaders); var op = streamOptionsCase switch { StreamOptionOneofCase.Stream => ReadOperation.WithParameter( Plugins.Authorization.Operations.Streams.Parameters.StreamId(request.Options.Stream.StreamIdentifier)), StreamOptionOneofCase.All => ReadOperation.WithParameter( Plugins.Authorization.Operations.Streams.Parameters.StreamId(SystemStreams.AllStream)), _ => throw new InvalidOperationException() }; if (!await _provider.CheckAccessAsync(user, op, context.CancellationToken).ConfigureAwait(false)) { throw AccessDenied(); } await using var enumerator = (streamOptionsCase, countOptionsCase, readDirection, filterOptionsCase) switch { (StreamOptionOneofCase.Stream, CountOptionOneofCase.Count, ReadDirection.Forwards, FilterOptionOneofCase.NoFilter) => (IAsyncEnumerator <ResolvedEvent>) new Enumerators.ReadStreamForwards( _publisher, request.Options.Stream.StreamIdentifier, request.Options.Stream.ToStreamRevision(), request.Options.Count, request.Options.ResolveLinks, user, requiresLeader, context.Deadline, StreamNotFound, context.CancellationToken), (StreamOptionOneofCase.Stream, CountOptionOneofCase.Count, ReadDirection.Backwards, FilterOptionOneofCase.NoFilter) => new Enumerators.ReadStreamBackwards( _publisher, request.Options.Stream.StreamIdentifier, request.Options.Stream.ToStreamRevision(), request.Options.Count, request.Options.ResolveLinks, user, requiresLeader, context.Deadline, StreamNotFound, context.CancellationToken), (StreamOptionOneofCase.All, CountOptionOneofCase.Count, ReadDirection.Forwards, FilterOptionOneofCase.NoFilter) => new Enumerators.ReadAllForwards( _publisher, request.Options.All.ToPosition(), request.Options.Count, request.Options.ResolveLinks, user, requiresLeader, context.Deadline, context.CancellationToken), (StreamOptionOneofCase.All, CountOptionOneofCase.Count, ReadDirection.Backwards, FilterOptionOneofCase.NoFilter) => new Enumerators.ReadAllBackwards( _publisher, request.Options.All.ToPosition(), request.Options.Count, request.Options.ResolveLinks, user, requiresLeader, context.Deadline, context.CancellationToken), (StreamOptionOneofCase.Stream, CountOptionOneofCase.Subscription, ReadDirection.Forwards, FilterOptionOneofCase.NoFilter) => new Enumerators.StreamSubscription <TStreamId>( _publisher, request.Options.Stream.StreamIdentifier, request.Options.Stream.ToSubscriptionStreamRevision(), request.Options.ResolveLinks, user, requiresLeader, _readIndex, context.CancellationToken), (StreamOptionOneofCase.All, CountOptionOneofCase.Subscription, ReadDirection.Forwards, FilterOptionOneofCase.NoFilter) => new Enumerators.AllSubscription( _publisher, request.Options.All.ToSubscriptionPosition(), request.Options.ResolveLinks, user, requiresLeader, _readIndex, context.CancellationToken), (StreamOptionOneofCase.All, CountOptionOneofCase.Subscription, ReadDirection.Forwards, FilterOptionOneofCase.Filter) => new Enumerators.AllSubscriptionFiltered( _publisher, request.Options.All.ToSubscriptionPosition(), request.Options.ResolveLinks, ConvertToEventFilter(true, request.Options.Filter), user, requiresLeader, _readIndex, request.Options.Filter.WindowCase switch { ReadReq.Types.Options.Types.FilterOptions.WindowOneofCase.Count => null, ReadReq.Types.Options.Types.FilterOptions.WindowOneofCase.Max => request.Options.Filter.Max, _ => throw new InvalidOperationException() },
public override async Task Read( ReadReq request, IServerStreamWriter <ReadResp> responseStream, ServerCallContext context) { var options = request.Options; var countOptionsCase = options.CountOptionsCase; var streamOptionsCase = options.StreamOptionsCase; var readDirection = options.ReadDirection; var filterOptionsCase = options.FilterOptionsCase; var user = await GetUser(_authenticationProvider, context.RequestHeaders); await using var enumerator = (streamOptionsCase, countOptionsCase, readDirection, filterOptionsCase) switch { (StreamOptionsOneofCase.Stream, CountOptionsOneofCase.Count, ReadDirection.Forwards, FilterOptionsOneofCase.NoFilter) => (IAsyncEnumerator <ResolvedEvent>) new Enumerators.ReadStreamForwards( _queue, request.Options.Stream.StreamName, request.Options.Stream.ToStreamRevision(), request.Options.Count, request.Options.ResolveLinks, user, context.CancellationToken), (StreamOptionsOneofCase.Stream, CountOptionsOneofCase.Count, ReadDirection.Backwards, FilterOptionsOneofCase.NoFilter) => new Enumerators.ReadStreamBackwards( _queue, request.Options.Stream.StreamName, request.Options.Stream.ToStreamRevision(), request.Options.Count, request.Options.ResolveLinks, user, context.CancellationToken), (StreamOptionsOneofCase.All, CountOptionsOneofCase.Count, ReadDirection.Forwards, FilterOptionsOneofCase.NoFilter) => new Enumerators.ReadAllForwards( _queue, request.Options.All.ToPosition(), request.Options.Count, request.Options.ResolveLinks, user, context.CancellationToken), (StreamOptionsOneofCase.All, CountOptionsOneofCase.Count, ReadDirection.Forwards, FilterOptionsOneofCase.Filter) => new Enumerators.ReadAllForwardsFiltered( _queue, request.Options.All.ToPosition(), request.Options.Count, request.Options.ResolveLinks, ConvertToEventFilter(request.Options.Filter), request.Options.Filter.WindowCase switch { ReadReq.Types.Options.Types.FilterOptions.WindowOneofCase.Count => default(int?), ReadReq.Types.Options.Types.FilterOptions.WindowOneofCase.Max => request.Options.Filter.Max, _ => throw new InvalidOperationException() },