Пример #1
0
        public object Clone()
        {
            var copy = new AndTreePredicate();

            foreach (var expression in _predciates)
            {
                copy.Add((ITreePredicate)expression.Clone());
            }
            return(copy);
        }
Пример #2
0
        public ITreePredicate Expand()
        {
            foreach (var predciate in _predciates)
            {
                var orPredicate = predciate as OrTreePredicate;
                if (orPredicate != null)
                {
                    ITreePredicate orParent = null;
                    foreach (var childPredciate in orPredicate.TreePredicates)
                    {
                        orParent = orParent ?? new OrTreePredicate();
                        if (orPredicate.TreePredicates.Count > 1)
                        {
                            var andChild = new AndTreePredicate();
                            foreach (var sibling in _predciates)
                            {
                                if (sibling != orPredicate)
                                {
                                    andChild.Add(sibling);
                                }
                            }
                            andChild.Add(childPredciate);
                            ((OrTreePredicate)orParent).Add(andChild);
                        }
                        else
                        {
                            orParent = childPredciate;
                        }
                    }
                    return(orParent);
                }
            }

            foreach (var predciate in _predciates)
            {
                var resultPred = predciate.Expand();
                if (resultPred != null)
                {
                    ITreePredicate returnPredciate = new AndTreePredicate();
                    foreach (var sibling in _predciates)
                    {
                        if (sibling != predciate)
                        {
                            ((AndTreePredicate)returnPredciate).Add((ITreePredicate)sibling.Clone());
                        }
                    }
                    ((AndTreePredicate)returnPredciate).Add(resultPred);
                    return(returnPredciate);
                }
            }
            return(null);
        }
Пример #3
0
        public ITreePredicate Contract()
        {
            Dictionary <int, ITreePredicate> contractedPreds = new Dictionary <int, ITreePredicate>();

            for (int i = 0; i < _predciates.Count; i++)
            {
                ITreePredicate contractedPred = _predciates[i].Contract();
                if (contractedPred != null)
                {
                    while (contractedPred.Contract() != null)
                    {
                        contractedPred = contractedPred.Contract();
                    }
                    contractedPreds[i] = contractedPred;
                }
            }

            if (contractedPreds.Count.Equals(0))
            {
                return(null);
            }

            AndTreePredicate returnAnd = new AndTreePredicate();

            for (int i = 0; i < _predciates.Count; i++)
            {
                if (contractedPreds.ContainsKey(i))
                {
                    returnAnd.Add(contractedPreds[i]);
                }
                else
                {
                    returnAnd.Add(_predciates[i]);
                }
            }

            ITreePredicate contractedExpr = returnAnd;

            while (contractedExpr.Contract() != null)
            {
                contractedExpr = contractedExpr.Contract();
            }
            return(contractedExpr);
        }
