Skip to content

Commit

Permalink
refactor(core): rename maxMachine to maxMachineId.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Sep 12, 2024
1 parent cca284c commit 70943cc
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
* @author ahoo wang
*/
public abstract class AbstractSnowflakeId implements SnowflakeId {

protected final long epoch;
protected final int timestampBit;
protected final int machineBit;
protected final int sequenceBit;

protected final long maxTimestamp;
protected final long maxSequence;
protected final int maxMachine;
protected final int maxMachineId;

protected final long machineLeft;
protected final long timestampLeft;
/**
Expand All @@ -43,7 +43,7 @@ public abstract class AbstractSnowflakeId implements SnowflakeId {
private final long sequenceResetThreshold;
protected long sequence = 0L;
protected long lastTimestamp = -1L;

public AbstractSnowflakeId(long epoch,
int timestampBit,
int machineBit,
Expand All @@ -59,105 +59,105 @@ public AbstractSnowflakeId(long epoch,
this.sequenceBit = sequenceBit;
this.maxTimestamp = ~(-1L << timestampBit);
this.maxSequence = ~(-1L << sequenceBit);
this.maxMachine = ~(-1 << machineBit);
this.maxMachineId = ~(-1 << machineBit);
this.machineLeft = sequenceBit;
this.timestampLeft = this.machineLeft + machineBit;
if (machineId > this.maxMachine || machineId < 0) {
throw new IllegalArgumentException(Strings.lenientFormat("machineId can't be greater than maxMachine[%s] or less than 0 .", maxMachine));
if (machineId > this.maxMachineId || machineId < 0) {
throw new IllegalArgumentException(Strings.lenientFormat("machineId[%s] can't be greater than maxMachineId[%s] or less than 0 .", machineId, maxMachineId));
}
this.machineId = machineId;
this.sequenceResetThreshold = sequenceResetThreshold;
}

protected long nextTime() {
long time = getCurrentTime();
while (time <= lastTimestamp) {
time = getCurrentTime();
}
return time;
}

/**
* get current timestamp.
*
* @return current timestamp
*/
protected abstract long getCurrentTime();

@Override
public synchronized long generate() {
long currentTimestamp = getCurrentTime();
if (currentTimestamp < lastTimestamp) {
throw new ClockBackwardsException(lastTimestamp, currentTimestamp);
}

//region Reset sequence based on sequence reset threshold,Optimize the problem of uneven sharding.

if (currentTimestamp > lastTimestamp
&& sequence >= sequenceResetThreshold) {
&& sequence >= sequenceResetThreshold) {
sequence = 0L;
}

sequence = (sequence + 1) & maxSequence;

if (sequence == 0L) {
currentTimestamp = nextTime();
}

//endregion
lastTimestamp = currentTimestamp;
long diffTimestamp = (currentTimestamp - epoch);
if (diffTimestamp > maxTimestamp) {
throw new TimestampOverflowException(epoch, diffTimestamp, maxTimestamp);
}
return diffTimestamp << timestampLeft
| machineId << machineLeft
| sequence;
| machineId << machineLeft
| sequence;
}

@Override
public long getEpoch() {
return epoch;
}

@Override
public int getTimestampBit() {
return timestampBit;
}

@Override
public int getMachineBit() {
return machineBit;
}

@Override
public int getSequenceBit() {
return sequenceBit;
}

@Override
public long getMaxTimestamp() {
return maxTimestamp;
}

@Override
public int getMaxMachine() {
return maxMachine;
public int getMaxMachineId() {
return maxMachineId;
}

@Override
public long getMaxSequence() {
return maxSequence;
}

@Override
public long getLastTimestamp() {
return lastTimestamp;
}

@Override
public int getMachineId() {
return (int) machineId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public long getMaxTimestamp() {
}

@Override
public int getMaxMachine() {
return actual.getMaxMachine();
public int getMaxMachineId() {
return actual.getMaxMachineId();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ default boolean isSafeJavascript() {

long getMaxTimestamp();

int getMaxMachine();
int getMaxMachineId();

long getMaxSequence();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public long getMaxTimestamp() {
}

@Override
public int getMaxMachine() {
return snowflakeId.getMaxMachine();
public int getMaxMachineId() {
return snowflakeId.getMaxMachineId();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ void getMaxTimestamp() {
}

@Test
void getMaxMachine() {
assertThat(clockSyncSnowflakeId.getMaxMachine(), equalTo(1023));
void getMaxMachineId() {
assertThat(clockSyncSnowflakeId.getMaxMachineId(), equalTo(1023));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ void getMaxTimestamp() {
}

@Test
void getMaxMachine() {
Assertions.assertEquals(delegate.getMaxMachine(), snowflakeId.getMaxMachine());
void getMaxMachineId() {
Assertions.assertEquals(delegate.getMaxMachineId(), snowflakeId.getMaxMachineId());
}

@Test
Expand Down

0 comments on commit 70943cc

Please sign in to comment.