Skip to content

Commit baaa360

Browse files
Merge pull request #473 from IABTechLab/cbc-UID2-1162-replace-optout-compare-bytes
Removed functions that are covered by the built-in functions
2 parents c7cc45c + 4e66986 commit baaa360

File tree

5 files changed

+17
-72
lines changed

5 files changed

+17
-72
lines changed

src/main/java/com/uid2/shared/optout/OptOutHeap.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,20 @@ private int maxChild(int i) {
231231
private int compareToEntryInHeap(byte[] entryAsBytes, int heapPos) {
232232
int heapBufPos = heapPos * OptOutConst.EntrySize;
233233
// comparing only identity hash (length == Sha256Bytes)
234-
return OptOutUtils.compareByteRange(entryAsBytes, 0, store, heapBufPos, OptOutConst.Sha256Bytes);
234+
return Arrays.compareUnsigned(
235+
entryAsBytes, 0, OptOutConst.Sha256Bytes,
236+
store, heapBufPos, heapBufPos+OptOutConst.Sha256Bytes
237+
);
235238
}
236239

237240
private int compareEntriesInHeap(int i, int j) {
238241
int p1 = i * OptOutConst.EntrySize;
239242
int p2 = j * OptOutConst.EntrySize;
240243
// comparing only identity hash (length == Sha256Bytes)
241-
return OptOutUtils.compareByteRange(store, p1, store, p2, OptOutConst.Sha256Bytes);
244+
return Arrays.compareUnsigned(
245+
store, p1, p1+OptOutConst.Sha256Bytes,
246+
store, p2, p2+OptOutConst.Sha256Bytes
247+
);
242248
}
243249

244250
private void swapEntriesInHeap(int srcPos, int dstPos) {

src/main/java/com/uid2/shared/optout/OptOutPartition.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.uid2.shared.optout;
22

3+
import java.util.Arrays;
4+
35
// OptOutPartition is a sorted array of items, each item is a range of bytes within the store
46
// that stores identity hash (byte[]), advertising id (byte[]), and the timestamp of the optout entry
57
public class OptOutPartition extends OptOutCollection {
@@ -57,7 +59,10 @@ private int compareEntryToIdentityHash(int entryIndex, byte[] identityHash) {
5759
int byteIndex = entryIndex * OptOutConst.EntrySize;
5860

5961
// compare if bytes match
60-
return OptOutUtils.compareByteRange(this.store, byteIndex, identityHash, 0, OptOutConst.Sha256Bytes);
62+
return Arrays.compareUnsigned(
63+
this.store, byteIndex, byteIndex+OptOutConst.Sha256Bytes,
64+
identityHash, 0, OptOutConst.Sha256Bytes
65+
);
6166
}
6267

6368
private long getTimestampByIndex(int entryIndex) {

src/main/java/com/uid2/shared/optout/OptOutUtils.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -214,31 +214,6 @@ public static long toLongBE(byte[] bytes, int byteIndex) {
214214
return ByteBuffer.wrap(bytes, 0, Long.BYTES).order(ByteOrder.BIG_ENDIAN).getLong();
215215
}
216216

217-
// positive if arr1 > arr2
218-
// negative if arr1 < arr2
219-
// 0 if arr1 == arr2
220-
public static int compareBytes(byte[] arr1, byte[] arr2) {
221-
assert arr1.length == arr2.length;
222-
return OptOutUtils.compareByteRange(arr1, 0, arr2, 0, arr1.length);
223-
}
224-
225-
// positive if arr1 > arr2
226-
// negative if arr1 < arr2
227-
// 0 if arr1 == arr2
228-
public static int compareByteRange(byte[] arr1, int idx1, byte[] arr2, int idx2, int bytesToCompare) {
229-
assert idx1 >= 0 && idx1 + bytesToCompare <= arr1.length;
230-
assert idx2 >= 0 && idx2 + bytesToCompare <= arr2.length;
231-
for (int i = 0; i < bytesToCompare; ++i) {
232-
int cmp = OptOutUtils.compareByte(arr1[idx1 + i], arr2[idx2 + i]);
233-
if (cmp != 0) return cmp;
234-
}
235-
return 0;
236-
}
237-
238-
public static int compareByte(byte a, byte b) {
239-
return Byte.toUnsignedInt(a) - Byte.toUnsignedInt(b);
240-
}
241-
242217
public static boolean isDeltaFile(String fn) {
243218
return fn.contains(OptOutUtils.prefixDeltaFile);
244219
}

src/test/java/com/uid2/shared/optout/OptOutHeapTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.jupiter.api.Test;
55

66
import java.util.HashSet;
7+
import java.util.Arrays;
78

89
import static org.junit.jupiter.api.Assertions.*;
910

@@ -118,14 +119,14 @@ private void checkHeapProperty(OptOutHeap heap, int i) {
118119
if (leftChild >= heap.size()) return;
119120
OptOutEntry left = heap.get(leftChild);
120121
// System.out.format("self vs left: %x %x\n", self.idHashAsLong(), left.idHashAsLong());
121-
assertTrue(OptOutUtils.compareBytes(self.identityHash, left.identityHash) > 0);
122+
assertTrue(Arrays.compareUnsigned(self.identityHash, left.identityHash) >0 );
122123
checkHeapProperty(heap, leftChild);
123124

124125
int rightChild = OptOutHeap.rightChild(i);
125126
if (rightChild >= heap.size()) return;
126127
OptOutEntry right = heap.get(rightChild);
127128
// System.out.format("self vs right: %x %x\n", self.idHashAsLong(), right.idHashAsLong());
128-
assertTrue(OptOutUtils.compareBytes(self.identityHash, right.identityHash) > 0);
129+
assertTrue(Arrays.compareUnsigned(self.identityHash, right.identityHash) >0 );
129130
checkHeapProperty(heap, rightChild);
130131
}
131132
}

src/test/java/com/uid2/shared/optout/OptOutUtilsTest.java

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -81,48 +81,6 @@ public void byteArrayToInt_tests() {
8181
assertEquals(0x7f7f7f7f, OptOutUtils.toInt(new byte[]{0x7f, 0x7f, 0x7f, 0x7f}, 0));
8282
}
8383

84-
@Test
85-
public void compareBytes_tests() {
86-
for (int i = 0; i < 1000; ++i) {
87-
long a = OptOutUtils.rand.nextLong();
88-
long b = OptOutUtils.rand.nextLong();
89-
int cmp1 = Long.compareUnsigned(a, b);
90-
// System.out.format("a vs b: %x vs %x (cmp %d)\n", a, b, cmp1);
91-
byte[] x = OptOutUtils.toByteArray(a);
92-
byte[] y = OptOutUtils.toByteArray(b);
93-
int cmp2 = OptOutUtils.compareBytes(x, y);
94-
if (cmp1 == 0) {
95-
assertTrue(cmp2 == 0);
96-
} else if (cmp1 > 0) {
97-
// little endian doesn't guarantee byte array comparison yields the same consistent result
98-
// assertTrue(cmp2 > 0);
99-
} else /* cmp1 < 0 */ {
100-
// little endian doesn't guarantee byte array comparison yields the same consistent result
101-
// assertTrue(cmp2 < 0);
102-
}
103-
}
104-
}
105-
106-
@Test
107-
public void compareBytesBE_tests() {
108-
for (int i = 0; i < 1000; ++i) {
109-
long a = OptOutUtils.rand.nextLong();
110-
long b = OptOutUtils.rand.nextLong();
111-
int cmp1 = Long.compareUnsigned(a, b);
112-
// System.out.format("a vs b: %x vs %x (cmp %d)\n", a, b, cmp1);
113-
byte[] x = OptOutUtils.toByteArrayBE(a);
114-
byte[] y = OptOutUtils.toByteArrayBE(b);
115-
int cmp2 = OptOutUtils.compareBytes(x, y);
116-
if (cmp1 == 0) {
117-
assertTrue(cmp2 == 0);
118-
} else if (cmp1 > 0) {
119-
assertTrue(cmp2 > 0);
120-
} else /* cmp1 < 0 */ {
121-
assertTrue(cmp2 < 0);
122-
}
123-
}
124-
}
125-
12684
@Test
12785
public void nullHash_tests() {
12886
assertTrue(OptOutUtils.isValidSha256Hex(OptOutUtils.nullHash));

0 commit comments

Comments
 (0)