-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Change TriggerState finished bitset coder to a SentinelBitSetCoder #38139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b24494f
Change TriggerState finished bitset coder to a SentinelBitSetCoder
arunpandianp a4ff2c6
fix style
arunpandianp c55b4cb
fix style
arunpandianp cf5050e
fix style
arunpandianp 848617b
fix style
arunpandianp 493fa8f
fix style
arunpandianp 0d929f1
address comment
arunpandianp 8608f0e
Merge remote-tracking branch 'beam/master' into sentinelbitsetcoder
arunpandianp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...re-java/src/main/java/org/apache/beam/runners/core/serialization/SentinelBitSetCoder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.beam.runners.core.serialization; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.BitSet; | ||
| import org.apache.beam.sdk.coders.AtomicCoder; | ||
| import org.apache.beam.sdk.coders.ByteArrayCoder; | ||
| import org.apache.beam.sdk.coders.CoderException; | ||
|
|
||
| /** | ||
| * Coder for {@link BitSet} that stores an empty bit set as a byte array with a single 0 element. In | ||
| * general BitSetCoder should be preferred as it encodes an empty bit set as an empty byte array. | ||
| * However, there are cases where non-empty values are useful to indicate presence. | ||
| */ | ||
| public class SentinelBitSetCoder extends AtomicCoder<BitSet> { | ||
|
|
||
| private static final SentinelBitSetCoder INSTANCE = new SentinelBitSetCoder(); | ||
| private static final ByteArrayCoder BYTE_ARRAY_CODER = ByteArrayCoder.of(); | ||
|
|
||
| private SentinelBitSetCoder() {} | ||
|
|
||
| public static SentinelBitSetCoder of() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public void encode(BitSet value, OutputStream outStream) throws CoderException, IOException { | ||
| encode(value, outStream, Context.NESTED); | ||
| } | ||
|
|
||
| @Override | ||
| public void encode(BitSet value, OutputStream outStream, Context context) | ||
| throws CoderException, IOException { | ||
| if (value == null) { | ||
| throw new CoderException("cannot encode a null BitSet"); | ||
| } | ||
| byte[] bytes = value.isEmpty() ? new byte[] {0} : value.toByteArray(); | ||
| BYTE_ARRAY_CODER.encodeAndOwn(bytes, outStream, context); | ||
| } | ||
|
|
||
| @Override | ||
| public BitSet decode(InputStream inStream) throws CoderException, IOException { | ||
| return decode(inStream, Context.NESTED); | ||
| } | ||
|
|
||
| @Override | ||
| public BitSet decode(InputStream inStream, Context context) throws CoderException, IOException { | ||
| return BitSet.valueOf(BYTE_ARRAY_CODER.decode(inStream, context)); | ||
| } | ||
|
|
||
| @Override | ||
| public void verifyDeterministic() throws NonDeterministicException { | ||
| verifyDeterministic( | ||
| this, | ||
| "SentinelBitSetCoder requires its ByteArrayCoder to be deterministic.", | ||
| BYTE_ARRAY_CODER); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean consistentWithEquals() { | ||
| return true; | ||
|
arunpandianp marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
...ava/src/test/java/org/apache/beam/runners/core/serialization/SentinelBitSetCoderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.beam.runners.core.serialization; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.BitSet; | ||
| import java.util.List; | ||
| import org.apache.beam.sdk.coders.BitSetCoder; | ||
| import org.apache.beam.sdk.coders.Coder; | ||
| import org.apache.beam.sdk.coders.Coder.Context; | ||
| import org.apache.beam.sdk.coders.CoderException; | ||
| import org.apache.beam.sdk.testing.CoderProperties; | ||
| import org.apache.beam.sdk.util.CoderUtils; | ||
| import org.apache.beam.sdk.values.TypeDescriptor; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.ExpectedException; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| /** Tests for {@link SentinelBitSetCoder}. */ | ||
| @RunWith(JUnit4.class) | ||
| public class SentinelBitSetCoderTest { | ||
|
|
||
| private static final Coder<BitSet> TEST_CODER = SentinelBitSetCoder.of(); | ||
|
|
||
| private static final List<BitSet> TEST_VALUES = | ||
| Arrays.asList( | ||
| BitSet.valueOf(new byte[] {0xa, 0xb, 0xc}), | ||
| BitSet.valueOf(new byte[] {0xd, 0x3}), | ||
| BitSet.valueOf(new byte[] {0xd, 0xe}), | ||
| BitSet.valueOf(new byte[] {0}), | ||
| BitSet.valueOf(new byte[] {})); | ||
|
|
||
| @Test | ||
| public void testDecodeEncodeEquals() throws Exception { | ||
| for (BitSet value : TEST_VALUES) { | ||
| CoderProperties.coderDecodeEncodeEqual(TEST_CODER, value); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testRegisterByteSizeObserver() throws Exception { | ||
| CoderProperties.testByteCount( | ||
| SentinelBitSetCoder.of(), Coder.Context.OUTER, TEST_VALUES.toArray(new BitSet[] {})); | ||
|
|
||
| CoderProperties.testByteCount( | ||
| SentinelBitSetCoder.of(), Coder.Context.NESTED, TEST_VALUES.toArray(new BitSet[] {})); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStructuralValueConsistentWithEquals() throws Exception { | ||
| for (BitSet value1 : TEST_VALUES) { | ||
| for (BitSet value2 : TEST_VALUES) { | ||
| CoderProperties.structuralValueConsistentWithEquals(TEST_CODER, value1, value2); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generated data to check that the wire format has not changed. "CgsM" is {0xa, 0xb, 0xc} "DQM" | ||
| * is {0xd, 0x3} "DQ4" is {0xd, 0xe} "AA==" is {0} (Sentinel for empty BitSet) | ||
| */ | ||
| private static final List<String> TEST_ENCODINGS = | ||
| Arrays.asList("CgsM", "DQM", "DQ4", "AA", "AA"); | ||
|
|
||
| @Test | ||
| public void testWireFormatEncode() throws Exception { | ||
| CoderProperties.coderEncodesBase64(TEST_CODER, TEST_VALUES, TEST_ENCODINGS); | ||
| } | ||
|
|
||
| @Rule public ExpectedException thrown = ExpectedException.none(); | ||
|
|
||
| @Test | ||
| public void encodeNullThrowsCoderException() throws Exception { | ||
| thrown.expect(CoderException.class); | ||
| thrown.expectMessage("cannot encode a null BitSet"); | ||
|
|
||
| CoderUtils.encodeToBase64(TEST_CODER, null); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEncodedTypeDescriptor() throws Exception { | ||
| assertThat(TEST_CODER.getEncodedTypeDescriptor(), equalTo(TypeDescriptor.of(BitSet.class))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyBitSetEncoding() throws Exception { | ||
| { | ||
| byte[] encoded = CoderUtils.encodeToByteArray(TEST_CODER, new BitSet()); | ||
| // ByteArrayCoder in OUTER context encodes as is. | ||
| assertThat(encoded, equalTo(new byte[] {0})); | ||
| } | ||
| { | ||
| byte[] encoded = CoderUtils.encodeToByteArray(TEST_CODER, new BitSet(), Context.NESTED); | ||
| // Varint length = 1, data = 1 | ||
| assertThat(encoded, equalTo(new byte[] {1, 0})); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testCompatibilityWithBitSetCoder() throws Exception { | ||
| BitSetCoder bitSetCoder = BitSetCoder.of(); | ||
| SentinelBitSetCoder sentinelCoder = SentinelBitSetCoder.of(); | ||
|
|
||
| for (BitSet bitset : TEST_VALUES) { | ||
| for (Coder.Context context : Arrays.asList(Coder.Context.OUTER, Coder.Context.NESTED)) { | ||
| // Test SentinelBitSetCoder can decode bytes encoded by BitSetCoder | ||
| { | ||
| byte[] encodedByBitSet = CoderUtils.encodeToByteArray(bitSetCoder, bitset, context); | ||
| BitSet decodedBySentinel = | ||
| CoderUtils.decodeFromByteArray(sentinelCoder, encodedByBitSet, context); | ||
| assertThat( | ||
| "Decoding BitSetCoder encoded value with context " + context, | ||
| decodedBySentinel, | ||
| equalTo(bitset)); | ||
| } | ||
|
|
||
| // Test BitSetCoder can decode bytes encoded by SentinelBitSetCoder | ||
| { | ||
| byte[] encodedBySentinel = CoderUtils.encodeToByteArray(sentinelCoder, bitset, context); | ||
| BitSet decodedByBitSet = | ||
| CoderUtils.decodeFromByteArray(bitSetCoder, encodedBySentinel, context); | ||
| assertThat( | ||
| "Decoding SentinelBitSetCoder encoded value with context " + context, | ||
| decodedByBitSet, | ||
| equalTo(bitset)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.