Skip to content

Commit 7722d88

Browse files
authored
Merge pull request #324 from alex268/master
Update methods to create and serialize UUIDs
2 parents ad920ec + 8b36eec commit 7722d88

File tree

6 files changed

+253
-174
lines changed

6 files changed

+253
-174
lines changed

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

Lines changed: 6 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.google.protobuf.UnsafeByteOperations;
2020

2121
import tech.ydb.proto.ValueProtos;
22-
import tech.ydb.table.utils.LittleEndian;
2322
import tech.ydb.table.values.proto.ProtoValue;
2423

2524

@@ -123,10 +122,12 @@ public String getUuidString() {
123122
throw new IllegalStateException("expected Uuid, but was " + getClass().getSimpleName());
124123
}
125124

125+
@Deprecated
126126
public long getUuidHigh() {
127127
throw new IllegalStateException("expected Uuid, but was " + getClass().getSimpleName());
128128
}
129129

130+
@Deprecated
130131
public long getUuidLow() {
131132
throw new IllegalStateException("expected Uuid, but was " + getClass().getSimpleName());
132133
}
@@ -245,16 +246,17 @@ public static PrimitiveValue newJsonDocument(String value) {
245246
return value.isEmpty() ? Text.EMPTY_JSON_DOCUMENT : new Text(PrimitiveType.JsonDocument, value);
246247
}
247248

249+
@Deprecated
248250
public static PrimitiveValue newUuid(long high, long low) {
249-
return new Uuid(high, low);
251+
return ProtoValue.newUuid(high, low);
250252
}
251253

252254
public static PrimitiveValue newUuid(UUID uuid) {
253-
return new Uuid(uuid);
255+
return ProtoValue.newUuid(uuid);
254256
}
255257

256258
public static PrimitiveValue newUuid(String uuid) {
257-
return new Uuid(uuid);
259+
return ProtoValue.newUuid(uuid);
258260
}
259261

260262
public static PrimitiveValue newDate(long daysSinceEpoch) {
@@ -1091,112 +1093,6 @@ public ValueProtos.Value toPb() {
10911093
}
10921094
}
10931095

