From 6b37c5315433186ae8008bf3de66876a65a8bf6a Mon Sep 17 00:00:00 2001 From: mramotar_dbx Date: Wed, 18 Oct 2023 18:24:09 -0400 Subject: [PATCH] Move StoreKey and StoreData to core Signed-off-by: mramotar_dbx --- cache/build.gradle.kts | 1 + .../store/cache5/Identifiable.kt | 1 + .../store/cache5/MultiCache.kt | 1 + .../store/cache5/StoreMultiCache.kt | 45 +++++++++++-------- .../store/cache5/StoreMultiCacheAccessor.kt | 34 +++++++------- core/build.gradle.kts | 40 +++++++++++++++++ .../store/core5}/InsertionStrategy.kt | 2 +- .../store/core5}/KeyProvider.kt | 2 +- .../store/core5}/StoreData.kt | 2 +- .../store/core5}/StoreKey.kt | 2 +- paging/build.gradle.kts | 3 +- .../store/paging5/LaunchPagingStore.kt | 2 + .../store/paging5/util/PostData.kt | 4 +- .../store/paging5/util/PostKey.kt | 4 +- .../store/paging5/util/PostStoreFactory.kt | 8 ++-- settings.gradle | 3 +- store/build.gradle.kts | 1 + 17 files changed, 105 insertions(+), 50 deletions(-) rename paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/PagingCache.kt => cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCache.kt (73%) rename paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/PagingCacheAccessor.kt => cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCacheAccessor.kt (65%) create mode 100644 core/build.gradle.kts rename {paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5 => core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5}/InsertionStrategy.kt (53%) rename {paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5 => core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5}/KeyProvider.kt (82%) rename {paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5 => core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5}/StoreData.kt (95%) rename {paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5 => core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5}/StoreKey.kt (96%) diff --git a/cache/build.gradle.kts b/cache/build.gradle.kts index 15527d9dd..b5d635478 100644 --- a/cache/build.gradle.kts +++ b/cache/build.gradle.kts @@ -44,6 +44,7 @@ kotlin { val commonMain by getting { dependencies { api(libs.kotlinx.atomic.fu) + api(project(":core")) } } val jvmMain by getting diff --git a/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/Identifiable.kt b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/Identifiable.kt index d523bfb67..457a3ce9f 100644 --- a/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/Identifiable.kt +++ b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/Identifiable.kt @@ -1,5 +1,6 @@ package org.mobilenativefoundation.store.cache5 +@Deprecated("Use StoreMultiCache instead of MultiCache") interface Identifiable { val id: Id } diff --git a/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/MultiCache.kt b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/MultiCache.kt index 3dfc0b561..c3dd43562 100644 --- a/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/MultiCache.kt +++ b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/MultiCache.kt @@ -7,6 +7,7 @@ package org.mobilenativefoundation.store.cache5 * Stores and manages the relationship among single items and collections. * Delegates cache storage and behavior to Guava caches. */ +@Deprecated("Use StoreMultiCache") class MultiCache>( cacheBuilder: CacheBuilder ) { diff --git a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/PagingCache.kt b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCache.kt similarity index 73% rename from paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/PagingCache.kt rename to cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCache.kt index 5455e525f..b334d8ec4 100644 --- a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/PagingCache.kt +++ b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCache.kt @@ -1,20 +1,27 @@ @file:Suppress("UNCHECKED_CAST") -package org.mobilenativefoundation.store.paging5 +package org.mobilenativefoundation.store.cache5 -import org.mobilenativefoundation.store.cache5.Cache +import org.mobilenativefoundation.store.core5.KeyProvider +import org.mobilenativefoundation.store.core5.StoreData +import org.mobilenativefoundation.store.core5.StoreKey /** - * A class that represents a caching system for pagination. + * A class that represents a caching system with collection decomposition. * Manages data with utility functions to get, invalidate, and add items to the cache. - * Depends on [PagingCacheAccessor] for internal data management. + * Depends on [StoreMultiCacheAccessor] for internal data management. * @see [Cache]. */ -class PagingCache, StoreOutput : StoreData, Collection : StoreData.Collection, Single : StoreData.Single>( +class StoreMultiCache, Single : StoreData.Single, Collection : StoreData.Collection, Output : StoreData>( private val keyProvider: KeyProvider, -) : Cache { + singlesCache: Cache, Single> = CacheBuilder, Single>().build(), + collectionsCache: Cache, Collection> = CacheBuilder, Collection>().build(), +) : Cache { - private val accessor = PagingCacheAccessor() + private val accessor = StoreMultiCacheAccessor( + singlesCache = singlesCache, + collectionsCache = collectionsCache, + ) private fun Key.castSingle() = this as StoreKey.Single private fun Key.castCollection() = this as StoreKey.Collection @@ -22,20 +29,20 @@ class PagingCache, StoreOutput : StoreData, Col private fun StoreKey.Collection.cast() = this as Key private fun StoreKey.Single.cast() = this as Key - override fun getIfPresent(key: Key): StoreOutput? { + override fun getIfPresent(key: Key): Output? { return when (key) { - is StoreKey.Single<*> -> accessor.getSingle(key.castSingle()) as? StoreOutput - is StoreKey.Collection<*> -> accessor.getCollection(key.castCollection()) as? StoreOutput + is StoreKey.Single<*> -> accessor.getSingle(key.castSingle()) as? Output + is StoreKey.Collection<*> -> accessor.getCollection(key.castCollection()) as? Output else -> { throw UnsupportedOperationException(invalidKeyErrorMessage(key)) } } } - override fun getOrPut(key: Key, valueProducer: () -> StoreOutput): StoreOutput { + override fun getOrPut(key: Key, valueProducer: () -> Output): Output { return when (key) { is StoreKey.Single<*> -> { - val single = accessor.getSingle(key.castSingle()) as? StoreOutput + val single = accessor.getSingle(key.castSingle()) as? Output if (single != null) { single } else { @@ -46,7 +53,7 @@ class PagingCache, StoreOutput : StoreData, Col } is StoreKey.Collection<*> -> { - val collection = accessor.getCollection(key.castCollection()) as? StoreOutput + val collection = accessor.getCollection(key.castCollection()) as? Output if (collection != null) { collection } else { @@ -62,18 +69,18 @@ class PagingCache, StoreOutput : StoreData, Col } } - override fun getAllPresent(keys: List<*>): Map { - val map = mutableMapOf() + override fun getAllPresent(keys: List<*>): Map { + val map = mutableMapOf() keys.filterIsInstance>().forEach { key -> when (key) { is StoreKey.Collection -> { val collection = accessor.getCollection(key) - collection?.let { map[key.cast()] = it as StoreOutput } + collection?.let { map[key.cast()] = it as Output } } is StoreKey.Single -> { val single = accessor.getSingle(key) - single?.let { map[key.cast()] = it as StoreOutput } + single?.let { map[key.cast()] = it as Output } } } } @@ -92,11 +99,11 @@ class PagingCache, StoreOutput : StoreData, Col } } - override fun putAll(map: Map) { + override fun putAll(map: Map) { map.entries.forEach { (key, value) -> put(key, value) } } - override fun put(key: Key, value: StoreOutput) { + override fun put(key: Key, value: Output) { when (key) { is StoreKey.Single<*> -> { val single = value as Single diff --git a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/PagingCacheAccessor.kt b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCacheAccessor.kt similarity index 65% rename from paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/PagingCacheAccessor.kt rename to cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCacheAccessor.kt index 23b531211..4d948c037 100644 --- a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/PagingCacheAccessor.kt +++ b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCacheAccessor.kt @@ -1,32 +1,34 @@ -package org.mobilenativefoundation.store.paging5 +package org.mobilenativefoundation.store.cache5 -import org.mobilenativefoundation.store.cache5.CacheBuilder +import org.mobilenativefoundation.store.core5.StoreData +import org.mobilenativefoundation.store.core5.StoreKey /** - * Intermediate data manager for a caching system supporting pagination. + * Intermediate data manager for a caching system supporting list decomposition. * Tracks keys for rapid data retrieval and modification. */ -class PagingCacheAccessor, Single : StoreData.Single> { - private val collections = CacheBuilder, Collection>().build() - private val singles = CacheBuilder, Single>().build() +class StoreMultiCacheAccessor, Single : StoreData.Single>( + private val singlesCache: Cache, Single>, + private val collectionsCache: Cache, Collection>, +) { private val keys = mutableSetOf>() /** * Retrieves a collection of items from the cache using the provided key. */ - fun getCollection(key: StoreKey.Collection): Collection? = collections.getIfPresent(key) + fun getCollection(key: StoreKey.Collection): Collection? = collectionsCache.getIfPresent(key) /** * Retrieves an individual item from the cache using the provided key. */ - fun getSingle(key: StoreKey.Single): Single? = singles.getIfPresent(key) + fun getSingle(key: StoreKey.Single): Single? = singlesCache.getIfPresent(key) /** * Stores a collection of items in the cache and updates the key set. */ fun putCollection(key: StoreKey.Collection, collection: Collection) { - collections.put(key, collection) + collectionsCache.put(key, collection) keys.add(key) } @@ -34,7 +36,7 @@ class PagingCacheAccessor, single: Single) { - singles.put(key, single) + singlesCache.put(key, single) keys.add(key) } @@ -42,8 +44,8 @@ class PagingCacheAccessor) { - singles.invalidate(key) + singlesCache.invalidate(key) keys.remove(key) } @@ -59,7 +61,7 @@ class PagingCacheAccessor) { - collections.invalidate(key) + collectionsCache.invalidate(key) keys.remove(key) } @@ -73,14 +75,14 @@ class PagingCacheAccessor -> { - val single = singles.getIfPresent(key) + val single = singlesCache.getIfPresent(key) if (single != null) { count++ } } is StoreKey.Collection -> { - val collection = collections.getIfPresent(key) + val collection = collectionsCache.getIfPresent(key) if (collection != null) { count += collection.items.size } diff --git a/core/build.gradle.kts b/core/build.gradle.kts new file mode 100644 index 000000000..b55cc5c72 --- /dev/null +++ b/core/build.gradle.kts @@ -0,0 +1,40 @@ + +plugins { + kotlin("multiplatform") + kotlin("plugin.serialization") + id("com.android.library") + id("com.vanniktech.maven.publish") + id("org.jetbrains.dokka") + id("org.jetbrains.kotlinx.kover") + `maven-publish` + id("kotlinx-atomicfu") + id("org.jetbrains.compose") version("1.5.1") +} + +kotlin { + androidTarget() + jvm() + iosArm64() + iosX64() + linuxX64() + iosSimulatorArm64() + js { + browser() + nodejs() + } + + sourceSets { + val commonMain by getting { + dependencies { + implementation(libs.kotlin.stdlib) + } + } + } +} + +android { + + compileSdk = 33 + + sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml") +} diff --git a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/InsertionStrategy.kt b/core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5/InsertionStrategy.kt similarity index 53% rename from paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/InsertionStrategy.kt rename to core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5/InsertionStrategy.kt index c3b58617c..9be64a478 100644 --- a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/InsertionStrategy.kt +++ b/core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5/InsertionStrategy.kt @@ -1,4 +1,4 @@ -package org.mobilenativefoundation.store.paging5 +package org.mobilenativefoundation.store.core5 enum class InsertionStrategy { APPEND, diff --git a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/KeyProvider.kt b/core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5/KeyProvider.kt similarity index 82% rename from paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/KeyProvider.kt rename to core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5/KeyProvider.kt index 36c7602d9..255abd9ec 100644 --- a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/KeyProvider.kt +++ b/core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5/KeyProvider.kt @@ -1,4 +1,4 @@ -package org.mobilenativefoundation.store.paging5 +package org.mobilenativefoundation.store.core5 interface KeyProvider> { fun from(key: StoreKey.Collection, value: Single): StoreKey.Single diff --git a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/StoreData.kt b/core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5/StoreData.kt similarity index 95% rename from paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/StoreData.kt rename to core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5/StoreData.kt index 8e8e7d55b..e163605bb 100644 --- a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/StoreData.kt +++ b/core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5/StoreData.kt @@ -1,4 +1,4 @@ -package org.mobilenativefoundation.store.paging5 +package org.mobilenativefoundation.store.core5 /** diff --git a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/StoreKey.kt b/core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5/StoreKey.kt similarity index 96% rename from paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/StoreKey.kt rename to core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5/StoreKey.kt index 07997a2df..245ce05a9 100644 --- a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/StoreKey.kt +++ b/core/src/commonMain/kotlin/org/mobilenativefoundation/store/core5/StoreKey.kt @@ -1,4 +1,4 @@ -package org.mobilenativefoundation.store.paging5 +package org.mobilenativefoundation.store.core5 /** * An interface that defines keys used by Store for data-fetching operations. diff --git a/paging/build.gradle.kts b/paging/build.gradle.kts index 06a0bd090..981273851 100644 --- a/paging/build.gradle.kts +++ b/paging/build.gradle.kts @@ -25,8 +25,7 @@ kotlin { implementation(compose.ui) implementation(compose.foundation) implementation(compose.material) - - + api(project(":core")) } } diff --git a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/LaunchPagingStore.kt b/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/LaunchPagingStore.kt index 22bd4af1e..ceb8b3619 100644 --- a/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/LaunchPagingStore.kt +++ b/paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/LaunchPagingStore.kt @@ -7,6 +7,8 @@ package org.mobilenativefoundation.store.paging5 import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.* import kotlinx.coroutines.launch +import org.mobilenativefoundation.store.core5.StoreData +import org.mobilenativefoundation.store.core5.StoreKey import org.mobilenativefoundation.store.store5.* private class StopProcessingException : Exception() diff --git a/paging/src/commonTest/kotlin/org/mobilenativefoundation/store/paging5/util/PostData.kt b/paging/src/commonTest/kotlin/org/mobilenativefoundation/store/paging5/util/PostData.kt index 7c51fb208..e0f6da992 100644 --- a/paging/src/commonTest/kotlin/org/mobilenativefoundation/store/paging5/util/PostData.kt +++ b/paging/src/commonTest/kotlin/org/mobilenativefoundation/store/paging5/util/PostData.kt @@ -1,7 +1,7 @@ package org.mobilenativefoundation.store.paging5.util -import org.mobilenativefoundation.store.paging5.InsertionStrategy -import org.mobilenativefoundation.store.paging5.StoreData +import org.mobilenativefoundation.store.core5.InsertionStrategy +import org.mobilenativefoundation.store.core5.StoreData sealed class PostData : StoreData { data class Post(val postId: String, val title: String) : StoreData.Single, PostData() { diff --git a/paging/src/commonTest/kotlin/org/mobilenativefoundation/store/paging5/util/PostKey.kt b/paging/src/commonTest/kotlin/org/mobilenativefoundation/store/paging5/util/PostKey.kt index 48e52726e..df5f0254c 100644 --- a/paging/src/commonTest/kotlin/org/mobilenativefoundation/store/paging5/util/PostKey.kt +++ b/paging/src/commonTest/kotlin/org/mobilenativefoundation/store/paging5/util/PostKey.kt @@ -1,7 +1,7 @@ package org.mobilenativefoundation.store.paging5.util -import org.mobilenativefoundation.store.paging5.InsertionStrategy -import org.mobilenativefoundation.store.paging5.StoreKey +import org.mobilenativefoundation.store.core5.InsertionStrategy +import org.mobilenativefoundation.store.core5.StoreKey sealed class PostKey : StoreKey { data class Cursor( diff --git a/paging/src/commonTest/kotlin/org/mobilenativefoundation/store/paging5/util/PostStoreFactory.kt b/paging/src/commonTest/kotlin/org/mobilenativefoundation/store/paging5/util/PostStoreFactory.kt index 7c9210ba1..d8ed2dbeb 100644 --- a/paging/src/commonTest/kotlin/org/mobilenativefoundation/store/paging5/util/PostStoreFactory.kt +++ b/paging/src/commonTest/kotlin/org/mobilenativefoundation/store/paging5/util/PostStoreFactory.kt @@ -4,9 +4,9 @@ package org.mobilenativefoundation.store.paging5.util import kotlinx.coroutines.flow.flow import org.mobilenativefoundation.store.cache5.Cache -import org.mobilenativefoundation.store.paging5.KeyProvider -import org.mobilenativefoundation.store.paging5.PagingCache -import org.mobilenativefoundation.store.paging5.StoreKey +import org.mobilenativefoundation.store.core5.KeyProvider +import org.mobilenativefoundation.store.cache5.StoreMultiCache +import org.mobilenativefoundation.store.core5.StoreKey import org.mobilenativefoundation.store.store5.* import kotlin.math.floor @@ -115,7 +115,7 @@ class PostStoreFactory(private val api: PostApi, private val db: PostDatabase) { } private fun createMemoryCache(): Cache = - PagingCache(createPagingCacheKeyProvider()) + StoreMultiCache(createPagingCacheKeyProvider()) fun create(): MutableStore = StoreBuilder.from( fetcher = createFetcher(), diff --git a/settings.gradle b/settings.gradle index ca7e239ff..8c532892f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,4 +2,5 @@ include ':store' include ':cache' include ':multicast' include ':rx2' -include ':paging' \ No newline at end of file +include ':paging' +include ':core' \ No newline at end of file diff --git a/store/build.gradle.kts b/store/build.gradle.kts index 68813dacc..e9a9d1f8d 100644 --- a/store/build.gradle.kts +++ b/store/build.gradle.kts @@ -56,6 +56,7 @@ kotlin { implementation(libs.touchlab.kermit) implementation(project(":multicast")) implementation(project(":cache")) + api(project(":core")) } }