perf[orm]: optimized cache landing

This commit is contained in:
luca
2024-07-29 14:09:38 +08:00
parent 04d481557c
commit 3217238c61
2 changed files with 7 additions and 7 deletions
@@ -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);
+3 -3
View File
@@ -99,7 +99,7 @@ public class EntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> 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<PK extends Comparable<PK>, E extends IEntity<PK>> 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);
}