删掉 DistributeGenerator 功能
This commit is contained in:
@@ -271,7 +271,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
||||
}
|
||||
|
||||
private <T> EntityInfo<T> loadEntityInfo(Class<T> clazz) {
|
||||
return EntityInfo.load(clazz, this.nodeid, this.cacheForbidden, this.readPool.props, this, fullloader);
|
||||
return EntityInfo.load(clazz, this.cacheForbidden, this.readPool.props, this, fullloader);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -317,32 +317,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
||||
final String sql = info.getInsertSQL(values[0]);
|
||||
final Class primaryType = info.getPrimary().type();
|
||||
final Attribute primary = info.getPrimary();
|
||||
final boolean distributed = info.distributed;
|
||||
Attribute<T, Serializable>[] attrs = info.insertAttributes;
|
||||
if (distributed && !info.initedPrimaryValue && primaryType.isPrimitive()) { //由DataSource生成主键
|
||||
synchronized (info) {
|
||||
if (!info.initedPrimaryValue) { //初始化最大主键值
|
||||
try {
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT MAX(" + info.getPrimarySQLColumn() + ") FROM " + info.getTable(values[0]));
|
||||
if (rs.next()) {
|
||||
if (primaryType == int.class) {
|
||||
int v = rs.getInt(1) / info.allocationSize;
|
||||
if (v > info.primaryValue.get()) info.primaryValue.set(v);
|
||||
} else {
|
||||
long v = rs.getLong(1) / info.allocationSize;
|
||||
if (v > info.primaryValue.get()) info.primaryValue.set(v);
|
||||
}
|
||||
}
|
||||
rs.close();
|
||||
stmt.close();
|
||||
} catch (SQLException se) {
|
||||
if (info.tableStrategy == null) throw se;
|
||||
}
|
||||
info.initedPrimaryValue = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
PreparedStatement prestmt = createInsertPreparedStatement(conn, sql, info, values);
|
||||
try {
|
||||
prestmt.executeBatch();
|
||||
@@ -442,7 +417,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
||||
|
||||
for (final T value : values) {
|
||||
int i = 0;
|
||||
if (info.distributed || info.autouuid) info.createPrimaryValue(value);
|
||||
if (info.autouuid) info.createPrimaryValue(value);
|
||||
for (Attribute<T, Serializable> attr : attrs) {
|
||||
prestmt.setObject(++i, attr.get(value));
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.source;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@Target({FIELD})
|
||||
@Retention(RUNTIME)
|
||||
public @interface DistributeGenerator {
|
||||
|
||||
long initialValue() default 1;
|
||||
|
||||
/**
|
||||
* 如果allocationSize的值小于或等于1,则主键不会加上nodeid
|
||||
*
|
||||
* @return allocationSize
|
||||
*/
|
||||
int allocationSize() default 1000;
|
||||
}
|
||||
@@ -11,7 +11,6 @@ import java.lang.reflect.*;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.*;
|
||||
import java.util.logging.Level;
|
||||
import javax.persistence.*;
|
||||
@@ -89,34 +88,23 @@ public final class EntityInfo<T> {
|
||||
private final Map<String, String> sortOrderbySqls = new ConcurrentHashMap<>();
|
||||
|
||||
//---------------------计算主键值----------------------------
|
||||
private final int nodeid;
|
||||
|
||||
final boolean autoGenerated;
|
||||
|
||||
final boolean autouuid;
|
||||
|
||||
final boolean distributed;
|
||||
|
||||
boolean initedPrimaryValue = false;
|
||||
|
||||
final AtomicLong primaryValue = new AtomicLong(0);
|
||||
|
||||
final int allocationSize;
|
||||
|
||||
final DataSource source;
|
||||
|
||||
final BiFunction<DataSource, Class, List> fullloader;
|
||||
//------------------------------------------------------------
|
||||
|
||||
static <T> EntityInfo<T> load(Class<T> clazz, final int nodeid, final boolean cacheForbidden, final Properties conf,
|
||||
static <T> EntityInfo<T> load(Class<T> clazz, final boolean cacheForbidden, final Properties conf,
|
||||
DataSource source, BiFunction<DataSource, Class, List> fullloader) {
|
||||
EntityInfo rs = entityInfos.get(clazz);
|
||||
if (rs != null) return rs;
|
||||
synchronized (entityInfos) {
|
||||
rs = entityInfos.get(clazz);
|
||||
if (rs == null) {
|
||||
if (nodeid < 0) throw new IllegalArgumentException("nodeid(" + nodeid + ") is illegal");
|
||||
rs = new EntityInfo(clazz, nodeid, cacheForbidden, conf, source, fullloader);
|
||||
rs = new EntityInfo(clazz, cacheForbidden, conf, source, fullloader);
|
||||
entityInfos.put(clazz, rs);
|
||||
if (rs.cache != null) {
|
||||
if (fullloader == null) throw new IllegalArgumentException(clazz.getName() + " auto loader is illegal");
|
||||
@@ -131,12 +119,11 @@ public final class EntityInfo<T> {
|
||||
return entityInfos.get(clazz);
|
||||
}
|
||||
|
||||
private EntityInfo(Class<T> type, int nodeid, final boolean cacheForbidden,
|
||||
private EntityInfo(Class<T> type, final boolean cacheForbidden,
|
||||
Properties conf, DataSource source, BiFunction<DataSource, Class, List> fullloader) {
|
||||
this.type = type;
|
||||
this.source = source;
|
||||
//---------------------------------------------
|
||||
this.nodeid = nodeid >= 0 ? nodeid : 0;
|
||||
|
||||
LogLevel ll = type.getAnnotation(LogLevel.class);
|
||||
this.logLevel = ll == null ? Integer.MIN_VALUE : Level.parse(ll.value()).intValue();
|
||||
@@ -176,8 +163,6 @@ public final class EntityInfo<T> {
|
||||
List<Attribute<T, Serializable>> updateattrs = new ArrayList<>();
|
||||
boolean auto = false;
|
||||
boolean uuid = false;
|
||||
boolean sqldistribute = false;
|
||||
int allocationSize0 = 0;
|
||||
|
||||
do {
|
||||
for (Field field : cltmp.getDeclaredFields()) {
|
||||
@@ -205,17 +190,6 @@ public final class EntityInfo<T> {
|
||||
// if (gv != null && gv.strategy() != GenerationType.IDENTITY) {
|
||||
// throw new RuntimeException(cltmp.getName() + "'s @ID primary not a GenerationType.IDENTITY");
|
||||
// }
|
||||
DistributeGenerator dg = field.getAnnotation(DistributeGenerator.class);
|
||||
if (dg != null) {
|
||||
if (!field.getType().isPrimitive()) {
|
||||
throw new RuntimeException(cltmp.getName() + "'s @"
|
||||
+ DistributeGenerator.class.getSimpleName() + " primary must be primitive class type field");
|
||||
}
|
||||
sqldistribute = true;
|
||||
auto = false;
|
||||
allocationSize0 = dg.allocationSize();
|
||||
primaryValue.set(dg.initialValue());
|
||||
}
|
||||
if (gv != null && field.getType() == String.class) { //UUID
|
||||
uuid = true;
|
||||
auto = false;
|
||||
@@ -272,8 +246,6 @@ public final class EntityInfo<T> {
|
||||
}
|
||||
this.autoGenerated = auto;
|
||||
this.autouuid = uuid;
|
||||
this.distributed = sqldistribute;
|
||||
this.allocationSize = allocationSize0;
|
||||
//----------------cache--------------
|
||||
Cacheable c = type.getAnnotation(Cacheable.class);
|
||||
if (this.table == null || (!cacheForbidden && c != null && c.value())) {
|
||||
@@ -290,16 +262,7 @@ public final class EntityInfo<T> {
|
||||
}
|
||||
|
||||
public void createPrimaryValue(T src) {
|
||||
if (autouuid) {
|
||||
getPrimary().set(src, Utility.uuid());
|
||||
return;
|
||||
}
|
||||
long v = allocationSize > 1 ? (primaryValue.incrementAndGet() * allocationSize + nodeid) : primaryValue.incrementAndGet();
|
||||
if (primary.type() == int.class || primary.type() == Integer.class) {
|
||||
getPrimary().set(src, (Integer) ((Long) v).intValue());
|
||||
} else {
|
||||
getPrimary().set(src, v);
|
||||
}
|
||||
if (autouuid) getPrimary().set(src, Utility.uuid());
|
||||
}
|
||||
|
||||
public EntityCache<T> getCache() {
|
||||
|
||||
@@ -21,10 +21,10 @@ public class FilterNodeTest {
|
||||
public static void main(String[] args) throws Exception {
|
||||
final Properties props = new Properties();
|
||||
final BiFunction<DataSource, Class, List> fullloader = (s, t) -> new ArrayList();
|
||||
final Function<Class, EntityInfo> func = (Class t) -> EntityInfo.load(t, 0, false, props, null, fullloader);
|
||||
final EntityInfo<CarTestTable> carEntity = EntityInfo.load(CarTestTable.class, 0, false, props, null, (s, t) -> CarTestTable.createList());
|
||||
final EntityInfo<UserTestTable> userEntity = EntityInfo.load(UserTestTable.class, 0, false, props, null, (s, t) -> UserTestTable.createList());
|
||||
final EntityInfo<CarTypeTestTable> typeEntity = EntityInfo.load(CarTypeTestTable.class, 0, false, props, null, (s, t) -> CarTypeTestTable.createList());
|
||||
final Function<Class, EntityInfo> func = (Class t) -> EntityInfo.load(t, false, props, null, fullloader);
|
||||
final EntityInfo<CarTestTable> carEntity = EntityInfo.load(CarTestTable.class, false, props, null, (s, t) -> CarTestTable.createList());
|
||||
final EntityInfo<UserTestTable> userEntity = EntityInfo.load(UserTestTable.class, false, props, null, (s, t) -> UserTestTable.createList());
|
||||
final EntityInfo<CarTypeTestTable> typeEntity = EntityInfo.load(CarTypeTestTable.class, false, props, null, (s, t) -> CarTypeTestTable.createList());
|
||||
|
||||
final CarTestBean bean = new CarTestBean();
|
||||
bean.carid = 70002;
|
||||
|
||||
Reference in New Issue
Block a user