Skip to content

Commit

Permalink
- Support custom time zone (SnowflakeFriendlyId).
Browse files Browse the repository at this point in the history
- Override SnowflakeFriendlyId.generateAsString to return friendlyId (default).
  • Loading branch information
Ahoo-Wang committed Nov 10, 2021
1 parent 92d74cc commit a15767d
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package me.ahoo.cosid.snowflake;

import java.time.ZoneId;

/**
* @author ahoo wang
*/
Expand All @@ -21,8 +23,12 @@ public class DefaultSnowflakeFriendlyId implements SnowflakeFriendlyId {
private final SnowflakeIdStateParser snowflakeIdStateParser;

public DefaultSnowflakeFriendlyId(SnowflakeId delegate) {
this(delegate, ZoneId.systemDefault());
}

public DefaultSnowflakeFriendlyId(SnowflakeId delegate, ZoneId zoneId) {
this.delegate = delegate;
this.snowflakeIdStateParser = SnowflakeIdStateParser.of(delegate);
this.snowflakeIdStateParser = SnowflakeIdStateParser.of(delegate, zoneId);
}

public SnowflakeId getDelegate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public class MillisecondSnowflakeIdStateParser extends SnowflakeIdStateParser {
.toFormatter();

public MillisecondSnowflakeIdStateParser(long epoch, int timestampBit, int machineBit, int sequenceBit) {
super(epoch, timestampBit, machineBit, sequenceBit);
this(epoch, timestampBit, machineBit, sequenceBit, ZoneId.systemDefault());
}

public MillisecondSnowflakeIdStateParser(long epoch, int timestampBit, int machineBit, int sequenceBit, ZoneId zoneId) {
super(epoch, timestampBit, machineBit, sequenceBit, zoneId);
}

@Override
Expand All @@ -49,15 +53,19 @@ protected DateTimeFormatter getDateTimeFormatter() {

@Override
protected LocalDateTime getTimestamp(long diffTime) {
return Instant.ofEpochMilli(epoch + diffTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
return Instant.ofEpochMilli(epoch + diffTime).atZone(getZoneId()).toLocalDateTime();
}

@Override
protected long getDiffTime(LocalDateTime timestamp) {
return ZonedDateTime.of(timestamp, ZoneId.systemDefault()).toInstant().toEpochMilli() - epoch;
return ZonedDateTime.of(timestamp, getZoneId()).toInstant().toEpochMilli() - epoch;
}

public static MillisecondSnowflakeIdStateParser of(SnowflakeId snowflakeId) {
return new MillisecondSnowflakeIdStateParser(snowflakeId.getEpoch(), snowflakeId.getTimestampBit(), snowflakeId.getMachineBit(), snowflakeId.getSequenceBit());
return of(snowflakeId, ZoneId.systemDefault());
}

public static MillisecondSnowflakeIdStateParser of(SnowflakeId snowflakeId, ZoneId zoneId) {
return new MillisecondSnowflakeIdStateParser(snowflakeId.getEpoch(), snowflakeId.getTimestampBit(), snowflakeId.getMachineBit(), snowflakeId.getSequenceBit(), zoneId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public class SecondSnowflakeIdStateParser extends SnowflakeIdStateParser {
.toFormatter();

public SecondSnowflakeIdStateParser(long epoch, int timestampBit, int machineBit, int sequenceBit) {
super(epoch, timestampBit, machineBit, sequenceBit);
this(epoch, timestampBit, machineBit, sequenceBit, ZoneId.systemDefault());
}

public SecondSnowflakeIdStateParser(long epoch, int timestampBit, int machineBit, int sequenceBit, ZoneId zoneId) {
super(epoch, timestampBit, machineBit, sequenceBit, zoneId);
}

@Override
Expand All @@ -48,15 +52,19 @@ protected DateTimeFormatter getDateTimeFormatter() {

@Override
protected LocalDateTime getTimestamp(long diffTime) {
return Instant.ofEpochSecond(epoch + diffTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
return Instant.ofEpochSecond(epoch + diffTime).atZone(getZoneId()).toLocalDateTime();
}

@Override
protected long getDiffTime(LocalDateTime timestamp) {
return ZonedDateTime.of(timestamp, ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000 - epoch;
return ZonedDateTime.of(timestamp, getZoneId()).toInstant().toEpochMilli() / 1000 - epoch;
}

public static SecondSnowflakeIdStateParser of(SnowflakeId snowflakeId) {
return new SecondSnowflakeIdStateParser(snowflakeId.getEpoch(), snowflakeId.getTimestampBit(), snowflakeId.getMachineBit(), snowflakeId.getSequenceBit());
return of(snowflakeId, ZoneId.systemDefault());
}

public static SecondSnowflakeIdStateParser of(SnowflakeId snowflakeId, ZoneId zoneId) {
return new SecondSnowflakeIdStateParser(snowflakeId.getEpoch(), snowflakeId.getTimestampBit(), snowflakeId.getMachineBit(), snowflakeId.getSequenceBit(), zoneId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ default SnowflakeIdState friendlyId() {
return friendlyId(id);
}

@Override
default String generateAsString() {
return friendlyId().getFriendlyId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.base.Strings;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

Expand All @@ -28,7 +29,7 @@
public abstract class SnowflakeIdStateParser {

public static final String DELIMITER = "-";

protected final ZoneId zoneId;
protected final long epoch;

protected final int sequenceBit;
Expand All @@ -44,18 +45,27 @@ public abstract class SnowflakeIdStateParser {

public SnowflakeIdStateParser(long epoch
, int timestampBit, int machineBit, int sequenceBit) {
this(epoch, timestampBit, machineBit, sequenceBit, ZoneId.systemDefault());
}

public SnowflakeIdStateParser(long epoch
, int timestampBit, int machineBit, int sequenceBit, ZoneId zoneId) {
this.epoch = epoch;
this.sequenceMask = getMask(sequenceBit);
this.sequenceBit = sequenceBit;
this.machineMask = getMask(machineBit);
this.machineBit = machineBit;
this.timestampMask = getMask(timestampBit);
this.timestampBit = timestampBit;

this.zoneId = zoneId;
this.machineLeft = sequenceBit;
this.timestampLeft = machineLeft + machineBit;
}

public ZoneId getZoneId() {
return zoneId;
}

protected abstract DateTimeFormatter getDateTimeFormatter();

protected abstract LocalDateTime getTimestamp(long diffTime);
Expand Down Expand Up @@ -115,9 +125,13 @@ private long getMask(long bits) {


public static SnowflakeIdStateParser of(SnowflakeId snowflakeId) {
return of(snowflakeId, ZoneId.systemDefault());
}

public static SnowflakeIdStateParser of(SnowflakeId snowflakeId, ZoneId zoneId) {
if (snowflakeId instanceof SecondSnowflakeId) {
return SecondSnowflakeIdStateParser.of(snowflakeId);
return SecondSnowflakeIdStateParser.of(snowflakeId, zoneId);
}
return MillisecondSnowflakeIdStateParser.of(snowflakeId);
return MillisecondSnowflakeIdStateParser.of(snowflakeId, zoneId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Object generate(@PathVariable String name) {
public String friendlyId(@PathVariable String name) {
IdGenerator idGenerator = getIdGenerator(name);
if (idGenerator instanceof SnowflakeFriendlyId) {
return ((SnowflakeFriendlyId) idGenerator).friendlyId().getFriendlyId();
return idGenerator.generateAsString();
}

throw new IllegalArgumentException(Strings.lenientFormat("idGenerator:[%s] is not SnowflakeFriendlyId.", name));
Expand Down
4 changes: 3 additions & 1 deletion cosid-rest-api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
server:
port: 8004
spring:
application:
name: ${service.name:cosid-example}
datasource:
url: jdbc:mysql://localhost:3306/test_db
url: jdbc:mysql://localhost:3306/cosid_db
username: root
password: root
redis:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.time.ZoneId;
import java.util.Objects;

/**
Expand Down Expand Up @@ -156,7 +157,8 @@ private SnowflakeId createIdGen(MachineIdDistributor machineIdDistributor, Insta
snowflakeId = new ClockSyncSnowflakeId(snowflakeId, clockBackwardsSynchronizer);
}
if (idDefinition.isFriendly()) {
snowflakeId = new DefaultSnowflakeFriendlyId(snowflakeId);
final ZoneId zoneId = ZoneId.of(snowflakeIdProperties.getZoneId());
snowflakeId = new DefaultSnowflakeFriendlyId(snowflakeId, zoneId);
}
return snowflakeId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.time.Duration;
import java.time.ZoneId;
import java.util.Map;

/**
Expand All @@ -30,6 +31,7 @@ public class SnowflakeIdProperties {
public final static String PREFIX = CosId.COSID_PREFIX + "snowflake";

private boolean enabled = false;
private String zoneId = ZoneId.systemDefault().getId();
private long epoch = CosId.COSID_EPOCH;
private Machine machine;
private ClockBackwards clockBackwards;
Expand All @@ -50,6 +52,14 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public String getZoneId() {
return zoneId;
}

public void setZoneId(String zoneId) {
this.zoneId = zoneId;
}

public long getEpoch() {
return epoch;
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#

group=me.ahoo.cosid
version=1.3.17
version=1.3.18

description=Global distributed ID generator
website=https://github.com/Ahoo-Wang/CosId
Expand Down

0 comments on commit a15767d

Please sign in to comment.