Skip to content

Commit

Permalink
add HashCodeMEssageExtractor Factory method (#6173)
Browse files Browse the repository at this point in the history
* feat: add add HashCodeMEssageExtractor Factory method

* test: verified change of previous commit

* test: verified missing api changes

Co-authored-by: Aaron Stannard <aaron@petabridge.com>
Co-authored-by: Gregorius Soedharmo <arkatufus@yahoo.com>
  • Loading branch information
3 people authored Oct 14, 2022
1 parent 0b944de commit f5d5838
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/contrib/cluster/Akka.Cluster.Sharding/ClusterSharding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;

using Akka.Actor;
using Akka.Cluster.Tools.Singleton;
using Akka.Configuration;
Expand Down Expand Up @@ -54,6 +55,33 @@ public override ClusterSharding CreateExtension(ExtendedActorSystem system)
/// </summary>
public abstract class HashCodeMessageExtractor : IMessageExtractor
{
private class Implementation : HashCodeMessageExtractor
{
private readonly Func<object, string> _entityIdExtractor;
private readonly Func<object, object> _messageExtractor;
public Implementation(int maxNumberOfShards, Func<object, string> entityIdExtractor, Func<object, object> messageExtractor = null) : base(maxNumberOfShards)
{
_entityIdExtractor = entityIdExtractor ?? throw new NullReferenceException(nameof(entityIdExtractor));
_messageExtractor = messageExtractor;
}

public override string EntityId(object message)
=> _entityIdExtractor.Invoke(message);

public override object EntityMessage(object message)
=> _messageExtractor?.Invoke(message) ?? base.EntityMessage(message);
}

/// <summary>
/// creates a instance of the <see cref="HashCodeMessageExtractor"/> with the given handlers
/// </summary>
/// <param name="maxNumberOfShards"></param>
/// <param name="entityIdExtractor"></param>
/// <param name="messageExtractor"></param>
/// <returns></returns>
public static HashCodeMessageExtractor Create(int maxNumberOfShards, Func<object, string> entityIdExtractor, Func<object, object> messageExtractor = null)
=> new Implementation(maxNumberOfShards, entityIdExtractor, messageExtractor);

/// <summary>
/// TBD
/// </summary>
Expand Down Expand Up @@ -1459,7 +1487,7 @@ internal static class Extensions
/// <returns>TBD</returns>
public static ExtractEntityId ToExtractEntityId(this IMessageExtractor self)
{
Option<(EntityId, Msg)> ExtractEntityId (object msg)
Option<(EntityId, Msg)> ExtractEntityId(object msg)
{
if (self.EntityId(msg) != null)
return (self.EntityId(msg), self.EntityMessage(msg));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ namespace Akka.Cluster.Sharding
{
public readonly int MaxNumberOfShards;
protected HashCodeMessageExtractor(int maxNumberOfShards) { }
public static Akka.Cluster.Sharding.HashCodeMessageExtractor Create(int maxNumberOfShards, System.Func<object, string> entityIdExtractor, System.Func<object, object> messageExtractor = null) { }
public abstract string EntityId(object message);
public virtual object EntityMessage(object message) { }
public virtual string ShardId(object message) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ namespace Akka.Cluster.Sharding
{
public readonly int MaxNumberOfShards;
protected HashCodeMessageExtractor(int maxNumberOfShards) { }
public static Akka.Cluster.Sharding.HashCodeMessageExtractor Create(int maxNumberOfShards, System.Func<object, string> entityIdExtractor, System.Func<object, object> messageExtractor = null) { }
public abstract string EntityId(object message);
public virtual object EntityMessage(object message) { }
public virtual string ShardId(object message) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ namespace Akka.Cluster.Sharding
{
public readonly int MaxNumberOfShards;
protected HashCodeMessageExtractor(int maxNumberOfShards) { }
public static Akka.Cluster.Sharding.HashCodeMessageExtractor Create(int maxNumberOfShards, System.Func<object, string> entityIdExtractor, System.Func<object, object> messageExtractor = null) { }
public abstract string EntityId(object message);
public virtual object EntityMessage(object message) { }
public virtual string ShardId(object message) { }
Expand Down

0 comments on commit f5d5838

Please sign in to comment.