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));
}
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;
}
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;
}
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;
}
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());
}
Aggregations