mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-09 00:27:11 +00:00
@@ -34,8 +34,6 @@ public interface IAccessor {
|
||||
|
||||
<PK extends Comparable<PK>, E extends IEntity<PK>> void batchUpdate(List<E> entities);
|
||||
|
||||
<PK extends Comparable<PK>, E extends IEntity<PK>> void batchUpdateNode(List<PNode<PK,E>> entities);
|
||||
|
||||
<PK extends Comparable<PK>, E extends IEntity<PK>> boolean delete(E entity);
|
||||
|
||||
<PK extends Comparable<PK>, E extends IEntity<PK>> boolean delete(PK pk, Class<E> entityClazz);
|
||||
|
||||
@@ -104,37 +104,6 @@ public class MongodbAccessor implements IAccessor {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <PK extends Comparable<PK>, E extends IEntity<PK>> void batchUpdateNode(List<PNode<PK,E>> nodes) {
|
||||
if (CollectionUtils.isEmpty(nodes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
var entityClazz = (Class<E>) nodes.get(0).getClass();
|
||||
var collection = OrmContext.getOrmManager().getCollection(entityClazz);
|
||||
List<E> entities = nodes.stream().map(PNode::getEntity).toList();
|
||||
var batchList = entities.stream()
|
||||
.map(it -> new ReplaceOneModel<E>(Filters.eq("_id", it.id()), it))
|
||||
.toList();
|
||||
|
||||
var result = collection.bulkWrite(batchList, new BulkWriteOptions().ordered(false));
|
||||
|
||||
//设置修改时间
|
||||
long currentTime = TimeUtils.currentTimeMillis();
|
||||
nodes.forEach(k->k.resetTime(currentTime));
|
||||
|
||||
if (result.getMatchedCount() != entities.size()) {
|
||||
// 在数据库的批量更新操作中需要更新的数量和最终更新的数量不相同
|
||||
logger.warn("database:[{}] update size:[{}] not equal with matched size:[{}](some entity of id not exist in database)"
|
||||
, entityClazz.getSimpleName(), entities.size(), result.getMatchedCount());
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
logger.error("batchUpdate unknown exception", t);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <PK extends Comparable<PK>, E extends IEntity<PK>> boolean delete(E entity) {
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
+23
-1
@@ -309,7 +309,29 @@ public class EntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> imple
|
||||
for (var currentPage = 1; currentPage <= maxPageSize; currentPage++) {
|
||||
page.setPage(currentPage);
|
||||
var currentUpdateList = page.currentPageList(updateList);
|
||||
OrmContext.getAccessor().batchUpdateNode(currentUpdateList);
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
var entityClazz = (Class<E>) currentUpdateList.get(0).getClass();
|
||||
var collection = OrmContext.getOrmManager().getCollection(entityClazz);
|
||||
List<E> entities = currentUpdateList.stream().map(PNode::getEntity).toList();
|
||||
var batchList = entities.stream()
|
||||
.map(it -> new ReplaceOneModel<E>(Filters.eq("_id", it.id()), it))
|
||||
.toList();
|
||||
|
||||
var result = collection.bulkWrite(batchList, new BulkWriteOptions().ordered(false));
|
||||
|
||||
//设置修改时间
|
||||
long currentTime = TimeUtils.currentTimeMillis();
|
||||
currentUpdateList.forEach(k->k.resetTime(currentTime));
|
||||
|
||||
if (result.getMatchedCount() != entities.size()) {
|
||||
// 在数据库的批量更新操作中需要更新的数量和最终更新的数量不相同
|
||||
logger.warn("database:[{}] update size:[{}] not equal with matched size:[{}](some entity of id not exist in database)"
|
||||
, entityClazz.getSimpleName(), entities.size(), result.getMatchedCount());
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
logger.error("batchUpdate unknown exception", t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user