提交 b17c85e3 编写于 作者: G gaohongtao

fix #225 ID's sequence can not reset to zero

上级 036df561
......@@ -20,6 +20,7 @@ next = "/03-community/directory-structure"
1. [ISSUE #212](https://github.com/dangdangdotcom/sharding-jdbc/issues/212) 对去缺少数据源规则给出更有意义的提示
1. [ISSUE #214](https://github.com/dangdangdotcom/sharding-jdbc/issues/214) where中 table_name.column_name in (?,?)无法解析表达式
1. [ISSUE #180](https://github.com/dangdangdotcom/sharding-jdbc/issues/180) 批量执行Update返回值不准确
1. [ISSUE #225](https://github.com/dangdangdotcom/sharding-jdbc/issues/225) 自动生成Id最后一位不归零
## 1.4.1
......
......@@ -112,15 +112,6 @@ public class CommonSelfIdGenerator implements IdGenerator {
CommonSelfIdGenerator.workerId = workerId;
}
/**
* 获取工作Id的二进制长度.
*
* @return 工作Id的二进制长度
*/
public static long getWorkerIdLength() {
return WORKER_ID_BITS;
}
/**
* 生成Id.
*
......@@ -131,7 +122,7 @@ public class CommonSelfIdGenerator implements IdGenerator {
long time = clock.millis();
Preconditions.checkState(lastTime <= time, "Clock is moving backwards, last time is %d milliseconds, current time is %d milliseconds", lastTime, time);
if (lastTime == time) {
if (0L == (++sequence & SEQUENCE_MASK)) {
if (0L == (sequence = ++sequence & SEQUENCE_MASK)) {
time = waitUntilNextTime(time);
}
} else {
......
......@@ -61,20 +61,16 @@ public class CommonSelfIdGeneratorTest {
public void testMaxSequence() throws Exception {
assertThat(maxId((1 << 12) - 1), is((1L << 12L) - 2));
assertThat(maxId(1 << 12), is((1L << 12L) - 1));
assertThat(maxId((1 << 12) + 1), is((1L << 12L) - 1));
assertThat(maxId(1 << 13), is((1L << 12L) - 1));
assertThat(maxId((1 << 13) + 1), is((1L << 12L) - 1));
assertThat(maxId((1 << 12) + 1), is(1L << 22));
}
private long maxId(final int maxSequence) {
CommonSelfIdGenerator idGenerator = new CommonSelfIdGenerator();
CommonSelfIdGenerator.setClock(new FixClock(maxSequence));
CommonSelfIdGenerator.setClock(new FixClock(1 << 13));
long id = 0;
long preId = 0;
while (id < (1L << 12)) {
preId = id;
for (int i = 0; i < maxSequence; i++) {
id = (Long) idGenerator.generateId();
}
return preId;
return id;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册