Search in sources :

Example 1 with FieldKind

use of com.hazelcast.nio.serialization.FieldKind in project hazelcast by hazelcast.

the class Schema method init.

private void init() {
    List<FieldDescriptor> fixedSizeFields = new ArrayList<>();
    List<FieldDescriptor> booleanFields = new ArrayList<>();
    List<FieldDescriptor> variableSizeFields = new ArrayList<>();
    for (FieldDescriptor descriptor : fieldDefinitionMap.values()) {
        FieldKind fieldKind = descriptor.getKind();
        if (fieldOperations(fieldKind).kindSizeInBytes() == VARIABLE_SIZE) {
            variableSizeFields.add(descriptor);
        } else {
            if (FieldKind.BOOLEAN == fieldKind) {
                booleanFields.add(descriptor);
            } else {
                fixedSizeFields.add(descriptor);
            }
        }
    }
    fixedSizeFields.sort(Comparator.comparingInt(d -> fieldOperations(((FieldDescriptor) d).getKind()).kindSizeInBytes()).reversed());
    int offset = 0;
    for (FieldDescriptor descriptor : fixedSizeFields) {
        descriptor.setOffset(offset);
        offset += fieldOperations(descriptor.getKind()).kindSizeInBytes();
    }
    int bitOffset = 0;
    for (FieldDescriptor descriptor : booleanFields) {
        descriptor.setOffset(offset);
        descriptor.setBitOffset((byte) (bitOffset % Byte.SIZE));
        bitOffset++;
        if (bitOffset % Byte.SIZE == 0) {
            offset += 1;
        }
    }
    if (bitOffset % Byte.SIZE != 0) {
        offset++;
    }
    fixedSizeFieldsLength = offset;
    int index = 0;
    for (FieldDescriptor descriptor : variableSizeFields) {
        descriptor.setIndex(index++);
    }
    numberVarSizeFields = index;
    schemaId = RabinFingerprint.fingerprint64(this);
}
Also used : ArrayList(java.util.ArrayList) FieldKind(com.hazelcast.nio.serialization.FieldKind)

Example 2 with FieldKind

use of com.hazelcast.nio.serialization.FieldKind in project hazelcast by hazelcast.

the class SerializingGenericRecordCloner method build.

@Override
@Nonnull
public GenericRecord build() {
    try {
        for (FieldDescriptor field : schema.getFields()) {
            String fieldName = field.getFieldName();
            Writer writer = fields.get(fieldName);
            if (writer != null) {
                writer.write();
            } else {
                // Field is not overwritten. Write the field from the generic record.
                FieldKind fieldKind = field.getKind();
                fieldOperations(fieldKind).writeFieldFromRecordToWriter(cw, genericRecord, fieldName);
            }
        }
        cw.end();
        byte[] bytes = cw.toByteArray();
        Class associatedClass = genericRecord.getAssociatedClass();
        BufferObjectDataInput dataInput = bufferObjectDataInputFunc.apply(bytes);
        return new DefaultCompactReader(serializer, dataInput, schema, associatedClass, false);
    } catch (IOException e) {
        throw new HazelcastSerializationException(e);
    }
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) FieldKind(com.hazelcast.nio.serialization.FieldKind) IOException(java.io.IOException) BufferObjectDataInput(com.hazelcast.internal.nio.BufferObjectDataInput) Nonnull(javax.annotation.Nonnull)

Example 3 with FieldKind

use of com.hazelcast.nio.serialization.FieldKind in project hazelcast by hazelcast.

the class DeserializedGenericRecord method getArrayOfInt32.

@Override
@Nullable
public int[] getArrayOfInt32(@Nonnull String fieldName) {
    FieldKind fieldKind = check(fieldName, ARRAY_OF_INT32, ARRAY_OF_NULLABLE_INT32);
    if (fieldKind == ARRAY_OF_NULLABLE_INT32) {
        Integer[] array = (Integer[]) objects.get(fieldName);
        int[] result = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            if (array[i] == null) {
                throw exceptionForUnexpectedNullValueInArray(fieldName, "Int32");
            }
            result[i] = array[i];
        }
        return result;
    }
    return (int[]) objects.get(fieldName);
}
Also used : FieldKind(com.hazelcast.nio.serialization.FieldKind) Nullable(javax.annotation.Nullable)

Example 4 with FieldKind

use of com.hazelcast.nio.serialization.FieldKind in project hazelcast by hazelcast.

the class DeserializedGenericRecord method getArrayOfFloat64.

@Override
@Nullable
public double[] getArrayOfFloat64(@Nonnull String fieldName) {
    FieldKind fieldKind = check(fieldName, ARRAY_OF_FLOAT64, ARRAY_OF_NULLABLE_FLOAT64);
    if (fieldKind == ARRAY_OF_NULLABLE_FLOAT64) {
        Double[] array = (Double[]) objects.get(fieldName);
        double[] result = new double[array.length];
        for (int i = 0; i < array.length; i++) {
            if (array[i] == null) {
                throw exceptionForUnexpectedNullValueInArray(fieldName, "Float64");
            }
            result[i] = array[i];
        }
        return result;
    }
    return (double[]) objects.get(fieldName);
}
Also used : FieldKind(com.hazelcast.nio.serialization.FieldKind) Nullable(javax.annotation.Nullable)

Example 5 with FieldKind

use of com.hazelcast.nio.serialization.FieldKind in project hazelcast by hazelcast.

the class DeserializedGenericRecord method getArrayOfNullableInt32.

@Nullable
@Override
public Integer[] getArrayOfNullableInt32(@Nonnull String fieldName) {
    FieldKind fieldKind = check(fieldName, ARRAY_OF_INT32, ARRAY_OF_NULLABLE_INT32);
    if (fieldKind == ARRAY_OF_INT32) {
        int[] array = (int[]) objects.get(fieldName);
        Integer[] result = new Integer[array.length];
        Arrays.setAll(result, i -> array[i]);
        return result;
    }
    return (Integer[]) objects.get(fieldName);
}
Also used : FieldKind(com.hazelcast.nio.serialization.FieldKind) Nullable(javax.annotation.Nullable)

Aggregations

FieldKind (com.hazelcast.nio.serialization.FieldKind)40 Nullable (javax.annotation.Nullable)24 BufferObjectDataInput (com.hazelcast.internal.nio.BufferObjectDataInput)17 ObjectDataInput (com.hazelcast.nio.ObjectDataInput)16 IOException (java.io.IOException)14 Nonnull (javax.annotation.Nonnull)3 HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)2 QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)2 DeserializedSchemaBoundGenericRecordBuilder (com.hazelcast.internal.serialization.impl.compact.DeserializedSchemaBoundGenericRecordBuilder)1 Schema (com.hazelcast.internal.serialization.impl.compact.Schema)1 FieldTypeToFieldKind (com.hazelcast.internal.serialization.impl.portable.FieldTypeToFieldKind)1 FAILING_TOP_LEVEL_INJECTOR (com.hazelcast.jet.sql.impl.inject.UpsertInjector.FAILING_TOP_LEVEL_INJECTOR)1 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)1 GenericRecordBuilder (com.hazelcast.nio.serialization.GenericRecordBuilder)1 QueryException (com.hazelcast.sql.impl.QueryException)1 BigDecimal (java.math.BigDecimal)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 OffsetDateTime (java.time.OffsetDateTime)1