lukecwik commented on a change in pull request #13069:
URL: https://github.com/apache/beam/pull/13069#discussion_r510437586



##########
File path: model/pipeline/src/main/proto/beam_runner_api.proto
##########
@@ -879,6 +879,28 @@ message StandardCoders {
     // Components: None
     // Experimental.
     ROW = 13 [(beam_urn) = "beam:coder:row:v1"];
+
+    // Encodes a use key and a shard id which is an opaque byte string.

Review comment:
       ```suggestion
       // Encodes a user key and a shard id which is an opaque byte string.
   ```

##########
File path: model/pipeline/src/main/proto/beam_runner_api.proto
##########
@@ -879,6 +879,28 @@ message StandardCoders {
     // Components: None
     // Experimental.
     ROW = 13 [(beam_urn) = "beam:coder:row:v1"];
+
+    // Encodes a use key and a shard id which is an opaque byte string.
+    //
+    // The encoding for a sharded key consists of a shard id byte string and 
the
+    // encoded user key in the following order:
+    //
+    //     - length prefixed shard id using beam:coder:bytes:v1

Review comment:
       ```suggestion
       //     - bytes shard id using beam:coder:bytes:v1
   ```

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/util/ShardedKey.java
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.util;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import org.apache.beam.sdk.coders.ByteArrayCoder;
+import org.apache.beam.sdk.coders.StructuredCoder;
+import org.apache.beam.sdk.util.common.ElementByteSizeObserver;
+
+/** A sharded key consisting of a user key and an opaque shard id represented 
by bytes. */
+public class ShardedKey<K> {
+  private static final byte[] EMPTY_SHARD_ID = new byte[0];
+
+  private final K key;
+  private final byte[] shardId;
+
+  protected ShardedKey(K key, byte[] shardId) {
+    this.key = key;
+    this.shardId = shardId;
+  }
+
+  /** Creates a ShardedKey with given key and shard id. Shard id must not be 
null. */
+  public static <K> ShardedKey<K> of(K key, byte[] shardId) {
+    checkArgument(key != null, "Key should not be null!");
+    checkArgument(shardId != null, "Shard id should not be null!");
+    return new ShardedKey<K>(key, shardId);
+  }
+
+  public K getKey() {
+    return key;
+  }
+
+  @Override
+  public String toString() {
+    return "ShardedKey{" + "key=" + key + ", shardId=" + 
Arrays.toString(shardId) + "}";
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (o == this) {
+      return true;
+    }
+    if (o instanceof ShardedKey) {
+      ShardedKey<?> that = (ShardedKey<?>) o;
+      return this.key.equals(that.key) && Arrays.equals(this.shardId, 
that.shardId);
+    }
+    return false;
+  }
+
+  @Override
+  public int hashCode() {
+    int h$ = 1;

Review comment:
       rename `h$` to `hash`

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/util/ShardedKey.java
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.util;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import org.apache.beam.sdk.coders.ByteArrayCoder;
+import org.apache.beam.sdk.coders.StructuredCoder;
+import org.apache.beam.sdk.util.common.ElementByteSizeObserver;
+
+/** A sharded key consisting of a user key and an opaque shard id represented 
by bytes. */
+public class ShardedKey<K> {
+  private static final byte[] EMPTY_SHARD_ID = new byte[0];
+
+  private final K key;
+  private final byte[] shardId;
+
+  protected ShardedKey(K key, byte[] shardId) {
+    this.key = key;
+    this.shardId = shardId;
+  }
+
+  /** Creates a ShardedKey with given key and shard id. Shard id must not be 
null. */
+  public static <K> ShardedKey<K> of(K key, byte[] shardId) {
+    checkArgument(key != null, "Key should not be null!");
+    checkArgument(shardId != null, "Shard id should not be null!");
+    return new ShardedKey<K>(key, shardId);
+  }
+
+  public K getKey() {
+    return key;
+  }
+
+  @Override
+  public String toString() {
+    return "ShardedKey{" + "key=" + key + ", shardId=" + 
Arrays.toString(shardId) + "}";
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (o == this) {
+      return true;
+    }
+    if (o instanceof ShardedKey) {
+      ShardedKey<?> that = (ShardedKey<?>) o;
+      return this.key.equals(that.key) && Arrays.equals(this.shardId, 
that.shardId);
+    }
+    return false;
+  }
+
+  @Override
+  public int hashCode() {
+    int h$ = 1;
+    h$ *= 1000003;
+    h$ ^= key.hashCode();
+    h$ *= 1000003;
+    h$ ^= Arrays.hashCode(shardId);
+    return h$;
+  }
+
+  public static class Coder<K> extends StructuredCoder<ShardedKey<K>> {
+
+    private final ByteArrayCoder shardCoder = ByteArrayCoder.of();
+    private final org.apache.beam.sdk.coders.Coder<K> keyCoder;
+
+    private Coder(org.apache.beam.sdk.coders.Coder<K> coder) {
+      keyCoder = coder;
+    }
+
+    public static <K> ShardedKey.Coder<K> 
of(org.apache.beam.sdk.coders.Coder<K> keyCoder) {
+      return new ShardedKey.Coder<K>(keyCoder);
+    }
+
+    public org.apache.beam.sdk.coders.Coder<K> getKeyCoder() {
+      return keyCoder;
+    }
+
+    @Override
+    public void encode(ShardedKey<K> shardedKey, OutputStream outStream) 
throws IOException {
+      // The encoding should follow the order:
+      //   length prefixed shard id byte string

Review comment:
       ```suggestion
         //   shard id byte string
   ```

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/util/ShardedKey.java
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.util;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import org.apache.beam.sdk.coders.ByteArrayCoder;
+import org.apache.beam.sdk.coders.StructuredCoder;
+import org.apache.beam.sdk.util.common.ElementByteSizeObserver;
+
+/** A sharded key consisting of a user key and an opaque shard id represented 
by bytes. */
+public class ShardedKey<K> {
+  private static final byte[] EMPTY_SHARD_ID = new byte[0];
+
+  private final K key;
+  private final byte[] shardId;
+
+  protected ShardedKey(K key, byte[] shardId) {
+    this.key = key;
+    this.shardId = shardId;
+  }
+
+  /** Creates a ShardedKey with given key and shard id. Shard id must not be 
null. */
+  public static <K> ShardedKey<K> of(K key, byte[] shardId) {
+    checkArgument(key != null, "Key should not be null!");
+    checkArgument(shardId != null, "Shard id should not be null!");
+    return new ShardedKey<K>(key, shardId);
+  }
+
+  public K getKey() {
+    return key;
+  }
+
+  @Override
+  public String toString() {
+    return "ShardedKey{" + "key=" + key + ", shardId=" + 
Arrays.toString(shardId) + "}";

Review comment:
       ```suggestion
       return "ShardedKey{key=" + key + ", shardId=" + Arrays.toString(shardId) 
+ "}";
   ```

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/util/ShardedKey.java
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.util;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import org.apache.beam.sdk.coders.ByteArrayCoder;
+import org.apache.beam.sdk.coders.StructuredCoder;
+import org.apache.beam.sdk.util.common.ElementByteSizeObserver;
+
+/** A sharded key consisting of a user key and an opaque shard id represented 
by bytes. */
+public class ShardedKey<K> {
+  private static final byte[] EMPTY_SHARD_ID = new byte[0];
+
+  private final K key;
+  private final byte[] shardId;
+
+  protected ShardedKey(K key, byte[] shardId) {
+    this.key = key;
+    this.shardId = shardId;
+  }
+
+  /** Creates a ShardedKey with given key and shard id. Shard id must not be 
null. */

Review comment:
       ```suggestion
     /** Creates a ShardedKey with given key and shard id. Shard id must not be 
null and must not be mutated. */
   ```

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/util/ShardedKey.java
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.util;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import org.apache.beam.sdk.coders.ByteArrayCoder;
+import org.apache.beam.sdk.coders.StructuredCoder;
+import org.apache.beam.sdk.util.common.ElementByteSizeObserver;
+
+/** A sharded key consisting of a user key and an opaque shard id represented 
by bytes. */
+public class ShardedKey<K> {
+  private static final byte[] EMPTY_SHARD_ID = new byte[0];
+
+  private final K key;
+  private final byte[] shardId;
+
+  protected ShardedKey(K key, byte[] shardId) {
+    this.key = key;
+    this.shardId = shardId;
+  }
+
+  /** Creates a ShardedKey with given key and shard id. Shard id must not be 
null. */
+  public static <K> ShardedKey<K> of(K key, byte[] shardId) {
+    checkArgument(key != null, "Key should not be null!");
+    checkArgument(shardId != null, "Shard id should not be null!");
+    return new ShardedKey<K>(key, shardId);
+  }
+
+  public K getKey() {
+    return key;
+  }
+
+  @Override
+  public String toString() {
+    return "ShardedKey{" + "key=" + key + ", shardId=" + 
Arrays.toString(shardId) + "}";
+  }
+
+  @Override
+  public boolean equals(Object o) {

Review comment:
       FYI, this equals method assumes the key type has a valid equals method 
and generally shouldn't be used. We should use structuralValue anywhere for 
comparisons.




----------------------------------------------------------------
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]


Reply via email to