예제 #1
0
        public async Task <EventRecord[]> LoadAsync(Guid streamId, int fromVersion, bool pessimisticLock, CancellationToken cancellationToken = new CancellationToken())
        {
            List <EventRecord> list = null;

            using (var cnn = new SqlConnection(_connectionString))
            {
                using (var cmd = await cnn.OpenAndCreateCommandAsync(SqlQueries.SelectEvents.Query, cancellationToken).NotOnCapturedContext())
                {
                    cmd
                    .AddInputParam(SqlQueries.SelectEvents.ParamStreamId, DbType.Guid, streamId)
                    .AddInputParam(SqlQueries.SelectEvents.ParamFromVersion, DbType.Int32, fromVersion)
                    .AddInputParam(SqlQueries.SelectEvents.ParamPessimisticLock, DbType.Boolean, pessimisticLock);

                    using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken).NotOnCapturedContext())
                    {
                        if (reader.HasRows)
                        {
                            list = new List <EventRecord>(100);
                        }

                        while (await reader.ReadAsync(cancellationToken).NotOnCapturedContext()) // read snapshot
                        {
                            if (await reader.IsDBNullAsync(colIndexForContractName, cancellationToken).NotOnCapturedContext())
                            {
                                break;
                            }

                            list.Add(EventRecord.Snapshot(
                                         await reader.GetFieldValueAsync <string>(colIndexForContractName, cancellationToken).NotOnCapturedContext(),
                                         await reader.GetFieldValueAsync <int>(colIndexForVersion, cancellationToken).NotOnCapturedContext(),
                                         await reader.GetFieldValueAsync <byte[]>(colIndexForPayload, cancellationToken).NotOnCapturedContext()));
                        }

                        await reader.NextResultAsync(cancellationToken).NotOnCapturedContext();

                        if (list == null && reader.HasRows)
                        {
                            list = new List <EventRecord>(30);
                        }

                        while (await reader.ReadAsync(cancellationToken).NotOnCapturedContext()) // read events
                        {
                            list.Add(EventRecord.Event(
                                         await reader.GetFieldValueAsync <string>(colIndexForContractName, cancellationToken).NotOnCapturedContext(),
                                         await reader.GetFieldValueAsync <int>(colIndexForVersion, cancellationToken).NotOnCapturedContext(),
                                         await reader.GetFieldValueAsync <byte[]>(colIndexForPayload, cancellationToken).NotOnCapturedContext()));
                        }
                    }
                }
            }
            return(list?.ToArray() ?? new EventRecord[0]);
        }
예제 #2
0
 private static EventRecord CreateEventObject(InMemoryEventRecord arg)
 {
     return(EventRecord.Event(arg.ContractName, arg.Version, arg.EventData));
 }
예제 #3
0
        public EventRecord[] Load(Guid streamId, int fromVersion, bool pessimisticLock)
        {
            List <EventRecord> list = null;

            try
            {
                using (var cnn = new SqlConnection(_connectionString))
                {
                    using (var cmd = cnn.OpenAndCreateCommand(SqlQueries.SelectEvents.Query))
                    {
                        cmd
                        .AddInputParam(SqlQueries.SelectEvents.ParamStreamId, DbType.Guid, streamId)
                        .AddInputParam(SqlQueries.SelectEvents.ParamFromVersion, DbType.Int32, fromVersion)
                        .AddInputParam(SqlQueries.SelectEvents.ParamPessimisticLock, DbType.Boolean, pessimisticLock);

                        using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                        {
                            if (reader.HasRows)
                            {
                                list = new List <EventRecord>(100);
                            }

                            while (reader.Read()) // read snapshot
                            {
                                if (reader[0] == DBNull.Value)
                                {
                                    break;
                                }

                                list.Add(EventRecord.Snapshot(
                                             reader.GetString(colIndexForContractName),
                                             reader.GetInt32(colIndexForVersion),
                                             GetBytes(reader)));
                            }

                            reader.NextResult();

                            if (list == null && reader.HasRows)
                            {
                                list = new List <EventRecord>(30);
                            }

                            while (reader.Read()) // read events
                            {
                                list.Add(EventRecord.Event(
                                             reader.GetString(colIndexForContractName),
                                             reader.GetInt32(colIndexForVersion),
                                             GetBytes(reader)));
                            }
                        }
                    }
                }
            }
            catch (SqlException ex)
            {
                if (ex.IsStreamNotLockable())
                {
                    throw new InvalidOperationException($"Can not load and lock stream {streamId} because it is not created with param 'isLockable' set to true.", ex);
                }
                throw;
            }
            return(list?.ToArray() ?? new EventRecord[0]);
        }