Search in sources :

Example 1 with NamedListView

use of org.apache.ignite.configuration.NamedListView in project ignite-3 by apache.

the class SchemaUtils method columnMapper.

/**
 * Prepares column mapper.
 *
 * @param oldDesc Old schema descriptor.
 * @param oldTbl  Old table configuration.
 * @param newDesc New schema descriptor.
 * @param newTbl  New table configuration.
 * @return Column mapper.
 */
public static ColumnMapper columnMapper(SchemaDescriptor oldDesc, TableView oldTbl, SchemaDescriptor newDesc, TableChange newTbl) {
    ColumnMapper mapper = null;
    NamedListView<? extends ColumnView> newTblColumns = newTbl.columns();
    NamedListView<? extends ColumnView> oldTblColumns = oldTbl.columns();
    // because removed keys are simply replaced with nulls
    assert newTblColumns.size() >= oldTblColumns.size();
    for (int i = 0; i < newTblColumns.size(); ++i) {
        ColumnView newColView = newTblColumns.get(i);
        // new value can be null if a column has been deleted
        if (newColView == null) {
            continue;
        }
        if (i < oldTblColumns.size()) {
            ColumnView oldColView = oldTblColumns.get(i);
            Column newCol = newDesc.column(newColView.name());
            Column oldCol = oldDesc.column(oldColView.name());
            if (newCol.schemaIndex() == oldCol.schemaIndex()) {
                continue;
            }
            if (mapper == null) {
                mapper = ColumnMapping.createMapper(newDesc);
            }
            mapper.add(newCol.schemaIndex(), oldCol.schemaIndex());
        } else {
            // if the new Named List is larger than the old one, it can only mean that a new column has been added
            Column newCol = newDesc.column(newColView.name());
            assert !newDesc.isKeyColumn(newCol.schemaIndex());
            if (mapper == null) {
                mapper = ColumnMapping.createMapper(newDesc);
            }
            mapper.add(newCol);
        }
    }
    // since newTblColumns comes from a TableChange, it will contain nulls for removed columns
    Optional<Column> droppedKeyCol = newTblColumns.namedListKeys().stream().filter(k -> newTblColumns.get(k) == null).map(oldDesc::column).filter(c -> oldDesc.isKeyColumn(c.schemaIndex())).findAny();
    // TODO: configuration validators.
    assert droppedKeyCol.isEmpty() : IgniteStringFormatter.format("Dropping of key column is forbidden: [schemaVer={}, col={}]", newDesc.version(), droppedKeyCol.get());
    return mapper == null ? ColumnMapping.identityMapping() : mapper;
}
Also used : ColumnView(org.apache.ignite.configuration.schemas.table.ColumnView) SchemaDescriptorConverter(org.apache.ignite.internal.schema.configuration.SchemaDescriptorConverter) TableDefinition(org.apache.ignite.schema.definition.TableDefinition) SchemaConfigurationConverter(org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter) NamedListView(org.apache.ignite.configuration.NamedListView) Optional(java.util.Optional) ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) TableChange(org.apache.ignite.configuration.schemas.table.TableChange) ColumnMapping(org.apache.ignite.internal.schema.mapping.ColumnMapping) TableView(org.apache.ignite.configuration.schemas.table.TableView) ColumnView(org.apache.ignite.configuration.schemas.table.ColumnView) ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper)

Example 2 with NamedListView

use of org.apache.ignite.configuration.NamedListView in project ignite-3 by apache.

the class ConfigurationChangerTest method testGetLatestNamedList.

/**
 * Tests the {@link DynamicConfigurationChanger#getLatest} method by retrieving different Named List configuration values.
 */
