Beispiel #1
0
        private List <Dictionary <string, object> > ProcessOperationQueryResponse(ChildrenDefinition qdef, PortalContext portalContext, ODataRequest req, out int count)
        {
            var cdef = new ChildrenDefinition
            {
                PathUsage            = qdef.PathUsage,
                ContentQuery         = qdef.ContentQuery,
                Top                  = req.Top > 0 ? req.Top : qdef.Top,
                Skip                 = req.Skip > 0 ? req.Skip : qdef.Skip,
                Sort                 = req.Sort != null && req.Sort.Count() > 0 ? req.Sort : qdef.Sort,
                EnableAutofilters    = req.AutofiltersEnabled != FilterStatus.Default ? req.AutofiltersEnabled : qdef.EnableAutofilters,
                EnableLifespanFilter = req.LifespanFilterEnabled != FilterStatus.Default ? req.AutofiltersEnabled : qdef.EnableLifespanFilter
            };

            var lucQuery = SnExpression.BuildQuery(req.Filter, typeof(Content), null, cdef);
            var result   = lucQuery.Execute();
            var idResult = result.Select(x => x.NodeId);

            //var count = req.InlineCount == InlineCount.AllPages ? ExecuteQueryWithCountOnly(lucQuery, cdef.EnableAutofilters, cdef.EnableLifespanFilter) : idResult.Count();
            count = req.InlineCount == InlineCount.AllPages ? lucQuery.TotalCount : idResult.Count();

            if (req.CountOnly)
            {
                return(null);
            }

            var contents   = new List <Dictionary <string, object> >();
            var projector  = Projector.Create(req, true);
            var missingIds = new List <int>();

            foreach (var id in idResult)
            {
                var content = Content.Load(id);
                if (content == null)
                {
                    // collect missing ids for logging purposes
                    missingIds.Add(id);
                    continue;
                }

                var fields = CreateFieldDictionary(content, portalContext, projector);
                contents.Add(fields);
            }

            if (missingIds.Count > 0)
            {
                // subtract missing count from result count
                count = Math.Max(0, count - missingIds.Count);

                // index anomaly: there are ids in the index that could not be loaded from the database
                Logger.WriteWarning(ContentRepository.EventId.Querying.MissingIds, "Missing ids found in the index that could not be loaded from the database. See id list below.",
                                    properties: new Dictionary <string, object>
                {
                    { "MissingIds", string.Join(", ", missingIds.OrderBy(id => id)) }
                });
            }

            return(contents);
        }
Beispiel #2
0
        public void Linq_CombiningQueries()
        {
            var childrenDef = new ChildrenDefinition {
                PathUsage = PathUsageMode.InFolderOr, ContentQuery = "Id:>42", EnableAutofilters = FilterStatus.Disabled, Skip = 18, Top = 15
            };
            var expr     = Content.All.Where(c => c.IsFolder == true).Skip(8).Take(5).Expression;
            var actual   = SnExpression.BuildQuery(expr, typeof(Content), "/Root/FakePath", childrenDef).ToString();
            var expected = "(+IsFolder:yes +Id:>42) InFolder:/root/fakepath .TOP:15 .SKIP:18 .AUTOFILTERS:OFF";

            Assert.AreEqual(expected, actual);
        }
Beispiel #3
0
        public void Linq_OptimizeBooleans()
        {
            var folder = Repository.Root;

            var childrenDef = new ChildrenDefinition {
                PathUsage = PathUsageMode.InFolderAnd
            };
            //var expr = Content.All.Where(c => c.Path != "/Root/A" && c.Path != "/Root/B" && c.Path != "/Root/C" && c.Type("Folder") && c.InFolder(folder)).Expression;
            var expr     = Content.All.Where(c => c.Name != "A" && c.Name != "B" && c.Name != "C" && c.TypeIs("Folder")).Expression;
            var actual   = SnExpression.BuildQuery(expr, typeof(Content), "/Root/FakePath", childrenDef).ToString();
            var expected = "+(+TypeIs:folder -Name:c -Name:b -Name:a) +InFolder:/root/fakepath";

            Assert.AreEqual(expected, actual);
        }
        private IEnumerable <Content> ProcessODataFilters(IEnumerable <Content> inputContents, PortalContext portalContext, ODataRequest req, out int?totalCount)
        {
            var x = inputContents;

            if (req.HasFilter)
            {
                var y = x as IQueryable <Content>;
                if (y != null)
                {
                    x = y.Where((Expression <Func <Content, bool> >)req.Filter);
                }
                else
                {
                    var filter     = SnExpression.GetCaseInsensitiveFilter(req.Filter);
                    var lambdaExpr = (LambdaExpression)filter;
                    x = x.Where((Func <Content, bool>)lambdaExpr.Compile());
                }
            }
            if (req.HasSort)
            {
                x = AddSortToCollectionExpression(x, req.Sort);
            }

            if (req.InlineCount == InlineCount.AllPages)
            {
                x          = x.ToList();
                totalCount = ((IList)x).Count;
            }
            else
            {
                totalCount = null;
            }

            if (req.HasSkip)
            {
                x = x.Skip(req.Skip);
            }
            if (req.HasTop)
            {
                x = x.Take(req.Top);
            }

            return(x);
        }
Beispiel #5
0
        public void Linq_API()
        {
            ContentSet <Content>[] contentSets =
            {
                (ContentSet <Content>)Content.All.Where(c => c.Id < 6),                                           // -, -
                (ContentSet <Content>)Content.All.EnableAutofilters().Where(c => c.Id < 6),                       // -, -
                (ContentSet <Content>)Content.All.DisableAutofilters().Where(c => c.Id < 6),                      //
                (ContentSet <Content>)Content.All.EnableLifespan().Where(c => c.Id < 6),                          //
                (ContentSet <Content>)Content.All.DisableLifespan().Where(c => c.Id < 6),                         //
                (ContentSet <Content>)Content.All.EnableAutofilters().EnableLifespan().Where(c => c.Id < 6),      //
                (ContentSet <Content>)Content.All.EnableAutofilters().DisableLifespan().Where(c => c.Id < 6),     //
                (ContentSet <Content>)Content.All.DisableAutofilters().EnableLifespan().Where(c => c.Id < 6),     //
                (ContentSet <Content>)Content.All.DisableAutofilters().DisableLifespan().Where(c => c.Id < 6),    //
                (ContentSet <Content>)Content.All.EnableLifespan().EnableAutofilters().Where(c => c.Id < 6),      //
                (ContentSet <Content>)Content.All.DisableLifespan().EnableAutofilters().Where(c => c.Id < 6),     //
                (ContentSet <Content>)Content.All.EnableLifespan().DisableAutofilters().Where(c => c.Id < 6),     //
                (ContentSet <Content>)Content.All.DisableLifespan().DisableAutofilters().Where(c => c.Id < 6),    //
            };
            var queries = new string[contentSets.Length];

            for (var i = 0; i < contentSets.Length; i++)
            {
                queries[i] = SnExpression.BuildQuery(contentSets[i].Expression, typeof(Content), contentSets[i].ContextPath, contentSets[i].ChildrenDefinition).ToString();
            }

            var expected = @"Id:<6
Id:<6
Id:<6 .AUTOFILTERS:OFF
Id:<6 .LIFESPAN:ON
Id:<6
Id:<6 .LIFESPAN:ON
Id:<6
Id:<6 .AUTOFILTERS:OFF .LIFESPAN:ON
Id:<6 .AUTOFILTERS:OFF
Id:<6 .LIFESPAN:ON
Id:<6
Id:<6 .AUTOFILTERS:OFF .LIFESPAN:ON
Id:<6 .AUTOFILTERS:OFF";
            var actual   = String.Join("\r\n", queries);

            Assert.AreEqual(expected, actual);
        }
