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

main.app.cash.backfila.client.stat.StaticDatasourceBackfill.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.stat

import app.cash.backfila.client.Backfill
import app.cash.backfila.client.BackfillConfig
import app.cash.backfila.client.PrepareBackfillConfig
import com.google.inject.TypeLiteral
import com.squareup.moshi.Types
import java.lang.reflect.ParameterizedType
import kotlin.reflect.KClass

abstract class StaticDatasourceBackfill : Backfill {
  val itemType: KClass

  /*
   * Extract the type parameters from the subtype's generic declaration. This uses Guice magic to
   * read the ("I") type parameter.
   */
  init {
    // Like MyBackfill.
    val thisType = TypeLiteral.get(this::class.java)

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

    // Like MyItem.
    @Suppress("UNCHECKED_CAST")
    itemType = (Types.getRawType(supertype.actualTypeArguments[0]) as Class).kotlin
  }

  /**
   * Override this and throw an exception to prevent the backfill from being created.
   * This is also a good place to do any prep work before batches are run.
   */
  open fun validate(config: PrepareBackfillConfig

) {} /** * Called for each batch of matching records. * Override in a backfill to process all records in a batch. */ open fun runBatch(items: List<@JvmSuppressWildcards I>, config: BackfillConfig

) { items.forEach { runOne(it, config) } } /** * Called for each matching record. * Override in a backfill to process one record at a time. */ open fun runOne(item: I, config: BackfillConfig

) { } /** * This invokes the static list of items that the backfill will iterate over. */ abstract val staticDatasource: List }