Java Code Examples for java.util.UUID#timestamp()

The following examples show how to use java.util.UUID#timestamp() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpdateProcessor.java    From Rhombus with MIT License 6 votes vote down vote up
protected List<Map<String,Object>> findUpdatesWithinTimeframe(IndexUpdateRow row, Long timeInNannos){
	List<Map<String,Object>> ret = Lists.newArrayList();
	UUID newer = null;
	int i = 0;
	for(UUID current : row.getIds()){
		if(newer != null){
			///do stuff
			Long difference = newer.timestamp() - current.timestamp();
			if(difference < timeInNannos){
				Map<String,Object> toadd = Maps.newHashMap();
				toadd.put("rowkey", row.getRowKey());
				toadd.put("new-item", row.getIndexValues().get(i));
				toadd.put("old-item", row.getIndexValues().get(i-1));
				toadd.put("difference", difference);
				ret.add(toadd);
			}
		}
		newer = current;
		i++;
	}
	return ret;
}
 
Example 2
Source File: SnippetUtilsTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateWithoutSeedSameCurrentIdIsCopySet() throws Exception {
    Method generateIdMethod = SnippetUtils.class.getDeclaredMethod("generateId", String.class, String.class,
            boolean.class);
    generateIdMethod.setAccessible(true);

    boolean isCopy = true;

    SnippetUtils utils = new SnippetUtils();
    String currentId = ComponentIdGenerator.generateId().toString();
    UUID id1 = UUID.fromString((String) generateIdMethod.invoke(utils, currentId, null, isCopy));
    UUID id2 = UUID.fromString((String) generateIdMethod.invoke(utils, currentId, null, isCopy));
    UUID id3 = UUID.fromString((String) generateIdMethod.invoke(utils, currentId, null, isCopy));
    // below simply validates that generated UUID is type-one, since timestamp() operation will result
    // in exception if generated UUID is not type-one
    id1.timestamp();
    id2.timestamp();
    id3.timestamp();
    assertTrue(id1.getMostSignificantBits() < id2.getMostSignificantBits());
    assertTrue(id2.getMostSignificantBits() < id3.getMostSignificantBits());
}
 
Example 3
Source File: UUIDUtils.java    From usergrid with Apache License 2.0 6 votes vote down vote up
/** Returns a UUID that is -1 of the passed uuid, sorted by time uuid only */
public static UUID decrement( UUID uuid ) {
    if ( !isTimeBased( uuid ) ) {
        throw new IllegalArgumentException( "The uuid must be a time type" );
    }


    //timestamp is in the 60 bit timestamp
    long timestamp = uuid.timestamp();
    timestamp--;

    if ( timestamp < 0 ) {
        throw new IllegalArgumentException( "You must specify a time uuid with a timestamp > 0" );
    }

    //get our bytes, then set the smaller timestamp into it
    byte[] uuidBytes = bytes( uuid );

    setTime( uuidBytes, timestamp );

    return uuid( uuidBytes );
}
 
Example 4
Source File: UUIDUtils.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public static long getTimestampInMicros( UUID uuid ) {
    if ( uuid == null ) {
        return 0;
    }
    long t = uuid.timestamp();
    return ( t - KCLOCK_OFFSET ) / 10;
}
 