1094-
private static final class Uuid extends PrimitiveValue {
1095-
private final long high;
1096-
private final long low;
1097-
1098-
Uuid(long high, long low) {
1099-
this.high = high;
1100-
this.low = low;
1101-
}
1102-
1103-
Uuid(String value) {
1104-
String[] components = value.split("-");
1105-
if (components.length != 5) {
1106-
throw new IllegalArgumentException("invalid UUID string: " + value);
1107-
}
1108-
1109-
long timeLow = Long.parseLong(components[0], 16);
1110-
long timeMid = Long.parseLong(components[1], 16) << 32;
1111-
long timeHighAndVersion = Long.parseLong(components[2], 16) << 48;
1112-
this.low = timeLow | timeMid | timeHighAndVersion;
1113-
1114-
long lsb = Long.parseLong(components[3], 16) << 48;
1115-
lsb |= Long.parseLong(components[4], 16);
1116-
this.high = LittleEndian.bswap(lsb);
1117-
}
1118-
1119-
Uuid(UUID uuid) {
1120-
long msb = uuid.getMostSignificantBits();
1121-
long timeLow = (msb & 0xffffffff00000000L) >>> 32;
1122-
long timeMid = (msb & 0x00000000ffff0000L) << 16;
1123-
long timeHighAndVersion = (msb & 0x000000000000ffffL) << 48;
1124-
1125-
this.low = timeLow | timeMid | timeHighAndVersion;
1126-
this.high = LittleEndian.bswap(uuid.getLeastSignificantBits());
1127-
}
1128-
1129-
@Override
1130-
public PrimitiveType getType() {
1131-
return PrimitiveType.Uuid;
1132-
}
1133-
1134-
@Override
1135-
public boolean equals(Object o) {
1136-
if (this == o) {
1137-
return true;
1138-
}
1139-
1140-
if (o == null || getClass() != o.getClass()) {
1141-
return false;
1142-
}
1143-
1144-
Uuid uuid = (Uuid) o;
1145-
return high == uuid.high && low == uuid.low;
1146-
}
1147-
1148-
@Override
1149-
public int hashCode() {
1150-
int result = (int) (high ^ (high >>> 32));
1151-
result = 31 * result + (int) (low ^ (low >>> 32));
1152-
return result;
1153-
}
1154-
1155-
@Override
1156-
public String toString() {
1157-
return '\"' + getUuidString() + '\"';
1158-
}
1159-
1160-
@Override
1161-
public String getUuidString() {
1162-
long hiBe = LittleEndian.bswap(high);
1163-
return
1164-
digits(low, 8) + "-" + digits(low >>> 32, 4) + "-" + digits(low >>> 48, 4) + "-" +
1165-
digits(hiBe >> 48, 4) + "-" + digits(hiBe, 12);
1166-
}
1167-
1168-
@Override
1169-
public long getUuidHigh() {
1170-
return high;
1171-
}
1172-
1173-
@Override
1174-
public long getUuidLow() {
1175-
return low;
1176-
}
1177-
1178-
@Override
1179-
public UUID getUuidJdk() {
1180-
long timeLow = (low & 0x00000000ffffffffL) << 32;
1181-
long timeMid = (low & 0x0000ffff00000000L) >>> 16;
1182-
long timeHighAndVersion = (low & 0xffff000000000000L) >>> 48;
1183-
1184-
long hiBe = LittleEndian.bswap(high);
1185-
return new UUID(timeLow | timeMid | timeHighAndVersion, hiBe);
1186-
}
1187-
1188-
@Override
1189-
public ValueProtos.Value toPb() {
1190-
return ProtoValue.fromUuid(high, low);
1191-
}
1192-
1193-
/** Returns val represented by the specified number of hex digits. */
1194-
private static String digits(long val, int digits) {
1195-
long high = 1L << (digits * 4);
1196-
return Long.toHexString(high | (val & (high - 1))).substring(1);
1197-
}
1198-
}
1199-
12001096
private static final class InstantValue extends PrimitiveValue {
12011097
private final PrimitiveType type;
12021098
private final long microsSinceEpoch;

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

Lines changed: 121 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import tech.ydb.table.values.VariantType;
3737
import tech.ydb.table.values.VoidValue;
3838

39-
import static tech.ydb.table.values.Type.Kind.VOID;
40-
4139

4240
/**
4341
* @author Sergey Polovko
@@ -787,7 +785,7 @@ private static PrimitiveValue primitiveFromPb(PrimitiveType primitiveType, Value
787785
case Yson: return PrimitiveValue.newYson(value.getBytesValue());
788786
case Json: return PrimitiveValue.newJson(value.getTextValue());
789787
case JsonDocument: return PrimitiveValue.newJsonDocument(value.getTextValue());
790-
case Uuid: return PrimitiveValue.newUuid(value.getHigh128(), value.getLow128());
788+
case Uuid: return newUuid(value.getHigh128(), value.getLow128());
791789
case Date: return PrimitiveValue.newDate(Integer.toUnsignedLong(value.getUint32Value()));
792790
case Datetime: return PrimitiveValue.newDatetime(Integer.toUnsignedLong(value.getUint32Value()));
793791
case Timestamp: return PrimitiveValue.newTimestamp(value.getUint64Value());
@@ -806,4 +804,124 @@ public static ValueProtos.TypedValue toTypedValue(Value<?> p) {
806804
.setValue(p.toPb())
807805
.build();
808806
}
807+
808+
public static PrimitiveValue newUuid(long high, long low) {
809+
return new Uuid(high, low);
810+
}
811+
812+
public static PrimitiveValue newUuid(UUID uuid) {
813+
return new Uuid(uuid);
814+
}
815+
816+
public static PrimitiveValue newUuid(String uuid) {
817+
return new Uuid(uuid);
818+
}
819+
820+
private static final class Uuid extends PrimitiveValue {
821+
private final long high;
822+
private final long low;
823+
824+
Uuid(long high, long low) {
825+
this.high = high;
826+
this.low = low;
827+
}
828+
829+
Uuid(String value) {
830+
String[] components = value.split("-");
831+
if (components.length != 5) {
832+
throw new IllegalArgumentException("invalid UUID string: " + value);
833+
}
834+
835+
long timeLow = Long.parseLong(components[0], 16);
836+
long timeMid = Long.parseLong(components[1], 16) << 32;
837+
long timeHighAndVersion = Long.parseLong(components[2], 16) << 48;
838+
this.low = timeLow | timeMid | timeHighAndVersion;
839+
840+
long lsb = Long.parseLong(components[3], 16) << 48;
841+
lsb |= Long.parseLong(components[4], 16);
842+
this.high = LittleEndian.bswap(lsb);
843+
}
844+
845+
Uuid(UUID uuid) {
846+
long msb = uuid.getMostSignificantBits();
847+
long timeLow = (msb & 0xffffffff00000000L) >>> 32;
848+
long timeMid = (msb & 0x00000000ffff0000L) << 16;
849+
long timeHighAndVersion = (msb & 0x000000000000ffffL) << 48;
850+
851+
this.low = timeLow | timeMid | timeHighAndVersion;
852+
this.high = LittleEndian.bswap(uuid.getLeastSignificantBits());
853+
}
854+
855+
@Override
856+
public PrimitiveType getType() {
857+
return PrimitiveType.Uuid;
858+
}
859+
860+
@Override
861+
public boolean equals(Object o) {
862+
if (this == o) {
863+
return true;
864+
}
865+
866+
if (o == null || getClass() != o.getClass()) {
867+
return false;
868+
}
869+
870+
Uuid uuid = (Uuid) o;
871+
return high == uuid.high && low == uuid.low;
872+
}
873+
874+
@Override
875+
public int hashCode() {
876+
int result = (int) (high ^ (high >>> 32));
877+
result = 31 * result + (int) (low ^ (low >>> 32));
878+
return result;
879+
}
880+
881+
@Override
882+
public String toString() {
883+
return '\"' + getUuidString() + '\"';
884+
}
885+
886+
@Override
887+
public String getUuidString() {
888+
long hiBe = LittleEndian.bswap(high);
889+
return
890+
digits(low, 8) + "-" + digits(low >>> 32, 4) + "-" + digits(low >>> 48, 4) + "-" +
891+
digits(hiBe >> 48, 4) + "-" + digits(hiBe, 12);
892+
}
893+
894+
@Override
895+
@SuppressWarnings("deprecation")
896+
public long getUuidHigh() {
897+
return high;
898+
}
899+
900+
@Override
901+
@SuppressWarnings("deprecation")
902+
public long getUuidLow() {
903+
return low;
904+
}
905+
906+
@Override
907+
public UUID getUuidJdk() {
908+
long timeLow = (low & 0x00000000ffffffffL) << 32;
909+
long timeMid = (low & 0x0000ffff00000000L) >>> 16;
910+
long timeHighAndVersion = (low & 0xffff000000000000L) >>> 48;
911+
912+
long hiBe = LittleEndian.bswap(high);
913+
return new UUID(timeLow | timeMid | timeHighAndVersion, hiBe);
914+
}
915+
916+
@Override
917+
public ValueProtos.Value toPb() {
918+
return ProtoValue.fromUuid(high, low);
919+
}
920+
921+
/** Returns val represented by the specified number of hex digits. */
922+
private static String digits(long val, int digits) {
923+
long high = 1L << (digits * 4);
924+
return Long.toHexString(high | (val & (high - 1))).substring(1);
925+
}
926+
}
809927
}

table/src/test/java/tech/ydb/table/integration/NullReadTest.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)