refactor Timeuuid, add minOf and maxOf methods for Timeuuid

This commit is contained in:
Albert Shift 2015-09-12 20:35:24 -07:00
parent 071f6466ae
commit c1f710584a
6 changed files with 61 additions and 29 deletions

View file

@ -225,7 +225,7 @@ public final class SelectOperation<E> extends AbstractFilterStreamOperation<E, S
}
if (ifFilters != null && !ifFilters.isEmpty()) {
logger.warn("onlyIf conditions " + ifFilters + " will be ignored in the statement " + select);
logger.error("onlyIf conditions " + ifFilters + " would be ignored in the statement " + select);
}
if (allowFiltering) {

View file

@ -19,19 +19,21 @@ import java.util.Date;
import java.util.UUID;
import java.util.function.Function;
import com.noorq.casser.support.Timeuuid;
/**
* Simple Date to TimeUUID Converter
*
*/
public enum DateToTimeUUIDConverter implements Function<Date, UUID> {
public enum DateToTimeuuidConverter implements Function<Date, UUID> {
INSTANCE;
@Override
public UUID apply(Date source) {
long milliseconds = source.getTime();
return TimeUUIDUtil.createTimeUUID(milliseconds);
return Timeuuid.of(milliseconds);
}
}

View file

@ -19,13 +19,15 @@ import java.util.Date;
import java.util.UUID;
import java.util.function.Function;
public enum TimeUUIDToDateConverter implements Function<UUID, Date> {
import com.noorq.casser.support.Timeuuid;
public enum TimeuuidToDateConverter implements Function<UUID, Date> {
INSTANCE;
@Override
public Date apply(UUID source) {
return new Date(TimeUUIDUtil.getTimestampMillis(source));
return new Date(Timeuuid.getTimestampMillis(source));
}
}

View file

@ -26,8 +26,8 @@ import com.datastax.driver.core.DataType;
import com.noorq.casser.core.SessionRepository;
import com.noorq.casser.mapping.ColumnType;
import com.noorq.casser.mapping.annotation.Types;
import com.noorq.casser.mapping.convert.DateToTimeUUIDConverter;
import com.noorq.casser.mapping.convert.TimeUUIDToDateConverter;
import com.noorq.casser.mapping.convert.DateToTimeuuidConverter;
import com.noorq.casser.mapping.convert.TimeuuidToDateConverter;
import com.noorq.casser.mapping.convert.TypedConverter;
import com.noorq.casser.mapping.type.AbstractDataType;
import com.noorq.casser.mapping.type.DTDataType;
@ -63,7 +63,7 @@ public final class DateJavaType extends AbstractJavaType {
return Optional.of(TypedConverter.create(
UUID.class,
Date.class,
TimeUUIDToDateConverter.INSTANCE));
TimeuuidToDateConverter.INSTANCE));
}
return Optional.empty();
@ -79,7 +79,7 @@ public final class DateJavaType extends AbstractJavaType {
return Optional.of(TypedConverter.create(
Date.class,
UUID.class,
DateToTimeUUIDConverter.INSTANCE));
DateToTimeuuidConverter.INSTANCE));
}
return Optional.empty();

View file

