Search in sources :

Example 21 with TypeSerializer

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;
}
Also used : TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer)

Example 22 with TypeSerializer

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;
}
Also used : TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer)

Example 23 with TypeSerializer

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);
    }
}
Also used : TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer)

Example 24 with TypeSerializer

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);
        }
    }
}
Also used : TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer)

Example 25 with TypeSerializer

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);
}
Also used : TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer)

Aggregations

TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)39 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)28 StreamElementSerializer (org.apache.flink.streaming.runtime.streamrecord.StreamElementSerializer)27 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)27 RichFunction (org.apache.flink.api.common.functions.RichFunction)16 Test (org.junit.Test)13 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)11 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)10 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 PublicEvolving (org.apache.flink.annotation.PublicEvolving)9 KeyedOneInputStreamOperatorTestHarness (org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness)9 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)7 GlobalWindow (org.apache.flink.streaming.api.windowing.windows.GlobalWindow)7 FoldingStateDescriptor (org.apache.flink.api.common.state.FoldingStateDescriptor)6 MergingWindowAssigner (org.apache.flink.streaming.api.windowing.assigners.MergingWindowAssigner)6 InternalIterableAllWindowFunction (org.apache.flink.streaming.runtime.operators.windowing.functions.InternalIterableAllWindowFunction)5 InternalIterableWindowFunction (org.apache.flink.streaming.runtime.operators.windowing.functions.InternalIterableWindowFunction)5 InternalSingleValueAllWindowFunction (org.apache.flink.streaming.runtime.operators.windowing.functions.InternalSingleValueAllWindowFunction)5 InternalSingleValueWindowFunction (org.apache.flink.streaming.runtime.operators.windowing.functions.InternalSingleValueWindowFunction)5