コード例 #1
0
        private async Task <DeleteResult> TombstoneInternal(TombstoneReq request,
                                                            EventStoreClientOperationOptions operationOptions, UserCredentials userCredentials,
                                                            CancellationToken cancellationToken)
        {
            var result = await _client.TombstoneAsync(request, RequestMetadata.Create(userCredentials),
                                                      deadline : DeadLine.After(operationOptions.TimeoutAfter), cancellationToken);

            return(new DeleteResult(new Position(result.Position.CommitPosition, result.Position.PreparePosition)));
        }
コード例 #2
0
        private async Task <DeleteResult> DeleteInternal(DeleteReq request, EventStoreClientOperationOptions operationOptions,
                                                         UserCredentials?userCredentials,
                                                         CancellationToken cancellationToken)
        {
            _log.LogDebug("Deleting stream {streamName}.", request.Options.StreamName);
            var result = await _client.DeleteAsync(request, RequestMetadata.Create(userCredentials ?? Settings.DefaultCredentials),
                                                   deadline : DeadLine.After(operationOptions.TimeoutAfter), cancellationToken);

            return(new DeleteResult(new Position(result.Position.CommitPosition, result.Position.PreparePosition)));
        }
コード例 #3
0
        private async Task <WriteResult> AppendToStreamInternal(
            AppendReq header,
            IEnumerable <EventData> eventData,
            EventStoreClientOperationOptions operationOptions,
            UserCredentials userCredentials,
            CancellationToken cancellationToken)
        {
            using var call = _client.Append(RequestMetadata.Create(userCredentials),
                                            deadline: DeadLine.After(operationOptions.TimeoutAfter), cancellationToken: cancellationToken);

            try {
                await call.RequestStream.WriteAsync(header).ConfigureAwait(false);

                foreach (var e in eventData)
                {
                    _log.LogTrace("Appending event to stream - {streamName}@{eventId} {eventType}.",
                                  header.Options.StreamName, e.EventId, e.Type);
                    await call.RequestStream.WriteAsync(new AppendReq {
                        ProposedMessage = new AppendReq.Types.ProposedMessage {
                            Id             = e.EventId.ToDto(),
                            Data           = ByteString.CopyFrom(e.Data.Span),
                            CustomMetadata = ByteString.CopyFrom(e.Metadata.Span),
                            Metadata       =
                            {
                                { Constants.Metadata.Type,        e.Type        },
                                { Constants.Metadata.ContentType, e.ContentType }
                            }
                        }
                    }).ConfigureAwait(false);
                }

                await call.RequestStream.CompleteAsync().ConfigureAwait(false);
            } catch (InvalidOperationException exc) {
                _log.LogTrace(exc, "Got InvalidOperationException when appending events to stream - {streamName}. This is perfectly normal if the connection was closed from the server-side.", header.Options.StreamName);
            } catch (RpcException exc) {
                _log.LogTrace(exc, "Got RpcException when appending events to stream - {streamName}. This is perfectly normal if the connection was closed from the server-side.", header.Options.StreamName);
            }

            var response = await call.ResponseAsync.ConfigureAwait(false);

            var writeResult = new WriteResult(
                response.CurrentRevisionOptionCase == AppendResp.CurrentRevisionOptionOneofCase.NoStream
                                        ? AnyStreamRevision.NoStream.ToInt64()
                                        : new StreamRevision(response.CurrentRevision).ToInt64(),
                response.PositionOptionCase == AppendResp.PositionOptionOneofCase.Position
                                        ? new Position(response.Position.CommitPosition, response.Position.PreparePosition)
                                        : default);

            _log.LogDebug("Append to stream succeeded - {streamName}@{logPosition}/{nextExpectedVersion}.",
                          header.Options.StreamName, writeResult.LogPosition, writeResult.NextExpectedVersion);

            return(writeResult);
        }
コード例 #4
0
        private async Task <WriteResult> AppendToStreamInternal(
            AppendReq header,
            IEnumerable <EventData> eventData,
            EventStoreClientOperationOptions operationOptions,
            UserCredentials userCredentials,
            CancellationToken cancellationToken)
        {
            using var call = _client.Append(RequestMetadata.Create(userCredentials),
                                            deadline: DeadLine.After(operationOptions.TimeoutAfter), cancellationToken: cancellationToken);

            await call.RequestStream.WriteAsync(header).ConfigureAwait(false);

            foreach (var e in eventData)
            {
                await call.RequestStream.WriteAsync(new AppendReq {
                    ProposedMessage = new AppendReq.Types.ProposedMessage {
                        Id             = e.EventId.ToStreamsDto(),
                        Data           = ByteString.CopyFrom(e.Data),
                        CustomMetadata = ByteString.CopyFrom(e.Metadata),
                        Metadata       =
                        {
                            { Constants.Metadata.Type,   e.Type                                 },
                            { Constants.Metadata.IsJson, e.IsJson.ToString().ToLowerInvariant() }
                        }
                    }
                }).ConfigureAwait(false);
            }

            await call.RequestStream.CompleteAsync().ConfigureAwait(false);

            var response = await call.ResponseAsync.ConfigureAwait(false);

            return(new WriteResult(
                       response.CurrentRevisionOptionCase == AppendResp.CurrentRevisionOptionOneofCase.NoStream
                                        ? AnyStreamRevision.NoStream.ToInt64()
                                        : new StreamRevision(response.CurrentRevision).ToInt64(),
                       response.PositionOptionCase == AppendResp.PositionOptionOneofCase.Position
                                        ? new Position(response.Position.CommitPosition, response.Position.PreparePosition)
                                        : default));
        }
