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 @@ -81,8 +81,10 @@ namespace Akka.Persistence.Query
public Akka.Event.ILoggingAdapter Log { get; }
public static Akka.Persistence.Query.PersistenceQuery Get(Akka.Actor.ActorSystem system) { }
public static Akka.Configuration.Config GetDefaultConfig<TJournal>() { }
public static Akka.Configuration.Config GetDefaultConfig(System.Type journalType) { }
public TJournal ReadJournalFor<TJournal>(string readJournalPluginId)
where TJournal : Akka.Persistence.Query.IReadJournal { }
public Akka.Persistence.Query.IReadJournal ReadJournalFor(System.Type readJournalType, string readJournalPluginId) { }
}
public class static PersistenceQueryExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ namespace Akka.Persistence.Query
public Akka.Event.ILoggingAdapter Log { get; }
public static Akka.Persistence.Query.PersistenceQuery Get(Akka.Actor.ActorSystem system) { }
public static Akka.Configuration.Config GetDefaultConfig<TJournal>() { }
public static Akka.Configuration.Config GetDefaultConfig(System.Type journalType) { }
public TJournal ReadJournalFor<TJournal>(string readJournalPluginId)
where TJournal : Akka.Persistence.Query.IReadJournal { }
public Akka.Persistence.Query.IReadJournal ReadJournalFor(System.Type readJournalType, string readJournalPluginId) { }
}
public class static PersistenceQueryExtensions
{
Expand Down
21 changes: 16 additions & 5 deletions src/core/Akka.Persistence.Query/PersistenceQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace Akka.Persistence.Query
{
public sealed class PersistenceQuery : IExtension
{
private static readonly Type ReadJournalType = typeof(IReadJournal);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type cache of IReadJournal


private readonly ExtendedActorSystem _system;
private readonly ConcurrentDictionary<string, IReadJournal> _readJournalPluginExtensionIds = new();
private ILoggingAdapter _log;
Expand All @@ -34,18 +36,24 @@ public PersistenceQuery(ExtendedActorSystem system)
}

public TJournal ReadJournalFor<TJournal>(string readJournalPluginId) where TJournal : IReadJournal
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Old API is left intact

=> (TJournal) ReadJournalFor(typeof(TJournal), readJournalPluginId);

public IReadJournal ReadJournalFor(Type readJournalType, string readJournalPluginId)
{
if(!ReadJournalType.IsAssignableFrom(readJournalType))
throw new ArgumentException("Must implement IReadJournal interface", nameof(readJournalType));
Comment on lines +43 to +44
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added type checking code to make sure that readJournalType implements IReadJournal


if(_readJournalPluginExtensionIds.TryGetValue(readJournalPluginId, out var plugin))
return (TJournal)plugin;
return plugin;

lock (_lock)
{
if (_readJournalPluginExtensionIds.TryGetValue(readJournalPluginId, out plugin))
return (TJournal)plugin;
return plugin;

plugin = CreatePlugin(readJournalPluginId, GetDefaultConfig<TJournal>()).GetReadJournal();
plugin = CreatePlugin(readJournalPluginId, GetDefaultConfig(readJournalType)).GetReadJournal();
_readJournalPluginExtensionIds[readJournalPluginId] = plugin;
return (TJournal)plugin;
return plugin;
}
}

Expand Down Expand Up @@ -79,8 +87,11 @@ private IReadJournalProvider CreateType(Type pluginType, object[] parameters)
}

public static Config GetDefaultConfig<TJournal>()
=> GetDefaultConfig(typeof(TJournal));

public static Config GetDefaultConfig(Type journalType)
{
var defaultConfigMethod = typeof(TJournal).GetMethod("DefaultConfiguration", BindingFlags.Public | BindingFlags.Static);
var defaultConfigMethod = journalType.GetMethod("DefaultConfiguration", BindingFlags.Public | BindingFlags.Static);
return defaultConfigMethod?.Invoke(null, null) as Config;
}
}
Expand Down