Class UUIDs
- java.lang.Object
-
- com.datastax.driver.core.utils.UUIDs
-
public final class UUIDs extends Object
Utility methods to help working with UUIDs, and more specifically, with time-based UUIDs (also known as Version 1 UUIDs).Notes on the algorithm used to generate time-based UUIDs
The algorithm follows roughly the description in RFC-4122, but with the following adaptations:- Since Java does not provide direct access to the host's MAC address, that information is replaced with a digest of all IP addresses available on the host;
- The process ID (PID) isn't easily available to Java either, so it is determined by one of
the following methods, in the order they are listed below:
- If the System property
"com.datastax.driver.PID"is set then the value to use as a PID will be read from that property; - Otherwise, if a native call to
getpid()is possible, then the PID will be read from that call; - Otherwise, an attempt will be made to read the PID from JMX's
RuntimeMXBean, which is a well-known, yet undocumented "hack", since most JVMs tend to use the JVM's PID as part of that MXBean name; - If all of the above fails, a random integer will be generated and used as a surrogate PID.
- If the System property
-
-
Field Summary
Fields Modifier and Type Field Description static StringPID_SYSTEM_PROPERTYThe System property to use to force the value of the process ID (PID).
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static UUIDendOf(long timestamp)Creates a "fake" time-based UUID that sorts as the biggest possible version 1 UUID generated at the provided timestamp.static UUIDrandom()Creates a new random (version 4) UUID.static UUIDstartOf(long timestamp)Creates a "fake" time-based UUID that sorts as the smallest possible version 1 UUID generated at the provided timestamp.static UUIDtimeBased()Creates a new time-based (version 1) UUID.static longunixTimestamp(UUID uuid)Return the Unix timestamp contained by the provided time-based UUID.
-
-
-
Field Detail
-
PID_SYSTEM_PROPERTY
public static final String PID_SYSTEM_PROPERTY
The System property to use to force the value of the process ID (PID).- See Also:
- Constant Field Values
-
-
Method Detail
-
random
public static UUID random()
Creates a new random (version 4) UUID.This method is just a convenience for
UUID.randomUUID().- Returns:
- a newly generated, pseudo random, version 4 UUID.
-
timeBased
public static UUID timeBased()
Creates a new time-based (version 1) UUID.UUIDs generated by this method are suitable for use with the
timeuuidCassandra type. In particular the generated UUID includes the timestamp of its generation.Note that there is no way to provide your own timestamp. This is deliberate, as we feel that this does not conform to the UUID specification, and therefore don't want to encourage it through the API. If you want to do it anyway, use the following workaround:
Random random = new Random(); UUID uuid = new UUID(UUIDs.startOf(userProvidedTimestamp).getMostSignificantBits(), random.nextLong());
If you simply need to perform a range query on atimeuuidcolumn, use the "fake" UUID generated bystartOf(long)andendOf(long).- Returns:
- a new time-based UUID.
-
startOf
public static UUID startOf(long timestamp)
Creates a "fake" time-based UUID that sorts as the smallest possible version 1 UUID generated at the provided timestamp.Such created UUIDs are useful in queries to select a time range of a
timeuuidcolumn.The UUIDs created by this method are not unique and as such are not suitable for anything else than querying a specific time range. In particular, you should not insert such UUIDs. "True" UUIDs from user-provided timestamps are not supported (see
timeBased()for more explanations).Also, the timestamp to provide as a parameter must be a Unix timestamp (as returned by
System.currentTimeMillis()orDate.getTime()), and not a count of 100-nanosecond intervals since 00:00:00.00, 15 October 1582 (as required by RFC-4122).In other words, given a UUID
uuid, you should never callstartOf(uuid.timestamp())but ratherstartOf(unixTimestamp(uuid)).Lastly, please note that Cassandra's
timeuuidsorting is not compatible withUUID.compareTo(java.util.UUID)and hence the UUIDs created by this method are not necessarily lower bound for that latter method.- Parameters:
timestamp- the Unix timestamp for which the created UUID must be a lower bound.- Returns:
- the smallest (for Cassandra
timeuuidsorting) UUID oftimestamp.
-
endOf
public static UUID endOf(long timestamp)
Creates a "fake" time-based UUID that sorts as the biggest possible version 1 UUID generated at the provided timestamp.See
startOf(long)for explanations about the intended usage of such UUID.- Parameters:
timestamp- the Unix timestamp for which the created UUID must be an upper bound.- Returns:
- the biggest (for Cassandra
timeuuidsorting) UUID oftimestamp.
-
unixTimestamp
public static long unixTimestamp(UUID uuid)
Return the Unix timestamp contained by the provided time-based UUID.This method is not equivalent to
UUID.timestamp(). More precisely, a version 1 UUID stores a timestamp that represents the number of 100-nanoseconds intervals since midnight, 15 October 1582 and that is whatUUID.timestamp()returns. This method however converts that timestamp to the equivalent Unix timestamp in milliseconds, i.e. a timestamp representing a number of milliseconds since midnight, January 1, 1970 UTC. In particular, the timestamps returned by this method are comparable to the timestamps returned bySystem.currentTimeMillis(),Date.getTime(), etc.- Parameters:
uuid- the UUID to return the timestamp of.- Returns:
- the Unix timestamp of
uuid. - Throws:
IllegalArgumentException- ifuuidis not a version 1 UUID.
-
-