Java Code Examples for org.apache.beam.sdk.transforms.Combine#BinaryCombineIntegerFn

The following examples show how to use org.apache.beam.sdk.transforms.Combine#BinaryCombineIntegerFn . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StateTagTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testCombiningValueEquality() {
  Combine.BinaryCombineIntegerFn maxFn = Max.ofIntegers();
  Coder<Integer> input1 = VarIntCoder.of();
  Coder<Integer> input2 = BigEndianIntegerCoder.of();
  Combine.BinaryCombineIntegerFn minFn = Min.ofIntegers();

  StateTag<?> fooCoder1Max1 = StateTags.combiningValueFromInputInternal("foo", input1, maxFn);
  StateTag<?> fooCoder1Max2 = StateTags.combiningValueFromInputInternal("foo", input1, maxFn);
  StateTag<?> fooCoder1Min = StateTags.combiningValueFromInputInternal("foo", input1, minFn);

  StateTag<?> fooCoder2Max = StateTags.combiningValueFromInputInternal("foo", input2, maxFn);
  StateTag<?> barCoder1Max = StateTags.combiningValueFromInputInternal("bar", input1, maxFn);

  // Same name, coder and combineFn
  assertEquals(fooCoder1Max1, fooCoder1Max2);
  assertEquals(
      StateTags.convertToBagTagInternal((StateTag) fooCoder1Max1),
      StateTags.convertToBagTagInternal((StateTag) fooCoder1Max2));

  // Different combineFn, but we treat them as equal since we only serialize the bits.
  assertEquals(fooCoder1Max1, fooCoder1Min);
  assertEquals(
      StateTags.convertToBagTagInternal((StateTag) fooCoder1Max1),
      StateTags.convertToBagTagInternal((StateTag) fooCoder1Min));

  // Different input coder coder.
  assertEquals(fooCoder1Max1, fooCoder2Max);
  assertEquals(
      StateTags.convertToBagTagInternal((StateTag) fooCoder1Max1),
      StateTags.convertToBagTagInternal((StateTag) fooCoder2Max));

  // These StateTags have different IDs.
  assertNotEquals(fooCoder1Max1, barCoder1Max);
  assertNotEquals(
      StateTags.convertToBagTagInternal((StateTag) fooCoder1Max1),
      StateTags.convertToBagTagInternal((StateTag) barCoder1Max));
}
 
Example 2
Source File: StateTagTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testCombiningValueWithContextEquality() {
  CoderRegistry registry = CoderRegistry.createDefault();

  Combine.BinaryCombineIntegerFn maxFn = Max.ofIntegers();
  Combine.BinaryCombineIntegerFn minFn = Min.ofIntegers();

  Coder<int[]> accum1 = maxFn.getAccumulatorCoder(registry, VarIntCoder.of());
  Coder<int[]> accum2 = minFn.getAccumulatorCoder(registry, BigEndianIntegerCoder.of());

  StateTag<?> fooCoder1Max1 =
      StateTags.combiningValueWithContext("foo", accum1, CombineFnUtil.toFnWithContext(maxFn));
  StateTag<?> fooCoder1Max2 =
      StateTags.combiningValueWithContext("foo", accum1, CombineFnUtil.toFnWithContext(maxFn));
  StateTag<?> fooCoder1Min =
      StateTags.combiningValueWithContext("foo", accum1, CombineFnUtil.toFnWithContext(minFn));

  StateTag<?> fooCoder2Max =
      StateTags.combiningValueWithContext("foo", accum2, CombineFnUtil.toFnWithContext(maxFn));
  StateTag<?> barCoder1Max =
      StateTags.combiningValueWithContext("bar", accum1, CombineFnUtil.toFnWithContext(maxFn));

  // Same name, coder and combineFn
  assertEquals(fooCoder1Max1, fooCoder1Max2);
  assertEquals(
      StateTags.convertToBagTagInternal((StateTag) fooCoder1Max1),
      StateTags.convertToBagTagInternal((StateTag) fooCoder1Max2));
  // Different combineFn, but we treat them as equal since we only serialize the bits.
  assertEquals(fooCoder1Max1, fooCoder1Min);
  assertEquals(
      StateTags.convertToBagTagInternal((StateTag) fooCoder1Max1),
      StateTags.convertToBagTagInternal((StateTag) fooCoder1Min));

  // Different input coder coder.
  assertEquals(fooCoder1Max1, fooCoder2Max);
  assertEquals(
      StateTags.convertToBagTagInternal((StateTag) fooCoder1Max1),
      StateTags.convertToBagTagInternal((StateTag) fooCoder2Max));

  // These StateTags have different IDs.
  assertNotEquals(fooCoder1Max1, barCoder1Max);
  assertNotEquals(
      StateTags.convertToBagTagInternal((StateTag) fooCoder1Max1),
      StateTags.convertToBagTagInternal((StateTag) barCoder1Max));
}