Skip to content

Commit f90b3d0

Browse files
committed
Ensure we compare buytes in the same way accumulo does (#3160)
1 parent bd6283f commit f90b3d0

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

warehouse/query-core/src/main/java/datawave/query/util/sortedset/ByteArrayComparator.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,12 @@
22

33
import java.util.Comparator;
44

5+
import org.apache.hadoop.io.WritableComparator;
6+
57
public class ByteArrayComparator implements Comparator<byte[]> {
68

79
@Override
810
public int compare(byte[] data, byte[] term) {
9-
int minSize = data.length;
10-
if (term.length < minSize)
11-
minSize = term.length;
12-
int comparison = 0;
13-
for (int i = 0; i < minSize; i++) {
14-
comparison = new Byte(data[i]).compareTo(term[i]);
15-
if (comparison != 0)
16-
break;
17-
}
18-
if (comparison == 0) {
19-
if (minSize < data.length) {
20-
comparison = 1;
21-
} else if (minSize < term.length) {
22-
comparison = -1;
23-
}
24-
}
25-
return comparison;
11+
return WritableComparator.compareBytes(data, 0, data.length, term, 0, term.length);
2612
}
2713
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package datawave.query.util.sortedset;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.util.Random;
6+
7+
import org.junit.Test;
8+
9+
public class ByteArrayComparatorTest {
10+
ByteArrayComparator comparator = new ByteArrayComparator();
11+
12+
@Test
13+
public void testCompareEqualLen() {
14+
Random random = new Random();
15+
byte[] bytes1 = new byte[20];
16+
byte[] bytes2 = new byte[20];
17+
random.nextBytes(bytes1);
18+
System.arraycopy(bytes1, 0, bytes2, 0, 20);
19+
assertTrue(comparator.compare(bytes1, bytes2) == 0);
20+
assertTrue(comparator.compare(bytes2, bytes1) == 0);
21+
22+
bytes1[10] = (byte) 10;
23+
bytes2[10] = (byte) 11;
24+
25+
assertTrue(comparator.compare(bytes1, bytes2) < 0);
26+
assertTrue(comparator.compare(bytes2, bytes1) > 0);
27+
28+
bytes1[10] = (byte) -181; // as an unsigned byte this should be greater than 11
29+
30+
assertTrue(comparator.compare(bytes1, bytes2) > 0);
31+
assertTrue(comparator.compare(bytes2, bytes1) < 0);
32+
}
33+
34+
@Test
35+
public void testCompareUnequalLen() {
36+
Random random = new Random();
37+
byte[] bytes1 = new byte[20];
38+
byte[] bytes2 = new byte[30];
39+
random.nextBytes(bytes1);
40+
System.arraycopy(bytes1, 0, bytes2, 0, 20);
41+
42+
assertTrue(comparator.compare(bytes1, bytes2) < 0);
43+
assertTrue(comparator.compare(bytes2, bytes1) > 0);
44+
45+
bytes2[10] = (byte) 11;
46+
bytes1[10] = (byte) -181; // as an unsigned byte this should be greater than 11
47+
48+
assertTrue(comparator.compare(bytes1, bytes2) > 0);
49+
assertTrue(comparator.compare(bytes2, bytes1) < 0);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)