mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-20 08:24:16 +00:00
ref[orm]: refactor orm
This commit is contained in:
+3
-3
@@ -44,9 +44,9 @@ import java.util.stream.Collectors;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class EntityCaches<PK extends Comparable<PK>, E extends IEntity<PK>> implements IEntityCaches<PK, E> {
|
||||
public class EntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> implements IEntityCache<PK, E> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(EntityCaches.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(EntityCache.class);
|
||||
|
||||
private static final int BATCH_SIZE = 512;
|
||||
|
||||
@@ -54,7 +54,7 @@ public class EntityCaches<PK extends Comparable<PK>, E extends IEntity<PK>> impl
|
||||
|
||||
private final LoadingCache<PK, PNode<E>> cache;
|
||||
|
||||
public EntityCaches(EntityDef entityDef) {
|
||||
public EntityCache(EntityDef entityDef) {
|
||||
this.entityDef = entityDef;
|
||||
|
||||
this.cache = Caffeine.newBuilder()
|
||||
+1
-1
@@ -21,7 +21,7 @@ import java.util.function.BiConsumer;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public interface IEntityCaches<PK extends Comparable<PK>, E extends IEntity<PK>> {
|
||||
public interface IEntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> {
|
||||
|
||||
/**
|
||||
* 从数据库中加载数据到缓存,如果数据库不存在则返回一个id为空的默认值,并且将这个默认值加入缓存
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
package com.zfoo.orm.cache.persister;
|
||||
|
||||
import com.zfoo.orm.cache.EntityCaches;
|
||||
import com.zfoo.orm.cache.EntityCache;
|
||||
import com.zfoo.orm.model.EntityDef;
|
||||
|
||||
/**
|
||||
@@ -24,10 +24,10 @@ public abstract class AbstractOrmPersister implements IOrmPersister {
|
||||
|
||||
protected EntityDef entityDef;
|
||||
|
||||
protected EntityCaches<?, ?> entityCaches;
|
||||
protected EntityCache<?, ?> entityCaches;
|
||||
|
||||
|
||||
public AbstractOrmPersister(EntityDef entityDef, EntityCaches<?, ?> entityCaches) {
|
||||
public AbstractOrmPersister(EntityDef entityDef, EntityCache<?, ?> entityCaches) {
|
||||
this.entityDef = entityDef;
|
||||
this.entityCaches = entityCaches;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ package com.zfoo.orm.cache.persister;
|
||||
|
||||
import com.zfoo.event.manager.EventBus;
|
||||
import com.zfoo.orm.OrmContext;
|
||||
import com.zfoo.orm.cache.EntityCaches;
|
||||
import com.zfoo.orm.cache.EntityCache;
|
||||
import com.zfoo.orm.model.EntityDef;
|
||||
import com.zfoo.protocol.exception.ExceptionUtils;
|
||||
import com.zfoo.scheduler.manager.SchedulerBus;
|
||||
@@ -45,7 +45,7 @@ public class CronOrmPersister extends AbstractOrmPersister {
|
||||
private final CronExpression cronExpression;
|
||||
|
||||
|
||||
public CronOrmPersister(EntityDef entityDef, EntityCaches<?, ?> entityCaches) {
|
||||
public CronOrmPersister(EntityDef entityDef, EntityCache<?, ?> entityCaches) {
|
||||
super(entityDef, entityCaches);
|
||||
this.cronExpression = CronExpression.parse(entityDef.getPersisterStrategy().getConfig());
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ package com.zfoo.orm.cache.persister;
|
||||
|
||||
import com.zfoo.event.manager.EventBus;
|
||||
import com.zfoo.orm.OrmContext;
|
||||
import com.zfoo.orm.cache.EntityCaches;
|
||||
import com.zfoo.orm.cache.EntityCache;
|
||||
import com.zfoo.orm.model.EntityDef;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import com.zfoo.scheduler.manager.SchedulerBus;
|
||||
@@ -33,7 +33,7 @@ public class TimeOrmPersister extends AbstractOrmPersister {
|
||||
*/
|
||||
private final long rate;
|
||||
|
||||
public TimeOrmPersister(EntityDef entityDef, EntityCaches<?, ?> entityCaches) {
|
||||
public TimeOrmPersister(EntityDef entityDef, EntityCache<?, ?> entityCaches) {
|
||||
super(entityDef, entityCaches);
|
||||
this.rate = Long.parseLong(entityDef.getPersisterStrategy().getConfig());
|
||||
if (this.rate <= 0) {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
package com.zfoo.orm.config;
|
||||
|
||||
import com.zfoo.orm.cache.EntityCaches;
|
||||
import com.zfoo.orm.cache.EntityCache;
|
||||
import com.zfoo.orm.cache.persister.CronOrmPersister;
|
||||
import com.zfoo.orm.cache.persister.IOrmPersister;
|
||||
import com.zfoo.orm.cache.persister.TimeOrmPersister;
|
||||
@@ -27,19 +27,19 @@ public enum PersisterTypeEnum {
|
||||
|
||||
QUEUE {
|
||||
@Override
|
||||
public IOrmPersister createPersister(EntityDef entityDef, EntityCaches<?, ?> entityCaches) {
|
||||
public IOrmPersister createPersister(EntityDef entityDef, EntityCache<?, ?> entityCaches) {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
CRON {
|
||||
@Override
|
||||
public IOrmPersister createPersister(EntityDef entityDef, EntityCaches<?, ?> entityCaches) {
|
||||
public IOrmPersister createPersister(EntityDef entityDef, EntityCache<?, ?> entityCaches) {
|
||||
return new CronOrmPersister(entityDef, entityCaches);
|
||||
}
|
||||
},
|
||||
TIME {
|
||||
@Override
|
||||
public IOrmPersister createPersister(EntityDef entityDef, EntityCaches<?, ?> entityCaches) {
|
||||
public IOrmPersister createPersister(EntityDef entityDef, EntityCache<?, ?> entityCaches) {
|
||||
return new TimeOrmPersister(entityDef, entityCaches);
|
||||
}
|
||||
};
|
||||
@@ -54,6 +54,6 @@ public enum PersisterTypeEnum {
|
||||
throw new IllegalArgumentException(StringUtils.format("无效的持久化类型[persisterType:{}]", persisterType));
|
||||
}
|
||||
|
||||
public abstract IOrmPersister createPersister(EntityDef entityDef, EntityCaches<?, ?> entityCaches);
|
||||
public abstract IOrmPersister createPersister(EntityDef entityDef, EntityCache<?, ?> entityCaches);
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ package com.zfoo.orm.manager;
|
||||
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.zfoo.orm.cache.IEntityCaches;
|
||||
import com.zfoo.orm.cache.IEntityCache;
|
||||
import com.zfoo.orm.model.IEntity;
|
||||
import org.bson.Document;
|
||||
|
||||
@@ -39,9 +39,9 @@ public interface IOrmManager {
|
||||
*/
|
||||
MongoClient mongoClient();
|
||||
|
||||
<E extends IEntity<?>> IEntityCaches<?, E> getEntityCaches(Class<E> clazz);
|
||||
<E extends IEntity<?>> IEntityCache<?, E> getEntityCaches(Class<E> clazz);
|
||||
|
||||
Collection<IEntityCaches<?, ?>> getAllEntityCaches();
|
||||
Collection<IEntityCache<?, ?>> getAllEntityCaches();
|
||||
|
||||
/**
|
||||
* 基于对象的orm操作
|
||||
|
||||
@@ -24,8 +24,8 @@ import com.mongodb.client.model.IndexOptions;
|
||||
import com.mongodb.client.model.Indexes;
|
||||
import com.zfoo.orm.OrmContext;
|
||||
import com.zfoo.orm.anno.*;
|
||||
import com.zfoo.orm.cache.EntityCaches;
|
||||
import com.zfoo.orm.cache.IEntityCaches;
|
||||
import com.zfoo.orm.cache.EntityCache;
|
||||
import com.zfoo.orm.cache.IEntityCache;
|
||||
import com.zfoo.orm.config.OrmConfig;
|
||||
import com.zfoo.orm.model.EntityDef;
|
||||
import com.zfoo.orm.model.IEntity;
|
||||
@@ -72,7 +72,7 @@ public class OrmManager implements IOrmManager {
|
||||
*/
|
||||
private final Map<Class<?>, Boolean> allEntityCachesUsableMap = new HashMap<>();
|
||||
|
||||
private final Map<Class<? extends IEntity<?>>, IEntityCaches<?, ?>> entityCachesMap = new HashMap<>();
|
||||
private final Map<Class<? extends IEntity<?>>, IEntityCache<?, ?>> entityCachesMap = new HashMap<>();
|
||||
|
||||
private final Map<Class<? extends IEntity<?>>, String> collectionNameMap = new ConcurrentHashMap<>();
|
||||
|
||||
@@ -89,7 +89,7 @@ public class OrmManager implements IOrmManager {
|
||||
var entityDefMap = entityClass();
|
||||
|
||||
for (var entityDef : entityDefMap.values()) {
|
||||
var entityCaches = new EntityCaches(entityDef);
|
||||
var entityCaches = new EntityCache(entityDef);
|
||||
entityCachesMap.put(entityDef.getClazz(), entityCaches);
|
||||
allEntityCachesUsableMap.put(entityDef.getClazz(), false);
|
||||
}
|
||||
@@ -199,7 +199,7 @@ public class OrmManager implements IOrmManager {
|
||||
|
||||
Type[] types = ((ParameterizedType) type).getActualTypeArguments();
|
||||
Class<? extends IEntity<?>> entityClazz = (Class<? extends IEntity<?>>) types[1];
|
||||
IEntityCaches<?, ?> entityCaches = entityCachesMap.get(entityClazz);
|
||||
IEntityCache<?, ?> entityCaches = entityCachesMap.get(entityClazz);
|
||||
|
||||
if (entityCaches == null) {
|
||||
throw new RunException("实体缓存对象不存在,请检查配置[entity-package:{}]和[entityCaches:{}]的位置是否正确", ormConfig.getEntityPackage(), entityClazz);
|
||||
@@ -226,7 +226,7 @@ public class OrmManager implements IOrmManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends IEntity<?>> IEntityCaches<?, E> getEntityCaches(Class<E> clazz) {
|
||||
public <E extends IEntity<?>> IEntityCache<?, E> getEntityCaches(Class<E> clazz) {
|
||||
var usable = allEntityCachesUsableMap.get(clazz);
|
||||
if (usable == null) {
|
||||
throw new RunException("没有定义[]的EntityCaches,无法获取", clazz.getCanonicalName());
|
||||
@@ -234,11 +234,11 @@ public class OrmManager implements IOrmManager {
|
||||
if (!usable) {
|
||||
throw new RunException("Orm没有使用[]的EntityCaches,为了节省内存提前释放了它;只有使用EntityCachesInjection注解的Entity才能被动态获取", clazz.getCanonicalName());
|
||||
}
|
||||
return (IEntityCaches<?, E>) entityCachesMap.get(clazz);
|
||||
return (IEntityCache<?, E>) entityCachesMap.get(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<IEntityCaches<?, ?>> getAllEntityCaches() {
|
||||
public Collection<IEntityCache<?, ?>> getAllEntityCaches() {
|
||||
return Collections.unmodifiableCollection(entityCachesMap.values());
|
||||
}
|
||||
|
||||
@@ -294,7 +294,7 @@ public class OrmManager implements IOrmManager {
|
||||
String packageSearchPath = prefixPattern + scanLocation.replace(StringUtils.PERIOD, StringUtils.SLASH) + StringUtils.SLASH + suffixPattern;
|
||||
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
|
||||
var result = new HashSet<Class<?>>();
|
||||
String name = EntityCache.class.getName();
|
||||
String name = com.zfoo.orm.anno.EntityCache.class.getName();
|
||||
for (Resource resource : resources) {
|
||||
if (resource.isReadable()) {
|
||||
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
|
||||
@@ -320,7 +320,7 @@ public class OrmManager implements IOrmManager {
|
||||
var cacheStrategies = ormConfig.getCaches();
|
||||
var persisterStrategies = ormConfig.getPersisters();
|
||||
|
||||
var entityCache = clazz.getAnnotation(EntityCache.class);
|
||||
var entityCache = clazz.getAnnotation(com.zfoo.orm.anno.EntityCache.class);
|
||||
var cache = entityCache.cache();
|
||||
var cacheStrategyOptional = cacheStrategies.stream().filter(it -> it.getStrategy().equals(cache.value())).findFirst();
|
||||
AssertionUtils.isTrue(cacheStrategyOptional.isPresent(), "实体类Entity[{}]没有找到缓存策略[{}]", clazz.getSimpleName(), cache.value());
|
||||
@@ -365,9 +365,9 @@ public class OrmManager implements IOrmManager {
|
||||
|
||||
private void analyze(Class<?> clazz) {
|
||||
// 是否实现了IEntity接口
|
||||
AssertionUtils.isTrue(IEntity.class.isAssignableFrom(clazz), "被[{}]注解标注的实体类[{}]没有实现接口[{}]", EntityCache.class.getName(), clazz.getCanonicalName(), IEntity.class.getCanonicalName());
|
||||
AssertionUtils.isTrue(IEntity.class.isAssignableFrom(clazz), "被[{}]注解标注的实体类[{}]没有实现接口[{}]", com.zfoo.orm.anno.EntityCache.class.getName(), clazz.getCanonicalName(), IEntity.class.getCanonicalName());
|
||||
// 实体类Entity必须被注解EntityCache标注
|
||||
AssertionUtils.notNull(clazz.getAnnotation(EntityCache.class), "实体类Entity[{}]必须被注解[{}]标注", clazz.getCanonicalName(), EntityCache.class.getName());
|
||||
AssertionUtils.notNull(clazz.getAnnotation(com.zfoo.orm.anno.EntityCache.class), "实体类Entity[{}]必须被注解[{}]标注", clazz.getCanonicalName(), com.zfoo.orm.anno.EntityCache.class.getName());
|
||||
|
||||
// 校验entity格式
|
||||
var entitySubClassMap = new HashMap<Class<?>, Set<Class<?>>>();
|
||||
|
||||
@@ -36,7 +36,7 @@ public class EntityCachesTest {
|
||||
var context = new ClassPathXmlApplicationContext("application.xml");
|
||||
|
||||
// 动态去拿到UserEntity的EntityCaches
|
||||
var userEntityCaches = (IEntityCaches<Long, UserEntity>) OrmContext.getOrmManager().getEntityCaches(UserEntity.class);
|
||||
var userEntityCaches = (IEntityCache<Long, UserEntity>) OrmContext.getOrmManager().getEntityCaches(UserEntity.class);
|
||||
|
||||
for (var i = 1; i <= 10; i++) {
|
||||
var entity = userEntityCaches.load((long) i);
|
||||
|
||||
+1
-1
@@ -25,6 +25,6 @@ import org.springframework.stereotype.Component;
|
||||
public class UserManager {
|
||||
|
||||
@EntityCacheAutowired
|
||||
public IEntityCaches<Long, UserEntity> userEntityCaches;
|
||||
public IEntityCache<Long, UserEntity> userEntityCaches;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user