Skip to content

Commit adfda88

Browse files
committed
chore: Update
1 parent 1f34b5b commit adfda88

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

aggregates/account/src/main/java/org/cardanofoundation/ledgersync/account/processor/AddressTxAmountProcessor.java

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,49 @@ public class AddressTxAmountProcessor {
3737
private List<Pair<EventMetadata, TxInputOutput>> txInputOutputListCache = Collections.synchronizedList(new ArrayList<>());
3838
private List<AddressTxAmount> addressTxAmountListCache = Collections.synchronizedList(new ArrayList<>());
3939

40+
private Map<Integer, List<AddressTxAmount>> addressTxAmountListCacheMap = new HashMap<>();
41+
private int bucketSize = 10;
42+
4043
private final PlatformTransactionManager transactionManager;
4144
private TransactionTemplate transactionTemplate;
4245

4346
@PostConstruct
4447
void init() {
4548
transactionTemplate = new TransactionTemplate(transactionManager);
4649
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
50+
51+
initAddressTxAmtCache();
52+
}
53+
54+
private void initAddressTxAmtCache() {
55+
for (int i = 0; i < bucketSize; i++) {
56+
addressTxAmountListCacheMap.put(i, Collections.synchronizedList(new ArrayList<>()));
57+
}
58+
}
59+
60+
private void save(long blockNo, List<AddressTxAmount> addressTxAmounts) {
61+
int index = (int) (blockNo % bucketSize);
62+
List<AddressTxAmount> addressTxAmountList = addressTxAmountListCacheMap.get(index);
63+
addressTxAmountList.addAll(addressTxAmounts);
64+
65+
if (addressTxAmountList.size() > 1000) {
66+
synchronized (addressTxAmountList) {
67+
addressTxAmountStorage.save(addressTxAmountList);
68+
if (log.isDebugEnabled())
69+
log.debug("-- Saved address_tx_amounts records : {}", addressTxAmountList.size());
70+
addressTxAmountList.clear();
71+
}
72+
}
73+
}
74+
75+
private void clearCache() {
76+
for (int i = 0; i < bucketSize; i++) {
77+
List<AddressTxAmount> addressTxAmountList = addressTxAmountListCacheMap.get(i);
78+
if (addressTxAmountList.size() > 0) {
79+
//addressTxAmountStorage.save(addressTxAmountList);
80+
addressTxAmountList.clear();
81+
}
82+
}
4783
}
4884

4985
@EventListener
@@ -66,13 +102,14 @@ public void processAddressUtxoEvent(AddressUtxoEvent addressUtxoEvent) {
66102
addressTxAmountList.addAll(txAddressTxAmountEntities);
67103
}
68104

69-
if (addressTxAmountList.size() > 100) {
70-
addressTxAmountStorage.save(addressTxAmountList); //Save
71-
return;
72-
}
105+
// if (addressTxAmountList.size() > 100) {
106+
// addressTxAmountStorage.save(addressTxAmountList); //Save
107+
// return;
108+
// }
73109

74110
if (addressTxAmountList.size() > 0) {
75-
addressTxAmountListCache.addAll(addressTxAmountList);
111+
//TODO -- addressTxAmountListCache.addAll(addressTxAmountList);
112+
save(addressUtxoEvent.getEventMetadata().getBlock(), addressTxAmountList);
76113
}
77114
}
78115

@@ -193,17 +230,25 @@ public void handleRemainingTxInputOuputs(ReadyForBalanceAggregationEvent readyFo
193230
addressTxAmountListCache.addAll(addressTxAmountList);
194231
}
195232

233+
var remainingAddressAmtList = addressTxAmountListCacheMap.values().stream()
234+
.flatMap(List::stream)
235+
.toList();
236+
if (remainingAddressAmtList.size() > 0) {
237+
addressTxAmountListCache.addAll(remainingAddressAmtList);
238+
}
239+
196240
long t1 = System.currentTimeMillis();
197241
if (addressTxAmountListCache.size() > 0) {
198242
addressTxAmountStorage.save(addressTxAmountListCache);
199-
log.info("Total {} address_tx_amounts records saved", addressTxAmountListCache.size());
200243
}
244+
201245
long t2 = System.currentTimeMillis();
202-
log.info("Time taken to save address_tx_amounts records : " + (t2 - t1) + " ms");
246+
log.info("Time taken to save address_tx_amounts records : {}, time: {} ms", addressTxAmountListCache.size(), (t2 - t1));
203247

204248
} finally {
205249
txInputOutputListCache.clear();
206250
addressTxAmountListCache.clear();
251+
clearCache();
207252
}
208253
}
209254

0 commit comments

Comments
 (0)