提交 68fab127 编写于 作者: T Tzu-Li (Gordon) Tai

[FLINK-11073] [core] Let ScalaEitherSerializerSnapshot be a CompositeTypeSerializerSnapshot

上级 5e6664df
......@@ -18,81 +18,51 @@
package org.apache.flink.api.scala.typeutils;
import org.apache.flink.api.common.typeutils.CompositeSerializerSnapshot;
import org.apache.flink.api.common.typeutils.CompositeTypeSerializerSnapshot;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility;
import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
import org.apache.flink.core.memory.DataInputView;
import org.apache.flink.core.memory.DataOutputView;
import org.apache.flink.util.Preconditions;
import java.io.IOException;
import scala.util.Either;
import static org.apache.flink.util.Preconditions.checkState;
/**
* Configuration snapshot for serializers of Scala's {@link Either} type,
* containing configuration snapshots of the Left and Right serializers.
*/
public class ScalaEitherSerializerSnapshot<L, R> implements TypeSerializerSnapshot<Either<L, R>> {
public class ScalaEitherSerializerSnapshot<L, R> extends CompositeTypeSerializerSnapshot<Either<L, R>, EitherSerializer> {
private static final int CURRENT_VERSION = 1;
private CompositeSerializerSnapshot nestedLeftRightSerializerSnapshot;
/**
* Constructor for read instantiation.
*/
public ScalaEitherSerializerSnapshot() {}
public ScalaEitherSerializerSnapshot() {
super(EitherSerializer.class);
}
/**
* Constructor to create the snapshot for writing.
*/
public ScalaEitherSerializerSnapshot(TypeSerializer<L> leftSerializer, TypeSerializer<R> rightSerializer) {
Preconditions.checkNotNull(leftSerializer);
Preconditions.checkNotNull(rightSerializer);
this.nestedLeftRightSerializerSnapshot = new CompositeSerializerSnapshot(leftSerializer, rightSerializer);
public ScalaEitherSerializerSnapshot(EitherSerializer<L, R> eitherSerializer) {
super(eitherSerializer);
}
@Override
public int getCurrentVersion() {
public int getCurrentOuterSnapshotVersion() {
return CURRENT_VERSION;
}
@Override
public TypeSerializer<Either<L, R>> restoreSerializer() {
return new EitherSerializer<>(
nestedLeftRightSerializerSnapshot.getRestoreSerializer(0),
nestedLeftRightSerializerSnapshot.getRestoreSerializer(1));
}
@Override
public TypeSerializerSchemaCompatibility<Either<L, R>> resolveSchemaCompatibility(
TypeSerializer<Either<L, R>> newSerializer) {
checkState(nestedLeftRightSerializerSnapshot != null);
protected EitherSerializer createOuterSerializerWithNestedSerializers(TypeSerializer<?>[] nestedSerializers) {
@SuppressWarnings("unchecked")
TypeSerializer<L> leftSerializer = (TypeSerializer<L>) nestedSerializers[0];
if (newSerializer instanceof EitherSerializer) {
EitherSerializer<L, R> serializer = (EitherSerializer<L, R>) newSerializer;
@SuppressWarnings("unchecked")
TypeSerializer<R> rightSerializer = (TypeSerializer<R>) nestedSerializers[1];
return nestedLeftRightSerializerSnapshot.resolveCompatibilityWithNested(
TypeSerializerSchemaCompatibility.compatibleAsIs(),
serializer.getLeftSerializer(),
serializer.getRightSerializer());
}
else {
return TypeSerializerSchemaCompatibility.incompatible();
}
}
@Override
public void writeSnapshot(DataOutputView out) throws IOException {
nestedLeftRightSerializerSnapshot.writeCompositeSnapshot(out);
return new EitherSerializer<>(leftSerializer, rightSerializer);
}
@Override
public void readSnapshot(int readVersion, DataInputView in, ClassLoader userCodeClassLoader) throws IOException {
this.nestedLeftRightSerializerSnapshot = CompositeSerializerSnapshot.readCompositeSnapshot(in, userCodeClassLoader);
protected TypeSerializer<?>[] getNestedSerializers(EitherSerializer outerSerializer) {
return new TypeSerializer<?>[] { outerSerializer.getLeftSerializer(), outerSerializer.getRightSerializer() };
}
}
......@@ -125,7 +125,7 @@ class EitherSerializer[A, B](
// --------------------------------------------------------------------------------------------
override def snapshotConfiguration(): ScalaEitherSerializerSnapshot[A, B] = {
new ScalaEitherSerializerSnapshot[A, B](leftSerializer, rightSerializer)
new ScalaEitherSerializerSnapshot[A, B](this)
}
override def ensureCompatibility(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册