Skip to content

Commit

Permalink
feat: 增加基于本地数据库表生成id的服务
Browse files Browse the repository at this point in the history
fix #46
  • Loading branch information
wayshall committed Jun 13, 2022
1 parent cf41cfa commit bdeda0e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/main/java/org/onetwo/dbm/id/TableIdGeneratorManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.onetwo.dbm.id;

import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.onetwo.common.date.DateUtils;
import org.onetwo.common.date.NiceDate;
import org.onetwo.common.db.spi.BaseEntityManager;
import org.onetwo.common.exception.BaseException;
import org.onetwo.common.expr.Expression;
import org.onetwo.common.expr.ExpressionFacotry;
import org.onetwo.common.utils.CUtils;
import org.onetwo.common.utils.LangUtils;
import org.onetwo.common.utils.StringUtils;
import org.onetwo.dbm.core.spi.DbmSessionImplementor;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

/**
* 利用数据库表生成id
* @author weishao zeng
* <br/>
*/
public class TableIdGeneratorManager {

private Cache<String, TableIdGenerator> idGeneratorCaches = CacheBuilder.newBuilder()
.build();
private BaseEntityManager baseEntityManager;
private Expression parser = ExpressionFacotry.BRACE;
private String cachePrefix = "TableIdGen";
private int allocationSize = 30;

public TableIdGeneratorManager(BaseEntityManager baseEntityManager) {
this.baseEntityManager = baseEntityManager;
}

public TableIdGeneratorManager(BaseEntityManager baseEntityManager, int allocationSize) {
this.allocationSize = allocationSize;
this.baseEntityManager = baseEntityManager;
}

public TableIdGeneratorManager(BaseEntityManager baseEntityManager, String cachePrefix, int allocationSize) {
this.baseEntityManager = baseEntityManager;
this.cachePrefix = cachePrefix;
this.allocationSize = allocationSize;
}

public Long nextId(String idGeneratorName) {
try {
Long genId = idGeneratorCaches.get(idGeneratorName, () -> {
String name = cacheKey(idGeneratorName);
TableGeneratorAttrs attr = new TableGeneratorAttrs(name, name, allocationSize);
TableIdGenerator idGen = new TableIdGenerator(attr);
return idGen;
}).generate((DbmSessionImplementor)baseEntityManager.getSessionFactory().getSession());
return genId;
} catch (ExecutionException e) {
throw new BaseException("create TableIdGenerator error for order no: " + e);
}
}

public String nextId(String idGeneratorName, String idExprTemplate, Object... vars) {
if (StringUtils.isBlank(idExprTemplate)) {
throw new BaseException("idExprTemplate can not be blank");
}
Long genId = nextId(idGeneratorName);
Map<String, Object> varCtx = CUtils.asMap("date", NiceDate.New().format(DateUtils.DATEONLY),
"idGeneratorName", idGeneratorName,
"genId", genId);
if (!LangUtils.isEmpty(vars)) {
Map<String, Object> addVars = CUtils.asMap(vars);
varCtx.putAll(addVars);
}
String nextId = parser.parseByProvider(idExprTemplate, varCtx);
return nextId;
}

protected String cacheKey(String key) {
return cachePrefix + ":" + key;
}

public void setBaseEntityManager(BaseEntityManager baseEntityManager) {
this.baseEntityManager = baseEntityManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.onetwo.dbm.exception.DbmException;
import org.onetwo.dbm.id.DbmIds;
import org.onetwo.dbm.id.SnowflakeIdGenerator;
import org.onetwo.dbm.id.TableIdGeneratorManager;
import org.onetwo.dbm.mapping.DbmConfig;
import org.onetwo.dbm.mapping.DbmConfig.SnowflakeIdConfig;
import org.onetwo.dbm.mapping.DefaultDbmConfig;
Expand Down Expand Up @@ -140,6 +141,13 @@ public SnowflakeIdGenerator dbmSnowflakeIdGenerator() {
return sid;
}

@Bean
@Autowired
public TableIdGeneratorManager tableIdGeneratorManager(DbmSessionFactory sessionFactory) {
TableIdGeneratorManager tidManager = new TableIdGeneratorManager(dbmEntityManager(sessionFactory));
return tidManager;
}

/*@Bean
public DataQueryFilterListener dataQueryFilterListener(){
return new DataQueryFilterListener();
Expand Down

0 comments on commit bdeda0e

Please sign in to comment.