Commits FetchAllCommits() { var commits = new List <CommittedEventStream>(); var events = FetchAllEvents(); events.GroupBy(_ => _serializer.FromJsonBytes <EventMetadata>(_.Event.Metadata).Commit.Id).ForEach(commitGroup => { CommitMetadata commitMetadata = null; var commitEvents = new List <EventEnvelope>(); foreach (var @event in commitGroup) { var data = _serializer.PropertyBagFromJsonBytes(@event.Event.Data); var eventMetadata = _serializer.FromJsonBytes <EventMetadata>(@event.Event.Metadata); commitMetadata = eventMetadata.Commit; commitEvents.Add(new EventEnvelope(eventMetadata.Event, data)); } commits.Add(new CommittedEventStream( commitMetadata.Sequence, commitMetadata.Source, commitMetadata.Id, commitMetadata.CorrelationId, commitMetadata.Timestamp, new EventStream(commitEvents) )); }); return(new Commits(commits)); }
/// <inheritdoc /> public CommittedEventStream Commit(UncommittedEventStream uncommittedEvents) { while (true) { var nextSequenceNumber = GetNextCommitSequenceNumber(); var lastEventSourceVersion = GetLastEventSourceVersion(uncommittedEvents.Source.Key); if (uncommittedEvents.Source.Version.CompareTo(lastEventSourceVersion) <= 0) { throw new EventSourceConcurrencyConflict($"Current Version is {lastEventSourceVersion}, tried to commit {uncommittedEvents.Source.Version}"); } var commitMetadata = new CommitMetadata( nextSequenceNumber.Value, uncommittedEvents.Source, uncommittedEvents.Id, DateTimeOffset.UtcNow, uncommittedEvents.CorrelationId ); _connection.AppendToStreamAsync( GetStreamForCommit(), nextSequenceNumber.Version, CreateEventsFromUncomitted(uncommittedEvents, commitMetadata) ).Wait(); return(new CommittedEventStream( nextSequenceNumber.Value, uncommittedEvents.Source, uncommittedEvents.Id, uncommittedEvents.CorrelationId, uncommittedEvents.Timestamp, uncommittedEvents.Events )); } }
/// <summary> /// /// </summary> public EventMetadata(Events.EventMetadata @event, CommitMetadata commit) { Event = @event; Commit = commit; }
IEnumerable <EventData> CreateEventsFromUncomitted(UncommittedEventStream uncommittedEvents, CommitMetadata commitMetadata) { var events = new List <EventData>(); foreach (var @event in uncommittedEvents.Events) { events.Add(new EventData( @event.Id, _artifactTypeMap.GetTypeFor(@event.Metadata.Artifact).Name, true, _serializer.ToJsonBytes(@event.Event), _serializer.ToJsonBytes(new EventMetadata(@event.Metadata, commitMetadata)) )); } return(events); }