-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from Cratis:dolittle-event-stream-formalzation
Extracted out EventStream queries
- Loading branch information
Showing
9 changed files
with
206 additions
and
41 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,48 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Cratis.Extensions.MongoDB; | ||
using Microsoft.Extensions.Logging; | ||
using MongoDB.Driver; | ||
|
||
namespace Cratis.Extensions.Dolittle.EventStore | ||
{ | ||
/// <summary> | ||
/// Represents an implementation of <see cref="IEventStore"/>. | ||
/// </summary> | ||
public class EventStore : IEventStore | ||
{ | ||
readonly IMongoClient _client; | ||
readonly IMongoDatabase _database; | ||
readonly ILoggerFactory _loggerFactory; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EventStore"/> class. | ||
/// </summary> | ||
/// <param name="mongoDBClientFactory"><see cref="IMongoDBClientFactory"/> for connecting to MongoDB.</param> | ||
/// <param name="loggerFactory"><see cref="ILoggerFactory"/> for creating loggers.</param> | ||
public EventStore(IMongoDBClientFactory mongoDBClientFactory, ILoggerFactory loggerFactory) | ||
{ | ||
var mongoUrlBuilder = new MongoUrlBuilder | ||
{ | ||
Servers = new[] { new MongoServerAddress("localhost", 27017) } | ||
}; | ||
var url = mongoUrlBuilder.ToMongoUrl(); | ||
var settings = MongoClientSettings.FromUrl(url); | ||
_client = mongoDBClientFactory.Create(settings); | ||
_database = _client.GetDatabase("event_store"); | ||
_loggerFactory = loggerFactory; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IEventStream GetStream(EventStreamId id) | ||
{ | ||
if (id.Equals(EventStreamId.EventLog)) | ||
{ | ||
return new EventStream(_database.GetCollection<Event>("event-log"), _loggerFactory.CreateLogger<EventStream>()); | ||
} | ||
|
||
throw new NotImplementedException("Event log is the only stream supported at this point"); | ||
} | ||
} | ||
} |
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,58 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Dolittle.SDK.Events; | ||
using Microsoft.Extensions.Logging; | ||
using MongoDB.Driver; | ||
|
||
namespace Cratis.Extensions.Dolittle.EventStore | ||
{ | ||
/// <summary> | ||
/// Represents an implementation of <see cref="IEventStream"/>. | ||
/// </summary> | ||
public class EventStream : IEventStream | ||
{ | ||
readonly IMongoCollection<Event> _collection; | ||
readonly ILogger<EventStream> _logger; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EventStream"/> class. | ||
/// </summary> | ||
/// <param name="collection"><see cref="IMongoCollection{T}"/> that holds the event stream.</param> | ||
/// <param name="logger"><see cref="ILogger{T}"/> for logging.</param> | ||
public EventStream(IMongoCollection<Event> collection, ILogger<EventStream> logger) | ||
{ | ||
_collection = collection; | ||
_logger = logger; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IChangeStreamCursor<ChangeStreamDocument<Event>> Watch() => _collection.Watch(options: new() { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup }); | ||
|
||
/// <inheritdoc/> | ||
public long Count() => _collection.CountDocuments(FilterDefinition<Event>.Empty); | ||
|
||
/// <inheritdoc/> | ||
public async Task<IAsyncCursor<Event>> GetFromPosition(uint position, IEnumerable<EventType>? eventTypes = default, EventSourceId? eventSourceId = default) | ||
{ | ||
var offsetFilter = Builders<Event>.Filter.Gt(_ => _.Id, position); | ||
var eventTypeFilters = eventTypes?.Select(_ => Builders<Event>.Filter.Eq(_ => _.Metadata.TypeId, _.Id.Value)).ToArray() ?? Array.Empty<FilterDefinition<Event>>(); | ||
var eventSourceFilter = (eventSourceId is null) ? FilterDefinition<Event>.Empty : Builders<Event>.Filter.Eq(_ => _.Metadata.EventSource, eventSourceId.Value); | ||
|
||
var filter = Builders<Event>.Filter.And( | ||
offsetFilter, | ||
eventSourceFilter, | ||
Builders<Event>.Filter.Or(eventTypeFilters) | ||
); | ||
|
||
_logger.GettingEventsFromOffset(position); | ||
|
||
return await _collection.FindAsync( | ||
filter, | ||
new() | ||
{ | ||
Sort = Builders<Event>.Sort.Ascending(_ => _.Id) | ||
}); | ||
} | ||
} | ||
} |
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,12 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Cratis.Concepts; | ||
|
||
namespace Cratis.Extensions.Dolittle.EventStore | ||
{ | ||
public record EventStreamId(Guid Value) : ConceptAs<Guid>(Value) | ||
{ | ||
public static readonly EventStreamId EventLog = new(Guid.Empty); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Source/Extensions/Dolittle/EventStore/EventStreamLogMessages.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Cratis.Extensions.Dolittle.EventStore | ||
{ | ||
/// <summary> | ||
/// Holds log messages for <see cref="EventStream"/>. | ||
/// </summary> | ||
public static partial class EventStreamLogMessages | ||
{ | ||
[LoggerMessage(0, LogLevel.Trace, "Getting events from offset {Offset}")] | ||
internal static partial void GettingEventsFromOffset(this ILogger logger, uint offset); | ||
} | ||
} |
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,18 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Cratis.Extensions.Dolittle.EventStore | ||
{ | ||
/// <summary> | ||
/// Defines a system for working with the Dolittle event store. | ||
/// </summary> | ||
public interface IEventStore | ||
{ | ||
/// <summary> | ||
/// Get a specific <see cref="IEventStream"/>. | ||
/// </summary> | ||
/// <param name="id"><see cref="EventStreamId"/>.</param> | ||
/// <returns><see cref="IEventStream"/>.</returns> | ||
IEventStream GetStream(EventStreamId id); | ||
} | ||
} |
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,35 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Dolittle.SDK.Events; | ||
using MongoDB.Driver; | ||
|
||
namespace Cratis.Extensions.Dolittle.EventStore | ||
{ | ||
/// <summary> | ||
/// Defines an API for working with a Dolittle event stream. | ||
/// </summary> | ||
public interface IEventStream | ||
{ | ||
/// <summary> | ||
/// Count number of events in the stream. | ||
/// </summary> | ||
/// <returns>The count of events.</returns> | ||
long Count(); | ||
|
||
/// <summary> | ||
/// Watch for changes on the event stream. | ||
/// </summary> | ||
/// <returns><see cref="IChangeStreamCursor{T}"/> for changes.</returns> | ||
IChangeStreamCursor<ChangeStreamDocument<Event>> Watch(); | ||
|
||
/// <summary> | ||
/// Get events from a specific position. | ||
/// </summary> | ||
/// <param name="position">Position to get from.</param> | ||
/// <param name="eventTypes">Optional event types to get. If not specified, all will be given.</param> | ||
/// <param name="eventSourceId">Optional <see cref="EventSourceId"/>.</param> | ||
/// <returns><see cref="IAsyncCursor{Event}"/> for events in the stream.</returns> | ||
Task<IAsyncCursor<Event>> GetFromPosition(uint position, IEnumerable<EventType>? eventTypes = default, EventSourceId? eventSourceId = default); | ||
} | ||
} |
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