Optimise/simplify some stream filtering for the REST API

Replace the streaming of Map entry sets to pick out a single entry by
key equality, and instead do a lookup into the map. Also, optimise the
date range filtering in 'TradeStatisticsManager::getTradeStatisticsList'
by using 'RangeUtils::subSet' to avoid scanning the entire collection.
(This method is applicable, as the trade statistics set is navigable and
naturally sorted by date.)
This commit is contained in:
Steven Barclay 2024-07-28 21:46:46 +02:00
parent bbd53f1895
commit 66d6530652
No known key found for this signature in database
GPG Key ID: 9FED6BF1176D500B
4 changed files with 13 additions and 20 deletions

View File

@ -23,7 +23,6 @@ import bisq.core.dao.burningman.model.BurningManCandidate;
import bisq.common.util.DateUtil; import bisq.common.util.DateUtil;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.HashMap; import java.util.HashMap;
@ -54,8 +53,7 @@ public class BalanceModel {
receivedBtcBalanceEntries.add(balanceEntry); receivedBtcBalanceEntries.add(balanceEntry);
Date month = balanceEntry.getMonth(); Date month = balanceEntry.getMonth();
receivedBtcBalanceEntriesByMonth.putIfAbsent(month, new HashSet<>()); receivedBtcBalanceEntriesByMonth.computeIfAbsent(month, m -> new HashSet<>()).add(balanceEntry);
receivedBtcBalanceEntriesByMonth.get(month).add(balanceEntry);
} }
public Set<ReceivedBtcBalanceEntry> getReceivedBtcBalanceEntries() { public Set<ReceivedBtcBalanceEntry> getReceivedBtcBalanceEntries() {
@ -63,11 +61,7 @@ public class BalanceModel {
} }
public Set<ReceivedBtcBalanceEntry> getReceivedBtcBalanceEntriesByMonth(Date month) { public Set<ReceivedBtcBalanceEntry> getReceivedBtcBalanceEntriesByMonth(Date month) {
return receivedBtcBalanceEntriesByMonth.entrySet().stream() return receivedBtcBalanceEntriesByMonth.getOrDefault(month, Set.of());
.filter(e -> e.getKey().equals(month))
.map(Map.Entry::getValue)
.flatMap(Collection::stream)
.collect(Collectors.toSet());
} }
public Stream<BurnedBsqBalanceEntry> getBurnedBsqBalanceEntries(Set<BurnOutputModel> burnOutputModels) { public Stream<BurnedBsqBalanceEntry> getBurnedBsqBalanceEntries(Set<BurnOutputModel> burnOutputModels) {

View File

@ -33,12 +33,15 @@ import bisq.network.p2p.storage.persistence.AppendOnlyDataStoreService;
import bisq.common.config.Config; import bisq.common.config.Config;
import bisq.common.file.JsonFileManager; import bisq.common.file.JsonFileManager;
import bisq.common.util.RangeUtils;
import com.google.inject.Inject; import com.google.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import com.google.common.collect.Range;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableSet; import javafx.collections.ObservableSet;
@ -136,10 +139,10 @@ public class TradeStatisticsManager {
} }
public List<TradeStatistics3> getTradeStatisticsList(long dateStart, long dateEnd) { public List<TradeStatistics3> getTradeStatisticsList(long dateStart, long dateEnd) {
return observableTradeStatisticsSet.stream() return new ArrayList<>(RangeUtils.subSet(navigableTradeStatisticsSet)
.filter(x -> x.getDateAsLong() > dateStart && x.getDateAsLong() <= dateEnd) .withKey(TradeStatistics3::getDateAsLong)
.sorted((o1, o2) -> (Long.compare(o2.getDateAsLong(), o1.getDateAsLong()))) .overRange(Range.openClosed(Math.min(dateStart, dateEnd), dateEnd))
.collect(Collectors.toList()); .descendingSet());
} }
private void maybeDumpStatistics() { private void maybeDumpStatistics() {

View File

@ -103,8 +103,8 @@ public final class CallRateMeteringInterceptor implements ServerInterceptor {
private Optional<Map.Entry<String, GrpcCallRateMeter>> getRateMeterKV(ServerCall<?, ?> serverCall) { private Optional<Map.Entry<String, GrpcCallRateMeter>> getRateMeterKV(ServerCall<?, ?> serverCall) {
String rateMeterKey = getRateMeterKey(serverCall); String rateMeterKey = getRateMeterKey(serverCall);
return serviceCallRateMeters.entrySet().stream() return Optional.ofNullable(serviceCallRateMeters.get(rateMeterKey))
.filter((e) -> e.getKey().equals(rateMeterKey)).findFirst(); .map(meter -> Map.entry(rateMeterKey, meter));
} }
private String getRateMeterKey(ServerCall<?, ?> serverCall) { private String getRateMeterKey(ServerCall<?, ?> serverCall) {

View File

@ -22,11 +22,10 @@ import bisq.core.dao.state.model.blockchain.BaseTx;
import bisq.core.dao.state.model.blockchain.Tx; import bisq.core.dao.state.model.blockchain.Tx;
import bisq.core.dao.state.model.blockchain.TxType; import bisq.core.dao.state.model.blockchain.TxType;
import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -86,10 +85,7 @@ public class ExplorerTransactionsApi {
address = address.substring(1, address.length()); address = address.substring(1, address.length());
} }
String finalAddress = address; String finalAddress = address;
List<JsonTx> result = daoStateService.getTxIdSetByAddress().entrySet().stream() List<JsonTx> result = daoStateService.getTxIdSetByAddress().getOrDefault(finalAddress, Set.of()).stream()
.filter(e -> e.getKey().equals(finalAddress))
.map(Map.Entry::getValue)
.flatMap(Collection::stream)
.flatMap(txId -> daoStateService.getTx(txId).stream()) .flatMap(txId -> daoStateService.getTx(txId).stream())
.map(tx -> BlockDataToJsonConverter.getJsonTx(daoStateService, tx)) .map(tx -> BlockDataToJsonConverter.getJsonTx(daoStateService, tx))
.collect(Collectors.toList()); .collect(Collectors.toList());