All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.infobip.kafkistry.sql.RollingSQLRepository.kt Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
package com.infobip.kafkistry.sql

class RollingSQLRepository(
    private val primary: SQLRepository,
    private val secondary: SQLRepository,
) : SQLRepository {

    private enum class ActiveRepository {
        PRIMARY, SECONDARY
    }

    @Volatile
    private var active: ActiveRepository = ActiveRepository.PRIMARY

    private fun current(): SQLRepository = when (active) {
        ActiveRepository.PRIMARY -> primary
        ActiveRepository.SECONDARY -> secondary
    }

    private fun next(): SQLRepository = when (active) {
        ActiveRepository.PRIMARY -> secondary
        ActiveRepository.SECONDARY -> primary
    }

    private fun swap(){
        active = when (active) {
            ActiveRepository.PRIMARY -> ActiveRepository.SECONDARY
            ActiveRepository.SECONDARY -> ActiveRepository.PRIMARY
        }
    }

    override val tableColumns: List = primary.tableColumns
    override val queryExamples: List = primary.queryExamples

    override fun updateAllLists(objectLists: List>) {
        next().updateAllLists(objectLists)
        swap()
    }

    override fun query(sql: String): QueryResult = current().query(sql)

    override fun close() {
        primary.close()
        secondary.close()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy