Ejemplo n.º 1
0
 internal ContentSet(bool countOnly, bool headersOnly, ChildrenDefinition childrenDef, string contextPath)
 {
     CountOnlyEnabled   = countOnly;
     HeadersOnlyEnabled = headersOnly;
     ChildrenDefinition = childrenDef;
     ContextPath        = contextPath;
 }
Ejemplo n.º 2
0
        internal Query GetQuery(Type sourceCollectionItemType, ChildrenDefinition childrenDef)
        {
            if (sourceCollectionItemType != null && sourceCollectionItemType != typeof(Content))
            {
                _queries.Push(CreateTypeIsQuery(sourceCollectionItemType));
                if (_queries.Count > 1)
                {
                    CombineTwoQueriesOnStack();
                }
            }
            if (_queries.Count == 0)
            {
                return(null);
            }
            if (_queries.Count > 1)
            {
                CombineAllQueriesOnStack();
            }
            var bmq = _queries.Peek() as BooleanMemberQuery;

            if (bmq != null)
            {
                _queries.Pop();
                _queries.Push(CreateTermQuery(bmq.FieldName, bmq.Value));
            }
            return(_queries.Peek());
        }
Ejemplo n.º 3
0
 internal SnQueryPredicate GetPredicate(Type sourceCollectionItemType, ChildrenDefinition childrenDef)
 {
     if (sourceCollectionItemType != null && sourceCollectionItemType != typeof(Content))
     {
         _predicates.Push(CreateTypeIsPredicate(sourceCollectionItemType));
         if (_predicates.Count > 1)
         {
             CombineTwoPredicatesOnStack();
         }
     }
     if (_predicates.Count == 0)
     {
         return(null);
     }
     if (_predicates.Count > 1)
     {
         CombineAllPredicatesOnStack();
     }
     if (_predicates.Peek() is BooleanMemberPredicate bmq)
     {
         _predicates.Pop();
         _predicates.Push(CreateTextPredicate(bmq.FieldName, bmq.Value));
     }
     return(_predicates.Peek());
 }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public static LucQuery BuildQuery(System.Linq.Expressions.Expression expression, Type sourceCollectionItemType, string contextPath, QuerySettings settings)
        {
            var childrenDef = new ChildrenDefinition
            {
                PathUsage            = PathUsageMode.InFolderAnd,
                Top                  = settings.Top,
                Skip                 = settings.Skip,
                Sort                 = settings.Sort,
                EnableAutofilters    = settings.EnableAutofilters,
                EnableLifespanFilter = settings.EnableLifespanFilter
            };

            return(BuildLucQuery(expression, sourceCollectionItemType, contextPath, childrenDef));
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
0
        private static LucQuery BuildLucQuery(System.Linq.Expressions.Expression expression, Type sourceCollectionItemType, string contextPath, ChildrenDefinition childrenDef)
        {
            Query q0 = null;

            CQVisitor v = null;

            // #1 compiling linq expression
            if (expression != null)
            {
                var v1    = new SetExecVisitor();
                var expr1 = v1.Visit(expression);
                var expr2 = expr1;
                if (v1.HasParameter)
                {
                    var v2 = new ExecutorVisitor(v1.GetExpressions());
                    expr2 = v2.Visit(expr1);
                }
                v = new CQVisitor();
                v.Visit(expr2);
                q0 = v.GetQuery(sourceCollectionItemType, childrenDef);
            }

            // #2 combining with additional query clause
            LucQuery lq = null;

            if (!string.IsNullOrEmpty(childrenDef.ContentQuery))
            {
                lq = LucQuery.Parse(childrenDef.ContentQuery);
                if (q0 == null)
                {
                    q0 = lq.Query;
                }
                else
                {
                    q0 = CombineQueries(q0, lq.Query);
                }
            }

            // #3 combining with context path
            if (q0 == null)
            {
                if (childrenDef != null && childrenDef.PathUsage != PathUsageMode.NotUsed && contextPath != null)
                {
                    q0 = GetPathQuery(contextPath, childrenDef.PathUsage == PathUsageMode.InTreeAnd || childrenDef.PathUsage == PathUsageMode.InTreeOr);
                }
            }
            else
            {
                if (childrenDef != null && childrenDef.PathUsage != PathUsageMode.NotUsed && contextPath != null)
                {
                    q0 = CombinePathQuery(q0, contextPath, childrenDef.PathUsage);
                }
            }

            // #4 empty query is invalid in this place
            if (q0 == null)
            {
                throw new NotSupportedException("Cannot execute empty query. Expression: " + expression);
            }

            var q1 = OptimizeBooleans(q0);

            // #5 configuring query by linq expression (the smallest priority)
            var query = LucQuery.Create(q1);

            if (v != null)
            {
                query.Skip          = v.Skip;
                query.Top           = v.Top;
                query.CountOnly     = v.CountOnly;
                query.SortFields    = v.SortFields.ToArray();
                query.ThrowIfEmpty  = v.ThrowIfEmpty;
                query.ExistenceOnly = v.ExistenceOnly;
            }
            // #6 configuring query by children definition
            if (childrenDef != null)
            {
                if (childrenDef.Skip > 0)
                {
                    query.Skip = childrenDef.Skip;
                }
                if (childrenDef.Top > 0)
                {
                    query.Top = childrenDef.Top;
                }
                if (childrenDef.Sort != null)
                {
                    query.SetSort(childrenDef.Sort);
                }
                if (childrenDef.CountAllPages != null)
                {
                    query.CountAllPages = childrenDef.CountAllPages.Value;
                }
                if (childrenDef.EnableAutofilters != FilterStatus.Default)
                {
                    query.EnableAutofilters = childrenDef.EnableAutofilters;
                }
                if (childrenDef.EnableLifespanFilter != FilterStatus.Default)
                {
                    query.EnableLifespanFilter = childrenDef.EnableLifespanFilter;
                }
                if (childrenDef.QueryExecutionMode != QueryExecutionMode.Default)
                {
                    query.QueryExecutionMode = childrenDef.QueryExecutionMode;
                }
            }

            // #7 configuring query by additional query clause (the biggest priority)
            if (lq != null)
            {
                if (lq.Skip > 0)
                {
                    query.Skip = lq.Skip;
                }
                if (lq.Top > 0)
                {
                    query.Top = lq.Top;
                }
                if (lq.SortFields != null && lq.SortFields.Length > 0)
                {
                    query.SortFields = lq.SortFields;
                }
                if (lq.EnableAutofilters != FilterStatus.Default)
                {
                    query.EnableAutofilters = lq.EnableAutofilters;
                }
                if (lq.EnableLifespanFilter != FilterStatus.Default)
                {
                    query.EnableLifespanFilter = lq.EnableLifespanFilter;
                }
                if (lq.QueryExecutionMode != QueryExecutionMode.Default)
                {
                    query.QueryExecutionMode = lq.QueryExecutionMode;
                }
            }

            return(query);
        }
Ejemplo n.º 12
0
 public static LucQuery BuildQuery(System.Linq.Expressions.Expression expression, Type sourceCollectionItemType, string contextPath, ChildrenDefinition childrenDef)
 {
     return(BuildLucQuery(expression, sourceCollectionItemType, contextPath, childrenDef));
 }
Ejemplo n.º 13
0
 public static SnQuery BuildQuery(Expression expression, Type sourceCollectionItemType, string contextPath, ChildrenDefinition childrenDef, out string elementSelection)
 {
     return(BuildSnQuery(expression, sourceCollectionItemType, contextPath, childrenDef, out elementSelection));
 }
Ejemplo n.º 14
0
 public ChildrenContentSet(Expression expression, string path, bool subTree, ChildrenDefinition childrenDef) : base(expression, childrenDef, path)
 {
     SubTree = subTree;
 }
Ejemplo n.º 15
0
 public ChildrenContentSet(string path, bool headersOnly, ChildrenDefinition childrenDef) : base(false, headersOnly, childrenDef, path)
 {
     SubTree = childrenDef.PathUsage == PathUsageMode.InTreeAnd;
 }
Ejemplo n.º 16
0
        private static SnQuery BuildSnQuery(Expression expression, Type sourceCollectionItemType, string contextPath, ChildrenDefinition childrenDef, out string elementSelection)
        {
            SnQueryPredicate q0 = null;

            elementSelection = null;

            SnLinqVisitor v = null;

            // #1 compiling linq expression
            if (expression != null)
            {
                var v1    = new SetExecVisitor();
                var expr1 = v1.Visit(expression);
                var expr2 = expr1;
                if (v1.HasParameter)
                {
                    var v2 = new ExecutorVisitor(v1.GetExpressions());
                    expr2 = v2.Visit(expr1);
                }
                v = new SnLinqVisitor();
                v.Visit(expr2);
                q0 = v.GetPredicate(sourceCollectionItemType, childrenDef);
                elementSelection = v.ElementSelection;
            }

            // #2 combining with additional query clause
            SnQuery lq = null;

            if (!string.IsNullOrEmpty(childrenDef?.ContentQuery))
            {
                var queryText = TemplateManager.Replace(typeof(ContentQueryTemplateReplacer), childrenDef.ContentQuery);

                lq = SnQuery.Parse(queryText, new SnQueryContext(QuerySettings.Default, User.Current.Id));
                q0 = q0 == null
                    ? lq.QueryTree
                    : CombineQueries(q0, lq.QueryTree);
            }

            // #3 combining with context path
            if (q0 == null)
            {
                if (childrenDef != null && childrenDef.PathUsage != PathUsageMode.NotUsed && contextPath != null)
                {
                    q0 = GetPathPredicate(contextPath, childrenDef.PathUsage == PathUsageMode.InTreeAnd || childrenDef.PathUsage == PathUsageMode.InTreeOr);
                }
            }
            else
            {
                if (childrenDef != null && childrenDef.PathUsage != PathUsageMode.NotUsed && contextPath != null)
                {
                    q0 = CombinePathPredicate(q0, contextPath, childrenDef.PathUsage);
                }
            }

            // #4 empty query substitution
            if (q0 == null)
            {
                q0 = new RangePredicate(IndexFieldName.NodeId, new IndexValue(0), null, true, false);
            }

            var q1 = OptimizeBooleans(q0);

            // #5 configuring query by linq expression (the smallest priority)
            var query = SnQuery.Create(q1);

            if (v != null)
            {
                query.Skip          = v.Skip;
                query.Top           = v.Top;
                query.CountOnly     = v.CountOnly;
                query.Sort          = v.Sort.ToArray();
                query.ThrowIfEmpty  = v.ThrowIfEmpty;
                query.ExistenceOnly = v.ExistenceOnly;
            }
            // #6 configuring query by children definition
            if (childrenDef != null)
            {
                if (childrenDef.Skip > 0)
                {
                    query.Skip = childrenDef.Skip;
                }
                if (childrenDef.Top > 0)
                {
                    query.Top = childrenDef.Top;
                }
                if (childrenDef.Sort != null)
                {
                    query.Sort = childrenDef.Sort.ToArray();
                }
                if (childrenDef.CountAllPages != null)
                {
                    query.CountAllPages = childrenDef.CountAllPages.Value;
                }
                if (childrenDef.EnableAutofilters != FilterStatus.Default)
                {
                    query.EnableAutofilters = childrenDef.EnableAutofilters;
                }
                if (childrenDef.EnableLifespanFilter != FilterStatus.Default)
                {
                    query.EnableLifespanFilter = childrenDef.EnableLifespanFilter;
                }
                if (childrenDef.QueryExecutionMode != QueryExecutionMode.Default)
                {
                    query.QueryExecutionMode = childrenDef.QueryExecutionMode;
                }
            }

            // #7 configuring query by additional query clause (the biggest priority)
            if (lq != null)
            {
                if (lq.Skip > 0)
                {
                    query.Skip = lq.Skip;
                }
                if (lq.Top > 0 && lq.Top != int.MaxValue)
                {
                    query.Top = lq.Top;
                }
                if (lq.Sort != null && lq.Sort.Length > 0)
                {
                    query.Sort = lq.Sort;
                }
                if (lq.EnableAutofilters != FilterStatus.Default)
                {
                    query.EnableAutofilters = lq.EnableAutofilters;
                }
                if (lq.EnableLifespanFilter != FilterStatus.Default)
                {
                    query.EnableLifespanFilter = lq.EnableLifespanFilter;
                }
                if (lq.QueryExecutionMode != QueryExecutionMode.Default)
                {
                    query.QueryExecutionMode = lq.QueryExecutionMode;
                }
                if (lq.AllVersions)
                {
                    query.AllVersions = true;
                }
            }

            return(query);
        }
Ejemplo n.º 17
0
 internal static SnQuery GetSnQuery(Expression expr, bool autoFiltersEnabled, bool lifespanEnabled, ChildrenDefinition childrenDef, string contextPath)
 {
     return(SnExpression.BuildQuery(expr, typeof(T), contextPath, childrenDef));
 }
Ejemplo n.º 18
0
 internal ContentSet(ChildrenDefinition childrenDef, string contextPath)
 {
     ChildrenDefinition = childrenDef;
     ContextPath        = contextPath;
 }
Ejemplo n.º 19
0
 internal ContentSet(Expression expression, ChildrenDefinition childrenDef, string contextPath)
 {
     _expression        = expression;
     ChildrenDefinition = childrenDef;
     ContextPath        = contextPath;
 }