class Host2NodesMap {

  boolean remove(DatanodeDescriptor node) {
    if (node == null) {
      return false;
    }

    String ipAddr = node.getIpAddr();
    String hostname = node.getHostName();
    hostmapLock.writeLock().lock();
    try {

      DatanodeDescriptor[] nodes = map.get(ipAddr);
      if (nodes == null) {
        return false;
      }
      if (nodes.length == 1) {
        if (nodes[0] == node) {
          map.remove(ipAddr);
          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.arraycopy(nodes, 0, newNodes, 0, i);
        System.arraycopy(nodes, i + 1, newNodes, i, nodes.length - i - 1);
        map.put(ipAddr, newNodes);
        return true;
      }
    } finally {
      hostmapLock.writeLock().unlock();
    }
  }

}