Skip to content

Commit

Permalink
[FLINK-11073] [core] Let ScalaEitherSerializerSnapshot be a Composite…
Browse files Browse the repository at this point in the history
…TypeSerializerSnapshot
  • Loading branch information
tzulitai committed Jan 8, 2019
1 parent 5e6664d commit 68fab12
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 68fab12

Please sign in to comment.