Skip to content

Commit

Permalink
Merge pull request #324 from alex268/master
Browse files Browse the repository at this point in the history
Update methods to create and serialize UUIDs
  • Loading branch information
alex268 authored Oct 14, 2024
2 parents ad920ec + 8b36eec commit 7722d88
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 174 deletions.
116 changes: 6 additions & 110 deletions table/src/main/java/tech/ydb/table/values/PrimitiveValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.protobuf.UnsafeByteOperations;

import tech.ydb.proto.ValueProtos;
import tech.ydb.table.utils.LittleEndian;
import tech.ydb.table.values.proto.ProtoValue;


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

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

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

@Deprecated
public static PrimitiveValue newUuid(long high, long low) {
return new Uuid(high, low);
return ProtoValue.newUuid(high, low);
}

public static PrimitiveValue newUuid(UUID uuid) {
return new Uuid(uuid);
return ProtoValue.newUuid(uuid);
}

public static PrimitiveValue newUuid(String uuid) {
return new Uuid(uuid);
return ProtoValue.newUuid(uuid);
}

public static PrimitiveValue newDate(long daysSinceEpoch) {
Expand Down Expand Up @@ -1091,112 +1093,6 @@ public ValueProtos.Value toPb() {
}
}

private static final class Uuid extends PrimitiveValue {
private final long high;
private final long low;

Uuid(long high, long low) {
this.high = high;
this.low = low;
}

Uuid(String value) {
String[] components = value.split("-");
if (components.length != 5) {
throw new IllegalArgumentException("invalid UUID string: " + value);
}

long timeLow = Long.parseLong(components[0], 16);
long timeMid = Long.parseLong(components[1], 16) << 32;
long timeHighAndVersion = Long.parseLong(components[2], 16) << 48;
this.low = timeLow | timeMid | timeHighAndVersion;

long lsb = Long.parseLong(components[3], 16) << 48;
lsb |= Long.parseLong(components[4], 16);
this.high = LittleEndian.bswap(lsb);
}

Uuid(UUID uuid) {
long msb = uuid.getMostSignificantBits();
long timeLow = (msb & 0xffffffff00000000L) >>> 32;
long timeMid = (msb & 0x00000000ffff0000L) << 16;
long timeHighAndVersion = (msb & 0x000000000000ffffL) << 48;

this.low = timeLow | timeMid | timeHighAndVersion;
this.high = LittleEndian.bswap(uuid.getLeastSignificantBits());
}

@Override
public PrimitiveType getType() {
return PrimitiveType.Uuid;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

Uuid uuid = (Uuid) o;
return high == uuid.high && low == uuid.low;
}

@Override
public int hashCode() {
int result = (int) (high ^ (high >>> 32));
result = 31 * result + (int) (low ^ (low >>> 32));
return result;
}

@Override
public String toString() {
return '\"' + getUuidString() + '\"';
}

@Override
public String getUuidString() {
long hiBe = LittleEndian.bswap(high);
return
digits(low, 8) + "-" + digits(low >>> 32, 4) + "-" + digits(low >>> 48, 4) + "-" +
digits(hiBe >> 48, 4) + "-" + digits(hiBe, 12);
}

@Override
public long getUuidHigh() {
return high;
}

@Override
public long getUuidLow() {
return low;
}

@Override
public UUID getUuidJdk() {
long timeLow = (low & 0x00000000ffffffffL) << 32;
long timeMid = (low & 0x0000ffff00000000L) >>> 16;
long timeHighAndVersion = (low & 0xffff000000000000L) >>> 48;

long hiBe = LittleEndian.bswap(high);
return new UUID(timeLow | timeMid | timeHighAndVersion, hiBe);
}

@Override
public ValueProtos.Value toPb() {
return ProtoValue.fromUuid(high, low);
}

/** Returns val represented by the specified number of hex digits. */
private static String digits(long val, int digits) {
long high = 1L << (digits * 4);
return Long.toHexString(high | (val & (high - 1))).substring(1);
}
}

