Skip to content

Commit c9ed2c1

Browse files
authored
refactor: move ExpectedRevision to StreamState. (#337)
1 parent 8437642 commit c9ed2c1

File tree

65 files changed

+409
-1013
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+409
-1013
lines changed

samples/appending-events/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ await client.AppendToStreamAsync(
102102
static async Task AppendWithConcurrencyCheck(KurrentDBClient client) {
103103
await client.AppendToStreamAsync(
104104
"concurrency-stream",
105-
StreamRevision.None,
105+
StreamState.Any,
106106
new[] { new EventData(Uuid.NewUuid(), "-", ReadOnlyMemory<byte>.Empty) }
107107
);
108108

samples/server-side-filtering/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
await client.AppendToStreamAsync(
4040
Guid.NewGuid().ToString("N"),
41-
StreamRevision.None,
41+
StreamState.Any,
4242
new List<EventData> { eventData }
4343
);
4444
}

src/KurrentDB.Client/Core/Common/MetadataExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public static bool TryGetValue(this Metadata metadata, string key, out string? v
1717
return false;
1818
}
1919

20-
public static StreamRevision GetStreamRevision(this Metadata metadata, string key) =>
20+
public static StreamState GetStreamState(this Metadata metadata, string key) =>
2121
metadata.TryGetValue(key, out var s) && ulong.TryParse(s, out var value)
22-
? new(value)
23-
: StreamRevision.None;
22+
? value
23+
: StreamState.NoStream;
2424

2525
public static int GetIntValueOrDefault(this Metadata metadata, string key) =>
2626
metadata.TryGetValue(key, out var s) && int.TryParse(s, out var value)

src/KurrentDB.Client/Core/Exceptions/WrongExpectedVersionException.cs

+9-26
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,26 @@ public class WrongExpectedVersionException : Exception {
2424
/// <summary>
2525
/// The current <see cref="StreamRevision" /> of the stream that the operation was attempted on.
2626
/// </summary>
27-
public StreamRevision ActualStreamRevision { get; }
27+
public StreamState ActualStreamState { get; }
2828

2929
/// <summary>
3030
/// If available, the expected version specified for the operation that failed.
3131
/// </summary>
32-
public StreamRevision ExpectedStreamRevision { get; }
32+
public StreamState ExpectedStreamState { get; }
3333

3434
/// <summary>
3535
/// Constructs a new instance of <see cref="WrongExpectedVersionException" /> with the expected and actual versions if available.
3636
/// </summary>
37-
public WrongExpectedVersionException(string streamName, StreamRevision expectedStreamRevision,
38-
StreamRevision actualStreamRevision, Exception? exception = null, string? message = null) :
37+
public WrongExpectedVersionException(string streamName, StreamState expectedStreamState,
38+
StreamState actualStreamState, Exception? exception = null, string? message = null) :
3939
base(
40-
message ?? $"Append failed due to WrongExpectedVersion. Stream: {streamName}, Expected version: {expectedStreamRevision}, Actual version: {actualStreamRevision}",
40+
message ?? $"Append failed due to WrongExpectedVersion. Stream: {streamName}, Expected version: {expectedStreamState}, Actual version: {actualStreamState}",
4141
exception) {
4242
StreamName = streamName;
43-
ActualStreamRevision = actualStreamRevision;
44-
ExpectedStreamRevision = expectedStreamRevision;
45-
ExpectedVersion = expectedStreamRevision == StreamRevision.None ? new long?() : expectedStreamRevision.ToInt64();
46-
ActualVersion = actualStreamRevision == StreamRevision.None ? new long?() : actualStreamRevision.ToInt64();
47-
}
48-
49-
/// <summary>
50-
/// Constructs a new instance of <see cref="WrongExpectedVersionException" /> with the expected and actual versions if available.
51-
/// </summary>
52-
/// <param name="streamName"></param>
53-
/// <param name="expectedStreamState"></param>
54-
/// <param name="actualStreamRevision"></param>
55-
/// <param name="exception"></param>
56-
public WrongExpectedVersionException(string streamName, StreamState expectedStreamState,
57-
StreamRevision actualStreamRevision, Exception? exception = null) : base(
58-
$"Append failed due to WrongExpectedVersion. Stream: {streamName}, Expected state: {expectedStreamState}, Actual version: {actualStreamRevision}",
59-
exception) {
60-
StreamName = streamName;
61-
ActualStreamRevision = actualStreamRevision;
62-
ActualVersion = actualStreamRevision == StreamRevision.None ? new long?() : actualStreamRevision.ToInt64();
63-
ExpectedStreamRevision = StreamRevision.None;
43+
ActualStreamState = actualStreamState;
44+
ExpectedStreamState = expectedStreamState;
45+
ExpectedVersion = expectedStreamState.ToInt64();
46+
ActualVersion = actualStreamState.ToInt64();
6447
}
6548
}
6649
}

src/KurrentDB.Client/Core/StreamPosition.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public static StreamPosition FromInt64(long value) =>
3030
/// </summary>
3131
/// <param name="revision"></param>
3232
/// <returns></returns>
33-
public static StreamPosition FromStreamRevision(StreamRevision revision) => revision.ToUInt64() switch {
33+
public static StreamPosition FromStreamRevision(ulong revision) => revision switch {
3434
ulong.MaxValue => throw new ArgumentOutOfRangeException(nameof(revision)),
35-
_ => new StreamPosition(revision.ToUInt64())
35+
_ => new StreamPosition(revision)
3636
};
3737

3838
/// <summary>

src/KurrentDB.Client/Core/StreamRevision.cs

-196
This file was deleted.

src/KurrentDB.Client/Core/StreamState.cs

+16-12
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,29 @@ namespace KurrentDB.Client {
2020
/// </summary>
2121
public static readonly StreamState StreamExists = new StreamState(Constants.StreamExists);
2222

23-
private readonly int _value;
23+
public static StreamState StreamRevision(ulong value) => new StreamState((long)value);
24+
25+
private readonly long _value;
2426

2527
private static class Constants {
26-
public const int NoStream = 1;
27-
public const int Any = 2;
28-
public const int StreamExists = 4;
28+
public const int NoStream = -1;
29+
public const int Any = -2;
30+
public const int StreamExists = -4;
2931
}
3032

31-
internal StreamState(int value) {
33+
internal StreamState(long value) {
3234
switch (value) {
3335
case Constants.NoStream:
3436
case Constants.Any:
3537
case Constants.StreamExists:
3638
_value = value;
3739
return;
3840
default:
39-
throw new ArgumentOutOfRangeException(nameof(value));
41+
if (value < 0)
42+
throw new ArgumentOutOfRangeException(nameof(value));
43+
44+
_value = value;
45+
return;
4046
}
4147
}
4248

@@ -69,13 +75,11 @@ internal StreamState(int value) {
6975
/// Converts the <see cref="StreamState"/> to a <see cref="long"/>. It is not meant to be used directly from your code.
7076
/// </summary>
7177
/// <returns></returns>
72-
public long ToInt64() => -Convert.ToInt64(_value);
78+
public long ToInt64() => _value;
7379

74-
/// <summary>
75-
/// Converts the <see cref="StreamState"/> to an <see cref="int"/>. It is not meant to be used directly from your code.
76-
/// </summary>
77-
/// <returns></returns>
78-
public static implicit operator int(StreamState streamState) => streamState._value;
80+
public bool HasPosition => _value >= 0;
81+
82+
public static implicit operator StreamState(ulong value) => new StreamState((long)value);
7983

8084
/// <inheritdoc />
8185
public override string ToString() => _value switch {

src/KurrentDB.Client/KurrentDB.Client.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0"/>
1414
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="9.0.0"/>
1515
<PackageReference Include="System.Linq.Async" Version="6.0.1"/>
16-
<PackageReference Include="OpenTelemetry.Api" Version="1.10.0" />
16+
<PackageReference Include="OpenTelemetry.Api" Version="1.11.2" />
1717

1818
<PackageReference Include="Grpc.Tools" Version="2.68.1">
1919
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)