Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.bitclave.node.configuration

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import javax.sql.DataSource
import java.util.HashMap
import com.zaxxer.hikari.HikariDataSource
import com.zaxxer.hikari.HikariConfig
import com.bitclave.node.routingdatasource.RoutingDataSource
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.env.Environment

/**
* Replication DataSources Configuration
*
* `@Primary` and `@DependsOn` are the key requirements for Spring Boot.
*/
@Configuration
class DataSourceConfig {
private val PRIMARY_DATASOURCE_PREFIX = "spring.primary.datasource"
private val REPLICA_DATASOURCE_PREFIX = "spring.replica.datasource"

@Autowired
private val environment: Environment? = null

@Bean
@Primary
fun dataSource(): DataSource {
val routingDataSource = RoutingDataSource()

val primaryDataSource = buildDataSource("PrimaryHikariPool", PRIMARY_DATASOURCE_PREFIX)
val replicaDataSource = buildDataSource("ReplicaHikariPool", REPLICA_DATASOURCE_PREFIX)

val targetDataSources = HashMap<Any, Any>()
targetDataSources[RoutingDataSource.Route.PRIMARY] = primaryDataSource
targetDataSources[RoutingDataSource.Route.REPLICA] = replicaDataSource

routingDataSource.setTargetDataSources(targetDataSources)
routingDataSource.setDefaultTargetDataSource(primaryDataSource)

return routingDataSource
}

private fun buildDataSource(poolName: String, dataSourcePrefix: String): DataSource {
val hikariConfig = HikariConfig()

hikariConfig.poolName = poolName
hikariConfig.jdbcUrl = environment!!.getProperty(String.format("%s.url", dataSourcePrefix))
hikariConfig.username = environment.getProperty(String.format("%s.username", dataSourcePrefix))
hikariConfig.password = environment.getProperty(String.format("%s.password", dataSourcePrefix))
hikariConfig.driverClassName = environment.getProperty(String.format("%s.driver", dataSourcePrefix))

return HikariDataSource(hikariConfig)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ import java.util.Date
@Transactional
interface AccountCrudRepository : CrudRepository<Account, String> {

@Transactional(readOnly = true)
fun findAllBy(pageable: Pageable): Slice<Account>

@Transactional(readOnly = true)
fun findByPublicKey(key: String): Account?

@Transactional(readOnly = true)
fun findAllByPublicKeyIn(key: List<String>): List<Account>

fun deleteByPublicKey(key: String)

@Transactional(readOnly = true)
fun findByCreatedAtAfter(createdAt: Date): List<Account>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Slice
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import java.util.Date

@Component
Expand Down Expand Up @@ -38,6 +39,7 @@ class PostgresAccountRepositoryImpl(val repository: AccountCrudRepository) : Acc
.toList()
}

@Transactional(readOnly = true)
override fun getTotalCount(): Long {
return repository.count()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.bitclave.node.services.errors.DataNotSavedException
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
@Qualifier("postgres")
Expand All @@ -16,6 +17,7 @@ class PostgresClientDataRepositoryImpl(
return emptyList()
}

@Transactional(readOnly = true)
override fun getData(publicKey: String): Map<String, String> {
return repository.findByIdOrNull(publicKey)?.data ?: emptyMap()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import org.springframework.transaction.annotation.Transactional
@Transactional
interface FileCrudRepository : CrudRepository<UploadedFile, String> {

@Transactional(readOnly = true)
fun findByPublicKey(publicKey: String): List<UploadedFile>

@Transactional(readOnly = true)
fun findById(id: Long): UploadedFile?

fun deleteByIdAndPublicKey(id: Long, publicKey: String): Long

fun deleteByPublicKey(publicKey: String): Long

@Transactional(readOnly = true)
fun findByIdAndPublicKey(id: Long, publicKey: String): UploadedFile?
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,31 @@ import java.math.BigInteger
@Transactional
interface OfferCrudRepository : PagingAndSortingRepository<Offer, Long> {

@Transactional(readOnly = true)
fun findAllByIdIn(ids: List<Long>, pageable: Pageable): Page<Offer>

@Transactional(readOnly = true)
@Query("SELECT id FROM Offer WHERE owner = :owner", nativeQuery = true)
fun findIdsByOwner(@Param("owner") owner: String): List<BigInteger>

@Transactional(readOnly = true)
fun findByOwner(owner: String): List<Offer>

@Transactional(readOnly = true)
fun findByOwner(owner: String, pageable: Pageable): Page<Offer>

fun deleteByIdAndOwner(id: Long, owner: String): Long

fun deleteByOwner(owner: String): List<Offer>

@Transactional(readOnly = true)
fun findByIdAndOwner(id: Long, owner: String): Offer?

@Transactional(readOnly = true)
@Query("FROM Offer o JOIN o.tags t WHERE o.owner = :owner and KEY(t) = :tagKey")
fun getOfferByOwnerAndTag(@Param("owner") owner: String, @Param("tagKey") tagKey: String): List<Offer>

@Transactional(readOnly = true)
@Query(
value = """
select * from offer o
Expand All @@ -48,6 +55,7 @@ interface OfferCrudRepository : PagingAndSortingRepository<Offer, Long> {
)
fun getAllOffersExceptProducts(@Param("pageable") pageable: Pageable): Page<Offer>

@Transactional(readOnly = true)
@Query(
value = """
select * from offer o
Expand All @@ -58,8 +66,10 @@ interface OfferCrudRepository : PagingAndSortingRepository<Offer, Long> {
)
fun getAllOffersExceptProductsSlice(pageable: Pageable): Slice<Offer>

@Transactional(readOnly = true)
fun getAllOffersBy(pageable: Pageable): Slice<Offer>

@Transactional(readOnly = true)
@Query(
value = """
SELECT o.* FROM offer o
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.springframework.data.domain.Slice
import org.springframework.data.domain.SliceImpl
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import java.math.BigInteger
import java.util.HashMap
import javax.persistence.EntityManager
Expand Down Expand Up @@ -94,6 +95,7 @@ class PostgresOfferRepositoryImpl(
return syncElementCollections(repository.findAllByIdIn(ids, pageable))
}

@Transactional(readOnly = true)
override fun findByIds(ids: List<Long>): List<Offer> {
return syncElementCollections(repository.findAllById(ids).toList())
}
Expand All @@ -102,10 +104,12 @@ class PostgresOfferRepositoryImpl(
return syncElementCollections(repository.findByIdAndOwner(id, owner))
}

@Transactional(readOnly = true)
override fun findAll(): List<Offer> {
return syncElementCollections(repository.findAll().toList())
}

@Transactional(readOnly = true)
override fun findAll(pageable: Pageable): Page<Offer> {
var result: Page<Offer>? = null
val step1 = measureTimeMillis {
Expand All @@ -117,6 +121,7 @@ class PostgresOfferRepositoryImpl(
return syncElementCollections(result!!)
}

@Transactional(readOnly = true)
override fun getTotalCount(): Long {
return repository.count()
}
Expand Down Expand Up @@ -177,6 +182,7 @@ class PostgresOfferRepositoryImpl(
return SliceImpl(result, pageable, slice.hasNext())
}

@Transactional(readOnly = true)
private fun syncElementCollections(
offers: List<Offer>,
syncCompare: Boolean = true,
Expand Down Expand Up @@ -299,6 +305,7 @@ class PostgresOfferRepositoryImpl(
return result
}

@Transactional(readOnly = true)
private fun syncPriceRules(offerPriceIds: List<Long>): Map<Long, List<Array<Any>>> {
val ids = offerPriceIds.joinToString(",")
var result = mapOf<Long, List<Array<Any>>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package com.bitclave.node.repository.price
import com.bitclave.node.repository.entities.OfferPrice
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
import javax.transaction.Transactional
import org.springframework.transaction.annotation.Transactional

@Repository
@Transactional
interface OfferPriceCrudRepository : CrudRepository<OfferPrice, Long> {

@Transactional(readOnly = true)
fun findByOfferId(id: Long): List<OfferPrice>

fun deleteAllByOfferIdIn(ids: List<Long>): List<OfferPrice>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package com.bitclave.node.repository.priceRule
import com.bitclave.node.repository.entities.OfferPriceRules
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
import javax.transaction.Transactional
import org.springframework.transaction.annotation.Transactional

@Repository
@Transactional
interface OfferPriceRulesCrudRepository : CrudRepository<OfferPriceRules, Long> {

@Transactional(readOnly = true)
fun findByOfferPriceId(offerPriceId: Long): List<OfferPriceRules>
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import org.springframework.transaction.annotation.Transactional
@Transactional
interface OfferRankCrudRepository : PagingAndSortingRepository<OfferRank, Long> {

@Transactional(readOnly = true)
fun findByOfferId(offerId: Long): List<OfferRank>

@Transactional(readOnly = true)
fun findByOfferIdAndRankerId(offerId: Long, rankerId: String): OfferRank?

@Modifying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.bitclave.node.repository.entities.OfferRank
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
@Qualifier("postgres")
Expand All @@ -23,6 +24,7 @@ class PostgresOfferRankRepositoryImpl(
return repository.save(rankOffer)
}

@Transactional(readOnly = true)
override fun findById(id: Long): OfferRank? {
return repository.findByIdOrNull(id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.bitclave.node.services.errors.DataNotSavedException
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
@Qualifier("postgres")
Expand Down Expand Up @@ -49,6 +50,7 @@ class PostgresRequestDataRepositoryImpl(val repository: RequestDataCrudRepositor
repository.getReshareByClientsAndKeysAndRootPk(clientsPk, keys, rootPk)
}

@Transactional(readOnly = true)
override fun findById(id: Long): RequestData? {
return repository.findByIdOrNull(id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ import org.springframework.transaction.annotation.Transactional
@Transactional
interface RequestDataCrudRepository : CrudRepository<RequestData, Long> {

@Transactional(readOnly = true)
fun findByFromPk(from: String): List<RequestData>

@Transactional(readOnly = true)
fun findByToPk(to: String): List<RequestData>

@Transactional(readOnly = true)
fun findByFromPkAndToPk(from: String, to: String): List<RequestData>

@Transactional(readOnly = true)
fun findByFromPkAndToPkAndRequestData(from: String, to: String, requestData: String): RequestData?

@Transactional(readOnly = true)
fun findByRequestDataAndRootPk(requestData: String, rootPk: String): List<RequestData>

@Transactional(readOnly = true)
@Query(
value = """
SELECT * FROM request_data WHERE to_pk = ?1 AND from_pk IN ?2 AND request_data IN ?3
Expand All @@ -28,6 +34,7 @@ interface RequestDataCrudRepository : CrudRepository<RequestData, Long> {
)
fun getByFromAndToAndKeys(to: String, from: List<String>, keys: List<String>): List<RequestData>

@Transactional(readOnly = true)
@Query(
value = """
SELECT * FROM request_data WHERE to_pk = ?1 AND from_pk IN ?2
Expand All @@ -36,6 +43,7 @@ interface RequestDataCrudRepository : CrudRepository<RequestData, Long> {
)
fun getByFromAndTo(to: String, from: List<String>): List<RequestData>

@Transactional(readOnly = true)
@Query(
value = """
SELECT * FROM request_data WHERE (from_pk IN ?1 OR to_pk IN ?1) AND request_data IN ?2 AND root_pk=?3
Expand All @@ -48,6 +56,7 @@ interface RequestDataCrudRepository : CrudRepository<RequestData, Long> {
rootPk: String
): List<RequestData>

@Transactional(readOnly = true)
@Query(
value = """
SELECT * FROM request_data WHERE (from_pk IN ?1 OR to_pk IN ?1) AND root_pk=?2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.springframework.data.domain.Slice
import org.springframework.data.domain.SliceImpl
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import java.math.BigInteger
import java.util.HashMap
import javax.persistence.EntityManager
Expand Down Expand Up @@ -42,10 +43,12 @@ class PostgresSearchRequestRepositoryImpl(
return repository.deleteByIdIn(ids)
}

@Transactional(readOnly = true)
override fun findById(id: Long): SearchRequest? {
return syncElementCollections(repository.findByIdOrNull(id))
}

@Transactional(readOnly = true)
override fun findById(ids: List<Long>): List<SearchRequest> {
return syncElementCollections(repository.findAllById(ids).toList())
}
Expand All @@ -58,10 +61,12 @@ class PostgresSearchRequestRepositoryImpl(
return syncElementCollections(repository.findByIdAndOwner(id, owner))
}

@Transactional(readOnly = true)
override fun findAll(): List<SearchRequest> {
return syncElementCollections(repository.findAll().toList())
}

@Transactional(readOnly = true)
override fun findAll(pageable: Pageable): Page<SearchRequest> {
return syncElementCollections(repository.findAll(pageable))
}
Expand All @@ -73,6 +78,7 @@ class PostgresSearchRequestRepositoryImpl(
override fun findByOwnerInSlice(owners: List<String>, pageable: Pageable): Slice<SearchRequest> =
syncElementCollections(repository.findByOwnerIn(owners, pageable))

@Transactional(readOnly = true)
override fun getTotalCount(): Long {
return repository.count()
}
Expand Down Expand Up @@ -112,6 +118,7 @@ class PostgresSearchRequestRepositoryImpl(
return SliceImpl(result, pageable, slice.hasNext())
}

@Transactional(readOnly = true)
private fun syncElementCollections(searchRequests: List<SearchRequest>): List<SearchRequest> {
val ids = searchRequests.map { it.id }.distinct().joinToString(",")

Expand Down
Loading