Skip to content

Commit

Permalink
chore: add LatestAddressBalance materialized view
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-DucPhung committed May 7, 2024
1 parent 20a2945 commit 907ae57
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.cardanofoundation.job.repository.ledgersync;

import jakarta.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import org.cardanofoundation.explorer.common.entity.compositeKey.AddressBalanceId;
import org.cardanofoundation.explorer.common.entity.ledgersync.LatestAddressBalance;

public interface LatestAddressBalanceRepository
extends JpaRepository<LatestAddressBalance, AddressBalanceId> {

@Modifying(clearAutomatically = true)
@Transactional
@Query(value = "REFRESH MATERIALIZED VIEW latest_address_balance", nativeQuery = true)
void refreshMaterializedView();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.stereotype.Component;

import org.cardanofoundation.job.repository.ledgersync.AddressTxCountRepository;
import org.cardanofoundation.job.repository.ledgersync.LatestAddressBalanceRepository;
import org.cardanofoundation.job.repository.ledgersync.LatestTokenBalanceRepository;
import org.cardanofoundation.job.repository.ledgersync.aggregate.AggregateAddressTokenRepository;
import org.cardanofoundation.job.repository.ledgersync.aggregate.AggregateAddressTxBalanceRepository;
Expand All @@ -20,6 +21,7 @@ public class AggregateAnalyticSchedule {
private final AggregateAddressTokenRepository aggregateAddressTokenRepository;
private final AggregateAddressTxBalanceRepository aggregateAddressTxBalanceRepository;
private final LatestTokenBalanceRepository latestTokenBalanceRepository;
private final LatestAddressBalanceRepository latestAddressBalanceRepository;
private final AddressTxCountRepository addressTxCountRepository;
private final TxChartService txChartService;

Expand Down Expand Up @@ -60,6 +62,16 @@ public void refreshLatestTokenBalance() {
System.currentTimeMillis() - currentTime);
}

@Scheduled(fixedDelay = 1000 * 60 * 5) // 5 minutes
public void refreshLatestAddressBalance() {
long currentTime = System.currentTimeMillis();
log.info("Start job refreshLatestTokenBalance");
latestAddressBalanceRepository.refreshMaterializedView();
log.info(
"End Job refreshLatestTokenBalance, Time taken {}ms",
System.currentTimeMillis() - currentTime);
}

@Scheduled(fixedDelay = 1000 * 60 * 15) // 15 minutes
public void updateTxCountTable() {
log.info("Start job to update tx count for address");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,17 @@ WHERE ab.quantity > 0
CREATE INDEX IF NOT EXISTS latest_token_balance_address_idx ON latest_token_balance (address);
CREATE INDEX IF NOT EXISTS latest_token_balance_unit_idx ON latest_token_balance (unit);
CREATE INDEX IF NOT EXISTS latest_token_balance_slot_idx ON latest_token_balance (slot);
CREATE INDEX IF NOT EXISTS latest_token_balance_quantity_idx ON latest_token_balance (quantity);
CREATE INDEX IF NOT EXISTS latest_token_balance_quantity_idx ON latest_token_balance (quantity);


-- latest address balance
CREATE MATERIALIZED VIEW IF NOT EXISTS latest_address_balance AS
SELECT ab.address AS address, ab.slot as slot, ab.unit AS unit, ab.quantity as quantity
from address_balance ab
where ab.unit = 'lovelace'
and not exists(select 1 from address_balance ab2 where ab2.address = ab.address and ab2.slot > ab.slot and ab2.unit = 'lovelace');

CREATE INDEX IF NOT EXISTS latest_address_balance_address_idx ON latest_address_balance (address);
CREATE INDEX IF NOT EXISTS latest_address_balance_unit_idx ON latest_address_balance (unit);
CREATE INDEX IF NOT EXISTS latest_address_balance_slot_idx ON latest_address_balance (slot);
CREATE INDEX IF NOT EXISTS latest_address_balance_quantity_idx ON latest_address_balance (quantity);

0 comments on commit 907ae57

Please sign in to comment.