-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ByteArray class for primary keys with byte[] (#17)
Byte arrays in primary keys support. We can't do it with simple `byte[]` because Java Records' default `equals()` compares references, not real array data. Special class for byte arrays looks better than magic over `byte[]`, anyway. --------- Co-authored-by: Alexander Lavrukov <[email protected]>
- Loading branch information
Showing
15 changed files
with
308 additions
and
8 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
databind/src/main/java/tech/ydb/yoj/databind/ByteArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package tech.ydb.yoj.databind; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Arrays; | ||
|
||
public final class ByteArray implements Comparable<ByteArray> { | ||
private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray(); | ||
|
||
private final byte[] array; | ||
|
||
private ByteArray(byte[] array) { | ||
this.array = array; | ||
} | ||
|
||
public static ByteArray wrap(byte[] array) { | ||
return new ByteArray(array); | ||
} | ||
|
||
public static ByteArray copy(byte[] array) { | ||
return new ByteArray(array.clone()); | ||
} | ||
|
||
public ByteArray copy() { | ||
return ByteArray.copy(array); | ||
} | ||
|
||
public byte[] getArray() { | ||
return array; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
ByteArray that = (ByteArray) o; | ||
return Arrays.equals(array, that.array); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Arrays.hashCode(array); | ||
} | ||
|
||
@Override | ||
public int compareTo(@NotNull ByteArray o) { | ||
return Arrays.compare(array, o.array); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (array.length > 16) { | ||
return "bytes(length > 16)"; | ||
} | ||
|
||
char[] hexChars = new char[array.length * 2 + 7]; | ||
hexChars[0] = 'b'; | ||
hexChars[1] = 'y'; | ||
hexChars[2] = 't'; | ||
hexChars[3] = 'e'; | ||
hexChars[4] = 's'; | ||
hexChars[5] = '('; | ||
|
||
int i = 0; | ||
for (; i < array.length; i++) { | ||
int v = array[i] & 0xFF; | ||
int ci = i * 2 + 6; | ||
hexChars[ci] = HEX_CHARS[v >>> 4]; | ||
hexChars[ci + 1] = HEX_CHARS[v & 0x0F]; | ||
} | ||
hexChars[i * 2 + 6] = ')'; | ||
|
||
return new String(hexChars); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
databind/src/test/java/tech/ydb/yoj/databind/ByteArrayTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package tech.ydb.yoj.databind; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
import java.util.TreeSet; | ||
|
||
public class ByteArrayTest { | ||
@Test | ||
public void testWrap() { | ||
byte[] arr = bytesOf(0, 1, 2); | ||
var b = ByteArray.wrap(arr); | ||
b.getArray()[1] = 3; | ||
|
||
Assert.assertEquals(arr[1], 3); | ||
} | ||
|
||
@Test | ||
public void testCopy() { | ||
byte[] arr = bytesOf(0, 1, 2); | ||
var b = ByteArray.copy(arr); | ||
b.getArray()[1] = 3; | ||
|
||
Assert.assertEquals(arr[1], 1); | ||
} | ||
|
||
@Test | ||
public void testEquals() { | ||
byte[] arrA = bytesOf(0, 1, 2); | ||
byte[] arrB = bytesOf(0, 1, 2); | ||
var a = ByteArray.wrap(arrA); | ||
var b = ByteArray.wrap(arrB); | ||
|
||
Assert.assertNotEquals(arrA, arrB); | ||
Assert.assertEquals(a, a); | ||
Assert.assertEquals(a, b); | ||
Assert.assertNotEquals(a, null); | ||
} | ||
|
||
@Test | ||
public void testSetWork() { | ||
TreeSet<ByteArray> set = new TreeSet<>(List.of( | ||
valueOf(0, 1, 2), | ||
valueOf(0, 1, 2), | ||
valueOf(), | ||
valueOf(255), | ||
valueOf(1, 2, 3), | ||
valueOf(1, 2, 3), | ||
valueOf(0, 1, 3) | ||
)); | ||
|
||
Assert.assertEquals(set.stream().toList(), List.of( | ||
valueOf(), | ||
valueOf(255), | ||
valueOf(0, 1, 2), | ||
valueOf(0, 1, 3), | ||
valueOf(1, 2, 3) | ||
)); | ||
} | ||
|
||
@Test | ||
public void testCompare() { | ||
Assert.assertEquals(0, valueOf(0, 1, 2).compareTo(valueOf(0, 1, 2))); | ||
Assert.assertEquals(1, valueOf(0, 1, 3).compareTo(valueOf(0, 1, 2))); | ||
Assert.assertTrue(0 > valueOf(0, 1, 2).compareTo(valueOf(1, 2, 3))); | ||
Assert.assertTrue(0 > valueOf().compareTo(valueOf(1, 2, 3))); | ||
Assert.assertTrue(0 < valueOf(1, 2, 3).compareTo(valueOf())); | ||
Assert.assertTrue(0 < valueOf(0, 1, 2).compareTo(valueOf(0, 1))); | ||
} | ||
|
||
@Test | ||
public void testToString() { | ||
Assert.assertEquals("bytes()", valueOf().toString()); | ||
Assert.assertEquals("bytes(00)", valueOf(0).toString()); | ||
Assert.assertEquals("bytes(0102ff)", valueOf(1, 2, 255).toString()); | ||
Assert.assertEquals("bytes(00a2fe00)", valueOf(0, 162, 254, 0).toString()); | ||
Assert.assertEquals( | ||
"bytes(01010101010101010101010101010101)", | ||
valueOf(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1).toString() | ||
); | ||
Assert.assertEquals( | ||
"bytes(length > 16)", | ||
valueOf(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1).toString() | ||
); | ||
} | ||
|
||
private static byte[] bytesOf(int... array) { | ||
byte[] result = new byte[array.length]; | ||
for (int i = 0; i < array.length; i++) { | ||
result[i] = (byte) array[i]; | ||
} | ||
return result; | ||
} | ||
|
||
private static ByteArray valueOf(int... array) { | ||
byte[] result = new byte[array.length]; | ||
for (int i = 0; i < array.length; i++) { | ||
result[i] = (byte) array[i]; | ||
} | ||
return ByteArray.wrap(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.