修复无线循环bug
This commit is contained in:
parent
c285523c1a
commit
91a2050b9f
@ -227,40 +227,60 @@ class InitConfigNode(BaseNode):
|
|||||||
# 构建章节树
|
# 构建章节树
|
||||||
chapter_map = {ch["id"]: ch for ch in chapters}
|
chapter_map = {ch["id"]: ch for ch in chapters}
|
||||||
|
|
||||||
|
# 预构建子章节映射,避免重复遍历
|
||||||
|
children_map: Dict[str, List[Dict[str, Any]]] = defaultdict(list)
|
||||||
|
for ch in chapters:
|
||||||
|
parent_id = ch.get("parent_id")
|
||||||
|
if parent_id:
|
||||||
|
children_map[parent_id].append(ch)
|
||||||
|
|
||||||
|
# 对每个父节点的子节点按order_index排序
|
||||||
|
for parent_id in children_map:
|
||||||
|
children_map[parent_id].sort(key=lambda x: x.get("order_index", 0))
|
||||||
|
|
||||||
# 找出根节点(1级标题)
|
# 找出根节点(1级标题)
|
||||||
root_chapters = [ch for ch in chapters if ch["level"] == 1]
|
root_chapters = [ch for ch in chapters if ch["level"] == 1]
|
||||||
|
|
||||||
# 深度优先遍历
|
# 深度优先遍历,使用visited集合防止循环引用
|
||||||
queue = []
|
queue: List[Dict[str, Any]] = []
|
||||||
|
visited: set[str] = set()
|
||||||
|
|
||||||
for root in root_chapters:
|
for root in root_chapters:
|
||||||
self._dfs_traverse(root, chapter_map, queue, chapters)
|
self._dfs_traverse(root, children_map, queue, visited)
|
||||||
|
|
||||||
return queue
|
return queue
|
||||||
|
|
||||||
def _dfs_traverse(
|
def _dfs_traverse(
|
||||||
self,
|
self,
|
||||||
chapter: Dict[str, Any],
|
chapter: Dict[str, Any],
|
||||||
chapter_map: Dict[str, Dict],
|
children_map: Dict[str, List[Dict[str, Any]]],
|
||||||
queue: List[Dict],
|
queue: List[Dict[str, Any]],
|
||||||
all_chapters: List[Dict],
|
visited: set[str],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""深度优先遍历
|
"""深度优先遍历
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
chapter: 当前章节
|
chapter: 当前章节
|
||||||
chapter_map: 章节映射
|
children_map: 父ID -> 子章节列表映射
|
||||||
queue: 结果队列
|
queue: 结果队列
|
||||||
all_chapters: 所有章节列表
|
visited: 已访问章节ID集合,防止循环引用
|
||||||
"""
|
"""
|
||||||
|
chapter_id = chapter["id"]
|
||||||
|
|
||||||
|
# 防止循环引用导致无限递归
|
||||||
|
if chapter_id in visited:
|
||||||
|
logger.warning("检测到循环引用,跳过章节: %s", chapter_id)
|
||||||
|
return
|
||||||
|
|
||||||
|
visited.add(chapter_id)
|
||||||
queue.append(chapter)
|
queue.append(chapter)
|
||||||
|
|
||||||
chapter_id = chapter["id"]
|
# 获取子章节(已预排序)
|
||||||
children = [ch for ch in all_chapters if ch.get("parent_id") == chapter_id]
|
children = children_map.get(chapter_id, [])
|
||||||
children.sort(key=lambda x: x.get("order_index", 0))
|
|
||||||
|
|
||||||
# 递归遍历子章节
|
# 递归遍历子章节
|
||||||
for child in children:
|
for child in children:
|
||||||
self._dfs_traverse(child, chapter_map, queue, all_chapters)
|
self._dfs_traverse(child, children_map, queue, visited)
|
||||||
|
|
||||||
def _expand_config_inheritance(self, chapter_queue: List[Dict[str, Any]]) -> Dict[str, str]:
|
def _expand_config_inheritance(self, chapter_queue: List[Dict[str, Any]]) -> Dict[str, str]:
|
||||||
"""预展开配置继承链,避免运行时查找
|
"""预展开配置继承链,避免运行时查找
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user