use of org.apache.flink.api.common.typeutils.TypeSerializer in project flink by apache.
the class PojoSerializer method deserialize.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public T deserialize(T reuse, DataInputView source) throws IOException {
// handle null values
int flags = source.readByte();
if ((flags & IS_NULL) != 0) {
return null;
}
Class<?> subclass = null;
TypeSerializer subclassSerializer = null;
if ((flags & IS_SUBCLASS) != 0) {
String subclassName = source.readUTF();
try {
subclass = Class.forName(subclassName, true, cl);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Cannot instantiate class.", e);
}
subclassSerializer = getSubclassSerializer(subclass);
if (reuse == null || subclass != reuse.getClass()) {
// cannot reuse
reuse = (T) subclassSerializer.createInstance();
// also initialize fields for which the subclass serializer is not responsible
initializeFields(reuse);
}
} else if ((flags & IS_TAGGED_SUBCLASS) != 0) {
int subclassTag = source.readByte();
subclassSerializer = registeredSerializers[subclassTag];
if (reuse == null || ((PojoSerializer) subclassSerializer).clazz != reuse.getClass()) {
// cannot reuse
reuse = (T) subclassSerializer.createInstance();
// also initialize fields for which the subclass serializer is not responsible
initializeFields(reuse);
}
} else {
if (reuse == null || clazz != reuse.getClass()) {
reuse = createInstance();
}
}
if ((flags & NO_SUBCLASS) != 0) {
try {
for (int i = 0; i < numFields; i++) {
boolean isNull = source.readBoolean();
if (isNull) {
fields[i].set(reuse, null);
} else {
Object field;
Object reuseField = fields[i].get(reuse);
if (reuseField != null) {
field = fieldSerializers[i].deserialize(reuseField, source);
} else {
field = fieldSerializers[i].deserialize(source);
}
fields[i].set(reuse, field);
}
}
} catch (IllegalAccessException e) {
throw new RuntimeException("Error during POJO copy, this should not happen since we check the fields before.");
}
} else {
if (subclassSerializer != null) {
reuse = (T) subclassSerializer.deserialize(reuse, source);
}
}
return reuse;
}
use of org.apache.flink.api.common.typeutils.TypeSerializer in project flink by apache.
the class PojoSerializer method deserialize.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public T deserialize(DataInputView source) throws IOException {
int flags = source.readByte();
if ((flags & IS_NULL) != 0) {
return null;
}
T target;
Class<?> actualSubclass = null;
TypeSerializer subclassSerializer = null;
if ((flags & IS_SUBCLASS) != 0) {
String subclassName = source.readUTF();
try {
actualSubclass = Class.forName(subclassName, true, cl);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Cannot instantiate class.", e);
}
subclassSerializer = getSubclassSerializer(actualSubclass);
target = (T) subclassSerializer.createInstance();
// also initialize fields for which the subclass serializer is not responsible
initializeFields(target);
} else if ((flags & IS_TAGGED_SUBCLASS) != 0) {
int subclassTag = source.readByte();
subclassSerializer = registeredSerializers[subclassTag];
target = (T) subclassSerializer.createInstance();
// also initialize fields for which the subclass serializer is not responsible
initializeFields(target);
} else {
target = createInstance();
}
if ((flags & NO_SUBCLASS) != 0) {
try {
for (int i = 0; i < numFields; i++) {
boolean isNull = source.readBoolean();
if (isNull) {
fields[i].set(target, null);
} else {
Object field = fieldSerializers[i].deserialize(source);
fields[i].set(target, field);
}
}
} catch (IllegalAccessException e) {
throw new RuntimeException("Error during POJO copy, this should not happen since we check the fields" + "before.");
}
} else {
if (subclassSerializer != null) {
target = (T) subclassSerializer.deserialize(target, source);
}
}
return target;
}
use of org.apache.flink.api.common.typeutils.TypeSerializer in project flink by apache.
the class PojoSerializer method copy.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public T copy(T from) {
if (from == null) {
return null;
}
Class<?> actualType = from.getClass();
if (actualType == clazz) {
T target;
try {
target = (T) from.getClass().newInstance();
} catch (Throwable t) {
throw new RuntimeException("Cannot instantiate class.", t);
}
// no subclass
try {
for (int i = 0; i < numFields; i++) {
Object value = fields[i].get(from);
if (value != null) {
Object copy = fieldSerializers[i].copy(value);
fields[i].set(target, copy);
} else {
fields[i].set(target, null);
}
}
} catch (IllegalAccessException e) {
throw new RuntimeException("Error during POJO copy, this should not happen since we check the fields before.");
}
return target;
} else {
// subclass
TypeSerializer subclassSerializer = getSubclassSerializer(actualType);
return (T) subclassSerializer.copy(from);
}
}
use of org.apache.flink.api.common.typeutils.TypeSerializer in project flink by apache.
the class PojoSerializer method serialize.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void serialize(T value, DataOutputView target) throws IOException {
int flags = 0;
// handle null values
if (value == null) {
flags |= IS_NULL;
target.writeByte(flags);
return;
}
Integer subclassTag = -1;
Class<?> actualClass = value.getClass();
TypeSerializer subclassSerializer = null;
if (clazz != actualClass) {
subclassTag = registeredClasses.get(actualClass);
if (subclassTag != null) {
flags |= IS_TAGGED_SUBCLASS;
subclassSerializer = registeredSerializers[subclassTag];
} else {
flags |= IS_SUBCLASS;
subclassSerializer = getSubclassSerializer(actualClass);
}
} else {
flags |= NO_SUBCLASS;
}
target.writeByte(flags);
if ((flags & IS_SUBCLASS) != 0) {
target.writeUTF(actualClass.getName());
} else if ((flags & IS_TAGGED_SUBCLASS) != 0) {
target.writeByte(subclassTag);
}
if ((flags & NO_SUBCLASS) != 0) {
try {
for (int i = 0; i < numFields; i++) {
Object o = fields[i].get(value);
if (o == null) {
// null field handling
target.writeBoolean(true);
} else {
target.writeBoolean(false);
fieldSerializers[i].serialize(o, target);
}
}
} catch (IllegalAccessException e) {
throw new RuntimeException("Error during POJO copy, this should not happen since we check the fields" + "before.");
}
} else {
// subclass
if (subclassSerializer != null) {
subclassSerializer.serialize(value, target);
}
}
}
use of org.apache.flink.api.common.typeutils.TypeSerializer in project flink by apache.
the class AbstractHeapState method getSerializedValue.
public byte[] getSerializedValue(K key, N namespace) throws Exception {
Preconditions.checkState(namespace != null, "No namespace given.");
Preconditions.checkState(key != null, "No key given.");
SV result = stateTable.get(key, namespace);
if (result == null) {
return null;
}
@SuppressWarnings("unchecked,rawtypes") TypeSerializer serializer = stateDesc.getSerializer();
return KvStateRequestSerializer.serializeValue(result, serializer);
}
Aggregations