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 = "$")
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 {
this("");
@@ -287,7 +287,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, fullloader);
return EntityInfo.load(clazz, this.nodeid, this.cacheForbidden, this.readPool.props, this, fullloader);
}
/**
@@ -990,7 +990,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
* 更新对象指定的一些字段, 必须是Entity对象
*
* @param <T> Entity类的泛型
* @param bean Entity对象
* @param bean Entity对象
* @param columns 需要更新的字段
*/
@Override
@@ -1053,7 +1053,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
* 更新对象指定的一些字段, 必须是Entity对象
*
* @param <T> Entity类的泛型
* @param bean Entity对象
* @param bean Entity对象
* @param node 过滤node 不能为null
* @param columns 需要更新的字段
*/

View File

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

View File

@@ -95,18 +95,20 @@ public final class EntityInfo<T> {
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,
Function<Class, List> fullloader) {
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, fullloader);
rs = new EntityInfo(clazz, nodeid, cacheForbidden, conf, source, fullloader);
entityInfos.put(clazz, rs);
if (rs.cache != null) {
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);
}
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.source = source;
//---------------------------------------------
this.nodeid = nodeid >= 0 ? nodeid : 0;
DistributeTables dt = type.getAnnotation(DistributeTables.class);
@@ -134,7 +138,7 @@ public final class EntityInfo<T> {
Table t = type.getAnnotation(Table.class);
if (type.getAnnotation(VirtualEntity.class) != null) {
this.table = null;
Function<Class, List> loader = null;
BiFunction<DataSource, Class, List> loader = null;
try {
loader = type.getAnnotation(VirtualEntity.class).loader().newInstance();
} catch (Exception e) {

View File

@@ -28,12 +28,12 @@ public @interface VirtualEntity {
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
public List apply(Class u) {
public List apply(DataSource source, Class type) {
return null;
}
}

View File

@@ -20,11 +20,11 @@ public class FilterNodeTest {
public static void main(String[] args) throws Exception {
final Properties props = new Properties();
final Function<Class, List> fullloader = (Class t) -> new ArrayList();
final Function<Class, EntityInfo> func = (Class t) -> EntityInfo.load(t, 0, false, props, fullloader);
final EntityInfo<CarTestTable> carEntity = EntityInfo.load(CarTestTable.class, 0, false, props, (t) -> CarTestTable.createList());
final EntityInfo<UserTestTable> userEntity = EntityInfo.load(UserTestTable.class, 0, false, props, (t) -> UserTestTable.createList());
final EntityInfo<CarTypeTestTable> typeEntity = EntityInfo.load(CarTypeTestTable.class, 0, false, props, (t) -> CarTypeTestTable.createList());
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 CarTestBean bean = new CarTestBean();
bean.carid = 70002;
@@ -32,7 +32,7 @@ public class FilterNodeTest {
bean.createtime = 500;
bean.typename = "法拉利";
FilterNode joinNode1 = FilterJoinNode.create(UserTestTable.class, new String[]{"userid", "username"}, "username", LIKE, bean.username)
.or(FilterJoinNode.create(UserTestTable.class, new String[]{"userid", "username"}, "createtime", GREATERTHAN, bean.createtime));
.or(FilterJoinNode.create(UserTestTable.class, new String[]{"userid", "username"}, "createtime", GREATERTHAN, bean.createtime));
FilterNode joinNode2 = FilterJoinNode.create(CarTypeTestTable.class, "cartype", "typename", LIKE, bean.typename);
FilterNode node = CarTestBean.caridTransient() ? (joinNode2.or(joinNode1)) : FilterNode.create("carid", GREATERTHAN, bean.carid).and(joinNode1).or(joinNode2);
FilterNode beanNode = FilterNodeBean.createFilterNode(bean);

View File

@@ -6,7 +6,7 @@
package org.redkale.test.source;
import java.util.*;
import java.util.function.Function;
import java.util.function.BiFunction;
import javax.persistence.Id;
import org.redkale.source.*;
import org.redkale.util.Attribute;
@@ -33,8 +33,8 @@ public class CacheTestBean {
Attribute idattr = Attribute.create(CacheTestBean.class, "pkgid");
Attribute nameattr = Attribute.create(CacheTestBean.class, "name");
Attribute priceattr = Attribute.create(CacheTestBean.class, "price");
Function<Class, List> fullloader = (z) -> list;
EntityCache<CacheTestBean> cache = new EntityCache(EntityInfo.load(CacheTestBean.class, 0, true,new Properties(), fullloader));
BiFunction<DataSource, Class, List> fullloader = (s, z) -> list;
EntityCache<CacheTestBean> cache = new EntityCache(EntityInfo.load(CacheTestBean.class, 0, true, new Properties(), null, fullloader));
cache.fullLoad();
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 {
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];
for (int i = 0; i < entitys.length; i++) {
entitys[i] = new TestEntity(i + 1, "用户_" + (i + 1));