Search in sources :

Example 6 with NativeType

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

the class UpgradingRowAdapterTest method serializeValuesToRow.

/**
 * Validates row values after serialization-then-deserialization.
 *
 * @param schema Row schema.
 * @param vals   Row values.
 * @return Row bytes.
 */
private byte[] serializeValuesToRow(SchemaDescriptor schema, List<Object> vals) {
    assertEquals(schema.keyColumns().length() + schema.valueColumns().length(), vals.size());
    int nonNullVarLenKeyCols = 0;
    int nonNullVarLenValCols = 0;
    int nonNullVarLenKeySize = 0;
    int nonNullVarLenValSize = 0;
    for (int i = 0; i < vals.size(); i++) {
        NativeTypeSpec type = schema.column(i).type().spec();
        if (vals.get(i) != null && !type.fixedLength()) {
            if (type == NativeTypeSpec.BYTES) {
                byte[] val = (byte[]) vals.get(i);
                if (schema.isKeyColumn(i)) {
                    nonNullVarLenKeyCols++;
                    nonNullVarLenKeySize += val.length;
                } else {
                    nonNullVarLenValCols++;
                    nonNullVarLenValSize += val.length;
                }
            } else if (type == NativeTypeSpec.STRING) {
                if (schema.isKeyColumn(i)) {
                    nonNullVarLenKeyCols++;
                    nonNullVarLenKeySize += RowAssembler.utf8EncodedLength((CharSequence) vals.get(i));
                } else {
                    nonNullVarLenValCols++;
                    nonNullVarLenValSize += RowAssembler.utf8EncodedLength((CharSequence) vals.get(i));
                }
            } else if (type == NativeTypeSpec.NUMBER) {
                if (schema.isKeyColumn(i)) {
                    nonNullVarLenKeyCols++;
                    nonNullVarLenKeySize += RowAssembler.sizeInBytes((BigInteger) vals.get(i));
                } else {
                    nonNullVarLenValCols++;
                    nonNullVarLenValSize += RowAssembler.sizeInBytes((BigInteger) vals.get(i));
                }
            } else if (type == NativeTypeSpec.DECIMAL) {
                if (schema.isKeyColumn(i)) {
                    nonNullVarLenKeyCols++;
                    nonNullVarLenKeySize += RowAssembler.sizeInBytes((BigDecimal) vals.get(i));
                } else {
                    nonNullVarLenValCols++;
                    nonNullVarLenValSize += RowAssembler.sizeInBytes((BigDecimal) vals.get(i));
                }
            } else {
                throw new IllegalStateException("Unsupported variable-length type: " + type);
            }
        }
    }
    RowAssembler asm = new RowAssembler(schema, nonNullVarLenKeySize, nonNullVarLenKeyCols, nonNullVarLenValSize, nonNullVarLenValCols);
    for (int i = 0; i < vals.size(); i++) {
        if (vals.get(i) == null) {
            asm.appendNull();
        } else {
            NativeType type = schema.column(i).type();
            switch(type.spec()) {
                case INT8:
                    asm.appendByte((Byte) vals.get(i));
                    break;
                case INT16:
                    asm.appendShort((Short) vals.get(i));
                    break;
                case INT32:
                    asm.appendInt((Integer) vals.get(i));
                    break;
                case INT64:
                    asm.appendLong((Long) vals.get(i));
                    break;
                case FLOAT:
                    asm.appendFloat((Float) vals.get(i));
                    break;
                case DOUBLE:
                    asm.appendDouble((Double) vals.get(i));
                    break;
                case UUID:
                    asm.appendUuid((java.util.UUID) vals.get(i));
                    break;
                case STRING:
                    asm.appendString((String) vals.get(i));
                    break;
                case NUMBER:
                    asm.appendNumber((BigInteger) vals.get(i));
                    break;
                case DECIMAL:
                    asm.appendDecimal((BigDecimal) vals.get(i));
                    break;
                case BYTES:
                    asm.appendBytes((byte[]) vals.get(i));
                    break;
                case BITMASK:
                    asm.appendBitmask((BitSet) vals.get(i));
                    break;
                case DATE:
                    asm.appendDate((LocalDate) vals.get(i));
                    break;
                case TIME:
                    asm.appendTime((LocalTime) vals.get(i));
                    break;
                case DATETIME:
                    asm.appendDateTime((LocalDateTime) vals.get(i));
                    break;
                case TIMESTAMP:
                    asm.appendTimestamp((Instant) vals.get(i));
                    break;
                default:
                    throw new IllegalStateException("Unsupported test type: " + type);
            }
        }
    }
    return asm.toBytes();
}
Also used : NativeTypeSpec(org.apache.ignite.internal.schema.NativeTypeSpec) BigInteger(java.math.BigInteger) RowAssembler(org.apache.ignite.internal.schema.row.RowAssembler) NativeType(org.apache.ignite.internal.schema.NativeType)

Example 7 with NativeType

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

the class UpgradingRowAdapterTest method generateRowValues.

/**
 * Generate row values for given row schema.
 *
 * @param schema Row schema.
 * @return Row values.
 */
private List<Object> generateRowValues(SchemaDescriptor schema) {
    ArrayList<Object> res = new ArrayList<>(schema.length());
    for (int i = 0; i < schema.length(); i++) {
        NativeType type = schema.column(i).type();
        res.add(SchemaTestUtils.generateRandomValue(rnd, type));
    }
    return res;
}
Also used : ArrayList(java.util.ArrayList) NativeType(org.apache.ignite.internal.schema.NativeType)

Aggregations

NativeType (org.apache.ignite.internal.schema.NativeType)7 Column (org.apache.ignite.internal.schema.Column)3 BigInteger (java.math.BigInteger)2 Tuple (org.apache.ignite.table.Tuple)2 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 Random (java.util.Random)1 UUID (java.util.UUID)1 BitmaskNativeType (org.apache.ignite.internal.schema.BitmaskNativeType)1 DecimalNativeType (org.apache.ignite.internal.schema.DecimalNativeType)1 NativeTypeSpec (org.apache.ignite.internal.schema.NativeTypeSpec)1 NumberNativeType (org.apache.ignite.internal.schema.NumberNativeType)1 TemporalNativeType (org.apache.ignite.internal.schema.TemporalNativeType)1 VarlenNativeType (org.apache.ignite.internal.schema.VarlenNativeType)1 RowAssembler (org.apache.ignite.internal.schema.row.RowAssembler)1 Test (org.junit.jupiter.api.Test)1