Example #1
0
        public static async Task ApplyIndexingAsync <TEntity>(IMongoDbConnection connection, CancellationToken cancellationToken = default) where TEntity : class
        {
            if (HasAppliedIndexes.TryGetValue(typeof(TEntity), out var hasApplied) && hasApplied)
            {
                return;
            }

            var indexModel = IndexModelBuilder <TEntity> .BuildModel().ToArray();

            if (indexModel.Length > 0)
            {
                var definition = EntityMapping.GetOrCreateDefinition(typeof(TEntity));
                using (var diagnostics = DiagnosticRunner.Start(connection, indexModel))
                {
                    try
                    {
                        var collection = connection.GetDatabase().GetCollection <TEntity>(definition.CollectionName);
                        await collection.Indexes.CreateManyAsync(indexModel, cancellationToken).ConfigureAwait(false);

                        HasAppliedIndexes.TryAdd(typeof(TEntity), true);
                    }
                    catch (Exception exception)
                    {
                        diagnostics.Error(exception);
                        throw;
                    }
                }
            }
        }
        public async Task ApplyIndexingAsync(CancellationToken cancellationToken = default)
        {
            var indexModel = IndexModelBuilder <TEntity> .BuildModel();

            if (indexModel.Any())
            {
                using (var diagnostics = DiagnosticRunner.Start(Connection, indexModel))
                {
                    try
                    {
                        await GetCollection().Indexes.CreateManyAsync(indexModel, cancellationToken).ConfigureAwait(false);
                    }
                    catch (Exception exception)
                    {
                        diagnostics.Error(exception);
                        throw;
                    }
                }
            }
        }
        public void ApplyIndexing()
        {
            var indexModel = IndexModelBuilder <TEntity> .BuildModel();

            if (indexModel.Any())
            {
                using (var diagnostics = DiagnosticRunner.Start(Connection, indexModel))
                {
                    try
                    {
                        GetCollection().Indexes.CreateMany(indexModel);
                    }
                    catch (Exception exception)
                    {
                        diagnostics.Error(exception);
                        throw;
                    }
                }
            }
        }
        public async Task ApplyIndexingAsync(CancellationToken cancellationToken = default)
        {
            var indexModel = IndexModelBuilder <TEntity> .BuildModel();

            if (indexModel.Any())
            {
                var commandId = Guid.NewGuid();
                try
                {
                    Connection.DiagnosticListener.OnNext(new IndexDiagnosticCommand <TEntity>
                    {
                        CommandId    = commandId,
                        Source       = $"{nameof(EntityIndexWriter<TEntity>)}.{nameof(ApplyIndexingAsync)}",
                        CommandState = CommandState.Start,
                        IndexModel   = indexModel
                    });
                    await GetCollection().Indexes.CreateManyAsync(indexModel, cancellationToken).ConfigureAwait(false);

                    Connection.DiagnosticListener.OnNext(new IndexDiagnosticCommand <TEntity>
                    {
                        CommandId    = commandId,
                        Source       = $"{nameof(EntityIndexWriter<TEntity>)}.{nameof(ApplyIndexingAsync)}",
                        CommandState = CommandState.End,
                        IndexModel   = indexModel
                    });
                }
                catch (Exception ex)
                {
                    Connection.DiagnosticListener.OnNext(new IndexDiagnosticCommand <TEntity>
                    {
                        CommandId    = commandId,
                        Source       = $"{nameof(EntityIndexWriter<TEntity>)}.{nameof(ApplyIndexingAsync)}",
                        CommandState = CommandState.Error,
                        IndexModel   = indexModel
                    });
                    Connection.DiagnosticListener.OnError(ex);

                    throw;
                }
            }
        }
        public void ApplyIndexing()
        {
            var indexModel = IndexModelBuilder <TEntity> .BuildModel();

            if (indexModel.Any())
            {
                var commandId = Guid.NewGuid();
                try
                {
                    Connection.DiagnosticListener.OnNext(new IndexDiagnosticCommand <TEntity>
                    {
                        CommandId    = commandId,
                        Source       = $"{nameof(EntityIndexWriter<TEntity>)}.{nameof(ApplyIndexing)}",
                        CommandState = CommandState.Start,
                        IndexModel   = indexModel
                    });
                    GetCollection().Indexes.CreateMany(indexModel);
                    Connection.DiagnosticListener.OnNext(new IndexDiagnosticCommand <TEntity>
                    {
                        CommandId    = commandId,
                        Source       = $"{nameof(EntityIndexWriter<TEntity>)}.{nameof(ApplyIndexing)}",
                        CommandState = CommandState.End,
                        IndexModel   = indexModel
                    });
                }
                catch (Exception ex)
                {
                    Connection.DiagnosticListener.OnNext(new IndexDiagnosticCommand <TEntity>
                    {
                        CommandId    = commandId,
                        Source       = $"{nameof(EntityIndexWriter<TEntity>)}.{nameof(ApplyIndexing)}",
                        CommandState = CommandState.Error,
                        IndexModel   = indexModel
                    });
                    Connection.DiagnosticListener.OnError(ex);

                    throw;
                }
            }
        }