This commit is contained in:
Redkale
2016-07-26 09:58:37 +08:00
parent 7a11b7887c
commit 3c667d88aa
7 changed files with 27 additions and 23 deletions

View File

@@ -72,7 +72,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
@Resource(name = "$") @Resource(name = "$")
private DataCacheListener cacheListener; private DataCacheListener cacheListener;
private final Function<Class, List> fullloader = (t) -> querySheet(false, false, t, null, null, (FilterNode) null).list(true); private final BiFunction<DataSource, Class, List> fullloader = (s, t) -> querySheet(false, false, t, null, null, (FilterNode) null).list(true);
public DataDefaultSource() throws IOException { public DataDefaultSource() throws IOException {
this(""); this("");
@@ -287,7 +287,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
} }
private <T> EntityInfo<T> loadEntityInfo(Class<T> clazz) { private <T> EntityInfo<T> loadEntityInfo(Class<T> clazz) {
return EntityInfo.load(clazz, this.nodeid, this.cacheForbidden, this.readPool.props, fullloader); return EntityInfo.load(clazz, this.nodeid, this.cacheForbidden, this.readPool.props, this, fullloader);
} }
/** /**

View File

@@ -82,7 +82,7 @@ public final class EntityCache<T> {
public void fullLoad() { public void fullLoad() {
if (info.fullloader == null) return; if (info.fullloader == null) return;
clear(); clear();
List<T> all = info.fullloader.apply(type); List<T> all = info.fullloader.apply(info.source, type);
if (all != null) { if (all != null) {
all.stream().filter(x -> x != null).forEach(x -> { all.stream().filter(x -> x != null).forEach(x -> {
this.map.put(this.primary.get(x), x); this.map.put(this.primary.get(x), x);

View File

@@ -95,18 +95,20 @@ public final class EntityInfo<T> {
final int allocationSize; final int allocationSize;
final Function<Class, List> fullloader; final DataSource source;
final BiFunction<DataSource, Class, List> fullloader;
//------------------------------------------------------------ //------------------------------------------------------------
public static <T> EntityInfo<T> load(Class<T> clazz, final int nodeid, final boolean cacheForbidden, final Properties conf, public static <T> EntityInfo<T> load(Class<T> clazz, final int nodeid, final boolean cacheForbidden, final Properties conf,
Function<Class, List> fullloader) { DataSource source, BiFunction<DataSource, 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) {
if (nodeid < 0) throw new IllegalArgumentException("nodeid(" + nodeid + ") is illegal"); if (nodeid < 0) throw new IllegalArgumentException("nodeid(" + nodeid + ") is illegal");
rs = new EntityInfo(clazz, nodeid, cacheForbidden, conf, fullloader); rs = new EntityInfo(clazz, nodeid, cacheForbidden, conf, source, fullloader);
entityInfos.put(clazz, rs); entityInfos.put(clazz, rs);
if (rs.cache != null) { if (rs.cache != null) {
if (fullloader == null) throw new IllegalArgumentException(clazz.getName() + " auto loader is illegal"); if (fullloader == null) throw new IllegalArgumentException(clazz.getName() + " auto loader is illegal");
@@ -121,8 +123,10 @@ public final class EntityInfo<T> {
return entityInfos.get(clazz); return entityInfos.get(clazz);
} }
private EntityInfo(Class<T> type, int nodeid, final boolean cacheForbidden, Properties conf, Function<Class, List> fullloader) { private EntityInfo(Class<T> type, int nodeid, final boolean cacheForbidden,
Properties conf, DataSource source, BiFunction<DataSource, Class, List> fullloader) {
this.type = type; this.type = type;
this.source = source;
//--------------------------------------------- //---------------------------------------------
this.nodeid = nodeid >= 0 ? nodeid : 0; this.nodeid = nodeid >= 0 ? nodeid : 0;
DistributeTables dt = type.getAnnotation(DistributeTables.class); DistributeTables dt = type.getAnnotation(DistributeTables.class);
@@ -134,7 +138,7 @@ public final class EntityInfo<T> {
Table t = type.getAnnotation(Table.class); Table t = type.getAnnotation(Table.class);
if (type.getAnnotation(VirtualEntity.class) != null) { if (type.getAnnotation(VirtualEntity.class) != null) {
this.table = null; this.table = null;
Function<Class, List> loader = null; BiFunction<DataSource, Class, List> loader = null;
try { try {
loader = type.getAnnotation(VirtualEntity.class).loader().newInstance(); loader = type.getAnnotation(VirtualEntity.class).loader().newInstance();
} catch (Exception e) { } catch (Exception e) {

View File

@@ -28,12 +28,12 @@ public @interface VirtualEntity {
boolean direct() default false; boolean direct() default false;
//初始化时数据的加载器 //初始化时数据的加载器
Class<? extends Function< Class, List>> loader() default DefaultFunctionLoader.class; Class<? extends BiFunction<DataSource, Class, List>> loader() default DefaultFunctionLoader.class;
public static class DefaultFunctionLoader implements Function< Class, List> { public static class DefaultFunctionLoader implements BiFunction<DataSource, Class, List> {
@Override @Override
public List apply(Class u) { public List apply(DataSource source, Class type) {
return null; return null;
} }
} }

View File

@@ -20,11 +20,11 @@ public class FilterNodeTest {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
final Properties props = new Properties(); final Properties props = new Properties();
final Function<Class, List> fullloader = (Class t) -> new ArrayList(); final BiFunction<DataSource, Class, List> fullloader = (s, t) -> new ArrayList();
final Function<Class, EntityInfo> func = (Class t) -> EntityInfo.load(t, 0, false, props, fullloader); 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, (t) -> CarTestTable.createList()); 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, (t) -> UserTestTable.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, (t) -> CarTypeTestTable.createList()); final EntityInfo<CarTypeTestTable> typeEntity = EntityInfo.load(CarTypeTestTable.class, 0, false, props, null, (s, t) -> CarTypeTestTable.createList());
final CarTestBean bean = new CarTestBean(); final CarTestBean bean = new CarTestBean();
bean.carid = 70002; bean.carid = 70002;

View File

@@ -6,7 +6,7 @@
package org.redkale.test.source; package org.redkale.test.source;
import java.util.*; import java.util.*;
import java.util.function.Function; import java.util.function.BiFunction;
import javax.persistence.Id; import javax.persistence.Id;
import org.redkale.source.*; import org.redkale.source.*;
import org.redkale.util.Attribute; import org.redkale.util.Attribute;
@@ -33,8 +33,8 @@ public class CacheTestBean {
Attribute idattr = Attribute.create(CacheTestBean.class, "pkgid"); Attribute idattr = Attribute.create(CacheTestBean.class, "pkgid");
Attribute nameattr = Attribute.create(CacheTestBean.class, "name"); Attribute nameattr = Attribute.create(CacheTestBean.class, "name");
Attribute priceattr = Attribute.create(CacheTestBean.class, "price"); Attribute priceattr = Attribute.create(CacheTestBean.class, "price");
Function<Class, List> fullloader = (z) -> list; BiFunction<DataSource, Class, List> fullloader = (s, z) -> list;
EntityCache<CacheTestBean> cache = new EntityCache(EntityInfo.load(CacheTestBean.class, 0, true,new Properties(), fullloader)); EntityCache<CacheTestBean> cache = new EntityCache(EntityInfo.load(CacheTestBean.class, 0, true, new Properties(), null, fullloader));
cache.fullLoad(); cache.fullLoad();
System.out.println(cache.queryColumnMap("pkgid", FilterFunc.COUNT, "name", null)); System.out.println(cache.queryColumnMap("pkgid", FilterFunc.COUNT, "name", null));

View File

@@ -40,7 +40,7 @@ public class TestSourceCache {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
final EntityInfo<TestEntity> info = EntityInfo.load(TestEntity.class, 0, false,new Properties(), null); final EntityInfo<TestEntity> info = EntityInfo.load(TestEntity.class, 0, false, new Properties(), null, null);
TestEntity[] entitys = new TestEntity[10_0000]; TestEntity[] entitys = new TestEntity[10_0000];
for (int i = 0; i < entitys.length; i++) { for (int i = 0; i < entitys.length; i++) {
entitys[i] = new TestEntity(i + 1, "用户_" + (i + 1)); entitys[i] = new TestEntity(i + 1, "用户_" + (i + 1));