From a245e63fc304706a286258cdbf8f8c2fe6d0e6a2 Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 30 Mar 2025 19:45:53 +0800 Subject: [PATCH] perf[orm]: if create entity fail when load entity, and throw exception --- .../java/com/zfoo/orm/cache/EntityCache.java | 7 +++- .../java/com/zfoo/orm/cache/IEntityCache.java | 37 ++++++++++++------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java index 10e434cb..468c4b67 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java @@ -130,7 +130,10 @@ public class EntityCache, E extends IEntity> imple // 如果数据库中不存在则给一个默认值 if (entity == null) { entity = wrapper.newEntity(pk); - OrmContext.getAccessor().insert(entity); + var inserted = OrmContext.getAccessor().insert(entity); + if (!inserted) { + throw new RunException("loadOrCreate: [{}] can not create [pk:{}]", clazz.getSimpleName(), pk); + } } pnode = new PNode<>(entity); caches.put(pk, pnode); @@ -150,7 +153,7 @@ public class EntityCache, E extends IEntity> imple // 比较地址是否相等 if (entity != cachePnode.getEntity()) { - throw new RunException("fetchCachePnode(): cache entity [id:{}] not equal with update entity [id:{}]", cachePnode.getEntity().id(), id); + throw new RunException("fetchCachePnode: cache entity [id:{}] not equal with update entity [id:{}]", cachePnode.getEntity().id(), id); } if (safe) { diff --git a/orm/src/main/java/com/zfoo/orm/cache/IEntityCache.java b/orm/src/main/java/com/zfoo/orm/cache/IEntityCache.java index 55024837..3f75aacb 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/IEntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/IEntityCache.java @@ -32,9 +32,9 @@ public interface IEntityCache, E extends IEntity> /** * EN: Load data from the database into the cache, and if the database does not exist, - * return a default example with the id as the passed in value. And put it into storage. + * return a default example with the id as the passed in value and put it into storage. *

- * CN: 从数据库中加载数据到缓存,如果数据库不存在则返回一个id为传入值的默认示例。并且入库。(设置了索引唯一的无法使用该方法) + * CN: 从数据库中加载数据到缓存,如果数据库不存在则返回一个id为传入值的默认示例,并且入库。(设置了索引唯一的无法使用该方法) */ E loadOrCreate(PK pk); @@ -46,44 +46,53 @@ public interface IEntityCache, E extends IEntity> E get(PK pk); /** - * 更新缓存中的数据,只更新缓存的时间戳,并通过一定策略写入到数据库 + * EN: Update the timestamp of the cache in the memory. + * The first update() will record the thread number, and a thread-safe warning will be given if the thread number of next update() is not equal with the first time *

- * 第一次update()会记录线程号表面当前哪个线程在更新这个entity,后面如果发现update()线程号和第一次不一致会给出线程安全的警告 + * CN: 更新缓存中的数据,只更新缓存的时间戳,并通过一定策略写入到数据库 + * 第一次update()会记录更新的线程号,后面如果发现update()所在的线程号和第一次不一致会给出线程安全的警告 */ void update(E entity); /** - * 同update(),不会校验更新的线程是否一致 + * EN: The similar as update() will not check whether the updated thread number is equal. + *

+ * CN: 同update(),不会校验更新的线程是否一致 */ void updateUnsafe(E entity); /** - * 更新缓存中的数据,立刻写入到数据库 + * EN: Update the cached entity and write it to the database immediately + *

+ * CN: 更新缓存中的数据,立刻写入到数据库 */ void updateNow(E entity); /** - * 同updateNow(),不会校验更新的线程是否一致 + * EN: The similar as updateNow() will not check whether the updated thread number is equal. + *

+ * CN: 同updateNow(),不会校验更新的线程是否一致 */ void updateUnsafeNow(E entity); /** - * 不会删除数据库中的数据,只会删除缓存数据 + * EN: The data in the database will not be deleted, only the cached entity will be deleted. + *

+ * CN: 不会删除数据库中的数据,只会删除缓存数据 * - * @param pk 组要删除的主键 + * @param pk primary key */ void invalidate(PK pk); /** - * 持久化缓存数据 + * EN: Persistence cached entity in memory + *

+ * CN: 持久化缓存数据 * - * @param pk 主键 + * @param pk primary key */ void persist(PK pk); - /** - * 持久化所有缓存数据 - */ void persistAll(); void persistAllBlock();