Skip to content

Commit 90681c1

Browse files
committed
Added method getUuidAsBytes
1 parent ad0ae12 commit 90681c1

File tree

5 files changed

+87
-6
lines changed

5 files changed

+87
-6
lines changed

table/src/main/java/tech/ydb/table/values/PrimitiveValue.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public long getUuidLow() {
132132
throw new IllegalStateException("expected Uuid, but was " + getClass().getSimpleName());
133133
}
134134

135+
public byte[] getUuidAsBytes() {
136+
throw new IllegalStateException("expected Uuid, but was " + getClass().getSimpleName());
137+
}
138+
135139
public UUID getUuidJdk() {
136140
throw new IllegalStateException("expected Uuid, but was " + getClass().getSimpleName());
137141
}

table/src/main/java/tech/ydb/table/values/proto/ProtoValue.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package tech.ydb.table.values.proto;
22

3+
import java.nio.ByteBuffer;
34
import java.nio.charset.Charset;
45
import java.time.Duration;
56
import java.time.Instant;
@@ -817,6 +818,10 @@ public static PrimitiveValue newUuid(String uuid) {
817818
return new Uuid(uuid);
818819
}
819820

821+
public static PrimitiveValue newUuid(byte[] uuid) {
822+
return new Uuid(uuid);
823+
}
824+
820825
private static final class Uuid extends PrimitiveValue {
821826
private final long high;
822827
private final long low;
@@ -826,6 +831,12 @@ private static final class Uuid extends PrimitiveValue {
826831
this.low = low;
827832
}
828833

834+
Uuid(byte[] value) {
835+
ByteBuffer buf = ByteBuffer.wrap(value);
836+
this.high = buf.getLong();
837+
this.low = buf.getLong();
838+
}
839+
829840
Uuid(String value) {
830841
String[] components = value.split("-");
831842
if (components.length != 5) {
@@ -903,6 +914,14 @@ public long getUuidLow() {
903914
return low;
904915
}
905916

917+
@Override
918+
public byte[] getUuidAsBytes() {
919+
ByteBuffer buf = ByteBuffer.allocate(16);
920+
buf.putLong(high);
921+
buf.putLong(low);
922+
return buf.array();
923+
}
924+
906925
@Override
907926
public UUID getUuidJdk() {
908927
long timeLow = (low & 0x00000000ffffffffL) << 32;

table/src/test/java/tech/ydb/table/integration/NullReadTest.java renamed to table/src/test/java/tech/ydb/table/integration/ValuesReadTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package tech.ydb.table.integration;
22

3+
import java.util.UUID;
4+
5+
import com.google.common.io.BaseEncoding;
36
import org.junit.Assert;
47
import org.junit.ClassRule;
58
import org.junit.Test;
@@ -14,13 +17,15 @@
1417
import tech.ydb.table.values.NullType;
1518
import tech.ydb.table.values.NullValue;
1619
import tech.ydb.table.values.PrimitiveType;
20+
import tech.ydb.table.values.PrimitiveValue;
21+
import tech.ydb.table.values.Type;
1722
import tech.ydb.test.junit4.GrpcTransportRule;
1823

1924
/**
2025
*
2126
* @author Aleksandr Gorshenin
2227
*/
23-
public class NullReadTest {
28+
public class ValuesReadTest {
2429
@ClassRule
2530
public static final GrpcTransportRule YDB_TRANSPORT = new GrpcTransportRule();
2631
private static final SessionRetryContext CTX = SessionRetryContext.create(SimpleTableClient.newClient(
@@ -54,4 +59,34 @@ public void nullReadTest() {
5459
Assert.assertEquals(123, p2.getInt32());
5560
Assert.assertSame(NullValue.of(), p3.getValue());
5661
}
62+
63+
@Test
64+
@SuppressWarnings("deprecation")
65+
public void uuidReadTest() {
66+
DataQueryResult result = CTX.supplyResult(
67+
s -> s.executeDataQuery(
68+
"SELECT CAST('123e4567-e89b-12d3-a456-426614174000' AS UUID) AS p1",
69+
TxControl.serializableRw()
70+
)
71+
).join().getValue();
72+
73+
Assert.assertEquals(1, result.getResultSetCount());
74+
75+
ResultSetReader rs = result.getResultSet(0);
76+
Assert.assertTrue(rs.next());
77+
78+
ValueReader p1 = rs.getColumn("p1");
79+
Assert.assertNotNull(p1);
80+
81+
Assert.assertSame(Type.Kind.OPTIONAL, p1.getType().getKind());
82+
Assert.assertSame(PrimitiveType.Uuid, p1.getType().unwrapOptional());
83+
84+
Assert.assertEquals(UUID.fromString("123e4567-e89b-12d3-a456-426614174000"), p1.getUuid());
85+
86+
PrimitiveValue v = p1.getValue().asOptional().get().asData();
87+
Assert.assertEquals("123e4567-e89b-12d3-a456-426614174000", v.getUuidString());
88+
Assert.assertEquals(0x12d3e89b123e4567L, v.getUuidLow());
89+
Assert.assertEquals(0x00401714664256a4L, v.getUuidHigh());
90+
Assert.assertArrayEquals(BaseEncoding.base16().decode("00401714664256A412D3E89B123E4567"), v.getUuidAsBytes());
91+
}
5792
}

table/src/test/java/tech/ydb/table/values/PrimitiveValueTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,6 @@ public void json() {
435435
}
436436

437437
@Test
438-
@SuppressWarnings("deprecation")
439438
public void uuid() {
440439
long low = 0x6677445500112233L, high = 0xffeeddccbbaa9988L;
441440
String uuidStr = "00112233-4455-6677-8899-aabbccddeeff";
@@ -444,13 +443,10 @@ public void uuid() {
444443
Consumer<PrimitiveValue> doTest = (v) -> {
445444
Assert.assertEquals(PrimitiveValue.newUuid(uuid), v);
446445
Assert.assertEquals(PrimitiveValue.newUuid(uuidStr), v);
447-
Assert.assertEquals(PrimitiveValue.newUuid(high, low), v);
448446
Assert.assertNotEquals(PrimitiveValue.newUuid(UUID.randomUUID()), v);
449447

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

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

462458
doTest.accept(PrimitiveValue.newUuid(uuid));
463459
doTest.accept(PrimitiveValue.newUuid(uuidStr));
464-
doTest.accept(PrimitiveValue.newUuid(high, low));
465460
}
466461

467462
@Test

table/src/test/java/tech/ydb/table/values/proto/ProtoValueTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
import java.time.Month;
44
import java.time.ZoneId;
55
import java.time.ZonedDateTime;
6+
import java.util.UUID;
67
import java.util.concurrent.TimeUnit;
78

9+
import com.google.common.io.BaseEncoding;
810
import org.junit.Assert;
911
import org.junit.Test;
1012

13+
import tech.ydb.table.values.PrimitiveValue;
14+
1115

1216

1317
/**
@@ -59,4 +63,28 @@ public void tzTimestamp() {
5963
Assert.assertEquals(ZoneId.of("America/Chicago"), dateTime.getZone());
6064
Assert.assertEquals("2018-10-22T01:23:45.678901-05:00[America/Chicago]", dateTime.toString());
6165
}
66+
67+
@Test
68+
@SuppressWarnings("deprecation")
69+
public void uuid() {
70+
String uuid = "123e4567-e89b-12d3-a456-426614174000";
71+
long low = 0x12d3e89b123e4567L, high = 0x00401714664256a4L;
72+
byte[] bytes = BaseEncoding.base16().decode("00401714664256A412D3E89B123E4567");
73+
74+
PrimitiveValue u1 = ProtoValue.newUuid(uuid);
75+
PrimitiveValue u2 = ProtoValue.newUuid(UUID.fromString(uuid));
76+
PrimitiveValue u3 = ProtoValue.newUuid(bytes);
77+
PrimitiveValue u4 = ProtoValue.newUuid(high, low);
78+
79+
Assert.assertEquals(u1, u2);
80+
Assert.assertEquals(u1, u3);
81+
Assert.assertEquals(u1, u4);
82+
83+
Assert.assertEquals(uuid, u1.getUuidString());
84+
Assert.assertEquals(UUID.fromString("123e4567-e89b-12d3-a456-426614174000"), u1.getUuidJdk());
85+
Assert.assertEquals(low, u1.getUuidLow());
86+
Assert.assertEquals(high, u1.getUuidHigh());
87+
88+
Assert.assertArrayEquals(bytes, u1.getUuidAsBytes());
89+
}
6290
}

0 commit comments

Comments
 (0)