StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.
Kotlin iptv-player
perf: memoize provider observation decoding
David Nashashd034d124267e
diff --git a/data/src/main/java/com/streamvault/data/provider/ProviderObservationStreams.kt b/data/src/main/java/com/streamvault/data/provider/ProviderObservationStreams.ktnew file mode 100644index 00000000..f170e5d0--- /dev/null+++ b/data/src/main/java/com/streamvault/data/provider/ProviderObservationStreams.kt@@ -0,0 +1,76 @@+package com.streamvault.data.provider++import com.streamvault.data.local.dao.ProviderDao+import com.streamvault.data.local.dao.ProviderSnapshotDao+import com.streamvault.data.local.entity.ProviderConfigEntity+import com.streamvault.data.local.entity.ProviderAccountRuntimeEntity+import com.streamvault.data.local.entity.StalkerPortalStateEntity+import com.streamvault.domain.model.LegacyProvider+import kotlinx.coroutines.CoroutineDispatcher+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.flow.Flow+import kotlinx.coroutines.flow.combine+import kotlinx.coroutines.flow.distinctUntilChanged+import kotlinx.coroutines.flow.flowOn+import kotlinx.coroutines.flow.map++internal class ProviderObservationStreams(+ providerDao: ProviderDao,+ providerSnapshotDao: ProviderSnapshotDao,+ private val projection: ProviderPublicProjection,+ private val workDispatcher: CoroutineDispatcher = Dispatchers.IO+) {+ private data class CachedProjection(+ val source: ProviderConfigEntity,+ val value: RedactedProviderConfigurationProjection+ )++ private val cacheLock = Any()+ private val projectionCache = mutableMapOf<Long, CachedProjection>()++ private val publicConfigurations: Flow<Map<Long, RedactedProviderConfigurationProjection>> =+ providerSnapshotDao.observeConfigs()+ .distinctUntilChanged()+ .map(::resolveConfigurationRows)++ val providers: Flow<List<LegacyProvider>> = combine(+ providerDao.getAll(),+ publicConfigurations,+ providerSnapshotDao.observeRuntimes(),+ providerSnapshotDao.observeStalkerPortalStates()+ ) { identities, configurations, runtimes, portalStates ->+ val runtimeByProvider = runtimes.associateBy(ProviderAccountRuntimeEntity::providerId)+ val portalStateByProvider = portalStates.associateBy(StalkerPortalStateEntity::providerId)+ identities.map { identity ->+ projection.assemble(+ identity = identity,+ configuration = configurations[identity.id],+ runtime = runtimeByProvider[identity.id],+ portalState = portalStateByProvider[identity.id]+ )+ }+ }.flowOn(workDispatcher)++ val activeProvider: Flow<LegacyProvider?> = providers+ .map { list -> list.firstOrNull(LegacyProvider::isActive) }+ .distinctUntilChanged()++ private fun resolveConfigurationRows(+ rows: List<ProviderConfigEntity>+ ): Map<Long, RedactedProviderConfigurationProjection> = synchronized(cacheLock) {+ val currentProviderIds = rows.asSequence().map(ProviderConfigEntity::providerId).toSet()+ val resolved = rows.associate { row ->+ val cached = projectionCache[row.providerId]+ val value = if (cached != null && cached.source == row) {+ cached.value+ } else {+ projection.decode(row).also { decoded ->+ projectionCache[row.providerId] = CachedProjection(row, decoded)+ }+ }+ row.providerId to value+ }+ projectionCache.keys.retainAll(currentProviderIds)+ resolved+ }+}diff --git a/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt b/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.ktindex 9221f4b8..ec477fd2 100644--- a/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt+++ b/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt@@ -24,15 +24,14 @@ import com.streamvault.data.preferences.PreferencesRepositoryimport com.streamvault.data.provider.ProviderConfigurationCodecimport com.streamvault.data.provider.ProviderConfigRevisionCodecimport com.streamvault.data.provider.ProviderCapabilityResolver+import com.streamvault.data.provider.DefaultProviderPublicProjection+import com.streamvault.data.provider.ProviderObservationStreamsimport com.streamvault.data.provider.StalkerClientOptionsimport com.streamvault.data.provider.TypedProviderClientFactoryimport com.streamvault.data.provider.toAccountRuntime-import com.streamvault.data.provider.toDomainRuntime-import com.streamvault.data.provider.toPublicDomainimport com.streamvault.data.provider.redactedCredentialsimport com.streamvault.data.provider.toTypedConfigurationimport com.streamvault.data.provider.toLegacyProvider-import com.streamvault.data.provider.toGenerationValidLearningimport com.streamvault.data.provider.guidePolicyimport com.streamvault.data.provider.logoPolicyimport com.streamvault.data.remote.jellyfin.JellyfinProvider@@ -81,7 +80,6 @@ import kotlinx.coroutines.NonCancellableimport kotlinx.coroutines.coroutineScopeimport kotlinx.coroutines.delayimport kotlinx.coroutines.flow.Flow-import kotlinx.coroutines.flow.combineimport kotlinx.coroutines.flow.firstimport kotlinx.coroutines.flow.mapimport kotlinx.coroutines.launch@@ -146,6 +144,14 @@ class ProviderRepositoryImpl @Inject constructor(private val repositoryScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)+ private val observationStreams by lazy {+ ProviderObservationStreams(+ providerDao = providerDao,+ providerSnapshotDao = providerSnapshotDao,+ projection = DefaultProviderPublicProjection(providerConfigurationCodec, gson)+ )+ }+private data class PendingProviderEdit(val revision: Long,val candidate: Provider,@@ -158,55 +164,9 @@ class ProviderRepositoryImpl @Inject constructor(val pendingEdit: PendingProviderEdit? = null)- override fun getProviders(): Flow<List<Provider>> = combine(- providerDao.getAll(),- providerSnapshotDao.observeConfigs(),- providerSnapshotDao.observeRuntimes(),- providerSnapshotDao.observeStalkerPortalStates()- ) { identities, configs, runtimes, portalStates ->- val configsByProvider = configs.associateBy { it.providerId }- val runtimesByProvider = runtimes.associateBy { it.providerId }- val portalStatesByProvider = portalStates.associateBy { it.providerId }- identities.map { identity ->- val stored = configsByProvider[identity.id]- if (stored == null) {- identity.toPublicDomain()- } else {- val stableIdentity = StableProvider(- id = identity.id,- name = identity.name,- type = identity.type,- isActive = identity.isActive,- status = identity.status,- lastSyncedAt = identity.lastSyncedAt,- createdAt = identity.createdAt- )- val runtime = runtimesByProvider[identity.id]?.toDomainRuntime(gson)- ?: ProviderAccountRuntime()- val learning = if (identity.type == ProviderType.STALKER_PORTAL) {- portalStatesByProvider[identity.id]?.toGenerationValidLearning(- gson,- stored.configurationGeneration- )- } else {- null- }- ProviderSnapshot(- provider = stableIdentity,- configuration = providerConfigurationCodec.decode(- stored.type,- stored.encryptedConfigJson- ),- configurationGeneration = stored.configurationGeneration,- accountRuntime = runtime,- stalkerLearning = learning- ).toLegacyProvider().redactedCredentials()- }- }- }+ override fun getProviders(): Flow<List<Provider>> = observationStreams.providers- override fun getActiveProvider(): Flow<Provider?> =- getProviders().map { providers -> providers.firstOrNull { it.isActive } }+ override fun getActiveProvider(): Flow<Provider?> = observationStreams.activeProvideroverride suspend fun getProvider(id: Long): Provider? =loadLegacyProvider(id)?.redactedCredentials()diff --git a/data/src/test/java/com/streamvault/data/provider/ProviderObservationStreamsTest.kt b/data/src/test/java/com/streamvault/data/provider/ProviderObservationStreamsTest.ktnew file mode 100644index 00000000..a2e6b028--- /dev/null+++ b/data/src/test/java/com/streamvault/data/provider/ProviderObservationStreamsTest.kt@@ -0,0 +1,320 @@+package com.streamvault.data.provider++import com.google.common.truth.Truth.assertThat+import com.streamvault.data.local.dao.ProviderDao+import com.streamvault.data.local.dao.ProviderSnapshotDao+import com.streamvault.data.local.entity.ProviderAccountRuntimeEntity+import com.streamvault.data.local.entity.ProviderConfigEntity+import com.streamvault.data.local.entity.ProviderEntity+import com.streamvault.data.local.entity.StalkerPortalStateEntity+import com.streamvault.domain.model.CatalogLayout+import com.streamvault.domain.model.LegacyProvider+import com.streamvault.domain.model.ProviderType+import com.streamvault.domain.model.XtreamConfig+import kotlinx.coroutines.CoroutineDispatcher+import kotlinx.coroutines.CoroutineStart+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.async+import kotlinx.coroutines.flow.Flow+import kotlinx.coroutines.flow.MutableSharedFlow+import kotlinx.coroutines.flow.MutableStateFlow+import kotlinx.coroutines.flow.first+import kotlinx.coroutines.flow.flowOf+import kotlinx.coroutines.flow.take+import kotlinx.coroutines.flow.toList+import kotlinx.coroutines.launch+import kotlinx.coroutines.test.StandardTestDispatcher+import kotlinx.coroutines.ExperimentalCoroutinesApi+import kotlinx.coroutines.test.advanceUntilIdle+import kotlinx.coroutines.test.runTest+import org.junit.Test+import org.mockito.kotlin.mock+import org.mockito.kotlin.whenever+import java.util.concurrent.atomic.AtomicInteger+import java.util.concurrent.CountDownLatch+import java.util.concurrent.TimeUnit++@OptIn(ExperimentalCoroutinesApi::class)+class ProviderObservationStreamsTest {++ private data class DecodeGate(+ val started: CountDownLatch,+ val release: CountDownLatch+ )++ @Test+ fun `multiple collectors reuse one decoded redacted projection`() = runTest {+ val fixture = stateFixture()+ val streams = fixture.streams(StandardTestDispatcher(testScheduler))++ val providers = async { streams.providers.first() }+ val secondProviders = async { streams.providers.first() }+ val active = async { streams.activeProvider.first() }+ val secondActive = async { streams.activeProvider.first() }+ advanceUntilIdle()++ assertThat(providers.await()).hasSize(1)+ assertThat(secondProviders.await()).hasSize(1)+ assertThat(active.await()?.id).isEqualTo(9L)+ assertThat(secondActive.await()?.id).isEqualTo(9L)+ assertThat(fixture.projection.decodeCalls.get()).isEqualTo(1)+ assertThat(providers.getCompleted().single().password).isEmpty()+ }++ @Test+ fun `concurrent collectors decode one row only`() = runTest {+ val fixture = stateFixture()+ val decodeStarted = CountDownLatch(1)+ val releaseDecode = CountDownLatch(1)+ fixture.projection.decodeGate = DecodeGate(decodeStarted, releaseDecode)+ val streams = fixture.streams(Dispatchers.Default)++ val first = async(Dispatchers.Default) { streams.providers.first() }+ assertThat(decodeStarted.await(2, TimeUnit.SECONDS)).isTrue()++ val second = async(Dispatchers.Default) { streams.providers.first() }+ Thread.sleep(100)++ assertThat(first.isCompleted).isFalse()+ assertThat(second.isCompleted).isFalse()+ assertThat(fixture.projection.decodeCalls.get()).isEqualTo(1)++ releaseDecode.countDown()+ assertThat(first.await()).hasSize(1)+ assertThat(second.await()).hasSize(1)+ assertThat(fixture.projection.decodeCalls.get()).isEqualTo(1)+ }++ @Test+ fun `runtime-only changes assemble without another decode`() = runTest {+ val fixture = stateFixture()+ val streams = fixture.streams(StandardTestDispatcher(testScheduler))+ val results = mutableListOf<List<LegacyProvider>>()+ val collector = launch {+ streams.providers.take(2).toList(results)+ }+ advanceUntilIdle()++ fixture.runtimes.value = listOf(+ ProviderAccountRuntimeEntity(+ providerId = 9L,+ maxConnections = 4,+ catalogLayout = CatalogLayout.UNIFIED_VOD,+ catalogLayoutDetectionVersion = 2+ )+ )+ advanceUntilIdle()+ collector.join()++ assertThat(results).hasSize(2)+ assertThat(results.last().single().maxConnections).isEqualTo(4)+ assertThat(results.last().single().catalogLayout).isEqualTo(CatalogLayout.UNIFIED_VOD)+ assertThat(fixture.projection.decodeCalls.get()).isEqualTo(1)+ }++ @Test+ fun `changed configuration row decodes once and removed rows are evicted`() = runTest {+ val fixture = stateFixture()+ val streams = fixture.streams(StandardTestDispatcher(testScheduler))+ streams.providers.first()+ advanceUntilIdle()+ assertThat(fixture.projection.decodeCalls.get()).isEqualTo(1)++ fixture.configs.value = listOf(+ fixture.config.copy(+ encryptedConfigJson = com.google.gson.Gson().toJson(+ XtreamConfig(+ serverUrl = "https://changed.example",+ username = "changed-user",+ password = "changed-secret"+ )+ )+ )+ )+ streams.providers.first()+ advanceUntilIdle()+ assertThat(fixture.projection.decodeCalls.get()).isEqualTo(2)++ fixture.configs.value = emptyList()+ val fallback = streams.providers.first()+ advanceUntilIdle()+ assertThat(fallback.single().password).isEmpty()++ fixture.configs.value = listOf(fixture.config)+ streams.providers.first()+ advanceUntilIdle()+ assertThat(fixture.projection.decodeCalls.get()).isEqualTo(3)+ }++ @Test+ fun `first waits for real DAO emissions`() = runTest {+ val identities = MutableSharedFlow<List<ProviderEntity>>()+ val configs = MutableSharedFlow<List<ProviderConfigEntity>>()+ val runtimes = MutableSharedFlow<List<ProviderAccountRuntimeEntity>>()+ val portalStates = MutableSharedFlow<List<StalkerPortalStateEntity>>()+ val projection = CountingProjection()+ val streams = ProviderObservationStreams(+ providerDao = providerDao(identities),+ providerSnapshotDao = snapshotDao(configs, runtimes, portalStates),+ projection = projection,+ workDispatcher = StandardTestDispatcher(testScheduler)+ )++ val result = async(start = CoroutineStart.UNDISPATCHED) { streams.providers.first() }+ advanceUntilIdle()+ assertThat(result.isCompleted).isFalse()++ identities.emit(listOf(providerEntity()))+ configs.emit(listOf(configEntity()))+ runtimes.emit(emptyList())+ portalStates.emit(emptyList())+ advanceUntilIdle()++ assertThat(result.await()).hasSize(1)+ }++ @Test+ fun `failed decode reaches collector and is retried without caching failure`() = runTest {+ val fixture = stateFixture()+ fixture.projection.failure = IllegalStateException("decode failed")+ val streams = fixture.streams(StandardTestDispatcher(testScheduler))++ val firstFailure = try {+ streams.providers.first()+ null+ } catch (error: IllegalStateException) {+ error+ }+ assertThat(firstFailure).isNotNull()+ assertThat(firstFailure?.message).isEqualTo("decode failed")++ fixture.projection.failure = null+ assertThat(streams.providers.first()).hasSize(1)+ advanceUntilIdle()+ assertThat(fixture.projection.decodeCalls.get()).isEqualTo(2)+ }++ @Test+ fun `cold provider flow completes when all DAO flows complete`() = runTest {+ val projection = CountingProjection()+ val streams = ProviderObservationStreams(+ providerDao = providerDao(flowOf(listOf(providerEntity()))),+ providerSnapshotDao = snapshotDao(+ configs = flowOf(listOf(configEntity())),+ runtimes = flowOf(emptyList()),+ portalStates = flowOf(emptyList())+ ),+ projection = projection,+ workDispatcher = StandardTestDispatcher(testScheduler)+ )++ val values = streams.providers.toList()++ assertThat(values).hasSize(1)+ assertThat(values.single()).hasSize(1)+ }++ private fun stateFixture(): Fixture {+ val identities = MutableStateFlow(listOf(providerEntity()))+ val configs = MutableStateFlow(listOf(configEntity()))+ val runtimes = MutableStateFlow(emptyList<ProviderAccountRuntimeEntity>())+ val portalStates = MutableStateFlow(emptyList<StalkerPortalStateEntity>())+ return Fixture(+ identities = identities,+ configs = configs,+ runtimes = runtimes,+ portalStates = portalStates,+ config = configs.value.single(),+ projection = CountingProjection()+ )+ }++ private inner class Fixture(+ val identities: MutableStateFlow<List<ProviderEntity>>,+ val configs: MutableStateFlow<List<ProviderConfigEntity>>,+ val runtimes: MutableStateFlow<List<ProviderAccountRuntimeEntity>>,+ val portalStates: MutableStateFlow<List<StalkerPortalStateEntity>>,+ val config: ProviderConfigEntity,+ val projection: CountingProjection+ ) {+ fun streams(workDispatcher: CoroutineDispatcher) = ProviderObservationStreams(+ providerDao = providerDao(identities),+ providerSnapshotDao = snapshotDao(configs, runtimes, portalStates),+ projection = projection,+ workDispatcher = workDispatcher+ )+ }++ private class CountingProjection : ProviderPublicProjection {+ private val delegate = DefaultProviderPublicProjection(+ codec = ProviderConfigurationCodec(+ gson = com.google.gson.Gson(),+ credentialCrypto = object : com.streamvault.data.security.CredentialCrypto {+ override fun encryptIfNeeded(value: String): String = value+ override fun decryptIfNeeded(value: String): String = value+ }+ ),+ gson = com.google.gson.Gson()+ )+ val decodeCalls = AtomicInteger()+ var failure: RuntimeException? = null+ var decodeGate: DecodeGate? = null++ override fun decode(entity: ProviderConfigEntity): RedactedProviderConfigurationProjection {+ val call = decodeCalls.incrementAndGet()+ failure?.let { throw it }+ decodeGate?.takeIf { call == 1 }?.let { gate ->+ gate.started.countDown()+ check(gate.release.await(2, TimeUnit.SECONDS)) {+ "timed out waiting for concurrent decode test"+ }+ }+ return delegate.decode(entity)+ }++ override fun assemble(+ identity: ProviderEntity,+ configuration: RedactedProviderConfigurationProjection?,+ runtime: ProviderAccountRuntimeEntity?,+ portalState: StalkerPortalStateEntity?+ ): LegacyProvider = delegate.assemble(identity, configuration, runtime, portalState)+ }++ private fun providerDao(flow: Flow<List<ProviderEntity>>): ProviderDao = mock<ProviderDao>().also {+ whenever(it.getAll()).thenReturn(flow)+ }++ private fun snapshotDao(+ configs: Flow<List<ProviderConfigEntity>>,+ runtimes: Flow<List<ProviderAccountRuntimeEntity>>,+ portalStates: Flow<List<StalkerPortalStateEntity>>+ ): ProviderSnapshotDao = mock<ProviderSnapshotDao>().also {+ whenever(it.observeConfigs()).thenReturn(configs)+ whenever(it.observeRuntimes()).thenReturn(runtimes)+ whenever(it.observeStalkerPortalStates()).thenReturn(portalStates)+ }++ private fun providerEntity() = ProviderEntity(+ id = 9L,+ name = "Living room",+ type = ProviderType.XTREAM_CODES,+ isActive = true+ )++ private fun configEntity() = ProviderConfigEntity(+ providerId = 9L,+ type = ProviderType.XTREAM_CODES,+ schemaVersion = 1,+ configurationGeneration = 7L,+ identityKey = "identity",+ encryptedConfigJson = com.google.gson.Gson().toJson(+ XtreamConfig(+ serverUrl = "https://example.test",+ username = "user",+ password = "secret"+ )+ ),+ updatedAt = 1L+ )++}