Ejemplo n.º 1
0
        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(
                    Authorization.Operations.Streams.Parameters.StreamId(request.Options.Stream.StreamName)),
                StreamOptionOneofCase.All => ReadOperation.WithParameter(
                    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.StreamName,
                                request.Options.Stream.ToStreamRevision(),
                                request.Options.Count,
                                request.Options.ResolveLinks,
                                user,
                                requiresLeader,
                                context.Deadline,
                                context.CancellationToken),
                            (StreamOptionOneofCase.Stream,
                             CountOptionOneofCase.Count,
                             ReadDirection.Backwards,
                             FilterOptionOneofCase.NoFilter) => new Enumerators.ReadStreamBackwards(
                                _publisher,
                                request.Options.Stream.StreamName,
                                request.Options.Stream.ToStreamRevision(),
                                request.Options.Count,
                                request.Options.ResolveLinks,
                                user,
                                requiresLeader,
                                context.Deadline,
                                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(
                                _publisher,
                                request.Options.Stream.StreamName,
                                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(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()
                },
Ejemplo n.º 2
0
        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;

            await using var enumerator =
                            (streamOptionsCase, countOptionsCase, readDirection, filterOptionsCase) switch {
                            (StreamOptionOneofCase.Stream,
                             CountOptionOneofCase.Count,
                             ReadDirection.Forwards,
                             FilterOptionOneofCase.NoFilter) => (IAsyncEnumerator <ResolvedEvent>)
                            new Enumerators.ReadStreamForwards(
                                _queue,
                                request.Options.Stream.StreamName,
                                request.Options.Stream.ToStreamRevision(),
                                request.Options.Count,
                                request.Options.ResolveLinks,
                                user,
                                context.Deadline,
                                context.CancellationToken),
                            (StreamOptionOneofCase.Stream,
                             CountOptionOneofCase.Count,
                             ReadDirection.Backwards,
                             FilterOptionOneofCase.NoFilter) => new Enumerators.ReadStreamBackwards(
                                _queue,
                                request.Options.Stream.StreamName,
                                request.Options.Stream.ToStreamRevision(),
                                request.Options.Count,
                                request.Options.ResolveLinks,
                                user,
                                context.Deadline,
                                context.CancellationToken),
                            (StreamOptionOneofCase.All,
                             CountOptionOneofCase.Count,
                             ReadDirection.Forwards,
                             FilterOptionOneofCase.NoFilter) => new Enumerators.ReadAllForwards(
                                _queue,
                                request.Options.All.ToPosition(),
                                request.Options.Count,
                                request.Options.ResolveLinks,
                                user,
                                context.Deadline,
                                context.CancellationToken),
                            (StreamOptionOneofCase.All,
                             CountOptionOneofCase.Count,
                             ReadDirection.Forwards,
                             FilterOptionOneofCase.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 => null,
                    ReadReq.Types.Options.Types.FilterOptions.WindowOneofCase.Max => request.Options.Filter.Max,
                    _ => throw new InvalidOperationException()
                },