Search in sources :

Example 1 with CompactReader

use of com.hazelcast.nio.serialization.compact.CompactReader in project hazelcast by hazelcast.

the class JavaRecordSerializer method populateReadersWriters.

private void populateReadersWriters(Class<?> clazz) {
    try {
        Object[] recordComponents = (Object[]) getRecordComponentsMethod.invoke(clazz);
        Class<?>[] componentTypes = new Class<?>[recordComponents.length];
        ComponentReader[] componentReaders = new ComponentReader[recordComponents.length];
        ComponentWriter[] componentWriters = new ComponentWriter[recordComponents.length];
        for (int i = 0; i < recordComponents.length; i++) {
            Object recordComponent = recordComponents[i];
            Class<?> type = (Class<?>) getTypeMethod.invoke(recordComponent);
            String name = (String) getNameMethod.invoke(recordComponent);
            componentTypes[i] = type;
            Method componentGetter = clazz.getDeclaredMethod(name);
            componentGetter.setAccessible(true);
            // frictionless for reflectively serialized record objects.
            if (Byte.TYPE.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.INT8, FieldKind.NULLABLE_INT8)) {
                        return (byte) 0;
                    }
                    return compactReader.readInt8(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeInt8(name, (byte) componentGetter.invoke(object));
            } else if (Byte.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.NULLABLE_INT8, FieldKind.INT8)) {
                        return null;
                    }
                    return compactReader.readNullableInt8(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeNullableInt8(name, (Byte) componentGetter.invoke(object));
            } else if (Character.TYPE.equals(type)) {
                throwUnsupportedFieldTypeException("char");
            } else if (Character.class.equals(type)) {
                throwUnsupportedFieldTypeException("Character");
            } else if (Short.TYPE.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.INT16, FieldKind.NULLABLE_INT16)) {
                        return (short) 0;
                    }
                    return compactReader.readInt16(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeInt16(name, (short) componentGetter.invoke(object));
            } else if (Short.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.NULLABLE_INT16, FieldKind.INT16)) {
                        return null;
                    }
                    return compactReader.readNullableInt16(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeNullableInt16(name, (Short) componentGetter.invoke(object));
            } else if (Integer.TYPE.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.INT32, FieldKind.NULLABLE_INT32)) {
                        return 0;
                    }
                    return compactReader.readInt32(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeInt32(name, (int) componentGetter.invoke(object));
            } else if (Integer.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.NULLABLE_INT32, FieldKind.INT32)) {
                        return null;
                    }
                    return compactReader.readNullableInt32(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeNullableInt32(name, (Integer) componentGetter.invoke(object));
            } else if (Long.TYPE.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.INT64, FieldKind.NULLABLE_INT64)) {
                        return 0L;
                    }
                    return compactReader.readInt64(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeInt64(name, (long) componentGetter.invoke(object));
            } else if (Long.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.NULLABLE_INT64, FieldKind.INT64)) {
                        return null;
                    }
                    return compactReader.readNullableInt64(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeNullableInt64(name, (Long) componentGetter.invoke(object));
            } else if (Float.TYPE.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.FLOAT32, FieldKind.NULLABLE_FLOAT32)) {
                        return 0F;
                    }
                    return compactReader.readFloat32(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeFloat32(name, (float) componentGetter.invoke(object));
            } else if (Float.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.NULLABLE_FLOAT32, FieldKind.FLOAT32)) {
                        return null;
                    }
                    return compactReader.readNullableFloat32(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeNullableFloat32(name, (Float) componentGetter.invoke(object));
            } else if (Double.TYPE.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.FLOAT64, FieldKind.NULLABLE_FLOAT64)) {
                        return 0D;
                    }
                    return compactReader.readFloat64(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeFloat64(name, (double) componentGetter.invoke(object));
            } else if (Double.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.NULLABLE_FLOAT64, FieldKind.FLOAT64)) {
                        return null;
                    }
                    return compactReader.readNullableFloat64(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeNullableFloat64(name, (Double) componentGetter.invoke(object));
            } else if (Boolean.TYPE.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.BOOLEAN, FieldKind.NULLABLE_BOOLEAN)) {
                        return false;
                    }
                    return compactReader.readBoolean(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeBoolean(name, (boolean) componentGetter.invoke(object));
            } else if (Boolean.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.NULLABLE_BOOLEAN, FieldKind.BOOLEAN)) {
                        return null;
                    }
                    return compactReader.readNullableBoolean(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeNullableBoolean(name, (Boolean) componentGetter.invoke(object));
            } else if (String.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.STRING)) {
                        return null;
                    }
                    return compactReader.readString(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeString(name, (String) componentGetter.invoke(object));
            } else if (BigDecimal.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.DECIMAL)) {
                        return null;
                    }
                    return compactReader.readDecimal(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeDecimal(name, (BigDecimal) componentGetter.invoke(object));
            } else if (LocalTime.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.TIME)) {
                        return null;
                    }
                    return compactReader.readTime(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeTime(name, (LocalTime) componentGetter.invoke(object));
            } else if (LocalDate.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.DATE)) {
                        return null;
                    }
                    return compactReader.readDate(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeDate(name, (LocalDate) componentGetter.invoke(object));
            } else if (LocalDateTime.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.TIMESTAMP)) {
                        return null;
                    }
                    return compactReader.readTimestamp(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeTimestamp(name, (LocalDateTime) componentGetter.invoke(object));
            } else if (OffsetDateTime.class.equals(type)) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.TIMESTAMP_WITH_TIMEZONE)) {
                        return null;
                    }
                    return compactReader.readTimestampWithTimezone(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeTimestampWithTimezone(name, (OffsetDateTime) componentGetter.invoke(object));
            } else if (type.isEnum()) {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.STRING)) {
                        return null;
                    }
                    String enumName = compactReader.readString(name, null);
                    return enumName == null ? null : Enum.valueOf((Class<? extends Enum>) type, enumName);
                };
                componentWriters[i] = (compactWriter, object) -> {
                    Object rawValue = componentGetter.invoke(object);
                    String value = rawValue == null ? null : ((Enum) rawValue).name();
                    compactWriter.writeString(name, value);
                };
            } else if (type.isArray()) {
                Class<?> componentType = type.getComponentType();
                if (Byte.TYPE.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_INT8, FieldKind.ARRAY_OF_NULLABLE_INT8)) {
                            return null;
                        }
                        return compactReader.readArrayOfInt8(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfInt8(name, (byte[]) componentGetter.invoke(object));
                } else if (Byte.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_NULLABLE_INT8, FieldKind.ARRAY_OF_INT8)) {
                            return null;
                        }
                        return compactReader.readArrayOfNullableInt8(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfNullableInt8(name, (Byte[]) componentGetter.invoke(object));
                } else if (Character.TYPE.equals(componentType)) {
                    throwUnsupportedFieldTypeException("char[]");
                } else if (Character.class.equals(componentType)) {
                    throwUnsupportedFieldTypeException("Character[]");
                } else if (Short.TYPE.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_INT16, FieldKind.ARRAY_OF_NULLABLE_INT16)) {
                            return null;
                        }
                        return compactReader.readArrayOfInt16(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfInt16(name, (short[]) componentGetter.invoke(object));
                } else if (Short.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_NULLABLE_INT16, FieldKind.ARRAY_OF_INT16)) {
                            return null;
                        }
                        return compactReader.readArrayOfNullableInt16(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfNullableInt16(name, (Short[]) componentGetter.invoke(object));
                } else if (Integer.TYPE.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_INT32, FieldKind.ARRAY_OF_NULLABLE_INT32)) {
                            return null;
                        }
                        return compactReader.readArrayOfInt32(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfInt32(name, (int[]) componentGetter.invoke(object));
                } else if (Integer.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_NULLABLE_INT32, FieldKind.ARRAY_OF_INT32)) {
                            return null;
                        }
                        return compactReader.readArrayOfNullableInt32(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfNullableInt32(name, (Integer[]) componentGetter.invoke(object));
                } else if (Long.TYPE.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_INT64, FieldKind.ARRAY_OF_NULLABLE_INT64)) {
                            return null;
                        }
                        return compactReader.readArrayOfInt64(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfInt64(name, (long[]) componentGetter.invoke(object));
                } else if (Long.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_NULLABLE_INT64, FieldKind.ARRAY_OF_INT64)) {
                            return null;
                        }
                        return compactReader.readArrayOfNullableInt64(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfNullableInt64(name, (Long[]) componentGetter.invoke(object));
                } else if (Float.TYPE.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_FLOAT32, FieldKind.ARRAY_OF_NULLABLE_FLOAT32)) {
                            return null;
                        }
                        return compactReader.readArrayOfFloat32(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfFloat32(name, (float[]) componentGetter.invoke(object));
                } else if (Float.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_NULLABLE_FLOAT32, FieldKind.ARRAY_OF_FLOAT32)) {
                            return null;
                        }
                        return compactReader.readArrayOfNullableFloat32(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfNullableFloat32(name, (Float[]) componentGetter.invoke(object));
                } else if (Double.TYPE.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_FLOAT64, FieldKind.ARRAY_OF_NULLABLE_FLOAT64)) {
                            return null;
                        }
                        return compactReader.readArrayOfFloat64(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfFloat64(name, (double[]) componentGetter.invoke(object));
                } else if (Double.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_NULLABLE_FLOAT64, FieldKind.ARRAY_OF_FLOAT64)) {
                            return null;
                        }
                        return compactReader.readArrayOfNullableFloat64(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfNullableFloat64(name, (Double[]) componentGetter.invoke(object));
                } else if (Boolean.TYPE.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_BOOLEAN, FieldKind.ARRAY_OF_NULLABLE_BOOLEAN)) {
                            return null;
                        }
                        return compactReader.readArrayOfBoolean(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfBoolean(name, (boolean[]) componentGetter.invoke(object));
                } else if (Boolean.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_NULLABLE_BOOLEAN, FieldKind.ARRAY_OF_BOOLEAN)) {
                            return null;
                        }
                        return compactReader.readArrayOfNullableBoolean(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfNullableBoolean(name, (Boolean[]) componentGetter.invoke(object));
                } else if (String.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_STRING)) {
                            return null;
                        }
                        return compactReader.readArrayOfString(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfString(name, (String[]) componentGetter.invoke(object));
                } else if (BigDecimal.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_DECIMAL)) {
                            return null;
                        }
                        return compactReader.readArrayOfDecimal(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfDecimal(name, (BigDecimal[]) componentGetter.invoke(object));
                } else if (LocalTime.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_TIME)) {
                            return null;
                        }
                        return compactReader.readArrayOfTime(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfTime(name, (LocalTime[]) componentGetter.invoke(object));
                } else if (LocalDate.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_DATE)) {
                            return null;
                        }
                        return compactReader.readArrayOfDate(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfDate(name, (LocalDate[]) componentGetter.invoke(object));
                } else if (LocalDateTime.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_TIMESTAMP)) {
                            return null;
                        }
                        return compactReader.readArrayOfTimestamp(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfTimestamp(name, (LocalDateTime[]) componentGetter.invoke(object));
                } else if (OffsetDateTime.class.equals(componentType)) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_TIMESTAMP_WITH_TIMEZONE)) {
                            return null;
                        }
                        return compactReader.readArrayOfTimestampWithTimezone(name);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfTimestampWithTimezone(name, (OffsetDateTime[]) componentGetter.invoke(object));
                } else if (componentType.isEnum()) {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_STRING)) {
                            return null;
                        }
                        String[] enumNames = compactReader.readArrayOfString(name);
                        if (enumNames == null) {
                            return null;
                        }
                        Class<? extends Enum> enumType = (Class<? extends Enum>) componentType;
                        Enum[] enums = (Enum[]) Array.newInstance(enumType, enumNames.length);
                        for (int j = 0; j < enumNames.length; j++) {
                            String enumName = enumNames[j];
                            enums[j] = enumName == null ? null : Enum.valueOf(enumType, enumName);
                        }
                        return enums;
                    };
                    componentWriters[i] = (compactWriter, object) -> {
                        Enum[] enums = (Enum[]) componentGetter.invoke(object);
                        String[] enumNames = null;
                        if (enums != null) {
                            enumNames = new String[enums.length];
                            for (int j = 0; j < enums.length; j++) {
                                Enum e = enums[j];
                                enumNames[j] = e == null ? null : e.name();
                            }
                        }
                        compactWriter.writeArrayOfString(name, enumNames);
                    };
                } else {
                    componentReaders[i] = (compactReader, schema) -> {
                        if (!isFieldExist(schema, name, FieldKind.ARRAY_OF_COMPACT)) {
                            return null;
                        }
                        return compactReader.readArrayOfCompact(name, componentType);
                    };
                    componentWriters[i] = (compactWriter, object) -> compactWriter.writeArrayOfCompact(name, (Object[]) componentGetter.invoke(object));
                }
            } else {
                componentReaders[i] = (compactReader, schema) -> {
                    if (!isFieldExist(schema, name, FieldKind.COMPACT)) {
                        return null;
                    }
                    return compactReader.readCompact(name);
                };
                componentWriters[i] = (compactWriter, object) -> compactWriter.writeCompact(name, componentGetter.invoke(object));
            }
        }
        Constructor<?> constructor = clazz.getDeclaredConstructor(componentTypes);
        constructor.setAccessible(true);
        JavaRecordReader recordReader = new JavaRecordReader(constructor, componentReaders);
        readersCache.put(clazz, recordReader);
        writersCache.put(clazz, componentWriters);
    } catch (Exception e) {
        throw new HazelcastSerializationException("Failed to construct the readers/writers for the Java record", e);
    }
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) CompactReader(com.hazelcast.nio.serialization.compact.CompactReader) Array(java.lang.reflect.Array) DefaultCompactReader(com.hazelcast.internal.serialization.impl.compact.DefaultCompactReader) LocalDateTime(java.time.LocalDateTime) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Constructor(java.lang.reflect.Constructor) Schema(com.hazelcast.internal.serialization.impl.compact.Schema) CompactWriter(com.hazelcast.nio.serialization.compact.CompactWriter) BigDecimal(java.math.BigDecimal) FieldKind(com.hazelcast.nio.serialization.FieldKind) OffsetDateTime(java.time.OffsetDateTime) LocalDate(java.time.LocalDate) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) LocalTime(java.time.LocalTime) CompactSerializer(com.hazelcast.nio.serialization.compact.CompactSerializer) Nonnull(javax.annotation.Nonnull) Method(java.lang.reflect.Method) FieldDescriptor(com.hazelcast.internal.serialization.impl.compact.FieldDescriptor) LocalDateTime(java.time.LocalDateTime) LocalDate(java.time.LocalDate) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) LocalTime(java.time.LocalTime) Method(java.lang.reflect.Method) BigDecimal(java.math.BigDecimal) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) OffsetDateTime(java.time.OffsetDateTime)

