perf: 批量更新优先级与标签改名,按范围收敛隐私清算
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.guo.learningprogresstracker.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 批量重算优先级的一行输入(权重保存时用)。
|
||||
*
|
||||
* <p>优先级仍由 {@code CalculatedPriorityTool} 在 Java 侧算出,这里只负责把「主键 + 算好的值」交给一条
|
||||
* CASE 更新,避免逐任务 {@code updateById} 变成 N 次往返。</p>
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class TaskPriorityUpdate {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private Double calculatedPriority;
|
||||
}
|
||||
@@ -2,7 +2,10 @@ package com.guo.learningprogresstracker.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.guo.learningprogresstracker.dto.TaskPriorityUpdate;
|
||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
@@ -27,4 +30,16 @@ public interface TasksMapper extends BaseMapper<TaskEntity> {
|
||||
* @param markerName 用户标签名({@code kind=USER});空白时调用方不应走这个分支
|
||||
*/
|
||||
IPage<TaskEntity> selectPageByMarker(IPage<TaskEntity> page, @Param("markerName") String markerName);
|
||||
|
||||
/**
|
||||
* 一批任务一条 CASE 更新写入重算后的优先级(权重保存时用)。
|
||||
*
|
||||
* <p>逐任务 {@code updateById} 会把一次保存放大成任务数条往返;这里由调用方按批切分。
|
||||
* 自动填充不会作用于自定义 SQL,故审计列由调用方显式传入。</p>
|
||||
*/
|
||||
int updateCalculatedPriorities(
|
||||
@Param("rows") List<TaskPriorityUpdate> rows,
|
||||
@Param("lastModifiedTime") LocalDateTime lastModifiedTime,
|
||||
@Param("lastModifiedBy") String lastModifiedBy,
|
||||
@Param("deviceInfo") String deviceInfo);
|
||||
}
|
||||
|
||||
@@ -18,11 +18,14 @@ import com.guo.learningprogresstracker.service.MarkerService;
|
||||
import com.guo.learningprogresstracker.service.ReviewTagMindMapService;
|
||||
import com.guo.learningprogresstracker.service.recall.RecallPrivacyService;
|
||||
import com.guo.learningprogresstracker.service.review.TagDictionaryService;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -146,25 +149,34 @@ public class MarkerServiceImpl implements MarkerService {
|
||||
}
|
||||
|
||||
recallPrivacyService.tagRenamed(from);
|
||||
int renamed = 0;
|
||||
// 目标标签命中的对象一次取齐:逐条 exists 会把改名放大成「标记数 × 1」次往返
|
||||
Set<String> taken = new HashSet<>();
|
||||
for (MarkerEntity existing : markerMapper.selectList(Wrappers.<MarkerEntity>lambdaQuery()
|
||||
.eq(MarkerEntity::getKind, MarkerKindEnum.USER.name())
|
||||
.eq(MarkerEntity::getMarkerName, to))) {
|
||||
taken.add(objectKey(existing));
|
||||
}
|
||||
List<Integer> duplicates = new ArrayList<>();
|
||||
List<Integer> renames = new ArrayList<>();
|
||||
for (MarkerEntity marker : targets) {
|
||||
// 目标标签已存在于同一对象时,这条改名后的记录会与既有记录重复,直接清掉
|
||||
boolean duplicateExists = markerMapper.exists(Wrappers.<MarkerEntity>lambdaQuery()
|
||||
.eq(MarkerEntity::getObjectType, marker.getObjectType())
|
||||
.eq(MarkerEntity::getObjectNum, marker.getObjectNum())
|
||||
.eq(MarkerEntity::getKind, MarkerKindEnum.USER.name())
|
||||
.eq(MarkerEntity::getMarkerName, to));
|
||||
if (duplicateExists) {
|
||||
markerMapper.deleteById(marker.getId());
|
||||
continue;
|
||||
if (taken.contains(objectKey(marker))) {
|
||||
duplicates.add(marker.getId());
|
||||
} else {
|
||||
renames.add(marker.getId());
|
||||
}
|
||||
}
|
||||
if (!duplicates.isEmpty()) {
|
||||
markerMapper.delete(Wrappers.<MarkerEntity>lambdaQuery().in(MarkerEntity::getId, duplicates));
|
||||
}
|
||||
if (!renames.isEmpty()) {
|
||||
markerMapper.update(
|
||||
new MarkerEntity(),
|
||||
Wrappers.<MarkerEntity>lambdaUpdate()
|
||||
.eq(MarkerEntity::getId, marker.getId())
|
||||
.in(MarkerEntity::getId, renames)
|
||||
.set(MarkerEntity::getMarkerName, to));
|
||||
renamed++;
|
||||
}
|
||||
int renamed = renames.size();
|
||||
log.info("标签[{}]重命名为[{}],影响 {} 条标记", from, to, renamed);
|
||||
// 两个名字的取材范围都变了 → 相关标签导图置「待更新」(闭环-04,不自动重建)
|
||||
reviewTagMindMapService.markStaleForTags(List.of(from, to));
|
||||
@@ -354,6 +366,11 @@ public class MarkerServiceImpl implements MarkerService {
|
||||
return num;
|
||||
}
|
||||
|
||||
/** 标记落在哪个对象上:对象类型与对象号联合定位,用来判断改名后是否会与既有标记重复 */
|
||||
private static String objectKey(MarkerEntity marker) {
|
||||
return marker.getObjectType() + ":" + marker.getObjectNum();
|
||||
}
|
||||
|
||||
/** 当前登录用户,用于标签使用情况的统计(该查询不走拦截器,需显式传入) */
|
||||
private String currentUser() {
|
||||
try {
|
||||
|
||||
+56
-12
@@ -1,7 +1,10 @@
|
||||
package com.guo.learningprogresstracker.service.impl;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.guo.learningprogresstracker.common.TenantContext;
|
||||
import com.guo.learningprogresstracker.dto.PriorityDto;
|
||||
import com.guo.learningprogresstracker.dto.TaskPriorityUpdate;
|
||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||
import com.guo.learningprogresstracker.entity.UserPriorityWeightsEntity;
|
||||
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
||||
@@ -9,6 +12,8 @@ import com.guo.learningprogresstracker.mapper.TasksMapper;
|
||||
import com.guo.learningprogresstracker.mapper.UserPriorityWeightsMapper;
|
||||
import com.guo.learningprogresstracker.service.PriorityWeightsService;
|
||||
import com.guo.learningprogresstracker.utils.CalculatedPriorityTool;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -24,6 +29,8 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
public class PriorityWeightsServiceImpl implements PriorityWeightsService {
|
||||
|
||||
private static final double SUM_TOLERANCE = 0.001;
|
||||
/** 每条 CASE 更新带的任务数上限:任务很多时分批,避免单条 SQL 参数过多 */
|
||||
private static final int PRIORITY_BATCH_SIZE = 200;
|
||||
|
||||
private final UserPriorityWeightsMapper weightsMapper;
|
||||
private final TasksMapper tasksMapper;
|
||||
@@ -53,20 +60,57 @@ public class PriorityWeightsServiceImpl implements PriorityWeightsService {
|
||||
return weights;
|
||||
}
|
||||
|
||||
/** 用新权重重算当前用户的全部任务优先级 */
|
||||
/** 用新权重重算当前用户的全部任务优先级:每批一条 CASE 更新,任务数不放大往返次数。 */
|
||||
private void recalculateAllTasks(UserPriorityWeightsEntity weights) {
|
||||
List<TaskEntity> tasks = tasksMapper.selectList(Wrappers.lambdaQuery(TaskEntity.class));
|
||||
for (TaskEntity task : tasks) {
|
||||
PriorityDto dto = new PriorityDto();
|
||||
dto.setUrgency(orZero(task.getUrgency()));
|
||||
dto.setImportance(orZero(task.getImportance()));
|
||||
dto.setContentDifficulty(orZero(task.getContentDifficulty()));
|
||||
dto.setFutureValue(orZero(task.getFutureValue()));
|
||||
dto.setSubjectivePriority(orZero(task.getSubjectivePriority()));
|
||||
task.setCalculatedPriority(CalculatedPriorityTool.calculatedPriority(dto, weights));
|
||||
tasksMapper.updateById(task);
|
||||
List<TaskPriorityUpdate> rows = new ArrayList<>();
|
||||
for (TaskEntity task : tasksMapper.selectList(Wrappers.lambdaQuery(TaskEntity.class))) {
|
||||
rows.add(new TaskPriorityUpdate(
|
||||
task.getId(), CalculatedPriorityTool.calculatedPriority(priorityOf(task), weights)));
|
||||
}
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
String user = currentUser();
|
||||
String device = currentDevice();
|
||||
for (int from = 0; from < rows.size(); from += PRIORITY_BATCH_SIZE) {
|
||||
List<TaskPriorityUpdate> batch =
|
||||
rows.subList(from, Math.min(rows.size(), from + PRIORITY_BATCH_SIZE));
|
||||
tasksMapper.updateCalculatedPriorities(batch, now, user, device);
|
||||
}
|
||||
log.info("权重更新,已重算 {} 个任务的优先级", rows.size());
|
||||
}
|
||||
|
||||
private static PriorityDto priorityOf(TaskEntity task) {
|
||||
PriorityDto dto = new PriorityDto();
|
||||
dto.setUrgency(orZero(task.getUrgency()));
|
||||
dto.setImportance(orZero(task.getImportance()));
|
||||
dto.setContentDifficulty(orZero(task.getContentDifficulty()));
|
||||
dto.setFutureValue(orZero(task.getFutureValue()));
|
||||
dto.setSubjectivePriority(orZero(task.getSubjectivePriority()));
|
||||
return dto;
|
||||
}
|
||||
|
||||
/** 与自动填充同源:后台线程走 TenantContext,请求线程走 Sa-Token;取不到身份时留空不动库里的旧值。 */
|
||||
private static String currentUser() {
|
||||
TenantContext.Identity identity = TenantContext.get();
|
||||
if (identity != null) {
|
||||
return identity.loginId();
|
||||
}
|
||||
try {
|
||||
return StpUtil.getLoginIdAsString();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String currentDevice() {
|
||||
TenantContext.Identity identity = TenantContext.get();
|
||||
if (identity != null) {
|
||||
return identity.device();
|
||||
}
|
||||
try {
|
||||
return StpUtil.getLoginDevice();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
log.info("权重更新,已重算 {} 个任务的优先级", tasks.size());
|
||||
}
|
||||
|
||||
private void validate(UserPriorityWeightsEntity weights) throws ErrorParameterException {
|
||||
|
||||
@@ -36,6 +36,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -129,6 +131,12 @@ public class ReviewServiceImpl implements ReviewService {
|
||||
.eq(StudyReportFragmentsEntity::getContextNum, report.getSessionNum())
|
||||
.orderByAsc(StudyReportFragmentsEntity::getCreatedTime));
|
||||
|
||||
// 归属任务名一次 IN 取齐:computeIfAbsent 里逐个查会把分组数变成额外的 N 次往返
|
||||
Set<String> ownerTaskNums = fragments.stream()
|
||||
.map(StudyReportFragmentsEntity::getOwnerTaskNum)
|
||||
.filter(StringUtils::hasText)
|
||||
.collect(Collectors.toSet());
|
||||
Map<String, String> ownerTaskNames = loadTaskNames(ownerTaskNums);
|
||||
Map<String, StudyReportDetail.FragmentGroup> groups = new LinkedHashMap<>();
|
||||
for (StudyReportFragmentsEntity fragment : fragments) {
|
||||
String owner = fragment.getOwnerTaskNum();
|
||||
@@ -136,7 +144,7 @@ public class ReviewServiceImpl implements ReviewService {
|
||||
StudyReportDetail.FragmentGroup group = groups.computeIfAbsent(key, k -> {
|
||||
StudyReportDetail.FragmentGroup created = new StudyReportDetail.FragmentGroup();
|
||||
created.setTaskNum(StringUtils.hasText(owner) ? owner : null);
|
||||
created.setTaskName(StringUtils.hasText(owner) ? loadTaskName(owner) : null);
|
||||
created.setTaskName(StringUtils.hasText(owner) ? ownerTaskNames.get(owner) : null);
|
||||
// 旁支 = 归属不是本会话所属任务的内容
|
||||
created.setBranch(!StringUtils.hasText(owner) || !Objects.equals(owner, detail.getSessionTaskNum()));
|
||||
return created;
|
||||
@@ -261,15 +269,21 @@ public class ReviewServiceImpl implements ReviewService {
|
||||
}
|
||||
|
||||
private String loadTaskName(String taskNum) {
|
||||
if (!StringUtils.hasText(taskNum)) {
|
||||
return null;
|
||||
return StringUtils.hasText(taskNum) ? loadTaskNames(List.of(taskNum)).get(taskNum) : null;
|
||||
}
|
||||
|
||||
/** 任务名一次 IN 取齐;已删除的任务不在结果里,调用方按 null 处理 */
|
||||
private Map<String, String> loadTaskNames(Collection<String> taskNums) {
|
||||
if (taskNums.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
return tasksMapper
|
||||
.selectOne(Wrappers.<TaskEntity>lambdaQuery()
|
||||
.eq(TaskEntity::getTaskNum, taskNum)
|
||||
.select(TaskEntity::getTaskName)
|
||||
.last("LIMIT 1"))
|
||||
.getTaskName();
|
||||
Map<String, String> names = new LinkedHashMap<>();
|
||||
for (TaskEntity task : tasksMapper.selectList(Wrappers.<TaskEntity>lambdaQuery()
|
||||
.in(TaskEntity::getTaskNum, taskNums)
|
||||
.select(TaskEntity::getTaskNum, TaskEntity::getTaskName))) {
|
||||
names.put(task.getTaskNum(), task.getTaskName());
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+6
-8
@@ -79,12 +79,10 @@ public class RecallPrivacyService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void taskDeleted(String taskNum) {
|
||||
store.lock();
|
||||
for (ReviewRecallEntity row : store.all()) {
|
||||
if ("TASK".equals(row.getScopeType()) && taskNum.equals(row.getTaskNum())) {
|
||||
RecallDocument doc = readable(row);
|
||||
if (doc != null) {
|
||||
store.delete(doc);
|
||||
}
|
||||
for (ReviewRecallEntity row : store.taskScoped(taskNum)) {
|
||||
RecallDocument doc = readable(row);
|
||||
if (doc != null) {
|
||||
store.delete(doc);
|
||||
}
|
||||
}
|
||||
List<String> nums = sessions
|
||||
@@ -112,7 +110,7 @@ public class RecallPrivacyService {
|
||||
public void tagRenamed(String oldName) {
|
||||
store.lock();
|
||||
String normalized = TagScopeTool.normalize(oldName);
|
||||
for (ReviewRecallEntity row : store.all()) {
|
||||
for (ReviewRecallEntity row : store.tagScoped()) {
|
||||
RecallDocument doc = readable(row);
|
||||
if (doc == null) {
|
||||
continue;
|
||||
@@ -131,7 +129,7 @@ public class RecallPrivacyService {
|
||||
public void tagsChanged() {
|
||||
store.lock();
|
||||
List<RecallDocument> docs = new ArrayList<>();
|
||||
for (ReviewRecallEntity row : store.all()) {
|
||||
for (ReviewRecallEntity row : store.tagScoped()) {
|
||||
RecallDocument doc = readable(row);
|
||||
if (doc != null) {
|
||||
docs.add(doc);
|
||||
|
||||
@@ -329,4 +329,17 @@ public class ReviewRecallStore {
|
||||
public List<ReviewRecallEntity> all() {
|
||||
return recalls.selectList(Wrappers.<ReviewRecallEntity>lambdaQuery());
|
||||
}
|
||||
|
||||
/** 只看标签范围的回忆:隐私清算按范围类型过滤,避免把整张表(含文档正文)都解析一遍。 */
|
||||
public List<ReviewRecallEntity> tagScoped() {
|
||||
return recalls.selectList(
|
||||
Wrappers.<ReviewRecallEntity>lambdaQuery().eq(ReviewRecallEntity::getScopeType, "TAG"));
|
||||
}
|
||||
|
||||
/** 只看某个任务范围的回忆:删任务时不需要扫其它范围的记录。 */
|
||||
public List<ReviewRecallEntity> taskScoped(String taskNum) {
|
||||
return recalls.selectList(Wrappers.<ReviewRecallEntity>lambdaQuery()
|
||||
.eq(ReviewRecallEntity::getScopeType, "TASK")
|
||||
.eq(ReviewRecallEntity::getTaskNum, taskNum));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user