Search in sources :

Example 1 with EitherTypeInfo

use of org.apache.flink.api.java.typeutils.EitherTypeInfo in project flink by apache.

the class EitherSerializerTest method testSerializeIndividually.

@Test
public void testSerializeIndividually() throws IOException {
    EitherTypeInfo<LongValue, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>(ValueTypeInfo.LONG_VALUE_TYPE_INFO, ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO);
    EitherSerializer<LongValue, DoubleValue> eitherSerializer = (EitherSerializer<LongValue, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig());
    LongValue lv = new LongValue();
    DoubleValue dv = new DoubleValue();
    Either<LongValue, DoubleValue> left = Left(lv);
    Either<LongValue, DoubleValue> right = Right(dv);
    TestOutputView out = new TestOutputView();
    eitherSerializer.serialize(left, out);
    eitherSerializer.serialize(right, out);
    eitherSerializer.serialize(left, out);
    TestInputView in = out.getInputView();
    // the first deserialization creates a new instance of Left
    Either<LongValue, DoubleValue> copy0 = eitherSerializer.deserialize(right, in);
    // then the cross-references are used for future copies
    Either<LongValue, DoubleValue> copy1 = eitherSerializer.deserialize(copy0, in);
    Either<LongValue, DoubleValue> copy2 = eitherSerializer.deserialize(copy1, in);
    // validate reference equality
    assertSame(right, copy1);
    assertSame(copy0, copy2);
    // validate reference equality of contained objects
    assertSame(right.right(), copy1.right());
    assertSame(copy0.left(), copy2.left());
}
Also used : TestOutputView(org.apache.flink.api.common.typeutils.ComparatorTestBase.TestOutputView) TestInputView(org.apache.flink.api.common.typeutils.ComparatorTestBase.TestInputView) DoubleValue(org.apache.flink.types.DoubleValue) LongValue(org.apache.flink.types.LongValue) EitherTypeInfo(org.apache.flink.api.java.typeutils.EitherTypeInfo) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Test(org.junit.Test)

Example 2 with EitherTypeInfo

use of org.apache.flink.api.java.typeutils.EitherTypeInfo in project flink by apache.

the class EitherSerializerTest method testStringValueDoubleValueEither.

@Test
public void testStringValueDoubleValueEither() {
    @SuppressWarnings("unchecked") Either<StringValue, DoubleValue>[] testData = new Either[] { Left(new StringValue("banana")), Left.of(new StringValue("apple")), new Left(new StringValue("")), Right(new DoubleValue(32.0)), Right.of(new DoubleValue(Double.MIN_VALUE)), new Right(new DoubleValue(Double.MAX_VALUE)) };
    EitherTypeInfo<StringValue, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>(ValueTypeInfo.STRING_VALUE_TYPE_INFO, ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO);
    EitherSerializer<StringValue, DoubleValue> eitherSerializer = (EitherSerializer<StringValue, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig());
    SerializerTestInstance<Either<StringValue, DoubleValue>> testInstance = new EitherSerializerTestInstance<>(eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData);
    testInstance.testAll();
}
Also used : Left(org.apache.flink.types.Either.Left) Right(org.apache.flink.types.Either.Right) EitherTypeInfo(org.apache.flink.api.java.typeutils.EitherTypeInfo) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) DoubleValue(org.apache.flink.types.DoubleValue) Either(org.apache.flink.types.Either) StringValue(org.apache.flink.types.StringValue) Test(org.junit.Test)

Example 3 with EitherTypeInfo

use of org.apache.flink.api.java.typeutils.EitherTypeInfo in project flink by apache.

the class EitherSerializerTest method testEitherWithTupleValues.

@Test
public void testEitherWithTupleValues() {
    @SuppressWarnings("unchecked") Either<Tuple2<LongValue, LongValue>, DoubleValue>[] testData = new Either[] { Left(new Tuple2<>(new LongValue(2L), new LongValue(9L))), new Left<>(new Tuple2<>(new LongValue(Long.MIN_VALUE), new LongValue(Long.MAX_VALUE))), new Right<>(new DoubleValue(32.0)), Right(new DoubleValue(Double.MIN_VALUE)), Right(new DoubleValue(Double.MAX_VALUE)) };
    EitherTypeInfo<Tuple2<LongValue, LongValue>, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>(new TupleTypeInfo<Tuple2<LongValue, LongValue>>(ValueTypeInfo.LONG_VALUE_TYPE_INFO, ValueTypeInfo.LONG_VALUE_TYPE_INFO), ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO);
    EitherSerializer<Tuple2<LongValue, LongValue>, DoubleValue> eitherSerializer = (EitherSerializer<Tuple2<LongValue, LongValue>, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig());
    SerializerTestInstance<Either<Tuple2<LongValue, LongValue>, DoubleValue>> testInstance = new EitherSerializerTestInstance<>(eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData);
    testInstance.testAll();
}
Also used : Left(org.apache.flink.types.Either.Left) Right(org.apache.flink.types.Either.Right) EitherTypeInfo(org.apache.flink.api.java.typeutils.EitherTypeInfo) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) DoubleValue(org.apache.flink.types.DoubleValue) Tuple2(org.apache.flink.api.java.tuple.Tuple2) LongValue(org.apache.flink.types.LongValue) Either(org.apache.flink.types.Either) Test(org.junit.Test)

Example 4 with EitherTypeInfo

use of org.apache.flink.api.java.typeutils.EitherTypeInfo in project flink by apache.

the class EitherSerializerTest method testEitherWithObjectReuse.

@Test
public void testEitherWithObjectReuse() {
    EitherTypeInfo<LongValue, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>(ValueTypeInfo.LONG_VALUE_TYPE_INFO, ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO);
    EitherSerializer<LongValue, DoubleValue> eitherSerializer = (EitherSerializer<LongValue, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig());
    LongValue lv = new LongValue();
    DoubleValue dv = new DoubleValue();
    Either<LongValue, DoubleValue> left = Left(lv);
    Either<LongValue, DoubleValue> right = Right(dv);
    // the first copy creates a new instance of Left
    Either<LongValue, DoubleValue> copy0 = eitherSerializer.copy(left, right);
    // then the cross-references are used for future copies
    Either<LongValue, DoubleValue> copy1 = eitherSerializer.copy(right, copy0);
    Either<LongValue, DoubleValue> copy2 = eitherSerializer.copy(left, copy1);
    // validate reference equality
    assertSame(right, copy1);
    assertSame(copy0, copy2);
    // validate reference equality of contained objects
    assertSame(right.right(), copy1.right());
    assertSame(copy0.left(), copy2.left());
}
Also used : DoubleValue(org.apache.flink.types.DoubleValue) LongValue(org.apache.flink.types.LongValue) EitherTypeInfo(org.apache.flink.api.java.typeutils.EitherTypeInfo) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Test(org.junit.Test)

Aggregations

ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)4 EitherTypeInfo (org.apache.flink.api.java.typeutils.EitherTypeInfo)4 DoubleValue (org.apache.flink.types.DoubleValue)4 Test (org.junit.Test)4 LongValue (org.apache.flink.types.LongValue)3 Either (org.apache.flink.types.Either)2 Left (org.apache.flink.types.Either.Left)2 Right (org.apache.flink.types.Either.Right)2 TestInputView (org.apache.flink.api.common.typeutils.ComparatorTestBase.TestInputView)1 TestOutputView (org.apache.flink.api.common.typeutils.ComparatorTestBase.TestOutputView)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 StringValue (org.apache.flink.types.StringValue)1