public async Task <Try <Store.Definition.ProjectionDefinition> > TryGet(ProjectionId projection, ScopeId scope, CancellationToken token)
 {
     try
     {
         return(await OnDefinitions <Try <Store.Definition.ProjectionDefinition> >(
                    scope,
                    async collection =>
         {
             var findResult = await collection
                              .Find(CreateIdFilter(projection))
                              .Project(_ => Tuple.Create(_.InitialStateRaw, _.EventSelectors, _.Copies))
                              .SingleOrDefaultAsync(token).ConfigureAwait(false);
             if (findResult == null)
             {
                 return Try <Store.Definition.ProjectionDefinition> .Failed(new ProjectionDefinitionDoesNotExist(projection, scope));
             }
             var(initialState, eventSelectors, copies) = findResult;
             return _definitionConverter.ToRuntime(projection, scope, eventSelectors, initialState, copies);
         },
                    token).ConfigureAwait(false));
     }
     catch (Exception ex)
     {
         return(ex);
     }
 }
Exemple #2
0
    /// <inheritdoc/>
    public async Task <bool> TryReplace(ProjectionId projection, ScopeId scope, ProjectionKey key, ProjectionState state, CancellationToken token)
    {
        try
        {
            var collection = await _projections.GetStates(scope, projection, token).ConfigureAwait(false);

            var filter           = CreateKeyFilter(key);
            var updateDefinition = Builders <Projection>
                                   .Update
                                   .Set(_ => _.Content, BsonDocument.Parse(state.Value))
                                   .Set(_ => _.ContentRaw, state.Value);

            var updateResult = await collection.UpdateOneAsync(
                filter,
                updateDefinition,
                new UpdateOptions { IsUpsert = true },
                token).ConfigureAwait(false);

            return(updateResult.IsAcknowledged);
        }
        catch (MongoWaitQueueFullException)
        {
            return(false);
        }
    }
 public Store.Definition.ProjectionDefinition ToRuntime(
     ProjectionId projection,
     ScopeId scope,
     IEnumerable <ProjectionEventSelector> eventSelectors,
     Store.State.ProjectionState initialState,
     ProjectionCopies copies)
 => new(
 public override int GetHashCode()
 {
     unchecked
     {
         int hashCode = (Status != null ? Status.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ Enabled.GetHashCode();
         hashCode = (hashCode * 397) ^ (int)MasterStatus;
         hashCode = (hashCode * 397) ^ (StateReason != null ? StateReason.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (Name != null ? Name.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ ProjectionId.GetHashCode();
         hashCode = (hashCode * 397) ^ Epoch.GetHashCode();
         hashCode = (hashCode * 397) ^ Version.GetHashCode();
         hashCode = (hashCode * 397) ^ (int)Mode;
         hashCode = (hashCode * 397) ^ (Position != null ? Position.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ Progress.GetHashCode();
         hashCode = (hashCode * 397) ^ (LastCheckpoint != null ? LastCheckpoint.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ EventsProcessedAfterRestart;
         hashCode = (hashCode * 397) ^ BufferedEvents;
         hashCode = (hashCode * 397) ^ (CheckpointStatus != null ? CheckpointStatus.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ WritePendingEventsBeforeCheckpoint;
         hashCode = (hashCode * 397) ^ WritePendingEventsAfterCheckpoint;
         hashCode = (hashCode * 397) ^ PartitionsCached;
         hashCode = (hashCode * 397) ^ ReadsInProgress;
         hashCode = (hashCode * 397) ^ WritesInProgress;
         hashCode = (hashCode * 397) ^ (EffectiveName != null ? EffectiveName.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (ResultStreamName != null ? ResultStreamName.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ CoreProcessingTime.GetHashCode();
         return(hashCode);
     }
 }
Exemple #5
0
    /// <inheritdoc/>
    public async Task <Try <ProjectionCurrentState> > TryGet(ProjectionId projection, ScopeId scope, ProjectionKey key, CancellationToken token)
    {
        try
        {
            Log.GettingOneProjection(_logger, projection, scope, key);
            _metrics.IncrementTotalGet();

            var state = await _projectionStates.TryGet(projection, scope, key, token).ConfigureAwait(false);

            switch (state)
            {
            case { Success: true } :
                return(new ProjectionCurrentState(ProjectionCurrentStateType.Persisted, state.Result, key));
Exemple #6
0
    /// <inheritdoc/>
    public async Task <bool> TryDrop(ProjectionId projection, ScopeId scope, CancellationToken token)
    {
        try
        {
            var collection = await _projections.GetStates(scope, projection, token).ConfigureAwait(false);

            var deleteResult = await collection.DeleteManyAsync(Builders <Projection> .Filter.Empty, token).ConfigureAwait(false);

            return(deleteResult.IsAcknowledged);
        }
        catch (MongoWaitQueueFullException)
        {
            return(false);
        }
    }
Exemple #7
0
    /// <inheritdoc/>
    public async Task <bool> TryRemove(ProjectionId projection, ScopeId scope, ProjectionKey key, CancellationToken token)
    {
        try
        {
            var collection = await _projections.GetStates(scope, projection, token).ConfigureAwait(false);

            var deleteResult = await collection.DeleteOneAsync(CreateKeyFilter(key), token).ConfigureAwait(false);

            return(deleteResult.IsAcknowledged);
        }
        catch (MongoWaitQueueFullException)
        {
            return(false);
        }
    }
Exemple #8
0
    /// <inheritdoc />
    public async Task <Try> Replay(MicroserviceAddress runtime, ScopeId scope, ProjectionId projection, TenantId tenant = null)
    {
        var client  = _clients.CreateClientFor <ProjectionsClient>(runtime);
        var request = new ReplayProjectionRequest
        {
            ScopeId      = scope.ToProtobuf(),
            ProjectionId = projection.ToProtobuf(),
            TenantId     = tenant?.ToProtobuf(),
        };

        var response = await client.ReplayAsync(request);

        if (response.Failure != null)
        {
            return(new ReplayProjectionFailed(scope, projection, response.Failure.Reason));
        }

        return(Try.Succeeded());
    }
Exemple #9
0
    /// <inheritdoc/>
    public async Task <Try <ProjectionState> > TryGet(ProjectionId projection, ScopeId scope, ProjectionKey key, CancellationToken token)
    {
        try
        {
            var collection = await _projections.GetStates(scope, projection, token).ConfigureAwait(false);

            var projectionState = await collection
                                  .Find(CreateKeyFilter(key))
                                  .Project(_ => _.ContentRaw)
                                  .SingleOrDefaultAsync(token)
                                  .ConfigureAwait(false);

            return(string.IsNullOrEmpty(projectionState)
                ? Try <ProjectionState> .Failed(new ProjectionStateDoesNotExist(projection, key, scope))
                : new ProjectionState(projectionState));
        }
        catch (Exception ex)
        {
            return(ex);
        }
    }
Exemple #10
0
 internal static void GettingOneProjection(this ILogger logger, ProjectionId projection, ScopeId scope, ProjectionKey key)
 => _gettingOneProjection(logger, projection, scope, key, null);
Exemple #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GetOneProjectionFailed"/> class.
 /// </summary>
 /// <param name="scope">The scope of the Projection.</param>
 /// <param name="projection">The id of the Projection.</param>
 /// <param name="reason">The reason why getting the Projection failed.</param>
 public GetOneProjectionFailed(ScopeId scope, ProjectionId projection, string reason)
     : base($"Could not get projection {projection} in scope {scope} because {reason}")
 {
 }
Exemple #12
0
 internal static partial void CreatingProjectionStatusForAllTenants(ILogger logger, ProjectionId projection, ScopeId scope);
Exemple #13
0
 internal static partial void CreatingProjectionStatusForTenant(ILogger logger, ProjectionId projection, ScopeId scope, TenantId tenant);
Exemple #14
0
 internal static partial void FailedToReplayProjection(ILogger logger, ProjectionId projection, ScopeId scope, Exception exception);
 /// <summary>
 /// Initializes an instance of the <see cref="ProjectionStateDoesNotExist" /> class.
 /// </summary>
 /// <param name="projection">The projection id.</param>
 /// <param name="key">The projection key.</param>
 /// <param name="scope">The scope id.</param>
 public ProjectionStateDoesNotExist(ProjectionId projection, ProjectionKey key, ScopeId scope)
     : base($"A projection state for projection {projection.Value} with key {key.Value} in scope {scope.Value} does not exist")
 {
 }
Exemple #16
0
    /// <inheritdoc />
    public async Task <Try <ProjectionStatus> > Get(MicroserviceAddress runtime, ScopeId scope, ProjectionId projection, TenantId tenant = null)
    {
        var client  = _clients.CreateClientFor <ProjectionsClient>(runtime);
        var request = new GetOneProjectionRequest
        {
            ScopeId      = scope.ToProtobuf(),
            ProjectionId = projection.ToProtobuf(),
            TenantId     = tenant?.ToProtobuf()
        };

        var response = await client.GetOneAsync(request);

        if (response.Failure != null)
        {
            return(new GetOneProjectionFailed(scope, projection, response.Failure.Reason));
        }

        return(CreateProjectionStatus(response.Projection));
    }
Exemple #17
0
 internal static partial void GetOne(ILogger logger, ProjectionId projection, ScopeId scope);
Exemple #18
0
 internal static partial void FailedToDropProjectionInCopyStore(ILogger logger, ProjectionId projection, ScopeId scope);
Exemple #19
0
    static Try GetScopeAndProjectionIds(Uuid scope, Uuid projection, out ScopeId scopeId, out ProjectionId projectionId)
    {
        scopeId      = default;
        projectionId = default;

        if (scope == default)
        {
            return(Try.Failed(new ArgumentNullException(nameof(scope), "Scope id is missing in request")));
        }
        if (projection == default)
        {
            return(Try.Failed(new ArgumentNullException(nameof(projection), "Projection id is missing in request")));
        }

        scopeId      = scope.ToGuid();
        projectionId = projection.ToGuid();

        return(Try.Succeeded());
    }
Exemple #20
0
 /// <inheritdoc/>
 public Task <Try <ProjectionCurrentState> > TryGetOne(ProjectionId projection, ScopeId scope, ProjectionKey key, ExecutionContext context, CancellationToken token)
 => _executionContextCreator
 .TryCreateUsing(context)
 .Then(_ => _getProjectionStoreFor(_.Tenant))
 .Then(_ => _.TryGet(projection, scope, key, token));
Exemple #21
0
 /// <inheritdoc/>
 public Task <Try <IAsyncEnumerable <ProjectionCurrentState> > > TryGetAll(ProjectionId projection, ScopeId scope, ExecutionContext context, CancellationToken token)
 => _executionContextCreator
 .TryCreateUsing(context)
 .Then(_ => _getProjectionStoreFor(_.Tenant))
 .Then(_ => _.TryGetAll(projection, scope, token));
Exemple #22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProjectionAlreadyRegistered"/> class.
 /// </summary>
 /// <param name="scope">The scope of the Projection that was already registered.</param>
 /// <param name="projection">The id of the Projection that was already registered.</param>
 public ProjectionAlreadyRegistered(ScopeId scope, ProjectionId projection)
     : base($"The projection {projection} in scope {scope} is already registered")
 {
 }
Exemple #23
0
 internal static void GettingAllProjections(this ILogger logger, ProjectionId projection, ScopeId scope)
 => _gettingAllProjections(logger, projection, scope, null);
Exemple #24
0
 internal static partial void Replay(ILogger logger, ProjectionId projection, ScopeId scope);
Exemple #25
0
 internal static partial void DroppingProjection(ILogger logger, ProjectionId projection, ScopeId scope);
Exemple #26
0
 internal static partial void ProjectionNotRegistered(ILogger logger, ProjectionId projection, ScopeId scope);
Exemple #27
0
    /// <inheritdoc/>
    public async Task <Try <IAsyncEnumerable <(ProjectionState State, ProjectionKey Key)> > > TryGetAll(ProjectionId projection, ScopeId scope, CancellationToken token)
    {
        try
        {
            var collection = await _projections.GetStates(scope, projection, token).ConfigureAwait(false);

            var states = collection
                         .Find(Builders <Projection> .Filter.Empty)
                         .Project(_ => new Tuple <ProjectionState, ProjectionKey>(_.ContentRaw, _.Key))
                         .ToAsyncEnumerable(token);

            var result = states.Select(_ =>
            {
                return(_.Item1, _.Item2);
            });

            return(Try <IAsyncEnumerable <(ProjectionState State, ProjectionKey Key)> > .Succeeded(result));
        }
        catch (Exception ex)
        {
            return(ex);
        }
    }
 /// <summary>
 /// Initializes a new instance of the <see cref="ReplayProjectionFailed"/> class.
 /// </summary>
 /// <param name="scope">The scope of the Projection.</param>
 /// <param name="projection">The id of the Projection.</param>
 /// <param name="reason">The reason why replaying the Projection failed.</param>
 public ReplayProjectionFailed(ScopeId scope, ProjectionId projection, string reason)
     : base($"Could not replay projection {projection} in scope {scope} because {reason}")
 {
 }
 FilterDefinition <ProjectionDefinition> CreateIdFilter(ProjectionId projection)
 => Builders <ProjectionDefinition> .Filter.Eq(_ => _.Projection, projection.Value);
 public static projection_definition_builder create(ProjectionId projection, ScopeId scope) => new(projection, scope);