@Test
public void testGetLatestNamedList() throws Exception {
    ConfigurationChanger changer = createChanger(DefaultsConfiguration.KEY);
    changer.start();
    changer.initializeDefaults();
    ConfigurationSource source = source(DefaultsConfiguration.KEY, (DefaultsChange change) -> change.changeChildrenList(children -> {
        children.create("name1", child -> {
        });
        children.create("name2", child -> {
        });
    }));
    changer.change(source).get(1, SECONDS);
    for (String name : List.of("name1", "name2")) {
        NamedListView<DefaultsChildView> childrenListView = changer.getLatest(List.of(node("def"), node("childrenList")));
        DefaultsChildView childrenListElementView = childrenListView.get(name);
        assertEquals("bar", childrenListElementView.defStr());
        assertArrayEquals(new String[] { "xyz" }, childrenListElementView.arr());
        childrenListElementView = changer.getLatest(List.of(node("def"), node("childrenList"), listNode(name)));
        assertEquals("bar", childrenListElementView.defStr());
        assertArrayEquals(new String[] { "xyz" }, childrenListElementView.arr());
        String childrenListStrValueView = changer.getLatest(List.of(node("def"), node("childrenList"), listNode(name), node("defStr")));
        assertEquals("bar", childrenListStrValueView);
        String[] childrenListArrView = changer.getLatest(List.of(node("def"), node("childrenList"), listNode(name), node("arr")));
        assertArrayEquals(new String[] { "xyz" }, childrenListArrView);
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) Data(org.apache.ignite.internal.configuration.storage.Data) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) TimeoutException(java.util.concurrent.TimeoutException) ConstructableTreeNode(org.apache.ignite.internal.configuration.tree.ConstructableTreeNode) CompletableFuture(java.util.concurrent.CompletableFuture) ConfigurationStorage(org.apache.ignite.internal.configuration.storage.ConfigurationStorage) Validator(org.apache.ignite.configuration.validation.Validator) Retention(java.lang.annotation.Retention) NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) Immutable(org.apache.ignite.configuration.validation.Immutable) AfterAll(org.junit.jupiter.api.AfterAll) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyPathNode(org.apache.ignite.internal.configuration.direct.KeyPathNode) Map(java.util.Map) KEY(org.apache.ignite.internal.configuration.FirstConfiguration.KEY) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) FIELD(java.lang.annotation.ElementType.FIELD) ConfigurationRoot(org.apache.ignite.configuration.annotation.ConfigurationRoot) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) NamedListView(org.apache.ignite.configuration.NamedListView) Set(java.util.Set) Target(java.lang.annotation.Target) ConfigurationChangeException(org.apache.ignite.configuration.ConfigurationChangeException) RootKey(org.apache.ignite.configuration.RootKey) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) Serializable(java.io.Serializable) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) ConfigValue(org.apache.ignite.configuration.annotation.ConfigValue) List(java.util.List) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) LOCAL(org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL) Config(org.apache.ignite.configuration.annotation.Config) TestConfigurationStorage(org.apache.ignite.internal.configuration.storage.TestConfigurationStorage) ConfigurationSource(org.apache.ignite.internal.configuration.tree.ConfigurationSource) Matchers.containsString(org.hamcrest.Matchers.containsString) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Value(org.apache.ignite.configuration.annotation.Value) ConfigurationAsmGenerator(org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator) ConfigurationSource(org.apache.ignite.internal.configuration.tree.ConfigurationSource) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Example 3 with NamedListView

use of org.apache.ignite.configuration.NamedListView in project ignite-3 by apache.

the class ValidationUtilTest method validateNamedListNode.

@Test
public void validateNamedListNode() throws Exception {
    var rootsNode = new SuperRoot(key -> null, Map.of(ValidatedRootConfiguration.KEY, root));
    Validator<NamedListValidation, NamedListView<?>> validator = new Validator<>() {

        @Override
        public void validate(NamedListValidation annotation, ValidationContext<NamedListView<?>> ctx) {
            assertEquals("root.elements", ctx.currentKey());
            assertEquals(List.of(), ctx.getOldValue().namedListKeys());
            assertEquals(List.of(), ctx.getNewValue().namedListKeys());
            ctx.addIssue(new ValidationIssue("bar"));
        }
    };
    Map<Class<? extends Annotation>, Set<Validator<?, ?>>> validators = Map.of(NamedListValidation.class, Set.of(validator));
    List<ValidationIssue> issues = ValidationUtil.validate(rootsNode, rootsNode, null, new HashMap<>(), validators);
    assertEquals(1, issues.size());
    assertEquals("bar", issues.get(0).message());
}
Also used : Set(java.util.Set) SuperRoot(org.apache.ignite.internal.configuration.SuperRoot) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) Annotation(java.lang.annotation.Annotation) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) NamedListView(org.apache.ignite.configuration.NamedListView) Validator(org.apache.ignite.configuration.validation.Validator) Test(org.junit.jupiter.api.Test)

Example 4 with NamedListView

use of org.apache.ignite.configuration.NamedListView in project ignite-3 by apache.

the class TableValidatorImplTest method testMisalignedColumnNamedListKeys.

/**
 * Tests that column names and column keys inside a Named List must be equal.
 */
