diff --git a/musikr/src/main/java/org/oxycblt/musikr/cache/db/DBCache.kt b/musikr/src/main/java/org/oxycblt/musikr/cache/db/DBCache.kt index c10e4e1ee..437c21ce8 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/cache/db/DBCache.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/cache/db/DBCache.kt @@ -37,13 +37,16 @@ import org.oxycblt.musikr.tag.parse.ParsedTags * Create an instance with [from]. */ class DBCache private constructor(private val readDao: CacheReadDao) : Cache { - private var mapping: Map? = null + // The initialized map is immutable, so volatile publication is sufficient for cache hits. + @Volatile private var mapping: Map? = null private val mappingLock = Mutex() override suspend fun read(file: File): CacheResult { - val currentMapping = mappingLock.withLock { - mapping ?: readDao.selectAllSongs().associateBy { it.uri }.also { mapping = it } - } + val currentMapping = + mapping + ?: mappingLock.withLock { + mapping ?: readDao.selectAllSongs().associateBy { it.uri }.also { mapping = it } + } val dbSong = currentMapping[file.uri] ?: return CacheResult.Miss(file) if (dbSong.modifiedMs != file.modifiedMs) { return CacheResult.Stale(file, dbSong.addedMs) diff --git a/musikr/src/test/java/org/oxycblt/musikr/cache/db/DBCacheTest.kt b/musikr/src/test/java/org/oxycblt/musikr/cache/db/DBCacheTest.kt new file mode 100644 index 000000000..e4c20c58a --- /dev/null +++ b/musikr/src/test/java/org/oxycblt/musikr/cache/db/DBCacheTest.kt @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2026 Auxio Project + * DBCacheTest.kt is part of Auxio. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.oxycblt.musikr.cache.db + +import android.net.Uri +import io.mockk.coEvery +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.async +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.yield +import org.junit.Assert.assertTrue +import org.junit.Test +import org.oxycblt.musikr.fs.File + +class DBCacheTest { + @Test + fun read_initializedMapping_doesNotWaitForMappingLock() = runTest { + val firstUri = mockk() + val secondUri = mockk() + val readDao = mockk() + coEvery { readDao.selectAllSongs() } returns + listOf(cachedData(firstUri), cachedData(secondUri)) + val cache = DBCache.from(readDao) + cache.read(file(firstUri)) + + val field = DBCache::class.java.getDeclaredField("mappingLock") + field.isAccessible = true + val lock = field.get(cache) as Mutex + lock.lock() + val secondRead = async { cache.read(file(secondUri)) } + try { + yield() + + assertTrue(secondRead.isCompleted) + } finally { + lock.unlock() + } + secondRead.await() + } + + private fun cachedData(uri: Uri): CachedFileData { + val data = mockk(relaxed = true) + every { data.uri } returns uri + every { data.modifiedMs } returns 0L + every { data.addedMs } returns 0L + every { data.mimeType } returns null + return data + } + + private fun file(uri: Uri): File { + val file = mockk(relaxed = true) + every { file.uri } returns uri + every { file.modifiedMs } returns 0L + return file + } +}