Package org.apache.cassandra.utils
Class UUIDGen
- java.lang.Object
-
- org.apache.cassandra.utils.UUIDGen
-
public class UUIDGen extends java.lang.Object
The goods are here: www.ietf.org/rfc/rfc4122.txt.
-
-
Field Summary
Fields Modifier and Type Field Description static int
UUID_LEN
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]
decompose(java.util.UUID uuid)
decomposes a uuid into raw bytes.static long
getAdjustedTimestamp(java.util.UUID uuid)
Returns a milliseconds-since-epoch value for a type-1 UUID.static java.util.Collection<InetAddressAndPort>
getAllLocalAddresses()
Helper function used exclusively by UUIDGen to createstatic java.util.UUID
getRandomTimeUUIDFromMicros(long whenInMicros)
Similar togetTimeUUIDFromMicros(long)
, but randomize (using SecureRandom) the clock and sequence.static java.util.UUID
getTimeUUID()
Creates a type 1 UUID (time-based UUID).static java.util.UUID
getTimeUUID(long when)
Creates a type 1 UUID (time-based UUID) with the timestamp of @param when, in milliseconds.static java.util.UUID
getTimeUUID(long when, long nanos)
static java.util.UUID
getTimeUUID(long when, long nanos, long clockSeqAndNode)
static byte[]
getTimeUUIDBytes()
Returns a 16 byte representation of a type 1 UUID (a time-based UUID), based on the current system time.static byte[]
getTimeUUIDBytes(long timeMillis, int nanos)
Converts a 100-nanoseconds precision timestamp into the 16 byte representation of a type 1 UUID (a time-based UUID).static java.util.UUID
getTimeUUIDFromMicros(long whenInMicros)
Returns a version 1 UUID using the provided timestamp and the local clock and sequence.static java.util.UUID
getUUID(java.nio.ByteBuffer raw)
creates a type 1 uuid from raw bytes.static java.util.UUID
maxTimeUUID(long timestamp)
Returns the biggest possible type 1 UUID having the provided timestamp.static long
microsTimestamp(java.util.UUID uuid)
static java.util.UUID
minTimeUUID(long timestamp)
Returns the smaller possible type 1 UUID having the provided timestamp.static java.nio.ByteBuffer
toByteBuffer(java.util.UUID uuid)
static long
unixTimestamp(java.util.UUID uuid)
static int
unixTimestampInSec(java.util.UUID uuid)
static void
updateWithLong(com.google.common.hash.Hasher hasher, long val)
-
-
-
Field Detail
-
UUID_LEN
public static final int UUID_LEN
- See Also:
- Constant Field Values
-
-
Method Detail
-
getTimeUUID
public static java.util.UUID getTimeUUID()
Creates a type 1 UUID (time-based UUID).- Returns:
- a UUID instance
-
getTimeUUID
public static java.util.UUID getTimeUUID(long when)
Creates a type 1 UUID (time-based UUID) with the timestamp of @param when, in milliseconds.- Returns:
- a UUID instance
-
getTimeUUIDFromMicros
public static java.util.UUID getTimeUUIDFromMicros(long whenInMicros)
Returns a version 1 UUID using the provided timestamp and the local clock and sequence.Note that this method is generally only safe to use if you can guarantee that the provided parameter is unique across calls (otherwise the returned UUID won't be unique accross calls).
- Parameters:
whenInMicros
- a unix time in microseconds.- Returns:
- a new UUID
id
such thatmicrosTimestamp(id) == whenInMicros
. Please not that multiple calls to this method with the same value ofwhenInMicros
will return the same UUID.
-
getRandomTimeUUIDFromMicros
public static java.util.UUID getRandomTimeUUIDFromMicros(long whenInMicros)
Similar togetTimeUUIDFromMicros(long)
, but randomize (using SecureRandom) the clock and sequence.If you can guarantee that the
whenInMicros
argument is unique (for this JVM instance) for every call, then you should prefergetTimeUUIDFromMicros(long)
which is faster. If you can't guarantee this however, this method will ensure the returned UUID are still unique (accross calls) through randomization.- Parameters:
whenInMicros
- a unix time in microseconds.- Returns:
- a new UUID
id
such thatmicrosTimestamp(id) == whenInMicros
. The UUID returned by different calls will be unique even ifwhenInMicros
is not.
-
getTimeUUID
public static java.util.UUID getTimeUUID(long when, long nanos)
-
getTimeUUID
public static java.util.UUID getTimeUUID(long when, long nanos, long clockSeqAndNode)
-
getUUID
public static java.util.UUID getUUID(java.nio.ByteBuffer raw)
creates a type 1 uuid from raw bytes.
-
toByteBuffer
public static java.nio.ByteBuffer toByteBuffer(java.util.UUID uuid)
-
decompose
public static byte[] decompose(java.util.UUID uuid)
decomposes a uuid into raw bytes.
-
getTimeUUIDBytes
public static byte[] getTimeUUIDBytes()
Returns a 16 byte representation of a type 1 UUID (a time-based UUID), based on the current system time.- Returns:
- a type 1 UUID represented as a byte[]
-
minTimeUUID
public static java.util.UUID minTimeUUID(long timestamp)
Returns the smaller possible type 1 UUID having the provided timestamp. Warning: this method should only be used for querying as this doesn't at all guarantee the uniqueness of the resulting UUID.
-
maxTimeUUID
public static java.util.UUID maxTimeUUID(long timestamp)
Returns the biggest possible type 1 UUID having the provided timestamp. Warning: this method should only be used for querying as this doesn't at all guarantee the uniqueness of the resulting UUID.
-
unixTimestamp
public static long unixTimestamp(java.util.UUID uuid)
- Parameters:
uuid
-- Returns:
- milliseconds since Unix epoch
-
unixTimestampInSec
public static int unixTimestampInSec(java.util.UUID uuid)
- Parameters:
uuid
-- Returns:
- seconds since Unix epoch
-
microsTimestamp
public static long microsTimestamp(java.util.UUID uuid)
- Parameters:
uuid
-- Returns:
- microseconds since Unix epoch
-
getTimeUUIDBytes
public static byte[] getTimeUUIDBytes(long timeMillis, int nanos)
Converts a 100-nanoseconds precision timestamp into the 16 byte representation of a type 1 UUID (a time-based UUID). To specify a 100-nanoseconds precision timestamp, one should provide a milliseconds timestamp and a number0 <= n < 10000
such that n*100 is the number of nanoseconds within that millisecond.Warning: This method is not guaranteed to return unique UUIDs; Multiple invocations using identical timestamps will result in identical UUIDs.
- Returns:
- a type 1 UUID represented as a byte[]
-
getAdjustedTimestamp
public static long getAdjustedTimestamp(java.util.UUID uuid)
Returns a milliseconds-since-epoch value for a type-1 UUID.- Parameters:
uuid
- a type-1 (time-based) UUID- Returns:
- the number of milliseconds since the unix epoch
- Throws:
java.lang.IllegalArgumentException
- if the UUID is not version 1
-
updateWithLong
public static void updateWithLong(com.google.common.hash.Hasher hasher, long val)
-
getAllLocalAddresses
public static java.util.Collection<InetAddressAndPort> getAllLocalAddresses()
Helper function used exclusively by UUIDGen to create
-
-