Beispiel #6
0
        public void Linq_OptimizeBooleans_1()
        {
            // +(TypeIs:group TypeIs:user) +InFolder:/root/ims/builtin/demo/managers
            ChildrenDefinition childrenDef;

            System.Linq.Expressions.Expression expr;
            string actual;
            string expected;

            childrenDef = new ChildrenDefinition {
                PathUsage = PathUsageMode.InFolderAnd
            };
            expr     = Content.All.Where(c => c.ContentHandler is Group || c.ContentHandler is User).Expression;
            actual   = SnExpression.BuildQuery(expr, typeof(Content), "/Root/FakePath", childrenDef).ToString();
            expected = "+(TypeIs:user TypeIs:group) +InFolder:/root/fakepath";
            Assert.AreEqual(expected, actual);

            childrenDef = new ChildrenDefinition {
                PathUsage = PathUsageMode.InFolderAnd, ContentQuery = "Id:>0"
            };
            expr     = Content.All.Where(c => c.ContentHandler is Group || c.ContentHandler is User).Expression;
            actual   = SnExpression.BuildQuery(expr, typeof(Content), "/Root/FakePath", childrenDef).ToString();
            expected = "+(TypeIs:user TypeIs:group) +Id:>0 +InFolder:/root/fakepath";
            Assert.AreEqual(expected, actual);

            childrenDef = new ChildrenDefinition {
                PathUsage = PathUsageMode.InFolderAnd, ContentQuery = "TypeIs:user TypeIs:group"
            };
            actual   = SnExpression.BuildQuery(null, typeof(Content), "/Root/FakePath", childrenDef).ToString();
            expected = "+(TypeIs:user TypeIs:group) +InFolder:/root/fakepath";
            Assert.AreEqual(expected, actual);

            childrenDef = new ChildrenDefinition {
                PathUsage = PathUsageMode.InFolderAnd, ContentQuery = "+(TypeIs:user TypeIs:group)"
            };
            actual   = SnExpression.BuildQuery(null, typeof(Content), "/Root/FakePath", childrenDef).ToString();
            expected = "+(TypeIs:user TypeIs:group) +InFolder:/root/fakepath";
            Assert.AreEqual(expected, actual);
        }
Beispiel #7
0
        private List <Dictionary <string, object> > ProcessOperationQueryResponse(ChildrenDefinition qdef, PortalContext portalContext, ODataRequest req, out int count)
        {
            var cdef = new ChildrenDefinition
            {
                PathUsage            = qdef.PathUsage,
                ContentQuery         = qdef.ContentQuery,
                Top                  = req.Top > 0 ? req.Top : qdef.Top,
                Skip                 = req.Skip > 0 ? req.Skip : qdef.Skip,
                Sort                 = req.Sort != null && req.Sort.Count() > 0 ? req.Sort : qdef.Sort,
                EnableAutofilters    = req.AutofiltersEnabled != FilterStatus.Default ? req.AutofiltersEnabled : qdef.EnableAutofilters,
                EnableLifespanFilter = req.LifespanFilterEnabled != FilterStatus.Default ? req.AutofiltersEnabled : qdef.EnableLifespanFilter
            };

            var sourceCollectionItemType = typeof(Content);
            var lucQuery = SnExpression.BuildQuery(req.Filter, typeof(Content), null, cdef);
            var result   = lucQuery.Execute();
            var idResult = result.Select(x => x.NodeId);

            //var count = req.InlineCount == InlineCount.AllPages ? ExecuteQueryWithCountOnly(lucQuery, cdef.EnableAutofilters, cdef.EnableLifespanFilter) : idResult.Count();
            count = req.InlineCount == InlineCount.AllPages ? lucQuery.TotalCount : idResult.Count();

            if (req.CountOnly)
            {
                return(null);
            }

            var contents  = new List <Dictionary <string, object> >();
            var projector = Projector.Create(req, true);

            foreach (var id in idResult)
            {
                var content = Content.Load(id);
                var fields  = CreateFieldDictionary(content, portalContext, projector);
                contents.Add(fields);
            }
            return(contents);
        }
