Exemple #1
0
            private void ReadPage(Position startPosition, ulong readCount = 0)
            {
                var correlationId = Guid.NewGuid();

                var(commitPosition, preparePosition) = startPosition.ToInt64();

                _bus.Publish(new ClientMessage.FilteredReadAllEventsBackward(
                                 correlationId, correlationId, new ContinuationEnvelope(OnMessage, _semaphore, _cancellationToken),
                                 commitPosition, preparePosition, (int)Math.Min(ReadBatchSize, _maxCount), _resolveLinks,
                                 _requiresLeader, (int)_maxSearchWindow, null, _eventFilter, _user, expires: _deadline));

                async Task OnMessage(Message message, CancellationToken ct)
                {
                    if (message is ClientMessage.NotHandled notHandled &&
                        RpcExceptions.TryHandleNotHandled(notHandled, out var ex))
                    {
                        _channel.Writer.TryComplete(ex);
                        return;
                    }

                    if (!(message is ClientMessage.FilteredReadAllEventsBackwardCompleted completed))
                    {
                        _channel.Writer.TryComplete(
                            RpcExceptions.UnknownMessage <ClientMessage.FilteredReadAllEventsBackwardCompleted>(message));
                        return;
                    }

                    switch (completed.Result)
                    {
                    case FilteredReadAllResult.Success:
                        var nextPosition = completed.NextPos;

                        foreach (var @event in completed.Events)
                        {
                            if (readCount >= _maxCount)
                            {
                                await _channel.Writer.WriteAsync(new ReadResp {
                                    AllStreamPosition = new () {
                                        NextPosition = new() {
                                            CommitPosition  = (ulong)nextPosition.CommitPosition,
                                            PreparePosition = (ulong)nextPosition.PreparePosition
                                        },
                                        LastPosition = new() {
                                            CommitPosition  = (ulong)completed.CurrentPos.CommitPosition,
                                            PreparePosition = (ulong)completed.CurrentPos.PreparePosition
                                        }
                                    }
                                }, ct).ConfigureAwait(false);

                                _channel.Writer.TryComplete();
                                return;
                            }
                            await _channel.Writer.WriteAsync(new ReadResp {
                                Event = ConvertToReadEvent(_uuidOption, @event)
                            }, _cancellationToken).ConfigureAwait(false);

                            nextPosition = @event.OriginalPosition ?? TFPos.Invalid;
                            readCount++;
                        }
Exemple #2
0
            private void ReadPage(Position startPosition)
            {
                var correlationId = Guid.NewGuid();

                var(commitPosition, preparePosition) = startPosition.ToInt64();

                _bus.Publish(new ClientMessage.ReadAllEventsForward(
                                 correlationId, correlationId, new ContinuationEnvelope(OnMessage, _semaphore, _cancellationToken),
                                 commitPosition, preparePosition, (int)Math.Min(ReadBatchSize, _maxCount), _resolveLinks,
                                 _requiresLeader, default, _user, expires: _deadline));
Exemple #3
0
            private void ReadPage(Position position, Func <Message, CancellationToken, Task> onMessage)
            {
                Guid correlationId = Guid.NewGuid();

                Log.Verbose(
                    "Subscription {subscriptionId} to $all reading next page starting from {nextRevision}.",
                    _subscriptionId, position);

                var(commitPosition, preparePosition) = position.ToInt64();

                _bus.Publish(new ClientMessage.ReadAllEventsForward(
                                 correlationId, correlationId, new ContinuationEnvelope(onMessage, _semaphore, _cancellationToken),
                                 commitPosition, preparePosition, ReadBatchSize, _resolveLinks, _requiresLeader, default,
            private void ReadPage(Position startPosition, ulong readCount = 0)
            {
                var correlationId = Guid.NewGuid();

                var(commitPosition, preparePosition) = startPosition.ToInt64();

                _bus.Publish(new ClientMessage.FilteredReadAllEventsForward(
                                 correlationId, correlationId, new ContinuationEnvelope(OnMessage, _semaphore, _cancellationToken),
                                 commitPosition, preparePosition, (int)Math.Min(ReadBatchSize, _maxCount), _resolveLinks,
                                 _requiresLeader, (int)_maxSearchWindow, null, _eventFilter, _user, expires: _deadline));

                async Task OnMessage(Message message, CancellationToken ct)
                {
                    if (message is ClientMessage.NotHandled notHandled &&
                        RpcExceptions.TryHandleNotHandled(notHandled, out var ex))
                    {
                        _channel.Writer.TryComplete(ex);
                        return;
                    }

                    if (!(message is ClientMessage.FilteredReadAllEventsForwardCompleted completed))
                    {
                        _channel.Writer.TryComplete(
                            RpcExceptions.UnknownMessage <ClientMessage.FilteredReadAllEventsForwardCompleted>(message));
                        return;
                    }

                    switch (completed.Result)
                    {
                    case FilteredReadAllResult.Success:
                        foreach (var @event in completed.Events)
                        {
                            if (readCount >= _maxCount)
                            {
                                _channel.Writer.TryComplete();
                                return;
                            }
                            await _channel.Writer.WriteAsync(new ReadResp {
                                Event = ConvertToReadEvent(_uuidOption, @event)
                            }, _cancellationToken).ConfigureAwait(false);

                            readCount++;
                        }

                        if (completed.IsEndOfStream)
                        {
                            _channel.Writer.TryComplete();
                            return;
                        }

                        ReadPage(Position.FromInt64(
                                     completed.NextPos.CommitPosition,
                                     completed.NextPos.PreparePosition), readCount);
                        return;

                    case FilteredReadAllResult.AccessDenied:
                        _channel.Writer.TryComplete(RpcExceptions.AccessDenied());
                        return;

                    default:
                        _channel.Writer.TryComplete(RpcExceptions.UnknownError(completed.Result));
                        return;
                    }
                }
            }