private async Task ReadEventsAsync(BackupReader reader) { await reader.ReadEventsAsync(streamNameResolver, eventDataFormatter, async storedEvent => { await HandleEventAsync(reader, storedEvent.Stream, storedEvent.Event); }); Log($"Reading {reader.ReadEvents} events and {reader.ReadAttachments} attachments completed.", true); }
private async Task ReadEventsAsync(BackupReader reader) { await reader.ReadEventsAsync(streamNameResolver, async storedEvent => { var @event = eventDataFormatter.Parse(storedEvent.Data); await HandleEventAsync(reader, storedEvent, @event); }); Log("Reading events completed."); }
private async Task ReadEventsAsync(BackupReader reader) { await reader.ReadEventsAsync(streamNameResolver, async (storedEvent) => { var @event = eventDataFormatter.Parse(storedEvent.Data); if (@event.Payload is SquidexEvent squidexEvent) { squidexEvent.Actor = actor; } if (@event.Payload is AppCreated appCreated) { CurrentJob.AppId = appCreated.AppId.Id; if (!string.IsNullOrWhiteSpace(CurrentJob.NewAppName)) { appCreated.Name = CurrentJob.NewAppName; } } if (@event.Payload is AppEvent appEvent && !string.IsNullOrWhiteSpace(CurrentJob.NewAppName)) { appEvent.AppId = new NamedId <Guid>(appEvent.AppId.Id, CurrentJob.NewAppName); } foreach (var handler in handlers) { await handler.RestoreEventAsync(@event, CurrentJob.AppId, reader, actor); } var eventData = eventDataFormatter.ToEventData(@event, @event.Headers.CommitId()); var eventCommit = new List <EventData> { eventData }; await eventStore.AppendAsync(Guid.NewGuid(), storedEvent.StreamName, eventCommit); Log($"Read {reader.ReadEvents} events and {reader.ReadAttachments} attachments.", true); }); Log("Reading events completed."); }
public async Task Should_write_and_read_events_to_backup(BackupVersion version) { var stream = new MemoryStream(); var random = new Random(); var randomGuids = new List <Guid>(); for (var i = 0; i < 100; i++) { randomGuids.Add(Guid.NewGuid()); } Guid RandomGuid() { return(randomGuids[random.Next(randomGuids.Count)]); } var sourceEvents = new List <(string Stream, Envelope <IEvent> Event)>(); for (var i = 0; i < 200; i++) { var @event = new MyEvent { GuidNamed = NamedId.Of(RandomGuid(), $"name{i}"), GuidRaw = RandomGuid(), Values = new Dictionary <Guid, string> { [RandomGuid()] = "Key" } }; var envelope = Envelope.Create <IEvent>(@event); envelope.Headers.Add(RandomGuid().ToString(), i); envelope.Headers.Add("Id", RandomGuid().ToString()); envelope.Headers.Add("Index", i); sourceEvents.Add(($"My-{RandomGuid()}", envelope)); } using (var writer = new BackupWriter(serializer, stream, true, version)) { foreach (var(_, envelope) in sourceEvents) { var eventData = formatter.ToEventData(envelope, Guid.NewGuid(), true); var eventStored = new StoredEvent("S", "1", 2, eventData); var index = int.Parse(envelope.Headers["Index"].ToString()); if (index % 17 == 0) { await writer.WriteBlobAsync(index.ToString(), innerStream => { innerStream.WriteByte((byte)index); return(TaskHelper.Done); }); } else if (index % 37 == 0) { await writer.WriteJsonAsync(index.ToString(), $"JSON_{index}"); } writer.WriteEvent(eventStored); } } stream.Position = 0; var targetEvents = new List <(string Stream, Envelope <IEvent> Event)>(); using (var reader = new BackupReader(serializer, stream)) { await reader.ReadEventsAsync(streamNameResolver, formatter, async @event => { var index = int.Parse(@event.Event.Headers["Index"].ToString()); if (index % 17 == 0) { await reader.ReadBlobAsync(index.ToString(), innerStream => { var byteRead = innerStream.ReadByte(); Assert.Equal((byte)index, byteRead); return(TaskHelper.Done); }); } else if (index % 37 == 0) { var json = await reader.ReadJsonAttachmentAsync <string>(index.ToString()); Assert.Equal($"JSON_{index}", json); } targetEvents.Add(@event); }); void CompareGuid(Guid source, Guid target) { Assert.Equal(source, reader.OldGuid(target)); Assert.NotEqual(source, target); } for (var i = 0; i < targetEvents.Count; i++) { var target = targetEvents[i].Event.To <MyEvent>(); var source = sourceEvents[i].Event.To <MyEvent>(); CompareGuid(source.Payload.Values.First().Key, target.Payload.Values.First().Key); CompareGuid(source.Payload.GuidRaw, target.Payload.GuidRaw); CompareGuid(source.Payload.GuidNamed.Id, target.Payload.GuidNamed.Id); CompareGuid(source.Headers.GetGuid("Id"), target.Headers.GetGuid("Id")); Assert.Equal(Guid.Empty, target.Payload.GuidEmpty); } } }
public async Task Should_write_and_read_events() { var stream = new MemoryStream(); var sourceEvents = new List <StoredEvent>(); using (var writer = new BackupWriter(stream, true)) { for (var i = 0; i < 1000; i++) { var eventData = new EventData { Type = i.ToString(), Metadata = i, Payload = i }; var eventStored = new StoredEvent("S", "1", 2, eventData); if (i % 17 == 0) { await writer.WriteBlobAsync(eventData.Type, innerStream => { innerStream.WriteByte((byte)i); return(TaskHelper.Done); }); } else if (i % 37 == 0) { await writer.WriteJsonAsync(eventData.Type, $"JSON_{i}"); } writer.WriteEvent(eventStored); sourceEvents.Add(eventStored); } } stream.Position = 0; var readEvents = new List <StoredEvent>(); using (var reader = new BackupReader(stream)) { await reader.ReadEventsAsync(streamNameResolver, async @event => { var i = int.Parse(@event.Data.Type); if (i % 17 == 0) { await reader.ReadBlobAsync(@event.Data.Type, innerStream => { var b = innerStream.ReadByte(); Assert.Equal((byte)i, b); return(TaskHelper.Done); }); } else if (i % 37 == 0) { var j = await reader.ReadJsonAttachmentAsync(@event.Data.Type); Assert.Equal($"JSON_{i}", j.ToString()); } readEvents.Add(@event); }); } var sourceEventsWithNewStreamName = sourceEvents.Select(x => new StoredEvent(streamNameResolver.WithNewId(x.StreamName, null), x.EventPosition, x.EventStreamNumber, x.Data)).ToList(); readEvents.Should().BeEquivalentTo(sourceEventsWithNewStreamName); }