From 71d499e1ad7d4e2f2886627f7a57e0fadf0bc235 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期三, 06 五月 2026 16:48:11 +0800
Subject: [PATCH] #dfs
---
src/main/java/com/zy/common/utils/NavigateSolution.java | 77 +++++++++++++++++++++++++++++++++++++-
1 files changed, 74 insertions(+), 3 deletions(-)
diff --git a/src/main/java/com/zy/common/utils/NavigateSolution.java b/src/main/java/com/zy/common/utils/NavigateSolution.java
index 5dd839c..7845e9b 100644
--- a/src/main/java/com/zy/common/utils/NavigateSolution.java
+++ b/src/main/java/com/zy/common/utils/NavigateSolution.java
@@ -23,6 +23,7 @@
private static final long REDIS_MAP_CACHE_TTL_SECONDS = 60 * 60 * 24L;
private static final long MAP_CACHE_TTL_MS = 5000L;
+ private static final long SIMPLE_PATH_SEARCH_TIMEOUT_MS = 3000L;
private static final Map<String, CachedNavigateMap> NAVIGATE_MAP_CACHE = new ConcurrentHashMap<>();
private static final Map<String, CachedNavigateMapIndex> NAVIGATE_MAP_INDEX_CACHE = new ConcurrentHashMap<>();
@@ -459,6 +460,14 @@
AtomicInteger pathCount = new AtomicInteger(0);
Map<Integer, NavigateNode> guideNodeMap = buildGuideNodeMap(map, guideStationIds);
+ SimplePathSearchContext searchContext = new SimplePathSearchContext(
+ start,
+ end,
+ maxDepth,
+ maxPaths,
+ maxCost,
+ System.currentTimeMillis() + SIMPLE_PATH_SEARCH_TIMEOUT_MS
+ );
dfsAllSimplePaths(map, start, end,
visited, path, results,
@@ -467,7 +476,8 @@
maxDepth, maxPaths, maxCost,
pathCount,
guideStationIds,
- guideNodeMap
+ guideNodeMap,
+ searchContext
);
return results;
@@ -490,8 +500,13 @@
int maxCost,
AtomicInteger pathCount,
List<Integer> guideStationIds,
- Map<Integer, NavigateNode> guideNodeMap
+ Map<Integer, NavigateNode> guideNodeMap,
+ SimplePathSearchContext searchContext
) {
+ if (isSimplePathSearchTimedOut(searchContext, results, pathCount, depth)) {
+ return;
+ }
+
// 闃茬垎锛氭潯鏁伴檺鍒�
if (maxPaths > 0 && pathCount.get() >= maxPaths) return;
@@ -511,6 +526,10 @@
neighbors = sortNeighborsByGuide(current, path, neighbors, guideStationIds, guideNodeMap);
for (NavigateNode next : neighbors) {
+ if (isSimplePathSearchTimedOut(searchContext, results, pathCount, depth)) {
+ return;
+ }
+
// 闃茬垎锛氭潯鏁伴檺鍒�
if (maxPaths > 0 && pathCount.get() >= maxPaths) return;
@@ -540,13 +559,41 @@
maxDepth, maxPaths, maxCost,
pathCount,
guideStationIds,
- guideNodeMap
+ guideNodeMap,
+ searchContext
);
// 鍥炴函
path.removeLast();
visited.remove(nk);
}
+ }
+
+ private boolean isSimplePathSearchTimedOut(SimplePathSearchContext searchContext,
+ List<List<NavigateNode>> results,
+ AtomicInteger pathCount,
+ int depth) {
+ if (searchContext == null || searchContext.timeoutLogged) {
+ return searchContext != null && searchContext.timeoutLogged;
+ }
+ if (System.currentTimeMillis() < searchContext.deadlineMs) {
+ return false;
+ }
+
+ searchContext.timeoutLogged = true;
+ Integer startStationId = extractStationId(searchContext.startNode);
+ Integer endStationId = extractStationId(searchContext.endNode);
+ com.zy.core.News.warn("绔欑偣璺緞鍊欓�夋灇涓捐秴鏃惰繑鍥烇紝startStationId={}锛宔ndStationId={}锛宼imeoutMs={}锛宖oundPathCount={}锛宺esultSize={}锛宒epth={}锛宮axDepth={}锛宮axPaths={}锛宮axCost={}",
+ startStationId,
+ endStationId,
+ SIMPLE_PATH_SEARCH_TIMEOUT_MS,
+ pathCount == null ? 0 : pathCount.get(),
+ results == null ? 0 : results.size(),
+ depth,
+ searchContext.maxDepth,
+ searchContext.maxPaths,
+ searchContext.maxCost);
+ return true;
}
private String keyOf(NavigateNode n) {
@@ -1082,6 +1129,30 @@
}
}
+ private static class SimplePathSearchContext {
+ private final NavigateNode startNode;
+ private final NavigateNode endNode;
+ private final int maxDepth;
+ private final int maxPaths;
+ private final int maxCost;
+ private final long deadlineMs;
+ private boolean timeoutLogged;
+
+ private SimplePathSearchContext(NavigateNode startNode,
+ NavigateNode endNode,
+ int maxDepth,
+ int maxPaths,
+ int maxCost,
+ long deadlineMs) {
+ this.startNode = startNode;
+ this.endNode = endNode;
+ this.maxDepth = maxDepth;
+ this.maxPaths = maxPaths;
+ this.maxCost = maxCost;
+ this.deadlineMs = deadlineMs;
+ }
+ }
+
private static class CachedNavigateMap {
private final long cacheAtMs;
private final List<List<NavigateNode>> navigateMap;
--
Gitblit v1.9.1