Beispiel #8
0
        private IEnumerable <ODataEntity> ProcessOperationQueryResponse(ChildrenDefinition qdef, ODataRequest req, HttpContext httpContext, out int count)
        {
            var queryText = qdef.ContentQuery;

            if (queryText.Contains("}}"))
            {
                queryText = ContentQuery.ResolveInnerQueries(qdef.ContentQuery, new QuerySettings
                {
                    EnableAutofilters    = qdef.EnableAutofilters,
                    EnableLifespanFilter = qdef.EnableLifespanFilter,
                    QueryExecutionMode   = qdef.QueryExecutionMode,
                    Sort = qdef.Sort
                });
            }

            var cdef = new ChildrenDefinition
            {
                PathUsage            = qdef.PathUsage,
                ContentQuery         = queryText,
                Top                  = req.Top > 0 ? req.Top : qdef.Top,
                Skip                 = req.Skip > 0 ? req.Skip : qdef.Skip,
                Sort                 = req.Sort != null && req.Sort.Any() ? req.Sort : qdef.Sort,
                CountAllPages        = req.HasInlineCount ? req.InlineCount == InlineCount.AllPages : qdef.CountAllPages,
                EnableAutofilters    = req.AutofiltersEnabled != FilterStatus.Default ? req.AutofiltersEnabled : qdef.EnableAutofilters,
                EnableLifespanFilter = req.LifespanFilterEnabled != FilterStatus.Default ? req.AutofiltersEnabled : qdef.EnableLifespanFilter,
                QueryExecutionMode   = req.QueryExecutionMode != QueryExecutionMode.Default ? req.QueryExecutionMode : qdef.QueryExecutionMode,
            };

            var snQuery = SnExpression.BuildQuery(req.Filter, typeof(Content), null, cdef);

            if (cdef.EnableAutofilters != FilterStatus.Default)
            {
                snQuery.EnableAutofilters = cdef.EnableAutofilters;
            }
            if (cdef.EnableLifespanFilter != FilterStatus.Default)
            {
                snQuery.EnableLifespanFilter = cdef.EnableLifespanFilter;
            }
            if (cdef.QueryExecutionMode != QueryExecutionMode.Default)
            {
                snQuery.QueryExecutionMode = cdef.QueryExecutionMode;
            }

            var result = snQuery.Execute(new SnQueryContext(null, User.Current.Id));

            // for optimization purposes this combined condition is examined separately
            if (req.InlineCount == InlineCount.AllPages && req.CountOnly)
            {
                count = result.TotalCount;
                return(null);
            }

            var ids = result.Hits.ToArray();

            count = req.InlineCount == InlineCount.AllPages ? result.TotalCount : ids.Length;
            if (req.CountOnly)
            {
                return(null);
            }

            var contents   = new List <ODataEntity>();
            var projector  = Projector.Create(req, true);
            var missingIds = new List <int>();

            foreach (var id in ids)
            {
                var content = Content.Load(id);
                if (content == null)
                {
                    // collect missing ids for logging purposes
                    missingIds.Add(id);
                    continue;
                }

                var fields = CreateFieldDictionary(content, projector, httpContext);
                contents.Add(fields);
            }

            if (missingIds.Count > 0)
            {
                // subtract missing count from result count
                count = Math.Max(0, count - missingIds.Count);

                // index anomaly: there are ids in the index that could not be loaded from the database
                SnLog.WriteWarning("Missing ids found in the index that could not be loaded from the database. See id list below.",
                                   EventId.Indexing,
                                   properties: new ODataEntity
                {
                    { "MissingIds", string.Join(", ", missingIds.OrderBy(id => id)) }
                });
            }

            return(contents);
        }