From 7a11b7887c4765234c56f9074193b676683d8e63 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Tue, 26 Jul 2016 09:43:25 +0800 Subject: [PATCH] --- src/org/redkale/source/EntityCache.java | 16 ++++++++++------ src/org/redkale/source/EntityInfo.java | 15 ++++++++++++--- src/org/redkale/source/VirtualEntity.java | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/org/redkale/source/EntityCache.java b/src/org/redkale/source/EntityCache.java index e889779e5..1ef00559c 100644 --- a/src/org/redkale/source/EntityCache.java +++ b/src/org/redkale/source/EntityCache.java @@ -31,7 +31,8 @@ public final class EntityCache { private final ConcurrentHashMap map = new ConcurrentHashMap(); - private final Collection list = new ConcurrentLinkedQueue(); // CopyOnWriteArrayList 插入慢、查询快; 10w数据插入需要3.2秒; ConcurrentLinkedQueue 插入快、查询慢;10w数据查询需要 0.062秒, 查询慢40%; + // CopyOnWriteArrayList 插入慢、查询快; 10w数据插入需要3.2秒; ConcurrentLinkedQueue 插入快、查询慢;10w数据查询需要 0.062秒, 查询慢40%; + private final Collection list = new ConcurrentLinkedQueue(); private final Map> sortComparators = new ConcurrentHashMap<>(); @@ -56,7 +57,8 @@ public final class EntityCache { this.type = info.getType(); this.creator = info.getCreator(); this.primary = info.primary; - this.needcopy = true; + VirtualEntity ve = info.getType().getAnnotation(VirtualEntity.class); + this.needcopy = ve == null || !ve.direct(); this.newReproduce = Reproduce.create(type, type, (m) -> { try { return type.getDeclaredField(m).getAnnotation(Transient.class) == null; @@ -81,10 +83,12 @@ public final class EntityCache { if (info.fullloader == null) return; clear(); List all = info.fullloader.apply(type); - all.stream().filter(x -> x != null).forEach(x -> { - this.map.put(this.primary.get(x), x); - }); - this.list.addAll(all); + if (all != null) { + all.stream().filter(x -> x != null).forEach(x -> { + this.map.put(this.primary.get(x), x); + }); + this.list.addAll(all); + } this.fullloaded = true; } diff --git a/src/org/redkale/source/EntityInfo.java b/src/org/redkale/source/EntityInfo.java index 0fac5c088..04bf86e8e 100644 --- a/src/org/redkale/source/EntityInfo.java +++ b/src/org/redkale/source/EntityInfo.java @@ -123,7 +123,6 @@ public final class EntityInfo { private EntityInfo(Class type, int nodeid, final boolean cacheForbidden, Properties conf, Function fullloader) { this.type = type; - this.fullloader = fullloader; //--------------------------------------------- this.nodeid = nodeid >= 0 ? nodeid : 0; DistributeTables dt = type.getAnnotation(DistributeTables.class); @@ -135,7 +134,15 @@ public final class EntityInfo { Table t = type.getAnnotation(Table.class); if (type.getAnnotation(VirtualEntity.class) != null) { this.table = null; + Function loader = null; + try { + loader = type.getAnnotation(VirtualEntity.class).loader().newInstance(); + } catch (Exception e) { + logger.severe(type + " init @VirtualEntity.loader error", e); + } + this.fullloader = loader; } else { + this.fullloader = fullloader; this.table = (t == null) ? type.getSimpleName().toLowerCase() : (t.catalog().isEmpty()) ? t.name() : (t.catalog() + '.' + t.name()); } this.creator = Creator.create(type); @@ -180,8 +187,10 @@ public final class EntityInfo { // } 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"); + 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(); diff --git a/src/org/redkale/source/VirtualEntity.java b/src/org/redkale/source/VirtualEntity.java index e03db8eef..f9ded25cf 100644 --- a/src/org/redkale/source/VirtualEntity.java +++ b/src/org/redkale/source/VirtualEntity.java @@ -8,6 +8,8 @@ package org.redkale.source; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.*; +import java.util.*; +import java.util.function.*; /** * VirtualEntity表示虚拟的数据实体类, 通常Entity都会映射到数据库中的某个表,而标记为VirtualEntity的Entity类只存在DataCache中 @@ -22,4 +24,17 @@ import java.lang.annotation.*; @Retention(RUNTIME) public @interface VirtualEntity { + //DataSource是否直接返回对象的真实引用, 而不是copy一份 + boolean direct() default false; + + //初始化时数据的加载器 + Class> loader() default DefaultFunctionLoader.class; + + public static class DefaultFunctionLoader implements Function< Class, List> { + + @Override + public List apply(Class u) { + return null; + } + } }