-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the static const client and add a parameters version. (#393)
- Loading branch information
1 parent
67121fe
commit 4416d6c
Showing
15 changed files
with
380 additions
and
116 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
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
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
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
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
53 changes: 4 additions & 49 deletions
53
client-static/src/main/kotlin/app/cash/backfila/client/stat/StaticDatasourceBackfill.kt
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,57 +1,12 @@ | ||
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<I : Any, P : Any> : Backfill { | ||
val itemType: KClass<I> | ||
|
||
/* | ||
* 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<MyItem, Parameters>. | ||
val supertype = thisType.getSupertype( | ||
StaticDatasourceBackfill::class.java, | ||
).type as ParameterizedType | ||
|
||
// Like MyItem. | ||
@Suppress("UNCHECKED_CAST") | ||
itemType = (Types.getRawType(supertype.actualTypeArguments[0]) as Class<I>).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<P>) {} | ||
|
||
abstract class StaticDatasourceBackfill<I : Any, P : Any> : StaticDatasourceBackfillBase<I, P>() { | ||
/** | ||
* 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<P>) { | ||
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<P>) { | ||
} | ||
|
||
/** | ||
* This invokes the static list of items that the backfill will iterate over. | ||
* This provides the static list of items that the backfill will iterate over. | ||
*/ | ||
abstract val staticDatasource: List<I> | ||
|
||
override fun getStaticDatasource(config: PrepareBackfillConfig<P>) = staticDatasource | ||
} |
54 changes: 54 additions & 0 deletions
54
client-static/src/main/kotlin/app/cash/backfila/client/stat/StaticDatasourceBackfillBase.kt
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,54 @@ | ||
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 StaticDatasourceBackfillBase<I : Any, P : Any> : Backfill { | ||
val itemType: KClass<I> | ||
|
||
/* | ||
* 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 MyBackfill<MyItem, Parameters>. | ||
val supertype = thisType.getSupertype( | ||
StaticDatasourceBackfillBase::class.java, | ||
).type as ParameterizedType | ||
|
||
// Like MyItem. | ||
@Suppress("UNCHECKED_CAST") | ||
itemType = (Types.getRawType(supertype.actualTypeArguments[0]) as Class<I>).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<P>) {} | ||
|
||
/** | ||
* 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<P>) { | ||
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<P>) { | ||
} | ||
|
||
abstract fun getStaticDatasource(config: PrepareBackfillConfig<P>): List<@JvmSuppressWildcards I> | ||
} |
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
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
Oops, something went wrong.