use of com.hazelcast.nio.serialization.GenericRecord in project hazelcast by hazelcast.
the class RecordSerializationTest method shouldBeInteroperableBetweenNullableAndPrimitiveFixSizedTypes.
@Test
public void shouldBeInteroperableBetweenNullableAndPrimitiveFixSizedTypes() {
// Writing fix sized primitive field and reading them as nullable
// fix sized field and vice-versa.
GenericRecord genericRecord = GenericRecordBuilder.compact(AllTypesRecord.class.getName()).setInt8("objectInt8", Byte.MAX_VALUE).setNullableInt8("primitiveInt8", Byte.MIN_VALUE).setInt16("objectInt16", Short.MAX_VALUE).setNullableInt16("primitiveInt16", Short.MIN_VALUE).setInt32("objectInt32", Integer.MAX_VALUE).setNullableInt32("primitiveInt32", Integer.MIN_VALUE).setInt64("objectInt64", Long.MAX_VALUE).setNullableInt64("primitiveInt64", Long.MIN_VALUE).setFloat32("objectFloat32", Float.MAX_VALUE).setNullableFloat32("primitiveFloat32", Float.MIN_VALUE).setFloat64("objectFloat64", Double.MAX_VALUE).setNullableFloat64("primitiveFloat64", Double.MIN_VALUE).setBoolean("objectBoolean", Boolean.TRUE).setNullableBoolean("primitiveBoolean", Boolean.FALSE).setArrayOfInt8("objectInt8Array", new byte[] { Byte.MAX_VALUE }).setArrayOfNullableInt8("primitiveInt8Array", new Byte[] { Byte.MIN_VALUE }).setArrayOfInt16("objectInt16Array", new short[] { Short.MAX_VALUE }).setArrayOfNullableInt16("primitiveInt16Array", new Short[] { Short.MIN_VALUE }).setArrayOfInt32("objectInt32Array", new int[] { Integer.MAX_VALUE }).setArrayOfNullableInt32("primitiveInt32Array", new Integer[] { Integer.MIN_VALUE }).setArrayOfInt64("objectInt64Array", new long[] { Long.MAX_VALUE }).setArrayOfNullableInt64("primitiveInt64Array", new Long[] { Long.MIN_VALUE }).setArrayOfFloat32("objectFloat32Array", new float[] { Float.MAX_VALUE }).setArrayOfNullableFloat32("primitiveFloat32Array", new Float[] { Float.MIN_VALUE }).setArrayOfFloat64("objectFloat64Array", new double[] { Double.MAX_VALUE }).setArrayOfNullableFloat64("primitiveFloat64Array", new Double[] { Double.MIN_VALUE }).setArrayOfBoolean("objectBooleanArray", new boolean[] { Boolean.TRUE }).setArrayOfNullableBoolean("primitiveBooleanArray", new Boolean[] { Boolean.FALSE }).build();
Data serialized = service.toData(genericRecord);
AllTypesRecord deserialized = service.toObject(serialized);
assertThat(deserialized.primitiveInt8()).isEqualTo(genericRecord.getNullableInt8("primitiveInt8"));
assertThat(deserialized.objectInt8()).isEqualTo(genericRecord.getInt8("objectInt8"));
assertThat(deserialized.primitiveInt16()).isEqualTo(genericRecord.getNullableInt16("primitiveInt16"));
assertThat(deserialized.objectInt16()).isEqualTo(genericRecord.getInt16("objectInt16"));
assertThat(deserialized.primitiveInt32()).isEqualTo(genericRecord.getNullableInt32("primitiveInt32"));
assertThat(deserialized.objectInt32()).isEqualTo(genericRecord.getInt32("objectInt32"));
assertThat(deserialized.primitiveInt64()).isEqualTo(genericRecord.getNullableInt64("primitiveInt64"));
assertThat(deserialized.objectInt64()).isEqualTo(genericRecord.getInt64("objectInt64"));
assertThat(deserialized.primitiveFloat32()).isEqualTo(genericRecord.getNullableFloat32("primitiveFloat32"));
assertThat(deserialized.objectFloat32()).isEqualTo(genericRecord.getFloat32("objectFloat32"));
assertThat(deserialized.primitiveFloat64()).isEqualTo(genericRecord.getNullableFloat64("primitiveFloat64"));
assertThat(deserialized.objectFloat64()).isEqualTo(genericRecord.getFloat64("objectFloat64"));
assertThat(deserialized.primitiveBoolean()).isEqualTo(genericRecord.getNullableBoolean("primitiveBoolean"));
assertThat(deserialized.objectBoolean()).isEqualTo(genericRecord.getBoolean("objectBoolean"));
assertThat(deserialized.primitiveInt8Array()).containsExactly(genericRecord.getArrayOfNullableInt8("primitiveInt8Array")[0]);
assertThat(deserialized.objectInt8Array()).containsExactly(genericRecord.getArrayOfInt8("objectInt8Array")[0]);
assertThat(deserialized.primitiveInt16Array()).containsExactly(genericRecord.getArrayOfNullableInt16("primitiveInt16Array")[0]);
assertThat(deserialized.objectInt16Array()).containsExactly(genericRecord.getArrayOfInt16("objectInt16Array")[0]);
assertThat(deserialized.primitiveInt32Array()).containsExactly(genericRecord.getArrayOfNullableInt32("primitiveInt32Array")[0]);
assertThat(deserialized.objectInt32Array()).containsExactly(genericRecord.getArrayOfInt32("objectInt32Array")[0]);
assertThat(deserialized.primitiveInt64Array()).containsExactly(genericRecord.getArrayOfNullableInt64("primitiveInt64Array")[0]);
assertThat(deserialized.objectInt64Array()).containsExactly(genericRecord.getArrayOfInt64("objectInt64Array")[0]);
assertThat(deserialized.primitiveFloat32Array()).containsExactly(genericRecord.getArrayOfNullableFloat32("primitiveFloat32Array")[0]);
assertThat(deserialized.objectFloat32Array()).containsExactly(genericRecord.getArrayOfFloat32("objectFloat32Array")[0]);
assertThat(deserialized.primitiveFloat64Array()).containsExactly(genericRecord.getArrayOfNullableFloat64("primitiveFloat64Array")[0]);
assertThat(deserialized.objectFloat64Array()).containsExactly(genericRecord.getArrayOfFloat64("objectFloat64Array")[0]);
assertThat(deserialized.primitiveBooleanArray()).containsExactly(genericRecord.getArrayOfNullableBoolean("primitiveBooleanArray")[0]);
assertThat(deserialized.objectBooleanArray()).containsExactly(genericRecord.getArrayOfBoolean("objectBooleanArray")[0]);
}
use of com.hazelcast.nio.serialization.GenericRecord 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();
}
use of com.hazelcast.nio.serialization.GenericRecord in project hazelcast by hazelcast.
the class PortableSerializer method read.
/**
* Tries to construct the user's Portable object first via given factory config.
* If it can not find the related factory, this will return GenericRecord representation of the object.
*/
<T> T read(BufferObjectDataInput in, int factoryId, int classId) throws IOException {
Portable portable = createNewPortableInstance(factoryId, classId);
if (portable != null) {
int writeVersion = in.readInt();
int readVersion = findPortableVersion(factoryId, classId, portable);
DefaultPortableReader reader = createReader(in, factoryId, classId, writeVersion, readVersion);
portable.readPortable(reader);
reader.end();
final ManagedContext managedContext = context.getManagedContext();
return managedContext != null ? (T) managedContext.initialize(portable) : (T) portable;
}
GenericRecord genericRecord = readPortableGenericRecord(in, factoryId, classId);
assert genericRecord instanceof PortableGenericRecord;
return (T) genericRecord;
}
use of com.hazelcast.nio.serialization.GenericRecord in project hazelcast by hazelcast.
the class GenericRecordQueryReader method read.
public Object read(String fieldPath) throws IOException {
if (fieldPath == null) {
throw new IllegalArgumentException("field path can not be null");
}
if (fieldPath.endsWith(".")) {
throw new IllegalArgumentException("Malformed path " + fieldPath);
}
if (rootRecord.hasField(fieldPath)) {
return readLeaf(rootRecord, fieldPath);
}
LinkedList<Object> results = new LinkedList<>();
results.add(rootRecord);
MultiResult<Object> multiResult = new MultiResult<>(results);
int begin = 0;
int end = StringUtil.indexOf(fieldPath, '.');
// handle the paths except leaf
while (end != -1) {
String path = fieldPath.substring(begin, end);
if (path.length() == 0) {
throw new IllegalArgumentException("The token's length cannot be zero: " + fieldPath);
}
begin = end + 1;
end = StringUtil.indexOf(fieldPath, '.', begin);
ListIterator<Object> iterator = results.listIterator();
String fieldName = extractAttributeNameNameWithoutArguments(path);
if (!path.contains("]")) {
// ex: attribute
while (iterator.hasNext()) {
InternalGenericRecord record = (InternalGenericRecord) iterator.next();
if (!record.hasField(fieldName)) {
iterator.remove();
multiResult.setNullOrEmptyTarget(true);
continue;
}
InternalGenericRecord subGenericRecord = (InternalGenericRecord) record.getGenericRecord(fieldName);
if (subGenericRecord == null) {
iterator.remove();
multiResult.setNullOrEmptyTarget(true);
continue;
}
iterator.set(subGenericRecord);
}
} else if (path.endsWith("[any]")) {
// ex: attribute any
while (iterator.hasNext()) {
InternalGenericRecord record = (InternalGenericRecord) iterator.next();
iterator.remove();
if (!record.hasField(fieldName)) {
multiResult.setNullOrEmptyTarget(true);
continue;
}
GenericRecord[] genericRecords = record.getArrayOfGenericRecord(fieldName);
if (genericRecords == null || genericRecords.length == 0) {
multiResult.setNullOrEmptyTarget(true);
continue;
}
for (GenericRecord genericRecord : genericRecords) {
if (genericRecord != null) {
iterator.add(genericRecord);
} else {
multiResult.setNullOrEmptyTarget(true);
}
}
}
} else {
// ex: attribute[2]
int index = Integer.parseInt(extractArgumentsFromAttributeName(path));
while (iterator.hasNext()) {
InternalGenericRecord record = (InternalGenericRecord) iterator.next();
if (!record.hasField(fieldName)) {
iterator.remove();
multiResult.setNullOrEmptyTarget(true);
continue;
}
GenericRecord genericRecord = record.getGenericRecordFromArray(fieldName, index);
if (genericRecord != null) {
iterator.set(genericRecord);
} else {
iterator.remove();
multiResult.setNullOrEmptyTarget(true);
}
}
}
}
// last loop that we have skipped
String path = fieldPath.substring(begin);
if (path.length() == 0) {
throw new IllegalArgumentException("The token's length cannot be zero: " + fieldPath);
}
ListIterator<Object> iterator = results.listIterator();
String fieldName = extractAttributeNameNameWithoutArguments(path);
if (!path.contains("]")) {
// ex: attribute
while (iterator.hasNext()) {
InternalGenericRecord record = (InternalGenericRecord) iterator.next();
Object leaf = readLeaf(record, fieldName);
iterator.set(leaf);
}
} else if (path.endsWith("[any]")) {
// ex: attribute any
while (iterator.hasNext()) {
InternalGenericRecord record = (InternalGenericRecord) iterator.next();
iterator.remove();
Object leaves = readLeaf(record, fieldName);
if (leaves == null) {
multiResult.setNullOrEmptyTarget(true);
} else if (leaves instanceof Object[]) {
Object[] array = (Object[]) leaves;
if (array.length == 0) {
multiResult.setNullOrEmptyTarget(true);
continue;
}
for (Object leaf : array) {
iterator.add(leaf);
}
} else {
assert leaves.getClass().isArray() : "parameter is not an array";
if (!ExtractorHelper.reducePrimitiveArrayInto(iterator::add, leaves)) {
multiResult.setNullOrEmptyTarget(true);
}
}
}
} else {
// ex: attribute[2]
int index = Integer.parseInt(extractArgumentsFromAttributeName(path));
while (iterator.hasNext()) {
GenericRecord record = (GenericRecord) iterator.next();
Object leaf = readIndexed((InternalGenericRecord) record, fieldName, index);
iterator.set(leaf);
}
}
if (multiResult.isNullEmptyTarget()) {
results.addFirst(null);
} else if (results.size() == 1) {
return results.get(0);
}
return multiResult;
}
use of com.hazelcast.nio.serialization.GenericRecord in project hazelcast by hazelcast.
the class SqlCompactTest method test_readNonNullKindsOfCompactViaSQL.
@Test
public void test_readNonNullKindsOfCompactViaSQL() throws IOException {
String name = randomName();
sqlService.execute("CREATE MAPPING " + name + " (" + " __key INT " + ", b BOOLEAN " + ", st VARCHAR " + ", bt TINYINT " + ", s SMALLINT " + ", i INTEGER " + ", l BIGINT " + ", bd DECIMAL " + ", f REAL " + ", d DOUBLE " + ", t TIME " + ", dt DATE " + ", tmstmp TIMESTAMP " + ", tmstmpTz TIMESTAMP WITH TIME ZONE " + ") " + "TYPE " + IMapSqlConnector.TYPE_NAME + ' ' + "OPTIONS (" + '\'' + OPTION_KEY_FORMAT + "'='integer'" + ", '" + OPTION_VALUE_FORMAT + "'='" + COMPACT_FORMAT + '\'' + ", '" + OPTION_VALUE_COMPACT_TYPE_NAME + "'='" + PERSON_TYPE_NAME + '\'' + ")").updateCount();
boolean b = true;
String st = "test";
byte bt = (byte) 12;
short s = 1312;
int i = 12314;
long l = 23214141L;
BigDecimal bd = BigDecimal.TEN;
float f = 13221321.213213f;
double d = 13221321.213213d;
LocalDate dt = LocalDate.now();
LocalTime t = LocalTime.now();
LocalDateTime tmstmp = LocalDateTime.now();
OffsetDateTime tmstmpTz = OffsetDateTime.now();
GenericRecord record = GenericRecordBuilder.compact(PERSON_TYPE_NAME).setBoolean("b", b).setString("st", st).setInt8("bt", bt).setInt16("s", s).setInt32("i", i).setInt64("l", l).setDecimal("bd", bd).setFloat32("f", f).setFloat64("d", d).setTime("t", t).setDate("dt", dt).setTimestamp("tmstmp", tmstmp).setTimestampWithTimezone("tmstmpTz", tmstmpTz).build();
IMap<Object, Object> map = client().getMap(name);
map.put(1, record);
assertRowsAnyOrder("SELECT * FROM " + name, singletonList(new Row(1, b, st, bt, s, i, l, bd, f, d, t, dt, tmstmp, tmstmpTz)));
}
Aggregations