Skip to content

Commit

Permalink
[BACKPORT #6173] Add HashCodeMessageExtractor factory (#6182)
Browse files Browse the repository at this point in the history
* add HashCodeMEssageExtractor Factory method (#6173)

* 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>

* Update API Verify list

Co-authored-by: JonnyII <102029509+JonnyII@users.noreply.github.com>
Co-authored-by: Aaron Stannard <aaron@petabridge.com>
  • Loading branch information
3 people authored Oct 14, 2022
1 parent aca6850 commit cc3a361
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/contrib/cluster/Akka.Cluster.Sharding/ClusterSharding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,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
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,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 cc3a361

Please sign in to comment.