private async Task <List <Event> > GetAllEventsFromStream(string streamName, long start = StreamPosition.Start)
        {
            var results = new List <Event>();

            StreamEventsSlice currentSlice;

            long nextSliceStart = start;
            var  sliceSize      = 200;

            using (var connection = _store.Connect())
            {
                do
                {
                    _logger.Debug($"Getting event slice {nextSliceStart}-{sliceSize} from stream '{streamName}' from the event store.");

                    currentSlice = await connection.ReadStreamEventsForwardAsync(streamName, nextSliceStart, sliceSize, true);

                    nextSliceStart = currentSlice.NextEventNumber;

                    var items = currentSlice.Events.Select(e =>
                    {
                        var json = Encoding.ASCII.GetString(e.Event.Data);

                        if (!_eventDeserializers.ContainsKey(e.Event.EventType))
                        {
                            throw new InvalidOperationException($"No event serializer registered for event of type {e.Event.EventType}");
                        }

                        var obj = _eventDeserializers[e.Event.EventType](json);

                        var metaDataJson = Encoding.ASCII.GetString(e.Event.Metadata);
                        var metaDataObj  = JsonConvert.DeserializeObject(metaDataJson);
                        var metaData     = EventMetadata.FromObject(metaDataObj);

                        _logger.DebugWithContext($"Deserializing metadata for event {e.Event.EventId}", metaDataJson);
                        _logger.TraceWithContext($"Deserialized metadata for event {e.Event.EventId}", metaData);

                        var num = e.IsResolved
                            ? e.Link.EventNumber
                            : e.Event.EventNumber;

                        obj.SetVersion(num);
                        obj.SetMetadata(metaData);

                        return(obj);
                    });

                    results.AddRange(items);
                } while (!currentSlice.IsEndOfStream);
            }

            return(results);
        }
Пример #2
0
        private async Task <List <EventModel <TId> > > GetAllEventsFromStream(ActionContext context, IEventStoreConnection connection, string streamName, long start = StreamPosition.Start)
        {
            StreamEventsSlice currentSlice;

            long nextSliceStart = start;
            var  sliceSize      = 200;

            var data = new List <UnprocessedEventData>();

            do
            {
                context.Logger.Trace($"Getting event slice {nextSliceStart}-{sliceSize} from stream '{streamName}' from the event store.");

                currentSlice = await connection.ReadStreamEventsForwardAsync(streamName, nextSliceStart, sliceSize, true);

                nextSliceStart = currentSlice.NextEventNumber;

                var jsonData = currentSlice.Events.Select(e =>
                {
                    var type = e.Event.EventType;

                    var bodyJson = Encoding.ASCII.GetString(e.Event.Data);

                    var metadataJson = Encoding.ASCII.GetString(e.Event.Metadata);

                    var position = e.IsResolved
                        ? e.Link.EventNumber
                        : e.Event.EventNumber;

                    return(new UnprocessedEventData
                    {
                        Id = e.Event.EventId,
                        Type = type,
                        BodyJson = bodyJson,
                        MetaJson = metadataJson,
                        Position = position,
                        Version = e.Event.EventNumber
                    });
                });

                data.AddRange(jsonData);
            } while (!currentSlice.IsEndOfStream);

            if (data.Any())
            {
                context.Logger.Information($"{data.Count()} events found since index {start}!");
            }

            var items = data.Select(e =>
            {
                context.State.SetParam("eventToDeserialize", e);

                if (!_eventDeserializers.ContainsKey(e.Type))
                {
                    throw new InvalidOperationException($"No event deserializer registered for event of type {e.Type}");
                }

                var metaDataObj = JsonConvert.DeserializeObject(e.MetaJson);
                var metaData    = EventMetadata.FromObject(metaDataObj);

                var model = _eventDeserializers[e.Type](e.BodyJson, metaData, e.Position, e.Version);

                return(model);
            }).ToList();

            context.State.RemoveParam("eventToDeserialize");

            context.State.SetParam("events", items);

            return(items.ToList());
        }