-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore:compare data of ada_pots table
- Loading branch information
1 parent
3c13fb5
commit b659b11
Showing
14 changed files
with
457 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 51 additions & 51 deletions
102
...ava/org/cardanofoundation/ledgersync/verifier/data/app/config/DbSyncDatasourceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,51 @@ | ||
//package org.cardanofoundation.ledgersync.verifier.data.app.config; | ||
// | ||
//import lombok.RequiredArgsConstructor; | ||
//import org.springframework.beans.factory.annotation.Qualifier; | ||
//import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | ||
//import org.springframework.context.annotation.Bean; | ||
//import org.springframework.context.annotation.Configuration; | ||
//import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
//import org.springframework.orm.jpa.JpaTransactionManager; | ||
//import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
//import org.springframework.transaction.PlatformTransactionManager; | ||
//import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
// | ||
//import javax.sql.DataSource; | ||
//import java.util.Objects; | ||
// | ||
//@Configuration | ||
//@EnableTransactionManagement | ||
//@EnableJpaRepositories( | ||
// entityManagerFactoryRef = "dbSyncEntityManagerFactory", | ||
// transactionManagerRef = "dbSyncTransactionManager", | ||
// basePackages = {"org.cardanofoundation.ledgersync.verifier.data.app.repository.dbsync"}) | ||
//@RequiredArgsConstructor | ||
//public class DbSyncDatasourceConfig { | ||
// | ||
// private final MultiDataSourceProperties multiDataSourceProperties; | ||
// | ||
// @Bean(name = "dbSyncDataSource") | ||
// public DataSource ledgerSyncDataSource() { | ||
// return multiDataSourceProperties.buildDataSource( | ||
// multiDataSourceProperties.getDatasourceDbSync()); | ||
// } | ||
// | ||
// @Bean(name = "dbSyncEntityManagerFactory") | ||
// public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean( | ||
// EntityManagerFactoryBuilder builder, @Qualifier("dbSyncDataSource") DataSource dataSource) { | ||
// return builder | ||
// .dataSource(dataSource) | ||
// .packages( | ||
// "org.cardanofoundation.ledgersync.verifier.data.app.entity.dbsync") | ||
// .build(); | ||
// } | ||
// | ||
// @Bean(name = "dbSyncTransactionManager") | ||
// public PlatformTransactionManager dbSyncTransactionManager( | ||
// @Qualifier("dbSyncEntityManagerFactory") | ||
// LocalContainerEntityManagerFactoryBean ledgerSyncEntityManagerFactory) { | ||
// return new JpaTransactionManager( | ||
// Objects.requireNonNull(ledgerSyncEntityManagerFactory.getObject())); | ||
// } | ||
//} | ||
package org.cardanofoundation.ledgersync.verifier.data.app.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Objects; | ||
|
||
@Configuration | ||
@EnableTransactionManagement | ||
@EnableJpaRepositories( | ||
entityManagerFactoryRef = "dbSyncEntityManagerFactory", | ||
transactionManagerRef = "dbSyncTransactionManager", | ||
basePackages = {"org.cardanofoundation.ledgersync.verifier.data.app.repository.dbsync"}) | ||
@RequiredArgsConstructor | ||
public class DbSyncDatasourceConfig { | ||
|
||
private final MultiDataSourceProperties multiDataSourceProperties; | ||
|
||
@Bean(name = "dbSyncDataSource") | ||
public DataSource ledgerSyncDataSource() { | ||
return multiDataSourceProperties.buildDataSource( | ||
multiDataSourceProperties.getDatasourceDbSync()); | ||
} | ||
|
||
@Bean(name = "dbSyncEntityManagerFactory") | ||
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean( | ||
EntityManagerFactoryBuilder builder, @Qualifier("dbSyncDataSource") DataSource dataSource) { | ||
return builder | ||
.dataSource(dataSource) | ||
.packages( | ||
"org.cardanofoundation.ledgersync.verifier.data.app.entity.dbsync") | ||
.build(); | ||
} | ||
|
||
@Bean(name = "dbSyncTransactionManager") | ||
public PlatformTransactionManager dbSyncTransactionManager( | ||
@Qualifier("dbSyncEntityManagerFactory") | ||
LocalContainerEntityManagerFactoryBean ledgerSyncEntityManagerFactory) { | ||
return new JpaTransactionManager( | ||
Objects.requireNonNull(ledgerSyncEntityManagerFactory.getObject())); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...in/java/org/cardanofoundation/ledgersync/verifier/data/app/config/JdbcTemplateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.config; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class JdbcTemplateConfig { | ||
@Bean(name = "ledgerSyncJdbcTemplate") | ||
public JdbcTemplate ledgerSyncJdbcTemplate( | ||
@Qualifier("ledgerSyncDataSource") DataSource dataSource) { | ||
return new JdbcTemplate(dataSource); | ||
} | ||
|
||
@Bean(name = "dbSyncJdbcTemplate") | ||
public JdbcTemplate dbSyncJdbcTemplate(@Qualifier("dbSyncDataSource") DataSource dataSource) { | ||
return new JdbcTemplate(dataSource); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...va/org/cardanofoundation/ledgersync/verifier/data/app/mapper/AdaPotsComparisonMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.mapper; | ||
|
||
import java.sql.SQLException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import org.cardanofoundation.ledgersync.verifier.data.app.model.AdaPotsComparison; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.model.AdaPotsComparisonKey; | ||
|
||
@Component | ||
public interface AdaPotsComparisonMapper<T> { | ||
/** | ||
* Builds a map of Ada pots comparisons. | ||
* | ||
* @param source The source data list. | ||
* @return A map of Ada pots comparisons. | ||
*/ | ||
Map<AdaPotsComparisonKey, AdaPotsComparison> buildMap(List<T> source) throws SQLException; | ||
} |
35 changes: 35 additions & 0 deletions
35
...rdanofoundation/ledgersync/verifier/data/app/mapper/impl/AdaPotsComparisonMapperImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.mapper.impl; | ||
|
||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.cardanofoundation.ledgersync.verifier.data.app.mapper.AdaPotsComparisonMapper; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.model.AdaPotsComparison; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.model.AdaPotsComparisonKey; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.projection.AdaPotsProjection; | ||
|
||
public class AdaPotsComparisonMapperImpl implements AdaPotsComparisonMapper<AdaPotsProjection> { | ||
@Override | ||
public Map<AdaPotsComparisonKey, AdaPotsComparison> buildMap(List<AdaPotsProjection> source) | ||
throws SQLException { | ||
Map<AdaPotsComparisonKey, AdaPotsComparison> map = new HashMap<>(); | ||
for (AdaPotsProjection adaPotsProjection : source) { | ||
AdaPotsComparisonKey key = | ||
AdaPotsComparisonKey.builder() | ||
.slotNo(adaPotsProjection.getSlotNo()) | ||
.epochNo(adaPotsProjection.getEpochNo()) | ||
.build(); | ||
AdaPotsComparison value = | ||
AdaPotsComparison.builder() | ||
.adaPotsComparisonKey(key) | ||
.treasury(adaPotsProjection.getTreasury()) | ||
.reserves(adaPotsProjection.getReserves()) | ||
.rewards(adaPotsProjection.getRewards()) | ||
.build(); | ||
map.put(key, value); | ||
} | ||
return map; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...main/java/org/cardanofoundation/ledgersync/verifier/data/app/model/AdaPotsComparison.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.model; | ||
|
||
import java.math.BigInteger; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.experimental.FieldDefaults; | ||
|
||
@Builder | ||
@Setter | ||
@Getter | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
public class AdaPotsComparison { | ||
AdaPotsComparisonKey adaPotsComparisonKey; | ||
BigInteger treasury; | ||
BigInteger reserves; | ||
BigInteger rewards; | ||
} |
31 changes: 31 additions & 0 deletions
31
...n/java/org/cardanofoundation/ledgersync/verifier/data/app/model/AdaPotsComparisonKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.model; | ||
|
||
import java.util.Objects; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.experimental.FieldDefaults; | ||
|
||
@Builder | ||
@Setter | ||
@Getter | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
public class AdaPotsComparisonKey { | ||
Long slotNo; | ||
Long epochNo; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
AdaPotsComparisonKey that = (AdaPotsComparisonKey) o; | ||
return slotNo.equals(that.slotNo) && epochNo.equals(that.epochNo); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(slotNo, epochNo); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...java/org/cardanofoundation/ledgersync/verifier/data/app/projection/AdaPotsProjection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.projection; | ||
|
||
import java.math.BigInteger; | ||
import java.sql.SQLException; | ||
|
||
public interface AdaPotsProjection { | ||
Long getSlotNo() throws SQLException; | ||
|
||
Long getEpochNo() throws SQLException; | ||
|
||
BigInteger getTreasury() throws SQLException; | ||
|
||
BigInteger getReserves() throws SQLException; | ||
|
||
BigInteger getRewards() throws SQLException; | ||
} |
47 changes: 47 additions & 0 deletions
47
...cardanofoundation/ledgersync/verifier/data/app/projection/AdaPotsProjectionRowMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.projection; | ||
|
||
import java.math.BigInteger; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
import org.springframework.jdbc.core.RowMapper; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class AdaPotsProjectionRowMapper implements RowMapper<AdaPotsProjection> { | ||
@Override | ||
public AdaPotsProjection mapRow(@NotNull ResultSet rs, int rowNum) throws SQLException { | ||
Long slotNo = rs.getLong("slot_no"); | ||
Long epochNo = rs.getLong("epoch_no"); | ||
BigInteger treasury = rs.getBigDecimal("treasury").toBigInteger(); | ||
BigInteger reserves = rs.getBigDecimal("reserves").toBigInteger(); | ||
BigInteger rewards = rs.getBigDecimal("rewards").toBigInteger(); | ||
|
||
return new AdaPotsProjection() { | ||
@Override | ||
public Long getSlotNo() { | ||
return slotNo; | ||
} | ||
|
||
@Override | ||
public Long getEpochNo() { | ||
return epochNo; | ||
} | ||
|
||
@Override | ||
public BigInteger getTreasury() { | ||
return treasury; | ||
} | ||
|
||
@Override | ||
public BigInteger getReserves() { | ||
return reserves; | ||
} | ||
|
||
@Override | ||
public BigInteger getRewards() { | ||
return rewards; | ||
} | ||
}; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../main/java/org/cardanofoundation/ledgersync/verifier/data/app/service/AdaPotsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.service; | ||
|
||
import java.sql.SQLException; | ||
import java.util.*; | ||
|
||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
|
||
import org.cardanofoundation.ledgersync.verifier.data.app.mapper.impl.AdaPotsComparisonMapperImpl; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.model.AdaPotsComparison; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.model.AdaPotsComparisonKey; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.projection.AdaPotsProjection; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.projection.AdaPotsProjectionRowMapper; | ||
|
||
public abstract class AdaPotsService { | ||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public AdaPotsService(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public List<Long> getRandomEpochNos(int maxAdaPots) { | ||
List<Long> epochNos = | ||
jdbcTemplate.query( | ||
"SELECT epoch_no FROM ada_pots ORDER BY RANDOM() LIMIT ?", | ||
new Object[] {maxAdaPots}, | ||
(rs, rowNum) -> rs.getLong("epoch_no")); | ||
return epochNos; | ||
} | ||
|
||
public List<AdaPotsProjection> getAdaPotsByEpochNos(List<Long> epochNos) { | ||
Map<String, Object> params = Collections.singletonMap("epochNos", epochNos); | ||
AdaPotsProjectionRowMapper rowMapper = new AdaPotsProjectionRowMapper(); | ||
return new NamedParameterJdbcTemplate(jdbcTemplate) | ||
.query( | ||
"SELECT slot_no, epoch_no, treasury, reserves, rewards FROM ada_pots WHERE epoch_no IN (:epochNos)", | ||
params, | ||
(rs, rowNum) -> rowMapper.mapRow(rs, rowNum)); | ||
} | ||
|
||
public Map<AdaPotsComparisonKey, AdaPotsComparison> getMapAdaPotsFromEpochNos(List<Long> epochNos) | ||
throws SQLException { | ||
List<AdaPotsProjection> adaPotsProjections = getAdaPotsByEpochNos(epochNos); | ||
return new AdaPotsComparisonMapperImpl().buildMap(adaPotsProjections); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...cardanofoundation/ledgersync/verifier/data/app/service/impl/AdaPotsDbSyncServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.service.impl; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
import org.cardanofoundation.ledgersync.verifier.data.app.service.AdaPotsService; | ||
|
||
@Service | ||
public class AdaPotsDbSyncServiceImpl extends AdaPotsService { | ||
|
||
public AdaPotsDbSyncServiceImpl( | ||
@Qualifier("dbSyncJdbcTemplate") JdbcTemplate dbSyncJdbcTemplate) { | ||
super(dbSyncJdbcTemplate); | ||
} | ||
} |
Oops, something went wrong.