Search in sources :

Example 26 with SchemaDescriptor

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());
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) SchemaPlus(org.apache.calcite.schema.SchemaPlus) Frameworks(org.apache.calcite.tools.Frameworks) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) HashMap(java.util.HashMap) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Column(org.apache.ignite.internal.schema.Column) TableImpl(org.apache.ignite.internal.table.TableImpl) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) Map(java.util.Map) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) Comparator(java.util.Comparator) NotNull(org.jetbrains.annotations.NotNull) VersionedValue(org.apache.ignite.internal.causality.VersionedValue) TableManager(org.apache.ignite.internal.table.distributed.TableManager) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor)

Example 27 with SchemaDescriptor

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));
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) ArrayList(java.util.ArrayList) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition)

Example 28 with SchemaDescriptor

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);
    }
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor)

Example 29 with SchemaDescriptor

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);
    }
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) MarshallerException(org.apache.ignite.internal.schema.marshaller.MarshallerException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) ClassGenerator(com.facebook.presto.bytecode.ClassGenerator) StringWriter(java.io.StringWriter) ObjectFactory(org.apache.ignite.internal.util.ObjectFactory)

Example 30 with SchemaDescriptor

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();
}
Also used : IfStatement(com.facebook.presto.bytecode.control.IfStatement) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) RowAssembler(org.apache.ignite.internal.schema.row.RowAssembler) Columns(org.apache.ignite.internal.schema.Columns)

Aggregations

SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)105 Column (org.apache.ignite.internal.schema.Column)78 Test (org.junit.jupiter.api.Test)48 Tuple (org.apache.ignite.table.Tuple)35 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)34 Row (org.apache.ignite.internal.schema.row.Row)32 MethodSource (org.junit.jupiter.params.provider.MethodSource)30 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)22 TestObjectWithAllTypes (org.apache.ignite.internal.schema.testobjects.TestObjectWithAllTypes)11 DummySchemaManagerImpl (org.apache.ignite.internal.table.impl.DummySchemaManagerImpl)11 TupleMarshaller (org.apache.ignite.internal.schema.marshaller.TupleMarshaller)10 TupleMarshallerImpl (org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl)10 List (java.util.List)9 ArrayList (java.util.ArrayList)8 NotNull (org.jetbrains.annotations.NotNull)8 UUID (java.util.UUID)7 Collectors (java.util.stream.Collectors)7 TableImpl (org.apache.ignite.internal.table.TableImpl)7 Random (java.util.Random)6 Map (java.util.Map)5