示例#1
0
 /// <returns>the total number of leaf nodes</returns>
 public virtual int GetNumOfLeaves()
 {
     netlock.ReadLock().Lock();
     try
     {
         return(clusterMap.GetNumOfLeaves());
     }
     finally
     {
         netlock.ReadLock().Unlock();
     }
 }
示例#2
0
        private Node ChooseRandom(string scope, string excludedScope)
        {
            if (excludedScope != null)
            {
                if (scope.StartsWith(excludedScope))
                {
                    return(null);
                }
                if (!excludedScope.StartsWith(scope))
                {
                    excludedScope = null;
                }
            }
            Node node = GetNode(scope);

            if (!(node is NetworkTopology.InnerNode))
            {
                return(node);
            }
            NetworkTopology.InnerNode innerNode = (NetworkTopology.InnerNode)node;
            int numOfDatanodes = innerNode.GetNumOfLeaves();

            if (excludedScope == null)
            {
                node = null;
            }
            else
            {
                node = GetNode(excludedScope);
                if (!(node is NetworkTopology.InnerNode))
                {
                    numOfDatanodes -= 1;
                }
                else
                {
                    numOfDatanodes -= ((NetworkTopology.InnerNode)node).GetNumOfLeaves();
                }
            }
            if (numOfDatanodes == 0)
            {
                throw new NetworkTopology.InvalidTopologyException("Failed to find datanode (scope=\""
                                                                   + scope.ToString() + "\" excludedScope=\"" + excludedScope.ToString() + "\").");
            }
            int leaveIndex = r.Next(numOfDatanodes);

            return(innerNode.GetLeaf(leaveIndex, node));
        }
示例#3
0
        /// <summary>return leaves in <i>scope</i></summary>
        /// <param name="scope">a path string</param>
        /// <returns>leaves nodes under specific scope</returns>
        public virtual IList <Node> GetLeaves(string scope)
        {
            Node         node      = GetNode(scope);
            IList <Node> leafNodes = new AList <Node>();

            if (!(node is NetworkTopology.InnerNode))
            {
                leafNodes.AddItem(node);
            }
            else
            {
                NetworkTopology.InnerNode innerNode = (NetworkTopology.InnerNode)node;
                for (int i = 0; i < innerNode.GetNumOfLeaves(); i++)
                {
                    leafNodes.AddItem(innerNode.GetLeaf(i, null));
                }
            }
            return(leafNodes);
        }
示例#4
0
            /// <summary>
            /// get <i>leafIndex</i> leaf of this subtree
            /// if it is not in the <i>excludedNode</i>
            /// </summary>
            /// <param name="leafIndex">an indexed leaf of the node</param>
            /// <param name="excludedNode">an excluded node (can be null)</param>
            /// <returns/>
            internal virtual Node GetLeaf(int leafIndex, Node excludedNode)
            {
                int count = 0;
                // check if the excluded node a leaf
                bool isLeaf = excludedNode == null || !(excludedNode is NetworkTopology.InnerNode
                                                        );
                // calculate the total number of excluded leaf nodes
                int numOfExcludedLeaves = isLeaf ? 1 : ((NetworkTopology.InnerNode)excludedNode).
                                          GetNumOfLeaves();

                if (IsLeafParent())
                {
                    // children are leaves
                    if (isLeaf)
                    {
                        // excluded node is a leaf node
                        int excludedIndex = children.IndexOf(excludedNode);
                        if (excludedIndex != -1 && leafIndex >= 0)
                        {
                            // excluded node is one of the children so adjust the leaf index
                            leafIndex = leafIndex >= excludedIndex ? leafIndex + 1 : leafIndex;
                        }
                    }
                    // range check
                    if (leafIndex < 0 || leafIndex >= this.GetNumOfChildren())
                    {
                        return(null);
                    }
                    return(children[leafIndex]);
                }
                else
                {
                    for (int i = 0; i < children.Count; i++)
                    {
                        NetworkTopology.InnerNode child = (NetworkTopology.InnerNode)children[i];
                        if (excludedNode == null || excludedNode != child)
                        {
                            // not the excludedNode
                            int numOfLeaves = child.GetNumOfLeaves();
                            if (excludedNode != null && child.IsAncestor(excludedNode))
                            {
                                numOfLeaves -= numOfExcludedLeaves;
                            }
                            if (count + numOfLeaves > leafIndex)
                            {
                                // the leaf is in the child subtree
                                return(child.GetLeaf(leafIndex - count, excludedNode));
                            }
                            else
                            {
                                // go to the next child
                                count = count + numOfLeaves;
                            }
                        }
                        else
                        {
                            // it is the excluededNode
                            // skip it and set the excludedNode to be null
                            excludedNode = null;
                        }
                    }
                    return(null);
                }
            }