public Date computeShardStart ( int shardNumber ) { assert isSharded ( ) ; assert shardNumber > 0 ; assert m_shardingStartDate != null ; // Shard #1 always starts on the sharding-start date. Date result = null ; if ( shardNumber == 1 ) { result = m_shardingStartDate . getTime ( ) ; } else { // Clone m_shardingStartDate and adjust by shard number. GregorianCalendar shardDate = ( GregorianCalendar ) m_shardingStartDate . clone ( ) ; switch ( m_shardingGranularity ) { case HOUR : // Increment start date HOUR by shard number - 1. shardDate . add ( Calendar . HOUR_OF_DAY , shardNumber - 1 ) ; break ; case DAY : // Increment start date DAY by shard number - 1. shardDate . add ( Calendar . DAY_OF_MONTH , shardNumber - 1 ) ; break ; case WEEK : // Round the sharding-start date down to the MONDAY of the same week. // Then increment it's DAY by (shard number - 1) * 7. shardDate = Utils . truncateToWeek ( m_shardingStartDate ) ; shardDate . add ( Calendar . DAY_OF_MONTH , ( shardNumber - 1 ) * 7 ) ; break ; case MONTH : // Increment start date MONTH by shard number - 1, but the day is always 1. shardDate . add ( Calendar . MONTH , shardNumber - 1 ) ; shardDate . set ( Calendar . DAY_OF_MONTH , 1 ) ; break ; } result = shardDate . getTime ( ) ; } assert computeShardNumber ( result ) == shardNumber ; return result ; }
Compute and return the date at which the shard with the given number starts based on this table s sharding options . For example if sharding - granularity is MONTH and the sharding - start is 2012 - 10 - 15 then shard 2 starts on 2012 - 11 - 01 .