@Test
void testMisalignedColumnNamedListKeys(@InjectConfiguration(polymorphicExtensions = RocksDbDataRegionConfigurationSchema.class) DataStorageConfiguration dbCfg) {
    NamedListView<TableView> oldValue = tablesCfg.tables().value();
    TableConfiguration tableCfg = tablesCfg.tables().get("table");
    CompletableFuture<Void> tableChangeFuture = tableCfg.columns().change(columnsChange -> columnsChange.create("ololo", columnChange -> columnChange.changeName("not ololo").changeType(columnTypeChange -> columnTypeChange.changeType("STRING")).changeNullable(true)));
    assertThat(tableChangeFuture, willBe(nullValue(Void.class)));
    ValidationContext<NamedListView<TableView>> ctx = mockContext(oldValue, dbCfg.value());
    ArgumentCaptor<ValidationIssue> issuesCaptor = validate(ctx);
    assertThat(issuesCaptor.getAllValues(), hasSize(1));
    assertThat(issuesCaptor.getValue().message(), is(equalTo("Column name \"not ololo\" does not match its Named List key: \"ololo\"")));
}
Also used : HashIndexConfigurationSchema(org.apache.ignite.configuration.schemas.table.HashIndexConfigurationSchema) PageMemoryDataRegionConfigurationSchema(org.apache.ignite.configuration.schemas.store.PageMemoryDataRegionConfigurationSchema) TableConfiguration(org.apache.ignite.configuration.schemas.table.TableConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) PartialIndexConfigurationSchema(org.apache.ignite.configuration.schemas.table.PartialIndexConfigurationSchema) SortedIndexConfigurationSchema(org.apache.ignite.configuration.schemas.table.SortedIndexConfigurationSchema) InjectConfiguration(org.apache.ignite.internal.configuration.testframework.InjectConfiguration) ArgumentCaptor(org.mockito.ArgumentCaptor) UnsafeMemoryAllocatorConfigurationSchema(org.apache.ignite.configuration.schemas.store.UnsafeMemoryAllocatorConfigurationSchema) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ConfigurationExtension(org.apache.ignite.internal.configuration.testframework.ConfigurationExtension) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Matchers.hasSize(org.hamcrest.Matchers.hasSize) RocksDbDataRegionConfigurationSchema(org.apache.ignite.configuration.schemas.store.RocksDbDataRegionConfigurationSchema) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DataStorageView(org.apache.ignite.configuration.schemas.store.DataStorageView) Matchers.empty(org.hamcrest.Matchers.empty) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) NamedListView(org.apache.ignite.configuration.NamedListView) DataStorageConfiguration(org.apache.ignite.configuration.schemas.store.DataStorageConfiguration) TablesConfiguration(org.apache.ignite.configuration.schemas.table.TablesConfiguration) CompletableFutureMatcher.willBe(org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willBe) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) TableView(org.apache.ignite.configuration.schemas.table.TableView) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Nullable(org.jetbrains.annotations.Nullable) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) Matchers.equalTo(org.hamcrest.Matchers.equalTo) HashIndexChange(org.apache.ignite.configuration.schemas.table.HashIndexChange) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) NamedListView(org.apache.ignite.configuration.NamedListView) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) TableConfiguration(org.apache.ignite.configuration.schemas.table.TableConfiguration) TableView(org.apache.ignite.configuration.schemas.table.TableView) Test(org.junit.jupiter.api.Test)

Example 5 with NamedListView

use of org.apache.ignite.configuration.NamedListView in project ignite-3 by apache.

the class TableValidatorImplTest method testMisalignedIndexNamedListKeys.

/**
 * Tests that index names and index keys inside a Named List must be equal.
 */
