Skip to content

Commit 5cdc884

Browse files
committed
add leaf 1.1.5
1 parent 3f935f3 commit 5cdc884

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

springboot-starter-leaf/src/main/java/com/sankuai/inf/leaf/segment/dao/impl/DbHelper.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.sankuai.inf.leaf.exception.DbException;
44
import org.apache.commons.dbutils.QueryRunner;
5-
import org.apache.commons.dbutils.ResultSetHandler;
65
import org.h2.jdbcx.JdbcDataSource;
76

87
import java.sql.Connection;
@@ -12,7 +11,6 @@ public class DbHelper<T> {
1211

1312
private final JdbcDataSource dataSource;
1413
private final QueryRunner queryRunner;
15-
private final ResultSetHandler<T> handler;
1614

1715

1816
interface IExecute{
@@ -31,13 +29,10 @@ interface IQuery<T>{
3129
default T query(Connection connection,QueryRunner queryRunner) throws SQLException{return null;}
3230
}
3331

34-
public DbHelper(ResultSetHandler<T> handler,String jdbcUrl) {
32+
public DbHelper(String jdbcUrl) {
3533
this.dataSource = new JdbcDataSource();
3634
this.queryRunner = new QueryRunner();
37-
this.handler = handler;
38-
39-
dataSource.setURL(jdbcUrl);
40-
35+
this.dataSource.setURL(jdbcUrl);
4136
}
4237

4338

springboot-starter-leaf/src/main/java/com/sankuai/inf/leaf/segment/dao/impl/IDAllocDaoImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public IDAllocDaoImpl(String jdbcUrl) {
3030
}
3131
return list;
3232
};
33-
this.dbHelper = new DbHelper<>(handler,jdbcUrl);
33+
this.dbHelper = new DbHelper<>(jdbcUrl);
3434
this.init();
3535
}
3636

@@ -79,11 +79,12 @@ public boolean save(LeafAlloc leafAlloc){
7979
return dbHelper.update(new DbHelper.IUpdate() {
8080
@Override
8181
public int update(Connection connection, QueryRunner queryRunner) throws SQLException {
82-
String sql = "INSERT INTO LEAF_ALLOC (MAX_ID, STEP, UPDATE_TIME, TAG) VALUES (?, ?, ?, ?)";
8382
List<LeafAlloc> list = queryRunner.query(connection,"SELECT * FROM LEAF_ALLOC WHERE TAG = ?",handler,leafAlloc.getKey());
8483
if(list!=null&&list.size()>0){
8584
return 0;
8685
}
86+
87+
String sql = "INSERT INTO LEAF_ALLOC (MAX_ID, STEP, UPDATE_TIME, TAG) VALUES (?, ?, ?, ?)";
8788
return queryRunner.update(connection,sql,leafAlloc.getMaxId(),leafAlloc.getStep(),leafAlloc.getUpdateTime(),leafAlloc.getKey());
8889
}
8990
})>0;

springboot-starter-leaf/src/test/java/com/sankuai/inf/leaf/IDGenTest.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@
55
import com.sankuai.inf.leaf.segment.dao.IDAllocDao;
66
import com.sankuai.inf.leaf.segment.dao.impl.IDAllocDaoImpl;
77
import com.sankuai.inf.leaf.segment.model.LeafAlloc;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.junit.jupiter.api.BeforeEach;
810
import org.junit.jupiter.api.Test;
911

10-
import static org.junit.jupiter.api.Assertions.*;
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
1113

14+
@Slf4j
1215
class IDGenTest {
1316

14-
1517
private IDAllocDao allocDao;
1618
private IDGen idGen;
1719

18-
public IDGenTest() {
20+
@BeforeEach
21+
public void beforeInitIdGen() {
22+
//setting jdbc url to h2 memory
1923
allocDao = new IDAllocDaoImpl("jdbc:h2:mem:leaf;DB_CLOSE_DELAY=-1");
24+
25+
//add leaf-segment-test key
2026
LeafAlloc alloc = new LeafAlloc();
21-
alloc.setMaxId(1);
22-
alloc.setStep(1000);
27+
alloc.setMaxId(0);
28+
alloc.setStep(100);
2329
alloc.setKey("leaf-segment-test");
2430

31+
//init id gen
2532
allocDao.save(alloc);
2633
idGen = new SegmentIDGenImpl();
2734
((SegmentIDGenImpl) idGen).setDao(allocDao);
@@ -31,11 +38,10 @@ public IDGenTest() {
3138

3239
@Test
3340
public void testGetId() {
34-
35-
for (int i = 0; i < 100; ++i) {
41+
for (int i = 0; i < 1000; ++i) {
3642
Result r = idGen.get("leaf-segment-test");
37-
System.out.println(r);
38-
assertEquals(r.getId(), (i+1), "id gen value error.");
43+
assertEquals(r.getId(), (i), "id gen value error.");
44+
log.info("res:{}",r);
3945
}
4046
}
4147

0 commit comments

Comments
 (0)