Skip to content

Commit a245e63

Browse files
committed
perf[orm]: if create entity fail when load entity, and throw exception
1 parent e27ff42 commit a245e63

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

orm/src/main/java/com/zfoo/orm/cache/EntityCache.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ public E loadOrCreate(PK pk) {
130130
// 如果数据库中不存在则给一个默认值
131131
if (entity == null) {
132132
entity = wrapper.newEntity(pk);
133-
OrmContext.getAccessor().insert(entity);
133+
var inserted = OrmContext.getAccessor().insert(entity);
134+
if (!inserted) {
135+
throw new RunException("loadOrCreate: [{}] can not create [pk:{}]", clazz.getSimpleName(), pk);
136+
}
134137
}
135138
pnode = new PNode<>(entity);
136139
caches.put(pk, pnode);
@@ -150,7 +153,7 @@ private PNode<PK, E> fetchCachePnode(E entity, boolean safe) {
150153

151154
// 比较地址是否相等
152155
if (entity != cachePnode.getEntity()) {
153-
throw new RunException("fetchCachePnode(): cache entity [id:{}] not equal with update entity [id:{}]", cachePnode.getEntity().id(), id);
156+
throw new RunException("fetchCachePnode: cache entity [id:{}] not equal with update entity [id:{}]", cachePnode.getEntity().id(), id);
154157
}
155158

156159
if (safe) {

orm/src/main/java/com/zfoo/orm/cache/IEntityCache.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public interface IEntityCache<PK extends Comparable<PK>, E extends IEntity<PK>>
3232

3333
/**
3434
* EN: Load data from the database into the cache, and if the database does not exist,
35-
* return a default example with the id as the passed in value. And put it into storage.
35+
* return a default example with the id as the passed in value and put it into storage.
3636
* <p>
37-
* CN: 从数据库中加载数据到缓存,如果数据库不存在则返回一个id为传入值的默认示例并且入库。(设置了索引唯一的无法使用该方法)
37+
* CN: 从数据库中加载数据到缓存,如果数据库不存在则返回一个id为传入值的默认示例并且入库。(设置了索引唯一的无法使用该方法)
3838
*/
3939
E loadOrCreate(PK pk);
4040

@@ -46,44 +46,53 @@ public interface IEntityCache<PK extends Comparable<PK>, E extends IEntity<PK>>
4646
E get(PK pk);
4747

4848
/**
49-
* 更新缓存中的数据,只更新缓存的时间戳,并通过一定策略写入到数据库
49+
* EN: Update the timestamp of the cache in the memory.
50+
* 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
5051
* <p>
51-
* 第一次update()会记录线程号表面当前哪个线程在更新这个entity,后面如果发现update()线程号和第一次不一致会给出线程安全的警告
52+
* CN: 更新缓存中的数据,只更新缓存的时间戳,并通过一定策略写入到数据库
53+
* 第一次update()会记录更新的线程号,后面如果发现update()所在的线程号和第一次不一致会给出线程安全的警告
5254
*/
5355
void update(E entity);
5456

5557
/**
56-
* 同update(),不会校验更新的线程是否一致
58+
* EN: The similar as update() will not check whether the updated thread number is equal.
59+
* <p>
60+
* CN: 同update(),不会校验更新的线程是否一致
5761
*/
5862
void updateUnsafe(E entity);
5963

6064
/**
61-
* 更新缓存中的数据,立刻写入到数据库
65+
* EN: Update the cached entity and write it to the database immediately
66+
* <p>
67+
* CN: 更新缓存中的数据,立刻写入到数据库
6268
*/
6369
void updateNow(E entity);
6470

6571
/**
66-
* 同updateNow(),不会校验更新的线程是否一致
72+
* EN: The similar as updateNow() will not check whether the updated thread number is equal.
73+
* <p>
74+
* CN: 同updateNow(),不会校验更新的线程是否一致
6775
*/
6876
void updateUnsafeNow(E entity);
6977

7078
/**
71-
* 不会删除数据库中的数据,只会删除缓存数据
79+
* EN: The data in the database will not be deleted, only the cached entity will be deleted.
80+
* <p>
81+
* CN: 不会删除数据库中的数据,只会删除缓存数据
7282
*
73-
* @param pk 组要删除的主键
83+
* @param pk primary key
7484
*/
7585
void invalidate(PK pk);
7686

7787
/**
78-
* 持久化缓存数据
88+
* EN: Persistence cached entity in memory
89+
* <p>
90+
* CN: 持久化缓存数据
7991
*
80-
* @param pk 主键
92+
* @param pk primary key
8193
*/
8294
void persist(PK pk);
8395

84-
/**
85-
* 持久化所有缓存数据
86-
*/
8796
void persistAll();
8897

8998
void persistAllBlock();

0 commit comments

Comments
 (0)