Java Code Examples for com.datastax.driver.core.utils.UUIDs#startOf()

The following examples show how to use com.datastax.driver.core.utils.UUIDs#startOf() . 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: CObjectCQLGenerator.java    From Rhombus with MIT License 5 votes vote down vote up
/**
 *
 * @param objType - The name of the Object type aka CDefinition.name
 * @param criteria - The criteria object describing which rows to retrieve
 * @param countOnly - true means you want a count of rows, false means you want the rows themselves
 * @return Iterator of CQL statements that need to be executed for this task.
 */
@NotNull
public CQLStatementIterator makeCQLforList(String objType, Criteria criteria, boolean countOnly) throws CQLGenerationException {
	CDefinition definition = this.definitions.get(objType);
	CObjectOrdering ordering = (criteria.getOrdering() != null ? criteria.getOrdering(): CObjectOrdering.DESCENDING);
	UUID endUuid = (criteria.getEndUuid() == null ? UUIDs.startOf(DateTime.now().getMillis()) : criteria.getEndUuid());
	return makeCQLforList(this.keyspace, shardList, definition, criteria.getIndexKeys(), ordering, criteria.getStartUuid(),
			endUuid, criteria.getLimit(), criteria.getInclusive(), countOnly, criteria.getAllowFiltering());
}
 
Example 2
Source File: FakeCIndexTest.java    From Rhombus with MIT License 5 votes vote down vote up
@Test
public void getMasterIteratorTest() throws Exception {

	String json = TestHelpers.readFileToString(this.getClass(), "CObjectCQLGeneratorTestData.js");
	CDefinition def = CDefinition.fromJsonString(json);
	Long millistamp = System.currentTimeMillis();
	UUID startingUUID = UUIDs.startOf(millistamp);

	FakeCIndex subject = new FakeCIndex(
			def.getIndexes().get("instance:type"),
			def,
			startingUUID,
			100L,
			50L,
			10L );
	Iterator<Map<String,Object>> masterIt = subject.getMasterIterator(CObjectOrdering.DESCENDING);
	Set<String> idSet = Sets.newHashSet();

	long counter = 0;
	while(masterIt.hasNext()){
		counter++;
		Map<String,Object> item = masterIt.next();
		assertFalse("Id is not unique", idSet.contains(item.get("id").toString()));
		idSet.add(item.get("id").toString());
		System.out.println(item);
	}

	assertEquals(50L,counter);


}
 
Example 3
Source File: FakeIdRange.java    From Rhombus with MIT License 5 votes vote down vote up
public Object getIdAtCounter(Long counter, TimebasedShardingStrategy shardingStrategy)  throws RhombusException {
	//get current shard number
	Long indexOfShard = startingShardNumber + (counter/objectsPerShard);

	//get location in that shard
	Long itemInShard = counter%objectsPerShard;

	//get the id unit offset at the beginning of that shard

	//turn it into an id

	if(idType.equals(CField.CDataType.TIMEUUID)){
		Long timestamp = getMillisAtShardKey(indexOfShard, shardingStrategy);
		timestamp+=(spacing*itemInShard);
		return UUIDs.startOf(timestamp);
	}

	if(idType.equals(CField.CDataType.VARCHAR)){
		return counter+"";
	}

	if(idType.equals(CField.CDataType.BIGINT)){
		return Long.valueOf(counter);
	}

	throw new RhombusException("Index Id type not compatible with faking it");

}
 
Example 4
Source File: FakeIdRange.java    From Rhombus with MIT License 5 votes vote down vote up
public static UUID makeTimeUUIDFromLowestTimeUnit(Long timestamp){
		return UUIDs.startOf(timestamp);
//		// UUID v1 timestamp must be in 100-nanoseconds interval since 00:00:00.000 15 Oct 1582.
//		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"));
//		c.set(Calendar.YEAR, 1582);
//		c.set(Calendar.MONTH, Calendar.OCTOBER);
//		c.set(Calendar.DAY_OF_MONTH, 15);
//		c.set(Calendar.HOUR_OF_DAY, 0);
//		c.set(Calendar.MINUTE, 0);
//		c.set(Calendar.SECOND, 0);
//		c.set(Calendar.MILLISECOND, 0);
//		timestamp = timestamp - c.getTimeInMillis()*10000;
//
//		UUIDs.startOf(timestamp);
//		long clock = 42;
//		long node = 0x0000010000000000L;
//
//		long CLOCK_SEQ_AND_NODE = 0;
//		CLOCK_SEQ_AND_NODE |= (clock & 0x0000000000003FFFL) << 48;
//		CLOCK_SEQ_AND_NODE |= 0x8000000000000000L;
//		CLOCK_SEQ_AND_NODE |= node;
//
//		long msb = 0L;
//		msb |= (0x00000000ffffffffL & timestamp) << 32;
//		msb |= (0x0000ffff00000000L & timestamp) >>> 16;
//		msb |= (0x0fff000000000000L & timestamp) >>> 48;
//		msb |= 0x0000000000001000L; // sets the version to 1.
//		return new UUID(msb, CLOCK_SEQ_AND_NODE);
	}
 
