Search in sources :

Example 1 with Columns

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

the class TupleMarshallerImpl method toInternalTuple.

/**
 * Analyze tuple and wrap into internal tuple.
 *
 * @param schema  Schema.
 * @param tuple   Key or value tuple.
 * @param keyFlag If {@code true} marshal key columns, otherwise marshall value columns.
 * @return Internal tuple.
 * @throws SchemaMismatchException If tuple doesn't match the schema.
 */
@NotNull
private InternalTuple toInternalTuple(SchemaDescriptor schema, Tuple tuple, boolean keyFlag) throws SchemaMismatchException {
    if (tuple == null) {
        return InternalTuple.NO_VALUE;
    }
    Columns columns = keyFlag ? schema.keyColumns() : schema.valueColumns();
    int nonNullVarlen = 0;
    int nonNullVarlenSize = 0;
    int knownColumns = 0;
    Map<String, Object> defaults = new HashMap<>();
    if (tuple instanceof SchemaAware && Objects.equals(((SchemaAware) tuple).schema(), schema)) {
        for (int i = 0, len = columns.length(); i < len; i++) {
            final Column col = columns.column(i);
            Object val = tuple.valueOrDefault(col.name(), POISON_OBJECT);
            assert val != POISON_OBJECT;
            if (val == null || columns.firstVarlengthColumn() < i) {
                continue;
            }
            nonNullVarlenSize += getValueSize(val, col.type());
            nonNullVarlen++;
        }
    } else {
        for (int i = 0, len = columns.length(); i < len; i++) {
            final Column col = columns.column(i);
            Object val = tuple.valueOrDefault(col.name(), POISON_OBJECT);
            if (val == POISON_OBJECT) {
                if (keyFlag) {
                    throw new SchemaMismatchException("Missed key column: " + col.name());
                }
                val = col.defaultValue();
                defaults.put(col.name(), val);
            } else {
                knownColumns++;
            }
            col.validate(val);
            if (val == null || columns.isFixedSize(i)) {
                continue;
            }
            nonNullVarlenSize += getValueSize(val, col.type());
            nonNullVarlen++;
        }
    }
    return new InternalTuple(tuple, nonNullVarlen, nonNullVarlenSize, defaults, knownColumns);
}
Also used : SchemaAware(org.apache.ignite.internal.schema.SchemaAware) SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException) HashMap(java.util.HashMap) Column(org.apache.ignite.internal.schema.Column) Columns(org.apache.ignite.internal.schema.Columns) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Columns

use of org.apache.ignite.internal.schema.Columns 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)

Example 3 with Columns

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

the class TupleMarshallerFixlenOnlyBenchmark method measureTupleBuildAndMarshallerCost.

/**
 * Measure tuple build then marshall.
 *
 * @param bh Black hole.
 */
@Benchmark
public void measureTupleBuildAndMarshallerCost(Blackhole bh) throws TupleMarshallerException {
    final Columns cols = schema.valueColumns();
    final Tuple valBld = Tuple.create(cols.length());
    for (int i = 0; i < cols.length(); i++) {
        valBld.set(cols.column(i).name(), vals[i]);
    }
    Tuple keyTuple = Tuple.create(1).set("key", rnd.nextLong());
    final Row row = marshaller.marshal(keyTuple, valBld);
    bh.consume(row);
}
Also used : Columns(org.apache.ignite.internal.schema.Columns) Row(org.apache.ignite.internal.schema.row.Row) Tuple(org.apache.ignite.table.Tuple) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 4 with Columns

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

the class TupleMarshallerVarlenOnlyBenchmark method measureTupleBuildAndMarshallerCost.

/**
 * Measure tuple build then marshall.
 *
 * @param bh Black hole.
 */
@Benchmark
public void measureTupleBuildAndMarshallerCost(Blackhole bh) throws TupleMarshallerException {
    final Columns cols = schema.valueColumns();
    final Tuple valBld = Tuple.create(cols.length());
    for (int i = 0; i < cols.length(); i++) {
        valBld.set(cols.column(i).name(), val);
    }
    Tuple keyTuple = Tuple.create(1).set("key", rnd.nextLong());
    final Row row = marshaller.marshal(keyTuple, valBld);
    bh.consume(row);
}
Also used : Columns(org.apache.ignite.internal.schema.Columns) Row(org.apache.ignite.internal.schema.row.Row) Tuple(org.apache.ignite.table.Tuple) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 5 with Columns

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

the class TupleMarshallerImpl method buildRow.

/**
 * Marshal tuple to a row.
 *
 * @param schema    Schema.
 * @param keyTuple0 Internal key tuple.
 * @param valTuple0 Internal value tuple.
 * @return Row.
 * @throws SchemaMismatchException If failed to write tuple column.
 */
@NotNull
private Row buildRow(SchemaDescriptor schema, InternalTuple keyTuple0, InternalTuple valTuple0) throws SchemaMismatchException {
    RowAssembler rowBuilder = createAssembler(schema, keyTuple0, valTuple0);
    Columns columns = schema.keyColumns();
    for (int i = 0, len = columns.length(); i < len; i++) {
        final Column col = columns.column(i);
        writeColumn(rowBuilder, col, keyTuple0);
    }
    if (valTuple0.tuple != null) {
        columns = schema.valueColumns();
        for (int i = 0, len = columns.length(); i < len; i++) {
            final Column col = columns.column(i);
            writeColumn(rowBuilder, col, valTuple0);
        }
    }
    return new Row(schema, rowBuilder.build());
}
Also used : Column(org.apache.ignite.internal.schema.Column) RowAssembler(org.apache.ignite.internal.schema.row.RowAssembler) Columns(org.apache.ignite.internal.schema.Columns) Row(org.apache.ignite.internal.schema.row.Row) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Columns (org.apache.ignite.internal.schema.Columns)8 Column (org.apache.ignite.internal.schema.Column)4 Row (org.apache.ignite.internal.schema.row.Row)4 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)3 RowAssembler (org.apache.ignite.internal.schema.row.RowAssembler)3 SchemaMismatchException (org.apache.ignite.internal.schema.SchemaMismatchException)2 Tuple (org.apache.ignite.table.Tuple)2 NotNull (org.jetbrains.annotations.NotNull)2 Benchmark (org.openjdk.jmh.annotations.Benchmark)2 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)1 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)1 Scope (com.facebook.presto.bytecode.Scope)1 Variable (com.facebook.presto.bytecode.Variable)1 IfStatement (com.facebook.presto.bytecode.control.IfStatement)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Function (java.util.function.Function)1 InvalidTypeException (org.apache.ignite.internal.schema.InvalidTypeException)1 NativeTypeSpec (org.apache.ignite.internal.schema.NativeTypeSpec)1 SchemaAware (org.apache.ignite.internal.schema.SchemaAware)1