Skip to content

Instantly share code, notes, and snippets.

@gamebusterz
Created July 15, 2020 15:40
Show Gist options
  • Save gamebusterz/f31d0d2201cd90d472f49ab66dad3c77 to your computer and use it in GitHub Desktop.
Save gamebusterz/f31d0d2201cd90d472f49ab66dad3c77 to your computer and use it in GitHub Desktop.
@Description("Generates a deterministic (type 3) UUID using the specified namespace UUID and input name (using MD5 hash)")
@ScalarFunction()
@SqlType(StandardTypes.UUID)
public static Slice uuid_v3(@SqlType(StandardTypes.UUID) Slice nameSpaceUuid,@SqlType(StandardTypes.VARCHAR) Slice name)
{
java.util.UUID nameUuid = new java.util.UUID(nameSpaceUuid.getLong(0), nameSpaceUuid.getLong(SIZE_OF_LONG));
byte[] value = name.getBytes();
byte[] b = Hashing.md5().newHasher()
.putLong(Long.reverse(nameUuid.getMostSignificantBits()))
.putLong(Long.reverse(nameUuid.getLeastSignificantBits()))
.putBytes(value)
.hash().asBytes();
long msb = Longs.fromBytes(b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
long lsb = Longs.fromBytes(b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]);
return wrappedLongArray(msb, lsb);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment