Skip to content

Commit

Permalink
Added method getUuidAsBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
alex268 committed Oct 4, 2024
1 parent ad0ae12 commit 90681c1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 6 deletions.
4 changes: 4 additions & 0 deletions table/src/main/java/tech/ydb/table/values/PrimitiveValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public long getUuidLow() {
throw new IllegalStateException("expected Uuid, but was " + getClass().getSimpleName());
}

public byte[] getUuidAsBytes() {
throw new IllegalStateException("expected Uuid, but was " + getClass().getSimpleName());
}

public UUID getUuidJdk() {
throw new IllegalStateException("expected Uuid, but was " + getClass().getSimpleName());
}
Expand Down
19 changes: 19 additions & 0 deletions table/src/main/java/tech/ydb/table/values/proto/ProtoValue.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.ydb.table.values.proto;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -817,6 +818,10 @@ public static PrimitiveValue newUuid(String uuid) {
return new Uuid(uuid);
}

public static PrimitiveValue newUuid(byte[] uuid) {
return new Uuid(uuid);
}

private static final class Uuid extends PrimitiveValue {
private final long high;
private final long low;
Expand All @@ -826,6 +831,12 @@ private static final class Uuid extends PrimitiveValue {
this.low = low;
}

Uuid(byte[] value) {
ByteBuffer buf = ByteBuffer.wrap(value);
this.high = buf.getLong();
this.low = buf.getLong();
}

Uuid(String value) {
String[] components = value.split("-");
if (components.length != 5) {
Expand Down Expand Up @@ -903,6 +914,14 @@ public long getUuidLow() {
return low;
}

@Override
public byte[] getUuidAsBytes() {
ByteBuffer buf = ByteBuffer.allocate(16);
buf.putLong(high);
buf.putLong(low);
return buf.array();
}

@Override
public UUID getUuidJdk() {
long timeLow = (low & 0x00000000ffffffffL) << 32;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package tech.ydb.table.integration;

import java.util.UUID;

import com.google.common.io.BaseEncoding;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
Expand All @@ -14,13 +17,15 @@
import tech.ydb.table.values.NullType;
import tech.ydb.table.values.NullValue;
import tech.ydb.table.values.PrimitiveType;
import tech.ydb.table.values.PrimitiveValue;
import tech.ydb.table.values.Type;
import tech.ydb.test.junit4.GrpcTransportRule;

/**
*
* @author Aleksandr Gorshenin
*/
public class NullReadTest {
public class ValuesReadTest {
@ClassRule
public static final GrpcTransportRule YDB_TRANSPORT = new GrpcTransportRule();
private static final SessionRetryContext CTX = SessionRetryContext.create(SimpleTableClient.newClient(
Expand Down Expand Up @@ -54,4 +59,34 @@ public void nullReadTest() {
Assert.assertEquals(123, p2.getInt32());
Assert.assertSame(NullValue.of(), p3.getValue());
}

@Test
@SuppressWarnings("deprecation")
public void uuidReadTest() {
DataQueryResult result = CTX.supplyResult(
s -> s.executeDataQuery(
"SELECT CAST('123e4567-e89b-12d3-a456-426614174000' AS UUID) AS p1",
TxControl.serializableRw()
)
).join().getValue();

Assert.assertEquals(1, result.getResultSetCount());

ResultSetReader rs = result.getResultSet(0);
Assert.assertTrue(rs.next());

ValueReader p1 = rs.getColumn("p1");
Assert.assertNotNull(p1);

Assert.assertSame(Type.Kind.OPTIONAL, p1.getType().getKind());
Assert.assertSame(PrimitiveType.Uuid, p1.getType().unwrapOptional());

Assert.assertEquals(UUID.fromString("123e4567-e89b-12d3-a456-426614174000"), p1.getUuid());

PrimitiveValue v = p1.getValue().asOptional().get().asData();
Assert.assertEquals("123e4567-e89b-12d3-a456-426614174000", v.getUuidString());
Assert.assertEquals(0x12d3e89b123e4567L, v.getUuidLow());
Assert.assertEquals(0x00401714664256a4L, v.getUuidHigh());
Assert.assertArrayEquals(BaseEncoding.base16().decode("00401714664256A412D3E89B123E4567"), v.getUuidAsBytes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ public void json() {
}

@Test
@SuppressWarnings("deprecation")
public void uuid() {
long low = 0x6677445500112233L, high = 0xffeeddccbbaa9988L;
String uuidStr = "00112233-4455-6677-8899-aabbccddeeff";
Expand All @@ -444,13 +443,10 @@ public void uuid() {
Consumer<PrimitiveValue> doTest = (v) -> {
Assert.assertEquals(PrimitiveValue.newUuid(uuid), v);
Assert.assertEquals(PrimitiveValue.newUuid(uuidStr), v);
Assert.assertEquals(PrimitiveValue.newUuid(high, low), v);
Assert.assertNotEquals(PrimitiveValue.newUuid(UUID.randomUUID()), v);

Assert.assertEquals("\"00112233-4455-6677-8899-aabbccddeeff\"", v.toString());
Assert.assertEquals("00112233-4455-6677-8899-aabbccddeeff", v.getUuidString());
Assert.assertEquals(v.getUuidHigh(), high);
Assert.assertEquals(v.getUuidLow(), low);
Assert.assertEquals(v.getUuidJdk(), uuid);

ValueProtos.Value vPb = v.toPb();
Expand All @@ -461,7 +457,6 @@ public void uuid() {

doTest.accept(PrimitiveValue.newUuid(uuid));
doTest.accept(PrimitiveValue.newUuid(uuidStr));
doTest.accept(PrimitiveValue.newUuid(high, low));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import com.google.common.io.BaseEncoding;
import org.junit.Assert;
import org.junit.Test;

import tech.ydb.table.values.PrimitiveValue;



/**
Expand Down Expand Up @@ -59,4 +63,28 @@ public void tzTimestamp() {
Assert.assertEquals(ZoneId.of("America/Chicago"), dateTime.getZone());
Assert.assertEquals("2018-10-22T01:23:45.678901-05:00[America/Chicago]", dateTime.toString());
}

@Test
@SuppressWarnings("deprecation")
public void uuid() {
String uuid = "123e4567-e89b-12d3-a456-426614174000";
long low = 0x12d3e89b123e4567L, high = 0x00401714664256a4L;
byte[] bytes = BaseEncoding.base16().decode("00401714664256A412D3E89B123E4567");

PrimitiveValue u1 = ProtoValue.newUuid(uuid);
PrimitiveValue u2 = ProtoValue.newUuid(UUID.fromString(uuid));
PrimitiveValue u3 = ProtoValue.newUuid(bytes);
PrimitiveValue u4 = ProtoValue.newUuid(high, low);

Assert.assertEquals(u1, u2);
Assert.assertEquals(u1, u3);
Assert.assertEquals(u1, u4);

Assert.assertEquals(uuid, u1.getUuidString());
Assert.assertEquals(UUID.fromString("123e4567-e89b-12d3-a456-426614174000"), u1.getUuidJdk());
Assert.assertEquals(low, u1.getUuidLow());
Assert.assertEquals(high, u1.getUuidHigh());

Assert.assertArrayEquals(bytes, u1.getUuidAsBytes());
}
}

0 comments on commit 90681c1

Please sign in to comment.