| | |
| | | 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.BtsHistoryRecord; |
| | | import com.ruoyi.device.simulator.BtsSimulatorStateStore.BtsHistoryBatch; |
| | | 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<>(); |
| | | private final Map<String, ArrayList<BtsHistoryRecord>> btsHistory = new LinkedHashMap<>(); |
| | | |
| | | public InMemoryBtsSimulatorStateStore() { } |
| | | |
| | |
| | | { |
| | | 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)); |
| | | ArrayList<CollectorMeasurementRecord> history = collectorHistory.computeIfAbsent( |
| | | append.collectorCode(), key -> new ArrayList<>()); |
| | | history.addAll(append.records()); |
| | | int overflow = history.size() - (int) MAX_HISTORY_RECORDS; |
| | | if (overflow > 0) history.subList(0, overflow).clear(); |
| | | } |
| | | 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 |
| | |
| | | { |
| | | collectorHistory.remove(collectorCode); |
| | | this.state = state; |
| | | } |
| | | |
| | | @Override |
| | | public synchronized BtsHistoryBatch loadBtsHistoryPage(String historyKey, long offset, int limit) |
| | | { |
| | | List<BtsHistoryRecord> history = btsHistory.getOrDefault(historyKey, new ArrayList<>()); |
| | | int from = (int) Math.min(offset, history.size()); |
| | | int count = Math.min(limit, history.size() - from); |
| | | List<BtsHistoryRecord> records = new ArrayList<>(history.subList(from, from + count)); |
| | | long next = from + records.size(); |
| | | return new BtsHistoryBatch(offset, next, next < history.size(), history.size(), records); |
| | | } |
| | | |
| | | @Override |
| | | public synchronized void appendBtsHistory(String historyKey, BtsHistoryRecord record) |
| | | { |
| | | ArrayList<BtsHistoryRecord> history = btsHistory.computeIfAbsent(historyKey, key -> new ArrayList<>()); |
| | | history.add(record); |
| | | if (history.size() > MAX_HISTORY_RECORDS) history.remove(0); |
| | | } |
| | | |
| | | @Override |
| | |
| | | state = defaultState; |
| | | recentRequests = List.of(); |
| | | collectorHistory.clear(); |
| | | btsHistory.clear(); |
| | | } |
| | | } |