From 8761644af158c5e7f05e999e9834b6496ce414e7 Mon Sep 17 00:00:00 2001
From: yeg <1347747024@qq.com>
Date: 星期六, 29 八月 2026 16:04:31 +0800
Subject: [PATCH] 优化采集器长时历史的有界读取

---
 ruoyi-device/src/main/java/com/ruoyi/device/simulator/InMemoryBtsSimulatorStateStore.java |   35 ++++++++++++++++++++++++++---------
 1 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/ruoyi-device/src/main/java/com/ruoyi/device/simulator/InMemoryBtsSimulatorStateStore.java b/ruoyi-device/src/main/java/com/ruoyi/device/simulator/InMemoryBtsSimulatorStateStore.java
index 1c6db82..48e1263 100644
--- a/ruoyi-device/src/main/java/com/ruoyi/device/simulator/InMemoryBtsSimulatorStateStore.java
+++ b/ruoyi-device/src/main/java/com/ruoyi/device/simulator/InMemoryBtsSimulatorStateStore.java
@@ -5,6 +5,7 @@
 import java.util.List;
 import java.util.Map;
 import com.ruoyi.device.simulator.BtsSimulatorStateStore.CollectorHistoryAppend;
+import com.ruoyi.device.simulator.BtsSimulatorStateStore.CollectorHistoryBatch;
 import com.ruoyi.device.simulator.BtsSimulatorStateStore.CollectorMeasurementRecord;
 import com.ruoyi.device.simulator.BtsSimulatorStateStore.RecentRequest;
 import com.ruoyi.device.simulator.BtsSimulatorStateStore.SimulatorState;
@@ -14,7 +15,7 @@
 {
     private SimulatorState state;
     private List<RecentRequest> recentRequests = List.of();
-    private final Map<String, List<CollectorMeasurementRecord>> collectorHistory = new LinkedHashMap<>();
+    private final Map<String, ArrayList<CollectorMeasurementRecord>> collectorHistory = new LinkedHashMap<>();
 
     public InMemoryBtsSimulatorStateStore() { }
 
@@ -40,19 +41,35 @@
     public synchronized void save(SimulatorState state, List<CollectorHistoryAppend> historyAppends)
     {
         for (CollectorHistoryAppend append : historyAppends)
-        {
-            List<CollectorMeasurementRecord> records = new ArrayList<>(
-                    collectorHistory.getOrDefault(append.collectorCode(), List.of()));
-            records.addAll(append.records());
-            collectorHistory.put(append.collectorCode(), List.copyOf(records));
-        }
+            collectorHistory.computeIfAbsent(append.collectorCode(), key -> new ArrayList<>())
+                    .addAll(append.records());
         this.state = state;
     }
 
     @Override
-    public synchronized List<CollectorMeasurementRecord> loadCollectorHistory(String collectorCode)
+    public synchronized CollectorHistoryBatch loadCollectorHistoryPage(String collectorCode, long offset,
+            int limit, boolean reverse)
     {
-        return collectorHistory.getOrDefault(collectorCode, List.of());
+        if (offset < 0) throw new IllegalArgumentException("offset must not be negative");
+        if (limit < 1 || limit > MAX_HISTORY_BATCH_SIZE)
+            throw new IllegalArgumentException("limit must be between 1 and " + MAX_HISTORY_BATCH_SIZE);
+        List<CollectorMeasurementRecord> history = collectorHistory.getOrDefault(collectorCode,
+                new ArrayList<>());
+        long total = history.size();
+        int from = (int) Math.min(offset, total);
+        int count = Math.min(limit, history.size() - from);
+        List<CollectorMeasurementRecord> records = new ArrayList<>(count);
+        if (reverse)
+        {
+            for (int index = history.size() - 1 - from; index >= history.size() - from - count; index--)
+                records.add(history.get(index));
+        }
+        else
+        {
+            records.addAll(history.subList(from, from + count));
+        }
+        long nextOffset = from + records.size();
+        return new CollectorHistoryBatch(offset, nextOffset, nextOffset < total, total, records);
     }
 
     @Override

--
Gitblit v1.7.1