use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.
the class SqlSchemaManagerImpl method convert.
private IgniteTableImpl convert(TableImpl table) {
SchemaDescriptor descriptor = table.schemaView().schema();
List<ColumnDescriptor> colDescriptors = descriptor.columnNames().stream().map(descriptor::column).sorted(Comparator.comparingInt(Column::columnOrder)).map(col -> new ColumnDescriptorImpl(col.name(), descriptor.isKeyColumn(col.schemaIndex()), col.columnOrder(), col.schemaIndex(), col.type(), col::defaultValue)).collect(Collectors.toList());
return new IgniteTableImpl(new TableDescriptorImpl(colDescriptors), table.internalTable(), table.schemaView());
}
use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.
the class SchemaDescriptorConverter method convert.
/**
* Build schema descriptor by table schema.
*
* @param schemaVer Schema version.
* @param tblCfg Table schema.
* @return SchemaDescriptor.
*/
public static SchemaDescriptor convert(int schemaVer, TableDefinition tblCfg) {
Set<String> keyColumnsNames = tblCfg.keyColumns();
List<Column> keyCols = new ArrayList<>(keyColumnsNames.size());
List<Column> valCols = new ArrayList<>(tblCfg.columns().size() - keyColumnsNames.size());
int idx = 0;
for (ColumnDefinition col : tblCfg.columns()) {
if (keyColumnsNames.contains(col.name())) {
keyCols.add(convert(idx, col));
} else {
valCols.add(convert(idx, col));
}
idx++;
}
return new SchemaDescriptor(schemaVer, keyCols.toArray(Column[]::new), tblCfg.colocationColumns().toArray(String[]::new), valCols.toArray(Column[]::new));
}
use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.
the class SchemaRegistryImpl method schema.
/**
* {@inheritDoc}
*/
@Override
public SchemaDescriptor schema(int ver) {
if (ver == 0) {
// Use last version (any version may be used) for 0 version, that mean row doens't contain value.
ver = lastVer;
}
SchemaDescriptor desc = schemaCache.get(ver);
if (desc != null) {
return desc;
}
desc = history.apply(ver);
if (desc != null) {
schemaCache.putIfAbsent(ver, desc);
return desc;
}
if (lastVer < ver || ver <= 0) {
throw new SchemaRegistryException("Incorrect schema version requested: ver=" + ver);
} else {
throw new SchemaRegistryException("Failed to find schema: ver=" + ver);
}
}
use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.
the class AsmMarshallerGenerator method create.
/**
* {@inheritDoc}
*/
@Override
public <K, V> KvMarshaller<K, V> create(SchemaDescriptor schema, Mapper<K> keyMapper, Mapper<V> valueMapper) {
final String className = MARSHALLER_CLASS_NAME_PREFIX + schema.version();
Class<K> keyClass = keyMapper.targetType();
Class<V> valClass = valueMapper.targetType();
final StringWriter writer = new StringWriter();
try {
// Generate Marshaller code.
long generation = System.nanoTime();
final ClassDefinition classDef = generateMarshallerClass(className, schema, keyClass, valClass);
long compilationTime = System.nanoTime();
generation = compilationTime - generation;
final ClassGenerator generator = ClassGenerator.classGenerator(getClassLoader());
if (dumpCode) {
generator.outputTo(writer).fakeLineNumbers(true).runAsmVerifier(true).dumpRawBytecode(true);
}
final Class<? extends KvMarshaller> aClass = generator.defineClass(classDef, KvMarshaller.class);
compilationTime = System.nanoTime() - compilationTime;
if (LOG.isTraceEnabled()) {
LOG.trace("ASM marshaller created: codeGenStage={}us, compileStage={}us. Code: {}", TimeUnit.NANOSECONDS.toMicros(generation), TimeUnit.NANOSECONDS.toMicros(compilationTime), writer);
} else if (LOG.isDebugEnabled()) {
LOG.debug("ASM marshaller created: codeGenStage={}us, compileStage={}us.", TimeUnit.NANOSECONDS.toMicros(generation), TimeUnit.NANOSECONDS.toMicros(compilationTime));
}
// noinspection unchecked
return aClass.getDeclaredConstructor(SchemaDescriptor.class, ObjectFactory.class, ObjectFactory.class).newInstance(schema, MarshallerUtil.factoryForClass(keyClass), MarshallerUtil.factoryForClass(valClass));
} catch (Exception | LinkageError e) {
throw new IllegalArgumentException("Failed to create marshaller for key-value pair: schemaVer=" + schema.version() + ", keyClass=" + keyClass.getSimpleName() + ", valueClass=" + valClass.getSimpleName(), e);
}
}
use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.
the class AsmMarshallerGenerator method generateAssemblerFactoryMethod.
/**
* Generates helper method.
*
* @param classDef Marshaller class definition.
* @param schema Schema descriptor.
* @param keyMarsh Key marshaller code generator.
* @param valMarsh Value marshaller code generator.
*/
private void generateAssemblerFactoryMethod(ClassDefinition classDef, SchemaDescriptor schema, MarshallerCodeGenerator keyMarsh, MarshallerCodeGenerator valMarsh) {
final MethodDefinition methodDef = classDef.declareMethod(EnumSet.of(Access.PRIVATE), "createAssembler", ParameterizedType.type(RowAssembler.class), Parameter.arg("key", Object.class), Parameter.arg("val", Object.class));
final Scope scope = methodDef.getScope();
final BytecodeBlock body = methodDef.getBody();
final Variable varlenKeyCols = scope.declareVariable("varlenKeyCols", body, BytecodeExpressions.defaultValue(int.class));
final Variable varlenValueCols = scope.declareVariable("varlenValueCols", body, BytecodeExpressions.defaultValue(int.class));
final Variable keyCols = scope.declareVariable(Columns.class, "keyCols");
final Variable valCols = scope.declareVariable(Columns.class, "valCols");
body.append(keyCols.set(methodDef.getThis().getField("schema", SchemaDescriptor.class).invoke("keyColumns", Columns.class)));
body.append(valCols.set(methodDef.getThis().getField("schema", SchemaDescriptor.class).invoke("valueColumns", Columns.class)));
Columns columns = schema.keyColumns();
if (columns.hasVarlengthColumns()) {
final Variable tmp = scope.createTempVariable(Object.class);
for (int i = columns.firstVarlengthColumn(); i < columns.length(); i++) {
assert !columns.column(i).type().spec().fixedLength();
body.append(keyMarsh.getValue(classDef.getType(), scope.getVariable("key"), i)).putVariable(tmp);
body.append(new IfStatement().condition(BytecodeExpressions.isNotNull(tmp)).ifTrue(new BytecodeBlock().append(varlenKeyCols.increment())));
}
}
columns = schema.valueColumns();
if (columns.hasVarlengthColumns()) {
final Variable tmp = scope.createTempVariable(Object.class);
for (int i = columns.firstVarlengthColumn(); i < columns.length(); i++) {
assert !columns.column(i).type().spec().fixedLength();
body.append(valMarsh.getValue(classDef.getType(), scope.getVariable("val"), i)).putVariable(tmp);
body.append(new IfStatement().condition(BytecodeExpressions.isNotNull(tmp)).ifTrue(new BytecodeBlock().append(varlenValueCols.increment())));
}
}
body.append(BytecodeExpressions.newInstance(RowAssembler.class, methodDef.getThis().getField("schema", SchemaDescriptor.class), varlenKeyCols, varlenValueCols));
body.retObject();
}
Aggregations