| | |
| | | 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; |
| | |
| | | { |
| | | 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() { } |
| | | |
| | |
| | | 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 |