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

Improved Selector Caching #65

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 0 additions & 1 deletion src/Fluss.HotChocolate/Fluss.HotChocolate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<RepositoryUrl>https://github.com/atmina/fluss</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 5 additions & 2 deletions src/Fluss.Regen/FileBuilders/SelectorFileBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ public void WriteClassHeader()

public void WriteEndNamespace()
{
_writer.WriteIndentedLine("private record CacheEntryValue(object Value, global::System.Collections.Generic.IReadOnlyList<global::Fluss.UnitOfWorkRecordingProxy.EventListenerTypeWithKeyAndVersion>? EventListeners);");
_writer.WriteIndentedLine("private record CacheEntryValue(object Value, global::System.Collections.Generic.IReadOnlyList<global::Fluss.UnitOfWorkRecordingProxy.EventListenerTypeWithKeyAndVersion>? EventListeners, long CreatedAtVersion);");
_writer.WriteLine();
_writer.WriteIndented("private static async global::System.Threading.Tasks.ValueTask<bool> MatchesEventListenerState(global::Fluss.IUnitOfWork unitOfWork, CacheEntryValue value) ");
using (_writer.WriteBraces())
{
_writer.WriteIndentedLine("var version = await unitOfWork.ConsistentVersion();");
_writer.WriteIndentedLine("if (value.CreatedAtVersion == version) return true;");

_writer.WriteIndented("foreach (var eventListenerData in value.EventListeners ?? global::System.Array.Empty<global::Fluss.UnitOfWorkRecordingProxy.EventListenerTypeWithKeyAndVersion>()) ");
using (_writer.WriteBraces())
{
Expand Down Expand Up @@ -152,7 +155,7 @@ public void WriteMethodCacheMiss(string returnType)

using (_writer.WriteBraces())
{
_writer.WriteIndentedLine("entry.Value = new CacheEntryValue(result, recordingUnitOfWork.GetRecordedListeners());");
_writer.WriteIndentedLine("entry.Value = new CacheEntryValue(result, recordingUnitOfWork.GetRecordedListeners(), await unitOfWork.ConsistentVersion());");
_writer.WriteIndentedLine("entry.Size = 1;");
}

Expand Down
2 changes: 2 additions & 0 deletions src/Fluss.UnitTest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TestResults/

1 change: 1 addition & 0 deletions src/Fluss/UnitOfWork/IUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ ValueTask<TReadModel> UnsafeGetReadModelWithoutAuthorization<TReadModel>(long? a
where TKey : notnull where TReadModel : EventListener, IReadModel, IEventListenerWithKey<TKey>, new();

IUnitOfWork WithPrefilledVersion(long? version);
IUnitOfWork CopyWithVersion(long version);
}
8 changes: 8 additions & 0 deletions src/Fluss/UnitOfWork/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ public IUnitOfWork WithPrefilledVersion(long? version)
return this;
}

public IUnitOfWork CopyWithVersion(long version)
{
EnsureInstantiated();

var unitOfWork = Create(_eventRepository!, _eventListenerFactory!, _policies, _userIdProvider!, _validator!);
return unitOfWork.WithPrefilledVersion(version);
}

private Guid CurrentUserId()
{
EnsureInstantiated();
Expand Down
19 changes: 16 additions & 3 deletions src/Fluss/UnitOfWork/UnitOfWorkRecordingProxy.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using Fluss.Events;
using Fluss.ReadModel;

namespace Fluss;

public class AtNotAllowedInSelectorException : Exception;

public class UnitOfWorkRecordingProxy(IUnitOfWork impl) : IUnitOfWork
{
public ValueTask<long> ConsistentVersion()
Expand All @@ -18,42 +19,54 @@ public ValueTask<long> ConsistentVersion()

public ValueTask<IReadModel> GetReadModel([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type tReadModel, object? key, long? at = null)
{
if (at.HasValue) throw new AtNotAllowedInSelectorException();
return impl.GetReadModel(tReadModel, key, at);
}

public ValueTask<TReadModel> GetReadModel<TReadModel>(long? at = null) where TReadModel : EventListener, IRootEventListener, IReadModel, new()
{
if (at.HasValue) throw new AtNotAllowedInSelectorException();
return Record(impl.GetReadModel<TReadModel>(at));
}

public ValueTask<TReadModel> GetReadModel<TReadModel, TKey>(TKey key, long? at = null) where TReadModel : EventListener, IEventListenerWithKey<TKey>, IReadModel, new()
{
if (at.HasValue) throw new AtNotAllowedInSelectorException();
return Record(impl.GetReadModel<TReadModel, TKey>(key, at));
}

public ValueTask<TReadModel> UnsafeGetReadModelWithoutAuthorization<TReadModel>(long? at = null) where TReadModel : EventListener, IRootEventListener, IReadModel, new()
{
if (at.HasValue) throw new AtNotAllowedInSelectorException();
return Record(impl.UnsafeGetReadModelWithoutAuthorization<TReadModel>(at));
}

public ValueTask<TReadModel> UnsafeGetReadModelWithoutAuthorization<TReadModel, TKey>(TKey key, long? at = null) where TReadModel : EventListener, IEventListenerWithKey<TKey>, IReadModel, new()
{
if (at.HasValue) throw new AtNotAllowedInSelectorException();
return Record(impl.UnsafeGetReadModelWithoutAuthorization<TReadModel, TKey>(key, at));
}

public ValueTask<IReadOnlyList<TReadModel>> GetMultipleReadModels<TReadModel, TKey>(IEnumerable<TKey> keys, long? at = null) where TReadModel : EventListener, IReadModel, IEventListenerWithKey<TKey>, new() where TKey : notnull
{
if (at.HasValue) throw new AtNotAllowedInSelectorException();
return Record(impl.GetMultipleReadModels<TReadModel, TKey>(keys, at));
}

public ValueTask<IReadOnlyList<TReadModel>> UnsafeGetMultipleReadModelsWithoutAuthorization<TReadModel, TKey>(IEnumerable<TKey> keys, long? at = null) where TReadModel : EventListener, IReadModel, IEventListenerWithKey<TKey>, new() where TKey : notnull
{
if (at.HasValue) throw new AtNotAllowedInSelectorException();
return Record(impl.UnsafeGetMultipleReadModelsWithoutAuthorization<TReadModel, TKey>(keys, at));
}

public IUnitOfWork WithPrefilledVersion(long? version)
{
return impl.WithPrefilledVersion(version);
throw new AtNotAllowedInSelectorException();
}

public IUnitOfWork CopyWithVersion(long version)
{
throw new AtNotAllowedInSelectorException();
}

private async ValueTask<TReadModel> Record<TReadModel>(ValueTask<TReadModel> readModel) where TReadModel : EventListener
Expand Down
Loading