Add library snapshot name codec
This commit is contained in:
@@ -26,6 +26,7 @@ import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.charset.CodingErrorAction
|
||||
import java.text.Collator
|
||||
import java.util.UUID
|
||||
import org.oxycblt.musikr.Music
|
||||
import org.oxycblt.musikr.fs.Components
|
||||
@@ -36,14 +37,19 @@ import org.oxycblt.musikr.fs.path.ExternalVolumeValue
|
||||
import org.oxycblt.musikr.fs.path.InternalVolumeValue
|
||||
import org.oxycblt.musikr.tag.Date
|
||||
import org.oxycblt.musikr.tag.Disc
|
||||
import org.oxycblt.musikr.tag.KnownName
|
||||
import org.oxycblt.musikr.tag.Name
|
||||
import org.oxycblt.musikr.tag.Placeholder
|
||||
import org.oxycblt.musikr.tag.ReleaseType
|
||||
import org.oxycblt.musikr.tag.ReplayGainAdjustment
|
||||
import org.oxycblt.musikr.tag.Token
|
||||
|
||||
object LibrarySnapshotCodec {
|
||||
const val MAGIC = "AUXSNAP"
|
||||
private const val SCHEMA_VERSION = 1
|
||||
private const val MAX_STRING_BYTES = 1024 * 1024
|
||||
private const val MAX_FORMAT_DEPTH = 8
|
||||
private const val MAX_NAME_TOKENS = 10_000
|
||||
private val MAGIC_BYTES = MAGIC.toByteArray(Charsets.US_ASCII)
|
||||
|
||||
data class Header(val magic: String, val schemaVersion: Int)
|
||||
@@ -433,4 +439,72 @@ object LibrarySnapshotCodec {
|
||||
val addedMs = readLong(input)
|
||||
return FileProperties(uri, path, format, size, modifiedMs, addedMs)
|
||||
}
|
||||
|
||||
fun writeName(output: OutputStream, value: Name) {
|
||||
when (value) {
|
||||
is Name.Unknown -> {
|
||||
writeInt(output, 0)
|
||||
writeInt(
|
||||
output,
|
||||
when (value.placeholder) {
|
||||
Placeholder.ALBUM -> 0
|
||||
Placeholder.ARTIST -> 1
|
||||
Placeholder.GENRE -> 2
|
||||
},
|
||||
)
|
||||
}
|
||||
is Name.Known -> {
|
||||
writeInt(output, 1)
|
||||
writeString(output, value.raw)
|
||||
writeNullableString(output, value.sort)
|
||||
writeCount(output, value.tokens.size, MAX_NAME_TOKENS)
|
||||
for (token in value.tokens) {
|
||||
writeString(output, token.value)
|
||||
writeInt(
|
||||
output,
|
||||
when (token.type) {
|
||||
Token.Type.NUMERIC -> 0
|
||||
Token.Type.LEXICOGRAPHIC -> 1
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun readName(input: InputStream): Name {
|
||||
return when (readInt(input)) {
|
||||
0 -> Name.Unknown(readPlaceholder(input))
|
||||
1 -> readKnownName(input)
|
||||
else -> throw IOException("Invalid snapshot name")
|
||||
}
|
||||
}
|
||||
|
||||
private fun readPlaceholder(input: InputStream): Placeholder {
|
||||
return when (readInt(input)) {
|
||||
0 -> Placeholder.ALBUM
|
||||
1 -> Placeholder.ARTIST
|
||||
2 -> Placeholder.GENRE
|
||||
else -> throw IOException("Invalid snapshot name placeholder")
|
||||
}
|
||||
}
|
||||
|
||||
private fun readKnownName(input: InputStream): KnownName {
|
||||
val raw = readString(input)
|
||||
val sort = readNullableString(input)
|
||||
val count = readCount(input, MAX_NAME_TOKENS)
|
||||
val collator = Collator.getInstance().apply { strength = Collator.PRIMARY }
|
||||
val tokens =
|
||||
List(count) {
|
||||
val value = readString(input)
|
||||
val type =
|
||||
when (readInt(input)) {
|
||||
0 -> Token.Type.NUMERIC
|
||||
1 -> Token.Type.LEXICOGRAPHIC
|
||||
else -> throw IOException("Invalid snapshot token type")
|
||||
}
|
||||
Token(collator.getCollationKey(value), type)
|
||||
}
|
||||
return KnownName(raw, sort, tokens)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +70,13 @@ sealed interface Name : Comparable<Name> {
|
||||
}
|
||||
}
|
||||
|
||||
/** Shared value implementation used by live naming and snapshot restoration. */
|
||||
internal data class KnownName(
|
||||
override val raw: String,
|
||||
override val sort: String?,
|
||||
override val tokens: List<Token>,
|
||||
) : Name.Known()
|
||||
|
||||
/** An individual part of a name string that can be compared intelligently. */
|
||||
class Token internal constructor(internal val collationKey: CollationKey, internal val type: Type) :
|
||||
Comparable<Token> {
|
||||
|
||||
@@ -23,6 +23,7 @@ import android.os.Build
|
||||
import java.text.CollationKey
|
||||
import java.text.Collator
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import org.oxycblt.musikr.tag.KnownName
|
||||
import org.oxycblt.musikr.tag.Name
|
||||
import org.oxycblt.musikr.tag.Placeholder
|
||||
import org.oxycblt.musikr.tag.Token
|
||||
@@ -55,7 +56,7 @@ class IntelligentNaming(
|
||||
|
||||
override fun name(raw: String, sort: String?): Name.Known {
|
||||
val effectiveName = sort ?: raw
|
||||
return IntelligentKnownName(
|
||||
return KnownName(
|
||||
raw,
|
||||
sort,
|
||||
tokenCache.computeIfAbsent(effectiveName) {
|
||||
@@ -66,7 +67,8 @@ class IntelligentNaming(
|
||||
}
|
||||
|
||||
data object SimpleNaming : Naming() {
|
||||
override fun name(raw: String, sort: String?): Name.Known = SimpleKnownName(raw, sort)
|
||||
override fun name(raw: String, sort: String?): Name.Known =
|
||||
KnownName(raw, sort, simpleTokens(raw, sort))
|
||||
}
|
||||
|
||||
private val collator: Collator = Collator.getInstance().apply { strength = Collator.PRIMARY }
|
||||
@@ -84,34 +86,16 @@ private fun newTransliterator(): (String) -> String {
|
||||
|
||||
// TODO: Consider how you want to handle whitespace and "gaps" in names.
|
||||
|
||||
/**
|
||||
* Plain [Name.Known] implementation that is internationalization-safe.
|
||||
*
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
private data class SimpleKnownName(override val raw: String, override val sort: String?) :
|
||||
Name.Known() {
|
||||
override val tokens = listOf(parseToken(sort ?: raw))
|
||||
|
||||
private fun parseToken(name: String): Token {
|
||||
// Remove excess punctuation from the string, as those usually aren't considered in sorting.
|
||||
val stripped = name.replace(punctRegex, "").trim().ifEmpty { name }
|
||||
val collationKey = collator.getCollationKey(stripped)
|
||||
// Always use lexicographic mode since we aren't parsing any numeric components
|
||||
return Token(collationKey, Token.Type.LEXICOGRAPHIC)
|
||||
}
|
||||
private fun parseToken(name: String): Token {
|
||||
// Remove excess punctuation from the string, as those usually aren't considered in sorting.
|
||||
val stripped = name.replace(punctRegex, "").trim().ifEmpty { name }
|
||||
val collationKey = collator.getCollationKey(stripped)
|
||||
// Always use lexicographic mode since we aren't parsing any numeric components.
|
||||
return Token(collationKey, Token.Type.LEXICOGRAPHIC)
|
||||
}
|
||||
|
||||
/**
|
||||
* [Name.Known] implementation that adds advanced sorting behavior at the cost of localization.
|
||||
*
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
private data class IntelligentKnownName(
|
||||
override val raw: String,
|
||||
override val sort: String?,
|
||||
override val tokens: List<Token>,
|
||||
) : Name.Known()
|
||||
private fun simpleTokens(raw: String, sort: String?) = listOf(parseToken(sort ?: raw))
|
||||
|
||||
|
||||
private val TOKEN_REGEX by lazy { Regex("(\\d+)|(\\D+)") }
|
||||
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Auxio Project
|
||||
* LibrarySnapshotNameCodecTest.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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.musikr.snapshot
|
||||
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.DataOutputStream
|
||||
import java.io.IOException
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertThrows
|
||||
import org.junit.Test
|
||||
import org.oxycblt.musikr.tag.Name
|
||||
import org.oxycblt.musikr.tag.Placeholder
|
||||
import org.oxycblt.musikr.tag.Token
|
||||
import org.oxycblt.musikr.tag.interpret.IntelligentNaming
|
||||
import org.oxycblt.musikr.tag.interpret.Naming
|
||||
|
||||
class LibrarySnapshotNameCodecTest {
|
||||
@Test
|
||||
fun unknownName_roundTrip_preservesEveryPlaceholder() {
|
||||
val values =
|
||||
listOf(
|
||||
Name.Unknown(Placeholder.ALBUM),
|
||||
Name.Unknown(Placeholder.ARTIST),
|
||||
Name.Unknown(Placeholder.GENRE),
|
||||
)
|
||||
val output = ByteArrayOutputStream()
|
||||
values.forEach { LibrarySnapshotCodec.writeName(output, it) }
|
||||
val input = ByteArrayInputStream(output.toByteArray())
|
||||
|
||||
val decoded = values.map { LibrarySnapshotCodec.readName(input) }
|
||||
|
||||
assertEquals(values, decoded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun knownName_roundTrip_preservesRawSortAndTokens() {
|
||||
val value = Naming.simple().name("The Smile", "Smile")
|
||||
val output = ByteArrayOutputStream()
|
||||
|
||||
LibrarySnapshotCodec.writeName(output, value)
|
||||
val decoded =
|
||||
LibrarySnapshotCodec.readName(ByteArrayInputStream(output.toByteArray())) as Name.Known
|
||||
|
||||
assertEquals(value, decoded)
|
||||
assertEquals("The Smile", decoded.raw)
|
||||
assertEquals("Smile", decoded.sort)
|
||||
assertEquals(listOf("Smile"), decoded.tokens.map { it.value })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun knownName_numericAndPunctuationTokens_preserveParsedValuesAndTypes() {
|
||||
val value =
|
||||
IntelligentNaming(transliteratorFactory = { { input -> input } }).name("15-9", null)
|
||||
val output = ByteArrayOutputStream()
|
||||
|
||||
LibrarySnapshotCodec.writeName(output, value)
|
||||
val decoded =
|
||||
LibrarySnapshotCodec.readName(ByteArrayInputStream(output.toByteArray())) as Name.Known
|
||||
|
||||
assertEquals(value, decoded)
|
||||
assertEquals("15-9", decoded.raw)
|
||||
assertEquals(listOf("15", " ", "9"), decoded.tokens.map { it.value })
|
||||
assertEquals(
|
||||
listOf(Token.Type.NUMERIC, Token.Type.LEXICOGRAPHIC, Token.Type.NUMERIC),
|
||||
decoded.tokens.map { it.type },
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun knownName_nonLatinAndLeadingZeroText_preserveRawAndStoredTokens() {
|
||||
val value =
|
||||
IntelligentNaming(transliteratorFactory = { { input -> input } })
|
||||
.name("初音 0032", null)
|
||||
val output = ByteArrayOutputStream()
|
||||
|
||||
LibrarySnapshotCodec.writeName(output, value)
|
||||
val decoded =
|
||||
LibrarySnapshotCodec.readName(ByteArrayInputStream(output.toByteArray())) as Name.Known
|
||||
|
||||
assertEquals(value, decoded)
|
||||
assertEquals("初音 0032", decoded.raw)
|
||||
assertEquals(listOf("初音", "32"), decoded.tokens.map { it.value })
|
||||
assertEquals(
|
||||
listOf(Token.Type.LEXICOGRAPHIC, Token.Type.NUMERIC),
|
||||
decoded.tokens.map { it.type },
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun knownName_restoration_reusesStoredTransliterations() {
|
||||
var transliterations = 0
|
||||
val value =
|
||||
IntelligentNaming(
|
||||
transliteratorFactory = {
|
||||
{
|
||||
transliterations += 1
|
||||
"Hatsune Miku"
|
||||
}
|
||||
}
|
||||
)
|
||||
.name("初音ミク", null)
|
||||
assertEquals(1, transliterations)
|
||||
val output = ByteArrayOutputStream()
|
||||
|
||||
LibrarySnapshotCodec.writeName(output, value)
|
||||
val decoded =
|
||||
LibrarySnapshotCodec.readName(ByteArrayInputStream(output.toByteArray())) as Name.Known
|
||||
|
||||
assertEquals(value, decoded)
|
||||
assertEquals("Hatsune Miku", decoded.tokens.single().value)
|
||||
assertEquals(1, transliterations)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun name_malformedNameAndTokenData_throwIOException() {
|
||||
val invalidName = ByteArrayOutputStream()
|
||||
LibrarySnapshotCodec.writeInt(invalidName, 99)
|
||||
assertThrows(IOException::class.java) {
|
||||
LibrarySnapshotCodec.readName(ByteArrayInputStream(invalidName.toByteArray()))
|
||||
}
|
||||
|
||||
val invalidPlaceholder = ByteArrayOutputStream()
|
||||
LibrarySnapshotCodec.writeInt(invalidPlaceholder, 0)
|
||||
LibrarySnapshotCodec.writeInt(invalidPlaceholder, 99)
|
||||
assertThrows(IOException::class.java) {
|
||||
LibrarySnapshotCodec.readName(ByteArrayInputStream(invalidPlaceholder.toByteArray()))
|
||||
}
|
||||
|
||||
for (count in listOf(-1, 10_001)) {
|
||||
val invalidCount = ByteArrayOutputStream()
|
||||
LibrarySnapshotCodec.writeInt(invalidCount, 1)
|
||||
LibrarySnapshotCodec.writeString(invalidCount, "raw")
|
||||
LibrarySnapshotCodec.writeNullableString(invalidCount, null)
|
||||
DataOutputStream(invalidCount).writeInt(count)
|
||||
assertThrows(IOException::class.java) {
|
||||
LibrarySnapshotCodec.readName(ByteArrayInputStream(invalidCount.toByteArray()))
|
||||
}
|
||||
}
|
||||
|
||||
val invalidToken = ByteArrayOutputStream()
|
||||
LibrarySnapshotCodec.writeInt(invalidToken, 1)
|
||||
LibrarySnapshotCodec.writeString(invalidToken, "raw")
|
||||
LibrarySnapshotCodec.writeNullableString(invalidToken, null)
|
||||
LibrarySnapshotCodec.writeCount(invalidToken, 1, maximum = 10_000)
|
||||
LibrarySnapshotCodec.writeString(invalidToken, "token")
|
||||
LibrarySnapshotCodec.writeInt(invalidToken, 99)
|
||||
assertThrows(IOException::class.java) {
|
||||
LibrarySnapshotCodec.readName(ByteArrayInputStream(invalidToken.toByteArray()))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user