Beispiel #1
0
    /// <inheritdoc/>
    /// <remark>
    /// Returns all of the stored embedding states, even ones that have been marked as removed.
    /// <remark/>
    public async Task <Try <IEnumerable <(EmbeddingState State, ProjectionKey Key)> > > TryGetAll(
        EmbeddingId embedding,
        CancellationToken token)
    {
        try
        {
            var states = await OnEmbedding(
                embedding,
                async collection => await collection
                .Find(Builders <Embedding> .Filter.Empty)
                .Project(_ => Tuple.Create <EmbeddingState, ProjectionKey>(
                             new EmbeddingState(_.Content, _.Version, _.IsRemoved), _.Key))
                .ToListAsync(token)
                .ConfigureAwait(false),
                token).ConfigureAwait(false);

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

            return(Try <IEnumerable <(EmbeddingState State, ProjectionKey Key)> > .Succeeded(result));
        }
        catch (Exception ex)
        {
            return(ex);
        }
    }
Beispiel #2
0
    /// <inheritdoc/>
    public async Task <Try <EmbeddingCurrentState> > TryGet(EmbeddingId embedding, ProjectionKey key, CancellationToken token)
    {
        _logger.GettingOneEmbedding(embedding, key);

        var state = await _embeddingStates.TryGet(embedding, key, token);

        var result = state switch
        {
            { Success : true, Result : { IsRemoved : true } } => await TryGetInitialState(
Beispiel #3
0
 /// <summary>
 /// Initializes an instance of the <see cref="ProjectManyEvents" /> class.
 /// </summary>
 /// <param name="embedding">The <see cref="EmbeddingId"/> that identifies the embedding.</param>
 /// <param name="projection">The <see cref="IProjection"/> that is used to update the state.</param>
 /// <param name="initialState">The <see cref="ProjectionState"/> that is used to initialize newly created states.</param>
 /// <param name="logger">The <see cref="ILogger" />.</param>
 public ProjectManyEvents(
     EmbeddingId identifier,
     IEmbedding embedding,
     ProjectionState initialState,
     ILogger logger)
 {
     _identifier   = identifier;
     _embedding    = embedding;
     _initialState = initialState;
     _logger       = logger;
 }
Beispiel #4
0
 /// <summary>
 /// Initializes an instance of the <see cref="Embedding" /> class.
 /// </summary>
 /// <param name="embeddingId">The identifier for this embedding.</param>
 /// <param name="dispatcher">The reverse call dispatcher to use to dispatch update and delete calls to the client.</param>
 /// <param name="requestFactory">The factory to use to create requests to the client.</param>
 /// <param name="logger">The logger to use for logging.</param>
 public Embedding(
     EmbeddingId embeddingId,
     ReverseCallDispatcherType dispatcher,
     IEmbeddingRequestFactory requestFactory,
     ILogger logger)
 {
     _embeddingId    = embeddingId;
     _dispatcher     = dispatcher;
     _requestFactory = requestFactory;
     _logger         = logger;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="EmbeddingStateUpdater"/> class.
 /// </summary>
 /// <param name="embedding">The <see cref="EmbeddingId"/> that identifies the embedding.</param>
 /// <param name="committedEvents">The <see cref="IFetchCommittedEvents"/> that is used to fetch aggregate events.</param>
 /// <param name="embeddingStore">The <see cref="IEmbeddingStore"/> that is used to persist the states.</param>
 /// <param name="logger">The <see cref="ILogger"/>.</param>
 public EmbeddingStateUpdater(
     EmbeddingId embedding,
     IFetchCommittedEvents committedEvents,
     IEmbeddingStore embeddingStore,
     IProjectManyEvents projectManyEvents,
     ILogger logger)
 {
     _embedding         = embedding;
     _committedEvents   = committedEvents;
     _embeddingStore    = embeddingStore;
     _projectManyEvents = projectManyEvents;
     _logger            = logger;
 }
Beispiel #6
0
 bool TryGetRegisteredEmbeddingProcessorForTenant(TenantId tenant, EmbeddingId embedding, out IEmbeddingProcessor processor, out Failure failure)
 {
     failure = default;
     if (!_embeddingProcessors.TryGetEmbeddingProcessorFor(tenant, embedding, out processor))
     {
         failure = new Dolittle.Protobuf.Contracts.Failure
         {
             Id     = EmbeddingFailures.NoEmbeddingRegisteredForTenant.ToProtobuf(),
             Reason = $"No embedding with id {embedding.Value} registered for tenant {tenant.Value}"
         };
         return(false);
     }
     return(true);
 }
 /// <summary>
 /// Initializes an instance of the <see cref="StateTransitionEventsCalculator" /> class.
 /// </summary>
 /// <param name="identifier">The <see cref="EmbeddingId" />.</param>
 /// <param name="embedding">The <see cref="IEmbedding" />.</param>
 /// <param name="projector">The <see cref="IProjectManyEvents"/>.</param>
 /// <param name="stateComparer">The <see cref="ICompareStates"/>.</param>
 /// <param name="loopDetector">The <see cref="IDetectEmbeddingLoops"/>.</param>
 /// <param name="logger">The <see cref="ILogger"/>.</param>
 public StateTransitionEventsCalculator(
     EmbeddingId identifier,
     IEmbedding embedding,
     IProjectManyEvents projector,
     ICompareStates stateComparer,
     IDetectEmbeddingLoops loopDetector,
     ILogger logger)
 {
     _embeddingId   = identifier;
     _embedding     = embedding;
     _projector     = projector;
     _stateComparer = stateComparer;
     _loopDetector  = loopDetector;
     _logger        = logger;
 }
    /// <inheritdoc/>
    public IEmbeddingProcessor Create(EmbeddingId embeddingId, IEmbedding embedding, ProjectionState initialState, ExecutionContext executionContext)
    {
        var projectManyEvents = CreateProjectManyEvents(embeddingId, embedding, initialState);

        return(new EmbeddingProcessor(
                   embeddingId,
                   _tenant,
                   executionContext,
                   CreateEmbeddingStateUpdater(embeddingId, projectManyEvents),
                   _streamEventWatcher,
                   _eventStore,
                   _embeddingStore,
                   CreateStateTransitionEventsCalculator(embeddingId, embedding, projectManyEvents),
                   _loggerFactory.CreateLogger <EmbeddingProcessor>()));
    }
 /// <inheritdoc/>
 public async Task <Try <Store.Definition.EmbeddingDefinition> > TryGet(EmbeddingId embedding, CancellationToken token)
 {
     try
     {
         return(await OnDefinitions <Try <Store.Definition.EmbeddingDefinition> >(
                    async collection =>
         {
             var definition = await collection
                              .Find(CreateIdFilter(embedding))
                              .SingleOrDefaultAsync(token)
                              .ConfigureAwait(false);
             return definition == null
                     ? Try <Store.Definition.EmbeddingDefinition> .Failed(new EmbeddingDefinitionDoesNotExist(embedding))
                     : _definitionConverter.ToRuntime(definition);
         },
                    token).ConfigureAwait(false));
     }
     catch (Exception ex)
     {
         return(ex);
     }
 }
Beispiel #10
0
 /// <inheritdoc/>
 public async Task <Try <bool> > TryReplace(
     EmbeddingId embedding,
     ProjectionKey key,
     EmbeddingState state,
     CancellationToken token)
 {
     try
     {
         return(await OnEmbedding(
                    embedding,
                    async collection =>
         {
             var updateDefinition = Builders <Embedding>
                                    .Update
                                    .Set(_ => _.Content, state.State.Value)
                                    .Set(_ => _.Version, state.Version.Value)
                                    .Set(_ => _.IsRemoved, state.IsRemoved);
             var updateResult = await collection
                                .UpdateOneAsync(
                 CreateKeyFilter(key),
                 updateDefinition,
                 new UpdateOptions {
                 IsUpsert = true
             },
                 token)
                                .ConfigureAwait(false);
             return updateResult.IsAcknowledged;
         },
                    token).ConfigureAwait(false));
     }
     catch (MongoWaitQueueFullException)
     {
         return(false);
     }
     catch (Exception ex)
     {
         return(ex);
     }
 }
Beispiel #11
0
    /// <inheritdoc/>
    public async Task <Try> TryDrop(EmbeddingId embedding, CancellationToken token)
    {
        try
        {
            await OnEmbedding(
                embedding,
                async collection =>
            {
                var deleteResult = await collection
                                   .DeleteManyAsync(Builders <Embedding> .Filter.Empty, token)
                                   .ConfigureAwait(false);
                return(deleteResult.IsAcknowledged);
            },
                token).ConfigureAwait(false);

            return(Try.Succeeded());
        }
        catch (Exception ex)
        {
            return(ex);
        }
    }
Beispiel #12
0
 /// <inheritdoc/>
 public async Task <Try <bool> > TryMarkAsRemove(
     EmbeddingId embedding,
     ProjectionKey key,
     AggregateRootVersion version,
     CancellationToken token)
 {
     try
     {
         return(await OnEmbedding(
                    embedding,
                    async collection =>
         {
             var markDefinition = Builders <Embedding>
                                  .Update
                                  .Set(_ => _.IsRemoved, true)
                                  .Set(_ => _.Version, version.Value);
             var markResult = await collection
                              .UpdateOneAsync(
                 CreateKeyFilter(key),
                 markDefinition,
                 new UpdateOptions {
                 IsUpsert = true
             },
                 token)
                              .ConfigureAwait(false);
             return markResult.IsAcknowledged;
         },
                    token).ConfigureAwait(false));
     }
     catch (MongoWaitQueueFullException)
     {
         return(false);
     }
     catch (Exception ex)
     {
         return(ex);
     }
 }
Beispiel #13
0
    /// <inheritdoc/>
    public async Task <Try <EmbeddingState> > TryGet(
        EmbeddingId embeddingId,
        ProjectionKey key,
        CancellationToken token)
    {
        try
        {
            var embedding = await OnEmbedding(
                embeddingId,
                async collection => await collection
                .Find(CreateKeyFilter(key))
                .SingleOrDefaultAsync(token)
                .ConfigureAwait(false),
                token).ConfigureAwait(false);

            return(embedding == default
                ? Try <EmbeddingState> .Failed(new EmbeddingStateDoesNotExist(embeddingId, key))
                : new EmbeddingState(embedding.Content, embedding.Version, embedding.IsRemoved));
        }
        catch (Exception ex)
        {
            return(ex);
        }
    }
Beispiel #14
0
 async Task <TResult> OnEmbedding <TResult>(
     EmbeddingId embedding,
     Func <IMongoCollection <Embedding>, Task <TResult> > callback,
     CancellationToken token)
 => await callback(await _embeddings.GetStates(embedding, token).ConfigureAwait(false))
 .ConfigureAwait(false);
 /// <summary>
 /// Initializes a new instance of the <see cref="FailedProjectingEvents"/> class.
 /// </summary>
 /// <param name="embedding">The <see cref="EmbeddingId"/> </param>
 /// <param name="inner">The inner<see cref="Exception"/> </param>
 public FailedProjectingEvents(EmbeddingId embedding, Exception inner)
     : base($"A failure occurred while projecting events for embedding {embedding.Value}", inner)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="EmbeddingRemoteDeleteCallFailed"/> class.
 /// </summary>
 /// <param name="embedding">The <see cref="EmbeddingId"/> </param>
 public EmbeddingRemoteDeleteCallFailed(EmbeddingId embedding, string reason)
     : base($"A failure occurred during the remote Delete call for embedding {embedding.Value}. {reason}")
 {
 }
Beispiel #17
0
 /// <inheritdoc/>
 public Task <Try <IEnumerable <ProjectionKey> > > TryGetKeys(EmbeddingId embedding, ExecutionContext context, CancellationToken token)
 => _executionContextCreator
 .TryCreateUsing(context)
 .Then(_ => _getEmbeddingStoreFor(_.Tenant))
 .Then(_ => _.TryGetKeys(embedding, token));
 EmbeddingStateUpdater CreateEmbeddingStateUpdater(EmbeddingId embeddingId, IProjectManyEvents projectManyEvents)
 => new(embeddingId, _committedEventsFetcher, _embeddingStore, projectManyEvents, _loggerFactory.CreateLogger <EmbeddingStateUpdater>());
Beispiel #19
0
 /// <summary>
 /// Initializes an instance of the <see cref="FailedToRemoveEmbedding" /> class.
 /// </summary>
 /// <param name="embedding">The embedding identifier.</param>
 /// <param name="key">The projection key</param>
 /// <param name="version">The aggregate root version.</param>
 public FailedToRemoveEmbedding(EmbeddingId embedding, ProjectionKey key, AggregateRootVersion version)
     : base($"Failed to remove embedding with id {embedding.Value}, key {key.Value} and aggregate root version {version.Value}")
 {
 }
 /// <summary>
 /// Initializes an instance of the <see cref="FailedToGetEmbeddingKeys" /> class.
 /// </summary>
 /// <param name="embedding">The embedding identifier.</param>
 public FailedToGetEmbeddingKeys(EmbeddingId embedding)
     : base($"Failed to get embedding's keys, EmbeddingId: {embedding.Value}")
 {
 }
Beispiel #21
0
 internal static void ErrorGettingEmbeddingKeys(this ILogger logger, EmbeddingId embedding, Exception exception)
 => _errorGettingEmbeddingKeys(logger, embedding, exception);
Beispiel #22
0
 internal static void GettingEmbeddingKeys(this ILogger logger, EmbeddingId embedding)
 => _gettingEmbeddingKeys(logger, embedding, null);
Beispiel #23
0
 /// <inheritdoc/>
 public Task <Try <IEnumerable <EmbeddingCurrentState> > > TryGetAll(EmbeddingId embedding, ExecutionContext context, CancellationToken token)
 => _executionContextCreator
 .TryCreateUsing(context)
 .Then(_ => _getEmbeddingStoreFor(_.Tenant))
 .Then(_ => _.TryGetAll(embedding, token));
Beispiel #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EmbeddingProcessorNotStarted"/> class.
 /// </summary>
 /// <param name="embedding">The <see cref="EmbeddingId"/> </param>
 public EmbeddingProcessorNotStarted(EmbeddingId embedding)
     : base($"EmbeddingProcessor for embedding {embedding.Value} is not started")
 {
 }
 FilterDefinition <EmbeddingDefinition> CreateIdFilter(EmbeddingId embedding)
 => Builders <EmbeddingDefinition> .Filter.Eq(_ => _.Embedding, embedding.Value);
Beispiel #26
0
 internal static void RemovingEmbedding(this ILogger logger, EmbeddingId embedding, ProjectionKey key, AggregateRootVersion version)
 => _removingEmbedding(logger, embedding, key, version, null);
Beispiel #27
0
 internal static void ErrorRemovingEmbedding(this ILogger logger, EmbeddingId embedding, ProjectionKey key, AggregateRootVersion version, Exception exception)
 => _errorRemovingEmbedding(logger, embedding, key, version, exception);
Beispiel #28
0
 internal static void ReplacingEmbedding(this ILogger logger, EmbeddingId embedding, ProjectionKey key, AggregateRootVersion version, ProjectionState state)
 => _replacingEmbedding(logger, embedding, key, version, state, null);
Beispiel #29
0
 /// <summary>
 /// Initializes an instance of the <see cref="EmbeddingStateDoesNotExist" /> class.
 /// </summary>
 /// <param name="embedding">The embedding id.</param>
 /// <param name="key">The key to the embedding.</param>
 public EmbeddingStateDoesNotExist(EmbeddingId embedding, ProjectionKey key)
     : base($"An embedding state for embedding {embedding.Value} with key {key.Value} does not exist")
 {
 }
Beispiel #30
0
 /// <inheritdoc/>
 public Task <Try <EmbeddingCurrentState> > TryGetOne(EmbeddingId embedding, ProjectionKey key, ExecutionContext context, CancellationToken token)
 => _executionContextCreator
 .TryCreateUsing(context)
 .Then(_ => _getEmbeddingStoreFor(_.Tenant))
 .Then(_ => _.TryGet(embedding, key, token));