Beispiel #1
0
    /// <inheritdoc/>
    public async Task <Try <IEnumerable <StreamEvent> > > FetchInPartition(PartitionId partitionId, StreamPosition streamPosition, CancellationToken cancellationToken)
    {
        ThrowIfNotConstructedWithPartitionIdExpression();
        try
        {
            var events = await _collection.Find(
                _filter.EqStringOrGuid(_partitionIdExpression, partitionId.Value)
                & _filter.Gte(_sequenceNumberExpression, streamPosition.Value))
                         .Limit(FetchEventsBatchSize)
                         .Project(_eventToStreamEvent)
                         .ToListAsync(cancellationToken).ConfigureAwait(false);

            return(events == default || events.Count == 0
                ? new NoEventInPartitionInStreamFromPosition(_stream, _scope, partitionId, streamPosition)
                : events);
        }
        catch (MongoWaitQueueFullException ex)
        {
            throw new EventStoreUnavailable("Mongo wait queue is full", ex);
        }
    }
Beispiel #2
0
    /// <inheritdoc/>
    public async Task <AggregateRootVersion> FetchVersionFor(
        EventSourceId eventSource,
        ArtifactId aggregateRoot,
        CancellationToken cancellationToken)
    {
        _logger.FetchingVersionFor(aggregateRoot, eventSource);
        var eqFilter = _filter.EqStringOrGuid(_ => _.EventSource, eventSource.Value)
                       & _filter.Eq(_ => _.AggregateType, aggregateRoot.Value);
        var aggregateDocuments = await _aggregates.Aggregates
                                 .Find(eqFilter)
                                 .ToListAsync(cancellationToken).ConfigureAwait(false);

        return(aggregateDocuments.Count switch
        {
            0 => AggregateRootVersion.Initial,
            1 => aggregateDocuments[0].Version,
            _ => throw new MultipleAggregateInstancesFound(eventSource, aggregateRoot),
        });
    async Task <CommittedAggregateEvents> DoFetchForAggregate(
        EventSourceId eventSource,
        ArtifactId aggregateRoot,
        Func <FilterDefinition <MongoDB.Events.Event>, FilterDefinition <MongoDB.Events.Event> > filterCallback,
        CancellationToken cancellationToken)
    {
        try
        {
            var version = await _aggregateRoots.FetchVersionFor(
                eventSource,
                aggregateRoot,
                cancellationToken).ConfigureAwait(false);

            if (version <= AggregateRootVersion.Initial)
            {
                return(new CommittedAggregateEvents(
                           eventSource,
                           aggregateRoot,
                           Array.Empty <CommittedAggregateEvent>()));
            }
            var defaultFilter = _eventFilter.Eq(_ => _.Aggregate.WasAppliedByAggregate, true)
                                & _eventFilter.EqStringOrGuid(_ => _.Metadata.EventSource, eventSource.Value)
                                & _eventFilter.Eq(_ => _.Aggregate.TypeId, aggregateRoot.Value)
                                & _eventFilter.Lte(_ => _.Aggregate.Version, version.Value);

            var filter = filterCallback(defaultFilter);

            var events = await _streams.DefaultEventLog
                         .Find(filter)
                         .Sort(Builders <MongoDB.Events.Event> .Sort.Ascending(_ => _.Aggregate.Version))
                         .ToListAsync(cancellationToken).ConfigureAwait(false);

            return(new CommittedAggregateEvents(
                       eventSource,
                       aggregateRoot,
                       events.Select(_ => _eventConverter.ToRuntimeStreamEvent(_))
                       .Select(_ => _.Event)
                       .Cast <CommittedAggregateEvent>().ToList()));
        }
        catch (Exception ex)
        {
            throw new EventStoreUnavailable("Mongo wait queue is full", ex);
        }
    }
 FilterDefinition <MongoSubscriptionState> CreateFilter(SubscriptionId id) =>
 _subscriptionFilter.Eq(_ => _.Microservice, id.ProducerMicroserviceId.Value)
 & _subscriptionFilter.Eq(_ => _.Tenant, id.ProducerTenantId.Value)
 & _subscriptionFilter.Eq(_ => _.Stream, id.StreamId.Value)
 & _subscriptionFilter.EqStringOrGuid(_ => _.Partition, id.PartitionId.Value);