Search in sources :

Example 1 with InnerNode

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));
    }
}
Also used : Serializable(java.io.Serializable) NamedListNode(org.apache.ignite.internal.configuration.tree.NamedListNode) ConfigurationWrongPolymorphicTypeIdException(org.apache.ignite.configuration.ConfigurationWrongPolymorphicTypeIdException) ConfigurationVisitor(org.apache.ignite.internal.configuration.tree.ConfigurationVisitor) KeyPathNode(org.apache.ignite.internal.configuration.direct.KeyPathNode) NoSuchElementException(java.util.NoSuchElementException) InnerNode(org.apache.ignite.internal.configuration.tree.InnerNode) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with InnerNode

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);
}
Also used : PolymorphicId(org.apache.ignite.configuration.annotation.PolymorphicId) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Arrays(java.util.Arrays) RootInnerNode(org.apache.ignite.internal.configuration.RootInnerNode) DISTRIBUTED(org.apache.ignite.configuration.annotation.ConfigurationType.DISTRIBUTED) NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) AfterAll(org.junit.jupiter.api.AfterAll) BeforeAll(org.junit.jupiter.api.BeforeAll) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) ConfigurationUtil.addDefaults(org.apache.ignite.internal.configuration.util.ConfigurationUtil.addDefaults) Matchers.allOf(org.hamcrest.Matchers.allOf) Set(java.util.Set) UUID(java.util.UUID) RootKey(org.apache.ignite.configuration.RootKey) Serializable(java.io.Serializable) ConfigurationUtil.removeLastKey(org.apache.ignite.internal.configuration.util.ConfigurationUtil.removeLastKey) Test(org.junit.jupiter.api.Test) ConfigValue(org.apache.ignite.configuration.annotation.ConfigValue) Matchers.matchesPattern(org.hamcrest.Matchers.matchesPattern) List(java.util.List) NAME(org.apache.ignite.internal.configuration.tree.NamedListNode.NAME) LOCAL(org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL) ConfigurationUtil.checkConfigurationType(org.apache.ignite.internal.configuration.util.ConfigurationUtil.checkConfigurationType) ConfigurationUtil.collectSchemas(org.apache.ignite.internal.configuration.util.ConfigurationUtil.collectSchemas) ConfigurationFlattener.createFlattenedUpdatesMap(org.apache.ignite.internal.configuration.util.ConfigurationFlattener.createFlattenedUpdatesMap) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Config(org.apache.ignite.configuration.annotation.Config) Matchers.is(org.hamcrest.Matchers.is) NotNull(org.jetbrains.annotations.NotNull) ConfigurationAsmGenerator(org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator) EMPTY_CFG_SRC(org.apache.ignite.internal.configuration.util.ConfigurationUtil.EMPTY_CFG_SRC) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Matchers.aMapWithSize(org.hamcrest.Matchers.aMapWithSize) NamedListNode(org.apache.ignite.internal.configuration.tree.NamedListNode) ORDER_IDX(org.apache.ignite.internal.configuration.tree.NamedListNode.ORDER_IDX) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) InnerNode(org.apache.ignite.internal.configuration.tree.InnerNode) ConfigurationUtil.internalSchemaExtensions(org.apache.ignite.internal.configuration.util.ConfigurationUtil.internalSchemaExtensions) TraversableTreeNode(org.apache.ignite.internal.configuration.tree.TraversableTreeNode) PolymorphicConfig(org.apache.ignite.configuration.annotation.PolymorphicConfig) Collections.singletonMap(java.util.Collections.singletonMap) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) ConfigurationUtil.extensionsFields(org.apache.ignite.internal.configuration.util.ConfigurationUtil.extensionsFields) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) ConfigurationRoot(org.apache.ignite.configuration.annotation.ConfigurationRoot) ConfigurationUtil.compressDeletedEntries(org.apache.ignite.internal.configuration.util.ConfigurationUtil.compressDeletedEntries) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Consumer(java.util.function.Consumer) ConverterToMapVisitor(org.apache.ignite.internal.configuration.tree.ConverterToMapVisitor) Collectors.toList(java.util.stream.Collectors.toList) ConfigurationUtil.find(org.apache.ignite.internal.configuration.util.ConfigurationUtil.find) PolymorphicConfigInstance(org.apache.ignite.configuration.annotation.PolymorphicConfigInstance) SuperRoot(org.apache.ignite.internal.configuration.SuperRoot) ConfigurationUtil.polymorphicSchemaExtensions(org.apache.ignite.internal.configuration.util.ConfigurationUtil.polymorphicSchemaExtensions) TestConfigurationStorage(org.apache.ignite.internal.configuration.storage.TestConfigurationStorage) Matchers.anEmptyMap(org.hamcrest.Matchers.anEmptyMap) Value(org.apache.ignite.configuration.annotation.Value) InternalConfiguration(org.apache.ignite.configuration.annotation.InternalConfiguration) Serializable(java.io.Serializable) HashMap(java.util.HashMap) SuperRoot(org.apache.ignite.internal.configuration.SuperRoot) Matchers.hasToString(org.hamcrest.Matchers.hasToString) RootInnerNode(org.apache.ignite.internal.configuration.RootInnerNode) InnerNode(org.apache.ignite.internal.configuration.tree.InnerNode) Test(org.junit.jupiter.api.Test)

