-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[BEAM-13640][BEAM-12883] Add MetadataDynamicCoder to support encode-decode for new fields in Metatdata #15699
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
Closed
brachi-wernick
wants to merge
14
commits into
apache:master
from
brachi-wernick:add-metadata-dynamic-coder
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d2e5106
add coder readable files
058400d
add coder readable files
25b02e0
Merge remote-tracking branch 'origin/add-coder-readable-file-v2' into…
4dec390
:sdks:java:core:spotlessApply
a224a66
[BEAM-12883] add ability tp set custom MetadataCoder for ReadableFile…
1e3e452
[BEAM-12883] add ability tp set custom MetadataCoder for ReadableFile…
6cdddfe
[BEAM-12883] add ability t0 set custom MetadataCoder using Structured…
6c0906a
[BEAM-12883] remove asterisk-based import
d9f6f00
[BEAM-12883] add ability for MetadataCoder to support encode-decode f…
6f20c30
Merge branch 'apache:master' into add-metadata-dynamic-coder
brachi-wernick 420d4aa
[BEAM-13640][BEAM-12883] extract field coder needed stuff to a diff c…
9082b31
Merge branch 'master' of https://github.com/brachi-wernick/beam into …
f26c9e8
[BEAM-13640][BEAM-12883] fix
35d806b
[BEAM-13640][BEAM-12883] fix error `initialization.fields.uninitialized`
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
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
sdks/java/core/src/main/java/org/apache/beam/sdk/io/fs/MetadataDynamicCoder.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,96 @@ | ||
| /* | ||
| * 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.sdk.io.fs; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.BiConsumer; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.beam.sdk.coders.Coder; | ||
| import org.apache.beam.sdk.coders.StructuredCoder; | ||
| import org.apache.beam.sdk.transforms.SerializableBiConsumer; | ||
| import org.apache.beam.sdk.transforms.SerializableFunction; | ||
|
|
||
| public class MetadataDynamicCoder extends StructuredCoder<MatchResult.Metadata> { | ||
|
|
||
| private static final MetadataCoder V1_CODER = MetadataCoder.of(); | ||
|
|
||
| private List<MetadataFieldCoderDescription> fieldCoders = new ArrayList<>(); | ||
|
|
||
| public MetadataDynamicCoder() {} | ||
|
|
||
| public <T> MetadataDynamicCoder withCoderForField( | ||
| Coder<T> coder, | ||
| SerializableFunction<? super MatchResult.Metadata, T> getter, | ||
| SerializableBiConsumer<? super MatchResult.Metadata.Builder, T> setter) { | ||
| MetadataFieldCoderDescription metadataFieldCoderDescription = | ||
| new MetadataFieldCoderDescription(coder, getter, setter); | ||
| fieldCoders.add(metadataFieldCoderDescription); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public void encode(MatchResult.Metadata metadata, OutputStream outStream) throws IOException { | ||
| V1_CODER.encode(metadata, outStream); | ||
| for (MetadataFieldCoderDescription fieldCoderDescription : fieldCoders) { | ||
| SerializableFunction<? super MatchResult.Metadata, ?> getter = | ||
| fieldCoderDescription.getGetter(); | ||
| Coder coder = fieldCoderDescription.getCoder(); | ||
| try { | ||
| coder.encode(getter.apply(metadata), outStream); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException( | ||
| "Failed to encode " + getter + " with coder " + coder.getClass()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public MatchResult.Metadata decode(InputStream inStream) throws IOException { | ||
| MatchResult.Metadata.Builder builder = V1_CODER.decodeBuilder(inStream); | ||
|
|
||
| for (MetadataFieldCoderDescription metadataFieldCoderDescription : fieldCoders) { | ||
| Coder coder = metadataFieldCoderDescription.getCoder(); | ||
| BiConsumer setter = metadataFieldCoderDescription.getSetter(); | ||
|
|
||
| try { | ||
| setter.accept(builder, coder.decode(inStream)); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to decode with coder " + coder.getClass()); | ||
| } | ||
| } | ||
| return builder.build(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<? extends Coder<?>> getCoderArguments() { | ||
| return fieldCoders.stream() | ||
| .map(MetadataFieldCoderDescription::getCoder) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| @Override | ||
| public void verifyDeterministic() throws NonDeterministicException { | ||
| for (Coder<?> coder : getCoderArguments()) { | ||
| verifyDeterministic(this, "Coder must be deterministic " + coder.getClass(), coder); | ||
| } | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
sdks/java/core/src/main/java/org/apache/beam/sdk/io/fs/MetadataFieldCoderDescription.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,63 @@ | ||
| /* | ||
| * 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.sdk.io.fs; | ||
|
|
||
| import java.io.Serializable; | ||
| import org.apache.beam.sdk.coders.Coder; | ||
| import org.apache.beam.sdk.transforms.SerializableBiConsumer; | ||
| import org.apache.beam.sdk.transforms.SerializableFunction; | ||
|
|
||
| public class MetadataFieldCoderDescription implements Serializable { | ||
|
|
||
| private Coder<?> coder; | ||
| private SerializableFunction<? super MatchResult.Metadata, ?> getter; | ||
| private SerializableBiConsumer<? super MatchResult.Metadata.Builder, ?> setter; | ||
|
|
||
| public MetadataFieldCoderDescription( | ||
| Coder<?> coder, | ||
| SerializableFunction<? super MatchResult.Metadata, ?> getter, | ||
| SerializableBiConsumer<? super MatchResult.Metadata.Builder, ?> setter) { | ||
| this.coder = coder; | ||
| this.getter = getter; | ||
| this.setter = setter; | ||
| } | ||
|
|
||
| public Coder<?> getCoder() { | ||
| return coder; | ||
| } | ||
|
|
||
| public void setCoder(Coder<?> coder) { | ||
| this.coder = coder; | ||
| } | ||
|
|
||
| public SerializableFunction<? super MatchResult.Metadata, ?> getGetter() { | ||
| return getter; | ||
| } | ||
|
|
||
| public void setGetter(SerializableFunction<? super MatchResult.Metadata, ?> getter) { | ||
| this.getter = getter; | ||
| } | ||
|
|
||
| public SerializableBiConsumer<? super MatchResult.Metadata.Builder, ?> getSetter() { | ||
| return setter; | ||
| } | ||
|
|
||
| public void setSetter(SerializableBiConsumer<? super MatchResult.Metadata.Builder, ?> setter) { | ||
| this.setter = setter; | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
sdks/java/core/src/test/java/org/apache/beam/sdk/io/fs/MetadataDynamicCoderTest.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,78 @@ | ||
| /* | ||
| * 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.sdk.io.fs; | ||
|
|
||
| import java.nio.file.Path; | ||
| import org.apache.beam.sdk.coders.VarLongCoder; | ||
| import org.apache.beam.sdk.io.FileSystems; | ||
| import org.apache.beam.sdk.io.fs.MatchResult.Metadata; | ||
| import org.apache.beam.sdk.testing.CoderProperties; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
|
|
||
| /** Tests for {@link org.apache.beam.sdk.io.fs.MetadataDynamicCoder}. */ | ||
| public class MetadataDynamicCoderTest { | ||
|
|
||
| @Rule public transient TemporaryFolder tmpFolder = new TemporaryFolder(); | ||
|
|
||
| @Test | ||
| public void testEncodeDecodeWithDefaultLastModifiedMills() throws Exception { | ||
| Path filePath = tmpFolder.newFile("somefile").toPath(); | ||
| Metadata metadata = | ||
| Metadata.builder() | ||
| .setResourceId( | ||
| FileSystems.matchNewResource(filePath.toString(), false /* isDirectory */)) | ||
| .setIsReadSeekEfficient(true) | ||
| .setSizeBytes(1024) | ||
| .build(); | ||
|
|
||
| MetadataDynamicCoder metadataCoderDynamic = getMetadataDynamicCoder(); | ||
|
|
||
| CoderProperties.coderDecodeEncodeEqual(metadataCoderDynamic, metadata); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEncodeDecodeWithCustomLastModifiedMills() throws Exception { | ||
| Path filePath = tmpFolder.newFile("somefile").toPath(); | ||
| Metadata metadata = | ||
| Metadata.builder() | ||
| .setResourceId( | ||
| FileSystems.matchNewResource(filePath.toString(), false /* isDirectory */)) | ||
| .setIsReadSeekEfficient(true) | ||
| .setSizeBytes(1024) | ||
| .setLastModifiedMillis(1541097000L) | ||
| .build(); | ||
| MetadataDynamicCoder metadataCoderDynamic = getMetadataDynamicCoder(); | ||
|
|
||
| CoderProperties.coderDecodeEncodeEqual(metadataCoderDynamic, metadata); | ||
| } | ||
|
|
||
| private MetadataDynamicCoder getMetadataDynamicCoder() { | ||
| return new MetadataDynamicCoder() | ||
| .withCoderForField( | ||
| VarLongCoder.of(), | ||
| Metadata::lastModifiedMillis, | ||
| Metadata.Builder::setLastModifiedMillis); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCoderSerializable() { | ||
| CoderProperties.coderSerializable(getMetadataDynamicCoder()); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to add a test that uses two dynamic coders, so we can verify that adding multiple coders stack as expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kind problematic, since all fields now in Metadata are handled. except the one that I used in the test.
It can work if I won't use the basic
MetadataCoderin this dynamic coder, and work only with the fields coders I get in the list, But I think it will be mess to developers to assign all these basic fields for the dynamic coder, it is easy now, that they need to send only new/special fields and not all the basic fields.(coder now first does :
MatchResult.Metadata.Builder builder = V1_CODER.decodeBuilder(inStream);which covers most of the fields in Metadata. and new fields will be covered with this coder list of {coder,getter,setter})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good