private Func <CancellationToken, Task <string> > GetJsonData(MySqlStreamId streamId, int version)
        => async cancellationToken =>
        {
            using (var connection = await OpenConnection(cancellationToken))
                using (var transaction = await connection.BeginTransactionAsync(cancellationToken).ConfigureAwait(false))
                    using (var command = BuildStoredProcedureCall(
                               _schema.ReadJsonData,
                               transaction,
                               Parameters.StreamId(streamId),
                               Parameters.Version(version)))
                        using (var reader = await command
                                            .ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken)
                                            .NotOnCapturedContext())
                        {
                            if (!await reader.ReadAsync(cancellationToken).NotOnCapturedContext() || reader.IsDBNull(0))
                            {
                                return(null);
                            }

                            using (var textReader = reader.GetTextReader(0))
                            {
                                return(await textReader.ReadToEndAsync().NotOnCapturedContext());
                            }
                        }
        };
Esempio n. 2
0
        public StreamIdInfo(string idOriginal)
        {
            Ensure.That(idOriginal, nameof(idOriginal)).IsNotNullOrWhiteSpace();

            MySqlStreamId         = new MySqlStreamId(idOriginal);
            MetadataMySqlStreamId = new MySqlStreamId("$$" + idOriginal);
        }
        private async Task DeleteStreamInternal(
            MySqlStreamId streamId,
            int expectedVersion,
            MySqlTransaction transaction,
            CancellationToken cancellationToken)
        {
            var deletedStreamMessage = Deleted.CreateStreamDeletedMessage(streamId.IdOriginal);

            var deletedStreamId               = Parameters.DeletedStreamId();
            var deletedStreamIdOriginal       = Parameters.DeletedStreamIdOriginal();
            var deletedMetadataStreamId       = Parameters.DeletedMetadataStreamId();
            var deletedStreamMessageMessageId = Parameters.DeletedStreamMessageMessageId(deletedStreamMessage);
            var deletedStreamMessageType      = Parameters.DeletedStreamMessageType(deletedStreamMessage);
            var deletedStreamMessageJsonData  = Parameters.DeletedStreamMessageJsonData(deletedStreamMessage);

            using (var command = BuildStoredProcedureCall(
                       _schema.DeleteStream,
                       transaction,
                       Parameters.StreamId(streamId),
                       Parameters.ExpectedVersion(expectedVersion),
                       Parameters.CreatedUtc(_settings.GetUtcNow?.Invoke()),
                       Parameters.DeletionTrackingDisabled(_settings.DisableDeletionTracking),
                       _settings.DisableDeletionTracking ? deletedStreamId.Empty() : deletedStreamId,
                       _settings.DisableDeletionTracking ? deletedStreamIdOriginal.Empty() : deletedStreamIdOriginal,
                       _settings.DisableDeletionTracking ? deletedMetadataStreamId.Empty() : deletedMetadataStreamId,
                       _settings.DisableDeletionTracking
                    ? deletedStreamMessageMessageId.Empty()
                    : deletedStreamMessageMessageId,
                       _settings.DisableDeletionTracking ? deletedStreamMessageType.Empty() : deletedStreamMessageType,
                       _settings.DisableDeletionTracking
                    ? deletedStreamMessageJsonData.Empty()
                    : deletedStreamMessageJsonData))
            {
                try
                {
                    await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                }
                catch (MySqlException ex) when(ex.IsWrongExpectedVersion())
                {
                    await transaction.RollbackAsync(cancellationToken).ConfigureAwait(false);

                    throw new WrongExpectedVersionException(
                              ErrorMessages.DeleteStreamFailedWrongExpectedVersion(streamId.IdOriginal, expectedVersion),
                              streamId.IdOriginal,
                              expectedVersion,
                              ex);
                }
            }
        }
Esempio n. 4
0
        private async Task <ReadStreamPage> ReadStreamInternal(
            MySqlStreamId streamId,
            int start,
            int count,
            ReadDirection direction,
            bool prefetch,
            ReadNextStreamPage readNext,
            MySqlTransaction transaction,
            CancellationToken cancellationToken)
        {
            // If the count is int.MaxValue, TSql will see it as a negative number.
            // Users shouldn't be using int.MaxValue in the first place anyway.
            count = count == int.MaxValue ? count - 1 : count;

            // To read backwards from end, need to use int MaxValue
            var streamVersion = start == StreamVersion.End ? int.MaxValue : start;

            var    messages = new List <(StreamMessage message, int?maxAge)>();
            string procedure;
            Func <List <StreamMessage>, int, int> getNextVersion;

            if (direction == ReadDirection.Forward)
            {
                procedure      = prefetch ? _schema.ReadStreamForwardsWithData : _schema.ReadStreamForwards;
                getNextVersion = (events, lastVersion) => events.Any()
                    ? events.Last().StreamVersion + 1
                    : lastVersion + 1;
            }
            else
            {
                procedure      = prefetch ? _schema.ReadStreamBackwardsWithData : _schema.ReadStreamBackwards;
                getNextVersion = (events, lastVersion) => events.Any()
                    ? events.Last().StreamVersion - 1
                    : -1;
            }

            using (var command = BuildStoredProcedureCall(
                       procedure,
                       transaction,
                       Parameters.StreamId(streamId),
                       Parameters.Count(count + 1),
                       Parameters.Version(streamVersion)))
                using (var reader = await command
                                    .ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken)
                                    .NotOnCapturedContext())
                {
                    if (!reader.HasRows)
                    {
                        return(new ReadStreamPage(
                                   streamId.IdOriginal,
                                   PageReadStatus.StreamNotFound,
                                   start,
                                   -1,
                                   -1,
                                   -1,
                                   direction,
                                   true,
                                   readNext));
                    }

                    if (messages.Count == count)
                    {
                        messages.Add(default);