/// <summary> /// add node to the map /// return true if the node is added; false otherwise. /// </summary> internal virtual bool Add(DatanodeDescriptor node) { hostmapLock.WriteLock().Lock(); try { if (node == null || Contains(node)) { return(false); } string ipAddr = node.GetIpAddr(); string hostname = node.GetHostName(); mapHost[hostname] = ipAddr; DatanodeDescriptor[] nodes = map[ipAddr]; DatanodeDescriptor[] newNodes; if (nodes == null) { newNodes = new DatanodeDescriptor[1]; newNodes[0] = node; } else { // rare case: more than one datanode on the host newNodes = new DatanodeDescriptor[nodes.Length + 1]; System.Array.Copy(nodes, 0, newNodes, 0, nodes.Length); newNodes[nodes.Length] = node; } map[ipAddr] = newNodes; return(true); } finally { hostmapLock.WriteLock().Unlock(); } }
/// <summary>Check if node is already in the map.</summary> internal virtual bool Contains(DatanodeDescriptor node) { if (node == null) { return(false); } string ipAddr = node.GetIpAddr(); hostmapLock.ReadLock().Lock(); try { DatanodeDescriptor[] nodes = map[ipAddr]; if (nodes != null) { foreach (DatanodeDescriptor containedNode in nodes) { if (node == containedNode) { return(true); } } } } finally { hostmapLock.ReadLock().Unlock(); } return(false); }
/// <summary> /// remove node from the map /// return true if the node is removed; false otherwise. /// </summary> internal virtual bool Remove(DatanodeDescriptor node) { if (node == null) { return(false); } string ipAddr = node.GetIpAddr(); string hostname = node.GetHostName(); hostmapLock.WriteLock().Lock(); try { DatanodeDescriptor[] nodes = map[ipAddr]; if (nodes == null) { return(false); } if (nodes.Length == 1) { if (nodes[0] == node) { Sharpen.Collections.Remove(map, ipAddr); //remove hostname key since last datanode is removed Sharpen.Collections.Remove(mapHost, hostname); return(true); } else { return(false); } } //rare case int i = 0; for (; i < nodes.Length; i++) { if (nodes[i] == node) { break; } } if (i == nodes.Length) { return(false); } else { DatanodeDescriptor[] newNodes; newNodes = new DatanodeDescriptor[nodes.Length - 1]; System.Array.Copy(nodes, 0, newNodes, 0, i); System.Array.Copy(nodes, i + 1, newNodes, i, nodes.Length - i - 1); map[ipAddr] = newNodes; return(true); } } finally { hostmapLock.WriteLock().Unlock(); } }