改版EntityXInfo
This commit is contained in:
@@ -62,8 +62,6 @@ public final class DataJDBCSource implements DataSource {
|
|||||||
|
|
||||||
private final JDBCPoolSource writePool;
|
private final JDBCPoolSource writePool;
|
||||||
|
|
||||||
final List<Class> cacheClasses = new ArrayList<>();
|
|
||||||
|
|
||||||
@Resource(name = "property.datasource.nodeid")
|
@Resource(name = "property.datasource.nodeid")
|
||||||
int nodeid;
|
int nodeid;
|
||||||
|
|
||||||
@@ -164,7 +162,7 @@ public final class DataJDBCSource implements DataSource {
|
|||||||
for (Map.Entry<Object, Object> en : readprop.entrySet()) {
|
for (Map.Entry<Object, Object> en : readprop.entrySet()) {
|
||||||
if ("cache".equalsIgnoreCase(en.getValue().toString())) {
|
if ("cache".equalsIgnoreCase(en.getValue().toString())) {
|
||||||
try {
|
try {
|
||||||
cacheClasses.add(Class.forName(en.getKey().toString()));
|
EntityInfo.cacheClasses.add(Class.forName(en.getKey().toString()));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -179,7 +177,7 @@ public final class DataJDBCSource implements DataSource {
|
|||||||
for (Map.Entry<Object, Object> en : readprop.entrySet()) {
|
for (Map.Entry<Object, Object> en : readprop.entrySet()) {
|
||||||
if ("cache".equalsIgnoreCase(en.getValue().toString())) {
|
if ("cache".equalsIgnoreCase(en.getValue().toString())) {
|
||||||
try {
|
try {
|
||||||
cacheClasses.add(Class.forName(en.getKey().toString()));
|
EntityInfo.cacheClasses.add(Class.forName(en.getKey().toString()));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -339,8 +337,10 @@ public final class DataJDBCSource implements DataSource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final Function<Class, List> fullloader = (t) -> queryList(t, null);
|
||||||
|
|
||||||
private <T> EntityInfo<T> loadEntityInfo(Class<T> clazz) {
|
private <T> EntityInfo<T> loadEntityInfo(Class<T> clazz) {
|
||||||
return EntityInfo.load(clazz, this);
|
return EntityInfo.load(clazz, this.nodeid, fullloader);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -351,7 +351,7 @@ public final class DataJDBCSource implements DataSource {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public <T> void refreshCache(Class<T> clazz) {
|
public <T> void refreshCache(Class<T> clazz) {
|
||||||
EntityInfo<T> info = EntityInfo.load(clazz, this);
|
EntityInfo<T> info = EntityInfo.load(clazz, this.nodeid, fullloader);
|
||||||
EntityCache<T> cache = info.getCache();
|
EntityCache<T> cache = info.getCache();
|
||||||
if (cache == null) return;
|
if (cache == null) return;
|
||||||
cache.fullLoad(queryList(clazz, null));
|
cache.fullLoad(queryList(clazz, null));
|
||||||
@@ -505,7 +505,7 @@ public final class DataJDBCSource implements DataSource {
|
|||||||
|
|
||||||
public <T> void insertCache(T... values) {
|
public <T> void insertCache(T... values) {
|
||||||
if (values.length == 0) return;
|
if (values.length == 0) return;
|
||||||
final EntityInfo<T> info = EntityInfo.load((Class<T>) values[0].getClass(), this);
|
final EntityInfo<T> info = EntityInfo.load((Class<T>) values[0].getClass(), this.nodeid, fullloader);
|
||||||
final EntityCache<T> cache = info.getCache();
|
final EntityCache<T> cache = info.getCache();
|
||||||
if (cache == null) return;
|
if (cache == null) return;
|
||||||
for (T value : values) {
|
for (T value : values) {
|
||||||
@@ -2153,7 +2153,7 @@ public final class DataJDBCSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public EntityXXInfo(DataJDBCSource source, Class<T> type) {
|
public EntityXXInfo(DataJDBCSource source, Class<T> type) {
|
||||||
this.inner = EntityInfo.load(type, source);
|
this.inner = EntityInfo.load(type, source.nodeid, null);
|
||||||
this.nodeid = source.nodeid;
|
this.nodeid = source.nodeid;
|
||||||
DistributeTables dt = type.getAnnotation(DistributeTables.class);
|
DistributeTables dt = type.getAnnotation(DistributeTables.class);
|
||||||
this.distributeTables = dt == null ? null : dt.value();
|
this.distributeTables = dt == null ? null : dt.value();
|
||||||
|
|||||||
@@ -269,7 +269,7 @@ final class DataJPASource implements DataSource {
|
|||||||
*
|
*
|
||||||
* @param <T>
|
* @param <T>
|
||||||
* @param clazz
|
* @param clazz
|
||||||
* @param ids 主键值
|
* @param ids 主键值
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public <T> void delete(Class<T> clazz, Serializable... ids) {
|
public <T> void delete(Class<T> clazz, Serializable... ids) {
|
||||||
@@ -519,7 +519,7 @@ final class DataJPASource implements DataSource {
|
|||||||
final CriteriaBuilder builder = manager.getCriteriaBuilder();
|
final CriteriaBuilder builder = manager.getCriteriaBuilder();
|
||||||
CriteriaUpdate<T> cd = builder.createCriteriaUpdate(clazz);
|
CriteriaUpdate<T> cd = builder.createCriteriaUpdate(clazz);
|
||||||
cd.set(column, value);
|
cd.set(column, value);
|
||||||
cd.where(builder.equal(cd.from(clazz).get(EntityInfo.load(clazz, this).getPrimary().field()), id));
|
cd.where(builder.equal(cd.from(clazz).get(EntityInfo.load(clazz, 0, null).getPrimary().field()), id));
|
||||||
manager.createQuery(cd).executeUpdate();
|
manager.createQuery(cd).executeUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -558,7 +558,7 @@ final class DataJPASource implements DataSource {
|
|||||||
|
|
||||||
private <T> void updateColumns(final EntityManager manager, final T value, final String... columns) {
|
private <T> void updateColumns(final EntityManager manager, final T value, final String... columns) {
|
||||||
final Class clazz = value.getClass();
|
final Class clazz = value.getClass();
|
||||||
final EntityInfo info = EntityInfo.load(clazz, this);
|
final EntityInfo info = EntityInfo.load(clazz, 0, null);
|
||||||
final Attribute idattr = info.getPrimary();
|
final Attribute idattr = info.getPrimary();
|
||||||
final CriteriaBuilder builder = manager.getCriteriaBuilder();
|
final CriteriaBuilder builder = manager.getCriteriaBuilder();
|
||||||
final CriteriaUpdate<T> cd = builder.createCriteriaUpdate(clazz);
|
final CriteriaUpdate<T> cd = builder.createCriteriaUpdate(clazz);
|
||||||
@@ -891,8 +891,7 @@ final class DataJPASource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注意: 尚未实现识别express功能
|
* 注意: 尚未实现识别express功能 根据指定字段值查询对象集合, 对象只填充或排除SelectColumn指定的字段
|
||||||
* 根据指定字段值查询对象集合, 对象只填充或排除SelectColumn指定的字段
|
|
||||||
*
|
*
|
||||||
* @param <T>
|
* @param <T>
|
||||||
* @param clazz
|
* @param clazz
|
||||||
@@ -1036,7 +1035,7 @@ final class DataJPASource implements DataSource {
|
|||||||
|
|
||||||
private <T> List<T> selectList(final Class<T> clazz, final SelectColumn selects, final List<T> list) {
|
private <T> List<T> selectList(final Class<T> clazz, final SelectColumn selects, final List<T> list) {
|
||||||
if (selects == null || selects.isEmpty() || list.isEmpty()) return list;
|
if (selects == null || selects.isEmpty() || list.isEmpty()) return list;
|
||||||
final EntityInfo info = EntityInfo.load(clazz, this);
|
final EntityInfo info = EntityInfo.load(clazz, 0, null);
|
||||||
final Object dftValue = info.getCreator().create();
|
final Object dftValue = info.getCreator().create();
|
||||||
final Map<String, Attribute> map = info.getAttributes();
|
final Map<String, Attribute> map = info.getAttributes();
|
||||||
final List<Attribute> attrs = new ArrayList<>();
|
final List<Attribute> attrs = new ArrayList<>();
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ final class EntityCache<T> {
|
|||||||
|
|
||||||
private boolean fullloaded;
|
private boolean fullloaded;
|
||||||
|
|
||||||
public EntityCache(final Class<T> type, Creator<T> creator,
|
public EntityCache(final Class<T> type, Creator<T> creator, Attribute<T, Serializable> primary,
|
||||||
Attribute<T, Serializable> primary, Map<String, Attribute<T, Serializable>> attributes) {
|
Map<String, Attribute<T, Serializable>> attributes, Function<Class<T>, List<T>> fullloader) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.creator = creator;
|
this.creator = creator;
|
||||||
this.primary = primary;
|
this.primary = primary;
|
||||||
@@ -59,9 +59,14 @@ final class EntityCache<T> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
AutoLoad auto = type.getAnnotation(AutoLoad.class);
|
||||||
|
if (auto != null && auto.value() && fullloader != null) {
|
||||||
|
fullLoad(fullloader.apply(type));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void fullLoad(List<T> all) {
|
public void fullLoad(List<T> all) {
|
||||||
|
if (all == null) return;
|
||||||
clear();
|
clear();
|
||||||
all.stream().filter(x -> x != null).forEach(x -> this.map.put(this.primary.get(x), x));
|
all.stream().filter(x -> x != null).forEach(x -> this.map.put(this.primary.get(x), x));
|
||||||
this.list.addAll(all);
|
this.list.addAll(all);
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import java.sql.*;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
|
||||||
@@ -28,6 +29,8 @@ public final class EntityInfo<T> {
|
|||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(EntityInfo.class);
|
private static final Logger logger = Logger.getLogger(EntityInfo.class);
|
||||||
|
|
||||||
|
static final Set<Class> cacheClasses = new HashSet<>();
|
||||||
|
|
||||||
//Entity类的类名
|
//Entity类的类名
|
||||||
private final Class<T> type;
|
private final Class<T> type;
|
||||||
|
|
||||||
@@ -83,29 +86,21 @@ public final class EntityInfo<T> {
|
|||||||
final int allocationSize;
|
final int allocationSize;
|
||||||
//------------------------------------------------------------
|
//------------------------------------------------------------
|
||||||
|
|
||||||
public static <T> EntityInfo<T> load(Class<T> clazz, final DataSource source) {
|
public static <T> EntityInfo<T> load(Class<T> clazz, final int nodeid,
|
||||||
|
Function<Class, List> fullloader) {
|
||||||
EntityInfo rs = entityInfos.get(clazz);
|
EntityInfo rs = entityInfos.get(clazz);
|
||||||
if (rs != null) return rs;
|
if (rs != null) return rs;
|
||||||
synchronized (entityInfos) {
|
synchronized (entityInfos) {
|
||||||
rs = entityInfos.get(clazz);
|
rs = entityInfos.get(clazz);
|
||||||
if (rs == null) {
|
if (rs == null) {
|
||||||
rs = new EntityInfo(clazz, ((DataJDBCSource) source).nodeid, ((DataJDBCSource) source).cacheClasses);
|
rs = new EntityInfo(clazz, nodeid, fullloader);
|
||||||
entityInfos.put(clazz, rs);
|
entityInfos.put(clazz, rs);
|
||||||
if (rs.cache != null && source != null) {
|
|
||||||
AutoLoad auto = clazz.getAnnotation(AutoLoad.class);
|
|
||||||
if (auto != null && auto.value()) {
|
|
||||||
long s = System.currentTimeMillis();
|
|
||||||
rs.cache.fullLoad(source.queryList(clazz, null));
|
|
||||||
long e = System.currentTimeMillis() - s;
|
|
||||||
if (logger.isLoggable(Level.FINEST)) logger.finest(clazz.getName() + " full auto loaded for cache in " + e + " ms");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private EntityInfo(Class<T> type, int nodeid, final List<Class> cacheClasses) {
|
private EntityInfo(Class<T> type, int nodeid, Function<Class<T>, List<T>> fullloader) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
//---------------------------------------------
|
//---------------------------------------------
|
||||||
this.nodeid = nodeid;
|
this.nodeid = nodeid;
|
||||||
@@ -216,7 +211,7 @@ public final class EntityInfo<T> {
|
|||||||
Cacheable c = type.getAnnotation(Cacheable.class);
|
Cacheable c = type.getAnnotation(Cacheable.class);
|
||||||
boolean cf = (c == null) ? (cacheClasses != null && cacheClasses.contains(type)) : false;
|
boolean cf = (c == null) ? (cacheClasses != null && cacheClasses.contains(type)) : false;
|
||||||
if ((c != null && c.value()) || cf) {
|
if ((c != null && c.value()) || cf) {
|
||||||
this.cache = new EntityCache<>(type, creator, primary, attributes);
|
this.cache = new EntityCache<>(type, creator, primary, attributes, fullloader);
|
||||||
} else {
|
} else {
|
||||||
this.cache = null;
|
this.cache = null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ final class FilterInfo<T extends FilterBean> {
|
|||||||
joinTables.put(joinCol.table(), String.valueOf((char) ('a' + (++index))));
|
joinTables.put(joinCol.table(), String.valueOf((char) ('a' + (++index))));
|
||||||
}
|
}
|
||||||
String alias = joinTables.get(joinCol.table());
|
String alias = joinTables.get(joinCol.table());
|
||||||
EntityInfo info = EntityInfo.load(joinCol.table(), source);
|
EntityInfo info = EntityInfo.load(joinCol.table(), 0, null);
|
||||||
EntityCache cache = null;
|
EntityCache cache = null;
|
||||||
if (info.getCache() != null && info.getCache().isFullLoaded()) {
|
if (info.getCache() != null && info.getCache().isFullLoaded()) {
|
||||||
cache = info.getCache();
|
cache = info.getCache();
|
||||||
@@ -90,7 +90,7 @@ final class FilterInfo<T extends FilterBean> {
|
|||||||
cachejoin = false;
|
cachejoin = false;
|
||||||
}
|
}
|
||||||
item = new FilterItem(field, alias, cache);
|
item = new FilterItem(field, alias, cache);
|
||||||
EntityInfo secinfo = EntityInfo.load(joinCol.table(), null);
|
EntityInfo secinfo = EntityInfo.load(joinCol.table(), 0, null);
|
||||||
if (!again) {
|
if (!again) {
|
||||||
joinsb.append(" ").append(joinCol.type().name()).append(" JOIN ").append(secinfo.getTable())
|
joinsb.append(" ").append(joinCol.type().name()).append(" JOIN ").append(secinfo.getTable())
|
||||||
.append(" ").append(alias).append(" ON a.# = ").append(alias).append(".")
|
.append(" ").append(alias).append(" ON a.# = ").append(alias).append(".")
|
||||||
|
|||||||
Reference in New Issue
Block a user