Skip to content

Commit 4f59c7e

Browse files
xlorneclaude
andcommitted
fix: mock/test 仓储 id 生成统一改为单调递增,避免删除后 id 复用覆盖记录
- 13 个 mock/test 仓储将 cache.size()+1 改为 nextId++ 单调递增 - 与生产 JPA @GeneratedValue(IDENTITY) 语义一致 - 消除多级流程中待办/记录删除后重建时的 id 冲突(issue #202 实现中发现) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 247086e commit 4f59c7e

13 files changed

Lines changed: 64 additions & 37 deletions

flow-engine-framework/src/main/java/com/codingapi/flow/mock/repository/FlowRecordRepositoryMockImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
public class FlowRecordRepositoryMockImpl implements FlowRecordRepository {
1111

1212
private final Map<Long, FlowRecord> cache = new HashMap<>();
13+
private long nextId = 1;
1314

1415
@Override
1516
public FlowRecord get(long id) {
@@ -45,9 +46,10 @@ public void save(FlowRecord flowRecord) {
4546
if (flowRecord.getId() > 0) {
4647
cache.put(flowRecord.getId(), flowRecord);
4748
} else {
48-
long id = cache.size() + 1;
49-
flowRecord.setId(id);
50-
cache.put(id, flowRecord);
49+
// 使用单调递增 id:删除后的 cache.size()+1 可能与已删除的 id 重复,
50+
// 进而覆盖其他记录(mock 模式下 generateRecordId() 返回 0,id 由仓储分配)
51+
flowRecord.setId(nextId++);
52+
cache.put(flowRecord.getId(), flowRecord);
5153
}
5254
}
5355

flow-engine-framework/src/main/java/com/codingapi/flow/mock/repository/FlowTodoMergeRepositoryMockImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
public class FlowTodoMergeRepositoryMockImpl implements FlowTodoMergeRepository {
1111

1212
private final Map<Long, FlowTodoMerge> cache = new HashMap<>();
13+
private long nextId = 1;
1314

1415
private void save(FlowTodoMerge relation) {
1516
if (relation.getId() > 0) {
1617
cache.put(relation.getId(), relation);
1718
} else {
18-
long id = cache.size() + 1;
19-
relation.setId(id);
20-
cache.put(id, relation);
19+
// 使用单调递增 id:删除后的 cache.size()+1 可能与已删除的 id 重复,
20+
// 进而覆盖其他记录
21+
relation.setId(nextId++);
22+
cache.put(relation.getId(), relation);
2123
}
2224
}
2325

flow-engine-framework/src/main/java/com/codingapi/flow/mock/repository/FlowTodoRecordRepositoryMockImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ public class FlowTodoRecordRepositoryMockImpl implements FlowTodoRecordRepositor
1111

1212
private final Map<Long, FlowTodoRecord> cache = new HashMap<>();
1313
private final Map<String, FlowTodoRecord> cacheByMageKey = new HashMap<>();
14+
private long nextId = 1;
1415

1516
@Override
1617
public void save(FlowTodoRecord record) {
1718
if (record.getId() > 0) {
1819
cache.put(record.getId(), record);
1920
} else {
20-
long id = cache.size() + 1;
21-
record.setId(id);
22-
cache.put(id, record);
21+
// 使用单调递增 id:删除后的 cache.size()+1 可能与已删除的 id 重复,
22+
// 进而覆盖其他待办(多级流程中 B 待办删除后新建 C 待办会冲突)
23+
record.setId(nextId++);
24+
cache.put(record.getId(), record);
2325
}
2426
cacheByMageKey.put(record.getTodoKey(), record);
2527
}

flow-engine-framework/src/main/java/com/codingapi/flow/mock/repository/SubProcessRepositoryMockImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
public class SubProcessRepositoryMockImpl implements SubProcessRepository {
1111

1212
private final Map<Long, SubProcessRecord> cache = new LinkedHashMap<>();
13+
private long nextId = 1;
1314

1415
@Override
1516
public synchronized void save(SubProcessRecord record) {
1617
if (record.getId() == 0) {
17-
record.setId(cache.size() + 1L);
18+
// 使用单调递增 id:删除后(如有)的 cache.size()+1 可能与已删除的 id 重复,
19+
// 进而覆盖其他记录
20+
record.setId(nextId++);
1821
}
1922
cache.put(record.getId(), record);
2023
}

flow-engine-framework/src/main/java/com/codingapi/flow/mock/repository/UrgeIntervalRepositoryMockImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class UrgeIntervalRepositoryMockImpl implements UrgeIntervalRepository {
1010

1111
private final Map<Long, UrgeInterval> cache = new HashMap<>();
12+
private long nextId = 1;
1213

1314

1415
@Override
@@ -23,9 +24,10 @@ public void save(UrgeInterval urgeInterval) {
2324
if (urgeInterval.getId() > 0) {
2425
cache.put(urgeInterval.getId(), urgeInterval);
2526
} else {
26-
long id = cache.size() + 1;
27-
urgeInterval.setId(id);
28-
cache.put(id, urgeInterval);
27+
// 使用单调递增 id:删除后的 cache.size()+1 可能与已删除的 id 重复,
28+
// 进而覆盖其他记录
29+
urgeInterval.setId(nextId++);
30+
cache.put(urgeInterval.getId(), urgeInterval);
2931
}
3032
}
3133
}

flow-engine-framework/src/main/java/com/codingapi/flow/mock/repository/WorkflowRuntimeRepositoryMockImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
public class WorkflowRuntimeRepositoryMockImpl implements WorkflowRuntimeRepository {
1010

1111
private final Map<Long, WorkflowRuntime> cache = new HashMap<>();
12+
private long nextId = 1;
1213

1314
@Override
1415
public void save(WorkflowRuntime workflowRuntime) {
1516
if (workflowRuntime.getId() > 0) {
1617
cache.put(workflowRuntime.getId(), workflowRuntime);
1718
} else {
18-
long id = cache.size() + 1;
19-
workflowRuntime.setId(id);
20-
cache.put(id, workflowRuntime);
19+
// 使用单调递增 id:删除后的 cache.size()+1 可能与已删除的 id 重复,
20+
// 进而覆盖其他记录(WorkflowRuntime id 被 FlowRecord.workRuntimeId 引用)
21+
workflowRuntime.setId(nextId++);
22+
cache.put(workflowRuntime.getId(), workflowRuntime);
2123
}
2224
}
2325

flow-engine-framework/src/main/java/com/codingapi/flow/mock/repository/WorkflowVersionRepositoryMockImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
public class WorkflowVersionRepositoryMockImpl implements WorkflowVersionRepository {
1212

1313
private final Map<Long, WorkflowVersion> cache = new HashMap<>();
14+
private long nextId = 1;
1415

1516
@Override
1617
public WorkflowVersion get(long id) {
@@ -56,9 +57,10 @@ public void save(WorkflowVersion workflowVersion) {
5657
if (workflowVersion.getId() > 0) {
5758
cache.put(workflowVersion.getId(), workflowVersion);
5859
} else {
59-
long id = cache.size() + 1;
60-
workflowVersion.setId(id);
61-
cache.put(id, workflowVersion);
60+
// 使用单调递增 id:删除后的 cache.size()+1 可能与已删除的 id 重复,
61+
// 进而覆盖其他版本
62+
workflowVersion.setId(nextId++);
63+
cache.put(workflowVersion.getId(), workflowVersion);
6264
}
6365
}
6466
}

flow-engine-framework/src/test/java/com/codingapi/flow/repository/FlowRecordRepositoryImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class FlowRecordRepositoryImpl implements FlowRecordRepository {
1010

1111
private final Map<Long, FlowRecord> cache = new HashMap<>();
12+
private long nextId = 1;
1213

1314
@Override
1415
public FlowRecord get(long id) {
@@ -34,9 +35,10 @@ public void save(FlowRecord flowRecord) {
3435
if (flowRecord.getId() > 0) {
3536
cache.put(flowRecord.getId(), flowRecord);
3637
} else {
37-
long id = cache.size() + 1;
38-
flowRecord.setId(id);
39-
cache.put(id, flowRecord);
38+
// 使用单调递增 id:删除后的 cache.size()+1 可能与已删除的 id 重复,
39+
// 进而覆盖其他记录(mock 模式下 generateRecordId() 返回 0,id 由仓储分配)
40+
flowRecord.setId(nextId++);
41+
cache.put(flowRecord.getId(), flowRecord);
4042
}
4143
}
4244

flow-engine-framework/src/test/java/com/codingapi/flow/repository/FlowTodoMergeRepositoryImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
public class FlowTodoMergeRepositoryImpl implements FlowTodoMergeRepository {
1010

1111
private final Map<Long, FlowTodoMerge> cache = new HashMap<>();
12+
private long nextId = 1;
1213

1314
private void save(FlowTodoMerge relation) {
1415
if (relation.getId() > 0) {
1516
cache.put(relation.getId(), relation);
1617
} else {
17-
long id = cache.size() + 1;
18-
relation.setId(id);
19-
cache.put(id, relation);
18+
// 使用单调递增 id:删除后的 cache.size()+1 可能与已删除的 id 重复,
19+
// 进而覆盖其他记录
20+
relation.setId(nextId++);
21+
cache.put(relation.getId(), relation);
2022
}
2123
}
2224

flow-engine-framework/src/test/java/com/codingapi/flow/repository/FlowTodoRecordRepositoryImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ public class FlowTodoRecordRepositoryImpl implements FlowTodoRecordRepository {
1010

1111
private final Map<Long, FlowTodoRecord> cache = new HashMap<>();
1212
private final Map<String, FlowTodoRecord> cacheByMageKey = new HashMap<>();
13+
private long nextId = 1;
1314

1415
@Override
1516
public void save(FlowTodoRecord record) {
1617
if (record.getId() > 0) {
1718
cache.put(record.getId(), record);
1819
} else {
19-
long id = cache.size() + 1;
20-
record.setId(id);
21-
cache.put(id, record);
20+
// 使用单调递增 id:删除后的 cache.size()+1 可能与已删除的 id 重复,
21+
// 进而覆盖其他待办(多级流程中 B 待办删除后新建 C 待办会冲突)
22+
record.setId(nextId++);
23+
cache.put(record.getId(), record);
2224
}
2325
cacheByMageKey.put(record.getTodoKey(), record);
2426
}

0 commit comments

Comments
 (0)