Skip to content

Commit 6028834

Browse files
committed
Removed state boolean from effect store implementations
1 parent 5783bf9 commit 6028834

File tree

14 files changed

+128
-134
lines changed

14 files changed

+128
-134
lines changed

Core/Cleipnir.ResilientFunctions.Tests/TestTemplates/EffectStoreTests.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ await store
4545
await store.SetEffectResult(functionId, storedEffect2);
4646
storedEffects = await store.GetEffectResults(functionId);
4747
storedEffects.Count.ShouldBe(2);
48-
storedEffects[0].ShouldBe(storedEffect1);
49-
storedEffects[1].ShouldBe(storedEffect2);
48+
storedEffects.Any(s => s == storedEffect1).ShouldBeTrue();
49+
storedEffects.Any(s => s == storedEffect2).ShouldBeTrue();
5050

5151
await store.SetEffectResult(functionId, storedEffect2);
5252
await store.GetEffectResults(functionId);
5353

5454
await store.SetEffectResult(functionId, storedEffect2);
5555
storedEffects = await store.GetEffectResults(functionId);
5656
storedEffects.Count.ShouldBe(2);
57-
storedEffects[0].ShouldBe(storedEffect1);
58-
storedEffects[1].ShouldBe(storedEffect2);
57+
storedEffects.Any(s => s == storedEffect1).ShouldBeTrue();
58+
storedEffects.Any(s => s == storedEffect2).ShouldBeTrue();
5959
}
6060

6161
public abstract Task SingleEffectWithResultLifeCycle();
@@ -149,17 +149,17 @@ await store
149149
.SelectAsync(sas => sas.Count() == 2)
150150
.ShouldBeTrueAsync();
151151

152-
await store.DeleteEffectResult(functionId, storedEffect2.EffectId.ToStoredEffectId(), isState: false);
152+
await store.DeleteEffectResult(functionId, storedEffect2.EffectId.ToStoredEffectId());
153153
var storedEffects = await store.GetEffectResults(functionId);
154154
storedEffects.Count.ShouldBe(1);
155155
storedEffects[0].EffectId.ShouldBe(storedEffect1.EffectId);
156156

157-
await store.DeleteEffectResult(functionId, storedEffect2.EffectId.ToStoredEffectId(), isState: false);
157+
await store.DeleteEffectResult(functionId, storedEffect2.EffectId.ToStoredEffectId());
158158
storedEffects = await store.GetEffectResults(functionId);
159159
storedEffects.Count.ShouldBe(1);
160160
storedEffects[0].EffectId.ShouldBe(storedEffect1.EffectId);
161161

162-
await store.DeleteEffectResult(functionId, storedEffect1.EffectId.ToStoredEffectId(), isState: false);
162+
await store.DeleteEffectResult(functionId, storedEffect1.EffectId.ToStoredEffectId());
163163
await store
164164
.GetEffectResults(functionId)
165165
.SelectAsync(sas => sas.Any())
@@ -268,15 +268,14 @@ protected async Task BulkInsertTest(Task<IEffectsStore> storeTask)
268268
Result: "some result 2".ToUtf8Bytes(),
269269
StoredException: null
270270
);
271-
272-
271+
273272
await store.SetEffectResults(storedId, [storedEffect1, storedEffect2]);
274273

275274
var effects = await store.GetEffectResults(storedId);
276275
effects.Count.ShouldBe(2);
277-
effects[0].EffectId.ShouldBe(storedEffect1.EffectId);
278-
effects[0].Result.ShouldBe("some result 1".ToUtf8Bytes());
279-
effects[1].EffectId.ShouldBe(storedEffect2.EffectId);
280-
effects[1].Result.ShouldBe("some result 2".ToUtf8Bytes());
276+
var effect1 = effects.Single(e => e.EffectId == storedEffect1.EffectId);
277+
effect1.Result.ShouldBe("some result 1".ToUtf8Bytes());
278+
var effect2 = effects.Single(e => e.EffectId == storedEffect2.EffectId);
279+
effect2.Result.ShouldBe("some result 2".ToUtf8Bytes());
281280
}
282281
}

