-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP SDK projections, using eventhandler protocol instead of runtime p…
…rojections
- Loading branch information
1 parent
47f717f
commit 2a6eae1
Showing
54 changed files
with
663 additions
and
1,078 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Dolittle. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using Dolittle.SDK.Tenancy; | ||
using Proto.Cluster; | ||
|
||
namespace Dolittle.SDK.Projections.Actors; | ||
|
||
static class ClusterIdentityMapper | ||
{ | ||
public static ClusterIdentity GetClusterIdentity(TenantId tenantId, Key projectionKey, string kind) => | ||
ClusterIdentity.Create($"{tenantId}:{projectionKey}", kind); | ||
|
||
public static (TenantId, Key) GetTenantAndKey(ClusterIdentity clusterIdentity) | ||
{ | ||
ArgumentNullException.ThrowIfNull(clusterIdentity); | ||
var separator = clusterIdentity.Identity.IndexOf(':'); | ||
|
||
if(separator == -1) | ||
{ | ||
throw new ArgumentException("ClusterIdentity is not in the correct format", nameof(clusterIdentity)); | ||
} | ||
|
||
TenantId tenantId = clusterIdentity.Identity[..separator]; | ||
Key projectionKey = clusterIdentity.Identity[(separator + 1)..]; | ||
|
||
return (tenantId, projectionKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) Dolittle. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Dolittle.SDK.Async; | ||
using Dolittle.SDK.Events; | ||
using Proto.Cluster; | ||
|
||
namespace Dolittle.SDK.Projections.Actors; | ||
|
||
public interface IProjectionClient<TProjection> | ||
{ | ||
/// <summary> | ||
/// Project an event to the projection. | ||
/// </summary> | ||
/// <param name="event"></param> | ||
/// <param name="eventType"></param> | ||
/// <param name="context"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
Task<ProjectionResultType> On(object @event, EventType eventType, EventContext context, CancellationToken cancellationToken); | ||
} | ||
|
||
public class ProjectionClient<TProjection>(IProjection<TProjection> projection, Cluster cluster) : IProjectionClient<TProjection> where TProjection : ProjectionBase, new() | ||
{ | ||
readonly string _kind = ProjectionActor<TProjection>.GetKind(projection); | ||
|
||
public async Task<ProjectionResultType> On(object @event, EventType eventType, EventContext context, CancellationToken cancellationToken) | ||
{ | ||
if (!projection.Events.TryGetValue(eventType, out var keySelector)) | ||
{ | ||
throw new UnhandledEventType($"Projection {projection.Identifier} does not handle event type {eventType}.", eventType); | ||
} | ||
|
||
var key = keySelector.GetKey(@event, context); | ||
var message = new ProjectedEvent(key, @event, eventType, context); | ||
|
||
var clusterIdentity = GetIdentity(context, key); | ||
var response = await cluster.RequestAsync<Try<ProjectionResultType>>(clusterIdentity, message, cancellationToken); | ||
response.ThrowIfFailed(); | ||
|
||
return response.Result; | ||
} | ||
|
||
ClusterIdentity GetIdentity(EventContext context, Key key) => | ||
ClusterIdentityMapper.GetClusterIdentity(context.CurrentExecutionContext.Tenant, key, _kind); | ||
} | ||
|
||
public class UnhandledEventType(string message, EventType eventType) : Exception(message) | ||
{ | ||
public EventType EventType => eventType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.