fix: 修复回忆列表并发死锁并收敛多表查询

This commit is contained in:
2026-11-21 16:54:07 +08:00
parent 4b65eb57b1
commit 9c3e22680d
17 changed files with 497 additions and 128 deletions
@@ -1,6 +1,7 @@
package com.guo.learningprogresstracker.service.recall;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.guo.learningprogresstracker.dto.recall.RecallDtos.RecallDocument;
import com.guo.learningprogresstracker.dto.recall.RecallDtos.ReferenceSnapshot;
import com.guo.learningprogresstracker.dto.recall.RecallDtos.ReferenceSource;
import com.guo.learningprogresstracker.dto.recall.RecallDtos.ReferenceUnit;
@@ -10,6 +11,7 @@ import com.guo.learningprogresstracker.entity.BaseEntity;
import com.guo.learningprogresstracker.entity.MarkerEntity;
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
import com.guo.learningprogresstracker.entity.StudyReportsEntity;
import com.guo.learningprogresstracker.entity.TaskApplicationEntity;
import com.guo.learningprogresstracker.entity.TaskEntity;
import com.guo.learningprogresstracker.enums.TagCombineModeEnum;
import com.guo.learningprogresstracker.exception.RecallBusinessException;
@@ -21,6 +23,7 @@ import com.guo.learningprogresstracker.mapper.TasksMapper;
import com.guo.learningprogresstracker.utils.TagScopeTool;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -28,6 +31,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
@@ -93,18 +97,82 @@ public class RecallReferenceLoader {
return TagScopeTool.scopeKey(tags, TagCombineModeEnum.valueOf(combineMode));
}
/**
* 一次请求内共享的来源快照:可达性与标签范围有效性都从这几张「id 全集」里判断。
* 逐条 selectById 会把列表页放大成每行 N+1 条查询,而来源本来就是按租户整表可见的。
*/
public record SourceIndex(
Set<String> fragments,
Set<String> reports,
Set<String> applications,
Set<String> taskNums,
List<MarkerEntity> markers) {}
/** 文档集合的索引:列表页一次建好,逐行复用。 */
public SourceIndex index(Collection<RecallDocument> docs) {
List<ReferenceSource> sources = new ArrayList<>();
List<ScopeSnapshot> scopes = new ArrayList<>();
for (RecallDocument doc : docs) {
sources.addAll(doc.getReference().sources());
scopes.add(doc.getDetail().getScope());
}
return index(sources, scopes);
}
public SourceIndex index(Collection<ReferenceSource> sources, Collection<ScopeSnapshot> scopes) {
boolean tagScope = scopes.stream().anyMatch(s -> "TAG".equals(s.type()));
boolean taskScope = scopes.stream().anyMatch(s -> "TASK".equals(s.type()));
boolean fragmentSource = sources.stream().anyMatch(s -> "FRAGMENT".equals(s.type()));
boolean reportSource = sources.stream().anyMatch(s -> "REPORT".equals(s.type()));
boolean applicationSource = sources.stream().anyMatch(s -> "APPLICATION".equals(s.type()));
Set<String> fragmentIds = tagScope || fragmentSource ? fragmentIds() : Set.of();
Set<String> reportIds = reportSource ? reportIds() : Set.of();
Set<String> applicationIds = applicationSource ? applicationIds() : Set.of();
Set<String> taskNums = taskScope ? taskNums() : Set.of();
List<MarkerEntity> markers = tagScope ? actualMarkers() : List.of();
return new SourceIndex(fragmentIds, reportIds, applicationIds, taskNums, markers);
}
private Set<String> fragmentIds() {
return fragments.selectList(Wrappers.<StudyReportFragmentsEntity>lambdaQuery()
.select(StudyReportFragmentsEntity::getId))
.stream()
.map(f -> f.getId().toString())
.collect(Collectors.toSet());
}
private Set<String> reportIds() {
return reports.selectList(Wrappers.<StudyReportsEntity>lambdaQuery().select(StudyReportsEntity::getId))
.stream()
.map(r -> r.getId().toString())
.collect(Collectors.toSet());
}
private Set<String> applicationIds() {
return applications.selectList(Wrappers.<TaskApplicationEntity>lambdaQuery()
.select(TaskApplicationEntity::getId))
.stream()
.map(a -> a.getId().toString())
.collect(Collectors.toSet());
}
private Set<String> taskNums() {
return tasks.selectList(Wrappers.<TaskEntity>lambdaQuery().select(TaskEntity::getTaskNum)).stream()
.map(TaskEntity::getTaskNum)
.collect(Collectors.toSet());
}
public boolean valid(ScopeSnapshot scope) {
return valid(scope, index(List.of(), List.of(scope)));
}
public boolean valid(ScopeSnapshot scope, SourceIndex index) {
if ("TASK".equals(scope.type())) {
return tasks.exists(Wrappers.<TaskEntity>lambdaQuery().eq(TaskEntity::getTaskNum, scope.taskNum()));
return index.taskNums().contains(scope.taskNum());
}
Set<String> actual = new HashSet<>();
Set<String> accessible = new HashSet<>();
for (StudyReportFragmentsEntity f : fragments.selectList(
Wrappers.<StudyReportFragmentsEntity>lambdaQuery().select(StudyReportFragmentsEntity::getId))) {
accessible.add(f.getId().toString());
}
for (MarkerEntity marker : actualMarkers()) {
if (accessible.contains(marker.getObjectNum())) {
for (MarkerEntity marker : index.markers()) {
if (index.fragments().contains(marker.getObjectNum())) {
actual.add(TagScopeTool.normalize(marker.getMarkerName()));
}
}
@@ -223,19 +291,11 @@ public class RecallReferenceLoader {
return units;
}
public boolean accessible(ReferenceSource source) {
return accessible(source.type(), source.entityId());
}
public boolean accessible(String type, String entityId) {
Integer id = parseId(entityId);
if (id == null) {
return false;
}
return switch (type) {
case "FRAGMENT" -> fragments.selectById(id) != null;
case "REPORT" -> reports.selectById(id) != null;
case "APPLICATION" -> applications.selectById(id) != null;
public boolean accessible(ReferenceSource source, SourceIndex index) {
return switch (source.type()) {
case "FRAGMENT" -> index.fragments().contains(source.entityId());
case "REPORT" -> index.reports().contains(source.entityId());
case "APPLICATION" -> index.applications().contains(source.entityId());
default -> false;
};
}