diff --git a/components/scheduler/src/main/java/org/cardanofoundation/ledgersync/scheduler/service/impl/PoolOfflineDataStoringServiceImpl.java b/components/scheduler/src/main/java/org/cardanofoundation/ledgersync/scheduler/service/impl/PoolOfflineDataStoringServiceImpl.java index 34806de1..9b898c5f 100644 --- a/components/scheduler/src/main/java/org/cardanofoundation/ledgersync/scheduler/service/impl/PoolOfflineDataStoringServiceImpl.java +++ b/components/scheduler/src/main/java/org/cardanofoundation/ledgersync/scheduler/service/impl/PoolOfflineDataStoringServiceImpl.java @@ -61,7 +61,6 @@ public PoolOfflineDataStoringServiceImpl( public void saveSuccessPoolOfflineData(List successPools) { long startTime = System.currentTimeMillis(); log.info("Start saving success pool offline data"); - List poolOfflineDataNeedSave = new ArrayList<>(); List poolIds = successPools.stream().map(PoolData::getPoolId).toList(); List poolMetadataRefIds = successPools.stream().map(PoolData::getMetadataRefId).toList(); @@ -84,7 +83,7 @@ public void saveSuccessPoolOfflineData(List successPools) { .stream() .collect(Collectors.toMap(PoolOfflineData::getPoolId, Function.identity())); - poolDataMap.entrySet().parallelStream().forEach(poolDataEntry -> { + List poolOfflineDataNeedSave = poolDataMap.entrySet().parallelStream().map(poolDataEntry -> { Long poolId = poolDataEntry.getKey(); PoolData poolData = poolDataEntry.getValue(); PoolHash poolHash = poolHashMap.get(poolId); @@ -96,9 +95,10 @@ public void saveSuccessPoolOfflineData(List successPools) { if (poolOfflineDataSourceMap.containsKey(poolId)) { poolOfflineData.setId(poolOfflineDataSourceMap.get(poolId).getId()); } - poolOfflineDataNeedSave.add(poolOfflineData); } - }); + + return poolOfflineData; + }).filter(Objects::nonNull).collect(Collectors.toList()); poolOfflineDataRepository.saveAll(poolOfflineDataNeedSave); log.info("Saved success pool offline data, count: {}, time taken: {} ms", @@ -111,7 +111,6 @@ public void saveFailOfflineData(List failedPools) { long startTime = System.currentTimeMillis(); log.info("Start saving fail pool offline data"); - List poolOfflineFetchErrorsNeedSave = new ArrayList<>(); List poolIds = failedPools.stream().map(PoolData::getPoolId).toList(); List poolMetadataRefIds = failedPools.stream().map(PoolData::getMetadataRefId).toList(); @@ -130,7 +129,8 @@ public void saveFailOfflineData(List failedPools) { .stream() .collect(Collectors.toMap(PoolMetadataRef::getId, Function.identity())); - poolDataMap.entrySet().parallelStream().forEach(poolDataEntry -> { + List poolOfflineFetchErrorsNeedSave = + poolDataMap.entrySet().parallelStream().map(poolDataEntry -> { Long poolId = poolDataEntry.getKey(); PoolData poolData = poolDataEntry.getValue(); PoolHash poolHash = poolHashMap.get(poolId); @@ -153,8 +153,9 @@ public void saveFailOfflineData(List failedPools) { .fetchTime(Timestamp.valueOf(LocalDateTime.now(ZoneOffset.UTC))) .build(); } - poolOfflineFetchErrorsNeedSave.add(poolOfflineFetchError); - }); + + return poolOfflineFetchError; + }).collect(Collectors.toList()); poolOfflineFetchErrorRepository.saveAll(poolOfflineFetchErrorsNeedSave); log.info("Saved success fail pool offline data, count: {}, time taken: {} ms",