diff --git a/src/org/redkale/source/DataDefaultSource.java b/src/org/redkale/source/DataDefaultSource.java index f05694c6e..ff0f802fa 100644 --- a/src/org/redkale/source/DataDefaultSource.java +++ b/src/org/redkale/source/DataDefaultSource.java @@ -271,7 +271,7 @@ public final class DataDefaultSource implements DataSource, Function EntityInfo loadEntityInfo(Class 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[] 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 attr : attrs) { prestmt.setObject(++i, attr.get(value)); } diff --git a/src/org/redkale/source/DistributeGenerator.java b/src/org/redkale/source/DistributeGenerator.java deleted file mode 100644 index cb6dfda43..000000000 --- a/src/org/redkale/source/DistributeGenerator.java +++ /dev/null @@ -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; - -/** - * - *

- * 详情见: 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; -} diff --git a/src/org/redkale/source/EntityInfo.java b/src/org/redkale/source/EntityInfo.java index 805a9ac7e..c7c007b3a 100644 --- a/src/org/redkale/source/EntityInfo.java +++ b/src/org/redkale/source/EntityInfo.java @@ -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 { private final Map 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 fullloader; //------------------------------------------------------------ - static EntityInfo load(Class clazz, final int nodeid, final boolean cacheForbidden, final Properties conf, + static EntityInfo load(Class clazz, final boolean cacheForbidden, final Properties conf, DataSource source, BiFunction 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 { return entityInfos.get(clazz); } - private EntityInfo(Class type, int nodeid, final boolean cacheForbidden, + private EntityInfo(Class type, final boolean cacheForbidden, Properties conf, DataSource source, BiFunction 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 { List> 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 { // 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 { } 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 { } 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 getCache() { diff --git a/test/org/redkale/source/FilterNodeTest.java b/test/org/redkale/source/FilterNodeTest.java index 61f681520..9522087ca 100644 --- a/test/org/redkale/source/FilterNodeTest.java +++ b/test/org/redkale/source/FilterNodeTest.java @@ -21,10 +21,10 @@ public class FilterNodeTest { public static void main(String[] args) throws Exception { final Properties props = new Properties(); final BiFunction fullloader = (s, t) -> new ArrayList(); - final Function func = (Class t) -> EntityInfo.load(t, 0, false, props, null, fullloader); - final EntityInfo carEntity = EntityInfo.load(CarTestTable.class, 0, false, props, null, (s, t) -> CarTestTable.createList()); - final EntityInfo userEntity = EntityInfo.load(UserTestTable.class, 0, false, props, null, (s, t) -> UserTestTable.createList()); - final EntityInfo typeEntity = EntityInfo.load(CarTypeTestTable.class, 0, false, props, null, (s, t) -> CarTypeTestTable.createList()); + final Function func = (Class t) -> EntityInfo.load(t, false, props, null, fullloader); + final EntityInfo carEntity = EntityInfo.load(CarTestTable.class, false, props, null, (s, t) -> CarTestTable.createList()); + final EntityInfo userEntity = EntityInfo.load(UserTestTable.class, false, props, null, (s, t) -> UserTestTable.createList()); + final EntityInfo typeEntity = EntityInfo.load(CarTypeTestTable.class, false, props, null, (s, t) -> CarTypeTestTable.createList()); final CarTestBean bean = new CarTestBean(); bean.carid = 70002;