Skip to content

Commit 7817bdc

Browse files
committed
Make NormalizedCacheFactory a fun interface
It declares a single method, so implementations can be written as lambdas instead of object expressions. The SQLite factories, which build their cache in create(), read considerably better this way. This is a source and binary breaking change.
1 parent 92ee947 commit 7817bdc

12 files changed

Lines changed: 71 additions & 95 deletions

File tree

normalized-cache-sqlite/src/androidMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.android.kt

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
77
import app.cash.sqldelight.async.coroutines.synchronous
88
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
99
import com.apollographql.apollo.exception.apolloExceptionHandler
10-
import com.apollographql.cache.normalized.api.NormalizedCache
1110
import com.apollographql.cache.normalized.api.NormalizedCacheFactory
1211
import com.apollographql.cache.normalized.sql.internal.record.SqlRecordDatabase
1312

@@ -31,50 +30,48 @@ fun SqlNormalizedCacheFactory(
3130
configure: ((SupportSQLiteDatabase) -> Unit)? = null,
3231
useNoBackupDirectory: Boolean = false,
3332
windowSizeBytes: Long? = 4 * 1024 * 1024,
34-
): NormalizedCacheFactory = object : NormalizedCacheFactory() {
35-
override fun create(): NormalizedCache {
36-
val synchronousSchema = SqlRecordDatabase.Schema.synchronous()
37-
val filePath = when {
38-
name == null -> {
39-
null
40-
}
33+
): NormalizedCacheFactory = NormalizedCacheFactory {
34+
val synchronousSchema = SqlRecordDatabase.Schema.synchronous()
35+
val filePath = when {
36+
name == null -> {
37+
null
38+
}
4139

42-
name.startsWith("/") -> {
43-
// Absolute path: keep as-is
44-
name
45-
}
40+
name.startsWith("/") -> {
41+
// Absolute path: keep as-is
42+
name
43+
}
4644

47-
else -> {
48-
// Old versions of the library used to store the database in the database directory.
49-
// If such file exists, use it, otherwise, use the cache directory.
50-
(context.getDatabasePath(name).takeIf { it.exists() } ?: context.cacheDir.resolve(name)).absolutePath
51-
}
45+
else -> {
46+
// Old versions of the library used to store the database in the database directory.
47+
// If such file exists, use it, otherwise, use the cache directory.
48+
(context.getDatabasePath(name).takeIf { it.exists() } ?: context.cacheDir.resolve(name)).absolutePath
5249
}
53-
val driver = AndroidSqliteDriver(
54-
schema = synchronousSchema,
55-
context = context.applicationContext,
56-
name = filePath,
57-
factory = factory,
58-
callback = object : AndroidSqliteDriver.Callback(synchronousSchema) {
59-
override fun onConfigure(db: SupportSQLiteDatabase) {
60-
super.onConfigure(db)
61-
configure?.invoke(db)
62-
}
50+
}
51+
val driver = AndroidSqliteDriver(
52+
schema = synchronousSchema,
53+
context = context.applicationContext,
54+
name = filePath,
55+
factory = factory,
56+
callback = object : AndroidSqliteDriver.Callback(synchronousSchema) {
57+
override fun onConfigure(db: SupportSQLiteDatabase) {
58+
super.onConfigure(db)
59+
configure?.invoke(db)
60+
}
6361

64-
override fun onCorruption(db: SupportSQLiteDatabase) {
65-
apolloExceptionHandler(Exception("Corruption detected, recreating the database"))
66-
super.onCorruption(db)
67-
}
62+
override fun onCorruption(db: SupportSQLiteDatabase) {
63+
apolloExceptionHandler(Exception("Corruption detected, recreating the database"))
64+
super.onCorruption(db)
65+
}
6866

69-
override fun onDowngrade(db: SupportSQLiteDatabase, oldVersion: Int, newVersion: Int) {
70-
// Treat downgrades as corruption, which results in a clean database
71-
apolloExceptionHandler(Exception("onDowngrade from $oldVersion to $newVersion, treating as database corruption"))
72-
super.onCorruption(db)
73-
}
74-
},
75-
useNoBackupDirectory = useNoBackupDirectory,
76-
windowSizeBytes = windowSizeBytes,
77-
)
78-
return SqlNormalizedCache(driver, filePath)
79-
}
67+
override fun onDowngrade(db: SupportSQLiteDatabase, oldVersion: Int, newVersion: Int) {
68+
// Treat downgrades as corruption, which results in a clean database
69+
apolloExceptionHandler(Exception("onDowngrade from $oldVersion to $newVersion, treating as database corruption"))
70+
super.onCorruption(db)
71+
}
72+
},
73+
useNoBackupDirectory = useNoBackupDirectory,
74+
windowSizeBytes = windowSizeBytes,
75+
)
76+
SqlNormalizedCache(driver, filePath)
8077
}

normalized-cache-sqlite/src/appleMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.apple.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import app.cash.sqldelight.db.SqlDriver
55
import app.cash.sqldelight.driver.native.NativeSqliteDriver
66
import app.cash.sqldelight.driver.native.wrapConnection
77
import co.touchlab.sqliter.DatabaseConfiguration
8-
import com.apollographql.cache.normalized.api.NormalizedCache
98
import com.apollographql.cache.normalized.api.NormalizedCacheFactory
109
import com.apollographql.cache.normalized.sql.internal.record.SqlRecordDatabase
1110

@@ -18,10 +17,8 @@ import com.apollographql.cache.normalized.sql.internal.record.SqlRecordDatabase
1817
fun SqlNormalizedCacheFactory(
1918
name: String?,
2019
baseDir: String?,
21-
): NormalizedCacheFactory = object : NormalizedCacheFactory() {
22-
override fun create(): NormalizedCache {
23-
return SqlNormalizedCache(createDriver(name, baseDir), name)
24-
}
20+
): NormalizedCacheFactory = NormalizedCacheFactory {
21+
SqlNormalizedCache(createDriver(name, baseDir), name)
2522
}
2623

2724
actual fun SqlNormalizedCacheFactory(name: String?): NormalizedCacheFactory = SqlNormalizedCacheFactory(name, null)

normalized-cache-sqlite/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.apollographql.cache.normalized.sql
22

33
import app.cash.sqldelight.db.SqlDriver
4-
import com.apollographql.cache.normalized.api.NormalizedCache
54
import com.apollographql.cache.normalized.api.NormalizedCacheFactory
65

76
/**
@@ -21,8 +20,6 @@ expect fun SqlNormalizedCacheFactory(name: String? = "apollo.db"): NormalizedCac
2120

2221
fun SqlNormalizedCacheFactory(driver: SqlDriver): NormalizedCacheFactory = SqlNormalizedCacheFactory(driver, name = "apollo.db")
2322

24-
fun SqlNormalizedCacheFactory(driver: SqlDriver, name: String?): NormalizedCacheFactory = object : NormalizedCacheFactory() {
25-
override fun create(): NormalizedCache {
26-
return SqlNormalizedCache(driver, name)
27-
}
23+
fun SqlNormalizedCacheFactory(driver: SqlDriver, name: String?): NormalizedCacheFactory = NormalizedCacheFactory {
24+
SqlNormalizedCache(driver, name)
2825
}

normalized-cache-sqlite/src/jsCommonMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.jsCommon.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.apollographql.cache.normalized.sql
22

33
import app.cash.sqldelight.driver.worker.createDefaultWebWorkerDriver
4-
import com.apollographql.cache.normalized.api.NormalizedCache
54
import com.apollographql.cache.normalized.api.NormalizedCacheFactory
65

76
/**
@@ -15,9 +14,7 @@ import com.apollographql.cache.normalized.api.NormalizedCacheFactory
1514
*
1615
* See [the SQLDelight documentation](https://sqldelight.github.io/sqldelight/2.1.0/js_sqlite/sqljs_worker/).
1716
*/
18-
actual fun SqlNormalizedCacheFactory(name: String?): NormalizedCacheFactory = object : NormalizedCacheFactory() {
19-
override fun create(): NormalizedCache {
20-
return SqlNormalizedCache(createDefaultWebWorkerDriver(), name = null)
21-
}
17+
actual fun SqlNormalizedCacheFactory(name: String?): NormalizedCacheFactory = NormalizedCacheFactory {
18+
SqlNormalizedCache(createDefaultWebWorkerDriver(), name = null)
2219
}
2320

normalized-cache-sqlite/src/jvmMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.jvm.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.apollographql.cache.normalized.sql
22

33
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
4-
import com.apollographql.cache.normalized.api.NormalizedCache
54
import com.apollographql.cache.normalized.api.NormalizedCacheFactory
65
import java.io.File
76
import java.util.Properties
@@ -14,10 +13,8 @@ import java.util.Properties
1413
fun SqlNormalizedCacheFactory(
1514
url: String,
1615
properties: Properties,
17-
): NormalizedCacheFactory = object : NormalizedCacheFactory() {
18-
override fun create(): NormalizedCache {
19-
return SqlNormalizedCache(JdbcSqliteDriver(url, properties), name = null)
20-
}
16+
): NormalizedCacheFactory = NormalizedCacheFactory {
17+
SqlNormalizedCache(JdbcSqliteDriver(url, properties), name = null)
2118
}
2219

2320
/**
@@ -29,10 +26,8 @@ fun SqlNormalizedCacheFactory(
2926
fun SqlNormalizedCacheFactory(
3027
name: String?,
3128
baseDir: String?,
32-
): NormalizedCacheFactory = object : NormalizedCacheFactory() {
33-
override fun create(): NormalizedCache {
34-
return SqlNormalizedCache(JdbcSqliteDriver(name.toUrl(baseDir), Properties()), name = name)
35-
}
29+
): NormalizedCacheFactory = NormalizedCacheFactory {
30+
SqlNormalizedCache(JdbcSqliteDriver(name.toUrl(baseDir), Properties()), name = name)
3631
}
3732

3833
actual fun SqlNormalizedCacheFactory(name: String?): NormalizedCacheFactory = SqlNormalizedCacheFactory(name, null)

normalized-cache/api/normalized-cache.api

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,7 @@ public final class com/apollographql/cache/normalized/api/NormalizedCache$Defaul
658658
public static synthetic fun trim$default (Lcom/apollographql/cache/normalized/api/NormalizedCache;JFLkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
659659
}
660660

661-
public abstract class com/apollographql/cache/normalized/api/NormalizedCacheFactory {
662-
public fun <init> ()V
661+
public abstract interface class com/apollographql/cache/normalized/api/NormalizedCacheFactory {
663662
public abstract fun create ()Lcom/apollographql/cache/normalized/api/NormalizedCache;
664663
}
665664

normalized-cache/api/normalized-cache.klib.api

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ final enum class com.apollographql.cache.normalized/FetchPolicy : kotlin/Enum<co
3232
final fun values(): kotlin/Array<com.apollographql.cache.normalized/FetchPolicy> // com.apollographql.cache.normalized/FetchPolicy.values|values#static(){}[0]
3333
}
3434

35+
abstract fun interface com.apollographql.cache.normalized.api/NormalizedCacheFactory { // com.apollographql.cache.normalized.api/NormalizedCacheFactory|null[0]
36+
abstract fun create(): com.apollographql.cache.normalized.api/NormalizedCache // com.apollographql.cache.normalized.api/NormalizedCacheFactory.create|create(){}[0]
37+
}
38+
3539
abstract interface com.apollographql.cache.normalized.api/CacheKeyGenerator { // com.apollographql.cache.normalized.api/CacheKeyGenerator|null[0]
3640
abstract fun cacheKeyForObject(kotlin.collections/Map<kotlin/String, kotlin/Any?>, com.apollographql.cache.normalized.api/CacheKeyGeneratorContext): com.apollographql.cache.normalized.api/CacheKey? // com.apollographql.cache.normalized.api/CacheKeyGenerator.cacheKeyForObject|cacheKeyForObject(kotlin.collections.Map<kotlin.String,kotlin.Any?>;com.apollographql.cache.normalized.api.CacheKeyGeneratorContext){}[0]
3741
}
@@ -168,12 +172,6 @@ abstract class com.apollographql.cache.normalized.api/CacheKeyResolver : com.apo
168172
open fun listOfCacheKeysForField(com.apollographql.cache.normalized.api/ResolverContext): kotlin.collections/List<com.apollographql.cache.normalized.api/CacheKey?>? // com.apollographql.cache.normalized.api/CacheKeyResolver.listOfCacheKeysForField|listOfCacheKeysForField(com.apollographql.cache.normalized.api.ResolverContext){}[0]
169173
}
170174

171-
abstract class com.apollographql.cache.normalized.api/NormalizedCacheFactory { // com.apollographql.cache.normalized.api/NormalizedCacheFactory|null[0]
172-
constructor <init>() // com.apollographql.cache.normalized.api/NormalizedCacheFactory.<init>|<init>(){}[0]
173-
174-
abstract fun create(): com.apollographql.cache.normalized.api/NormalizedCache // com.apollographql.cache.normalized.api/NormalizedCacheFactory.create|create(){}[0]
175-
}
176-
177175
final class com.apollographql.cache.normalized.api/CacheControlCacheResolver : com.apollographql.cache.normalized.api/CacheResolver { // com.apollographql.cache.normalized.api/CacheControlCacheResolver|null[0]
178176
constructor <init>(com.apollographql.cache.normalized.api/MaxAgeProvider, com.apollographql.cache.normalized.api/CacheResolver) // com.apollographql.cache.normalized.api/CacheControlCacheResolver.<init>|<init>(com.apollographql.cache.normalized.api.MaxAgeProvider;com.apollographql.cache.normalized.api.CacheResolver){}[0]
179177
constructor <init>(com.apollographql.cache.normalized.api/MaxAgeProvider, kotlin.collections/Map<kotlin/String, com.apollographql.cache.normalized.api/FieldPolicies>) // com.apollographql.cache.normalized.api/CacheControlCacheResolver.<init>|<init>(com.apollographql.cache.normalized.api.MaxAgeProvider;kotlin.collections.Map<kotlin.String,com.apollographql.cache.normalized.api.FieldPolicies>){}[0]

normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/api/NormalizedCacheFactory.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ package com.apollographql.cache.normalized.api
44
* A Factory used to construct an instance of a [NormalizedCache] configured with the custom scalar adapters set in
55
* ApolloClient.Builder#addCustomScalarAdapter(ScalarType, CustomScalarAdapter).
66
*/
7-
abstract class NormalizedCacheFactory {
7+
fun interface NormalizedCacheFactory {
88

99
/**
1010
* ApolloClient.Builder#addCustomScalarAdapter(ScalarType, CustomScalarAdapter).
1111
* @return An implementation of [NormalizedCache].
1212
*/
13-
abstract fun create(): NormalizedCache
13+
fun create(): NormalizedCache
1414
}

normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/memory/MemoryCache.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class MemoryCache(
180180
class MemoryCacheFactory(
181181
private val maxSizeBytes: Int = Int.MAX_VALUE,
182182
private val expireAfterMillis: Long = -1,
183-
) : NormalizedCacheFactory() {
183+
) : NormalizedCacheFactory {
184184

185185
private var nextCacheFactory: NormalizedCacheFactory? = null
186186

tests/fetch-policy/src/commonTest/kotlin/test/FetchPolicyTest.kt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,16 @@ class FetchPolicyTest {
275275
* This removes flakiness by ensuring that when using `writeToCacheAsynchronously = true`, there is enough time to start observing the
276276
* cache, before writing happens.
277277
*/
278-
private fun AsyncCacheFactory(): NormalizedCacheFactory = object : NormalizedCacheFactory() {
279-
override fun create(): NormalizedCache {
280-
val wrapped = MemoryCacheFactory().create()
281-
return object : NormalizedCache by wrapped {
282-
override suspend fun merge(
283-
records: Collection<Record>,
284-
cacheHeaders: CacheHeaders,
285-
recordMerger: RecordMerger,
286-
): Set<String> {
287-
delay(100.milliseconds)
288-
return wrapped.merge(records, cacheHeaders, recordMerger)
289-
}
278+
private fun AsyncCacheFactory(): NormalizedCacheFactory = NormalizedCacheFactory {
279+
val wrapped = MemoryCacheFactory().create()
280+
object : NormalizedCache by wrapped {
281+
override suspend fun merge(
282+
records: Collection<Record>,
283+
cacheHeaders: CacheHeaders,
284+
recordMerger: RecordMerger,
285+
): Set<String> {
286+
delay(100.milliseconds)
287+
return wrapped.merge(records, cacheHeaders, recordMerger)
290288
}
291289
}
292290
}

0 commit comments

Comments
 (0)