Skip to content

Commit

Permalink
Optimise/simplify some stream filtering for the REST API
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
stejbac committed Jul 28, 2024
1 parent bbd53f1 commit 66d6530
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import bisq.common.util.DateUtil;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
Expand Down Expand Up @@ -54,20 +53,15 @@ public void addReceivedBtcBalanceEntry(ReceivedBtcBalanceEntry balanceEntry) {
receivedBtcBalanceEntries.add(balanceEntry);

Date month = balanceEntry.getMonth();
receivedBtcBalanceEntriesByMonth.putIfAbsent(month, new HashSet<>());
receivedBtcBalanceEntriesByMonth.get(month).add(balanceEntry);
receivedBtcBalanceEntriesByMonth.computeIfAbsent(month, m -> new HashSet<>()).add(balanceEntry);
}

public Set<ReceivedBtcBalanceEntry> getReceivedBtcBalanceEntries() {
return receivedBtcBalanceEntries;
}

public Set<ReceivedBtcBalanceEntry> getReceivedBtcBalanceEntriesByMonth(Date month) {
return receivedBtcBalanceEntriesByMonth.entrySet().stream()
.filter(e -> e.getKey().equals(month))
.map(Map.Entry::getValue)
.flatMap(Collection::stream)
.collect(Collectors.toSet());
return receivedBtcBalanceEntriesByMonth.getOrDefault(month, Set.of());
}

public Stream<BurnedBsqBalanceEntry> getBurnedBsqBalanceEntries(Set<BurnOutputModel> burnOutputModels) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@

import bisq.common.config.Config;
import bisq.common.file.JsonFileManager;
import bisq.common.util.RangeUtils;

import com.google.inject.Inject;

import javax.inject.Named;
import javax.inject.Singleton;

import com.google.common.collect.Range;

import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;

Expand Down Expand Up @@ -136,10 +139,10 @@ public ObservableSet<TradeStatistics3> getObservableTradeStatisticsSet() {
}

public List<TradeStatistics3> getTradeStatisticsList(long dateStart, long dateEnd) {
return observableTradeStatisticsSet.stream()
.filter(x -> x.getDateAsLong() > dateStart && x.getDateAsLong() <= dateEnd)
.sorted((o1, o2) -> (Long.compare(o2.getDateAsLong(), o1.getDateAsLong())))
.collect(Collectors.toList());
return new ArrayList<>(RangeUtils.subSet(navigableTradeStatisticsSet)
.withKey(TradeStatistics3::getDateAsLong)
.overRange(Range.openClosed(Math.min(dateStart, dateEnd), dateEnd))
.descendingSet());
}

private void maybeDumpStatistics() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ private String getDefaultRateExceededError(String methodName,

private Optional<Map.Entry<String, GrpcCallRateMeter>> getRateMeterKV(ServerCall<?, ?> serverCall) {
String rateMeterKey = getRateMeterKey(serverCall);
return serviceCallRateMeters.entrySet().stream()
.filter((e) -> e.getKey().equals(rateMeterKey)).findFirst();
return Optional.ofNullable(serviceCallRateMeters.get(rateMeterKey))
.map(meter -> Map.entry(rateMeterKey, meter));
}

private String getRateMeterKey(ServerCall<?, ?> serverCall) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
import bisq.core.dao.state.model.blockchain.Tx;
import bisq.core.dao.state.model.blockchain.TxType;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -86,10 +85,7 @@ public List<JsonTx> getBisqTxForAddr(@PathParam("addr") String address) {
address = address.substring(1, address.length());
}
String finalAddress = address;
List<JsonTx> result = daoStateService.getTxIdSetByAddress().entrySet().stream()
.filter(e -> e.getKey().equals(finalAddress))
.map(Map.Entry::getValue)
.flatMap(Collection::stream)
List<JsonTx> result = daoStateService.getTxIdSetByAddress().getOrDefault(finalAddress, Set.of()).stream()
.flatMap(txId -> daoStateService.getTx(txId).stream())
.map(tx -> BlockDataToJsonConverter.getJsonTx(daoStateService, tx))
.collect(Collectors.toList());
Expand Down

0 comments on commit 66d6530

Please sign in to comment.