Esempio n. 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;
                    }
                }
            }
        }
        private async IAsyncEnumerable <TResult> ExecuteModelAsync <TResult>(AggregateExecutionModel model, [EnumeratorCancellation] CancellationToken cancellationToken)
        {
            var serializer = model.Serializer as IBsonSerializer <TResult>;
            var pipeline   = PipelineDefinition <TEntity, TResult> .Create(model.Stages, serializer);

            using (var diagnostics = DiagnosticRunner.Start <TEntity>(Connection, model))
            {
                IAsyncCursor <TResult> underlyingCursor;

                try
                {
                    underlyingCursor = await GetCollection().AggregateAsync(pipeline, cancellationToken: cancellationToken);
                }
                catch (Exception exception)
                {
                    diagnostics.Error(exception);
                    throw;
                }

                var hasFirstResult = false;
                while (await underlyingCursor.MoveNextAsync(cancellationToken))
                {
                    if (!hasFirstResult)
                    {
                        hasFirstResult = true;
                        diagnostics.FirstReadResult <TResult>();
                    }

                    var resultBatch = underlyingCursor.Current;
                    foreach (var item in resultBatch)
                    {
                        if (item is TEntity entityItem &&
                            (model.ResultTransformer == null ||
                             model.ResultTransformer.ReturnType == typeof(ValueTask <TEntity>) ||
                             model.ResultTransformer.ReturnType == typeof(Task <TEntity>)))
                        {
                            EntityProcessors.ProcessEntity(entityItem, Connection);
                        }

                        yield return(item);
                    }
                }
            }
        }
        public void Write(IEnumerable <IWriteCommand <TEntity> > writeCommands)
        {
            var writeModel = writeCommands.SelectMany(c => c.GetModel());

            if (writeModel.Any())
            {
                using (var diagnostics = DiagnosticRunner.Start(Connection, writeModel))
                {
                    try
                    {
                        GetCollection().BulkWrite(writeModel);
                    }
                    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;
                    }
                }
            }
        }
Esempio n. 6
0
        private IEnumerable <TResult> ExecuteModel <TResult>(AggregateExecutionModel model)
        {
            var serializer = model.Serializer as IBsonSerializer <TResult>;
            var pipeline   = PipelineDefinition <TEntity, TResult> .Create(model.Stages, serializer);

            using (var diagnostics = DiagnosticRunner.Start <TEntity>(Connection, model))
            {
                IEnumerable <TResult> underlyingResult;

                try
                {
                    underlyingResult = GetCollection().Aggregate(pipeline).ToEnumerable();
                }
                catch (Exception exception)
                {
                    diagnostics.Error(exception);
                    throw;
                }

                using (var enumerator = underlyingResult.GetEnumerator())
                {
                    var hasFirstResult = false;
                    while (enumerator.MoveNext())
                    {
                        if (!hasFirstResult)
                        {
                            hasFirstResult = true;
                            diagnostics.FirstReadResult <TResult>();
                        }

                        var item = enumerator.Current;
                        if (item is TEntity entityItem)
                        {
                            EntityProcessors.ProcessEntity(entityItem, Connection);
                        }
                        yield return(item);
                    }
                }
            }
        }
        public async Task WriteAsync(IEnumerable <IWriteCommand <TEntity> > writeCommands, CancellationToken cancellationToken = default(CancellationToken))
        {
            var writeModel = writeCommands.SelectMany(c => c.GetModel());

            cancellationToken.ThrowIfCancellationRequested();

            if (writeModel.Any())
            {
                using (var diagnostics = DiagnosticRunner.Start(Connection, writeModel))
                {
                    try
                    {
                        await GetCollection().BulkWriteAsync(writeModel, null, cancellationToken).ConfigureAwait(false);
                    }
                    catch (Exception exception)
                    {
                        diagnostics.Error(exception);
                        throw;
                    }
                }
            }
        }
Esempio n. 8
0
        public static async Task WriteAsync <TEntity>(IMongoDbConnection connection, IEnumerable <IWriteCommand> commands, WriteModelOptions options, CancellationToken cancellationToken = default) where TEntity : class
        {
            var writeModels = commands.OfType <IWriteCommand <TEntity> >().SelectMany(c => c.GetModel(options)).ToArray();

            if (writeModels.Any())
            {
                var entityDefinition = EntityMapping.GetOrCreateDefinition(typeof(TEntity));
                var collection       = connection.GetDatabase().GetCollection <TEntity>(entityDefinition.CollectionName);
                using (var diagnostics = DiagnosticRunner.Start(connection, writeModels))
                {
                    try
                    {
                        await collection.BulkWriteAsync(writeModels, cancellationToken : cancellationToken);
                    }
                    catch (Exception exception)
                    {
                        diagnostics.Error(exception);
                        throw;
                    }
                }
            }
        }