WencongLiu commented on code in PR #24398:
URL: https://github.com/apache/flink/pull/24398#discussion_r1520976445


##########
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/sortpartition/FixedLengthByteKeyAndValueComparator.java:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.flink.streaming.api.operators.sortpartition;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeutils.TypeComparator;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.MemorySegment;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+/**
+ * The {@link FixedLengthByteKeyAndValueComparator} is used by {@link 
KeyedSortPartitionOperator} to
+ * compare records according to both the record key and record value. The 
length of record key must
+ * be fixed and will be initialized when the {@link 
FixedLengthByteKeyAndValueComparator} is
+ * created.
+ */
+@Internal
+public class FixedLengthByteKeyAndValueComparator<INPUT>
+        extends TypeComparator<Tuple2<byte[], INPUT>> {
+
+    private final int serializedKeyLength;
+
+    private final TypeComparator<INPUT> valueComparator;
+
+    private byte[] keyReference;
+
+    private INPUT valueReference;
+
+    FixedLengthByteKeyAndValueComparator(
+            int serializedKeyLength, TypeComparator<INPUT> valueComparator) {
+        this.serializedKeyLength = serializedKeyLength;
+        this.valueComparator = valueComparator;
+    }
+
+    @Override
+    public int hash(Tuple2<byte[], INPUT> record) {
+        return record.hashCode();
+    }
+
+    @Override
+    public void setReference(Tuple2<byte[], INPUT> toCompare) {
+        this.keyReference = toCompare.f0;
+        this.valueReference = toCompare.f1;
+    }
+
+    @Override
+    public boolean equalToReference(Tuple2<byte[], INPUT> candidate) {
+        return Arrays.equals(keyReference, candidate.f0) && valueReference == 
candidate.f1;

Review Comment:
   The method `equalToReference` is used to compare whether two objects are of 
the same content. Therefore, `equals()` should be used. The correction has been 
made.



##########
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/sortpartition/FixedLengthByteKeyAndValueComparator.java:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.flink.streaming.api.operators.sortpartition;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeutils.TypeComparator;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.MemorySegment;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+/**
+ * The {@link FixedLengthByteKeyAndValueComparator} is used by {@link 
KeyedSortPartitionOperator} to
+ * compare records according to both the record key and record value. The 
length of record key must
+ * be fixed and will be initialized when the {@link 
FixedLengthByteKeyAndValueComparator} is
+ * created.
+ */
+@Internal
+public class FixedLengthByteKeyAndValueComparator<INPUT>
+        extends TypeComparator<Tuple2<byte[], INPUT>> {
+
+    private final int serializedKeyLength;
+
+    private final TypeComparator<INPUT> valueComparator;
+
+    private byte[] keyReference;
+
+    private INPUT valueReference;
+
+    FixedLengthByteKeyAndValueComparator(
+            int serializedKeyLength, TypeComparator<INPUT> valueComparator) {
+        this.serializedKeyLength = serializedKeyLength;
+        this.valueComparator = valueComparator;
+    }
+
+    @Override
+    public int hash(Tuple2<byte[], INPUT> record) {
+        return record.hashCode();
+    }
+
+    @Override
+    public void setReference(Tuple2<byte[], INPUT> toCompare) {
+        this.keyReference = toCompare.f0;
+        this.valueReference = toCompare.f1;
+    }
+
+    @Override
+    public boolean equalToReference(Tuple2<byte[], INPUT> candidate) {
+        return Arrays.equals(keyReference, candidate.f0) && valueReference == 
candidate.f1;
+    }
+
+    @Override
+    public int compareToReference(TypeComparator<Tuple2<byte[], INPUT>> 
referencedComparator) {
+        byte[] otherKey =
+                ((FixedLengthByteKeyAndValueComparator<INPUT>) 
referencedComparator).keyReference;
+        INPUT otherValue =
+                ((FixedLengthByteKeyAndValueComparator<INPUT>) 
referencedComparator).valueReference;
+        int keyCmp = compare(otherKey, this.keyReference);
+        if (keyCmp != 0) {
+            return keyCmp;
+        }
+        return valueComparator.compare(this.valueReference, otherValue);
+    }
+
+    @Override
+    public int compare(Tuple2<byte[], INPUT> first, Tuple2<byte[], INPUT> 
second) {
+        int keyCmp = compare(first.f0, second.f0);
+        if (keyCmp != 0) {
+            return keyCmp;
+        }
+        return valueComparator.compare(first.f1, second.f1);
+    }
+
+    private int compare(byte[] first, byte[] second) {

Review Comment:
   Done.



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

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to