From 3217238c615bbab00f803fd7bdf4550ce3dfbf51 Mon Sep 17 00:00:00 2001 From: luca Date: Mon, 29 Jul 2024 14:09:38 +0800 Subject: [PATCH] perf[orm]: optimized cache landing --- .../main/java/com/zfoo/orm/accessor/MongodbAccessor.java | 8 ++++---- orm/src/main/java/com/zfoo/orm/cache/EntityCache.java | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/orm/src/main/java/com/zfoo/orm/accessor/MongodbAccessor.java b/orm/src/main/java/com/zfoo/orm/accessor/MongodbAccessor.java index 22934f0f..6a54a33e 100644 --- a/orm/src/main/java/com/zfoo/orm/accessor/MongodbAccessor.java +++ b/orm/src/main/java/com/zfoo/orm/accessor/MongodbAccessor.java @@ -64,7 +64,7 @@ public class MongodbAccessor implements IAccessor { var filter = Filters.eq("_id", entity.id()); var result = collection.replaceOne(filter, entity); - if (result.getModifiedCount() <= 0) { + if (result.getMatchedCount() <= 0) { // 数据库中没有这个id或者需要更新的数据和数据库中的相同 logger.warn("database:[{}] 1.[_id:{}] not exist or 2. update entity is equal with database entity", entityClazz.getSimpleName(), entity.id()); return false; @@ -92,10 +92,10 @@ public class MongodbAccessor implements IAccessor { .toList(); var result = collection.bulkWrite(batchList, new BulkWriteOptions().ordered(false)); - if (result.getModifiedCount() != entities.size()) { + if (result.getMatchedCount() != entities.size()) { // 在数据库的批量更新操作中需要更新的数量和最终更新的数量不相同(大部分原因都是因为需要更新的文档和数据库的文档相同) - logger.warn("database:[{}] update size:[{}] not equal with modified size:[{}](Most of the reasons is that update entity is equal with database entity)" - , entityClazz.getSimpleName(), entities.size(), result.getModifiedCount()); + logger.warn("database:[{}] update size:[{}] not equal with matched size:[{}](Most of the reasons is that update entity is equal with database entity)" + , entityClazz.getSimpleName(), entities.size(), result.getMatchedCount()); } } catch (Throwable t) { logger.error("batchUpdate unknown exception", t); 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 f749ceea..fae737f0 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java @@ -99,7 +99,7 @@ public class EntityCache, E extends IEntity> imple ? Filters.and(Filters.eq("_id", entity.id()), Filters.eq(wrapper.versionFieldName(), version)) : Filters.eq("_id", entity.id()); var result = collection.replaceOne(filter, entity); - if (result.getModifiedCount() <= 0) { + if (result.getMatchedCount() <= 0) { // 移除缓存时,更新数据库中的实体文档异常 logger.error("onRemoval(): update entity to db failed when remove [{}] [pk:{}] by [removalCause:{}]", clazz.getSimpleName(), entity.id(), removalCause); } @@ -340,14 +340,14 @@ public class EntityCache, E extends IEntity> imple .toList(); var result = collection.bulkWrite(batchList, new BulkWriteOptions().ordered(false)); - if (result.getModifiedCount() == batchList.size()) { + if (result.getMatchedCount() == batchList.size()) { continue; } // mostly because the document that needs to be updated is the same as the document in the database // 开始执行容错操作(大部分原因都是因为需要更新的文档和数据库的文档相同) logger.warn("persistAll(): [{}] batch update [{}] not equal to final update [{}], and try to use persistAllAndCompare() to update every single entity." - , clazz.getSimpleName(), currentUpdateList.size(), result.getModifiedCount()); + , clazz.getSimpleName(), currentUpdateList.size(), result.getMatchedCount()); } catch (Throwable t) { logger.error("persistAll(): [{}] batch update unknown error and try ", clazz.getSimpleName(), t); }