Пример #4
0
        public override bool Equals(object obj)
        {
            if (!(obj is AndTreePredicate))
            {
                return(false);
            }

            AndTreePredicate andObject = (AndTreePredicate)obj;

            if (!_predciates.Count.Equals(andObject.TreePredicates.Count))
            {
                return(false);
            }

            foreach (var expression in andObject.TreePredicates)
            {
                if (!_predciates.Contains(expression))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #5
0
        /// <summary>
        /// Tries to assign compound indexes to expressions if applicable...
        /// </summary>
        private static List <ProxyAndPredicate> AssignCompoundIndices(ProxyAndPredicate set,
                                                                      IEnumerable <IIndex> indexes, IQueryStore queryStore)
        {
            var attribExprList  = new OrderedList <string, ComparisonPredicate>();
            var proxyPredicates = new List <ProxyAndPredicate>();

            //For every expression in the set ready for assignment.
            foreach (var expresson in set.TreePredicates)
            {
                var expression = (ComparisonPredicate)expresson;
                if (expression.IsBinaryExpression)
                {
                    continue;
                }
                attribExprList.Add(expression.AttributeNames[0], expression);
            }

            //Each compound index will create a new state of the expression.
            foreach (var index in indexes)
            {
                int matchedNumber  = 0;
                var matchedAttribs = new List <string>();

                //If keys (attributes list) does not contain the first prefix of the index
                //Then the index is not usable.
                if (attribExprList.ContainsKey(index.Attributes.Name))
                {
                    matchedAttribs.Add(index.Attributes.Name);
                    matchedNumber += attribExprList[index.Attributes.Name].Count;
                }


                //if matched attributes are < 2 no need of compound assignment.
                if (matchedAttribs.Count < 2)
                {
                    continue;
                }

                //Assign each of them an index and get them to the predicate, and get the one with the lowest cost.
                ComparisonPredicate cheapestExpression = attribExprList[matchedAttribs[0]][0];
                IPredicate          cheapestPredicate  = cheapestExpression.AssignIndexPredicate(index, queryStore);
                for (int i = 1; i < attribExprList[matchedAttribs[0]].Count; i++)
                {
                    ComparisonPredicate tempExpression = attribExprList[matchedAttribs[0]][i];
                    IPredicate          tempPredicate  = tempExpression.AssignIndexPredicate(index, queryStore);

                    if (tempPredicate.Statistics[Statistic.ExpectedIO] <
                        cheapestPredicate.Statistics[Statistic.ExpectedIO])
                    {
                        cheapestExpression = tempExpression;
                        cheapestPredicate  = tempPredicate;
                    }
                }

                //For filteration and adding rest of expressions to the index.
                ITreePredicate assingedExpression = null;
                if (!matchedNumber.Equals(2))
                {
                    AndTreePredicate assingedAnd = new AndTreePredicate();
                    foreach (var attribute in matchedAttribs)
                    {
                        foreach (var expression in attribExprList[attribute])
                        {
                            if (expression != cheapestExpression)
                            {
                                assingedAnd.Add(expression);
                            }
                        }
                    }
                    assingedExpression = assingedAnd;
                }
                else
                {
                    foreach (var expression in attribExprList[matchedAttribs[1]])
                    {
                        assingedExpression = expression;
                    }
                }

                FilterPredicate filterPredicate = new FilterPredicate(assingedExpression, index);
                filterPredicate.AddChildPredicate(cheapestPredicate);
                ProxyAndPredicate proxyAnd = (ProxyAndPredicate)set.Clone();

                //Removing assigned expressions.
                foreach (var attribute in matchedAttribs)
                {
                    foreach (var expression in attribExprList[attribute])
                    {
                        proxyAnd.TreePredicates.Remove(expression);
                    }
                }

                proxyAnd.AddChildPredicate(new ProxyPredicate(filterPredicate, null));
                proxyPredicates.Add(proxyAnd);
            }

            foreach (var proxyAnd in proxyPredicates)
            {
                //Recursive call for each ProxyAndPredicate.
                if (proxyAnd.TreePredicates.Count > 1)
                {
                    var values = AssignCompoundIndices(proxyAnd, indexes, queryStore);
                    foreach (var value in values)
                    {
                        proxyPredicates.Add(value);
                    }
                }
            }
            return(proxyPredicates);
        }
Пример #6
0
        /// <summary>
        /// Assigns a proxy predicate to a single-attributed expressions.
        /// </summary>
        private static void AssignSingleAttributesPredicate(ProxyAndPredicate proxyAnd,
                                                            ICollection <IIndex> indexes, IQueryStore queryStore)
        {
            OrderedList <string, ComparisonPredicate> attribExprList = new OrderedList <string, ComparisonPredicate>();

            foreach (var value in proxyAnd.TreePredicates)
            {
                var expression = (ComparisonPredicate)value;
                if (expression.IsBinaryExpression)
                {
                    continue;
                }
                attribExprList.Add(expression.AttributeNames[0], expression);
            }

            if (attribExprList.Count > 0)
            {
                foreach (var attribute in attribExprList.Keys)
                {
                    IIndex lowestIndex = null;
                    foreach (var index in indexes)
                    {
                        if (attribute.Equals(index.Attributes.Name))
                        {
                            if (lowestIndex == null)
                            {
                                lowestIndex = index;
                            }
                            else if (lowestIndex.ValueCount > index.ValueCount)
                            {
                                lowestIndex = index;
                            }
                        }
                    }

                    if (lowestIndex != null)
                    {
                        foreach (var expression in attribExprList[attribute])
                        {
                            proxyAnd.AddChildPredicate(
                                new ProxyPredicate(expression.AssignIndexPredicate(lowestIndex, queryStore), expression));
                            proxyAnd.TreePredicates.Remove(expression);
                        }
                    }
                }

                //For every unassigned expression...
                if (proxyAnd.TreePredicates.Count > 0)
                {
                    ITreePredicate assingedExpression;
                    if (proxyAnd.TreePredicates.Count.Equals(1))
                    {
                        assingedExpression = proxyAnd.TreePredicates[0];
                    }
                    else
                    {
                        AndTreePredicate assingedAnd = new AndTreePredicate();
                        foreach (var expression in proxyAnd.TreePredicates)
                        {
                            assingedAnd.Add(expression);
                        }
                        assingedExpression = assingedAnd;
                    }
                    proxyAnd.AddChildPredicate(new ProxyPredicate(
                                                   new StorePredicate(assingedExpression, queryStore), assingedExpression));
                }
            }
        }
Пример #7
0
        public ITreePredicate Contract()
        {
            if (!IsTerminal)
            {
                Dictionary <int, ITreePredicate> contractedPreds = new Dictionary <int, ITreePredicate>();
                for (int i = 0; i < _predciates.Count; i++)
                {
                    ITreePredicate contracted = _predciates[i].Contract();
                    if (contracted != null)
                    {
                        while (contracted.Contract() != null)
                        {
                            contracted = contracted.Contract();
                        }
                        contractedPreds[i] = contracted;
                    }
                }

                if (contractedPreds.Count.Equals(0))
                {
                    return(null);
                }

                OrTreePredicate returnOr = new OrTreePredicate();
                for (int i = 0; i < _predciates.Count; i++)
                {
                    if (contractedPreds.ContainsKey(i))
                    {
                        returnOr.Add(contractedPreds[i]);
                    }
                    else
                    {
                        returnOr.Add(_predciates[i]);
                    }
                }

                ITreePredicate contractedPred = returnOr;
                while (contractedPred.Contract() != null)
                {
                    contractedPred = contractedPred.Contract();
                }
                return(contractedPred);
            }

            List <AndTreePredicate> ands = new List <AndTreePredicate>();

            foreach (var predciate in _predciates)
            {
                if (!(predciate is AndTreePredicate))
                {
                    return(null);
                }

                ands.Add((AndTreePredicate)(predciate));
            }

            if (ands.Count < 2)
            {
                return(null);
            }

            ITreePredicate commonPred = null;

            foreach (var expression in ands[0].TreePredicates)
            {
                commonPred = expression;
                for (int i = 1; i < ands.Count; i++)
                {
                    if (ands[i].TreePredicates.Count < 2 ||
                        !ands[i].TreePredicates.Contains(expression))
                    {
                        return(null);
                    }
                }
            }

            AndTreePredicate returnPred = new AndTreePredicate();

            returnPred.Add(commonPred);

            OrTreePredicate orChild = new OrTreePredicate();

            foreach (var and in ands)
            {
                foreach (var predciate in and.TreePredicates)
                {
                    if (!predciate.Equals(commonPred))
                    {
                        orChild.Add(predciate);
                    }
                }
            }

            returnPred.Add(orChild);
            ITreePredicate contractPred = returnPred;

            while (contractPred.Contract() != null)
            {
                contractPred = contractPred.Contract();
            }
            return(contractPred);
        }