Пример #1
0
        public async Task <IResultList <IAssetEntity> > QueryAsync(DomainId appId, DomainId?parentId, ClrQuery query)
        {
            using (Profiler.TraceMethod <MongoAssetRepository>("QueryAsyncByQuery"))
            {
                try
                {
                    query = query.AdjustToModel();

                    var filter = query.BuildFilter(appId, parentId);

                    var assetCount = Collection.Find(filter).CountDocumentsAsync();
                    var assetItems =
                        Collection.Find(filter)
                        .QueryLimit(query)
                        .QuerySkip(query)
                        .QuerySort(query)
                        .ToListAsync();

                    var(items, total) = await AsyncHelper.WhenAll(assetItems, assetCount);

                    return(ResultList.Create <IAssetEntity>(total, items));
                }
                catch (MongoQueryException ex) when(ex.Message.Contains("17406"))
                {
                    throw new DomainException(T.Get("common.resultTooLarge"));
                }
            }
        }
        public async Task <IResultList <IAssetEntity> > QueryAsync(Guid appId, Guid?parentId, ClrQuery query)
        {
            using (Profiler.TraceMethod <MongoAssetRepository>("QueryAsyncByQuery"))
            {
                try
                {
                    query = query.AdjustToModel();

                    var filter = query.BuildFilter(appId, parentId);

                    var assetCount = Collection.Find(filter).CountDocumentsAsync();
                    var assetItems =
                        Collection.Find(filter)
                        .AssetTake(query)
                        .AssetSkip(query)
                        .AssetSort(query)
                        .ToListAsync();

                    await Task.WhenAll(assetItems, assetCount);

                    return(ResultList.Create <IAssetEntity>(assetCount.Result, assetItems.Result));
                }
                catch (MongoQueryException ex)
                {
                    if (ex.Message.Contains("17406"))
                    {
                        throw new DomainException("Result set is too large to be retrieved. Use $top parameter to reduce the number of items.");
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Пример #3
0
        private string Q(ClrQuery query, bool useDraft = false)
        {
            var rendered =
                query.AdjustToModel(schemaDef, useDraft).BuildFilter <MongoContentEntity>().Filter
                .Render(Serializer, Registry).ToString();

            return(rendered);
        }
Пример #4
0
        private static string Q(ClrQuery query)
        {
            var rendered =
                query.AdjustToModel().BuildFilter <MongoAssetEntity>(false).Filter
                .Render(Serializer, Registry).ToString();

            return(rendered);
        }
Пример #5
0
        private string _Q(ClrQuery query)
        {
            var rendered =
                query.AdjustToModel(schemaDef).BuildFilter <MongoContentEntity>().Filter !
                .Render(Serializer, Registry).ToString();

            return(rendered);
        }
Пример #6
0
        private void AssertQuery(string expected, ClrQuery query, object?arg = null)
        {
            var rendered =
                query.AdjustToModel(appId).BuildFilter <MongoAssetEntity>(false).Filter !
                .Render(Serializer, Registry).ToString();

            var expectation = Cleanup(expected, arg);

            Assert.Equal(expectation, rendered);
        }
Пример #7
0
        public async Task <IResultList <IContentEntity> > DoAsync(IAppEntity app, ISchemaEntity schema, ClrQuery query, SearchScope scope)
        {
            Guard.NotNull(app);
            Guard.NotNull(schema);
            Guard.NotNull(query);

            try
            {
                query = query.AdjustToModel(schema.SchemaDef);

                List <Guid>?fullTextIds = null;

                if (!string.IsNullOrWhiteSpace(query.FullText))
                {
                    var searchFilter = SearchFilter.ShouldHaveSchemas(schema.Id);

                    fullTextIds = await indexer.SearchAsync(query.FullText, app, searchFilter, scope);

                    if (fullTextIds?.Count == 0)
                    {
                        return(ResultList.CreateFrom <IContentEntity>(0));
                    }
                }

                var filter = CreateFilter(schema.Id, fullTextIds, query);

                var contentCount = Collection.Find(filter).CountDocumentsAsync();
                var contentItems =
                    Collection.Find(filter)
                    .QueryLimit(query)
                    .QuerySkip(query)
                    .QuerySort(query)
                    .ToListAsync();

                await Task.WhenAll(contentItems, contentCount);

                foreach (var entity in contentItems.Result)
                {
                    entity.ParseData(schema.SchemaDef, serializer);
                }

                return(ResultList.Create <IContentEntity>(contentCount.Result, contentItems.Result));
            }
            catch (MongoQueryException ex)
            {
                if (ex.Message.Contains("17406"))
                {
                    throw new DomainException("Result set is too large to be retrieved. Use $top parameter to reduce the number of items.");
                }
                else
                {
                    throw;
                }
            }
        }
Пример #8
0
        public void Should_make_skip_statement()
        {
            var query = new ClrQuery {
                Skip = 3
            };
            var cursor = A.Fake <IFindFluent <MongoContentEntity, MongoContentEntity> >();

            cursor.ContentSkip(query.AdjustToModel(schemaDef, false));

            A.CallTo(() => cursor.Skip(3))
            .MustHaveHappened();
        }
Пример #9
0
        public void Should_make_take_statement()
        {
            var query = new ClrQuery {
                Take = 3
            };
            var cursor = A.Fake <IFindFluent <MongoAssetEntity, MongoAssetEntity> >();

            cursor.AssetTake(query.AdjustToModel());

            A.CallTo(() => cursor.Limit(3))
            .MustHaveHappened();
        }
Пример #10
0
        private void AssertQuery(string expected, ClrQuery query, object?arg = null)
        {
            var filter = query.AdjustToModel(appId).BuildFilter <MongoAssetEntity>(false).Filter !;

            var rendered =
                filter.Render(
                    BsonSerializer.SerializerRegistry.GetSerializer <MongoAssetEntity>(),
                    BsonSerializer.SerializerRegistry)
                .ToString();

            Assert.Equal(Cleanup(expected, arg), rendered);
        }
Пример #11
0
        public async Task <IResultList <IContentEntity> > DoAsync(IAppEntity app, ISchemaEntity schema, ClrQuery query, DomainId?referenced, SearchScope scope)
        {
            Guard.NotNull(app, nameof(app));
            Guard.NotNull(schema, nameof(schema));
            Guard.NotNull(query, nameof(query));

            try
            {
                query = query.AdjustToModel(schema.SchemaDef);

                List <DomainId>?fullTextIds = null;

                if (!string.IsNullOrWhiteSpace(query.FullText))
                {
                    var searchFilter = SearchFilter.ShouldHaveSchemas(schema.Id);

                    fullTextIds = await indexer.SearchAsync(query.FullText, app, searchFilter, scope);

                    if (fullTextIds?.Count == 0)
                    {
                        return(ResultList.CreateFrom <IContentEntity>(0));
                    }
                }

                var filter = CreateFilter(schema.AppId.Id, schema.Id, fullTextIds, query, referenced);

                var contentCount = Collection.Find(filter).CountDocumentsAsync();
                var contentItems = FindContentsAsync(query, filter);

                var(items, total) = await AsyncHelper.WhenAll(contentItems, contentCount);

                foreach (var entity in items)
                {
                    entity.ParseData(schema.SchemaDef, converter);
                }

                return(ResultList.Create <IContentEntity>(total, items));
            }
            catch (MongoCommandException ex) when(ex.Code == 96)
            {
                throw new DomainException(T.Get("common.resultTooLarge"));
            }
            catch (MongoQueryException ex) when(ex.Message.Contains("17406"))
            {
                throw new DomainException(T.Get("common.resultTooLarge"));
            }
        }
Пример #12
0
        public async Task <IResultList <IContentEntity> > QueryAsync(ISchemaEntity schema, ClrQuery query, List <Guid> ids, Status[] status, bool inDraft, bool includeDraft = true)
        {
            try
            {
                query = query.AdjustToModel(schema.SchemaDef, inDraft);

                var filter = query.ToFilter(schema.Id, ids, status);

                var contentCount = Collection.Find(filter).CountDocumentsAsync();
                var contentItems =
                    Collection.Find(filter)
                    .WithoutDraft(includeDraft)
                    .ContentTake(query)
                    .ContentSkip(query)
                    .ContentSort(query)
                    .ToListAsync();

                await Task.WhenAll(contentItems, contentCount);

                foreach (var entity in contentItems.Result)
                {
                    entity.ParseData(schema.SchemaDef, serializer);
                }

                return(ResultList.Create <IContentEntity>(contentCount.Result, contentItems.Result));
            }
            catch (MongoQueryException ex)
            {
                if (ex.Message.Contains("17406"))
                {
                    throw new DomainException("Result set is too large to be retrieved. Use $top parameter to reduce the number of items.");
                }
                else
                {
                    throw;
                }
            }
        }