Example 2 with CompactReader

use of com.hazelcast.nio.serialization.compact.CompactReader in project hazelcast by hazelcast.

the class ReflectiveCompactSerializer method readFast.

private boolean readFast(Class clazz, DefaultCompactReader compactReader, Object object) {
    Reader[] readers = readersCache.get(clazz);
    Schema schema = compactReader.getSchema();
    if (readers == null) {
        return false;
    }
    for (Reader reader : readers) {
        try {
            reader.read(compactReader, schema, object);
        } catch (Exception e) {
            throw new HazelcastSerializationException(e);
        }
    }
    return true;
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) CompactReader(com.hazelcast.nio.serialization.compact.CompactReader) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException)

Example 3 with CompactReader

use of com.hazelcast.nio.serialization.compact.CompactReader in project hazelcast by hazelcast.

the class SqlCompactTest method beforeClass.

@BeforeClass
public static void beforeClass() {
    Config config = new Config();
    config.getJetConfig().setEnabled(true);
    CompactSerializationConfig compactSerializationConfig = config.getSerializationConfig().getCompactSerializationConfig();
    compactSerializationConfig.setEnabled(true);
    // registering this class to the member to see it does not affect any of the tests.
    // It has a different schema than all the tests
    compactSerializationConfig.register(Person.class, PERSON_TYPE_NAME, new CompactSerializer<Person>() {

        @Nonnull
        @Override
        public Person read(@Nonnull CompactReader in) {
            Person person = new Person();
            person.surname = in.readString("surname", "NotAssigned");
            return person;
        }

        @Override
        public void write(@Nonnull CompactWriter out, @Nonnull Person person) {
            out.writeString("surname", person.surname);
        }
    });
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.getSerializationConfig().getCompactSerializationConfig().setEnabled(true);
    initializeWithClient(1, config, clientConfig);
    sqlService = instance().getSql();
    clientSqlService = client().getSql();
    serializationService = Util.getSerializationService(instance());
}
Also used : CompactWriter(com.hazelcast.nio.serialization.compact.CompactWriter) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) CompactReader(com.hazelcast.nio.serialization.compact.CompactReader) Nonnull(javax.annotation.Nonnull) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) BeforeClass(org.junit.BeforeClass)