Example 5
Source File: UuidTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimeBaseUuid() {
    final UUID uuid = UuidUtil.getTimeBasedUuid();
    //final UUID uuid2 = UuidUtil.getTimeBasedUUID(); // unused
    final long current = (System.currentTimeMillis() * 10000) + NUM_100NS_INTERVALS_SINCE_UUID_EPOCH;
    final long time = uuid.timestamp();
    assertTrue("Incorrect time", current + 10000 - time > 0);
    final UUID[] uuids = new UUID[COUNT];
    final long start = System.nanoTime();
    for (int i=0; i < COUNT; ++i) {
        uuids[i] = UuidUtil.getTimeBasedUuid();
    }
    final long elapsed = System.nanoTime() - start;
    System.out.println("Elapsed for " + COUNT + " UUIDS = " + elapsed + " Average = " + elapsed / COUNT + " ns");
    int errors = 0;
    for (int i=0; i < COUNT; ++i) {
        for (int j=i+1; j < COUNT; ++j) {
            if (uuids[i].equals(uuids[j])) {
                ++errors;
                System.out.println("UUID " + i + " equals UUID " + j);
            }
        }
    }
    assertEquals(errors + " duplicate UUIDS", 0, errors);
    final int variant = uuid.variant();
    assertEquals("Incorrect variant. Expected 2 got " + variant, 2, variant);
    final int version = uuid.version();
    assertEquals("Incorrect version. Expected 1 got " + version, 1, version);
    final long node = uuid.node();
    assertTrue("Invalid node", node != 0);
}
 
