Skip to content

Commit

Permalink
AVRO-4045: Use JDK compare for byte arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
belugabehr committed Aug 29, 2024
1 parent c7b79bf commit 4b05b82
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ public String toString() {

@Override
public int compareTo(Fixed that) {
return BinaryData.compareBytes(this.bytes, 0, this.bytes.length, that.bytes, 0, that.bytes.length);
return Arrays.compare(this.bytes, 0, this.bytes.length, that.bytes, 0, that.bytes.length);
}
}

Expand Down
16 changes: 4 additions & 12 deletions lang/java/avro/src/main/java/org/apache/avro/io/BinaryData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.avro.io;

import java.io.IOException;
import java.util.Arrays;

import org.apache.avro.Schema;
import org.apache.avro.Schema.Field;
Expand Down Expand Up @@ -155,7 +156,7 @@ private static int compare(Decoders d, Schema schema) throws IOException {
}
case FIXED: {
int size = schema.getFixedSize();
int c = compareBytes(d.d1.getBuf(), d.d1.getPos(), size, d.d2.getBuf(), d.d2.getPos(), size);
int c = Arrays.compare(d.d1.getBuf(), d.d1.getPos(), size, d.d2.getBuf(), d.d2.getPos(), size);
d.d1.skipFixed(size);
d.d2.skipFixed(size);
return c;
Expand All @@ -164,7 +165,7 @@ private static int compare(Decoders d, Schema schema) throws IOException {
case BYTES: {
int l1 = d1.readInt();
int l2 = d2.readInt();
int c = compareBytes(d.d1.getBuf(), d.d1.getPos(), l1, d.d2.getBuf(), d.d2.getPos(), l2);
int c = Arrays.compare(d.d1.getBuf(), d.d1.getPos(), l1, d.d2.getBuf(), d.d2.getPos(), l2);
d.d1.skipFixed(l1);
d.d2.skipFixed(l2);
return c;
Expand All @@ -181,16 +182,7 @@ private static int compare(Decoders d, Schema schema) throws IOException {
* return a positive value, if less than return a negative value.
*/
public static int compareBytes(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
int end1 = s1 + l1;
int end2 = s2 + l2;
for (int i = s1, j = s2; i < end1 && j < end2; i++, j++) {
int a = (b1[i] & 0xff);
int b = (b2[j] & 0xff);
if (a != b) {
return a - b;
}
}
return l1 - l2;
return Arrays.compare(b1, s1, l1, b2, s2, l2);
}

private static class HashData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericFixed;
import org.apache.avro.generic.IndexedRecord;
import org.apache.avro.io.BinaryData;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.specific.FixedSize;
Expand Down Expand Up @@ -987,7 +986,7 @@ protected int compare(Object o1, Object o2, Schema s, boolean equals) {
break;
byte[] b1 = (byte[]) o1;
byte[] b2 = (byte[]) o2;
return BinaryData.compareBytes(b1, 0, b1.length, b2, 0, b2.length);
return Arrays.compare(b1, 0, b1.length, b2, 0, b2.length);
}
return super.compare(o1, o2, s, equals);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Arrays;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericFixed;
import org.apache.avro.io.BinaryData;

/** Base class for generated fixed-sized data classes. */
public abstract class SpecificFixed implements GenericFixed, Comparable<SpecificFixed>, Externalizable {
Expand Down Expand Up @@ -70,7 +69,7 @@ public String toString() {

@Override
public int compareTo(SpecificFixed that) {
return BinaryData.compareBytes(this.bytes, 0, this.bytes.length, that.bytes, 0, that.bytes.length);
return Arrays.compare(this.bytes, 0, this.bytes.length, that.bytes, 0, that.bytes.length);
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Arrays;

import org.apache.avro.SystemLimitException;
import org.apache.avro.io.BinaryData;

/**
* A Utf8 string. Unlike {@link String}, instances are mutable. This is more
Expand Down Expand Up @@ -181,7 +180,7 @@ public int hashCode() {

@Override
public int compareTo(Utf8 that) {
return BinaryData.compareBytes(this.bytes, 0, this.length, that.bytes, 0, that.length);
return Arrays.compare(this.bytes, 0, this.length, that.bytes, 0, that.length);
}

// CharSequence implementation
Expand Down

0 comments on commit 4b05b82

Please sign in to comment.