Search in sources :

Example 1 with PortableGenericRecordBuilder

use of com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder in project hazelcast by hazelcast.

the class PortableUpsertTarget method toRecord.

private static GenericRecord toRecord(ClassDefinition classDefinition, Object[] values) {
    PortableGenericRecordBuilder portable = new PortableGenericRecordBuilder(classDefinition);
    for (int i = 0; i < classDefinition.getFieldCount(); i++) {
        FieldDefinition fieldDefinition = classDefinition.getField(i);
        String name = fieldDefinition.getName();
        FieldType type = fieldDefinition.getType();
        Object value = values[i];
        try {
            switch(type) {
                case BOOLEAN:
                    ensureNotNull(value);
                    portable.setBoolean(name, value != NOT_SET && (boolean) value);
                    break;
                case BYTE:
                    ensureNotNull(value);
                    portable.setInt8(name, value == NOT_SET ? (byte) 0 : (byte) value);
                    break;
                case SHORT:
                    ensureNotNull(value);
                    portable.setInt16(name, value == NOT_SET ? (short) 0 : (short) value);
                    break;
                case CHAR:
                    ensureNotNull(value);
                    portable.setChar(name, value == NOT_SET ? (char) 0 : (char) value);
                    break;
                case INT:
                    ensureNotNull(value);
                    portable.setInt32(name, value == NOT_SET ? 0 : (int) value);
                    break;
                case LONG:
                    ensureNotNull(value);
                    portable.setInt64(name, value == NOT_SET ? 0L : (long) value);
                    break;
                case FLOAT:
                    ensureNotNull(value);
                    portable.setFloat32(name, value == NOT_SET ? 0F : (float) value);
                    break;
                case DOUBLE:
                    ensureNotNull(value);
                    portable.setFloat64(name, value == NOT_SET ? 0D : (double) value);
                    break;
                case DECIMAL:
                    portable.setDecimal(name, value == NOT_SET ? null : (BigDecimal) value);
                    break;
                case UTF:
                    portable.setString(name, value == NOT_SET ? null : (String) QueryDataType.VARCHAR.convert(value));
                    break;
                case TIME:
                    portable.setTime(name, value == NOT_SET ? null : (LocalTime) value);
                    break;
                case DATE:
                    portable.setDate(name, value == NOT_SET ? null : (LocalDate) value);
                    break;
                case TIMESTAMP:
                    portable.setTimestamp(name, value == NOT_SET ? null : (LocalDateTime) value);
                    break;
                case TIMESTAMP_WITH_TIMEZONE:
                    portable.setTimestampWithTimezone(name, value == NOT_SET ? null : (OffsetDateTime) value);
                    break;
                case PORTABLE:
                    portable.setGenericRecord(name, value == NOT_SET ? null : (GenericRecord) value);
                    break;
                case BOOLEAN_ARRAY:
                    portable.setArrayOfBoolean(name, value == NOT_SET ? null : (boolean[]) value);
                    break;
                case BYTE_ARRAY:
                    portable.setArrayOfInt8(name, value == NOT_SET ? null : (byte[]) value);
                    break;
                case SHORT_ARRAY:
                    portable.setArrayOfInt16(name, value == NOT_SET ? null : (short[]) value);
                    break;
                case CHAR_ARRAY:
                    portable.setArrayOfChar(name, value == NOT_SET ? null : (char[]) value);
                    break;
                case INT_ARRAY:
                    portable.setArrayOfInt32(name, value == NOT_SET ? null : (int[]) value);
                    break;
                case LONG_ARRAY:
                    portable.setArrayOfInt64(name, value == NOT_SET ? null : (long[]) value);
                    break;
                case FLOAT_ARRAY:
                    portable.setArrayOfFloat32(name, value == NOT_SET ? null : (float[]) value);
                    break;
                case DOUBLE_ARRAY:
                    portable.setArrayOfFloat64(name, value == NOT_SET ? null : (double[]) value);
                    break;
                case DECIMAL_ARRAY:
                    portable.setArrayOfDecimal(name, value == NOT_SET ? null : (BigDecimal[]) value);
                    break;
                case UTF_ARRAY:
                    portable.setArrayOfString(name, value == NOT_SET ? null : (String[]) value);
                    break;
                case TIME_ARRAY:
                    portable.setArrayOfTime(name, value == NOT_SET ? null : (LocalTime[]) value);
                    break;
                case DATE_ARRAY:
                    portable.setArrayOfDate(name, value == NOT_SET ? null : (LocalDate[]) value);
                    break;
                case TIMESTAMP_ARRAY:
                    portable.setArrayOfTimestamp(name, value == NOT_SET ? null : (LocalDateTime[]) value);
                    break;
                case TIMESTAMP_WITH_TIMEZONE_ARRAY:
                    portable.setArrayOfTimestampWithTimezone(name, value == NOT_SET ? null : (OffsetDateTime[]) value);
                    break;
                case PORTABLE_ARRAY:
                    portable.setArrayOfGenericRecord(name, value == NOT_SET ? null : (GenericRecord[]) value);
                    break;
                default:
                    throw QueryException.error("Unsupported type: " + type);
            }
        } catch (Exception e) {
            throw QueryException.error("Cannot set value " + (value == null ? "null" : "of type " + value.getClass().getName()) + " to field \"" + name + "\" of type " + type + ": " + e.getMessage(), e);
        }
    }
    return portable.build();
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) FieldDefinition(com.hazelcast.nio.serialization.FieldDefinition) PortableGenericRecordBuilder(com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) QueryException(com.hazelcast.sql.impl.QueryException) FieldType(com.hazelcast.nio.serialization.FieldType) OffsetDateTime(java.time.OffsetDateTime) GenericRecord(com.hazelcast.nio.serialization.GenericRecord)

