Search in sources :

Example 1 with ColumnMapper

use of org.apache.ignite.internal.schema.mapping.ColumnMapper in project ignite-3 by apache.

the class AbstractSerializerTest method columnMappingSerializeTest.

/**
 * (de)Serialize column mapping test.
 */
@Test
public void columnMappingSerializeTest() {
    final AbstractSchemaSerializer assembler = SchemaSerializerImpl.INSTANCE;
    SchemaDescriptor desc = new SchemaDescriptor(100500, new Column[] { new Column("A", NativeTypes.INT8, false, () -> (byte) 1) }, new Column[] { new Column("A1", NativeTypes.stringOf(128), false, () -> "test"), new Column("B1", NativeTypes.numberOf(255), false, () -> BigInteger.TEN) });
    ColumnMapper mapper = ColumnMapping.createMapper(desc);
    mapper.add(0, 1);
    Column c1 = new Column("C1", NativeTypes.stringOf(128), false, () -> "brandNewColumn").copy(2);
    mapper.add(c1);
    desc.columnMapping(mapper);
    byte[] serialize = assembler.serialize(desc);
    SchemaDescriptor deserialize = assembler.deserialize(serialize);
    ColumnMapper mapper1 = deserialize.columnMapping();
    assertEquals(1, mapper1.map(0));
    assertEquals(c1, mapper1.mappedColumn(2));
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) AbstractSchemaSerializer(org.apache.ignite.internal.schema.marshaller.schema.AbstractSchemaSerializer) Column(org.apache.ignite.internal.schema.Column) ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper) Test(org.junit.jupiter.api.Test)

Example 2 with ColumnMapper

use of org.apache.ignite.internal.schema.mapping.ColumnMapper in project ignite-3 by apache.

the class SchemaSerializerImpl method readColumnMapping.

/**
 * Reads column mapping from byte buffer.
 *
 * @param desc SchemaDescriptor.
 * @param buf  Byte buffer.
 * @return ColumnMapper object.
 */
private ColumnMapper readColumnMapping(SchemaDescriptor desc, ByteBuffer buf) {
    int mappingSize = buf.getInt();
    if (mappingSize == 0) {
        return ColumnMapping.identityMapping();
    }
    ColumnMapper mapper = ColumnMapping.createMapper(desc);
    for (int i = 0; i < mappingSize; i++) {
        int from = buf.getInt();
        int to = buf.getInt();
        if (to == -1) {
            Column col = readColumn(buf);
            mapper.add(col);
        } else {
            mapper.add(from, to);
        }
    }
    return mapper;
}
Also used : Column(org.apache.ignite.internal.schema.Column) ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper)

Example 3 with ColumnMapper

use of org.apache.ignite.internal.schema.mapping.ColumnMapper 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 4 with ColumnMapper

use of org.apache.ignite.internal.schema.mapping.ColumnMapper in project ignite-3 by apache.

the class SchemaSerializerImpl method readFrom.

/**
 * {@inheritDoc}
 */
@Override
public SchemaDescriptor readFrom(ByteBuffer byteBuf) {
    int ver = byteBuf.getInt();
    Column[] keyCols = readColumns(byteBuf);
    Column[] valCols = readColumns(byteBuf);
    int colocationColsSize = byteBuf.getInt();
    String[] colocationCols = new String[colocationColsSize];
    for (int i = 0; i < colocationColsSize; i++) {
        colocationCols[i] = readString(byteBuf);
    }
    SchemaDescriptor descriptor = new SchemaDescriptor(ver, keyCols, colocationCols, valCols);
    ColumnMapper mapper = readColumnMapping(descriptor, byteBuf);
    descriptor.columnMapping(mapper);
    return descriptor;
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper)

Example 5 with ColumnMapper

use of org.apache.ignite.internal.schema.mapping.ColumnMapper in project ignite-3 by apache.

the class SchemaRegistryImplTest method testSchemaCacheCleanup.

/**
 * Check schema cache cleanup.
 */
@Test
public void testSchemaCacheCleanup() {
    final SchemaDescriptor schemaV1 = new SchemaDescriptor(1, new Column[] { new Column("keyLongCol", INT64, false) }, new Column[] { new Column("valBytesCol", BYTES, true) });
    final SchemaDescriptor schemaV2 = new SchemaDescriptor(2, new Column[] { new Column("keyLongCol", INT64, false) }, new Column[] { new Column("valBytesCol", BYTES, true), new Column("valStringCol", STRING, true) });
    schemaV2.columnMapping(createMapper(schemaV2).add(schemaV2.column("valStringCol")));
    final SchemaDescriptor schemaV3 = new SchemaDescriptor(3, new Column[] { new Column("keyLongCol", INT64, false) }, new Column[] { new Column("valStringCol", STRING, true) });
    schemaV3.columnMapping(createMapper(schemaV3).add(schemaV3.column("valStringCol").schemaIndex(), schemaV2.column("valStringCol").schemaIndex()));
    final SchemaDescriptor schemaV4 = new SchemaDescriptor(4, new Column[] { new Column("keyLongCol", INT64, false) }, new Column[] { new Column("valBytesCol", BYTES, true), new Column("valStringCol", STRING, true) });
    schemaV4.columnMapping(createMapper(schemaV4).add(schemaV4.column("valBytesCol")));
    final SchemaRegistryImpl reg = new SchemaRegistryImpl(v -> null, () -> INITIAL_SCHEMA_VERSION);
    final Map<Long, ColumnMapper> cache = reg.mappingCache();
    reg.onSchemaRegistered(schemaV1);
    reg.onSchemaRegistered(schemaV2);
    reg.onSchemaRegistered(schemaV3);
    reg.onSchemaRegistered(schemaV4);
    assertEquals(0, cache.size());
    reg.resolveMapping(schemaV4, schemaV1);
    reg.resolveMapping(schemaV3, schemaV1);
    reg.resolveMapping(schemaV4, schemaV2);
    assertEquals(3, cache.size());
    reg.onSchemaDropped(schemaV1.version());
    assertEquals(1, cache.size());
    reg.onSchemaDropped(schemaV2.version());
    assertEquals(0, cache.size());
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper) Test(org.junit.jupiter.api.Test)

Aggregations

ColumnMapper (org.apache.ignite.internal.schema.mapping.ColumnMapper)8 Column (org.apache.ignite.internal.schema.Column)5 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)5 Test (org.junit.jupiter.api.Test)3 Row (org.apache.ignite.internal.schema.row.Row)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Random (java.util.Random)1 NamedListView (org.apache.ignite.configuration.NamedListView)1 ColumnView (org.apache.ignite.configuration.schemas.table.ColumnView)1 TableChange (org.apache.ignite.configuration.schemas.table.TableChange)1 TableView (org.apache.ignite.configuration.schemas.table.TableView)1