Example 4 with CompactReader

use of com.hazelcast.nio.serialization.compact.CompactReader in project hazelcast by hazelcast.

the class CompactStreamSerializerTest method testWithExplicitSerializer_nested.

@Test
public void testWithExplicitSerializer_nested() {
    SerializationConfig serializationConfig = new SerializationConfig();
    CompactSerializationConfig compactSerializationConfig = serializationConfig.getCompactSerializationConfig();
    compactSerializationConfig.setEnabled(true);
    compactSerializationConfig.register(EmployeeDTO.class, "employee", new CompactSerializer<EmployeeDTO>() {

        @Nonnull
        @Override
        public EmployeeDTO read(@Nonnull CompactReader in) {
            return new EmployeeDTO(in.readInt32("a"), in.readInt64("i"));
        }

        @Override
        public void write(@Nonnull CompactWriter out, @Nonnull EmployeeDTO object) {
            out.writeInt32("a", object.getAge());
            out.writeInt64("i", object.getId());
        }
    });
    compactSerializationConfig.register(EmployerDTO.class, "employer", new CompactSerializer<EmployerDTO>() {

        @Nonnull
        @Override
        public EmployerDTO read(@Nonnull CompactReader in) {
            String name = in.readString("n");
            String status = in.readString("hs");
            int age = in.readInt32("a");
            long[] ids = in.readArrayOfInt64("ids");
            EmployeeDTO s = in.readCompact("s");
            EmployeeDTO[] ss = in.readArrayOfCompact("ss", EmployeeDTO.class);
            return new EmployerDTO(name, age, status == null ? null : HiringStatus.valueOf(status), ids, s, ss);
        }

        @Override
        public void write(@Nonnull CompactWriter out, @Nonnull EmployerDTO object) {
            out.writeString("n", object.getName());
            out.writeString("hs", object.getHiringStatus() == null ? null : object.getHiringStatus().name());
            out.writeInt32("a", object.getZcode());
            out.writeArrayOfInt64("ids", object.getIds());
            out.writeCompact("s", object.getSingleEmployee());
            out.writeArrayOfCompact("ss", object.getOtherEmployees());
        }
    });
    SerializationService serializationService = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).setSchemaService(schemaService).build();
    EmployeeDTO employeeDTO = new EmployeeDTO(30, 102310312);
    long[] ids = new long[2];
    ids[0] = 22;
    ids[1] = 44;
    EmployeeDTO[] employeeDTOS = new EmployeeDTO[5];
    for (int j = 0; j < employeeDTOS.length; j++) {
        employeeDTOS[j] = new EmployeeDTO(20 + j, j * 100);
    }
    EmployerDTO employerDTO = new EmployerDTO("nbss", 40, HIRING, ids, employeeDTO, employeeDTOS);
    Data data = serializationService.toData(employerDTO);
    Object object = serializationService.toObject(data);
    EmployerDTO o = (EmployerDTO) object;
    assertEquals(employerDTO, o);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) CompactWriter(com.hazelcast.nio.serialization.compact.CompactWriter) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) Nonnull(javax.annotation.Nonnull) SerializationConfig(com.hazelcast.config.SerializationConfig) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) EmployerDTO(example.serialization.EmployerDTO) EmployeeDTO(example.serialization.EmployeeDTO) ExternalizableEmployeeDTO(example.serialization.ExternalizableEmployeeDTO) CompactReader(com.hazelcast.nio.serialization.compact.CompactReader) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with CompactReader