Example 5
Source File: FakeCIndex.java    From Rhombus with MIT License 5 votes vote down vote up
public Object getFieldValueAtCounter(Long counter, CField field){
	switch (field.getType()) {
		case ASCII:
		case VARCHAR:
		case TEXT:
			return counter+"";
		case INT:
			return Integer.valueOf(counter.intValue());
		case BIGINT:
		case COUNTER:
			return Long.valueOf(counter);
		case BLOB:
			throw new IllegalArgumentException();
		case BOOLEAN:
			return ((counter%2)==0)? Boolean.valueOf(true) : Boolean.valueOf(false);
		case DECIMAL:
		case FLOAT:
			return Float.valueOf(counter.floatValue());
		case DOUBLE:
			return Double.valueOf(counter.floatValue());
		case TIMESTAMP:
			return new Date(counter);
		case UUID:
		case TIMEUUID:
			return UUIDs.startOf(counter);
		case VARINT:
			return BigInteger.valueOf(counter);
		default:
			return null;
	}

}
 
Example 6
Source File: ObjectMapperShardingITCase.java    From Rhombus with MIT License 5 votes vote down vote up
@Test
public void testQueriesUseShardIndex() throws Exception {
	logger.debug("Starting testQueriesUseShardIndex");

	// Get an object mapper for the keyspace and truncate the data
	ConnectionManager cm = getConnectionManager();
	ObjectMapper om = cm.getObjectMapper(keyspaceDefinition);
	om.truncateTables();
	om.setLogCql(true);

	// Insert a record that is more than ObjectMapper.reasonableStatementLimit months old
	DateTime dateTime = DateTime.now().minusMonths(om.getReasonableStatementLimit() * 2);
	UUID id = UUIDs.startOf(dateTime.getMillis());
	UUID accountId = UUID.fromString("00000003-0000-0030-0040-000000030000");
	UUID userId = UUID.fromString("00000003-0000-0030-0040-000000030000");
	Map<String, Object> record = Maps.newHashMap();
	record.put("id", id);
	record.put("account_id", accountId);
	record.put("user_id", userId);
	om.insert("object1", record);

	//Query it back out, make sure that we are using the shard index, because if we are not, we will surpass reasonable statement limit
	SortedMap<String, Object> indexValues = Maps.newTreeMap();
	indexValues.put("account_id", accountId);
	indexValues.put("user_id", userId);
	Criteria criteria = new Criteria();
	criteria.setIndexKeys(indexValues);
	criteria.setEndTimestamp(DateTime.now().getMillis());
	criteria.setLimit(50L);
	List<Map<String, Object>> results = om.list("object1", criteria);

	assertEquals(1, results.size());

	cm.teardown();
}
 
Example 7
Source File: FakeIdRangeTest.java    From Rhombus with MIT License 5 votes vote down vote up
@Test
public void getMillisAtShardKeyMonthlyTest() throws RhombusException{
	Long millistamp = System.currentTimeMillis();
	UUID startingUUID = UUIDs.startOf(millistamp);
	//System.out.println("UUID of start is "+startingUUID+" (DATE= "+UuidUtil.getDateFromUUID(startingUUID)+" )");
	TimebasedShardingStrategy shardingStrategy = new ShardingStrategyMonthly();
	FakeIdRange subject = new FakeIdRange(CField.CDataType.TIMEUUID,startingUUID,95L,10L, shardingStrategy, "testing");
	long counter = 90L;
	UUID id = (UUID)subject.getIdAtCounter(counter,shardingStrategy);
	//System.out.println("UUID of the id is "+id+" (DATE= "+UuidUtil.getDateFromUUID(id)+" ) Millis:"+UuidUtil.convertUUIDToJavaMillis(id));
	long actual = subject.getCounterAtId(id);
	assertEquals(counter,actual);
}
 
