Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #506 from cardano-foundation/1.5.0-perf-impr
Browse files Browse the repository at this point in the history
perf: delete all zero holder after sync data to latest_token_balance
  • Loading branch information
Sotatek-DucPhung authored Jul 15, 2024
2 parents 31c3ff7 + 02f791c commit 94bda69
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,26 @@ AND EXISTS (SELECT 1
deletedRows);
return deletedRows;
}

public void deleteAllZeroHolders(long targetBlockTime) {
log.info("Deleting all zero holders from address balance");
long startTime = System.currentTimeMillis();
String deleteQuery =
"""
DELETE
FROM address_balance
WHERE block_time < ?
AND unit != 'lovelace'
AND quantity = 0;
""";

TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
int deletedRows =
transactionTemplate.execute(status -> dsl.execute(deleteQuery, targetBlockTime));
log.info(
"Deleted all zero holders from address balance: {} record deleted in {} ms",
deletedRows,
System.currentTimeMillis() - startTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.cardanofoundation.job.repository.ledgersync.TokenTxCountRepository;
import org.cardanofoundation.job.repository.ledgersyncagg.AddressTxAmountRepository;
import org.cardanofoundation.job.repository.ledgersyncagg.LatestTokenBalanceRepository;
import org.cardanofoundation.job.repository.ledgersyncagg.jooq.JOOQAddressBalanceRepository;
import org.cardanofoundation.job.repository.ledgersyncagg.jooq.JOOQLatestTokenBalanceRepository;

@Slf4j
Expand All @@ -45,6 +46,7 @@ public class LatestTokenBalanceSchedule {
private final TokenTxCountRepository tokenTxCountRepository;
private final MultiAssetRepository multiAssetRepository;
private final JOOQLatestTokenBalanceRepository jooqLatestTokenBalanceRepository;
private final JOOQAddressBalanceRepository jooqAddressBalanceRepository;
private final LatestTokenBalanceRepository latestTokenBalanceRepository;
private final BlockRepository blockRepository;
private final RedisTemplate<String, Integer> redisTemplate;
Expand All @@ -71,7 +73,7 @@ public void syncLatestTokenBalance() {
addressTxAmountRepository.getMaxBlockNoFromCursor(), latestBlock.get().getBlockNo());
final Integer checkpoint = redisTemplate.opsForValue().get(latestTokenBalanceCheckpoint);
if (Objects.isNull(checkpoint) || latestTokenBalanceRepository.count() == 0) {
init();
init(currentMaxBlockNo);
} else if (currentMaxBlockNo > checkpoint.longValue()) {
update(checkpoint.longValue(), currentMaxBlockNo);
}
Expand All @@ -83,10 +85,13 @@ public void syncLatestTokenBalance() {
.set(latestTokenBalanceCheckpoint, Math.max((int) currentMaxBlockNo - 2160, 0));
}

private void init() {
private void init(long currentMaxBlockNo) {
log.info("Start init LatestTokenBalance");
long startTime = System.currentTimeMillis();

Long epochBlockTimeCurrent =
blockRepository.getBlockTimeByBlockNo(currentMaxBlockNo).toInstant().getEpochSecond();

// Drop all indexes before inserting the latest token balance data into the database.
jooqLatestTokenBalanceRepository.dropAllIndexes();
long index = 1;
Expand All @@ -105,6 +110,7 @@ private void init() {
// Create all indexes after inserting the latest token balance data into the database.
jooqLatestTokenBalanceRepository.createAllIndexes();
jooqLatestTokenBalanceRepository.deleteAllZeroHolders();
jooqAddressBalanceRepository.deleteAllZeroHolders(epochBlockTimeCurrent);
log.info("End init LatestTokenBalance in {} ms", System.currentTimeMillis() - startTime);
}

Expand Down Expand Up @@ -143,6 +149,7 @@ private void update(Long blockNoCheckpoint, Long currentMaxBlockNo) {
processingLatestTokenBalance(unitsInBlockRange, epochBlockTimeCheckpoint, true);

jooqLatestTokenBalanceRepository.deleteAllZeroHolders();
jooqAddressBalanceRepository.deleteAllZeroHolders(epochBlockTimeCurrent);

log.info(
"End update LatestTokenBalance with address size = {} in {} ms",
Expand Down

0 comments on commit 94bda69

Please sign in to comment.