Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ElasticSearch left fold projections
Cleaned EventStoreDB README links Minor updates in other places
- Loading branch information
1 parent
e624f4b
commit aa65a44
Showing
20 changed files
with
169 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Nest; | ||
|
||
namespace Core.ElasticSearch | ||
{ | ||
public class ElasticSearchConfig | ||
{ | ||
public string Url { get; set; } = default!; | ||
public string DefaultIndex { get; set; } = default!; | ||
} | ||
|
||
public static class ElasticSearchConfigExtensions | ||
{ | ||
private const string DefaultConfigKey = "ElasticSearch"; | ||
public static void AddElasticsearch( | ||
this IServiceCollection services, IConfiguration configuration, Action<ConnectionSettings>? config = null) | ||
{ | ||
var elasticSearchConfig = configuration.GetSection(DefaultConfigKey).Get<ElasticSearchConfig>(); | ||
|
||
var settings = new ConnectionSettings(new Uri(elasticSearchConfig.Url)) | ||
.DefaultIndex(elasticSearchConfig.DefaultIndex); | ||
|
||
config?.Invoke(settings); | ||
|
||
var client = new ElasticClient(settings); | ||
|
||
services.AddSingleton<IElasticClient>(client); | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,40 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Linq; | ||
|
||
namespace Core.ElasticSearch.Indices | ||
{ | ||
public class IndexNameMapper | ||
{ | ||
private static readonly IndexNameMapper Instance = new(); | ||
|
||
private readonly ConcurrentDictionary<Type, string> typeNameMap = new(); | ||
|
||
public static void AddCustomMap<TStream>(string mappedStreamName) => | ||
AddCustomMap(typeof(TStream), mappedStreamName); | ||
|
||
public static void AddCustomMap(Type streamType, string mappedStreamName) | ||
{ | ||
Instance.typeNameMap.AddOrUpdate(streamType, mappedStreamName, (_, _) => mappedStreamName); | ||
} | ||
|
||
public static string ToIndexPrefix<TStream>() => ToIndexPrefix(typeof(TStream)); | ||
|
||
public static string ToIndexPrefix(Type streamType) => Instance.typeNameMap.GetOrAdd(streamType, (_) => | ||
{ | ||
var modulePrefix = streamType.Namespace!.Split(".").First(); | ||
return $"{modulePrefix}-{streamType.Name}".ToLower(); | ||
}); | ||
|
||
public static string ToIndexName<TStream>(object? tenantId = null) => | ||
ToIndexName(typeof(TStream)); | ||
|
||
public static string ToIndexName(Type streamType, object? tenantId = null) | ||
{ | ||
var tenantPrefix = tenantId != null ? $"{tenantId}-" : ""; | ||
|
||
return $"{tenantPrefix}{ToIndexPrefix(streamType)}".ToLower(); | ||
} | ||
|
||
} | ||
} |
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,62 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Core.ElasticSearch.Indices; | ||
using Core.Events; | ||
using Core.Projections; | ||
using MediatR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Nest; | ||
|
||
namespace Core.ElasticSearch.Projections | ||
{ | ||
public class ElasticSearchProjection<TEvent, TView> : IEventHandler<TEvent> | ||
where TView : class, IProjection | ||
where TEvent : IEvent | ||
{ | ||
private readonly IElasticClient elasticClient; | ||
private readonly Func<TEvent, string> getId; | ||
|
||
public ElasticSearchProjection( | ||
IElasticClient elasticClient, | ||
Func<TEvent, string> getId | ||
) | ||
{ | ||
this.elasticClient = elasticClient ?? throw new ArgumentNullException(nameof(elasticClient)); | ||
this.getId = getId ?? throw new ArgumentNullException(nameof(getId)); | ||
} | ||
|
||
public async Task Handle(TEvent @event, CancellationToken ct) | ||
{ | ||
string id = getId(@event); | ||
|
||
var entity = (await elasticClient.GetAsync<TView>(id, ct: ct))?.Source | ||
?? (TView) Activator.CreateInstance(typeof(TView), true)!; | ||
|
||
entity.When(@event); | ||
|
||
var result = await elasticClient.UpdateAsync<TView>(id, | ||
u => u.Doc(entity).Upsert(entity).Index(IndexNameMapper.ToIndexName<TView>()), | ||
ct | ||
); | ||
} | ||
} | ||
|
||
public static class ElasticSearchProjectionConfig | ||
{ | ||
public static IServiceCollection Project<TEvent, TView>(this IServiceCollection services, | ||
Func<TEvent, string> getId) | ||
where TView : class, IProjection | ||
where TEvent : IEvent | ||
{ | ||
services.AddTransient<INotificationHandler<TEvent>>(sp => | ||
{ | ||
var session = sp.GetRequiredService<IElasticClient>(); | ||
return new ElasticSearchProjection<TEvent, TView>(session, getId); | ||
}); | ||
|
||
return services; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
using System.Text; | ||
using Core.Events; | ||
using Core.Reflection; | ||
using EventStore.Client; | ||
using Newtonsoft.Json; | ||
|
||
|
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
2 changes: 1 addition & 1 deletion
2
...ions/ISubscriptionCheckpointRepository.cs → ...ions/ISubscriptionCheckpointRepository.cs
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
5 changes: 0 additions & 5 deletions
5
Core.EventStoreDB/Subscriptions/SubscribeToAllBackgroundWorker.cs
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
1 change: 0 additions & 1 deletion
1
Sample/Tickets/Tickets.Tests/Stubs/Reservations/FakeReservationNumberGenerator.cs
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