Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add API to get UTF8String UUID.
  • Loading branch information
viirya committed Mar 16, 2018
commit 75b80a249c9e5492e6fe159ac527feaea4f46c5a
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import java.util.UUID

import org.apache.commons.math3.random.MersenneTwister

import org.apache.spark.unsafe.types.UTF8String

/**
* This class is used to generate a UUID from Pseudo-Random Numbers.
*
Expand All @@ -36,4 +38,6 @@ case class RandomUUIDGenerator(randomSeed: Long) {

new UUID(mostSigBits, leastSigBits)
Copy link
Contributor

@hvanhovell hvanhovell Mar 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to use a different RNG. java.util.Random only has 48 bits of state, which is less than the 122 bits we need for UUID generation. Something like PCG or a Mersenne twister would work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Mersenne Twister is used in the update.

}

def getNextUUIDUTF8String(): UTF8String = UTF8String.fromString(getNextUUID().toString())
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ class RandomUUIDGeneratorSuite extends SparkFunSuite {
assert(uuid1 != uuid3)
}
}

test("Get UTF8String UUID") {
val generator = RandomUUIDGenerator(new Random().nextLong())
val utf8StringUUID = generator.getNextUUIDUTF8String()
val uuid = java.util.UUID.fromString(utf8StringUUID.toString)
assert(uuid.version() == 4 && uuid.variant() == 2 && utf8StringUUID.toString == uuid.toString)
}
}