private URI getMatchedFloorKeyFromCache(URI configKeyURI) { URI floorKey = this.configStoreAccessorMap.floorKey(configKeyURI); if (floorKey == null) { return null; } // both scheme name and authority name, if present, should match // or both authority should be null if (ConfigClientUtils.isAncestorOrSame(configKeyURI, floorKey)) { return floorKey; } return null; }
public static boolean isAncestorOrSame(ConfigKeyPath descendant, ConfigKeyPath ancestor) { Preconditions.checkNotNull(descendant, "input can not be null"); Preconditions.checkNotNull(ancestor, "input can not be null"); if (descendant.equals(ancestor)) return true; if (descendant.isRootPath()) return false; return isAncestorOrSame(descendant.getParent(), ancestor); }
/** * Utility method to check whether one URI is the ancestor of the other * * return true iff both URI's scheme/authority name match and ancestor's path is the prefix of the descendant's path * @param descendant: the descendant URI to check * @param ancestor : the ancestor URI to check * @return */ public static boolean isAncestorOrSame(URI descendant, URI ancestor) { Preconditions.checkNotNull(descendant, "input can not be null"); Preconditions.checkNotNull(ancestor, "input can not be null"); if (!stringSame(descendant.getScheme(), ancestor.getScheme())) { return false; } if (!stringSame(descendant.getAuthority(), ancestor.getAuthority())) { return false; } return isAncestorOrSame(getConfigKeyPath(descendant.getPath()), getConfigKeyPath(ancestor.getPath())); }
private ConfigStoreAccessor getConfigStoreAccessor(URI configKeyURI) throws ConfigStoreFactoryDoesNotExistsException, ConfigStoreCreationException, VersionDoesNotExistException { URI matchedFloorKey = getMatchedFloorKeyFromCache(configKeyURI); ConfigStoreAccessor result; if (matchedFloorKey != null) { result = this.configStoreAccessorMap.get(matchedFloorKey); return result; } result = createNewConfigStoreAccessor(configKeyURI); ConfigStore cs = result.configStore; // put default root URI in cache as well for the URI which missing authority if (configKeyURI.getAuthority() == null) { // configKeyURI is missing authority/configstore root "etl-hdfs:///datasets/a1/a2" try { this.configStoreAccessorMap.put(new URI(configKeyURI.getScheme(), null, "/", null, null), result); } catch (URISyntaxException e) { // should not come here throw new RuntimeException("Can not build URI based on " + configKeyURI); } } else { // need to check Config Store's root is the prefix of input configKeyURI if (!ConfigClientUtils.isAncestorOrSame(configKeyURI, cs.getStoreURI())) { throw new RuntimeException( String.format("Config Store root URI %s is not the prefix of input %s", cs.getStoreURI(), configKeyURI)); } } // put to cache this.configStoreAccessorMap.put(cs.getStoreURI(), result); return result; }
@Test public void testIsAncestorOrSame() throws Exception{ //Path ancestor = new Path("/"); //Path descendant = new Path("/"); URI ancestor = new URI("etl-hdfs://eat1-nertznn01.grid.linkedin.com:9000/user/mitu/HdfsBasedConfigTest/"); URI descendant = new URI("etl-hdfs://eat1-nertznn01.grid.linkedin.com:9000/user/mitu/HdfsBasedConfigTest/datasets/a1/a2"); Assert.assertTrue(ConfigClientUtils.isAncestorOrSame(descendant, ancestor)); // ends with "/" descendant = new URI("etl-hdfs://eat1-nertznn01.grid.linkedin.com:9000/user/mitu/HdfsBasedConfigTest/datasets/a1/a2/"); Assert.assertTrue(ConfigClientUtils.isAncestorOrSame(descendant, ancestor)); // wrong authority descendant = new URI("etl-hdfs://ltx1-nertznn01.grid.linkedin.com:9000/user/mitu/HdfsBasedConfigTest/datasets/a1/a2"); Assert.assertTrue(!ConfigClientUtils.isAncestorOrSame(descendant, ancestor)); // wrong path descendant = new URI("etl-hdfs://eat1-nertznn01.grid.linkedin.com:9000/user/sahil/HdfsBasedConfigTest/datasets/a1/a2"); Assert.assertTrue(!ConfigClientUtils.isAncestorOrSame(descendant, ancestor)); ConfigKeyPath data = SingleLinkedListConfigKeyPath.ROOT.createChild("data"); ConfigKeyPath data2 = SingleLinkedListConfigKeyPath.ROOT.createChild("data2"); ConfigKeyPath identity = SingleLinkedListConfigKeyPath.ROOT.createChild("data").createChild("databases").createChild("Identity"); Assert.assertTrue(ConfigClientUtils.isAncestorOrSame(identity, data)); Assert.assertTrue(ConfigClientUtils.isAncestorOrSame(identity, SingleLinkedListConfigKeyPath.ROOT)); Assert.assertTrue(!ConfigClientUtils.isAncestorOrSame(identity, data2)); }
private URI getMatchedFloorKeyFromCache(URI configKeyURI) { URI floorKey = this.configStoreAccessorMap.floorKey(configKeyURI); if (floorKey == null) { return null; } // both scheme name and authority name, if present, should match // or both authority should be null if (ConfigClientUtils.isAncestorOrSame(configKeyURI, floorKey)) { return floorKey; } return null; }
public static boolean isAncestorOrSame(ConfigKeyPath descendant, ConfigKeyPath ancestor) { Preconditions.checkNotNull(descendant, "input can not be null"); Preconditions.checkNotNull(ancestor, "input can not be null"); if (descendant.equals(ancestor)) return true; if (descendant.isRootPath()) return false; return isAncestorOrSame(descendant.getParent(), ancestor); }
/** * Utility method to check whether one URI is the ancestor of the other * * return true iff both URI's scheme/authority name match and ancestor's path is the prefix of the descendant's path * @param descendant: the descendant URI to check * @param ancestor : the ancestor URI to check * @return */ public static boolean isAncestorOrSame(URI descendant, URI ancestor) { Preconditions.checkNotNull(descendant, "input can not be null"); Preconditions.checkNotNull(ancestor, "input can not be null"); if (!stringSame(descendant.getScheme(), ancestor.getScheme())) { return false; } if (!stringSame(descendant.getAuthority(), ancestor.getAuthority())) { return false; } return isAncestorOrSame(getConfigKeyPath(descendant.getPath()), getConfigKeyPath(ancestor.getPath())); }
private ConfigStoreAccessor getConfigStoreAccessor(URI configKeyURI) throws ConfigStoreFactoryDoesNotExistsException, ConfigStoreCreationException, VersionDoesNotExistException { URI matchedFloorKey = getMatchedFloorKeyFromCache(configKeyURI); ConfigStoreAccessor result; if (matchedFloorKey != null) { result = this.configStoreAccessorMap.get(matchedFloorKey); return result; } result = createNewConfigStoreAccessor(configKeyURI); ConfigStore cs = result.configStore; // put default root URI in cache as well for the URI which missing authority if (configKeyURI.getAuthority() == null) { // configKeyURI is missing authority/configstore root "etl-hdfs:///datasets/a1/a2" try { this.configStoreAccessorMap.put(new URI(configKeyURI.getScheme(), null, "/", null, null), result); } catch (URISyntaxException e) { // should not come here throw new RuntimeException("Can not build URI based on " + configKeyURI); } } else { // need to check Config Store's root is the prefix of input configKeyURI if (!ConfigClientUtils.isAncestorOrSame(configKeyURI, cs.getStoreURI())) { throw new RuntimeException( String.format("Config Store root URI %s is not the prefix of input %s", cs.getStoreURI(), configKeyURI)); } } // put to cache this.configStoreAccessorMap.put(cs.getStoreURI(), result); return result; }