Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package me.prettyprint.cassandra.utils;
  
  import java.util.UUID;
  
  
Utilitary class to generate TimeUUID (type 1)

Author(s):
Patricio Echague (pechague@gmail.com)
 
 public final class TimeUUIDUtils {

  
Gets a new and unique time uuid in milliseconds. It is useful to use in a TimeUUIDType sorted column family.

Returns:
the time uuid
 
   public static java.util.UUID getUniqueTimeUUIDinMillis() {
     return new java.util.UUID(UUIDGen.newTime(), UUIDGen.getClockSeqAndNode());
   }

  
Gets a new time uuid using me.prettyprint.hector.api.ClockResolution.createClock() as a time generator. It is useful to use in a TimeUUIDType sorted column family.

Parameters:
clock a ClockResolution
Returns:
the time uuid
 
   public static java.util.UUID getTimeUUID(ClockResolution clock) {
     return getTimeUUID(clock.createClock());
   }

  
Gets a new time uuid based on time. NOTE: this algorithm does not resolve duplicates. To avoid duplicates use getTimeUUID(ClockResolution clock) with an implementaion that provides unique timestamp resolution, like me.prettyprint.cassandra.service.clock.MicrosecondsSyncClockResolution It is useful to use in a TimeUUIDType sorted column family.

Parameters:
clock a ClockResolution
Returns:
the time uuid
 
   public static java.util.UUID getTimeUUID(long time) {
     return new java.util.UUID(createTime(time), UUIDGen.getClockSeqAndNode());
   }
 
   private static long createTime(long currentTime) {
     long time;
 
     // UTC time
     long timeToUse = (currentTime * 10000) + 0x01B21DD213814000L;
 
     // time low
     time = timeToUse << 32;
 
     // time mid
     time |= (timeToUse & 0xFFFF00000000L) >> 16;
 
     // time hi and version
     time |= 0x1000 | ((timeToUse >> 48) & 0x0FFF); // version 1
     return time;
   }


  
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.

Parameters:
uuid the uuid
Returns:
the java.util.uuid
 
   public static java.util.UUID toUUID(byte[] uuid) {
     return uuid(uuid, 0);
   }

  
Retrieves the time as long based on the byte[] representation of a UUID.

Parameters:
uuid byte[] uuid representation
Returns:
a long representing the time
 
   public static long getTimeFromUUID(byte[] uuid) {
     return TimeUUIDUtils.toUUID(uuid).timestamp();
   }

  
As byte array. This method is often used in conjunction with

Parameters:
uuid the uuid
Returns:
the byte[]
Link:
getTimeUUID(me.prettyprint.hector.api.ClockResolution)
 
  public static byte[] asByteArray(java.util.UUID uuid) {
    long msb = uuid.getMostSignificantBits();
    long lsb = uuid.getLeastSignificantBits();
    byte[] buffer = new byte[16];
    for (int i = 0; i < 8; i++) {
      buffer[i] = (byte) (msb >>> 8 * (7 - i));
    }
    for (int i = 8; i < 16; i++) {
      buffer[i] = (byte) (lsb >>> 8 * (7 - i));
    }
    return buffer;
  }

  
Coverts a java.util.UUID into a ByteBuffer.

Parameters:
uuid a java.util.UUID
Returns:
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));
  }
  public static UUID uuid(byte[] uuidint offset) {
    ByteBuffer bb = ByteBuffer.wrap(uuidoffset, 16);
    return new UUID(bb.getLong(), bb.getLong());
  }

  
Converts a ByteBuffer containing a UUID into a java.util.UUID

Parameters:
bb a ByteBuffer containing a UUID
Returns:
a java.util.UUID
  public static UUID uuid(ByteBuffer bb) {
    bb = bb.slice();
    return new UUID(bb.getLong(), bb.getLong());
  }
New to GrepCode? Check out our FAQ X