Core/Cleipnir.ResilientFunctions.Tests/TestTemplates/StatesStoreTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ await statesStore.SetEffectResult(
3737
var state2 = states.Single(s => s.EffectId == "Id#2".ToEffectId(isState: true));
3838
state2.Result.ShouldBe("SomeJson#2".ToUtf8Bytes());
3939

40-
await statesStore.DeleteEffectResult(flowId, state1.EffectId.ToStoredEffectId(), isState: true);
40+
await statesStore.DeleteEffectResult(flowId, state1.EffectId.ToStoredEffectId());
4141

4242
states = await statesStore.GetEffectResults(flowId);
4343
states.Count.ShouldBe(1);

Core/Cleipnir.ResilientFunctions.Tests/TestTemplates/WatchDogsTests/CrashableEffectStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public Task<IReadOnlyList<StoredEffect>> GetEffectResults(StoredId storedId)
4040
? Task.FromException<IReadOnlyList<StoredEffect>>(new TimeoutException())
4141
: _inner.GetEffectResults(storedId);
4242

43-
public Task DeleteEffectResult(StoredId storedId, StoredEffectId effectId, bool isState)
43+
public Task DeleteEffectResult(StoredId storedId, StoredEffectId effectId)
4444
=> _crashed
4545
? Task.FromException(new TimeoutException())
46-
: _inner.DeleteEffectResult(storedId, effectId, isState);
46+
: _inner.DeleteEffectResult(storedId, effectId);
4747

4848
public Task Remove(StoredId storedId)
4949
=> _crashed

Core/Cleipnir.ResilientFunctions/Domain/Effect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public async Task Clear(string id)
290290
if (!effectResults.ContainsKey(effectId))
291291
return;
292292

293-
await effectsStore.DeleteEffectResult(storedId, id.ToStoredEffectId(isState: false), isState: false);
293+
await effectsStore.DeleteEffectResult(storedId, id.ToStoredEffectId(isState: false));
294294
lock (_sync)
295295
effectResults.Remove(effectId);
296296
}

Core/Cleipnir.ResilientFunctions/Domain/EffectId.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
namespace Cleipnir.ResilientFunctions.Domain;
22

3-
public record EffectId(string Value, bool IsState);
3+
public record EffectId(string Value, bool IsState)
4+
{
5+
public string Serialize() => IsState ? $"S!{Value}" : $"E!{Value}" ;
6+
7+
public static EffectId Deserialize(string s)
8+
{
9+
var suffix = s[2..];
10+
var isState = s[0] == 'S';
11+
12+
return new EffectId(suffix, isState);
13+
}
14+
}
415

516
public static class EffectIdExtensions
617
{

Core/Cleipnir.ResilientFunctions/Domain/ExistingEffects.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public async Task<WorkStatus> GetStatus(EffectId effectId)
4747
public async Task Remove(string effectId)
4848
{
4949
var storedEffects = await GetStoredEffects();
50-
await effectsStore.DeleteEffectResult(storedId, effectId.ToStoredEffectId(isState: false), isState: false);
50+
await effectsStore.DeleteEffectResult(storedId, effectId.ToStoredEffectId(isState: false));
5151
storedEffects.Remove(new EffectId(effectId, IsState: false));
5252
}
5353

@@ -61,14 +61,14 @@ private async Task Set(StoredEffect storedEffect)
6161
public Task SetValue<TValue>(string effectId, TValue value) => SetSucceeded(effectId, value);
6262

6363
public Task SetStarted(string effectId)
64-
=> Set(new StoredEffect(new EffectId(effectId, IsState: false), StoredEffectId.Create(effectId), WorkStatus.Started, Result: null, StoredException: null));
64+
=> Set(new StoredEffect(effectId.ToEffectId(), effectId.ToEffectId().ToStoredEffectId(), WorkStatus.Started, Result: null, StoredException: null));
6565

6666
public Task SetSucceeded(string effectId)
67-
=> Set(new StoredEffect(new EffectId(effectId, IsState: false), StoredEffectId.Create(effectId), WorkStatus.Completed, Result: null, StoredException: null));
67+
=> Set(new StoredEffect(effectId.ToEffectId(), effectId.ToEffectId().ToStoredEffectId(), WorkStatus.Completed, Result: null, StoredException: null));
6868

6969
public Task SetSucceeded<TResult>(string effectId, TResult result)
70-
=> Set(new StoredEffect(new EffectId(effectId, IsState: false),StoredEffectId.Create(effectId), WorkStatus.Completed, Result: serializer.SerializeEffectResult(result), StoredException: null));
70+
=> Set(new StoredEffect(effectId.ToEffectId(), effectId.ToEffectId().ToStoredEffectId(), WorkStatus.Completed, Result: serializer.SerializeEffectResult(result), StoredException: null));
7171

7272
public Task SetFailed(string effectId, Exception exception)
73-
=> Set(new StoredEffect(new EffectId(effectId, IsState: false), StoredEffectId.Create(effectId), WorkStatus.Failed, Result: null, StoredException: serializer.SerializeException(exception)));
73+
=> Set(new StoredEffect(effectId.ToEffectId(), effectId.ToEffectId().ToStoredEffectId(), WorkStatus.Failed, Result: null, StoredException: serializer.SerializeException(exception)));
7474
}