コード例 #5
0
        private async Task <IWriteResult> AppendToStreamInternal(
            AppendReq header,
            IEnumerable <EventData> eventData,
            EventStoreClientOperationOptions operationOptions,
            UserCredentials?userCredentials,
            CancellationToken cancellationToken)
        {
            using var call = _client.Append(RequestMetadata.Create(userCredentials ?? Settings.DefaultCredentials),
                                            DeadLine.After(operationOptions.TimeoutAfter), cancellationToken);

            IWriteResult writeResult;

            try {
                await call.RequestStream.WriteAsync(header).ConfigureAwait(false);

                foreach (var e in eventData)
                {
                    _log.LogTrace("Appending event to stream - {streamName}@{eventId} {eventType}.",
                                  header.Options.StreamIdentifier, e.EventId, e.Type);
                    await call.RequestStream.WriteAsync(new AppendReq {
                        ProposedMessage = new AppendReq.Types.ProposedMessage {
                            Id             = e.EventId.ToDto(),
                            Data           = ByteString.CopyFrom(e.Data.Span),
                            CustomMetadata = ByteString.CopyFrom(e.Metadata.Span),
                            Metadata       =
                            {
                                { Constants.Metadata.Type,        e.Type        },
                                { Constants.Metadata.ContentType, e.ContentType }
                            }
                        }
                    }).ConfigureAwait(false);
                }

                await call.RequestStream.CompleteAsync().ConfigureAwait(false);
            } finally {
                var response = await call.ResponseAsync.ConfigureAwait(false);

                if (response.Success != null)
                {
                    writeResult = new SuccessResult(
                        response.Success.CurrentRevisionOptionCase ==
                        AppendResp.Types.Success.CurrentRevisionOptionOneofCase.NoStream
                                                        ? StreamState.NoStream.ToInt64()
                                                        : new StreamRevision(response.Success.CurrentRevision).ToInt64(),
                        response.Success.PositionOptionCase == AppendResp.Types.Success.PositionOptionOneofCase.Position
                                                        ? new Position(response.Success.Position.CommitPosition,
                                                                       response.Success.Position.PreparePosition)
                                                        : default);
                    _log.LogDebug("Append to stream succeeded - {streamName}@{logPosition}/{nextExpectedVersion}.",
                                  header.Options.StreamIdentifier, writeResult.LogPosition, writeResult.NextExpectedVersion);
                }
                else
                {
                    if (response.WrongExpectedVersion != null)
                    {
                        var expectedRevision = response.WrongExpectedVersion.ExpectedRevisionOptionCase switch {
                            AppendResp.Types.WrongExpectedVersion.ExpectedRevisionOptionOneofCase.Any =>
                            StreamState.Any.ToInt64(),
                            AppendResp.Types.WrongExpectedVersion.ExpectedRevisionOptionOneofCase.StreamExists =>
                            StreamState.StreamExists.ToInt64(),
                            _ => new StreamRevision(response.WrongExpectedVersion.ExpectedRevision).ToInt64()
                        };

                        var currentRevision = response.WrongExpectedVersion.CurrentRevisionOptionCase switch {
                            AppendResp.Types.WrongExpectedVersion.CurrentRevisionOptionOneofCase.NoStream =>
                            StreamState.NoStream.ToInt64(),
                            _ => new StreamRevision(response.WrongExpectedVersion.CurrentRevision).ToInt64()
                        };

                        _log.LogDebug(
                            "Append to stream failed with Wrong Expected Version - {streamName}/{expectedRevision}/{currentRevision}",
                            header.Options.StreamIdentifier, expectedRevision, currentRevision);

                        if (operationOptions.ThrowOnAppendFailure)
                        {
                            throw new WrongExpectedVersionException(header.Options.StreamIdentifier, expectedRevision,
                                                                    currentRevision);
                        }

                        writeResult = new WrongExpectedVersionResult(
                            header.Options.StreamIdentifier, expectedRevision, currentRevision);
                    }
                    else
                    {
                        throw new InvalidOperationException("The operation completed with an unexpected result.");
                    }
                }
            }

            return(writeResult);
        }