Example 3 with InnerNode

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));
}
Also used : PolymorphicId(org.apache.ignite.configuration.annotation.PolymorphicId) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Arrays(java.util.Arrays) RootInnerNode(org.apache.ignite.internal.configuration.RootInnerNode) DISTRIBUTED(org.apache.ignite.configuration.annotation.ConfigurationType.DISTRIBUTED) NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) AfterAll(org.junit.jupiter.api.AfterAll) BeforeAll(org.junit.jupiter.api.BeforeAll) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) ConfigurationUtil.addDefaults(org.apache.ignite.internal.configuration.util.ConfigurationUtil.addDefaults) Matchers.allOf(org.hamcrest.Matchers.allOf) Set(java.util.Set) UUID(java.util.UUID) RootKey(org.apache.ignite.configuration.RootKey) Serializable(java.io.Serializable) ConfigurationUtil.removeLastKey(org.apache.ignite.internal.configuration.util.ConfigurationUtil.removeLastKey) Test(org.junit.jupiter.api.Test) ConfigValue(org.apache.ignite.configuration.annotation.ConfigValue) Matchers.matchesPattern(org.hamcrest.Matchers.matchesPattern) List(java.util.List) NAME(org.apache.ignite.internal.configuration.tree.NamedListNode.NAME) LOCAL(org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL) ConfigurationUtil.checkConfigurationType(org.apache.ignite.internal.configuration.util.ConfigurationUtil.checkConfigurationType) ConfigurationUtil.collectSchemas(org.apache.ignite.internal.configuration.util.ConfigurationUtil.collectSchemas) ConfigurationFlattener.createFlattenedUpdatesMap(org.apache.ignite.internal.configuration.util.ConfigurationFlattener.createFlattenedUpdatesMap) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Config(org.apache.ignite.configuration.annotation.Config) Matchers.is(org.hamcrest.Matchers.is) NotNull(org.jetbrains.annotations.NotNull) ConfigurationAsmGenerator(org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator) EMPTY_CFG_SRC(org.apache.ignite.internal.configuration.util.ConfigurationUtil.EMPTY_CFG_SRC) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Matchers.aMapWithSize(org.hamcrest.Matchers.aMapWithSize) NamedListNode(org.apache.ignite.internal.configuration.tree.NamedListNode) ORDER_IDX(org.apache.ignite.internal.configuration.tree.NamedListNode.ORDER_IDX) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) InnerNode(org.apache.ignite.internal.configuration.tree.InnerNode) ConfigurationUtil.internalSchemaExtensions(org.apache.ignite.internal.configuration.util.ConfigurationUtil.internalSchemaExtensions) TraversableTreeNode(org.apache.ignite.internal.configuration.tree.TraversableTreeNode) PolymorphicConfig(org.apache.ignite.configuration.annotation.PolymorphicConfig) Collections.singletonMap(java.util.Collections.singletonMap) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) ConfigurationUtil.extensionsFields(org.apache.ignite.internal.configuration.util.ConfigurationUtil.extensionsFields) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) ConfigurationRoot(org.apache.ignite.configuration.annotation.ConfigurationRoot) ConfigurationUtil.compressDeletedEntries(org.apache.ignite.internal.configuration.util.ConfigurationUtil.compressDeletedEntries) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Consumer(java.util.function.Consumer) ConverterToMapVisitor(org.apache.ignite.internal.configuration.tree.ConverterToMapVisitor) Collectors.toList(java.util.stream.Collectors.toList) ConfigurationUtil.find(org.apache.ignite.internal.configuration.util.ConfigurationUtil.find) PolymorphicConfigInstance(org.apache.ignite.configuration.annotation.PolymorphicConfigInstance) SuperRoot(org.apache.ignite.internal.configuration.SuperRoot) ConfigurationUtil.polymorphicSchemaExtensions(org.apache.ignite.internal.configuration.util.ConfigurationUtil.polymorphicSchemaExtensions) TestConfigurationStorage(org.apache.ignite.internal.configuration.storage.TestConfigurationStorage) Matchers.anEmptyMap(org.hamcrest.Matchers.anEmptyMap) Value(org.apache.ignite.configuration.annotation.Value) InternalConfiguration(org.apache.ignite.configuration.annotation.InternalConfiguration) RootInnerNode(org.apache.ignite.internal.configuration.RootInnerNode) InnerNode(org.apache.ignite.internal.configuration.tree.InnerNode) Test(org.junit.jupiter.api.Test)

