dpcollins-google commented on a change in pull request #11919: URL: https://github.com/apache/beam/pull/11919#discussion_r443574688
########## File path: sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsublite/UuidDeduplicationOptions.java ########## @@ -0,0 +1,77 @@ +/* + * 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.gcp.pubsublite; + +import static com.google.cloud.pubsublite.internal.Preconditions.checkArgument; + +import com.google.auto.value.AutoValue; +import com.google.cloud.pubsublite.SequencedMessage; +import com.google.protobuf.ByteString; +import java.io.Serializable; +import java.util.List; +import org.apache.beam.sdk.state.TimeDomain; +import org.apache.beam.sdk.transforms.Deduplicate; + +@AutoValue +public abstract class UuidDeduplicationOptions implements Serializable { + private static final long serialVersionUID = 9837489720893L; + + public static final SerializableStatusFunction<SequencedMessage, Uuid> DEFAULT_UUID_EXTRACTOR = + message -> { + checkArgument( + message.message().attributes().containsKey(Uuid.DEFAULT_ATTRIBUTE), + "Uuid attribute missing."); + List<ByteString> attributes = + message.message().attributes().get(Uuid.DEFAULT_ATTRIBUTE); + checkArgument(attributes.size() == 1, "Duplicate Uuid attribute values exist."); + return Uuid.of(attributes.get(0)); + }; + + public static final int DEFAULT_HASH_PARTITIONS = 10000; + + // All parameters are optional. + public abstract SerializableStatusFunction<SequencedMessage, Uuid> uuidExtractor(); + + public abstract Deduplicate.KeyedValues<Uuid, SequencedMessage> deduplicate(); + + // The number of partitions to hash values into. + public abstract int hashPartitions(); + + @SuppressWarnings("CheckReturnValue") + public static Builder newBuilder() { + Builder builder = new AutoValue_UuidDeduplicationOptions.Builder(); + builder.setUuidExtractor(DEFAULT_UUID_EXTRACTOR); + builder.setDeduplicate( + Deduplicate.<Uuid, SequencedMessage>keyedValues().withTimeDomain(TimeDomain.EVENT_TIME)); + builder.setHashPartitions(DEFAULT_HASH_PARTITIONS); + return builder; + } + + @AutoValue.Builder + public abstract static class Builder { + public abstract Builder setUuidExtractor( + SerializableStatusFunction<SequencedMessage, Uuid> uuidExtractor); + + public abstract Builder setDeduplicate( + Deduplicate.KeyedValues<Uuid, SequencedMessage> deduplicate); Review comment: I don't really agree with you here. I think Deduplicate already provides a fluent interface for constructing a transform and knows all its properties. I think it mostly seems strange because the builder is wrapped up in the transform itself: if this was DeduplicateKeyedValuesOptions<Uuid, SequencedMessage>, that had a method toTransform(), I don't think it would seem awkward. I can add this, just think it would be duplicated code for no real gain. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