Example 6
Source File: TimeUUIDs.java    From emodb with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the smallest valid time UUID that is greater than the specified time uuid based on
 * {@link UUID#compareTo(java.util.UUID)}}, or <code>null</code> if the uuid is greater than or
 * equal to {@link #maximumUuid()}.
 */
public static UUID getNext(UUID uuid) {
    checkArgument(uuid.version() == 1, "Not a time UUID");
    UUID max = maximumUuid();
    long lsb = uuid.getLeastSignificantBits();
    if (lsb < max.getLeastSignificantBits()) {
        return new UUID(uuid.getMostSignificantBits(), lsb + 1);
    }
    long timestamp = uuid.timestamp();
    if (timestamp < max.timestamp()) {
        return new UUID(getMostSignificantBits(timestamp + 1), minimumUuid().getLeastSignificantBits());
    }
    return null;  // No next exists since uuid == maximumUuid()
}
 
Example 7
Source File: UUIDTools.java    From elasticactors with Apache License 2.0 5 votes vote down vote up
public static long toUnixTimestamp(UUID uuid) {
    long t = uuid.timestamp();
    // 0x01b21dd213814000 is the number of 100-ns intervals between the
    // UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
    t = t - 0x01b21dd213814000L;
    t = (long) (t / 1e4); //Convert to ms
    return t;
}
 
Example 8
Source File: SnippetUtilsTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateWithSameSeedSameInceptionIdNotSameInstanceIdIsCopySet() throws Exception {
    Method generateIdMethod = SnippetUtils.class.getDeclaredMethod("generateId", String.class, String.class,
            boolean.class);
    generateIdMethod.setAccessible(true);

    SnippetUtils utils = new SnippetUtils();
    String seed = ComponentIdGenerator.generateId().toString();

    UUID rootId = ComponentIdGenerator.generateId();

    String id1 = (String) generateIdMethod.invoke(utils,
            new UUID(rootId.getMostSignificantBits(), ComponentIdGenerator.generateId().getLeastSignificantBits()).toString(),
            seed, true);
    String id2 = (String) generateIdMethod.invoke(utils,
            new UUID(rootId.getMostSignificantBits(), ComponentIdGenerator.generateId().getLeastSignificantBits()).toString(),
            seed, true);
    String id3 = (String) generateIdMethod.invoke(utils,
            new UUID(rootId.getMostSignificantBits(), ComponentIdGenerator.generateId().getLeastSignificantBits()).toString(),
            seed, true);
    assertNotEquals(id1, id2);
    assertNotEquals(id2, id3);
    UUID uuid1 = UUID.fromString(id1);
    UUID uuid2 = UUID.fromString(id2);
    UUID uuid3 = UUID.fromString(id3);
    // below simply validates that generated UUID is type-one, since timestamp() operation will result
    // in exception if generated UUID is not type-one
    uuid1.timestamp();
    uuid2.timestamp();
    uuid3.timestamp();
    assertNotEquals(uuid1.getMostSignificantBits(), uuid2.getMostSignificantBits());
    assertNotEquals(uuid2.getMostSignificantBits(), uuid3.getMostSignificantBits());
}
 
Example 9
Source File: UUIDUtils.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public static long getTimestampInMicros( UUID uuid ) {
    if ( uuid == null ) {
        return 0;
    }
    long t = uuid.timestamp();
    return ( t - KCLOCK_OFFSET ) / 10;
}
 
Example 10
Source File: SnippetUtilsTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateWithSameSeedSameInceptionIdNotSameInstanceIdIsCopyNotSet() throws Exception {
    Method generateIdMethod = SnippetUtils.class.getDeclaredMethod("generateId", String.class, String.class,
            boolean.class);
    generateIdMethod.setAccessible(true);

    SnippetUtils utils = new SnippetUtils();
    String seed = ComponentIdGenerator.generateId().toString();

    UUID rootId = ComponentIdGenerator.generateId();

    String id1 = (String) generateIdMethod.invoke(utils,
            new UUID(rootId.getMostSignificantBits(), ComponentIdGenerator.generateId().getLeastSignificantBits()).toString(),
            seed, false);
    String id2 = (String) generateIdMethod.invoke(utils,
            new UUID(rootId.getMostSignificantBits(), ComponentIdGenerator.generateId().getLeastSignificantBits()).toString(),
            seed, false);
    String id3 = (String) generateIdMethod.invoke(utils,
            new UUID(rootId.getMostSignificantBits(), ComponentIdGenerator.generateId().getLeastSignificantBits()).toString(),
            seed, false);
    assertNotEquals(id1, id2);
    assertNotEquals(id2, id3);
    UUID uuid1 = UUID.fromString(id1);
    UUID uuid2 = UUID.fromString(id2);
    UUID uuid3 = UUID.fromString(id3);
    // below simply validates that generated UUID is type-one, since timestamp() operation will result
    // in exception if generated UUID is not type-one
    uuid1.timestamp();
    uuid2.timestamp();
    uuid3.timestamp();
    assertEquals(uuid1.getMostSignificantBits(), uuid2.getMostSignificantBits());
    assertEquals(uuid2.getMostSignificantBits(), uuid3.getMostSignificantBits());
}
 
Example 11
Source File: SnippetUtilsTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateWithSameSeedSameInceptionIdNotSameInstanceIdIsCopySet() throws Exception {
    Method generateIdMethod = SnippetUtils.class.getDeclaredMethod("generateId", String.class, String.class,
            boolean.class);
    generateIdMethod.setAccessible(true);

    SnippetUtils utils = new SnippetUtils();
    String seed = ComponentIdGenerator.generateId().toString();

    UUID rootId = ComponentIdGenerator.generateId();

    String id1 = (String) generateIdMethod.invoke(utils,
            new UUID(rootId.getMostSignificantBits(), ComponentIdGenerator.generateId().getLeastSignificantBits()).toString(),
            seed, true);
    String id2 = (String) generateIdMethod.invoke(utils,
            new UUID(rootId.getMostSignificantBits(), ComponentIdGenerator.generateId().getLeastSignificantBits()).toString(),
            seed, true);
    String id3 = (String) generateIdMethod.invoke(utils,
            new UUID(rootId.getMostSignificantBits(), ComponentIdGenerator.generateId().getLeastSignificantBits()).toString(),
            seed, true);
    assertNotEquals(id1, id2);
    assertNotEquals(id2, id3);
    UUID uuid1 = UUID.fromString(id1);
    UUID uuid2 = UUID.fromString(id2);
    UUID uuid3 = UUID.fromString(id3);
    // below simply validates that generated UUID is type-one, since timestamp() operation will result
    // in exception if generated UUID is not type-one
    uuid1.timestamp();
    uuid2.timestamp();
    uuid3.timestamp();
    assertNotEquals(uuid1.getMostSignificantBits(), uuid2.getMostSignificantBits());
    assertNotEquals(uuid2.getMostSignificantBits(), uuid3.getMostSignificantBits());
}
 
Example 12
Source File: AbstractBackupTest.java    From cassandra-backup with Apache License 2.0 4 votes vote down vote up
private Date uuidToDate(final UUID uuid) {
    return new Date(uuid.timestamp() / 10000L - 12219292800000L);
}
 
Example 13
Source File: UUIDs.java    From datacollector with Apache License 2.0 3 votes vote down vote up
/**
 * Return the unix timestamp contained by the provided time-based UUID.
 * <p>
 * This method is not equivalent to {@code 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 what {@code uuid.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 timestamp returned by
 * {@link System#currentTimeMillis}, {@link Date#getTime}, etc.
 *
 * @param uuid the UUID to return the timestamp of.
 * @return the unix timestamp of {@code uuid}.
 *
 * @throws IllegalArgumentException if {@code uuid} is not a version 1 UUID.
 */
public static long unixTimestamp(UUID uuid) {
  if (uuid.version() != 1)
    throw new IllegalArgumentException(String.format("Can only retrieve the unix timestamp for version 1 uuid (provided version %d)", uuid.version()));

  long timestamp = uuid.timestamp();
  return (timestamp / 10000) + START_EPOCH;
}
 
Example 14
Source File: UUIDGen.java    From stratio-cassandra with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a milliseconds-since-epoch value for a type-1 UUID.
 *
 * @param uuid a type-1 (time-based) UUID
 * @return the number of milliseconds since the unix epoch
 * @throws IllegalArgumentException if the UUID is not version 1
 */
public static long getAdjustedTimestamp(UUID uuid)
{
    if (uuid.version() != 1)
        throw new IllegalArgumentException("incompatible with uuid version: "+uuid.version());
    return (uuid.timestamp() / 10000) + START_EPOCH;
}
 
Example 15
Source File: UUIDGen.java    From sfs with Apache License 2.0 2 votes vote down vote up
/**
 * @param uuid
 * @return milliseconds since Unix epoch
 */
public static long unixTimestamp(UUID uuid) {
    return (uuid.timestamp() / 10000) + START_EPOCH;
}
 
Example 16
Source File: UUIDGen.java    From sfs with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a milliseconds-since-epoch value for a type-1 UUID.
 *
 * @param uuid a type-1 (time-based) UUID
 * @return the number of milliseconds since the unix epoch
 * @throws IllegalArgumentException if the UUID is not version 1
 */
public static long getAdjustedTimestamp(UUID uuid) {
    if (uuid.version() != 1)
        throw new IllegalArgumentException("incompatible with uuid version: " + uuid.version());
    return (uuid.timestamp() / 10000) + START_EPOCH;
}
 
Example 17
Source File: TimeUUIDs.java    From emodb with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the timestamp when the UUID was created, truncated to millisecond
 * boundary.
 * @throws UnsupportedOperationException if the uuid is not a timestamp UUID
 */
public static long getTimeMillis(UUID uuid) {
    return (uuid.timestamp() - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH) / 10000;
}
 
Example 18
Source File: UUIDGen.java    From hawkular-metrics with Apache License 2.0 2 votes vote down vote up
/**
 * @param uuid
 * @return milliseconds since Unix epoch
 */
public static long unixTimestamp(UUID uuid) {
    return (uuid.timestamp() / 10000) + START_EPOCH;
}
 
Example 19
Source File: UUIDGen.java    From hawkular-metrics with Apache License 2.0 2 votes vote down vote up
/**
 * @param uuid
 * @return microseconds since Unix epoch
 */
public static long microsTimestamp(UUID uuid) {
    return (uuid.timestamp() / 10) + START_EPOCH * 1000;
}
 
Example 20
Source File: UUIDGen.java    From hawkular-metrics with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a milliseconds-since-epoch value for a type-1 UUID.
 *
 * @param uuid a type-1 (time-based) UUID
 * @return the number of milliseconds since the unix epoch
 * @throws IllegalArgumentException if the UUID is not version 1
 */
public static long getAdjustedTimestamp(UUID uuid) {
    if (uuid.version() != 1)
        throw new IllegalArgumentException("incompatible with uuid version: " + uuid.version());
    return (uuid.timestamp() / 10000) + START_EPOCH;
}