@ -13,36 +13,52 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.mapping.convert;
package com.noorq.casser.support;
import java.security.SecureRandom;
import java.util.Date;
import java.util.UUID;
public final class TimeUUIDUtil {
public final class Timeuuid {
private static class Holder {
static final SecureRandom numberGenerator = new SecureRandom();
}
private TimeUUIDUtil() {
private Timeuuid() {
}
public static UUID createTimeUUID(long timestampMillis, int clockSequence, long node) {
return new UUIDBuilder().addVersion(1).addTimestampMillis(timestampMillis).addClockSequence(clockSequence)
public static UUID of(long timestampMillis, int clockSequence, long node) {
return new UuidBuilder().addVersion(1).addTimestampMillis(timestampMillis).addClockSequence(clockSequence)
.addNode(node).build();
}
public static UUID createTimeUUID(Date date, int clockSequence, long node) {
return createTimeUUID(date.getTime(), clockSequence, node);
public static UUID of(Date date, int clockSequence, long node) {
return of(date.getTime(), clockSequence, node);
}
public static UUID createTimeUUID(long timestampMillis) {
return createTimeUUID(timestampMillis, randomClockSequence(), randomNode());
public static UUID of(long timestampMillis) {
return of(timestampMillis, randomClockSequence(), randomNode());
}
public static UUID createTimeUUID(Date date) {
return createTimeUUID(date.getTime());
public static UUID of(Date date) {
return of(date.getTime());
}
public static UUID minOf(long timestampMillis) {
return new UuidBuilder().addVersion(1).addTimestampMillis(timestampMillis).setMinClockSeqAndNode().build();
}
public static UUID minOf(Date date) {
return minOf(date.getTime());
}
public static UUID maxOf(long timestampMillis) {
return new UuidBuilder().addVersion(1).addTimestampMillis(timestampMillis).setMaxClockSeqAndNode().build();
}
public static UUID maxOf(Date date) {
return maxOf(date.getTime());
}
public static int randomClockSequence() {
@ -54,6 +70,6 @@ public final class TimeUUIDUtil {
}
public static long getTimestampMillis(UUID uuid) {
return UUIDBuilder.getTimestampMillis(uuid);
return UuidBuilder.getTimestampMillis(uuid);
}
}

View file

@ -13,17 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.mapping.convert;
package com.noorq.casser.support;
import java.util.UUID;
public final class UUIDBuilder {
public final class UuidBuilder {
public static final long NUM_100NS_IN_MILLISECOND = 10000L;
public static final long NUM_100NS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;
private static final long MIN_CLOCK_SEQ_AND_NODE = 0x8080808080808080L;
private static final long MAX_CLOCK_SEQ_AND_NODE = 0x7f7f7f7f7f7f7f7fL;
private long leastSigBits = 0x8000000000000000L;
@ -41,7 +43,7 @@ public final class UUIDBuilder {
return new UUID(mostSigBits, leastSigBits);
}
public UUIDBuilder addVersion(int version) {
public UuidBuilder addVersion(int version) {
if (version < 1 || version > 4) {
throw new IllegalArgumentException("unsupported version " + version);
}
@ -51,7 +53,7 @@ public final class UUIDBuilder {
return this;
}
public UUIDBuilder addTimestamp(long uuid100Nanos) {
public UuidBuilder addTimestamp100Nanos(long uuid100Nanos) {
long timeLow = uuid100Nanos & 0xffffffffL;
long timeMid = uuid100Nanos & 0xffff00000000L;
@ -62,21 +64,31 @@ public final class UUIDBuilder {
return this;
}
public UUIDBuilder addTimestampMillis(long milliseconds) {
public UuidBuilder addTimestampMillis(long milliseconds) {
long uuid100Nanos = milliseconds * NUM_100NS_IN_MILLISECOND + NUM_100NS_SINCE_UUID_EPOCH;
return addTimestamp(uuid100Nanos);
return addTimestamp100Nanos(uuid100Nanos);
}
public UUIDBuilder addClockSequence(int clockSequence) {
public UuidBuilder addClockSequence(int clockSequence) {
leastSigBits |= ((long) (clockSequence & 0x3fff)) << 48;
return this;
}
public UUIDBuilder addNode(long node) {
public UuidBuilder addNode(long node) {
leastSigBits |= node & 0xffffffffffffL;
return this;
}
public UuidBuilder setMinClockSeqAndNode() {
this.leastSigBits = MIN_CLOCK_SEQ_AND_NODE;
return this;
}
public UuidBuilder setMaxClockSeqAndNode() {
this.leastSigBits = MAX_CLOCK_SEQ_AND_NODE;
return this;
}
public static long getTimestampMillis(UUID uuid) {
return (uuid.timestamp() - NUM_100NS_SINCE_UUID_EPOCH) / NUM_100NS_IN_MILLISECOND;
}