This commit is contained in:
Redkale
2016-07-26 09:43:25 +08:00
parent bfbc48a597
commit 7a11b7887c
3 changed files with 37 additions and 9 deletions

View File

@@ -31,7 +31,8 @@ public final class EntityCache<T> {
private final ConcurrentHashMap<Serializable, T> map = new ConcurrentHashMap();
private final Collection<T> list = new ConcurrentLinkedQueue(); // CopyOnWriteArrayList 插入慢、查询快; 10w数据插入需要3.2秒; ConcurrentLinkedQueue 插入快、查询慢10w数据查询需要 0.062秒, 查询慢40%;
// CopyOnWriteArrayList 插入慢、查询快; 10w数据插入需要3.2秒; ConcurrentLinkedQueue 插入快、查询慢10w数据查询需要 0.062秒, 查询慢40%;
private final Collection<T> list = new ConcurrentLinkedQueue();
private final Map<String, Comparator<T>> sortComparators = new ConcurrentHashMap<>();
@@ -56,7 +57,8 @@ public final class EntityCache<T> {
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<T> {
if (info.fullloader == null) return;
clear();
List<T> 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;
}

View File

@@ -123,7 +123,6 @@ public final class EntityInfo<T> {
private EntityInfo(Class<T> type, int nodeid, final boolean cacheForbidden, Properties conf, Function<Class, List> 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<T> {
Table t = type.getAnnotation(Table.class);
if (type.getAnnotation(VirtualEntity.class) != null) {
this.table = null;
Function<Class, List> 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<T> {
// }
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();

View File

@@ -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<? extends Function< Class, List>> loader() default DefaultFunctionLoader.class;
public static class DefaultFunctionLoader implements Function< Class, List> {
@Override
public List apply(Class u) {
return null;
}
}
}