use of org.apache.ignite.internal.configuration.tree.InnerNode in project ignite-3 by apache.
the class ConfigurationUtil method findEx.
/**
* Finds a node or a leaf by the given path.
*
* @param path Path.
* @param rootNode Root node.
* @param <T> Arbitrary result type.
* @return Node or leaf.
*/
@Nullable
public static <T> T findEx(List<KeyPathNode> path, InnerNode rootNode) {
try {
var visitor = new ConfigurationVisitor<T>() {
private final int pathSize = path.size();
/**
* Current index of the key in the {@code path}.
*/
private int idx;
/**
* {@inheritDoc}
*/
@Override
public T visitLeafNode(String key, Serializable val) {
if (idx != pathSize) {
throw new KeyNotFoundException("Configuration value '" + joinPath(path.subList(0, idx)) + "' is a leaf");
} else {
return (T) val;
}
}
/**
* {@inheritDoc}
*/
@Override
public T visitInnerNode(String key, InnerNode node) {
if (node == null) {
throw new KeyNotFoundException("Configuration node '" + joinPath(path.subList(0, idx)) + "' is null");
} else if (idx == pathSize) {
return (T) node;
} else {
try {
KeyPathNode pathNode = path.get(idx++);
assert !pathNode.unresolvedName;
if (INTERNAL_ID.equals(pathNode.key)) {
// It's impossible to get this value with a regular traversal. Just call a method.
return (T) node.internalId();
} else if (INJECTED_NAME.equals(pathNode.key)) {
// It's impossible to get this value with a regular traversal. Just call a method.
return (T) node.getInjectedNameFieldValue();
}
return node.traverseChild(pathNode.key, this, true);
} catch (NoSuchElementException e) {
throw new KeyNotFoundException("Configuration value '" + joinPath(path.subList(0, idx)) + "' has not been found");
} catch (ConfigurationWrongPolymorphicTypeIdException e) {
assert false : e;
return null;
}
}
}
/**
* {@inheritDoc}
*/
@Override
public T visitNamedListNode(String key, NamedListNode<?> node) {
if (idx == pathSize) {
return (T) node;
} else {
KeyPathNode pathNode = path.get(idx++);
assert pathNode.namedListEntry;
String name = pathNode.unresolvedName ? pathNode.key : node.keyByInternalId(UUID.fromString(pathNode.key));
return visitInnerNode(name, node.getInnerNode(name));
}
}
};
return rootNode.accept(null, visitor);
} catch (KeyNotFoundException e) {
throw new NoSuchElementException(joinPath(path));
}
}
use of org.apache.ignite.internal.configuration.tree.InnerNode in project ignite-3 by apache.
the class ConfigurationUtilTest method testFlattenedMapPolymorphicConfig.
@Test
void testFlattenedMapPolymorphicConfig() {
InnerNode polymorphicRootInnerNode = newNodeInstance(PolymorphicRootConfigurationSchema.class);
addDefaults(polymorphicRootInnerNode);
RootKey<?, ?> rootKey = PolymorphicRootConfiguration.KEY;
SuperRoot superRoot = new SuperRoot(key -> null, Map.of(rootKey, polymorphicRootInnerNode));
final Map<String, Serializable> act = flattenedMap(superRoot, rootKey, node -> ((PolymorphicRootChange) node).changePolymorphicSubCfg(c -> c.convert(SecondPolymorphicInstanceChange.class)));
Map<String, Serializable> exp = new HashMap<>();
exp.put("rootPolymorphic.polymorphicSubCfg.typeId", "second");
exp.put("rootPolymorphic.polymorphicSubCfg.longVal", 0L);
exp.put("rootPolymorphic.polymorphicSubCfg.strVal", null);
exp.put("rootPolymorphic.polymorphicSubCfg.intVal", 0);
assertEquals(exp, act);
}
use of org.apache.ignite.internal.configuration.tree.InnerNode in project ignite-3 by apache.
the class ConfigurationUtilTest method findUnsuccessfully.
/**
* Tests that {@link ConfigurationUtil#find(List, TraversableTreeNode, boolean)} throws {@link KeyNotFoundException} when provided with
* a wrong path.
*/
@Test
public void findUnsuccessfully() {
InnerNode parentNode = newNodeInstance(ParentConfigurationSchema.class);
ParentChange parentChange = (ParentChange) parentNode;
assertThrows(KeyNotFoundException.class, () -> ConfigurationUtil.find(List.of("elements", "name", "child"), parentNode, true));
parentChange.changeElements(elements -> elements.createOrUpdate("name", element -> {
}));
assertThrows(KeyNotFoundException.class, () -> ConfigurationUtil.find(List.of("elements", "name", "child", "str0"), parentNode, true));
((NamedElementChange) parentChange.elements().get("name")).changeChild(child -> child.changeStr("value"));
assertThrows(KeyNotFoundException.class, () -> ConfigurationUtil.find(List.of("elements", "name", "child", "str", "foo"), parentNode, true));
}
use of org.apache.ignite.internal.configuration.tree.InnerNode in project ignite-3 by apache.
the class ConfigurationAsmGeneratorTest method testConstructInternalConfig.
@Test
void testConstructInternalConfig() {
InnerNode innerNode = generator.instantiateNode(TestRootConfiguration.KEY.schemaClass());
addDefaults(innerNode);
InnerNode subInnerNode = (InnerNode) ((TestRootView) innerNode).subCfg();
// Check that no fields for internal configuration will be changed.
assertThrows(NoSuchElementException.class, () -> innerNode.construct("str1", null, false));
assertThrows(NoSuchElementException.class, () -> innerNode.construct("i1", null, false));
assertThrows(NoSuchElementException.class, () -> subInnerNode.construct("str3", null, false));
assertThrows(NoSuchElementException.class, () -> subInnerNode.construct("i1", null, false));
// Check that fields for internal configuration will be changed.
innerNode.construct("str1", null, true);
innerNode.construct("i1", null, true);
subInnerNode.construct("str3", null, true);
subInnerNode.construct("i1", null, true);
}
use of org.apache.ignite.internal.configuration.tree.InnerNode in project ignite-3 by apache.
the class ConfigurationUtilTest method testGetInternalConfigs.
@Test
void testGetInternalConfigs() {
Map<Class<?>, Set<Class<?>>> internalExtensions = internalSchemaExtensions(List.of(InternalFirstRootConfigurationSchema.class, InternalSecondRootConfigurationSchema.class, InternalFirstConfigurationSchema.class, InternalSecondConfigurationSchema.class));
ConfigurationAsmGenerator generator = new ConfigurationAsmGenerator();
generator.compileRootSchema(InternalRootConfigurationSchema.class, internalExtensions, Map.of());
InnerNode innerNode = generator.instantiateNode(InternalRootConfigurationSchema.class);
addDefaults(innerNode);
Map<String, Object> config = (Map<String, Object>) innerNode.accept(null, new ConverterToMapVisitor(false));
// Check that no internal configuration will be received.
assertEquals(4, config.size());
assertNull(config.get("str0"));
assertEquals("foo", config.get("str1"));
assertNotNull(config.get("subCfg"));
assertNotNull(config.get("namedCfg"));
Map<String, Object> subConfig = (Map<String, Object>) config.get("subCfg");
assertEquals(1, subConfig.size());
assertEquals("foo", subConfig.get("str00"));
// Check that no internal configuration will be received.
config = (Map<String, Object>) innerNode.accept(null, new ConverterToMapVisitor(true));
assertEquals(7, config.size());
assertNull(config.get("str0"));
assertNull(config.get("str2"));
assertEquals("foo", config.get("str1"));
assertEquals("foo", config.get("str3"));
assertNotNull(config.get("subCfg"));
assertNotNull(config.get("subCfg1"));
assertNotNull(config.get("namedCfg"));
subConfig = (Map<String, Object>) config.get("subCfg");
assertEquals(3, subConfig.size());
assertEquals("foo", subConfig.get("str00"));
assertEquals("foo", subConfig.get("str01"));
assertEquals("foo", subConfig.get("str02"));
subConfig = (Map<String, Object>) config.get("subCfg1");
assertEquals(3, subConfig.size());
assertEquals("foo", subConfig.get("str00"));
assertEquals("foo", subConfig.get("str01"));
assertEquals("foo", subConfig.get("str02"));
}
Aggregations