use of com.hazelcast.nio.serialization.compact.CompactReader in project hazelcast by hazelcast.

the class CompactStreamSerializerTest method testSchemaEvolution_fieldAdded.

@Test
public void testSchemaEvolution_fieldAdded() {
    SerializationConfig serializationConfig = new SerializationConfig();
    // Using this registration to mimic schema evolution. This is usage is not advised.
    serializationConfig.getCompactSerializationConfig().setEnabled(true).register(EmployeeDTO.class, EmployeeDTO.class.getName(), new CompactSerializer<EmployeeDTO>() {

        @Nonnull
        @Override
        public EmployeeDTO read(@Nonnull CompactReader in) {
            throw new UnsupportedOperationException("We will not read from here on this test");
        }

        @Override
        public void write(@Nonnull CompactWriter out, @Nonnull EmployeeDTO object) {
            out.writeInt32("age", object.getAge());
            out.writeInt64("id", object.getId());
            out.writeString("surname", "sir");
        }
    });
    SerializationService serializationService = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).setSchemaService(schemaService).build();
    EmployeeDTO expected = new EmployeeDTO(20, 102310312);
    Data data = serializationService.toData(expected);
    SerializationConfig serializationConfig2 = new SerializationConfig();
    serializationConfig2.getCompactSerializationConfig().setEnabled(true);
    SerializationService serializationService2 = new DefaultSerializationServiceBuilder().setSchemaService(schemaService).setConfig(serializationConfig2).build();
    EmployeeDTO actual = serializationService2.toObject(data);
    assertEquals(expected.getAge(), actual.getAge());
    assertEquals(expected.getId(), actual.getId());
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) CompactWriter(com.hazelcast.nio.serialization.compact.CompactWriter) EmployeeDTO(example.serialization.EmployeeDTO) ExternalizableEmployeeDTO(example.serialization.ExternalizableEmployeeDTO) CompactReader(com.hazelcast.nio.serialization.compact.CompactReader) Nonnull(javax.annotation.Nonnull) SerializationConfig(com.hazelcast.config.SerializationConfig) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

CompactReader (com.hazelcast.nio.serialization.compact.CompactReader)6 CompactWriter (com.hazelcast.nio.serialization.compact.CompactWriter)5 Nonnull (javax.annotation.Nonnull)5 CompactSerializationConfig (com.hazelcast.config.CompactSerializationConfig)4 SerializationConfig (com.hazelcast.config.SerializationConfig)3 Data (com.hazelcast.internal.serialization.Data)3 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)3 SerializationService (com.hazelcast.internal.serialization.SerializationService)3 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 EmployeeDTO (example.serialization.EmployeeDTO)3 ExternalizableEmployeeDTO (example.serialization.ExternalizableEmployeeDTO)3 Test (org.junit.Test)3 HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)2 ClientConfig (com.hazelcast.client.config.ClientConfig)1 Config (com.hazelcast.config.Config)1 DefaultCompactReader (com.hazelcast.internal.serialization.impl.compact.DefaultCompactReader)1 FieldDescriptor (com.hazelcast.internal.serialization.impl.compact.FieldDescriptor)1 Schema (com.hazelcast.internal.serialization.impl.compact.Schema)1