Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizing equally timestamps check #346

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/Elders.Cronus/EventStore/AggregateRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Elders.Cronus.AtomicAction;
Expand Down Expand Up @@ -72,13 +73,12 @@ internal async Task<AggregateCommit> SaveInternalAsync<AR>(AR aggregateRoot) whe
return default;
}

DateTimeOffset lastTimestamp = default;
foreach (var @event in aggregateRoot.UncommittedEvents)
{
if (@event.Timestamp == default)
throw new InvalidOperationException("Cannot use default timestamp for an aggregate event.");
IEnumerable<IGrouping<DateTimeOffset, DateTimeOffset>> duplicateTimestamps = aggregateRoot.UncommittedEvents
.Select(e => e.Timestamp)
.GroupBy(t => t)
.Where(g => g.Count() > 1);

if (lastTimestamp == @event.Timestamp)
if (duplicateTimestamps != null)
{
var errorMessage = "There are multiple events with the same timestamp within the same aggregate commit. Loss of data may occur!";
#if DEBUG
Expand All @@ -88,8 +88,7 @@ internal async Task<AggregateCommit> SaveInternalAsync<AR>(AR aggregateRoot) whe
#endif
}

lastTimestamp = @event.Timestamp;
}


var arCommit = new AggregateCommit(aggregateRoot.State.Id.RawId, aggregateRoot.Revision, aggregateRoot.UncommittedEvents.ToList(), aggregateRoot.UncommittedPublicEvents.ToList(), DateTime.UtcNow.ToFileTimeUtc());
arCommit = await aggregateInterceptor.OnAppendingAsync(arCommit).ConfigureAwait(false);
Expand Down
Loading