Example 4 with InnerNode

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);
}
Also used : InnerNode(org.apache.ignite.internal.configuration.tree.InnerNode) Test(org.junit.jupiter.api.Test)

Example 5 with InnerNode

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"));
}
Also used : Set(java.util.Set) ConfigurationAsmGenerator(org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator) ConverterToMapVisitor(org.apache.ignite.internal.configuration.tree.ConverterToMapVisitor) Matchers.hasToString(org.hamcrest.Matchers.hasToString) RootInnerNode(org.apache.ignite.internal.configuration.RootInnerNode) InnerNode(org.apache.ignite.internal.configuration.tree.InnerNode) Map(java.util.Map) ConfigurationFlattener.createFlattenedUpdatesMap(org.apache.ignite.internal.configuration.util.ConfigurationFlattener.createFlattenedUpdatesMap) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) Matchers.anEmptyMap(org.hamcrest.Matchers.anEmptyMap) Test(org.junit.jupiter.api.Test)

Aggregations

InnerNode (org.apache.ignite.internal.configuration.tree.InnerNode)20 HashMap (java.util.HashMap)13 Serializable (java.io.Serializable)12 List (java.util.List)12 Map (java.util.Map)12 NoSuchElementException (java.util.NoSuchElementException)11 Set (java.util.Set)11 NamedListNode (org.apache.ignite.internal.configuration.tree.NamedListNode)11 Collectors.toList (java.util.stream.Collectors.toList)10 RootKey (org.apache.ignite.configuration.RootKey)10 RootInnerNode (org.apache.ignite.internal.configuration.RootInnerNode)10 Test (org.junit.jupiter.api.Test)10 Arrays (java.util.Arrays)9 UUID (java.util.UUID)9 Consumer (java.util.function.Consumer)9 Config (org.apache.ignite.configuration.annotation.Config)9 ConfigurationRoot (org.apache.ignite.configuration.annotation.ConfigurationRoot)9 InternalConfiguration (org.apache.ignite.configuration.annotation.InternalConfiguration)9 NamedConfigValue (org.apache.ignite.configuration.annotation.NamedConfigValue)9 PolymorphicConfig (org.apache.ignite.configuration.annotation.PolymorphicConfig)9