Example 8
Source File: FakeIdRangeTest.java    From Rhombus with MIT License 5 votes vote down vote up
@Test
public void getMillisAtShardKeyWeeklyTest() throws RhombusException{
	Long millistamp = System.currentTimeMillis();
	UUID startingUUID = UUIDs.startOf(millistamp);
	//System.out.println("UUID of start is "+startingUUID+" (DATE= "+UuidUtil.getDateFromUUID(startingUUID)+" )");
	TimebasedShardingStrategy shardingStrategy = new ShardingStrategyWeekly();
	FakeIdRange subject = new FakeIdRange(CField.CDataType.TIMEUUID,startingUUID,950L,3L, shardingStrategy, "testing");
	long counter = 8L;
	UUID id = (UUID)subject.getIdAtCounter(counter,shardingStrategy);
	//System.out.println("UUID of the id is "+id+" (DATE= "+UuidUtil.getDateFromUUID(id)+" ) Millis:"+UuidUtil.convertUUIDToJavaMillis(id));
	long actual = subject.getCounterAtId(id);
	assertEquals(counter,actual);
}
 
Example 9
Source File: FakeIdRangeTest.java    From Rhombus with MIT License 5 votes vote down vote up
@Test
public void getMillisAtShardKeyDailyTest() throws RhombusException{
	Long millistamp = System.currentTimeMillis();
	UUID startingUUID = UUIDs.startOf(millistamp);
	//System.out.println("UUID of start is "+startingUUID+" (DATE= "+UuidUtil.getDateFromUUID(startingUUID)+" )");
	TimebasedShardingStrategy shardingStrategy = new ShardingStrategyDaily();
	FakeIdRange subject = new FakeIdRange(CField.CDataType.TIMEUUID,startingUUID,950L,3L, shardingStrategy, "testing");
	long counter = 8L;
	UUID id = (UUID)subject.getIdAtCounter(counter,shardingStrategy);
	//System.out.println("UUID of the id is "+id+" (DATE= "+UuidUtil.getDateFromUUID(id)+" ) Millis:"+UuidUtil.convertUUIDToJavaMillis(id));
	long actual = subject.getCounterAtId(id);
	assertEquals(counter,actual);
}
 
Example 10
Source File: FakeIdRangeTest.java    From Rhombus with MIT License 5 votes vote down vote up
@Test
public void getMillisAtShardKeyNoneTest() throws RhombusException{
	Long millistamp = System.currentTimeMillis();
	UUID startingUUID = UUIDs.startOf(millistamp);
	//System.out.println("UUID of start is "+startingUUID+" (DATE= "+UuidUtil.getDateFromUUID(startingUUID)+" )");
	TimebasedShardingStrategy shardingStrategy = new ShardingStrategyNone();
	FakeIdRange subject = new FakeIdRange(CField.CDataType.TIMEUUID,startingUUID,950L,3L, shardingStrategy, "testing");
	long counter = 8L;
	UUID id = (UUID)subject.getIdAtCounter(counter,shardingStrategy);
	//System.out.println("UUID of the id is "+id+" (DATE= "+UuidUtil.getDateFromUUID(id)+" ) Millis:"+UuidUtil.convertUUIDToJavaMillis(id));
	long actual = subject.getCounterAtId(id);
	assertEquals(counter,actual);
}
 
