Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-3116: Implement the Variant binary encoding #3117

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Arrays;
import java.util.Base64;
import java.util.Locale;
import java.util.UUID;

/**
* This Variant class holds the Variant-encoded value and metadata binary values.
Expand Down Expand Up @@ -129,8 +130,11 @@ public byte[] getBinary() {
/**
* @return the UUID value
*/
public byte[] getUUID() {
return VariantUtil.getUUID(value, pos);
public UUID getUUID() {
byte[] uuidBytes = VariantUtil.getUUID(value, pos);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any benefit to having the internal method return an array of bytes? If you do it in VariantUtil.getUuid, I think you can directly construct the byte buffer as ByteBuffer.wrap(value, start, UUID_SIZE).order(ByteOrder.BIG_ENDIAN);, and avoid an array copy, plus make the util and public interface more consistent.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks! I updated it.

long msb = ByteBuffer.wrap(uuidBytes, 0, 8).order(ByteOrder.BIG_ENDIAN).getLong();
long lsb = ByteBuffer.wrap(uuidBytes, 8, 8).order(ByteOrder.BIG_ENDIAN).getLong();
return new UUID(msb, lsb);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.SecureRandom;
import java.time.Instant;
import java.time.LocalDate;
Expand All @@ -31,6 +33,7 @@
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.junit.Assert;
Expand Down Expand Up @@ -346,7 +349,10 @@ public void testUUID() {
vb.appendUUID(uuid);
Assert.assertEquals(
"\"00112233-4455-6677-8899-aabbccddeeff\"", vb.result().toJson());
Assert.assertArrayEquals(uuid, vb.result().getUUID());
long msb = ByteBuffer.wrap(uuid, 0, 8).order(ByteOrder.BIG_ENDIAN).getLong();
long lsb = ByteBuffer.wrap(uuid, 8, 8).order(ByteOrder.BIG_ENDIAN).getLong();
UUID expected = new UUID(msb, lsb);
Assert.assertEquals(expected, vb.result().getUUID());
}

@Test
Expand Down