Ejemplo n.º 1
0
 public UpdateObject(string collection, Updator updator,
                     ITreePredicate filterPredicate)
 {
     _collection      = collection;
     _updator         = updator;
     _filterPredicate = filterPredicate;
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
 public SelectObject(bool isdistinct, List <IEvaluable> projections,
                     string collection, ITreePredicate filterPredicate, List <IEvaluable> groupValue,
                     List <IEvaluable> orderValue, IntegerConstantValue skip, IntegerConstantValue limit, string hint)
 {
     _isdistinct      = isdistinct;
     _projections     = projections;
     _collection      = collection;
     _filterPredicate = filterPredicate;
     _groupValue      = groupValue;
     _orderValue      = orderValue;
     _skip            = skip;
     _limit           = limit;
     _hint            = hint;
 }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 6
0
 public void Add(ITreePredicate item)
 {
     _predciates.Add(item);
 }
Ejemplo n.º 7
0
 public void AddExpression(ITreePredicate expression)
 {
     _treePredicates.Add(expression);
 }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
 public void AddTreePredicate(ITreePredicate predciate)
 {
     _treePredicates.Add(predciate);
 }
Ejemplo n.º 10
0
 public DeleteObject(string collection, ITreePredicate filterPredicate = null)
 {
     _collection      = collection;
     _fliterPredicate = filterPredicate;
 }
Ejemplo n.º 11
0
        public QueryPlan GetQueryPlan(IDmObject parsedQuery, IQuery query, IQueryStore queryStore, MetadataIndex rowsEnumerator)
        {
            //Todo: Plan's cache's key decision (query-string based plan cache/optimizable-section based cache).
            OrderedList <double, IProxyPredicate> sortedPlans = new OrderedList <double, IProxyPredicate>();
            var optimizableQuery = parsedQuery as IFilterObject;

            var criteria  = AddQueryCriteria(parsedQuery, query.Parameters, queryStore);
            var queryPlan = new QueryPlan {
                Criteria = criteria
            };

            if (optimizableQuery != null && optimizableQuery.WherePredicate != null)
            {
                ITreePredicate whereExpression = optimizableQuery.WherePredicate;

                ITreePredicate contractedExpression = whereExpression.Contract();

                if (contractedExpression == null)
                {
                    contractedExpression = whereExpression;
                }

                List <ITreePredicate> distribCombinations = new List <ITreePredicate>();
                distribCombinations.Add(contractedExpression);

                //Todo: Restrict this call if there doesn't exist a compound index.
                while (contractedExpression.Expand() != null)
                {
                    distribCombinations.Add(contractedExpression.Expand());
                    contractedExpression = contractedExpression.Expand();
                }

                foreach (var treePredicate in distribCombinations)
                {
                    if (treePredicate is OrTreePredicate || treePredicate is AndTreePredicate)
                    {
                        break;
                    }
                    if (treePredicate is ComparisonPredicate)
                    {
                        DocumentKey documentKey = null;
                        if (((ComparisonPredicate)treePredicate).TryGetProxyKeyPredicate(rowsEnumerator, out documentKey))
                        {
                            IProxyPredicate optimizablePredicate = new ProxyPredicate(new KeyPredicate(documentKey, rowsEnumerator), treePredicate);
                            sortedPlans.Add(optimizablePredicate.Statistics[Statistic.ExpectedIO], optimizablePredicate);
                            queryPlan.Predicate = sortedPlans.FirstValues[0].GetExecutionPredicate(queryStore);
                            return(queryPlan);
                        }
                    }
                }

                foreach (var expressionState in distribCombinations)
                {
                    IProxyPredicate optimizablePredicate = expressionState.GetProxyExecutionPredicate(_indexManager, queryStore, rowsEnumerator);
                    sortedPlans.Add(optimizablePredicate.Statistics[Statistic.ExpectedIO], optimizablePredicate);
                    //Todo: Add optimizedPredicate to the SortedList by the cost.
                }

                queryPlan.Predicate = sortedPlans.FirstValues[0].GetExecutionPredicate(queryStore);
            }
            else
            {
                if (criteria.GroupFields != null && criteria.GroupFields.Count == 1 && criteria.GroupFields[0] is AllField && criteria.ContainsAggregations)
                {
                    if (criteria.Aggregations.Count == 1 && criteria.Aggregations[0].Aggregation is COUNT &&
                        criteria.Aggregations[0].Evaluation is AllEvaluable)
                    {
                        queryPlan.Criteria.GroupByField = null;
                        queryPlan.Predicate             = new SpecialCountPredicate(rowsEnumerator.KeyCount);
                        queryPlan.IsSpecialExecution    = true;
                        return(queryPlan);
                    }
                }

                //Todo:1 Projection variable-based index assigning (Functions' arguments + attributes).
                queryPlan.Predicate = GetSelectAllPredicate(criteria, rowsEnumerator);
            }

            return(queryPlan);
        }
Ejemplo n.º 12
0
 public ProxyPredicate(IPredicate predicate, ITreePredicate expression)
 {
     _predicate  = predicate;
     _expression = expression;
 }
Ejemplo n.º 13
0
 public FilterPredicate(ITreePredicate condition, IIndex sourceIndex)
 {
     _condition = condition;
     source     = sourceIndex;
 }