Example 11
Source File: FakeIdRangeTest.java    From Rhombus with MIT License 5 votes vote down vote up
@Test
public void getIteratorTestMonthly() throws RhombusException {
	Long millistamp = 946740000000L;
	millistamp = System.currentTimeMillis();
	System.out.println(millistamp);
	UUID startingUUID = UUIDs.startOf(millistamp);
	System.out.println("UUID of start is "+startingUUID+" (DATE= "+UuidUtil.getDateFromUUID(startingUUID)+" )");


	TimebasedShardingStrategy shardingStrategy = new ShardingStrategyMonthly();
	int numberPerShard = 3;
	long totalNumberOfObjects = 950L;
	FakeIdRange subject = new FakeIdRange(CField.CDataType.TIMEUUID,startingUUID,totalNumberOfObjects,(long)numberPerShard, shardingStrategy, "testing");
	Iterator<FakeIdRange.IdInRange> it = subject.getIterator(CObjectOrdering.ASCENDING);

	long counter = 0;
	int lastmonth = -1;
	int countSinceChange = 0;
	while(it.hasNext()){
		counter++;
		FakeIdRange.IdInRange id = it.next();
		DateTime dt = UuidUtil.getDateFromUUID((UUID)id.getId());
		if(lastmonth == -1){
			//initialization case
			countSinceChange = 1;
			lastmonth = dt.getMonthOfYear();
		}
		if(dt.getMonthOfYear() == lastmonth){
			assert(countSinceChange < numberPerShard);
			countSinceChange++;
		}
		else{
			assertEquals(numberPerShard,countSinceChange);
			countSinceChange=1;
		};
		lastmonth = dt.getMonthOfYear();
		assertEquals(subject.getCounterAtId(id.getId()), id.getCounterValue());
	}
	assertEquals(totalNumberOfObjects,counter);
}
 
Example 12
Source File: FakeIdRangeTest.java    From Rhombus with MIT License 5 votes vote down vote up
@Test
public void getIteratorDailyWeekly() throws RhombusException {
	Long millistamp = System.currentTimeMillis();
	System.out.println(millistamp);
	UUID startingUUID = UUIDs.startOf(millistamp);
	System.out.println("UUID of start is "+startingUUID+" (DATE= "+UuidUtil.getDateFromUUID(startingUUID)+" )");

	TimebasedShardingStrategy shardingStrategy = new ShardingStrategyDaily();
	int numberPerShard = 3;
	long totalNumberOfObjects = 950L;
	FakeIdRange subject = new FakeIdRange(CField.CDataType.TIMEUUID,startingUUID,totalNumberOfObjects,(long)numberPerShard, shardingStrategy, "testing");
	Iterator<FakeIdRange.IdInRange> it = subject.getIterator(CObjectOrdering.ASCENDING);

	int counter = 0;
	Map<String,Long> shards = Maps.newTreeMap();
	while(it.hasNext()){
		counter++;
		FakeIdRange.IdInRange id = it.next();
		DateTime dt = UuidUtil.getDateFromUUID((UUID)id.getId());
		if(shards.get(dt.getDayOfYear()+"-"+dt.getYear()) == null){
			shards.put(dt.getDayOfYear() + "-" + dt.getYear(), 1L);
		}
		else{
			shards.put(dt.getDayOfYear()+"-"+dt.getYear(), shards.get(dt.getDayOfYear()+"-"+dt.getYear())+1L);
		}
		assertEquals(subject.getCounterAtId(id.getId()), id.getCounterValue());
	}
	assertEquals(totalNumberOfObjects, counter);
	
	for(Long shardcount : shards.values()){
		assert(Long.valueOf(numberPerShard) >= Long.valueOf(shardcount));
	}
}
 
Example 13
Source File: QakkaUtils.java    From usergrid with Apache License 2.0 4 votes vote down vote up
public static UUID getTimeUUID(long when) {
    return UUIDs.startOf( when );
}
 
Example 14
Source File: ShardAllocator.java    From usergrid with Apache License 2.0 4 votes vote down vote up
private void checkLatestShard( String queueName, Shard.Type type ) {

        Timer.Context timer = metricsService.getMetricRegistry().timer( MetricsService.ALLOCATE_TIME).time();

        try {

            String region = actorSystemFig.getRegionLocal();

            // find newest shard

            ShardIterator shardIterator = new ShardIterator(
                    cassandraClient, queueName, region, type, Optional.empty() );

            Shard shard = null;
            while (shardIterator.hasNext()) {
                shard = shardIterator.next();
            }

            if (shard == null) {
                shard = new Shard( queueName, actorSystemFig.getRegionLocal(),
                    type, 1L, QakkaUtils.getTimeUuid());
                shardSerialization.createShard( shard );

                return;
            }

            // if its count is greater than 90% of max shard size, then allocate a new shard

            long counterValue = 0;
            try {
                counterValue = shardCounterSerialization.getCounterValue( queueName, type, shard.getShardId() );

            } catch ( NotFoundException ignored ) {}

            if (counterValue > (0.9 * qakkaFig.getMaxShardSize())) {

                // Create UUID from a UNIX timestamp via DataStax utility
                // https://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/utils/UUIDs.html
                UUID futureUUID = UUIDs.startOf(
                        System.currentTimeMillis() + qakkaFig.getShardAllocationAdvanceTimeMillis());

                Shard newShard = new Shard( queueName, region, type, shard.getShardId() + 1, futureUUID );
                shardSerialization.createShard( newShard );
                shardCounterSerialization.incrementCounter( queueName, type, newShard.getShardId(), 0 );

                logger.info("Allocated new shard for queue, newShardID: {}, queueName: {}, shardMessageCount: {}, usedPercent: {}%",
                    newShard.getShardId(), queueName, counterValue, (long)((double)counterValue/(double)qakkaFig.getMaxShardSize()*100) );

            } else {
//                logger.debug("No new shard for queue {} counterValue {} of max {}",
//                    queueName, counterValue, qakkaFig.getMaxShardSize() );
            }

        } catch ( Throwable t ) {
            logger.error("Error while checking shard allocations", t);

        } finally {
            timer.close();
        }

    }
 