Example 2 with PortableGenericRecordBuilder

use of com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder in project hazelcast by hazelcast.

the class SqlPortableTest method test_topLevelFieldExtraction.

@Test
public void test_topLevelFieldExtraction() {
    String name = randomName();
    sqlService.execute("CREATE MAPPING " + name + ' ' + "TYPE " + IMapSqlConnector.TYPE_NAME + ' ' + "OPTIONS (" + '\'' + OPTION_KEY_FORMAT + "'='" + PORTABLE_FORMAT + '\'' + ", '" + OPTION_KEY_FACTORY_ID + "'='" + PERSON_ID_FACTORY_ID + '\'' + ", '" + OPTION_KEY_CLASS_ID + "'='" + PERSON_ID_CLASS_ID + '\'' + ", '" + OPTION_KEY_CLASS_VERSION + "'='" + PERSON_ID_CLASS_VERSION + '\'' + ", '" + OPTION_VALUE_FORMAT + "'='" + PORTABLE_FORMAT + '\'' + ", '" + OPTION_VALUE_FACTORY_ID + "'='" + PERSON_FACTORY_ID + '\'' + ", '" + OPTION_VALUE_CLASS_ID + "'='" + PERSON_CLASS_ID + '\'' + ", '" + OPTION_VALUE_CLASS_VERSION + "'='" + PERSON_CLASS_VERSION + '\'' + ")");
    sqlService.execute("SINK INTO " + name + " (id, name) VALUES (1, 'Alice')");
    Iterator<SqlRow> rowIterator = sqlService.execute("SELECT __key, this FROM " + name).iterator();
    SqlRow row = rowIterator.next();
    assertFalse(rowIterator.hasNext());
    assertEquals(new PortableGenericRecordBuilder(personIdClassDefinition).setInt32("id", 1).build(), row.getObject(0));
    assertEquals(new PortableGenericRecordBuilder(personClassDefinition).setInt32("id", 0).setString("name", "Alice").build(), row.getObject(1));
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) PortableGenericRecordBuilder(com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder) Test(org.junit.Test)

Example 3 with PortableGenericRecordBuilder

use of com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder in project hazelcast by hazelcast.

the class SampleMetadataResolverTest method test_portableRecord.

