yangys
2024-05-18 040976de6f9934b99f30268a28e2ecf42260e217
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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;
    }
}