Skip to content

Commit

Permalink
leaf代码调整
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinDai committed Aug 30, 2024
1 parent 9e3b309 commit 8a72782
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@ public class Constants {
public static final String LEAF_JDBC_USERNAME = "leaf.jdbc.username";
public static final String LEAF_JDBC_PASSWORD = "leaf.jdbc.password";

public static final String LEAF_SNOWFLAKE_ENABLE = "leaf.snowflake.enable";
public static final String LEAF_SNOWFLAKE_PORT = "leaf.snowflake.port";
public static final String LEAF_SNOWFLAKE_ZK_ADDRESS = "leaf.snowflake.zk.address";

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.doodl6.springboot.leaf.common;


import com.doodl6.springboot.leaf.IDGen;

public class ZeroIDGen implements IDGen {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SegmentIDGenImpl implements IDGen {
/**
* IDCache未初始化成功时的异常码
*/
private static final long EXCEPTION_ID_IDCACHE_INIT_FALSE = -1;
private static final long EXCEPTION_ID_CACHE_INIT_FALSE = -1;
/**
* key不存在时的异常码
*/
Expand Down Expand Up @@ -120,7 +120,7 @@ private void updateCacheFromDb() {
@Override
public Result get(final String key) {
if (!initOK) {
return new Result(EXCEPTION_ID_IDCACHE_INIT_FALSE, Status.EXCEPTION);
return new Result(EXCEPTION_ID_CACHE_INIT_FALSE, Status.EXCEPTION);
}
if (cache.containsKey(key)) {
SegmentBuffer buffer = cache.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,4 @@ public Result getId(String key) {
return idGen.get(key);
}

public SegmentIDGenImpl getIdGen() {
if (idGen instanceof SegmentIDGenImpl) {
return (SegmentIDGenImpl) idGen;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.doodl6.springboot.leaf.segment.model;

import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.atomic.AtomicLong;

@Getter
@Setter
public class Segment {

private AtomicLong value = new AtomicLong(0);
Expand All @@ -10,54 +15,24 @@ public class Segment {

private volatile int step;

private SegmentBuffer buffer;
private final SegmentBuffer buffer;

public Segment(SegmentBuffer buffer) {
this.buffer = buffer;
}

public AtomicLong getValue() {
return value;
}

public void setValue(AtomicLong value) {
this.value = value;
}

public long getMax() {
return max;
}

public void setMax(long max) {
this.max = max;
}

public int getStep() {
return step;
}

public void setStep(int step) {
this.step = step;
}

public SegmentBuffer getBuffer() {
return buffer;
}

public long getIdle() {
return this.getMax() - getValue().get();
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("Segment(");
sb.append("value:");
sb.append(value);
sb.append(",max:");
sb.append(max);
sb.append(",step:");
sb.append(step);
sb.append(")");
return sb.toString();
return "Segment(" + "value:" +
value +
",max:" +
max +
",step:" +
step +
")";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.doodl6.springboot.leaf.segment.model;

import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
Expand All @@ -9,19 +12,36 @@
/**
* 双buffer
*/
@Getter
@Setter
public class SegmentBuffer {

private String key;

private Segment[] segments; //双buffer
/**
* 双buffer
*/
private Segment[] segments;

private volatile int currentPos; //当前的使用的segment的index
/**
* 当前的使用的segment的index
*/
private volatile int currentPos;

private volatile boolean nextReady; //下一个segment是否处于可切换状态
/**
* 下一个segment是否处于可切换状态
*/
private volatile boolean nextReady;

private volatile boolean initOk; //是否初始化完成
/**
* 是否初始化完成
*/
private volatile boolean initOk;

private final AtomicBoolean threadRunning; //线程是否在运行中
/**
* 线程是否在运行中
*/
private final AtomicBoolean threadRunning;

private final ReadWriteLock lock;

Expand All @@ -40,26 +60,10 @@ public SegmentBuffer() {
lock = new ReentrantReadWriteLock();
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public Segment[] getSegments() {
return segments;
}

public Segment getCurrent() {
return segments[currentPos];
}

public int getCurrentPos() {
return currentPos;
}

public int nextPos() {
return (currentPos + 1) % 2;
}
Expand All @@ -68,26 +72,6 @@ public void switchPos() {
currentPos = nextPos();
}

public boolean isInitOk() {
return initOk;
}

public void setInitOk(boolean initOk) {
this.initOk = initOk;
}

public boolean isNextReady() {
return nextReady;
}

public void setNextReady(boolean nextReady) {
this.nextReady = nextReady;
}

public AtomicBoolean getThreadRunning() {
return threadRunning;
}

public Lock rLock() {
return lock.readLock();
}
Expand All @@ -96,43 +80,17 @@ public Lock wLock() {
return lock.writeLock();
}

public int getStep() {
return step;
}

public void setStep(int step) {
this.step = step;
}

public int getMinStep() {
return minStep;
}

public void setMinStep(int minStep) {
this.minStep = minStep;
}

public long getUpdateTimestamp() {
return updateTimestamp;
}

public void setUpdateTimestamp(long updateTimestamp) {
this.updateTimestamp = updateTimestamp;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("SegmentBuffer{");
sb.append("key='").append(key).append('\'');
sb.append(", segments=").append(Arrays.toString(segments));
sb.append(", currentPos=").append(currentPos);
sb.append(", nextReady=").append(nextReady);
sb.append(", initOk=").append(initOk);
sb.append(", threadRunning=").append(threadRunning);
sb.append(", step=").append(step);
sb.append(", minStep=").append(minStep);
sb.append(", updateTimestamp=").append(updateTimestamp);
sb.append('}');
return sb.toString();
return "SegmentBuffer{" + "key='" + key + '\'' +
", segments=" + Arrays.toString(segments) +
", currentPos=" + currentPos +
", nextReady=" + nextReady +
", initOk=" + initOk +
", threadRunning=" + threadRunning +
", step=" + step +
", minStep=" + minStep +
", updateTimestamp=" + updateTimestamp +
'}';
}
}

0 comments on commit 8a72782

Please sign in to comment.