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

main.app.cash.backfila.client.jooq.internal.JooqBackend.kt Maven / Gradle / Ivy

Go to download

Backfila is a service that manages backfill state, calling into other services to do batched work.

There is a newer version: 2024.10.28.205607-fab304f
Show newest version
package app.cash.backfila.client.jooq.internal

import app.cash.backfila.client.DeleteBy
import app.cash.backfila.client.Description
import app.cash.backfila.client.jooq.ForJooqBackend
import app.cash.backfila.client.jooq.JooqBackfill
import app.cash.backfila.client.parseDeleteByDate
import app.cash.backfila.client.spi.BackfilaParametersOperator
import app.cash.backfila.client.spi.BackfillBackend
import app.cash.backfila.client.spi.BackfillRegistration
import com.google.inject.Inject
import com.google.inject.Injector
import com.google.inject.Singleton
import com.google.inject.TypeLiteral
import com.squareup.moshi.Types
import java.lang.reflect.ParameterizedType
import kotlin.reflect.KClass
import kotlin.reflect.full.findAnnotation

@Singleton
class JooqBackend @Inject constructor(
  private val injector: Injector,
  @ForJooqBackend private val backfills: MutableMap>>,
) : BackfillBackend {

  override fun create(backfillName: String): JooqBackfillOperator<*, out Any>? {
    return getBackfill(backfillName)?.let {
      @Suppress("UNCHECKED_CAST")
      createJooqOperator(it as JooqBackfill)
    }
  }

  override fun backfills(): Set {
    return backfills.map {
      BackfillRegistration(
        name = it.key,
        description = it.value.findAnnotation()?.text,
        parametersClass = parametersClass(it.value as KClass>),
        deleteBy = it.value.findAnnotation()?.parseDeleteByDate(),
      )
    }.toSet()
  }

  private fun  createJooqOperator(
    backfill: JooqBackfill,
  ) = JooqBackfillOperator(
    backfill,
    BackfilaParametersOperator(parametersClass(backfill::class)),
  )

  /** Creates Backfill instances. Each backfill ID gets a new Backfill instance. */
  private fun getBackfill(name: String): JooqBackfill<*, *>? {
    val backfillClass = backfills[name]
    return if (backfillClass != null) {
      injector.getInstance(backfillClass.java)
    } else {
      null
    }
  }

  private fun  parametersClass(backfillClass: KClass>): KClass {
    // Like MyBackfill.
    val thisType = TypeLiteral.get(backfillClass.java)

    // Like Backfill.
    val supertype = thisType.getSupertype(JooqBackfill::class.java).type as ParameterizedType

    // Like MyDataClass
    return (Types.getRawType(supertype.actualTypeArguments[1]) as Class).kotlin
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy