package com.qianwen.core.sequence.range.db;
|
|
import javax.sql.DataSource;
|
import com.qianwen.core.sequence.exception.SeqException;
|
import com.qianwen.core.sequence.range.SeqRange;
|
import com.qianwen.core.sequence.range.SeqRangeMgr;
|
|
|
public class DbSeqRangeMgr implements SeqRangeMgr {
|
private DataSource dataSource;
|
private int step = 1;
|
private long stepStart = 0;
|
private int retryTimes = 3;
|
private String tableName = "range";
|
|
public boolean equals(final Object o) {
|
if (o == this) {
|
return true;
|
}
|
if (o instanceof DbSeqRangeMgr) {
|
DbSeqRangeMgr other = (DbSeqRangeMgr) o;
|
if (other.canEqual(this) && getStep() == other.getStep() && getStepStart() == other.getStepStart() && getRetryTimes() == other.getRetryTimes()) {
|
Object this$dataSource = getDataSource();
|
Object other$dataSource = other.getDataSource();
|
if (this$dataSource == null) {
|
if (other$dataSource != null) {
|
return false;
|
}
|
} else if (!this$dataSource.equals(other$dataSource)) {
|
return false;
|
}
|
Object this$tableName = getTableName();
|
Object other$tableName = other.getTableName();
|
return this$tableName == null ? other$tableName == null : this$tableName.equals(other$tableName);
|
}
|
return false;
|
}
|
return false;
|
}
|
|
protected boolean canEqual(final Object other) {
|
return other instanceof DbSeqRangeMgr;
|
}
|
|
public int hashCode() {
|
int result = (1 * 59) + getStep();
|
long $stepStart = getStepStart();
|
int result2 = (((result * 59) + ((int) (($stepStart >>> 32) ^ $stepStart))) * 59) + getRetryTimes();
|
Object $dataSource = getDataSource();
|
int result3 = (result2 * 59) + ($dataSource == null ? 43 : $dataSource.hashCode());
|
Object $tableName = getTableName();
|
return (result3 * 59) + ($tableName == null ? 43 : $tableName.hashCode());
|
}
|
|
public String toString() {
|
return "DbSeqRangeMgr(step=" + getStep() + ", stepStart=" + getStepStart() + ", retryTimes=" + getRetryTimes() + ", dataSource=" + getDataSource() + ", tableName=" + getTableName() + ")";
|
}
|
|
@Override // com.qianwen.core.sequence.range.SeqRangeMgr
|
public SeqRange nextRange(String name) throws SeqException {
|
Long newValue;
|
if (isEmpty(name)) {
|
throw new SecurityException("[DbSeqRangeMgr-nextRange] name is empty.");
|
}
|
for (int i = 0; i < getRetryTimes(); i++) {
|
Long oldValue = BaseDbHelper.selectRange(getDataSource(), getRealTableName(), name, getStepStart());
|
if (null != oldValue) {
|
if (oldValue.longValue() == this.stepStart) {
|
newValue = Long.valueOf(oldValue.longValue() + 1);
|
} else {
|
newValue = Long.valueOf(oldValue.longValue() + getStep());
|
}
|
if (BaseDbHelper.updateRange(getDataSource(), getRealTableName(), newValue, oldValue, name)) {
|
return new SeqRange(newValue.longValue(), newValue.longValue());
|
}
|
}
|
}
|
throw new SeqException("Retried too many times, retryTimes = " + getRetryTimes());
|
}
|
|
@Override // com.qianwen.core.sequence.range.SeqRangeMgr
|
public void init() {
|
checkParam();
|
if (BaseDbHelper.existTable(getDataSource(), getRealTableName()).booleanValue()) {
|
BaseDbHelper.createTable(getDataSource(), getRealTableName());
|
}
|
}
|
|
private String getRealTableName() {
|
return getTableName();
|
}
|
|
private boolean isEmpty(String str) {
|
return null == str || str.trim().length() == 0;
|
}
|
|
private void checkParam() {
|
if (this.step <= 0) {
|
throw new SecurityException("[DbSeqRangeMgr-checkParam] step must greater than 0.");
|
}
|
if (this.stepStart < 0) {
|
throw new SecurityException("[DbSeqRangeMgr-setStepStart] stepStart < 0.");
|
}
|
if (this.retryTimes <= 0) {
|
throw new SecurityException("[DbSeqRangeMgr-setRetryTimes] retryTimes must greater than 0.");
|
}
|
if (null == this.dataSource) {
|
throw new SecurityException("[DbSeqRangeMgr-setDataSource] dataSource is null.");
|
}
|
if (isEmpty(this.tableName)) {
|
throw new SecurityException("[DbSeqRangeMgr-setTableName] tableName is empty.");
|
}
|
}
|
|
public int getStep() {
|
return this.step;
|
}
|
|
public void setStep(int step) {
|
this.step = step;
|
}
|
|
public long getStepStart() {
|
return this.stepStart;
|
}
|
|
public void setStepStart(long stepStart) {
|
this.stepStart = stepStart;
|
}
|
|
public int getRetryTimes() {
|
return this.retryTimes;
|
}
|
|
public void setRetryTimes(int retryTimes) {
|
this.retryTimes = retryTimes;
|
}
|
|
public DataSource getDataSource() {
|
return this.dataSource;
|
}
|
|
public void setDataSource(DataSource dataSource) {
|
this.dataSource = dataSource;
|
}
|
|
public String getTableName() {
|
return this.tableName;
|
}
|
|
public void setTableName(String tableName) {
|
this.tableName = tableName;
|
}
|
}
|