Skip to content

Use more parameters when possible #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.apollographql.cache.normalized.sql.internal

import android.os.Build
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import app.cash.sqldelight.db.QueryResult
import com.apollographql.cache.normalized.sql.ApolloInitializer
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema


internal actual fun createDriver(name: String?, baseDir: String?, schema: SqlSchema<QueryResult.Value<Unit>>): SqlDriver {
check(baseDir == null) {
"Apollo: Android SqlNormalizedCacheFactory doesn't support 'baseDir'"
Expand All @@ -23,3 +23,11 @@ internal actual fun createDriver(name: String?, baseDir: String?, schema: SqlSch
internal actual fun maybeCreateOrMigrateSchema(driver: SqlDriver, schema: SqlSchema<QueryResult.Value<Unit>>) {
// no-op
}

// See https://www.sqlite.org/limits.html#:~:text=Maximum%20Number%20Of%20Host%20Parameters
// and https://developer.android.com/reference/android/database/sqlite/package-summary.html
internal actual val parametersMax: Int = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
999
} else {
32767
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ internal actual fun createDriver(name: String?, baseDir: String?, schema: SqlSch
internal actual fun maybeCreateOrMigrateSchema(driver: SqlDriver, schema: SqlSchema<QueryResult.Value<Unit>>) {
// no op
}

internal actual val parametersMax: Int = 999
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.apollographql.cache.normalized.api.RecordMerger
import com.apollographql.cache.normalized.api.RecordMergerContext
import com.apollographql.cache.normalized.api.withDates
import com.apollographql.cache.normalized.sql.internal.RecordDatabase
import com.apollographql.cache.normalized.sql.internal.parametersMax
import kotlin.reflect.KClass

class SqlNormalizedCache internal constructor(
Expand Down Expand Up @@ -88,7 +89,7 @@ class SqlNormalizedCache internal constructor(
} else {
emptySet()
}
return (keys + referencedKeys).chunked(999).sumOf { chunkedKeys ->
return (keys + referencedKeys).chunked(parametersMax).sumOf { chunkedKeys ->
recordDatabase.deleteRecords(chunkedKeys)
recordDatabase.changes().toInt()
}
Expand Down Expand Up @@ -126,7 +127,7 @@ class SqlNormalizedCache internal constructor(
private fun selectRecords(keys: Collection<CacheKey>): List<Record> {
return keys
.map { it.key }
.chunked(999).flatMap { chunkedKeys ->
.chunked(parametersMax).flatMap { chunkedKeys ->
recordDatabase.selectRecords(chunkedKeys)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ internal expect fun createDriver(name: String?, baseDir: String?, schema: SqlSch
* Others like JVM don't do this automatically. This is when [maybeCreateOrMigrateSchema] is needed
*/
internal expect fun maybeCreateOrMigrateSchema(driver: SqlDriver, schema: SqlSchema<QueryResult.Value<Unit>>)

// See https://www.sqlite.org/limits.html#:~:text=Maximum%20Number%20Of%20Host%20Parameters
internal expect val parametersMax: Int
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ internal actual fun maybeCreateOrMigrateSchema(driver: SqlDriver, schema: SqlSch
driver.execute(null, "PRAGMA $versionPragma=$newVersion", 0)
}
}

internal actual val parametersMax: Int = 999