@Override public ConfigKeyPath apply(String input) { if (input == null) { return null; } ConfigKeyPath configKey = SingleLinkedListConfigKeyPath.ROOT; for (String file : Splitter.on(SingleLinkedListConfigKeyPath.PATH_DELIMETER).omitEmptyStrings().split(input)) { configKey = configKey.createChild(file); } return configKey; } }
private Config getResolvedConfigRecursive(ConfigKeyPath configKey, Set<String> alreadyLoadedPaths, Optional<Config> runtimeConfig) { if (this.cs instanceof ConfigStoreWithResolution) { return ((ConfigStoreWithResolution) this.cs).getResolvedConfig(configKey, this.version); } if (!alreadyLoadedPaths.add(configKey.getAbsolutePathString())) { return ConfigFactory.empty(); } Config initialConfig = this.getOwnConfig(configKey); if (configKey.isRootPath()) { return initialConfig; } List<ConfigKeyPath> ownImports = this.topology.getOwnImports(configKey, runtimeConfig); // merge with other configs from imports if (ownImports != null) { for (ConfigKeyPath p : ownImports) { initialConfig = initialConfig.withFallback(this.getResolvedConfigRecursive(p, alreadyLoadedPaths, runtimeConfig)); } } // merge with configs from parent for Non root initialConfig = initialConfig .withFallback(this.getResolvedConfigRecursive(configKey.getParent(), alreadyLoadedPaths, runtimeConfig)); return initialConfig; }
private void mockupConfigValueForKey(ConfigKeyPath configKey){ final String generalKey = "generalKey"; Map<String, String> valueMap = new HashMap<>(); // key in all the nodes valueMap.put(generalKey, "valueOf_" +generalKey +"_"+configKey.getOwnPathName() ); // key in self node valueMap.put("keyOf_" + configKey.getOwnPathName(), "valueOf_" + configKey.getOwnPathName()); when(mockConfigStore.getOwnConfig(configKey, version)).thenReturn(ConfigFactory.parseMap(valueMap)); }
@Override public String getAbsolutePathString() { if (this.isRootPath()) { return this.getOwnPathName() + PATH_DELIMETER; } // first level children do not need to add "/" if (this.parent.isRootPath()) return this.parent.getAbsolutePathString() + this.ownName; return this.parent.getAbsolutePathString() + PATH_DELIMETER + this.ownName; }
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); }
@Test public void testNonRoot() { ConfigKeyPath data = SingleLinkedListConfigKeyPath.ROOT.createChild("data"); Assert.assertEquals(data.getAbsolutePathString(), "/data"); ConfigKeyPath profile = data.createChild("databases").createChild("identity").createChild("profile"); Assert.assertEquals(profile.toString(), "/data/databases/identity/profile"); }
/** * Get path object using zipped file system and relative path */ private Path getDatasetDirForKey(ConfigKeyPath configKey) throws VersionDoesNotExistException { return this.fs.getPath(this.storePrefix, configKey.getAbsolutePathString()); } }
@Test public void testResolveConfigOverridingInChild() { ConfigStore mockConfigStore = mock(ConfigStore.class, Mockito.RETURNS_SMART_NULLS); when(mockConfigStore.getCurrentVersion()).thenReturn(version); ConfigStoreTopologyInspector mockTopology = mock(ConfigStoreTopologyInspector.class, Mockito.RETURNS_SMART_NULLS); ConfigStoreBackedValueInspector valueInspector = new ConfigStoreBackedValueInspector(mockConfigStore, version, mockTopology); ConfigKeyPath keyPathA = SingleLinkedListConfigKeyPath.ROOT.createChild("a"); ConfigKeyPath keyPathA_Slash_B = keyPathA.createChild("b"); when(mockConfigStore.getOwnConfig(keyPathA.getParent(), version)).thenReturn(ConfigFactory.empty()); when(mockConfigStore.getOwnConfig(keyPathA, version)).thenReturn( ConfigFactory.parseString("key1 = value1InA \n key2 = ${key1}")); when(mockConfigStore.getOwnConfig(keyPathA_Slash_B, version)).thenReturn( ConfigFactory.parseString("key1 = value1InB")); Assert.assertEquals(valueInspector.getResolvedConfig(keyPathA_Slash_B).getString("key2"), "value1InB"); } }
@Test public void testNoCircular() { ConfigKeyPath tag = SingleLinkedListConfigKeyPath.ROOT.createChild("tag"); ConfigKeyPath highPriorityTag = tag.createChild("highPriorityTag"); ConfigKeyPath nertzHighPriorityTag = highPriorityTag.createChild("nertzHighPriorityTag"); Assert.assertEquals(it.next(), tag2); Assert.assertEquals(it.next(), tag); Assert.assertTrue(it.next().isRootPath()); Assert.assertEquals(it.next(), highPriorityTag);
@Test public void testSystemPropertyResolution() { ConfigStore mockConfigStore = mock(ConfigStore.class, Mockito.RETURNS_SMART_NULLS); when(mockConfigStore.getCurrentVersion()).thenReturn(version); ConfigStoreTopologyInspector mockTopology = mock(ConfigStoreTopologyInspector.class, Mockito.RETURNS_SMART_NULLS); ConfigStoreBackedValueInspector valueInspector = new ConfigStoreBackedValueInspector(mockConfigStore, version, mockTopology); ConfigKeyPath testConfigKeyPath = SingleLinkedListConfigKeyPath.ROOT.createChild("a"); when(mockConfigStore.getOwnConfig(testConfigKeyPath.getParent(), version)).thenReturn(ConfigFactory.empty()); when(mockConfigStore.getOwnConfig(testConfigKeyPath, version)).thenReturn( ConfigFactory.parseString("configProp = ${?" + VALUE_INSPECTOR_SYS_PROP_KEY + "}")); Assert.assertEquals(valueInspector.getResolvedConfig(testConfigKeyPath).getString("configProp"), VALUE_INSPECTOR_SYS_PROP_VALUE); }
public List<ConfigKeyPath> getImportsRecursively(ConfigKeyPath configKey, Optional<Config> runtimeConfig) { return new ImportTraverser<>(key -> { if (key.isRootPath()) { return new LinkedList<>(); } List<ConfigKeyPath> imports = Lists.newArrayList(); imports.addAll(Lists.reverse(getOwnImports(key, runtimeConfig))); imports.add(key.getParent()); return imports; }, this.recursiveImportMap).traverseGraphRecursively(configKey); }
return new URI(cs.getStoreURI().getScheme(), null, configKeyPath.getAbsolutePathString(), null, null); if (configKeyPath.isRootPath()) { return storeRoot; Path absPath = new Path(storeRoot.getPath(), configKeyPath.getAbsolutePathString().substring(1)); // remote the first "/"; return new URI(storeRoot.getScheme(), storeRoot.getAuthority(), absPath.toString(), null, null); } catch (URISyntaxException e) {
/** * Retrieves the name of a dataset from a given {@link ConfigKeyPath}, relative to the store root. */ private static String getDatasetFromConfigKey(ConfigKeyPath configKey) { return StringUtils.removeStart(configKey.getAbsolutePathString(), SingleLinkedListConfigKeyPath.PATH_DELIMETER); }
/** * Build the {@link ConfigKeyPath} based on the absolute/relative path * @param input - absolute/relative file path * @return - {@link ConfigKeyPath} corresponding to the input */ public static ConfigKeyPath getConfigKeyPath(String input) { ConfigKeyPath result = SingleLinkedListConfigKeyPath.ROOT; String[] paths = input.split("/"); for (String p : paths) { // in case input start with "/", some elements could be "", which should be skip if (p.equals("")) { continue; } result = result.createChild(p); } return result; }
private Config getResolvedConfigRecursive(ConfigKeyPath configKey, Set<String> alreadyLoadedPaths, Optional<Config> runtimeConfig) { if (this.cs instanceof ConfigStoreWithResolution) { return ((ConfigStoreWithResolution) this.cs).getResolvedConfig(configKey, this.version); } if (!alreadyLoadedPaths.add(configKey.getAbsolutePathString())) { return ConfigFactory.empty(); } Config initialConfig = this.getOwnConfig(configKey); if (configKey.isRootPath()) { return initialConfig; } List<ConfigKeyPath> ownImports = this.topology.getOwnImports(configKey, runtimeConfig); // merge with other configs from imports if (ownImports != null) { for (ConfigKeyPath p : ownImports) { initialConfig = initialConfig.withFallback(this.getResolvedConfigRecursive(p, alreadyLoadedPaths, runtimeConfig)); } } // merge with configs from parent for Non root initialConfig = initialConfig .withFallback(this.getResolvedConfigRecursive(configKey.getParent(), alreadyLoadedPaths, runtimeConfig)); return initialConfig; }
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); }
@Override public String getAbsolutePathString() { if (this.isRootPath()) { return this.getOwnPathName() + PATH_DELIMETER; } // first level children do not need to add "/" if (this.parent.isRootPath()) return this.parent.getAbsolutePathString() + this.ownName; return this.parent.getAbsolutePathString() + PATH_DELIMETER + this.ownName; }
@Test public void testGetOwnImportsWithResolution() throws IOException, URISyntaxException, ConfigStoreCreationException { String datasetName = "dataset-test-get-own-imports-resolution"; Path datasetPath = new Path(CONFIG_DIR_PATH, datasetName); try { this.fs.mkdirs(datasetPath); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(this.fs.create(new Path(datasetPath, "includes.conf")), Charsets.UTF_8)); writer.write("/path/to/${?" + TAG_NAME_SYS_PROP_KEY + "}"); writer.close(); ConfigKeyPath datasetConfigKey = SingleLinkedListConfigKeyPath.ROOT.createChild(datasetName); List<ConfigKeyPath> imports = this._simpleHadoopFilesystemConfigStore.getOwnImports(datasetConfigKey, VERSION); Assert.assertEquals(imports.size(), 1); Assert.assertEquals(imports.get(0).getAbsolutePathString(), "/path/to/" + TAG_NAME_SYS_PROP_VALUE); } finally { if (this.fs.exists(datasetPath)) { this.fs.delete(datasetPath, true); } } }
@Test public void testGetChildren() throws IOException, URISyntaxException, ConfigStoreCreationException { String datasetName = "dataset-test-get-children"; String childDatasetName = "childDataset"; Path datasetPath = new Path(CONFIG_DIR_PATH, datasetName); try { this.fs.mkdirs(new Path(datasetPath, childDatasetName)); ConfigKeyPath datasetConfigKey = SingleLinkedListConfigKeyPath.ROOT.createChild(datasetName); Collection<ConfigKeyPath> children = this._simpleHadoopFilesystemConfigStore.getChildren(datasetConfigKey, VERSION); Assert.assertEquals(children.size(), 1); Assert.assertEquals(children.iterator().next().getOwnPathName(), childDatasetName); } finally { if (this.fs.exists(datasetPath)) { this.fs.delete(datasetPath, true); } } }
children.add(configKey.createChild(fileStatus.getPath().getName()));