/** * Creates a {@link Location} instance which represents the parent of the given location. * Note: CDAP-13765 this method can return an invalid location if the parent is the root directory depending * on the implementation of the input Location. Consider calling {@link #isRoot(Location)} before * calling this. * * @param location location to extra parent from. * @return an instance representing the parent location or {@code null} if there is no parent. */ @Nullable public static Location getParent(Location location) { // If it is root, return null if (isRoot(location)) { return null; } URI source = location.toURI(); URI resolvedParent = URI.create(source.toString() + "/..").normalize(); // TODO: (CDAP-13765) move this logic to a better place // NOTE: if there is a trailing slash at the end, rename(), getName() and other operations on file // does not work in MapR. so we remove the trailing slash (if any) at the end. // However, don't remove the trailing slash if this is a root directory, as a URI without a path is an invalid // location if (resolvedParent.toString().endsWith("/") && !"/".equals(resolvedParent.getPath())) { String parent = resolvedParent.toString(); resolvedParent = URI.create(parent.substring(0, parent.length() - 1)); } return location.getLocationFactory().create(resolvedParent); }
/** * Creates a {@link Location} instance which represents the parent of the given location. * Note: CDAP-13765 this method can return an invalid location if the parent is the root directory depending * on the implementation of the input Location. Consider calling {@link #isRoot(Location)} before * calling this. * * @param location location to extra parent from. * @return an instance representing the parent location or {@code null} if there is no parent. */ @Nullable public static Location getParent(Location location) { // If it is root, return null if (isRoot(location)) { return null; } URI source = location.toURI(); URI resolvedParent = URI.create(source.toString() + "/..").normalize(); // TODO: (CDAP-13765) move this logic to a better place // NOTE: if there is a trailing slash at the end, rename(), getName() and other operations on file // does not work in MapR. so we remove the trailing slash (if any) at the end. // However, don't remove the trailing slash if this is a root directory, as a URI without a path is an invalid // location if (resolvedParent.toString().endsWith("/") && !"/".equals(resolvedParent.getPath())) { String parent = resolvedParent.toString(); resolvedParent = URI.create(parent.substring(0, parent.length() - 1)); } return location.getLocationFactory().create(resolvedParent); }
while (ancestor != null && (Locations.isRoot(ancestor) || !ancestor.exists())) { firstDirToCreate = ancestor; ancestor = Locations.getParent(ancestor);
while (ancestor != null && (Locations.isRoot(ancestor) || !ancestor.exists())) { firstDirToCreate = ancestor; ancestor = Locations.getParent(ancestor);
@Test public void testParentHDFS() { Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://1.2.3.4:8020"); LocationFactory locationFactory = new FileContextLocationFactory(conf, "abc"); Location location = locationFactory.create("def"); Assert.assertEquals("def", location.getName()); location = Locations.getParent(location); Assert.assertNotNull(location); Assert.assertEquals("abc", location.getName()); location = Locations.getParent(location); Assert.assertNotNull(location); Assert.assertTrue(Locations.isRoot(location)); Assert.assertTrue(location.getName().isEmpty()); Assert.assertNull(Locations.getParent(location)); }
@Test public void testRootParentFile() { Configuration conf = new Configuration(); conf.set("fs.defaultFS", "file:///"); LocationFactory locationFactory = new FileContextLocationFactory(conf, "abc"); Location location = locationFactory.create("def"); Assert.assertEquals("def", location.getName()); location = Locations.getParent(location); Assert.assertNotNull(location); Assert.assertEquals("abc", location.getName()); location = Locations.getParent(location); Assert.assertNotNull(location); Assert.assertTrue(Locations.isRoot(location)); Assert.assertTrue(location.getName().isEmpty()); Assert.assertNull(Locations.getParent(location)); }
@Test public void testRootParentLocal() throws IOException { LocationFactory locationFactory = new LocalLocationFactory(new File(File.separator)); Location location = locationFactory.create("abc"); location = location.append("def"); Assert.assertEquals("def", location.getName()); location = Locations.getParent(location); Assert.assertNotNull(location); Assert.assertEquals("abc", location.getName()); location = Locations.getParent(location); Assert.assertNotNull(location); Assert.assertTrue(Locations.isRoot(location)); Assert.assertTrue(location.getName().isEmpty()); Assert.assertNull(Locations.getParent(location)); }