From 77499de278a01f242c1b4791bf0028ba6d1dd354 Mon Sep 17 00:00:00 2001 From: godotg Date: Thu, 27 Jun 2024 13:35:33 +0800 Subject: [PATCH] del[batch]: unnecessary NOT_THREAD_SAFE_BATCH_SIZE --- .../java/com/zfoo/orm/cache/EntityCache.java | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 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 9653480c..d3eea459 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java @@ -47,7 +47,6 @@ public class EntityCache, E extends IEntity> imple private static final Logger logger = LoggerFactory.getLogger(EntityCache.class); private static final int DEFAULT_BATCH_SIZE = 512; - private static final int NOT_THREAD_SAFE_BATCH_SIZE = Math.max(128, DEFAULT_BATCH_SIZE / Runtime.getRuntime().availableProcessors()); private final EntityDef entityDef; @@ -237,24 +236,7 @@ public class EntityCache, E extends IEntity> imple pnode.resetTime(TimeUtils.currentTimeMillis()); var updateList = new ArrayList(); updateList.add(pnode.getEntity()); - doPersist(updateList, DEFAULT_BATCH_SIZE); - } - - @Override - public void persistAllBlock() { - var currentTime = TimeUtils.currentTimeMillis(); - var updateList = new ArrayList(); - cache.forEach(new BiConsumer>() { - @Override - public void accept(PK pk, PNode pnode) { - var entity = pnode.getEntity(); - if (pnode.getModifiedTime() != pnode.getWriteToDbTime()) { - pnode.resetTime(currentTime); - updateList.add(entity); - } - } - }); - doPersist(updateList, DEFAULT_BATCH_SIZE); + doPersist(updateList); } @@ -285,15 +267,32 @@ public class EntityCache, E extends IEntity> imple var updateList = entry.getValue(); var executor = ThreadUtils.executorByThreadId(threadId); if (executor == null) { - EventBus.asyncExecute(entityDef.getClazz().hashCode(), () -> doPersist(updateList, DEFAULT_BATCH_SIZE)); + EventBus.asyncExecute(entityDef.getClazz().hashCode(), () -> doPersist(updateList)); } else { - executor.execute(() -> doPersist(updateList, NOT_THREAD_SAFE_BATCH_SIZE)); + executor.execute(() -> doPersist(updateList)); } } } } - private void doPersist(List updateList, int batchSize) { + @Override + public void persistAllBlock() { + var currentTime = TimeUtils.currentTimeMillis(); + var updateList = new ArrayList(); + cache.forEach(new BiConsumer>() { + @Override + public void accept(PK pk, PNode pnode) { + var entity = pnode.getEntity(); + if (pnode.getModifiedTime() != pnode.getWriteToDbTime()) { + pnode.resetTime(currentTime); + updateList.add(entity); + } + } + }); + doPersist(updateList); + } + + private void doPersist(List updateList) { // 执行更新 if (updateList.isEmpty()) { return; @@ -302,7 +301,7 @@ public class EntityCache, E extends IEntity> imple @SuppressWarnings("unchecked") var entityClass = (Class) entityDef.getClazz(); - var page = Page.valueOf(1, batchSize, updateList.size()); + var page = Page.valueOf(1, DEFAULT_BATCH_SIZE, updateList.size()); var maxPageSize = page.totalPage(); for (var currentPage = 1; currentPage <= maxPageSize; currentPage++) {