From 7a309904b9cd804aa613ea58ab65889b1b42b39b Mon Sep 17 00:00:00 2001 From: BoD Date: Fri, 12 Jul 2024 19:48:26 +0200 Subject: [PATCH 1/6] Remove JSON based SQLite storage --- .../cache/normalized/api/MemoryCache.kt | 22 ++++- .../cache/normalized/api/Record.kt | 6 +- .../cache/normalized/api/RecordMerger.kt | 4 +- .../api/internal/JsonRecordSerializer.kt | 2 +- .../normalized/api/internal/Normalizer.kt | 2 +- .../normalized-cache-sqlite-incubating.api | 78 +--------------- .../normalized-cache-sqlite-incubating.api | 79 +---------------- ...ormalized-cache-sqlite-incubating.klib.api | 74 +--------------- .../sql/SqlNormalizedCacheFactory.kt | 11 +-- .../normalized/sql/createCacheFactory.kt | 8 -- .../sql/SqlNormalizedCacheFactory.kt | 11 +-- .../normalized/sql/createCacheFactory.kt | 8 -- .../normalized/sql/SqlNormalizedCache.kt | 2 +- .../sql/SqlNormalizedCacheFactory.kt | 8 +- .../sql/internal/JsonRecordDatabase.kt | 58 ------------ .../normalized/sql/internal/factoryHelpers.kt | 24 ++--- .../normalized/sql/internal/json/json.sq | 38 -------- .../cache/normalized/sql/OpenDbTest.kt | 25 ------ .../normalized/sql/SqlNormalizedCacheTest.kt | 8 +- .../sql/SqlNormalizedCacheFactoryJvm.kt | 12 +-- .../cache/normalized/sql/TrimTest.kt | 4 +- .../normalized/sql/createCacheFactory.kt | 8 -- tests/{sqlite => expiration}/build.gradle.kts | 0 .../src/commonMain/graphql/operations.graphql | 0 .../src/commonMain/graphql/schema.graphqls | 0 .../kotlin/ClientSideExpirationTest.kt} | 80 ++++------------- .../kotlin/ServerSideExpirationTest.kt | 88 +++++++++++++++++++ .../kotlin/ConnectionPaginationTest.kt | 11 +-- .../ConnectionProgrammaticPaginationTest.kt | 11 +-- .../ConnectionWithNodesPaginationTest.kt | 11 +-- .../kotlin/CursorBasedPaginationTest.kt | 11 +-- .../OffsetBasedWithArrayPaginationTest.kt | 11 +-- ...fsetBasedWithPageAndInputPaginationTest.kt | 11 +-- .../OffsetBasedWithPagePaginationTest.kt | 13 +-- 34 files changed, 194 insertions(+), 545 deletions(-) delete mode 100644 normalized-cache-sqlite-incubating/src/androidUnitTest/kotlin/com/apollographql/cache/normalized/sql/createCacheFactory.kt delete mode 100644 normalized-cache-sqlite-incubating/src/appleTest/kotlin/com/apollographql/cache/normalized/sql/createCacheFactory.kt delete mode 100644 normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/internal/JsonRecordDatabase.kt delete mode 100644 normalized-cache-sqlite-incubating/src/commonMain/sqldelight/json/com/apollographql/cache/normalized/sql/internal/json/json.sq delete mode 100644 normalized-cache-sqlite-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/sql/OpenDbTest.kt delete mode 100644 normalized-cache-sqlite-incubating/src/jvmTest/kotlin/com/apollographql/cache/normalized/sql/createCacheFactory.kt rename tests/{sqlite => expiration}/build.gradle.kts (100%) rename tests/{sqlite => expiration}/src/commonMain/graphql/operations.graphql (100%) rename tests/{sqlite => expiration}/src/commonMain/graphql/schema.graphqls (100%) rename tests/{sqlite/src/commonTest/kotlin/ExpirationTest.kt => expiration/src/commonTest/kotlin/ClientSideExpirationTest.kt} (58%) create mode 100644 tests/expiration/src/commonTest/kotlin/ServerSideExpirationTest.kt diff --git a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/MemoryCache.kt b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/MemoryCache.kt index b5f2fb84..993b53c0 100644 --- a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/MemoryCache.kt +++ b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/MemoryCache.kt @@ -124,11 +124,12 @@ class MemoryCache( private fun internalMerge(record: Record, cacheHeaders: CacheHeaders, recordMerger: RecordMerger): Set { val oldRecord = loadRecord(record.key, cacheHeaders) + val date = cacheHeaders.date() val changedKeys = if (oldRecord == null) { - lruCache[record.key] = record + lruCache[record.key] = record.withDate(date) record.fieldKeys() } else { - val (mergedRecord, changedKeys) = recordMerger.merge(existing = oldRecord, incoming = record, newDate = null) + val (mergedRecord, changedKeys) = recordMerger.merge(existing = oldRecord, incoming = record, newDate = date) lruCache[record.key] = mergedRecord changedKeys } @@ -167,3 +168,20 @@ class MemoryCacheFactory @JvmOverloads constructor( ) } } + +private fun CacheHeaders.date(): Long? { + return headerValue(ApolloCacheHeaders.DATE)?.toLong() +} + +private fun Record.withDate(date: Long?): Record { + if (date == null) { + return this + } + return Record( + key = key, + fields = fields, + mutationId = mutationId, + dates = fields.mapValues { date }, + metadata = metadata + ) +} diff --git a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/Record.kt b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/Record.kt index db827ba7..bf7b8e80 100644 --- a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/Record.kt +++ b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/Record.kt @@ -26,7 +26,7 @@ class Record( val mutationId: Uuid? = null, ) : Map by fields { - var dates: Map? = null + var dates: Map = emptyMap() private set /** @@ -40,10 +40,10 @@ class Record( key: String, fields: Map, mutationId: Uuid?, - date: Map, + dates: Map, metadata: Map>, ) : this(key, fields, mutationId) { - this.dates = date + this.dates = dates this.metadata = metadata } diff --git a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/RecordMerger.kt b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/RecordMerger.kt index d07e0b14..b1258055 100644 --- a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/RecordMerger.kt +++ b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/RecordMerger.kt @@ -42,7 +42,7 @@ object DefaultRecordMerger : RecordMerger { key = existing.key, fields = mergedFields, mutationId = incoming.mutationId, - date = date, + dates = date, metadata = existing.metadata + incoming.metadata, ) to changedKeys } @@ -115,7 +115,7 @@ class FieldRecordMerger(private val fieldMerger: FieldMerger) : RecordMerger { key = existing.key, fields = mergedFields, mutationId = incoming.mutationId, - date = date, + dates = date, metadata = mergedMetadata, ) to changedKeys } diff --git a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/internal/JsonRecordSerializer.kt b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/internal/JsonRecordSerializer.kt index b5159236..2b85e47f 100644 --- a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/internal/JsonRecordSerializer.kt +++ b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/internal/JsonRecordSerializer.kt @@ -76,7 +76,7 @@ object JsonRecordSerializer { key = key, fields = fields, mutationId = null, - date = emptyMap(), + dates = emptyMap(), metadata = allFields[KEY_METADATA] as Map> ) } diff --git a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/internal/Normalizer.kt b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/internal/Normalizer.kt index 688e29cd..c1206fc1 100644 --- a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/internal/Normalizer.kt +++ b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/internal/Normalizer.kt @@ -126,7 +126,7 @@ internal class Normalizer( key = key, fields = fieldValues, mutationId = null, - date = emptyMap(), + dates = emptyMap(), metadata = metadata, ) diff --git a/normalized-cache-sqlite-incubating/api/android/normalized-cache-sqlite-incubating.api b/normalized-cache-sqlite-incubating/api/android/normalized-cache-sqlite-incubating.api index 11d63315..241891bf 100644 --- a/normalized-cache-sqlite-incubating/api/android/normalized-cache-sqlite-incubating.api +++ b/normalized-cache-sqlite-incubating/api/android/normalized-cache-sqlite-incubating.api @@ -27,12 +27,10 @@ public final class com/apollographql/cache/normalized/sql/SqlNormalizedCacheFact public fun (Landroid/content/Context;Ljava/lang/String;Landroidx/sqlite/db/SupportSQLiteOpenHelper$Factory;Lkotlin/jvm/functions/Function1;)V public fun (Landroid/content/Context;Ljava/lang/String;Landroidx/sqlite/db/SupportSQLiteOpenHelper$Factory;Lkotlin/jvm/functions/Function1;Z)V public fun (Landroid/content/Context;Ljava/lang/String;Landroidx/sqlite/db/SupportSQLiteOpenHelper$Factory;Lkotlin/jvm/functions/Function1;ZLjava/lang/Long;)V - public fun (Landroid/content/Context;Ljava/lang/String;Landroidx/sqlite/db/SupportSQLiteOpenHelper$Factory;Lkotlin/jvm/functions/Function1;ZLjava/lang/Long;Z)V - public synthetic fun (Landroid/content/Context;Ljava/lang/String;Landroidx/sqlite/db/SupportSQLiteOpenHelper$Factory;Lkotlin/jvm/functions/Function1;ZLjava/lang/Long;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lapp/cash/sqldelight/db/SqlDriver;Z)V - public synthetic fun (Lapp/cash/sqldelight/db/SqlDriver;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Ljava/lang/String;Z)V - public synthetic fun (Ljava/lang/String;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V + public synthetic fun (Landroid/content/Context;Ljava/lang/String;Landroidx/sqlite/db/SupportSQLiteOpenHelper$Factory;Lkotlin/jvm/functions/Function1;ZLjava/lang/Long;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lapp/cash/sqldelight/db/SqlDriver;)V + public fun (Ljava/lang/String;)V + public synthetic fun (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun create ()Lcom/apollographql/cache/normalized/api/NormalizedCache; } @@ -150,71 +148,3 @@ public final class com/apollographql/cache/normalized/sql/internal/blob2/Records public fun toString ()Ljava/lang/String; } -public abstract interface class com/apollographql/cache/normalized/sql/internal/json/JsonDatabase : app/cash/sqldelight/Transacter { - public static final field Companion Lcom/apollographql/cache/normalized/sql/internal/json/JsonDatabase$Companion; - public abstract fun getJsonQueries ()Lcom/apollographql/cache/normalized/sql/internal/json/JsonQueries; -} - -public final class com/apollographql/cache/normalized/sql/internal/json/JsonDatabase$Companion { - public final fun getSchema ()Lapp/cash/sqldelight/db/SqlSchema; - public final fun invoke (Lapp/cash/sqldelight/db/SqlDriver;)Lcom/apollographql/cache/normalized/sql/internal/json/JsonDatabase; -} - -public final class com/apollographql/cache/normalized/sql/internal/json/JsonQueries : app/cash/sqldelight/TransacterImpl { - public fun (Lapp/cash/sqldelight/db/SqlDriver;)V - public final fun changes ()Lapp/cash/sqldelight/ExecutableQuery; - public final fun delete (Ljava/lang/String;)V - public final fun deleteAll ()V - public final fun deleteRecords (Ljava/util/Collection;)V - public final fun deleteRecordsWithKeyMatching (Ljava/lang/String;Ljava/lang/String;)V - public final fun insert (Ljava/lang/String;Ljava/lang/String;)V - public final fun recordForKey (Ljava/lang/String;)Lapp/cash/sqldelight/Query; - public final fun recordForKey (Ljava/lang/String;Lkotlin/jvm/functions/Function2;)Lapp/cash/sqldelight/Query; - public final fun recordsForKeys (Ljava/util/Collection;)Lapp/cash/sqldelight/Query; - public final fun recordsForKeys (Ljava/util/Collection;Lkotlin/jvm/functions/Function2;)Lapp/cash/sqldelight/Query; - public final fun selectRecords ()Lapp/cash/sqldelight/Query; - public final fun selectRecords (Lkotlin/jvm/functions/Function3;)Lapp/cash/sqldelight/Query; - public final fun update (Ljava/lang/String;Ljava/lang/String;)V -} - -public final class com/apollographql/cache/normalized/sql/internal/json/RecordForKey { - public fun (Ljava/lang/String;Ljava/lang/String;)V - public final fun component1 ()Ljava/lang/String; - public final fun component2 ()Ljava/lang/String; - public final fun copy (Ljava/lang/String;Ljava/lang/String;)Lcom/apollographql/cache/normalized/sql/internal/json/RecordForKey; - public static synthetic fun copy$default (Lcom/apollographql/cache/normalized/sql/internal/json/RecordForKey;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lcom/apollographql/cache/normalized/sql/internal/json/RecordForKey; - public fun equals (Ljava/lang/Object;)Z - public final fun getKey ()Ljava/lang/String; - public final fun getRecord ()Ljava/lang/String; - public fun hashCode ()I - public fun toString ()Ljava/lang/String; -} - -public final class com/apollographql/cache/normalized/sql/internal/json/Records { - public fun (JLjava/lang/String;Ljava/lang/String;)V - public final fun component1 ()J - public final fun component2 ()Ljava/lang/String; - public final fun component3 ()Ljava/lang/String; - public final fun copy (JLjava/lang/String;Ljava/lang/String;)Lcom/apollographql/cache/normalized/sql/internal/json/Records; - public static synthetic fun copy$default (Lcom/apollographql/cache/normalized/sql/internal/json/Records;JLjava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lcom/apollographql/cache/normalized/sql/internal/json/Records; - public fun equals (Ljava/lang/Object;)Z - public final fun getKey ()Ljava/lang/String; - public final fun getRecord ()Ljava/lang/String; - public final fun get_id ()J - public fun hashCode ()I - public fun toString ()Ljava/lang/String; -} - -public final class com/apollographql/cache/normalized/sql/internal/json/RecordsForKeys { - public fun (Ljava/lang/String;Ljava/lang/String;)V - public final fun component1 ()Ljava/lang/String; - public final fun component2 ()Ljava/lang/String; - public final fun copy (Ljava/lang/String;Ljava/lang/String;)Lcom/apollographql/cache/normalized/sql/internal/json/RecordsForKeys; - public static synthetic fun copy$default (Lcom/apollographql/cache/normalized/sql/internal/json/RecordsForKeys;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lcom/apollographql/cache/normalized/sql/internal/json/RecordsForKeys; - public fun equals (Ljava/lang/Object;)Z - public final fun getKey ()Ljava/lang/String; - public final fun getRecord ()Ljava/lang/String; - public fun hashCode ()I - public fun toString ()Ljava/lang/String; -} - diff --git a/normalized-cache-sqlite-incubating/api/jvm/normalized-cache-sqlite-incubating.api b/normalized-cache-sqlite-incubating/api/jvm/normalized-cache-sqlite-incubating.api index c4d7f455..7f84177a 100644 --- a/normalized-cache-sqlite-incubating/api/jvm/normalized-cache-sqlite-incubating.api +++ b/normalized-cache-sqlite-incubating/api/jvm/normalized-cache-sqlite-incubating.api @@ -10,15 +10,12 @@ public final class com/apollographql/cache/normalized/sql/SqlNormalizedCache : c } public final class com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory : com/apollographql/cache/normalized/api/NormalizedCacheFactory { - public fun (Lapp/cash/sqldelight/db/SqlDriver;Z)V - public synthetic fun (Lapp/cash/sqldelight/db/SqlDriver;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lapp/cash/sqldelight/db/SqlDriver;)V public fun (Ljava/lang/String;)V + public synthetic fun (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Ljava/lang/String;Ljava/lang/String;)V public fun (Ljava/lang/String;Ljava/util/Properties;)V - public fun (Ljava/lang/String;Ljava/util/Properties;Z)V - public synthetic fun (Ljava/lang/String;Ljava/util/Properties;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Ljava/lang/String;Z)V - public synthetic fun (Ljava/lang/String;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Ljava/lang/String;ZLjava/lang/String;)V + public synthetic fun (Ljava/lang/String;Ljava/util/Properties;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun create ()Lcom/apollographql/cache/normalized/api/NormalizedCache; } @@ -140,71 +137,3 @@ public final class com/apollographql/cache/normalized/sql/internal/blob2/Records public fun toString ()Ljava/lang/String; } -public abstract interface class com/apollographql/cache/normalized/sql/internal/json/JsonDatabase : app/cash/sqldelight/Transacter { - public static final field Companion Lcom/apollographql/cache/normalized/sql/internal/json/JsonDatabase$Companion; - public abstract fun getJsonQueries ()Lcom/apollographql/cache/normalized/sql/internal/json/JsonQueries; -} - -public final class com/apollographql/cache/normalized/sql/internal/json/JsonDatabase$Companion { - public final fun getSchema ()Lapp/cash/sqldelight/db/SqlSchema; - public final fun invoke (Lapp/cash/sqldelight/db/SqlDriver;)Lcom/apollographql/cache/normalized/sql/internal/json/JsonDatabase; -} - -public final class com/apollographql/cache/normalized/sql/internal/json/JsonQueries : app/cash/sqldelight/TransacterImpl { - public fun (Lapp/cash/sqldelight/db/SqlDriver;)V - public final fun changes ()Lapp/cash/sqldelight/ExecutableQuery; - public final fun delete (Ljava/lang/String;)V - public final fun deleteAll ()V - public final fun deleteRecords (Ljava/util/Collection;)V - public final fun deleteRecordsWithKeyMatching (Ljava/lang/String;Ljava/lang/String;)V - public final fun insert (Ljava/lang/String;Ljava/lang/String;)V - public final fun recordForKey (Ljava/lang/String;)Lapp/cash/sqldelight/Query; - public final fun recordForKey (Ljava/lang/String;Lkotlin/jvm/functions/Function2;)Lapp/cash/sqldelight/Query; - public final fun recordsForKeys (Ljava/util/Collection;)Lapp/cash/sqldelight/Query; - public final fun recordsForKeys (Ljava/util/Collection;Lkotlin/jvm/functions/Function2;)Lapp/cash/sqldelight/Query; - public final fun selectRecords ()Lapp/cash/sqldelight/Query; - public final fun selectRecords (Lkotlin/jvm/functions/Function3;)Lapp/cash/sqldelight/Query; - public final fun update (Ljava/lang/String;Ljava/lang/String;)V -} - -public final class com/apollographql/cache/normalized/sql/internal/json/RecordForKey { - public fun (Ljava/lang/String;Ljava/lang/String;)V - public final fun component1 ()Ljava/lang/String; - public final fun component2 ()Ljava/lang/String; - public final fun copy (Ljava/lang/String;Ljava/lang/String;)Lcom/apollographql/cache/normalized/sql/internal/json/RecordForKey; - public static synthetic fun copy$default (Lcom/apollographql/cache/normalized/sql/internal/json/RecordForKey;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lcom/apollographql/cache/normalized/sql/internal/json/RecordForKey; - public fun equals (Ljava/lang/Object;)Z - public final fun getKey ()Ljava/lang/String; - public final fun getRecord ()Ljava/lang/String; - public fun hashCode ()I - public fun toString ()Ljava/lang/String; -} - -public final class com/apollographql/cache/normalized/sql/internal/json/Records { - public fun (JLjava/lang/String;Ljava/lang/String;)V - public final fun component1 ()J - public final fun component2 ()Ljava/lang/String; - public final fun component3 ()Ljava/lang/String; - public final fun copy (JLjava/lang/String;Ljava/lang/String;)Lcom/apollographql/cache/normalized/sql/internal/json/Records; - public static synthetic fun copy$default (Lcom/apollographql/cache/normalized/sql/internal/json/Records;JLjava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lcom/apollographql/cache/normalized/sql/internal/json/Records; - public fun equals (Ljava/lang/Object;)Z - public final fun getKey ()Ljava/lang/String; - public final fun getRecord ()Ljava/lang/String; - public final fun get_id ()J - public fun hashCode ()I - public fun toString ()Ljava/lang/String; -} - -public final class com/apollographql/cache/normalized/sql/internal/json/RecordsForKeys { - public fun (Ljava/lang/String;Ljava/lang/String;)V - public final fun component1 ()Ljava/lang/String; - public final fun component2 ()Ljava/lang/String; - public final fun copy (Ljava/lang/String;Ljava/lang/String;)Lcom/apollographql/cache/normalized/sql/internal/json/RecordsForKeys; - public static synthetic fun copy$default (Lcom/apollographql/cache/normalized/sql/internal/json/RecordsForKeys;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lcom/apollographql/cache/normalized/sql/internal/json/RecordsForKeys; - public fun equals (Ljava/lang/Object;)Z - public final fun getKey ()Ljava/lang/String; - public final fun getRecord ()Ljava/lang/String; - public fun hashCode ()I - public fun toString ()Ljava/lang/String; -} - diff --git a/normalized-cache-sqlite-incubating/api/normalized-cache-sqlite-incubating.klib.api b/normalized-cache-sqlite-incubating/api/normalized-cache-sqlite-incubating.klib.api index 8f5802ae..f208ea92 100644 --- a/normalized-cache-sqlite-incubating/api/normalized-cache-sqlite-incubating.klib.api +++ b/normalized-cache-sqlite-incubating/api/normalized-cache-sqlite-incubating.klib.api @@ -24,15 +24,6 @@ abstract interface com.apollographql.cache.normalized.sql.internal.blob2/Blob2Da final fun (): app.cash.sqldelight.db/SqlSchema> // com.apollographql.cache.normalized.sql.internal.blob2/Blob2Database.Companion.Schema.|(){}[0] } } -abstract interface com.apollographql.cache.normalized.sql.internal.json/JsonDatabase : app.cash.sqldelight/Transacter { // com.apollographql.cache.normalized.sql.internal.json/JsonDatabase|null[0] - abstract val jsonQueries // com.apollographql.cache.normalized.sql.internal.json/JsonDatabase.jsonQueries|{}jsonQueries[0] - abstract fun (): com.apollographql.cache.normalized.sql.internal.json/JsonQueries // com.apollographql.cache.normalized.sql.internal.json/JsonDatabase.jsonQueries.|(){}[0] - final object Companion { // com.apollographql.cache.normalized.sql.internal.json/JsonDatabase.Companion|null[0] - final fun invoke(app.cash.sqldelight.db/SqlDriver): com.apollographql.cache.normalized.sql.internal.json/JsonDatabase // com.apollographql.cache.normalized.sql.internal.json/JsonDatabase.Companion.invoke|invoke(app.cash.sqldelight.db.SqlDriver){}[0] - final val Schema // com.apollographql.cache.normalized.sql.internal.json/JsonDatabase.Companion.Schema|{}Schema[0] - final fun (): app.cash.sqldelight.db/SqlSchema> // com.apollographql.cache.normalized.sql.internal.json/JsonDatabase.Companion.Schema.|(){}[0] - } -} final class com.apollographql.cache.normalized.sql.internal.blob/BlobQueries : app.cash.sqldelight/TransacterImpl { // com.apollographql.cache.normalized.sql.internal.blob/BlobQueries|null[0] constructor (app.cash.sqldelight.db/SqlDriver) // com.apollographql.cache.normalized.sql.internal.blob/BlobQueries.|(app.cash.sqldelight.db.SqlDriver){}[0] final fun <#A1: kotlin/Any> recordForKey(kotlin/String, kotlin/Function2): app.cash.sqldelight/Query<#A1> // com.apollographql.cache.normalized.sql.internal.blob/BlobQueries.recordForKey|recordForKey(kotlin.String;kotlin.Function2){0§}[0] @@ -122,64 +113,6 @@ final class com.apollographql.cache.normalized.sql.internal.blob2/RecordsForKeys final val key // com.apollographql.cache.normalized.sql.internal.blob2/RecordsForKeys.key|{}key[0] final fun (): kotlin/String // com.apollographql.cache.normalized.sql.internal.blob2/RecordsForKeys.key.|(){}[0] } -final class com.apollographql.cache.normalized.sql.internal.json/JsonQueries : app.cash.sqldelight/TransacterImpl { // com.apollographql.cache.normalized.sql.internal.json/JsonQueries|null[0] - constructor (app.cash.sqldelight.db/SqlDriver) // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.|(app.cash.sqldelight.db.SqlDriver){}[0] - final fun <#A1: kotlin/Any> recordForKey(kotlin/String, kotlin/Function2): app.cash.sqldelight/Query<#A1> // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.recordForKey|recordForKey(kotlin.String;kotlin.Function2){0§}[0] - final fun <#A1: kotlin/Any> recordsForKeys(kotlin.collections/Collection, kotlin/Function2): app.cash.sqldelight/Query<#A1> // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.recordsForKeys|recordsForKeys(kotlin.collections.Collection;kotlin.Function2){0§}[0] - final fun <#A1: kotlin/Any> selectRecords(kotlin/Function3): app.cash.sqldelight/Query<#A1> // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.selectRecords|selectRecords(kotlin.Function3){0§}[0] - final fun changes(): app.cash.sqldelight/ExecutableQuery // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.changes|changes(){}[0] - final fun delete(kotlin/String) // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.delete|delete(kotlin.String){}[0] - final fun deleteAll() // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.deleteAll|deleteAll(){}[0] - final fun deleteRecords(kotlin.collections/Collection) // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.deleteRecords|deleteRecords(kotlin.collections.Collection){}[0] - final fun deleteRecordsWithKeyMatching(kotlin/String, kotlin/String) // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.deleteRecordsWithKeyMatching|deleteRecordsWithKeyMatching(kotlin.String;kotlin.String){}[0] - final fun insert(kotlin/String, kotlin/String) // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.insert|insert(kotlin.String;kotlin.String){}[0] - final fun recordForKey(kotlin/String): app.cash.sqldelight/Query // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.recordForKey|recordForKey(kotlin.String){}[0] - final fun recordsForKeys(kotlin.collections/Collection): app.cash.sqldelight/Query // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.recordsForKeys|recordsForKeys(kotlin.collections.Collection){}[0] - final fun selectRecords(): app.cash.sqldelight/Query // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.selectRecords|selectRecords(){}[0] - final fun update(kotlin/String, kotlin/String) // com.apollographql.cache.normalized.sql.internal.json/JsonQueries.update|update(kotlin.String;kotlin.String){}[0] -} -final class com.apollographql.cache.normalized.sql.internal.json/RecordForKey { // com.apollographql.cache.normalized.sql.internal.json/RecordForKey|null[0] - constructor (kotlin/String, kotlin/String) // com.apollographql.cache.normalized.sql.internal.json/RecordForKey.|(kotlin.String;kotlin.String){}[0] - final fun component1(): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/RecordForKey.component1|component1(){}[0] - final fun component2(): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/RecordForKey.component2|component2(){}[0] - final fun copy(kotlin/String = ..., kotlin/String = ...): com.apollographql.cache.normalized.sql.internal.json/RecordForKey // com.apollographql.cache.normalized.sql.internal.json/RecordForKey.copy|copy(kotlin.String;kotlin.String){}[0] - final fun equals(kotlin/Any?): kotlin/Boolean // com.apollographql.cache.normalized.sql.internal.json/RecordForKey.equals|equals(kotlin.Any?){}[0] - final fun hashCode(): kotlin/Int // com.apollographql.cache.normalized.sql.internal.json/RecordForKey.hashCode|hashCode(){}[0] - final fun toString(): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/RecordForKey.toString|toString(){}[0] - final val key // com.apollographql.cache.normalized.sql.internal.json/RecordForKey.key|{}key[0] - final fun (): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/RecordForKey.key.|(){}[0] - final val record // com.apollographql.cache.normalized.sql.internal.json/RecordForKey.record|{}record[0] - final fun (): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/RecordForKey.record.|(){}[0] -} -final class com.apollographql.cache.normalized.sql.internal.json/Records { // com.apollographql.cache.normalized.sql.internal.json/Records|null[0] - constructor (kotlin/Long, kotlin/String, kotlin/String) // com.apollographql.cache.normalized.sql.internal.json/Records.|(kotlin.Long;kotlin.String;kotlin.String){}[0] - final fun component1(): kotlin/Long // com.apollographql.cache.normalized.sql.internal.json/Records.component1|component1(){}[0] - final fun component2(): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/Records.component2|component2(){}[0] - final fun component3(): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/Records.component3|component3(){}[0] - final fun copy(kotlin/Long = ..., kotlin/String = ..., kotlin/String = ...): com.apollographql.cache.normalized.sql.internal.json/Records // com.apollographql.cache.normalized.sql.internal.json/Records.copy|copy(kotlin.Long;kotlin.String;kotlin.String){}[0] - final fun equals(kotlin/Any?): kotlin/Boolean // com.apollographql.cache.normalized.sql.internal.json/Records.equals|equals(kotlin.Any?){}[0] - final fun hashCode(): kotlin/Int // com.apollographql.cache.normalized.sql.internal.json/Records.hashCode|hashCode(){}[0] - final fun toString(): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/Records.toString|toString(){}[0] - final val _id // com.apollographql.cache.normalized.sql.internal.json/Records._id|{}_id[0] - final fun (): kotlin/Long // com.apollographql.cache.normalized.sql.internal.json/Records._id.|(){}[0] - final val key // com.apollographql.cache.normalized.sql.internal.json/Records.key|{}key[0] - final fun (): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/Records.key.|(){}[0] - final val record // com.apollographql.cache.normalized.sql.internal.json/Records.record|{}record[0] - final fun (): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/Records.record.|(){}[0] -} -final class com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys { // com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys|null[0] - constructor (kotlin/String, kotlin/String) // com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys.|(kotlin.String;kotlin.String){}[0] - final fun component1(): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys.component1|component1(){}[0] - final fun component2(): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys.component2|component2(){}[0] - final fun copy(kotlin/String = ..., kotlin/String = ...): com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys // com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys.copy|copy(kotlin.String;kotlin.String){}[0] - final fun equals(kotlin/Any?): kotlin/Boolean // com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys.equals|equals(kotlin.Any?){}[0] - final fun hashCode(): kotlin/Int // com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys.hashCode|hashCode(){}[0] - final fun toString(): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys.toString|toString(){}[0] - final val key // com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys.key|{}key[0] - final fun (): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys.key.|(){}[0] - final val record // com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys.record|{}record[0] - final fun (): kotlin/String // com.apollographql.cache.normalized.sql.internal.json/RecordsForKeys.record.|(){}[0] -} final class com.apollographql.cache.normalized.sql/SqlNormalizedCache : com.apollographql.cache.normalized.api/NormalizedCache { // com.apollographql.cache.normalized.sql/SqlNormalizedCache|null[0] final fun clearAll() // com.apollographql.cache.normalized.sql/SqlNormalizedCache.clearAll|clearAll(){}[0] final fun dump(): kotlin.collections/Map, kotlin.collections/Map> // com.apollographql.cache.normalized.sql/SqlNormalizedCache.dump|dump(){}[0] @@ -192,10 +125,9 @@ final class com.apollographql.cache.normalized.sql/SqlNormalizedCache : com.apol } final class com.apollographql.cache.normalized.sql/SqlNormalizedCacheFactory : com.apollographql.cache.normalized.api/NormalizedCacheFactory { // com.apollographql.cache.normalized.sql/SqlNormalizedCacheFactory|null[0] constructor () // com.apollographql.cache.normalized.sql/SqlNormalizedCacheFactory.|(){}[0] - constructor (app.cash.sqldelight.db/SqlDriver, kotlin/Boolean = ...) // com.apollographql.cache.normalized.sql/SqlNormalizedCacheFactory.|(app.cash.sqldelight.db.SqlDriver;kotlin.Boolean){}[0] - constructor (kotlin/String) // com.apollographql.cache.normalized.sql/SqlNormalizedCacheFactory.|(kotlin.String){}[0] - constructor (kotlin/String? = ..., kotlin/Boolean = ...) // com.apollographql.cache.normalized.sql/SqlNormalizedCacheFactory.|(kotlin.String?;kotlin.Boolean){}[0] - constructor (kotlin/String?, kotlin/Boolean, kotlin/String?) // com.apollographql.cache.normalized.sql/SqlNormalizedCacheFactory.|(kotlin.String?;kotlin.Boolean;kotlin.String?){}[0] + constructor (app.cash.sqldelight.db/SqlDriver) // com.apollographql.cache.normalized.sql/SqlNormalizedCacheFactory.|(app.cash.sqldelight.db.SqlDriver){}[0] + constructor (kotlin/String? = ...) // com.apollographql.cache.normalized.sql/SqlNormalizedCacheFactory.|(kotlin.String?){}[0] + constructor (kotlin/String?, kotlin/String?) // com.apollographql.cache.normalized.sql/SqlNormalizedCacheFactory.|(kotlin.String?;kotlin.String?){}[0] final fun create(): com.apollographql.cache.normalized.api/NormalizedCache // com.apollographql.cache.normalized.sql/SqlNormalizedCacheFactory.create|create(){}[0] } final const val com.apollographql.cache.normalized.sql/VERSION // com.apollographql.cache.normalized.sql/VERSION|{}VERSION[0] diff --git a/normalized-cache-sqlite-incubating/src/androidMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt b/normalized-cache-sqlite-incubating/src/androidMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt index 781dc501..5a26c426 100644 --- a/normalized-cache-sqlite-incubating/src/androidMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt +++ b/normalized-cache-sqlite-incubating/src/androidMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt @@ -14,7 +14,6 @@ import com.apollographql.cache.normalized.api.NormalizedCache actual class SqlNormalizedCacheFactory actual constructor( private val driver: SqlDriver, - private val withDates: Boolean, ) : NormalizedCacheFactory() { /** @@ -33,14 +32,13 @@ actual class SqlNormalizedCacheFactory actual constructor( configure: ((SupportSQLiteDatabase) -> Unit)? = null, useNoBackupDirectory: Boolean = false, windowSizeBytes: Long? = null, - withDates: Boolean = false, ) : this( AndroidSqliteDriver( - getSchema(withDates), + getSchema(), context.applicationContext, name, factory, - object : AndroidSqliteDriver.Callback(getSchema(withDates)) { + object : AndroidSqliteDriver.Callback(getSchema()) { override fun onConfigure(db: SupportSQLiteDatabase) { super.onConfigure(db) configure?.invoke(db) @@ -49,13 +47,12 @@ actual class SqlNormalizedCacheFactory actual constructor( useNoBackupDirectory = useNoBackupDirectory, windowSizeBytes = windowSizeBytes, ), - withDates ) - actual constructor(name: String?, withDates: Boolean) : this(createDriver(name, null, getSchema(withDates)), withDates) + actual constructor(name: String?) : this(createDriver(name, null, getSchema())) actual override fun create(): NormalizedCache { - return SqlNormalizedCache(createRecordDatabase(driver, withDates)) + return SqlNormalizedCache(createRecordDatabase(driver)) } } diff --git a/normalized-cache-sqlite-incubating/src/androidUnitTest/kotlin/com/apollographql/cache/normalized/sql/createCacheFactory.kt b/normalized-cache-sqlite-incubating/src/androidUnitTest/kotlin/com/apollographql/cache/normalized/sql/createCacheFactory.kt deleted file mode 100644 index a84140e4..00000000 --- a/normalized-cache-sqlite-incubating/src/androidUnitTest/kotlin/com/apollographql/cache/normalized/sql/createCacheFactory.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.apollographql.cache.normalized.sql - -actual fun createCacheFactory( - baseDir: String, - withDates: Boolean, -): SqlNormalizedCacheFactory { - TODO("Allow to have both the Jdbc and the Android normalized caches at the same time") -} diff --git a/normalized-cache-sqlite-incubating/src/appleMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt b/normalized-cache-sqlite-incubating/src/appleMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt index 2a0e1a0f..26400ada 100644 --- a/normalized-cache-sqlite-incubating/src/appleMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt +++ b/normalized-cache-sqlite-incubating/src/appleMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt @@ -9,24 +9,21 @@ import com.apollographql.cache.normalized.sql.internal.getSchema actual class SqlNormalizedCacheFactory actual constructor( private val driver: SqlDriver, - private val withDates: Boolean, ) : NormalizedCacheFactory() { /** * @param name the name of the database or null for an in-memory database - * @param withDates whether to account for dates in the database. * @param baseDir the baseDirectory where to store the database. * [baseDir] must exist and be a directory * If [baseDir] is a relative path, it will be interpreted relative to the current working directory */ - constructor(name: String?, withDates: Boolean, baseDir: String?) : this(createDriver(name, baseDir, getSchema(withDates)), withDates) - actual constructor(name: String?, withDates: Boolean) : this(name, withDates, null) - constructor(name: String) : this(name, false) - constructor() : this("apollo.db", false) + constructor(name: String?, baseDir: String?) : this(createDriver(name, baseDir, getSchema())) + actual constructor(name: String?) : this(name, null) + constructor() : this("apollo.db") actual override fun create(): NormalizedCache { return SqlNormalizedCache( - recordDatabase = createRecordDatabase(driver, withDates) + recordDatabase = createRecordDatabase(driver) ) } } diff --git a/normalized-cache-sqlite-incubating/src/appleTest/kotlin/com/apollographql/cache/normalized/sql/createCacheFactory.kt b/normalized-cache-sqlite-incubating/src/appleTest/kotlin/com/apollographql/cache/normalized/sql/createCacheFactory.kt deleted file mode 100644 index 0adae2e0..00000000 --- a/normalized-cache-sqlite-incubating/src/appleTest/kotlin/com/apollographql/cache/normalized/sql/createCacheFactory.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.apollographql.cache.normalized.sql - -actual fun createCacheFactory( - baseDir: String, - withDates: Boolean, -): SqlNormalizedCacheFactory { - return SqlNormalizedCacheFactory(name = "apolloTestDb", withDates, baseDir) -} diff --git a/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCache.kt b/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCache.kt index d8eca82d..5aa47ba0 100644 --- a/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCache.kt +++ b/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCache.kt @@ -176,7 +176,7 @@ class SqlNormalizedCache internal constructor( key = key, fields = fields, mutationId = mutationId, - date = fields.mapValues { date }, + dates = fields.mapValues { date }, metadata = metadata ) } diff --git a/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt b/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt index 4a7fb4ff..8409909b 100644 --- a/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt +++ b/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt @@ -13,13 +13,9 @@ import com.apollographql.cache.normalized.api.NormalizedCacheFactory * - on MacOS, it will use "Application Support/databases/name" * - on the JVM, it will use "System.getProperty("user.home")/.apollo" * Default: "apollo.db" - * - * @param withDates: whether to store dates (receive dates or expiration dates) - * Once a database is created, this parameter cannot change - * Default: false */ -expect class SqlNormalizedCacheFactory(name: String? = "apollo.db", withDates: Boolean = false) : NormalizedCacheFactory { - constructor(driver: SqlDriver, withDates: Boolean = false) +expect class SqlNormalizedCacheFactory(name: String? = "apollo.db") : NormalizedCacheFactory { + constructor(driver: SqlDriver) override fun create(): NormalizedCache } diff --git a/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/internal/JsonRecordDatabase.kt b/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/internal/JsonRecordDatabase.kt deleted file mode 100644 index a017bef3..00000000 --- a/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/internal/JsonRecordDatabase.kt +++ /dev/null @@ -1,58 +0,0 @@ -package com.apollographql.cache.normalized.sql.internal - -import com.apollographql.cache.normalized.api.Record -import com.apollographql.cache.normalized.api.internal.JsonRecordSerializer -import com.apollographql.cache.normalized.sql.internal.json.JsonQueries - -internal class JsonRecordDatabase(private val jsonQueries: JsonQueries) : RecordDatabase { - override fun select(key: String): Record? { - return jsonQueries.recordForKey(key).executeAsList() - .map { - JsonRecordSerializer.deserialize(it.key, it.record) - } - .singleOrNull() - } - - override fun select(keys: Collection): List { - return jsonQueries.recordsForKeys(keys).executeAsList() - .map { - JsonRecordSerializer.deserialize(it.key, it.record) - } - } - - override fun transaction(noEnclosing: Boolean, body: () -> T): T { - return jsonQueries.transactionWithResult { - body() - } - } - - override fun delete(key: String) { - jsonQueries.delete(key) - } - - override fun deleteMatching(pattern: String) { - jsonQueries.deleteRecordsWithKeyMatching(pattern, "\\") - } - - override fun deleteAll() { - jsonQueries.deleteAll() - } - - override fun changes(): Long { - return jsonQueries.changes().executeAsOne() - } - - override fun insert(record: Record) { - jsonQueries.insert(record.key, JsonRecordSerializer.serialize(record)) - } - - override fun update(record: Record) { - jsonQueries.update(JsonRecordSerializer.serialize(record), record.key) - } - - override fun selectAll(): List { - return jsonQueries.selectRecords().executeAsList().map { - JsonRecordSerializer.deserialize(it.key, it.record) - } - } -} diff --git a/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/internal/factoryHelpers.kt b/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/internal/factoryHelpers.kt index a05bbd68..0fbdfe92 100644 --- a/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/internal/factoryHelpers.kt +++ b/normalized-cache-sqlite-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/internal/factoryHelpers.kt @@ -5,10 +5,9 @@ import app.cash.sqldelight.db.SqlDriver import app.cash.sqldelight.db.SqlSchema import com.apollographql.apollo.exception.apolloExceptionHandler import com.apollographql.cache.normalized.sql.internal.blob.BlobDatabase -import com.apollographql.cache.normalized.sql.internal.json.JsonDatabase -internal fun createRecordDatabase(driver: SqlDriver, withDates: Boolean): RecordDatabase { - maybeCreateOrMigrateSchema(driver, getSchema(withDates)) +internal fun createRecordDatabase(driver: SqlDriver): RecordDatabase { + maybeCreateOrMigrateSchema(driver, getSchema()) val tableNames = mutableListOf() @@ -31,23 +30,12 @@ internal fun createRecordDatabase(driver: SqlDriver, withDates: Boolean): Record */ } - val expectedTableName = if (withDates) "blobs" else "records" - + val expectedTableName = "blobs" check(tableNames.isEmpty() || tableNames.contains(expectedTableName)) { - "Apollo: Cannot find the '$expectedTableName' table, did you change the 'withDates' parameter? (found '$tableNames' instead)" + "Apollo: Cannot find the '$expectedTableName' table (found '$tableNames' instead)" } - - - return if (withDates) { - BlobRecordDatabase(BlobDatabase(driver).blobQueries) - } else { - JsonRecordDatabase(JsonDatabase(driver).jsonQueries) - } + return BlobRecordDatabase(BlobDatabase(driver).blobQueries) } -internal fun getSchema(withDates: Boolean): SqlSchema> = if (withDates) { - BlobDatabase.Schema -} else { - JsonDatabase.Schema -} +internal fun getSchema(): SqlSchema> = BlobDatabase.Schema diff --git a/normalized-cache-sqlite-incubating/src/commonMain/sqldelight/json/com/apollographql/cache/normalized/sql/internal/json/json.sq b/normalized-cache-sqlite-incubating/src/commonMain/sqldelight/json/com/apollographql/cache/normalized/sql/internal/json/json.sq deleted file mode 100644 index c73664f9..00000000 --- a/normalized-cache-sqlite-incubating/src/commonMain/sqldelight/json/com/apollographql/cache/normalized/sql/internal/json/json.sq +++ /dev/null @@ -1,38 +0,0 @@ -CREATE TABLE records ( - _id INTEGER PRIMARY KEY AUTOINCREMENT, - key TEXT NOT NULL, - record TEXT NOT NULL -); - -CREATE INDEX idx_records_key ON records(key); - -recordForKey: -SELECT key, record FROM records WHERE key=?; - -recordsForKeys: -SELECT key, record FROM records WHERE key IN ?; - -insert: -INSERT INTO records (key, record) VALUES (?,?); - -update: -UPDATE records SET record=:record WHERE key=:key; - -delete: -DELETE FROM records WHERE key=?; - -deleteRecords: -DELETE FROM records WHERE key IN ?; - -deleteRecordsWithKeyMatching: -DELETE FROM records WHERE key LIKE ? ESCAPE ?; - --- use only for debug -selectRecords: -SELECT * FROM records; - -changes: -SELECT changes(); - -deleteAll: -DELETE FROM records; diff --git a/normalized-cache-sqlite-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/sql/OpenDbTest.kt b/normalized-cache-sqlite-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/sql/OpenDbTest.kt deleted file mode 100644 index ddd177fc..00000000 --- a/normalized-cache-sqlite-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/sql/OpenDbTest.kt +++ /dev/null @@ -1,25 +0,0 @@ -package com.apollographql.cache.normalized.sql - -import com.apollographql.apollo.testing.HostFileSystem -import okio.Path.Companion.toPath -import kotlin.test.Test -import kotlin.test.assertTrue -import kotlin.test.fail - -class OpenDbTest { - @Test - fun openingDbWithDifferentParametersFails() { - val baseDir = "build/testDbs" - HostFileSystem.deleteRecursively(baseDir.toPath()) - HostFileSystem.createDirectories(baseDir.toPath()) - createCacheFactory(baseDir, false).create() - try { - createCacheFactory(baseDir, true).create() - fail("an exception was expected") - } catch (e: Exception) { - assertTrue(e.message?.contains("did you change the 'withDates' parameter?") ?: false) - } - } -} - -expect fun createCacheFactory(baseDir: String, withDates: Boolean): SqlNormalizedCacheFactory diff --git a/normalized-cache-sqlite-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheTest.kt b/normalized-cache-sqlite-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheTest.kt index cadc0d5c..7b6c24a4 100644 --- a/normalized-cache-sqlite-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheTest.kt +++ b/normalized-cache-sqlite-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheTest.kt @@ -13,8 +13,8 @@ import com.apollographql.cache.normalized.api.CacheKey import com.apollographql.cache.normalized.api.DefaultRecordMerger import com.apollographql.cache.normalized.api.NormalizedCache import com.apollographql.cache.normalized.api.Record -import com.apollographql.cache.normalized.sql.internal.JsonRecordDatabase -import com.apollographql.cache.normalized.sql.internal.json.JsonQueries +import com.apollographql.cache.normalized.sql.internal.BlobRecordDatabase +import com.apollographql.cache.normalized.sql.internal.blob.BlobQueries import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals @@ -24,7 +24,7 @@ import kotlin.test.assertTrue class SqlNormalizedCacheTest { - private val cache: NormalizedCache = SqlNormalizedCacheFactory(null, false).create() + private val cache: NormalizedCache = SqlNormalizedCacheFactory().create() @BeforeTest fun setUp() { @@ -227,7 +227,7 @@ class SqlNormalizedCacheTest { @Test fun exceptionCallsExceptionHandler() { - val badCache = SqlNormalizedCache(JsonRecordDatabase(JsonQueries(BadDriver))) + val badCache = SqlNormalizedCache(BlobRecordDatabase(BlobQueries(BadDriver))) var throwable: Throwable? = null apolloExceptionHandler = { throwable = it diff --git a/normalized-cache-sqlite-incubating/src/jvmMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactoryJvm.kt b/normalized-cache-sqlite-incubating/src/jvmMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactoryJvm.kt index 056b88fd..1097933f 100644 --- a/normalized-cache-sqlite-incubating/src/jvmMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactoryJvm.kt +++ b/normalized-cache-sqlite-incubating/src/jvmMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactoryJvm.kt @@ -11,32 +11,28 @@ import java.util.Properties actual class SqlNormalizedCacheFactory actual constructor( private val driver: SqlDriver, - private val withDates: Boolean, ) : NormalizedCacheFactory() { /** * @param url Database connection URL in the form of `jdbc:sqlite:path` where `path` is either blank * (creating an in-memory database) or a path to a file. * @param properties */ - @JvmOverloads constructor( url: String, properties: Properties = Properties(), - withDates: Boolean = false, - ) : this(JdbcSqliteDriver(url, properties), withDates) + ) : this(JdbcSqliteDriver(url, properties)) /** * @param name the name of the database or null for an in-memory database - * @param withDates whether to account for dates in the database. * @param baseDir the baseDirectory where to store the database. * If [baseDir] does not exist, it will be created * If [baseDir] is a relative path, it will be interpreted relative to the current working directory */ - constructor(name: String?, withDates: Boolean, baseDir: String?) : this(createDriver(name, baseDir, getSchema(withDates)), withDates) - actual constructor(name: String?, withDates: Boolean) : this(name, withDates, null) + constructor(name: String?, baseDir: String?) : this(createDriver(name, baseDir, getSchema())) + actual constructor(name: String?) : this(name, null) actual override fun create(): NormalizedCache { - return SqlNormalizedCache(createRecordDatabase(driver, withDates)) + return SqlNormalizedCache(createRecordDatabase(driver)) } } diff --git a/normalized-cache-sqlite-incubating/src/jvmTest/kotlin/com/apollographql/cache/normalized/sql/TrimTest.kt b/normalized-cache-sqlite-incubating/src/jvmTest/kotlin/com/apollographql/cache/normalized/sql/TrimTest.kt index bbe8e700..86079157 100644 --- a/normalized-cache-sqlite-incubating/src/jvmTest/kotlin/com/apollographql/cache/normalized/sql/TrimTest.kt +++ b/normalized-cache-sqlite-incubating/src/jvmTest/kotlin/com/apollographql/cache/normalized/sql/TrimTest.kt @@ -25,7 +25,7 @@ class TrimTest { key = "old", fields = mapOf("key" to "value"), mutationId = null, - date = mapOf("key" to 0L), + dates = mapOf("key" to 0L), metadata = emptyMap() ) cache.merge(oldRecord, CacheHeaders.NONE, recordMerger = DefaultRecordMerger) @@ -35,7 +35,7 @@ class TrimTest { key = "new$it", fields = mapOf("key" to largeString), mutationId = null, - date = mapOf("key" to 1 + it.toLong()), + dates = mapOf("key" to 1 + it.toLong()), metadata = emptyMap() ) } diff --git a/normalized-cache-sqlite-incubating/src/jvmTest/kotlin/com/apollographql/cache/normalized/sql/createCacheFactory.kt b/normalized-cache-sqlite-incubating/src/jvmTest/kotlin/com/apollographql/cache/normalized/sql/createCacheFactory.kt deleted file mode 100644 index 0adae2e0..00000000 --- a/normalized-cache-sqlite-incubating/src/jvmTest/kotlin/com/apollographql/cache/normalized/sql/createCacheFactory.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.apollographql.cache.normalized.sql - -actual fun createCacheFactory( - baseDir: String, - withDates: Boolean, -): SqlNormalizedCacheFactory { - return SqlNormalizedCacheFactory(name = "apolloTestDb", withDates, baseDir) -} diff --git a/tests/sqlite/build.gradle.kts b/tests/expiration/build.gradle.kts similarity index 100% rename from tests/sqlite/build.gradle.kts rename to tests/expiration/build.gradle.kts diff --git a/tests/sqlite/src/commonMain/graphql/operations.graphql b/tests/expiration/src/commonMain/graphql/operations.graphql similarity index 100% rename from tests/sqlite/src/commonMain/graphql/operations.graphql rename to tests/expiration/src/commonMain/graphql/operations.graphql diff --git a/tests/sqlite/src/commonMain/graphql/schema.graphqls b/tests/expiration/src/commonMain/graphql/schema.graphqls similarity index 100% rename from tests/sqlite/src/commonMain/graphql/schema.graphqls rename to tests/expiration/src/commonMain/graphql/schema.graphqls diff --git a/tests/sqlite/src/commonTest/kotlin/ExpirationTest.kt b/tests/expiration/src/commonTest/kotlin/ClientSideExpirationTest.kt similarity index 58% rename from tests/sqlite/src/commonTest/kotlin/ExpirationTest.kt rename to tests/expiration/src/commonTest/kotlin/ClientSideExpirationTest.kt index a6d716f6..8ad44eb4 100644 --- a/tests/sqlite/src/commonTest/kotlin/ExpirationTest.kt +++ b/tests/expiration/src/commonTest/kotlin/ClientSideExpirationTest.kt @@ -1,7 +1,6 @@ package test import com.apollographql.apollo.ApolloClient -import com.apollographql.apollo.api.ApolloResponse import com.apollographql.apollo.api.CustomScalarAdapters import com.apollographql.apollo.exception.CacheMissException import com.apollographql.apollo.mpp.currentTimeMillis @@ -11,7 +10,8 @@ import com.apollographql.cache.normalized.api.ApolloCacheHeaders import com.apollographql.cache.normalized.api.CacheHeaders import com.apollographql.cache.normalized.api.DefaultRecordMerger import com.apollographql.cache.normalized.api.EmptyMetadataGenerator -import com.apollographql.cache.normalized.api.ExpireDateCacheResolver +import com.apollographql.cache.normalized.api.MemoryCacheFactory +import com.apollographql.cache.normalized.api.NormalizedCacheFactory import com.apollographql.cache.normalized.api.ReceiveDateCacheResolver import com.apollographql.cache.normalized.api.TypePolicyCacheKeyGenerator import com.apollographql.cache.normalized.api.normalize @@ -20,22 +20,32 @@ import com.apollographql.cache.normalized.cacheHeaders import com.apollographql.cache.normalized.fetchPolicy import com.apollographql.cache.normalized.normalizedCache import com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory -import com.apollographql.cache.normalized.storeExpirationDate import com.apollographql.cache.normalized.storeReceiveDate -import com.apollographql.mockserver.MockResponse -import com.apollographql.mockserver.MockServer import sqlite.GetUserQuery import kotlin.test.Test import kotlin.test.assertTrue -@Suppress("JoinDeclarationAndAssignment") -class ExpirationTest { +class ClientSideExpirationTest { @Test - fun clientSideExpiration() = runTest { + fun memoryCache() { + test(MemoryCacheFactory()) + } + + @Test + fun sqlCache() { + test(SqlNormalizedCacheFactory()) + } + + @Test + fun chainedCache() { + test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory())) + } + + private fun test(normalizedCacheFactory: NormalizedCacheFactory) = runTest { val maxAge = 10 val client = ApolloClient.Builder() .normalizedCache( - normalizedCacheFactory = SqlNormalizedCacheFactory(name = null, withDates = true), + normalizedCacheFactory = normalizedCacheFactory, cacheKeyGenerator = TypePolicyCacheKeyGenerator, cacheResolver = ReceiveDateCacheResolver(maxAge), recordMerger = DefaultRecordMerger, @@ -72,58 +82,6 @@ class ExpirationTest { assertTrue(response2.data?.user?.name == "John") } - @Test - fun serverSideExpiration() = runTest { - val mockServer = MockServer() - val client = ApolloClient.Builder() - .normalizedCache( - normalizedCacheFactory = SqlNormalizedCacheFactory(name = null, withDates = true), - cacheKeyGenerator = TypePolicyCacheKeyGenerator, - cacheResolver = ExpireDateCacheResolver() - ) - .storeExpirationDate(true) - .serverUrl(mockServer.url()) - .build() - val query = GetUserQuery() - val data = """ - { - "data": { - "user": { - "name": "John", - "email": "john@doe.com" - } - } - } - """.trimIndent() - - val response: ApolloResponse - - // store data with an expiration date in the future - mockServer.enqueue( - MockResponse.Builder() - .addHeader("Cache-Control", "max-age=10") - .body(data) - .build() - ) - client.query(query).fetchPolicy(FetchPolicy.NetworkOnly).execute() - // read from cache -> it should succeed - response = client.query(query).fetchPolicy(FetchPolicy.CacheOnly).execute() - assertTrue(response.data?.user?.name == "John") - - // store expired data - mockServer.enqueue( - MockResponse.Builder() - .addHeader("Cache-Control", "max-age=0") - .body(data) - .build() - ) - client.query(query).fetchPolicy(FetchPolicy.NetworkOnly).execute() - // read from cache -> it should fail - val e = client.query(GetUserQuery()).fetchPolicy(FetchPolicy.CacheOnly).execute().exception as CacheMissException - assertTrue(e.stale) - } - - private fun cacheHeaders(date: Long): CacheHeaders { return CacheHeaders.Builder().addHeader(ApolloCacheHeaders.DATE, date.toString()).build() } diff --git a/tests/expiration/src/commonTest/kotlin/ServerSideExpirationTest.kt b/tests/expiration/src/commonTest/kotlin/ServerSideExpirationTest.kt new file mode 100644 index 00000000..39bee25e --- /dev/null +++ b/tests/expiration/src/commonTest/kotlin/ServerSideExpirationTest.kt @@ -0,0 +1,88 @@ +package test + +import com.apollographql.apollo.ApolloClient +import com.apollographql.apollo.api.ApolloResponse +import com.apollographql.apollo.exception.CacheMissException +import com.apollographql.apollo.testing.internal.runTest +import com.apollographql.cache.normalized.FetchPolicy +import com.apollographql.cache.normalized.api.ExpireDateCacheResolver +import com.apollographql.cache.normalized.api.MemoryCacheFactory +import com.apollographql.cache.normalized.api.NormalizedCacheFactory +import com.apollographql.cache.normalized.api.TypePolicyCacheKeyGenerator +import com.apollographql.cache.normalized.fetchPolicy +import com.apollographql.cache.normalized.normalizedCache +import com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory +import com.apollographql.cache.normalized.storeExpirationDate +import com.apollographql.mockserver.MockResponse +import com.apollographql.mockserver.MockServer +import sqlite.GetUserQuery +import kotlin.test.Test +import kotlin.test.assertTrue + +class ServerSideExpirationTest { + @Test + fun memoryCache() { + test(MemoryCacheFactory()) + } + + @Test + fun sqlCache() { + test(SqlNormalizedCacheFactory()) + } + + @Test + fun chainedCache() { + test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory())) + } + + @Suppress("JoinDeclarationAndAssignment") + private fun test(normalizedCacheFactory: NormalizedCacheFactory) = runTest { + val mockServer = MockServer() + val client = ApolloClient.Builder() + .normalizedCache( + normalizedCacheFactory = normalizedCacheFactory, + cacheKeyGenerator = TypePolicyCacheKeyGenerator, + cacheResolver = ExpireDateCacheResolver() + ) + .storeExpirationDate(true) + .serverUrl(mockServer.url()) + .build() + val query = GetUserQuery() + val data = """ + { + "data": { + "user": { + "name": "John", + "email": "john@doe.com" + } + } + } + """.trimIndent() + + val response: ApolloResponse + + // store data with an expiration date in the future + mockServer.enqueue( + MockResponse.Builder() + .addHeader("Cache-Control", "max-age=10") + .body(data) + .build() + ) + client.query(query).fetchPolicy(FetchPolicy.NetworkOnly).execute() + // read from cache -> it should succeed + response = client.query(query).fetchPolicy(FetchPolicy.CacheOnly).execute() + assertTrue(response.data?.user?.name == "John") + + // store expired data + mockServer.enqueue( + MockResponse.Builder() + .addHeader("Cache-Control", "max-age=0") + .body(data) + .build() + ) + client.query(query).fetchPolicy(FetchPolicy.NetworkOnly).execute() + // read from cache -> it should fail + val e = client.query(GetUserQuery()).fetchPolicy(FetchPolicy.CacheOnly).execute().exception as CacheMissException + assertTrue(e.stale) + } +} diff --git a/tests/pagination/src/commonTest/kotlin/ConnectionPaginationTest.kt b/tests/pagination/src/commonTest/kotlin/ConnectionPaginationTest.kt index 1d72c24a..9716ff73 100644 --- a/tests/pagination/src/commonTest/kotlin/ConnectionPaginationTest.kt +++ b/tests/pagination/src/commonTest/kotlin/ConnectionPaginationTest.kt @@ -26,18 +26,13 @@ class ConnectionPaginationTest { } @Test - fun blobSqlCache() { - test(SqlNormalizedCacheFactory(name = "blob", withDates = true)) - } - - @Test - fun jsonSqlCache() { - test(SqlNormalizedCacheFactory(name = "json", withDates = false)) + fun sqlCache() { + test(SqlNormalizedCacheFactory()) } @Test fun chainedCache() { - test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory(name = "json", withDates = false))) + test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory())) } private fun test(cacheFactory: NormalizedCacheFactory) = runTest { diff --git a/tests/pagination/src/commonTest/kotlin/ConnectionProgrammaticPaginationTest.kt b/tests/pagination/src/commonTest/kotlin/ConnectionProgrammaticPaginationTest.kt index cd162991..634c6350 100644 --- a/tests/pagination/src/commonTest/kotlin/ConnectionProgrammaticPaginationTest.kt +++ b/tests/pagination/src/commonTest/kotlin/ConnectionProgrammaticPaginationTest.kt @@ -29,18 +29,13 @@ class ConnectionProgrammaticPaginationTest { } @Test - fun blobSqlCache() { - test(SqlNormalizedCacheFactory(name = "blob", withDates = true)) - } - - @Test - fun jsonSqlCache() { - test(SqlNormalizedCacheFactory(name = "json", withDates = false)) + fun sqlCache() { + test(SqlNormalizedCacheFactory()) } @Test fun chainedCache() { - test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory(name = "json", withDates = false))) + test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory())) } private fun test(cacheFactory: NormalizedCacheFactory) = runTest { diff --git a/tests/pagination/src/commonTest/kotlin/ConnectionWithNodesPaginationTest.kt b/tests/pagination/src/commonTest/kotlin/ConnectionWithNodesPaginationTest.kt index a543d2f5..e3aa8817 100644 --- a/tests/pagination/src/commonTest/kotlin/ConnectionWithNodesPaginationTest.kt +++ b/tests/pagination/src/commonTest/kotlin/ConnectionWithNodesPaginationTest.kt @@ -25,18 +25,13 @@ class ConnectionWithNodesPaginationTest { } @Test - fun blobSqlCache() { - test(SqlNormalizedCacheFactory(name = "blob", withDates = true)) - } - - @Test - fun jsonSqlCache() { - test(SqlNormalizedCacheFactory(name = "json", withDates = false)) + fun sqlCache() { + test(SqlNormalizedCacheFactory()) } @Test fun chainedCache() { - test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory(name = "json", withDates = false))) + test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory())) } private fun test(cacheFactory: NormalizedCacheFactory) = runTest { diff --git a/tests/pagination/src/commonTest/kotlin/CursorBasedPaginationTest.kt b/tests/pagination/src/commonTest/kotlin/CursorBasedPaginationTest.kt index 57d6235f..a37dec46 100644 --- a/tests/pagination/src/commonTest/kotlin/CursorBasedPaginationTest.kt +++ b/tests/pagination/src/commonTest/kotlin/CursorBasedPaginationTest.kt @@ -33,18 +33,13 @@ class CursorBasedPaginationTest { } @Test - fun blobSqlCache() { - test(SqlNormalizedCacheFactory(name = "blob", withDates = true)) - } - - @Test - fun jsonSqlCache() { - test(SqlNormalizedCacheFactory(name = "json", withDates = false)) + fun sqlCache() { + test(SqlNormalizedCacheFactory()) } @Test fun chainedCache() { - test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory(name = "json", withDates = false))) + test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory())) } private fun test(cacheFactory: NormalizedCacheFactory) = runTest { diff --git a/tests/pagination/src/commonTest/kotlin/OffsetBasedWithArrayPaginationTest.kt b/tests/pagination/src/commonTest/kotlin/OffsetBasedWithArrayPaginationTest.kt index 0d6375de..557a24c1 100644 --- a/tests/pagination/src/commonTest/kotlin/OffsetBasedWithArrayPaginationTest.kt +++ b/tests/pagination/src/commonTest/kotlin/OffsetBasedWithArrayPaginationTest.kt @@ -26,18 +26,13 @@ class OffsetBasedWithArrayPaginationTest { } @Test - fun blobSqlCache() { - test(SqlNormalizedCacheFactory(name = "blob", withDates = true)) - } - - @Test - fun jsonSqlCache() { - test(SqlNormalizedCacheFactory(name = "json", withDates = false)) + fun sqlCache() { + test(SqlNormalizedCacheFactory()) } @Test fun chainedCache() { - test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory(name = "json", withDates = false))) + test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory())) } private fun test(cacheFactory: NormalizedCacheFactory) = runTest { diff --git a/tests/pagination/src/commonTest/kotlin/OffsetBasedWithPageAndInputPaginationTest.kt b/tests/pagination/src/commonTest/kotlin/OffsetBasedWithPageAndInputPaginationTest.kt index b37005ba..905bb261 100644 --- a/tests/pagination/src/commonTest/kotlin/OffsetBasedWithPageAndInputPaginationTest.kt +++ b/tests/pagination/src/commonTest/kotlin/OffsetBasedWithPageAndInputPaginationTest.kt @@ -33,18 +33,13 @@ class OffsetBasedWithPageAndInputPaginationTest { } @Test - fun blobSqlCache() { - test(SqlNormalizedCacheFactory(name = "blob", withDates = true)) - } - - @Test - fun jsonSqlCache() { - test(SqlNormalizedCacheFactory(name = "json", withDates = false)) + fun sqlCache() { + test(SqlNormalizedCacheFactory()) } @Test fun chainedCache() { - test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory(name = "json", withDates = false))) + test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory())) } private fun test(cacheFactory: NormalizedCacheFactory) = runTest { diff --git a/tests/pagination/src/commonTest/kotlin/OffsetBasedWithPagePaginationTest.kt b/tests/pagination/src/commonTest/kotlin/OffsetBasedWithPagePaginationTest.kt index 39e7488c..53e84634 100644 --- a/tests/pagination/src/commonTest/kotlin/OffsetBasedWithPagePaginationTest.kt +++ b/tests/pagination/src/commonTest/kotlin/OffsetBasedWithPagePaginationTest.kt @@ -29,18 +29,13 @@ class OffsetBasedWithPagePaginationTest { } @Test - fun blobSqlCache() { - test(SqlNormalizedCacheFactory(name = "blob", withDates = true)) - } - - @Test - fun jsonSqlCache() { - test(SqlNormalizedCacheFactory(name = "json", withDates = false)) + fun sqlCache() { + test(SqlNormalizedCacheFactory()) } @Test fun chainedCache() { - test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory(name = "json", withDates = false))) + test(MemoryCacheFactory().chain(SqlNormalizedCacheFactory())) } private fun test(cacheFactory: NormalizedCacheFactory) = runTest { @@ -246,7 +241,7 @@ internal fun assertChainedCachesAreEqual(apolloStore: ApolloStore) { val record2 = cache2[key]!! assertEquals(record1.key, record2.key) assertEquals(record1.fields, record2.fields) - assertEquals(record1.dates, record2.dates) + assertEquals(record1.dates.filterValues { it != null }, record2.dates.filterValues { it != null }) assertEquals(record1.metadata, record2.metadata) } } From 5224b3ccd0e09fb92f1dae1ff8070cdcc318fe47 Mon Sep 17 00:00:00 2001 From: BoD Date: Fri, 12 Jul 2024 19:58:24 +0200 Subject: [PATCH 2/6] Remove JsonRecordSerializer --- .../api/internal/JsonRecordSerializer.kt | 111 ------------------ .../normalized/JsonRecordSerializerTest.kt | 65 ---------- 2 files changed, 176 deletions(-) delete mode 100644 normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/internal/JsonRecordSerializer.kt delete mode 100644 normalized-cache-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/JsonRecordSerializerTest.kt diff --git a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/internal/JsonRecordSerializer.kt b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/internal/JsonRecordSerializer.kt deleted file mode 100644 index 2b85e47f..00000000 --- a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/api/internal/JsonRecordSerializer.kt +++ /dev/null @@ -1,111 +0,0 @@ -package com.apollographql.cache.normalized.api.internal - -import com.apollographql.apollo.annotations.ApolloInternal -import com.apollographql.apollo.api.json.BufferedSinkJsonWriter -import com.apollographql.apollo.api.json.BufferedSourceJsonReader -import com.apollographql.apollo.api.json.JsonWriter -import com.apollographql.apollo.api.json.readAny -import com.apollographql.cache.normalized.api.CacheKey -import com.apollographql.cache.normalized.api.Record -import okio.Buffer -import okio.ByteString.Companion.encodeUtf8 -import okio.use - -/** - * An adapter used to serialize and deserialize Record fields. Record object types will be serialized to [CacheKey]. - */ -@ApolloInternal -object JsonRecordSerializer { - // "apm" stands for Apollo Metadata (avoiding "__metadata" for potential clashes) - private const val KEY_METADATA = "__apm" - - fun serialize(record: Record): String { - return toJson(record) - } - - private fun toJson(record: Record): String { - val buffer = Buffer() - BufferedSinkJsonWriter(buffer).use { jsonWriter -> - jsonWriter.beginObject() - for ((key, value) in record.fields) { - jsonWriter.name(key).writeJsonValue(value) - } - jsonWriter.name(KEY_METADATA) - jsonWriter.writeJsonValue(record.metadata) - jsonWriter.endObject() - } - return buffer.readUtf8() - } - - private fun Any?.deserializeCacheKeys(): Any? { - return when (this) { - is String -> if (CacheKey.canDeserialize(this)) { - CacheKey.deserialize(this) - } else { - this - } - - is Map<*, *> -> mapValues { - it.value.deserializeCacheKeys() - } - - is List<*> -> map { it.deserializeCacheKeys() } - else -> this - } - } - - /** - * returns the [Record] for the given Json - * - * @throws if the [Record] cannot be deserialized - */ - @Suppress("UNCHECKED_CAST") - fun deserialize(key: String, jsonFieldSource: String): Record { - val buffer = Buffer().write(jsonFieldSource.encodeUtf8()) - - val allFields = BufferedSourceJsonReader(buffer).readAny() as Map - val fields = allFields - .filterKeys { it != KEY_METADATA } - .deserializeCacheKeys() as? Map - - check(fields != null) { - "error deserializing: $jsonFieldSource" - } - - return Record( - key = key, - fields = fields, - mutationId = null, - dates = emptyMap(), - metadata = allFields[KEY_METADATA] as Map> - ) - } - - @Suppress("UNCHECKED_CAST") - private fun JsonWriter.writeJsonValue(value: Any?) { - when (value) { - null -> this.nullValue() - is String -> this.value(value) - is Boolean -> this.value(value) - is Int -> this.value(value) - is Long -> this.value(value) - is Double -> this.value(value) - is CacheKey -> this.value(value.serialize()) - is List<*> -> { - this.beginArray() - value.forEach { writeJsonValue(it) } - this.endArray() - } - - is Map<*, *> -> { - this.beginObject() - for (entry in value as Map) { - this.name(entry.key).writeJsonValue(entry.value) - } - this.endObject() - } - - else -> error("Unsupported record value type: '$value'") - } - } -} diff --git a/normalized-cache-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/JsonRecordSerializerTest.kt b/normalized-cache-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/JsonRecordSerializerTest.kt deleted file mode 100644 index c8c16bce..00000000 --- a/normalized-cache-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/JsonRecordSerializerTest.kt +++ /dev/null @@ -1,65 +0,0 @@ -package com.apollographql.cache.normalized - -import com.apollographql.cache.normalized.api.CacheKey -import com.apollographql.cache.normalized.api.Record -import com.apollographql.cache.normalized.api.internal.JsonRecordSerializer -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertNull -import kotlin.test.assertTrue - -class JsonRecordSerializerTest { - - @Test - fun testFieldsAdapterSerializationDeserialization() { - val expectedDouble = "1.23" - val expectedStringValue = "StringValue" - val expectedBooleanValue = true - val expectedCacheKey = CacheKey("foo") - val expectedCacheKeyList = listOf(CacheKey("bar"), CacheKey("baz")) - val expectedScalarList = listOf("scalarOne", "scalarTwo") - val expectedListOfScalarList = listOf(listOf("scalarOne", "scalarTwo")) - val expectedMapKey = "foo" - val expectedMapValue = "bar" - val expectedMap = mapOf(expectedMapKey to expectedMapValue) - val expectLongValue: Long = 1 - val record = Record( - key = "root", - fields = mapOf( - "double" to expectedDouble, - "string" to expectedStringValue, - "boolean" to expectedBooleanValue, - "cacheReference" to expectedCacheKey, - "scalarList" to expectedScalarList, - "referenceList" to expectedCacheKeyList, - "nullValue" to null, - "listOfScalarList" to expectedListOfScalarList, - "map" to expectedMap, - "long" to expectLongValue - ) - ) - - val json = JsonRecordSerializer.serialize(record) - val deserializedMap = requireNotNull(JsonRecordSerializer.deserialize(record.key, json)) - - assertEquals(actual = deserializedMap["double"], expected = expectedDouble) - assertEquals(actual = deserializedMap["string"], expected = expectedStringValue) - assertEquals(actual = deserializedMap["boolean"], expected = expectedBooleanValue) - assertEquals(actual = deserializedMap["cacheReference"], expected = expectedCacheKey) - assertEquals(actual = deserializedMap["scalarList"], expected = expectedScalarList) - assertEquals(actual = deserializedMap["referenceList"], expected = expectedCacheKeyList) - assertTrue { deserializedMap.containsKey("nullValue") } - assertNull(deserializedMap["nullValue"]) - assertEquals(actual = (deserializedMap["listOfScalarList"] as List<*>).size, expected = 1) - assertEquals( - actual = (deserializedMap["listOfScalarList"] as List<*>)[0] as Iterable<*>?, - expected = expectedScalarList - ) - assertEquals(actual = (deserializedMap["map"] as Map<*, *>)[expectedMapKey], expected = expectedMapValue) - // The default deserialization algorithm will use the number type with the smallest possible width. - // This is OK as the generated parser know what to expect and will convert back to Long if needed. - // This test compares Strings to avoid a failure. - assertEquals(actual = deserializedMap["long"]?.toString(), expected = expectLongValue.toString()) - - } -} From fcf25cd38b6b099b0906d1bc4f89399adc6357a9 Mon Sep 17 00:00:00 2001 From: BoD Date: Mon, 15 Jul 2024 09:34:09 +0200 Subject: [PATCH 3/6] Update API dump --- .../api/normalized-cache-incubating.klib.api | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/normalized-cache-incubating/api/normalized-cache-incubating.klib.api b/normalized-cache-incubating/api/normalized-cache-incubating.klib.api index 611b4b18..aec381a0 100644 --- a/normalized-cache-incubating/api/normalized-cache-incubating.klib.api +++ b/normalized-cache-incubating/api/normalized-cache-incubating.klib.api @@ -265,7 +265,7 @@ final class com.apollographql.cache.normalized.api/Record : kotlin.collections/M final val values // com.apollographql.cache.normalized.api/Record.values|{}values[0] final fun (): kotlin.collections/Collection // com.apollographql.cache.normalized.api/Record.values.|(){}[0] final var dates // com.apollographql.cache.normalized.api/Record.dates|{}dates[0] - final fun (): kotlin.collections/Map? // com.apollographql.cache.normalized.api/Record.dates.|(){}[0] + final fun (): kotlin.collections/Map // com.apollographql.cache.normalized.api/Record.dates.|(){}[0] final var metadata // com.apollographql.cache.normalized.api/Record.metadata|{}metadata[0] final fun (): kotlin.collections/Map> // com.apollographql.cache.normalized.api/Record.metadata.|(){}[0] // Targets: [js] @@ -382,10 +382,6 @@ final object com.apollographql.cache.normalized.api.internal/BlobRecordSerialize final fun deserialize(kotlin/String, kotlin/ByteArray): com.apollographql.cache.normalized.api/Record // com.apollographql.cache.normalized.api.internal/BlobRecordSerializer.deserialize|deserialize(kotlin.String;kotlin.ByteArray){}[0] final fun serialize(com.apollographql.cache.normalized.api/Record): kotlin/ByteArray // com.apollographql.cache.normalized.api.internal/BlobRecordSerializer.serialize|serialize(com.apollographql.cache.normalized.api.Record){}[0] } -final object com.apollographql.cache.normalized.api.internal/JsonRecordSerializer { // com.apollographql.cache.normalized.api.internal/JsonRecordSerializer|null[0] - final fun deserialize(kotlin/String, kotlin/String): com.apollographql.cache.normalized.api/Record // com.apollographql.cache.normalized.api.internal/JsonRecordSerializer.deserialize|deserialize(kotlin.String;kotlin.String){}[0] - final fun serialize(com.apollographql.cache.normalized.api/Record): kotlin/String // com.apollographql.cache.normalized.api.internal/JsonRecordSerializer.serialize|serialize(com.apollographql.cache.normalized.api.Record){}[0] -} final object com.apollographql.cache.normalized.api/ApolloCacheHeaders { // com.apollographql.cache.normalized.api/ApolloCacheHeaders|null[0] final const val DATE // com.apollographql.cache.normalized.api/ApolloCacheHeaders.DATE|{}DATE[0] final fun (): kotlin/String // com.apollographql.cache.normalized.api/ApolloCacheHeaders.DATE.|(){}[0] From 41cfeeb5f57067c568f57d8f78288e81c302e3b2 Mon Sep 17 00:00:00 2001 From: BoD Date: Mon, 15 Jul 2024 09:44:29 +0200 Subject: [PATCH 4/6] Update API dump again --- .../api/normalized-cache-incubating.api | 6 ------ 1 file changed, 6 deletions(-) diff --git a/normalized-cache-incubating/api/normalized-cache-incubating.api b/normalized-cache-incubating/api/normalized-cache-incubating.api index a4ff4007..f35bccf1 100644 --- a/normalized-cache-incubating/api/normalized-cache-incubating.api +++ b/normalized-cache-incubating/api/normalized-cache-incubating.api @@ -479,12 +479,6 @@ public final class com/apollographql/cache/normalized/api/internal/BlobRecordSer public final fun serialize (Lcom/apollographql/cache/normalized/api/Record;)[B } -public final class com/apollographql/cache/normalized/api/internal/JsonRecordSerializer { - public static final field INSTANCE Lcom/apollographql/cache/normalized/api/internal/JsonRecordSerializer; - public final fun deserialize (Ljava/lang/String;Ljava/lang/String;)Lcom/apollographql/cache/normalized/api/Record; - public final fun serialize (Lcom/apollographql/cache/normalized/api/Record;)Ljava/lang/String; -} - public final class com/apollographql/cache/normalized/api/internal/Lock { public fun ()V public final fun read (Lkotlin/jvm/functions/Function0;)Ljava/lang/Object; From af2ef8d0935ffe14700fb8f0b737698de2838a51 Mon Sep 17 00:00:00 2001 From: BoD Date: Mon, 15 Jul 2024 09:58:47 +0200 Subject: [PATCH 5/6] Empty map takes 16 more bytes --- .../com/apollographql/cache/normalized/RecordWeigherTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/normalized-cache-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/RecordWeigherTest.kt b/normalized-cache-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/RecordWeigherTest.kt index d7fcd8bb..f4085d8e 100644 --- a/normalized-cache-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/RecordWeigherTest.kt +++ b/normalized-cache-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/RecordWeigherTest.kt @@ -29,7 +29,7 @@ class RecordWeigherTest { ) ) - assertTrue(record.sizeInBytes <= 250) - assertTrue(record.sizeInBytes >= 246) // JS takes less space, maybe for strings? + assertTrue(record.sizeInBytes <= 264) + assertTrue(record.sizeInBytes >= 260) // JS takes less space, maybe for strings? } } From 4b7675bbec6c067691a65466cdfdc2dcfb7939e5 Mon Sep 17 00:00:00 2001 From: BoD Date: Mon, 15 Jul 2024 10:27:04 +0200 Subject: [PATCH 6/6] Fix size in JS --- .../com/apollographql/cache/normalized/RecordWeigherTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/normalized-cache-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/RecordWeigherTest.kt b/normalized-cache-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/RecordWeigherTest.kt index f4085d8e..3e93ea05 100644 --- a/normalized-cache-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/RecordWeigherTest.kt +++ b/normalized-cache-incubating/src/commonTest/kotlin/com/apollographql/cache/normalized/RecordWeigherTest.kt @@ -30,6 +30,6 @@ class RecordWeigherTest { ) assertTrue(record.sizeInBytes <= 264) - assertTrue(record.sizeInBytes >= 260) // JS takes less space, maybe for strings? + assertTrue(record.sizeInBytes >= 258) // JS takes less space, maybe for strings? } }