Core/Cleipnir.ResilientFunctions/Domain/ExistingStates.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private async Task<Dictionary<StateId, StoredState>> GetStoredStates()
5656
public async Task Remove(string stateId)
5757
{
5858
var storedStates = await GetStoredStates();
59-
await _effectsStore.DeleteEffectResult(_storedId, stateId.ToStoredEffectId(isState: true), isState: true);
59+
await _effectsStore.DeleteEffectResult(_storedId, stateId.ToStoredEffectId(isState: true));
6060
storedStates.Remove(stateId);
6161
}
6262

Core/Cleipnir.ResilientFunctions/Domain/States.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private async Task RemoveInner(string id)
8686
if (!existingStoredStates.ContainsKey(id))
8787
return;
8888

89-
await effectStore.DeleteEffectResult(storedId, id.ToStoredEffectId(isState: true), isState: true);
89+
await effectStore.DeleteEffectResult(storedId, id.ToStoredEffectId(isState: true));
9090

9191
lock (_sync)
9292
{

Core/Cleipnir.ResilientFunctions/Storage/IEffectsStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ public interface IEffectsStore
1010
Task SetEffectResult(StoredId storedId, StoredEffect storedEffect);
1111
Task SetEffectResults(StoredId storedId, IReadOnlyList<StoredEffect> storedEffects);
1212
Task<IReadOnlyList<StoredEffect>> GetEffectResults(StoredId storedId);
13-
Task DeleteEffectResult(StoredId storedId, StoredEffectId effectId, bool isState);
13+
Task DeleteEffectResult(StoredId storedId, StoredEffectId effectId);
1414
Task Remove(StoredId storedId);
1515
}

Core/Cleipnir.ResilientFunctions/Storage/InMemoryEffectsStore.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Cleipnir.ResilientFunctions.Helpers;
56

67
namespace Cleipnir.ResilientFunctions.Storage;
78

89
public class InMemoryEffectsStore : IEffectsStore
910
{
10-
private record EffectKey(StoredEffectId EffectId, bool IsState);
11-
private readonly Dictionary<StoredId, Dictionary<EffectKey, StoredEffect>> _effects = new();
12-
private readonly object _sync = new();
11+
private readonly Dictionary<StoredId, Dictionary<StoredEffectId, StoredEffect>> _effects = new();
12+
private readonly Lock _sync = new();
1313

1414
public Task Initialize() => Task.CompletedTask;
1515

@@ -25,11 +25,10 @@ public Task SetEffectResult(StoredId storedId, StoredEffect storedEffect)
2525
{
2626
lock (_sync)
2727
{
28-
var key = new EffectKey(storedEffect.StoredEffectId, storedEffect.EffectId.IsState);
2928
if (!_effects.ContainsKey(storedId))
30-
_effects[storedId] = new Dictionary<EffectKey, StoredEffect>();
29+
_effects[storedId] = new Dictionary<StoredEffectId, StoredEffect>();
3130

32-
_effects[storedId][key] = storedEffect;
31+
_effects[storedId][storedEffect.StoredEffectId] = storedEffect;
3332
}
3433

3534
return Task.CompletedTask;
@@ -49,12 +48,11 @@ public Task<IReadOnlyList<StoredEffect>> GetEffectResults(StoredId storedId)
4948
: ((IReadOnlyList<StoredEffect>) _effects[storedId].Values.ToList()).ToTask();
5049
}
5150

52-
public Task DeleteEffectResult(StoredId storedId, StoredEffectId effectId, bool isState)
51+
public Task DeleteEffectResult(StoredId storedId, StoredEffectId effectId)
5352
{
54-
var key = new EffectKey(effectId, isState);
5553
lock (_sync)
5654
if (_effects.ContainsKey(storedId))
57-
_effects[storedId].Remove(key);
55+
_effects[storedId].Remove(effectId);
5856

5957
return Task.CompletedTask;
6058
}

0 commit comments

Comments
 (0)