Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,38 @@ public void should_read_the_correct_events() {
Assert.AreEqual(49, _responses.Last(x => x.Event is not null).Event.Event.StreamRevision);
}

[Test]
public async Task should_have_all_positions() {
// each (non transaction) event can be looked up in the all stream using its position
var events = _responses
.Where(x => x.ContentCase == ReadResp.ContentOneofCase.Event)
.Select(x => x.Event.Event);

foreach (var evt in events) {
Assert.AreEqual(evt.CommitPosition, evt.PreparePosition);

using var call = StreamsClient.Read(new() {
Options = new() {
UuidOption = new() { Structured = new() },
Count = 1,
ReadDirection = ReadReq.Types.Options.Types.ReadDirection.Forwards,
ResolveLinks = true,
All = new() {
Position = new() {
CommitPosition = evt.CommitPosition,
PreparePosition = evt.PreparePosition,
}
},
NoFilter = new(),
}
}, GetCallOptions(AdminCredentials));

var readEvents = await call.ResponseStream.ReadAllAsync().ToArrayAsync();
Assert.AreEqual(1, readEvents.Length);
Assert.AreEqual(evt, readEvents[0].Event.Event);
}
}

[Test]
public void should_indicate_last_position_of_stream() {
var streamPosition =
Expand Down
2 changes: 2 additions & 0 deletions src/EventStore.Core/Data/EventRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public bool IsJson {
get { return (Flags & PrepareFlags.IsJson) == PrepareFlags.IsJson; }
}

public bool IsSelfCommitted => Flags.HasAnyOf(PrepareFlags.IsCommitted);

public readonly long EventNumber;
public readonly long LogPosition;
public readonly Guid CorrelationId;
Expand Down
30 changes: 23 additions & 7 deletions src/EventStore.Core/Data/ResolvedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public struct ResolvedEvent {

public readonly EventRecord Event;
public readonly EventRecord Link;
private readonly long? _originalEventCommitPosition;

public EventRecord OriginalEvent {
get { return Link ?? Event; }
Expand All @@ -13,7 +14,11 @@ public EventRecord OriginalEvent {
/// <summary>
/// Position of the OriginalEvent (unresolved link or event) if available
/// </summary>
public readonly TFPos? OriginalPosition;
public TFPos? OriginalPosition => Link is not null ? LinkPosition : EventPosition;

public TFPos? EventPosition => CalculatePosition(Event);

public TFPos? LinkPosition => CalculatePosition(Link);

public readonly ReadEventResult ResolveResult;

Expand All @@ -30,12 +35,7 @@ private ResolvedEvent(EventRecord @event, EventRecord link, long? commitPosition
ReadEventResult resolveResult = default(ReadEventResult)) {
Event = @event;
Link = link;
if (commitPosition.HasValue) {
OriginalPosition = new TFPos(commitPosition.Value, (link ?? @event).LogPosition);
} else {
OriginalPosition = null;
}

_originalEventCommitPosition = commitPosition;
ResolveResult = resolveResult;
}

Expand All @@ -55,5 +55,21 @@ public static ResolvedEvent ForFailedResolvedLink(EventRecord link, ReadEventRes
public ResolvedEvent WithoutPosition() {
return new ResolvedEvent(Event, Link, null, ResolveResult);
}

private TFPos? CalculatePosition(EventRecord @event) {
if (@event is null)
return null;

// if this is the original event and we know where it was committed
if (@event == OriginalEvent && _originalEventCommitPosition.HasValue)
return new TFPos(_originalEventCommitPosition.Value, @event.LogPosition);

// we don't know where this event was committed, unless it committed itself
if (@event.IsSelfCommitted)
return new TFPos(@event.LogPosition, @event.LogPosition);

// we don't know where this event was committed
return null;
}
}
}
8 changes: 4 additions & 4 deletions src/EventStore.Core/Services/Transport/Grpc/Enumerators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ private static ReadResp.Types.ReadEvent.Types.RecordedEvent ConvertToRecordedEve
private static ReadResp.Types.ReadEvent ConvertToReadEvent(ReadReq.Types.Options.Types.UUIDOption uuidOption,
ResolvedEvent e) {
var readEvent = new ReadResp.Types.ReadEvent {
Link = ConvertToRecordedEvent(uuidOption, e.Link, e.OriginalPosition?.CommitPosition,
e.OriginalPosition?.PreparePosition),
Event = ConvertToRecordedEvent(uuidOption, e.Event, e.OriginalPosition?.CommitPosition,
e.OriginalPosition?.PreparePosition),
Link = ConvertToRecordedEvent(uuidOption, e.Link, e.LinkPosition?.CommitPosition,
e.LinkPosition?.PreparePosition),
Event = ConvertToRecordedEvent(uuidOption, e.Event, e.EventPosition?.CommitPosition,
e.EventPosition?.PreparePosition),
};
if (e.OriginalPosition.HasValue) {
var position = Position.FromInt64(
Expand Down