/// <summary> /// Add a leaf node /// Update node counter & rack counter if necessary /// </summary> /// <param name="node">node to be added; can be null</param> /// <exception> /// IllegalArgumentException /// if add a node to a leave /// or node to be added is not a leaf /// </exception> public virtual void Add(Node node) { if (node == null) { return; } int newDepth = NodeBase.LocationToDepth(node.GetNetworkLocation()) + 1; netlock.WriteLock().Lock(); try { string oldTopoStr = this.ToString(); if (node is NetworkTopology.InnerNode) { throw new ArgumentException("Not allow to add an inner node: " + NodeBase.GetPath (node)); } if ((depthOfAllLeaves != -1) && (depthOfAllLeaves != newDepth)) { Log.Error("Error: can't add leaf node " + NodeBase.GetPath(node) + " at depth " + newDepth + " to topology:\n" + oldTopoStr); throw new NetworkTopology.InvalidTopologyException("Failed to add " + NodeBase.GetPath (node) + ": You cannot have a rack and a non-rack node at the same " + "level of the network topology." ); } Node rack = GetNodeForNetworkLocation(node); if (rack != null && !(rack is NetworkTopology.InnerNode)) { throw new ArgumentException("Unexpected data node " + node.ToString() + " at an illegal network location" ); } if (clusterMap.Add(node)) { Log.Info("Adding a new node: " + NodeBase.GetPath(node)); if (rack == null) { numOfRacks++; } if (!(node is NetworkTopology.InnerNode)) { if (depthOfAllLeaves == -1) { depthOfAllLeaves = node.GetLevel(); } } } if (Log.IsDebugEnabled()) { Log.Debug("NetworkTopology became:\n" + this.ToString()); } } finally { netlock.WriteLock().Unlock(); } }
/// <summary>Add node <i>n</i> to the subtree of this node</summary> /// <param name="n">node to be added</param> /// <returns>true if the node is added; false otherwise</returns> internal virtual bool Add(Node n) { if (!IsAncestor(n)) { throw new ArgumentException(n.GetName() + ", which is located at " + n.GetNetworkLocation () + ", is not a decendent of " + GetPath(this)); } if (IsParent(n)) { // this node is the parent of n; add n directly n.SetParent(this); n.SetLevel(this.level + 1); for (int i = 0; i < children.Count; i++) { if (children[i].GetName().Equals(n.GetName())) { children.Set(i, n); return(false); } } children.AddItem(n); numOfLeaves++; return(true); } else { // find the next ancestor node string parentName = GetNextAncestorName(n); NetworkTopology.InnerNode parentNode = null; for (int i = 0; i < children.Count; i++) { if (children[i].GetName().Equals(parentName)) { parentNode = (NetworkTopology.InnerNode)children[i]; break; } } if (parentNode == null) { // create a new InnerNode parentNode = CreateParentNode(parentName); children.AddItem(parentNode); } // add n to the subtree of the next ancestor node if (parentNode.Add(n)) { numOfLeaves++; return(true); } else { return(false); } } }