/** * Gets a new time uuid using {@link ClockResolution#createClock()} as a time generator. * It is useful to use in a TimeUUIDType sorted column family. * * @param clock a ClockResolution * @return the time uuid */ public static java.util.UUID getTimeUUID(ClockResolution clock) { return getTimeUUID(clock.createClock()); }
@Test public void testTimeUUIDAsByteArray() { // Generate UUID, convert to array and back to UUID. Then assert. UUID uuid = TimeUUIDUtils.getUniqueTimeUUIDinMillis(); UUID uuidAfterConversion = TimeUUIDUtils.toUUID(TimeUUIDUtils.asByteArray(uuid)); assertEquals(uuid, uuidAfterConversion); // Used the previously generated UUID, convert to array and back to UUID. Then compare their times. long timeInUUID = TimeUUIDUtils.getTimeFromUUID(TimeUUIDUtils.asByteArray(uuid)); assertEquals((uuid.timestamp() - 0x01b21dd213814000L) / 10000, timeInUUID); }
@Test public void testTimeUUIDAsByteBuffer() { UUID expectedUuid = TimeUUIDUtils.getUniqueTimeUUIDinMillis(); UUID actualUuid = TimeUUIDUtils.uuid(TimeUUIDUtils.asByteBuffer(expectedUuid)); assertEquals(expectedUuid, actualUuid); }
/** * Retrieves the time as long based on the byte[] representation of a UUID. * * @param uuid byte[] uuid representation * @return a long representing the time */ public static long getTimeFromUUID(byte[] uuid) { return getTimeFromUUID(TimeUUIDUtils.toUUID(uuid)); }
log.info("----"); final UUID u1 = TimeUUIDUtils.getTimeUUID(originalTime); final long actual1 = TimeUUIDUtils.getTimeFromUUID(TimeUUIDUtils.asByteArray(u1)); log.info("Java UUID to bytes to time: " + actual1); log.info("Java UUID to bytes time to Date: " + new Date(actual1)); final long actual2 = TimeUUIDUtils.getTimeFromUUID(u1); log.info("Java UUID to time: " + actual2); log.info("Java UUID to time to Date: " + new Date(actual2));
@Test public void testDateUUIDConversion() { long originalTime = System.currentTimeMillis(); Date originalDate = new Date(originalTime); UUID originalUUID = TimeUUIDUtils.getTimeUUID(originalTime); long currentTime = TimeUUIDUtils.getTimeFromUUID(originalUUID); assertEquals(originalTime, currentTime); assertEquals(originalDate, new Date(currentTime)); }
/** * Rerturns a @see UUID based on time * @return the UUID */ public static java.util.UUID newTimeUuid() { return TimeUUIDUtils.getUniqueTimeUUIDinMillis(); }
/** * Coverts a java.util.UUID into a ByteBuffer. * @param uuid a java.util.UUID * @return a ByteBuffer representaion of the param UUID */ public static ByteBuffer asByteBuffer(java.util.UUID uuid) { if (uuid == null) { return null; } return ByteBuffer.wrap(asByteArray(uuid)); }
/** * Returns an instance of uuid. Useful for when you read out of cassandra * you are getting a byte[] that needs to be converted into a TimeUUID. * * @param uuid the uuid * @return the java.util.uuid */ public static java.util.UUID toUUID(byte[] uuid) { return uuid(uuid, 0); }
public static UUID decode(String encoded) { // TODO use with Guava 14 (Jclouds 1.6) // byte[] ba = BaseEncoding.base64Url().decode(encoded); // return TimeUUIDUtils.toUUID(ba); return TimeUUIDUtils.toUUID(DatatypeConverter.parseBase64Binary(encoded + "==")); } }
/** * Gets a new time uuid based on <code>time<code>. * NOTE: this algorithm does not resolve duplicates. To avoid duplicates use * {@link getTimeUUID(ClockResolution clock)} with an implementaion that provides unique timestamp resolution, like * {@link MicrosecondsSyncClockResolution} * It is useful to use in a TimeUUIDType sorted column family. * * @param clock a ClockResolution * @return the time uuid */ public static java.util.UUID getTimeUUID(long time) { return new java.util.UUID(createTime(time), UUIDGen.getClockSeqAndNode()); }
/** * Retrieves the time as long based on the byte[] representation of a UUID. * * @param uuid byte[] uuid representation * @return a long representing the time */ public static long getTimeFromUUID(byte[] uuid) { return getTimeFromUUID(TimeUUIDUtils.toUUID(uuid)); }
@Override protected UUID getMessageId(MessageIdBuilder builder) { return TimeUUIDUtils.getUniqueTimeUUIDinMillis(); }
/** * Coverts a java.util.UUID into a ByteBuffer. * @param uuid a java.util.UUID * @return a ByteBuffer representaion of the param UUID */ public static ByteBuffer asByteBuffer(java.util.UUID uuid) { if (uuid == null) { return null; } return ByteBuffer.wrap(asByteArray(uuid)); }
/** * Returns an instance of uuid. Useful for when you read out of cassandra * you are getting a byte[] that needs to be converted into a TimeUUID. * * @param uuid the uuid * @return the java.util.uuid */ public static java.util.UUID toUUID(byte[] uuid) { return uuid(uuid, 0); }
/** * Gets a new time uuid based on <code>time<code>. * NOTE: this algorithm does not resolve duplicates. To avoid duplicates use * {@link getTimeUUID(ClockResolution clock)} with an implementaion that provides unique timestamp resolution, like * {@link MicrosecondsSyncClockResolution} * It is useful to use in a TimeUUIDType sorted column family. * * @param clock a ClockResolution * @return the time uuid */ public static java.util.UUID getTimeUUID(long time) { return new java.util.UUID(createTime(time), UUIDGen.getClockSeqAndNode()); }
/** * This test must be placed FIRST. Please don't change the order. * @throws Exception */ @Test @Ignore public void testTimeUUIDWithClockResolution() throws Exception { ClockResolution clock = new MicrosecondsClockResolution(); long time = clock.createClock(); // Invoke twice with same time. Both generated UUID should be the same. // Test improved algorithm. assertEquals(TimeUUIDUtils.getTimeUUID(time), java.util.UUID.fromString( new com.eaio.uuid.UUID(UUIDGen.createTime(time), UUIDGen.getClockSeqAndNode()).toString())); clock = new MicrosecondsSyncClockResolution(); // Invoke twice with a clockResolution that guarantees unique timestamp. The second must be greater // than the first one. java.util.UUID first = TimeUUIDUtils.getTimeUUID(clock); java.util.UUID second = TimeUUIDUtils.getTimeUUID(clock); assertTrue(second.compareTo(first) > 0); }
/** * Retrieves the time as long based on the byte[] representation of a UUID. * * @param uuid byte[] uuid representation * @return a long representing the time */ public static long getTimeFromUUID(byte[] uuid) { return getTimeFromUUID(TimeUUIDUtils.toUUID(uuid)); }
@Before public void setUp() { cluster = getOrCreateCluster("Test Cluster", "127.0.0.1:9170"); keyspace = createKeyspace("Keyspace1", cluster); Mutator<String> m = createMutator(keyspace, se); for (int i = 0; i < 1000; i++) { m.addInsertion(KEY, CF, createColumn(TimeUUIDUtils.getUniqueTimeUUIDinMillis(), String.valueOf(i), us, se)); } m.execute(); }
/** * Coverts a java.util.UUID into a ByteBuffer. * @param uuid a java.util.UUID * @return a ByteBuffer representaion of the param UUID */ public static ByteBuffer asByteBuffer(java.util.UUID uuid) { if (uuid == null) { return null; } return ByteBuffer.wrap(asByteArray(uuid)); }