Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf[orm]: code style #134

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions orm/src/main/java/com/zfoo/orm/accessor/IAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 0 additions & 31 deletions orm/src/main/java/com/zfoo/orm/accessor/MongodbAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,37 +104,6 @@ public <PK extends Comparable<PK>, E extends IEntity<PK>> void batchUpdate(List<
}
}

@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")
Expand Down
24 changes: 23 additions & 1 deletion orm/src/main/java/com/zfoo/orm/cache/EntityCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,29 @@ private void doPersistNoVersion(List<PNode<PK,E>> updateList) {
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);
}
}
}

Expand Down
Loading