/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.underfs.hdfs;

import alluxio.AlluxioURI;
import alluxio.Configuration;
import alluxio.underfs.UnderFileSystem;
import alluxio.underfs.UnderFileSystemFactory;

import com.google.common.base.Preconditions;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.concurrent.ThreadSafe;

/**
 * Factory for creating {@link HdfsUnderFileSystem}.
 *
 * It caches created {@link HdfsUnderFileSystem}s, using the scheme and authority pair as the key.
 */
@ThreadSafe
public final class HdfsUnderFileSystemFactory implements UnderFileSystemFactory {
  /**
   * Cache mapping {@code Path}s to existing {@link UnderFileSystem} instances. The paths should be
   * normalized to root paths because only their schemes and authorities are needed to identify
   * which {@link FileSystem} they belong to.
   */
  private Map<Path, HdfsUnderFileSystem> mHdfsUfsCache = new HashMap<>();

  /**
   * Constructs a new {@link HdfsUnderFileSystemFactory}.
   */
  public HdfsUnderFileSystemFactory() {}

  @Override
  public UnderFileSystem create(String path, Configuration configuration, Object conf) {
    Preconditions.checkArgument(path != null, "path may not be null");

    // Normalize the path to just its root. This is all that's needed to identify which FileSystem
    // the Path belongs to.
    Path rootPath = getRoot(new Path(path));
    synchronized (mHdfsUfsCache) {
      if (!mHdfsUfsCache.containsKey(rootPath)) {
        mHdfsUfsCache
            .put(rootPath, new HdfsUnderFileSystem(new AlluxioURI(path), configuration, conf));
      }
      return mHdfsUfsCache.get(rootPath);
    }
  }

  private static Path getRoot(Path path) {
    Path currPath = path;
    while (currPath.getParent() != null) {
      currPath = currPath.getParent();
    }
    return currPath;
  }

  @Override
  public boolean supportsPath(String path, Configuration conf) {
    if (path == null) {
      return false;
    }

    return UnderFileSystem.isHadoopUnderFS(path, conf);
  }
}

@@ -731,131 +731,8 @@
 ons;
-%0Aimport org.apache.hadoop.fs.FileSystem;%0Aimport org.apache.hadoop.fs.Path;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;
 %0A%0Aim
@@ -1038,337 +1038,8 @@
 y %7B%0A
-  /**%0A   * Cache mapping %7B@code Path%7Ds to existing %7B@link UnderFileSystem%7D instances. The paths should be%0A   * normalized to root paths because only their schemes and authorities are needed to identify%0A   * which %7B@link FileSystem%7D they belong to.%0A   */%0A  private Map%3CPath, HdfsUnderFileSystem%3E mHdfsUfsCache = new HashMap%3C%3E();%0A%0A
   /*
@@ -1274,669 +1274,100 @@
 heck
-Argument(path != null, %22path may not be null%22);%0A%0A    // Normalize the path to just its root. This is all that's needed to identify which FileSystem%0A    // the Path belongs to.%0A    Path rootPath = getRoot(new Path
+NotNull
 (path)
-)
 ;%0A    
-synchronized (mHdfsUfsCache) %7B%0A      if (!mHdfsUfsCache.containsKey(rootPath)) %7B%0A        mHdfsUfsCache%0A            .put(rootPath, new HdfsUnderFileSystem(new AlluxioURI(path), configuration, conf));%0A      %7D%0A      return mHdfsUfsCache.get(rootPath);%0A    %7D%0A  %7D%0A%0A  private static Path getRoot(Path path) %7B%0A    Path currPath = path;%0A    while (currPath.getParent() != null) %7B%0A      currPath = currPath.getParent();%0A    %7D%0A    return currPath
+return new HdfsUnderFileSystem(new AlluxioURI(path), configuration, conf)
 ;%0A  

remove cache in HdfsUnderFSFactory