Example 15
Source File: FakeIdRangeTest.java    From Rhombus with MIT License 4 votes vote down vote up
@Test
public void getIteratorTestWeekly() throws RhombusException {
	Long millistamp = 946740000000L;
	millistamp = System.currentTimeMillis();
	System.out.println(millistamp);
	UUID startingUUID = UUIDs.startOf(millistamp);
	System.out.println("UUID of start is "+startingUUID+" (DATE= "+UuidUtil.getDateFromUUID(startingUUID)+" )");


	TimebasedShardingStrategy shardingStrategy = new ShardingStrategyWeekly();
	int numberPerShard = 3;
	long totalNumberOfObjects = 950L;
	FakeIdRange subject = new FakeIdRange(CField.CDataType.TIMEUUID,startingUUID,totalNumberOfObjects,(long)numberPerShard, shardingStrategy, "testing");
	Iterator<FakeIdRange.IdInRange> it = subject.getIterator(CObjectOrdering.ASCENDING);

	long counter = 0;
	int lastweek = -1;
	int countSinceChange = 0;
	while(it.hasNext()){
		counter++;
		FakeIdRange.IdInRange id = it.next();
		DateTime dt = UuidUtil.getDateFromUUID((UUID)id.getId());
		if(lastweek == -1){
			//initialization case
			countSinceChange = 1;
			lastweek = dt.getWeekOfWeekyear();
		}
		else if(dt.getWeekOfWeekyear() == lastweek){
			assert(countSinceChange < numberPerShard);
			countSinceChange++;
		}
		else{
			assertEquals(numberPerShard,countSinceChange);
			countSinceChange=1;
		};
		lastweek = dt.getWeekOfWeekyear();
		//System.out.println("countSinceChange = " + countSinceChange);
		//System.out.println("Actual Counter = " + counter);
		//System.out.println("Id Counter = " + id.getCounterValue());
		//System.out.println("Counter at ID = " + subject.getCounterAtId(id.getId()));
		//System.out.println("Date on UUID is: " + UuidUtil.getDateFromUUID((UUID)id.getId()));
		assertEquals(subject.getCounterAtId(id.getId()), id.getCounterValue());
	}
	assertEquals(totalNumberOfObjects,counter);
}
 
Example 16
Source File: Criteria.java    From Rhombus with MIT License 4 votes vote down vote up
public void setStartTimestamp(Long startTimestamp) {
	this.startUuid = UUIDs.startOf(startTimestamp);
}
 
Example 17
Source File: CObjectCQLGenerator.java    From Rhombus with MIT License 4 votes vote down vote up
public UUID getTimeUUIDAtEndOfConsistencyHorizion(){
	UUID ret = UUIDs.startOf(DateTime.now().getMillis() - consistencyHorizon);//now minus 5 seconds
	return ret;
}
 
Example 18
Source File: ObjectMapper.java    From Rhombus with MIT License 2 votes vote down vote up
/**
 * Used to insert an object with a UUID based on the provided timestamp
 * Best used for testing, as time resolution collisions are not accounted for
 * @param objectType Type of object to insert
 * @param values Values to insert
 * @param timestamp Timestamp to use to create the object UUID
 * @return the UUID of the newly inserted object
 */
public Object insert(String objectType, Map<String, Object> values, Long timestamp) throws CQLGenerationException, RhombusException {
	UUID uuid = UUIDs.startOf(timestamp);
	return insert(objectType, values, uuid);
}