@Test
void testMisalignedIndexNamedListKeys(@InjectConfiguration(polymorphicExtensions = RocksDbDataRegionConfigurationSchema.class) DataStorageConfiguration dbCfg) {
    NamedListView<TableView> oldValue = tablesCfg.tables().value();
    TableConfiguration tableCfg = tablesCfg.tables().get("table");
    CompletableFuture<Void> tableChangeFuture = tableCfg.indices().change(indicesChange -> indicesChange.create("ololo", indexChange -> indexChange.changeName("not ololo").convert(HashIndexChange.class).changeColNames("id")));
    assertThat(tableChangeFuture, willBe(nullValue(Void.class)));
    ValidationContext<NamedListView<TableView>> ctx = mockContext(oldValue, dbCfg.value());
    ArgumentCaptor<ValidationIssue> issuesCaptor = validate(ctx);
    assertThat(issuesCaptor.getAllValues(), hasSize(1));
    assertThat(issuesCaptor.getValue().message(), is(equalTo("Index name \"not ololo\" does not match its Named List key: \"ololo\"")));
}
Also used : HashIndexConfigurationSchema(org.apache.ignite.configuration.schemas.table.HashIndexConfigurationSchema) PageMemoryDataRegionConfigurationSchema(org.apache.ignite.configuration.schemas.store.PageMemoryDataRegionConfigurationSchema) TableConfiguration(org.apache.ignite.configuration.schemas.table.TableConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) PartialIndexConfigurationSchema(org.apache.ignite.configuration.schemas.table.PartialIndexConfigurationSchema) SortedIndexConfigurationSchema(org.apache.ignite.configuration.schemas.table.SortedIndexConfigurationSchema) InjectConfiguration(org.apache.ignite.internal.configuration.testframework.InjectConfiguration) ArgumentCaptor(org.mockito.ArgumentCaptor) UnsafeMemoryAllocatorConfigurationSchema(org.apache.ignite.configuration.schemas.store.UnsafeMemoryAllocatorConfigurationSchema) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ConfigurationExtension(org.apache.ignite.internal.configuration.testframework.ConfigurationExtension) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Matchers.hasSize(org.hamcrest.Matchers.hasSize) RocksDbDataRegionConfigurationSchema(org.apache.ignite.configuration.schemas.store.RocksDbDataRegionConfigurationSchema) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DataStorageView(org.apache.ignite.configuration.schemas.store.DataStorageView) Matchers.empty(org.hamcrest.Matchers.empty) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) NamedListView(org.apache.ignite.configuration.NamedListView) DataStorageConfiguration(org.apache.ignite.configuration.schemas.store.DataStorageConfiguration) TablesConfiguration(org.apache.ignite.configuration.schemas.table.TablesConfiguration) CompletableFutureMatcher.willBe(org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willBe) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) TableView(org.apache.ignite.configuration.schemas.table.TableView) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Nullable(org.jetbrains.annotations.Nullable) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) Matchers.equalTo(org.hamcrest.Matchers.equalTo) HashIndexChange(org.apache.ignite.configuration.schemas.table.HashIndexChange) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) HashIndexChange(org.apache.ignite.configuration.schemas.table.HashIndexChange) NamedListView(org.apache.ignite.configuration.NamedListView) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) TableConfiguration(org.apache.ignite.configuration.schemas.table.TableConfiguration) TableView(org.apache.ignite.configuration.schemas.table.TableView) Test(org.junit.jupiter.api.Test)

Aggregations

NamedListView (org.apache.ignite.configuration.NamedListView)5 ValidationContext (org.apache.ignite.configuration.validation.ValidationContext)4 ValidationIssue (org.apache.ignite.configuration.validation.ValidationIssue)4 Test (org.junit.jupiter.api.Test)4 CompletableFuture (java.util.concurrent.CompletableFuture)3 TableView (org.apache.ignite.configuration.schemas.table.TableView)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Set (java.util.Set)2 TimeUnit (java.util.concurrent.TimeUnit)2 DataStorageConfiguration (org.apache.ignite.configuration.schemas.store.DataStorageConfiguration)2 DataStorageView (org.apache.ignite.configuration.schemas.store.DataStorageView)2 PageMemoryDataRegionConfigurationSchema (org.apache.ignite.configuration.schemas.store.PageMemoryDataRegionConfigurationSchema)2 RocksDbDataRegionConfigurationSchema (org.apache.ignite.configuration.schemas.store.RocksDbDataRegionConfigurationSchema)2 UnsafeMemoryAllocatorConfigurationSchema (org.apache.ignite.configuration.schemas.store.UnsafeMemoryAllocatorConfigurationSchema)2 HashIndexChange (org.apache.ignite.configuration.schemas.table.HashIndexChange)2 HashIndexConfigurationSchema (org.apache.ignite.configuration.schemas.table.HashIndexConfigurationSchema)2 PartialIndexConfigurationSchema (org.apache.ignite.configuration.schemas.table.PartialIndexConfigurationSchema)2 SortedIndexConfigurationSchema (org.apache.ignite.configuration.schemas.table.SortedIndexConfigurationSchema)2 TableConfiguration (org.apache.ignite.configuration.schemas.table.TableConfiguration)2