Ejemplo n.º 1
0
        /// <summary>
        /// Applies the current rule set for selecting a join edge from a given set of options.
        /// </summary>
        /// <param name="joinGraph">The join graph.</param>
        /// <returns>
        /// The selected join edge.
        /// </returns>
        public jg::Edge ChooseJoinEdge(jg::Graph joinGraph)
        {
            //
            // we apply each rule in sequence to filter the set of available candidates until we
            // either 1) have only one option left or 2) arrive at the last rule, which is then
            // forced to make a choice

            var candidates = new List <jg::Edge>(joinGraph.Edges);

            for (int i = 0; i < m_rulesJoins.Count; i++)
            {
                //
                // if there's only one option left we can return this immediately

                if (candidates.Count == 1)
                {
                    return(candidates[0]);
                }

                if (i == m_rulesJoins.Count - 1)
                {
                    //
                    // last rule, so force it to choose

                    return(m_rulesJoins[i].Choose(m_context, candidates, joinGraph));
                }
                else
                {
                    //
                    // perform a filtering step

                    var filtered = new List <jg::Edge>(m_rulesJoins[i].Filter(m_context, candidates, joinGraph));
                    candidates = filtered;
                }
            }

            //
            // if, after application of all rules, we still have not arrived at a conclusive choice,
            // we simply select the first option from the candidates we have left

            if (candidates.Count > 0)
            {
                return(candidates[0]);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Merges a pair of given join nodes, causing all edges connecting to either of the two
        /// join nodes to connect to the newly formed node which merges the two.
        /// </summary>
        /// <param name="joinGraph">The join graph.</param>
        /// <param name="oldEdge">The old edge which is to be replaced.</param>
        /// <param name="newNode">The new node which takes merges the two nodes belonging to the given edge.</param>
        private static void MergeJoinNodes(jg::Graph joinGraph, jg::Edge oldEdge, jg::Node newNode)
        {
            //
            // remove the old edge and its nodes from the join graph

            joinGraph.Edges.Remove(oldEdge);
            joinGraph.Nodes.Remove(oldEdge.Left);
            joinGraph.Nodes.Remove(oldEdge.Right);

            //
            // any edges connecting to the removed nodes will be redirected to the new node that
            // merges the two

            foreach (var edge in joinGraph.Edges)
            {
                if (edge.Left.Equals(oldEdge.Left) || edge.Left.Equals(oldEdge.Right))
                {
                    edge.Left = newNode;
                }

                if (edge.Right.Equals(oldEdge.Left) || edge.Right.Equals(oldEdge.Right))
                {
                    edge.Right = newNode;
                }
            }

            //
            // we might end up with duplicate edges pointing to the new node, so make sure to
            // filter those out

            var uniqueEdges = joinGraph.Edges.Distinct().ToList();

            joinGraph.Edges.Clear();
            joinGraph.Edges.AddRange(uniqueEdges);

            //
            // insert the new node into the join graph

            joinGraph.Nodes.Add(newNode);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Computes a descriptive query plan for a given Basic Graph Pattern within a given
        /// database context.
        /// </summary>
        /// <param name="context">The database context.</param>
        /// <param name="pattern">The Basic Graph Pattern.</param>
        /// <returns>
        /// A descriptive query plan for the given BGP.
        /// </returns>
        public static QueryPlan ComputePlan(Database context, params Triple <TripleItem, TripleItem, TripleItem>[] pattern)
        {
            //
            // initialize the decision engine
            var decisionEngine = GetDecisionEngine(context);

            //
            // compute the atom collapse
            var collapse = AtomCollapse.Compute(pattern);

            //
            // step 1: compute join graph

            //
            // working copy of collapse graph nodes
            var currentCollapse = new List <ac::Node>();

            foreach (var node in collapse.Nodes)
            {
                currentCollapse.Add(node.Copy());
            }

            List <ac::Node> seeds;
            var             todo = new List <Triple <TripleItem, TripleItem, TripleItem> >();
            var             eval = new List <ac::Node>();

            do
            {
                //
                // compute the seeds
                seeds = ComputeSeeds(currentCollapse);

                //
                // pick one
                var seed = decisionEngine.ChooseSeed(seeds, collapse, currentCollapse).Copy();

                //
                // compute new todo's
                if (todo.Contains(seed.FirstSAP))
                {
                    todo.Remove(seed.FirstSAP);
                }
                foreach (var sap in ComputeNewTodos(seed, currentCollapse))
                {
                    todo.AddIfNotPresent(sap);
                }

                //
                // update the current collapse
                foreach (var node in currentCollapse)
                {
                    if (node.SAPs.Contains(seed.FirstSAP))
                    {
                        node.SAPs.Remove(seed.FirstSAP);
                    }
                }
                currentCollapse.RemoveAll(n => n.SAPs.Count == 0);

                //
                // add the seed to the evaluation list
                eval.Add(seed);
            } while (todo.Count > 0);

            //
            // compute edges
            var collapseEdges = new List <ac::Edge>();

            foreach (var edge in collapse.Edges)
            {
                foreach (var x in eval)
                {
                    foreach (var y in eval)
                    {
                        if (edge.Left.Equals(x) && edge.Right.Equals(y))
                        {
                            collapseEdges.Add(edge);
                        }
                    }
                }
            }

            //
            // create join graph nodes
            var joinNodes = new List <jg::Node>();

            foreach (var cNode in eval)
            {
                var jNode = new jg::Node(cNode.Item, cNode.FirstSAP);
                joinNodes.Add(jNode);
            }

            //
            // create join graph edges
            var joinEdges = new List <jg::Edge>();

            foreach (var cEdge in collapseEdges)
            {
                foreach (var x in joinNodes)
                {
                    foreach (var y in joinNodes)
                    {
                        if (cEdge.Left.Item.Equals(x.Item) && cEdge.Left.FirstSAP.Equals(x.SAP))
                        {
                            if (cEdge.Right.Item.Equals(y.Item) && cEdge.Right.FirstSAP.Equals(y.SAP))
                            {
                                var jEdge = new jg::Edge(x, y);
                                foreach (var lbl in cEdge.Labels)
                                {
                                    jEdge.Labels.Add(lbl);
                                }
                                joinEdges.Add(jEdge);
                            }
                        }
                    }
                }
            }

            //
            // make the join graph
            var joinGraph = new jg::Graph(joinNodes, joinEdges);

            //
            // step 2: compute query plan

            if (joinGraph.Edges.Count == 0)
            {
                var n = joinGraph.Nodes.ElementAt(0);
                n.Operator = decisionEngine.ChooseScan(n);
            }
            else
            {
                while (joinGraph.Nodes.Count > 1)
                {
                    //
                    // choose a join to perform
                    var joinEdge = decisionEngine.ChooseJoinEdge(joinGraph);

                    //
                    // compute the join node for this edge
                    var newNode = decisionEngine.ChooseJoinType(joinEdge);

                    //
                    // merge the nodes of the join edge
                    MergeJoinNodes(joinGraph, joinEdge, newNode);
                }
            }

            //
            // create plan object
            var plan = new QueryPlan(joinGraph.Nodes.ElementAt(0).Operator);

            //
            // assign memory to operators
            AssignMemory(context, plan.Root);

            //
            // return the query plan
            return(plan);
        }