@Test
public void test_portableRecord() {
    ClassDefinition classDefinition = new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, PORTABLE_CLASS_ID, PORTABLE_CLASS_VERSION).build();
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().addClassDefinition(classDefinition).build();
    Metadata metadata = SampleMetadataResolver.resolve(ss, new PortableGenericRecordBuilder(classDefinition).build(), key);
    assertThat(metadata.options()).containsExactly(entry(key ? OPTION_KEY_FORMAT : OPTION_VALUE_FORMAT, PORTABLE_FORMAT), entry(key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID, String.valueOf(PORTABLE_FACTORY_ID)), entry(key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID, String.valueOf(PORTABLE_CLASS_ID)), entry(key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION, String.valueOf(PORTABLE_CLASS_VERSION)));
    metadata = SampleMetadataResolver.resolve(ss, ss.toData(new PortableGenericRecordBuilder(classDefinition).build()), key);
    assertThat(metadata.options()).containsExactly(entry(key ? OPTION_KEY_FORMAT : OPTION_VALUE_FORMAT, PORTABLE_FORMAT), entry(key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID, String.valueOf(PORTABLE_FACTORY_ID)), entry(key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID, String.valueOf(PORTABLE_CLASS_ID)), entry(key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION, String.valueOf(PORTABLE_CLASS_VERSION)));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) PortableGenericRecordBuilder(com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with PortableGenericRecordBuilder

use of com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder in project hazelcast by hazelcast.

the class SqlPortableTest method when_noFieldsResolved_then_wholeValueMapped.

@Test
public void when_noFieldsResolved_then_wholeValueMapped() {
    String name = randomName();
    sqlService.execute("CREATE MAPPING " + name + ' ' + "TYPE " + IMapSqlConnector.TYPE_NAME + ' ' + "OPTIONS (" + '\'' + OPTION_KEY_FORMAT + "'='" + PORTABLE_FORMAT + '\'' + ", '" + OPTION_KEY_FACTORY_ID + "'='" + EMPTY_TYPES_FACTORY_ID + '\'' + ", '" + OPTION_KEY_CLASS_ID + "'='" + EMPTY_TYPES_CLASS_ID + '\'' + ", '" + OPTION_KEY_CLASS_VERSION + "'='" + EMPTY_TYPES_CLASS_VERSION + '\'' + ", '" + OPTION_VALUE_FORMAT + "'='" + PORTABLE_FORMAT + '\'' + ", '" + OPTION_VALUE_FACTORY_ID + "'='" + EMPTY_TYPES_FACTORY_ID + '\'' + ", '" + OPTION_VALUE_CLASS_ID + "'='" + EMPTY_TYPES_CLASS_ID + '\'' + ", '" + OPTION_VALUE_CLASS_VERSION + "'='" + EMPTY_TYPES_CLASS_VERSION + '\'' + ")");
    GenericRecord record = new PortableGenericRecordBuilder(emptyClassDefinition).build();
    instance().getMap(name).put(record, record);
    assertRowsAnyOrder("SELECT __key, this FROM " + name, singletonList(new Row(record, record)));
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) GenericRecord(com.hazelcast.nio.serialization.GenericRecord) InternalGenericRecord(com.hazelcast.internal.serialization.impl.InternalGenericRecord) PortableGenericRecordBuilder(com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder) Test(org.junit.Test)

Example 5 with PortableGenericRecordBuilder

use of com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder in project hazelcast by hazelcast.

the class PortableUpsertTargetTest method test_set.

@Test
public void test_set() throws IOException {
    ClassDefinition innerClassDefinition = new ClassDefinitionBuilder(4, 5, 6).build();
    ClassDefinition classDefinition = new ClassDefinitionBuilder(1, 2, 3).addPortableField("null", innerClassDefinition).addPortableField("object", innerClassDefinition).addStringField("string").addCharField("character").addBooleanField("boolean").addByteField("byte").addShortField("short").addIntField("int").addLongField("long").addFloatField("float").addDoubleField("double").addDecimalField("decimal").addTimeField("time").addDateField("date").addTimestampField("timestamp").addTimestampWithTimezoneField("timestampTz").build();
    UpsertTarget target = new PortableUpsertTarget(classDefinition);
    UpsertInjector nullFieldInjector = target.createInjector("null", QueryDataType.OBJECT);
    UpsertInjector objectFieldInjector = target.createInjector("object", QueryDataType.OBJECT);
    UpsertInjector stringFieldInjector = target.createInjector("string", QueryDataType.VARCHAR);
    UpsertInjector characterFieldInjector = target.createInjector("character", QueryDataType.VARCHAR_CHARACTER);
    UpsertInjector booleanFieldInjector = target.createInjector("boolean", QueryDataType.BOOLEAN);
    UpsertInjector byteFieldInjector = target.createInjector("byte", QueryDataType.TINYINT);
    UpsertInjector shortFieldInjector = target.createInjector("short", QueryDataType.SMALLINT);
    UpsertInjector intFieldInjector = target.createInjector("int", QueryDataType.INT);
    UpsertInjector longFieldInjector = target.createInjector("long", QueryDataType.BIGINT);
    UpsertInjector floatFieldInjector = target.createInjector("float", QueryDataType.REAL);
    UpsertInjector doubleFieldInjector = target.createInjector("double", QueryDataType.DOUBLE);
    UpsertInjector decimalFieldInjector = target.createInjector("decimal", QueryDataType.DECIMAL);
    UpsertInjector timeFieldInjector = target.createInjector("time", QueryDataType.TIME);
    UpsertInjector dateFieldInjector = target.createInjector("date", QueryDataType.DATE);
    UpsertInjector timestampFieldInjector = target.createInjector("timestamp", QueryDataType.TIMESTAMP);
    UpsertInjector timestampTzFieldInjector = target.createInjector("timestampTz", QueryDataType.TIMESTAMP_WITH_TZ_OFFSET_DATE_TIME);
    target.init();
    nullFieldInjector.set(null);
    objectFieldInjector.set(new PortableGenericRecordBuilder(innerClassDefinition).build());
    stringFieldInjector.set("1");
    characterFieldInjector.set('2');
    booleanFieldInjector.set(true);
    byteFieldInjector.set((byte) 3);
    shortFieldInjector.set((short) 4);
    intFieldInjector.set(5);
    longFieldInjector.set(6L);
    floatFieldInjector.set(7.1F);
    doubleFieldInjector.set(7.2D);
    decimalFieldInjector.set(new BigDecimal("8.1"));
    timeFieldInjector.set(LocalTime.of(12, 23, 34));
    dateFieldInjector.set(LocalDate.of(2021, 2, 9));
    timestampFieldInjector.set(LocalDateTime.of(2021, 2, 9, 12, 23, 34, 1_000_000));
    timestampTzFieldInjector.set(OffsetDateTime.of(2021, 2, 9, 12, 23, 34, 200_000_000, UTC));
    Object portable = target.conclude();
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    InternalGenericRecord record = ss.readAsInternalGenericRecord(ss.toData(portable));
    assertThat(record.getGenericRecord("null")).isNull();
    assertThat(record.getGenericRecord("object")).isEqualTo(new PortableGenericRecordBuilder(innerClassDefinition).build());
    assertThat(record.getString("string")).isEqualTo("1");
    assertThat(record.getChar("character")).isEqualTo('2');
    assertThat(record.getBoolean("boolean")).isEqualTo(true);
    assertThat(record.getInt8("byte")).isEqualTo((byte) 3);
    assertThat(record.getInt16("short")).isEqualTo((short) 4);
    assertThat(record.getInt32("int")).isEqualTo(5);
    assertThat(record.getInt64("long")).isEqualTo(6L);
    assertThat(record.getFloat32("float")).isEqualTo(7.1F);
    assertThat(record.getFloat64("double")).isEqualTo(7.2D);
    assertThat(record.getDecimal("decimal")).isEqualTo(new BigDecimal("8.1"));
    assertThat(record.getTime("time")).isEqualTo(LocalTime.of(12, 23, 34));
    assertThat(record.getDate("date")).isEqualTo(LocalDate.of(2021, 2, 9));
    assertThat(record.getTimestamp("timestamp")).isEqualTo(LocalDateTime.of(2021, 2, 9, 12, 23, 34, 1_000_000));
    assertThat(record.getTimestampWithTimezone("timestampTz")).isEqualTo(OffsetDateTime.of(2021, 2, 9, 12, 23, 34, 200_000_000, UTC));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) PortableGenericRecordBuilder(com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder) BigDecimal(java.math.BigDecimal) InternalGenericRecord(com.hazelcast.internal.serialization.impl.InternalGenericRecord) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Test(org.junit.Test)

Aggregations

PortableGenericRecordBuilder (com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder)5 Test (org.junit.Test)4 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)2 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)2 InternalGenericRecord (com.hazelcast.internal.serialization.impl.InternalGenericRecord)2 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)2 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)2 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)2 SqlRow (com.hazelcast.sql.SqlRow)2 BigDecimal (java.math.BigDecimal)2 FieldDefinition (com.hazelcast.nio.serialization.FieldDefinition)1 FieldType (com.hazelcast.nio.serialization.FieldType)1 QueryException (com.hazelcast.sql.impl.QueryException)1 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 OffsetDateTime (java.time.OffsetDateTime)1