private static final class InstantValue extends PrimitiveValue {
private final PrimitiveType type;
private final long microsSinceEpoch;
Expand Down
124 changes: 121 additions & 3 deletions table/src/main/java/tech/ydb/table/values/proto/ProtoValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import tech.ydb.table.values.VariantType;
import tech.ydb.table.values.VoidValue;

import static tech.ydb.table.values.Type.Kind.VOID;


/**
* @author Sergey Polovko
Expand Down Expand Up @@ -787,7 +785,7 @@ private static PrimitiveValue primitiveFromPb(PrimitiveType primitiveType, Value
case Yson: return PrimitiveValue.newYson(value.getBytesValue());
case Json: return PrimitiveValue.newJson(value.getTextValue());
case JsonDocument: return PrimitiveValue.newJsonDocument(value.getTextValue());
case Uuid: return PrimitiveValue.newUuid(value.getHigh128(), value.getLow128());
case Uuid: return newUuid(value.getHigh128(), value.getLow128());
case Date: return PrimitiveValue.newDate(Integer.toUnsignedLong(value.getUint32Value()));
case Datetime: return PrimitiveValue.newDatetime(Integer.toUnsignedLong(value.getUint32Value()));
case Timestamp: return PrimitiveValue.newTimestamp(value.getUint64Value());
Expand All @@ -806,4 +804,124 @@ public static ValueProtos.TypedValue toTypedValue(Value<?> p) {
.setValue(p.toPb())
.build();
}

public static PrimitiveValue newUuid(long high, long low) {
return new Uuid(high, low);
}

public static PrimitiveValue newUuid(UUID uuid) {
return new Uuid(uuid);
}

public static PrimitiveValue newUuid(String uuid) {
return new Uuid(uuid);
}

private static final class Uuid extends PrimitiveValue {
private final long high;
private final long low;

Uuid(long high, long low) {
this.high = high;
this.low = low;
}

Uuid(String value) {
String[] components = value.split("-");
if (components.length != 5) {
throw new IllegalArgumentException("invalid UUID string: " + value);
}

long timeLow = Long.parseLong(components[0], 16);
long timeMid = Long.parseLong(components[1], 16) << 32;
long timeHighAndVersion = Long.parseLong(components[2], 16) << 48;
this.low = timeLow | timeMid | timeHighAndVersion;

long lsb = Long.parseLong(components[3], 16) << 48;
lsb |= Long.parseLong(components[4], 16);
this.high = LittleEndian.bswap(lsb);
}

Uuid(UUID uuid) {
long msb = uuid.getMostSignificantBits();
long timeLow = (msb & 0xffffffff00000000L) >>> 32;
long timeMid = (msb & 0x00000000ffff0000L) << 16;
long timeHighAndVersion = (msb & 0x000000000000ffffL) << 48;

this.low = timeLow | timeMid | timeHighAndVersion;
this.high = LittleEndian.bswap(uuid.getLeastSignificantBits());
}

@Override
public PrimitiveType getType() {
return PrimitiveType.Uuid;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

Uuid uuid = (Uuid) o;
return high == uuid.high && low == uuid.low;
}

@Override
public int hashCode() {
int result = (int) (high ^ (high >>> 32));
result = 31 * result + (int) (low ^ (low >>> 32));
return result;
}

@Override
public String toString() {
return '\"' + getUuidString() + '\"';
}

@Override
public String getUuidString() {
long hiBe = LittleEndian.bswap(high);
return
digits(low, 8) + "-" + digits(low >>> 32, 4) + "-" + digits(low >>> 48, 4) + "-" +
digits(hiBe >> 48, 4) + "-" + digits(hiBe, 12);
}

@Override
@SuppressWarnings("deprecation")
public long getUuidHigh() {
return high;
}

@Override
@SuppressWarnings("deprecation")
public long getUuidLow() {
return low;
}

@Override
public UUID getUuidJdk() {
long timeLow = (low & 0x00000000ffffffffL) << 32;
long timeMid = (low & 0x0000ffff00000000L) >>> 16;
long timeHighAndVersion = (low & 0xffff000000000000L) >>> 48;

long hiBe = LittleEndian.bswap(high);
return new UUID(timeLow | timeMid | timeHighAndVersion, hiBe);
}

@Override
public ValueProtos.Value toPb() {
return ProtoValue.fromUuid(high, low);
}

/** Returns val represented by the specified number of hex digits. */
private static String digits(long val, int digits) {
long high = 1L << (digits * 4);
return Long.toHexString(high | (val & (high - 1))).substring(1);
}
}
}
57 changes: 0 additions & 57 deletions table/src/test/java/tech/ydb/table/integration/NullReadTest.java

This file was deleted.

Loading

0 comments on commit 7722d88

Please sign in to comment.