Schedules: a reading range from the oldest supported format (audit A7), newer files kept aside
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -43,7 +43,14 @@ internal fun migrateChannelNumbers(directory: File) {
|
||||
* the committed schedule and the aired progress. Each can be lost without affecting the others. Holds no login.
|
||||
*/
|
||||
internal class ScheduleStore(private val directory: File) {
|
||||
companion object { const val SCHEMA = 1 }
|
||||
companion object {
|
||||
const val SCHEMA = 1
|
||||
/**
|
||||
* The oldest format this Orbit still reads (audit A7, 9 November 2026). When the format changes, SCHEMA goes up and
|
||||
* this stays, so an update never strands a TV's plans; the reader then upgrades the older file as it goes.
|
||||
*/
|
||||
const val OLDEST_READABLE = 1
|
||||
}
|
||||
internal class SavedProgress(val enabled: Boolean, val aired: ScheduleCursor)
|
||||
internal class SavedPool(val series: List<PoolSeries>, val fetchedAtMs: Long, val signature: String = "")
|
||||
|
||||
@@ -69,6 +76,8 @@ internal class ScheduleStore(private val directory: File) {
|
||||
/** Replaces in one step, keeping the last good copy. An interrupted write can't damage either. */
|
||||
private fun write(target: File, root: JSONObject) {
|
||||
directory.mkdirs()
|
||||
// A file from a newer Orbit (after a recovery to an older build) is never lost: kept aside before it's replaced.
|
||||
newerSchema(target)?.let { v -> if (!File(directory, "${target.name}.schema$v").exists()) runCatching { target.copyTo(File(directory, "${target.name}.schema$v")) } }
|
||||
val temporary = File(directory, "${target.name}.tmp")
|
||||
FileOutputStream(temporary).use { output -> output.write(root.toString().toByteArray(Charsets.UTF_8)); output.fd.sync() }
|
||||
if (target.exists()) target.copyTo(File(directory, "${target.name}.prev"), overwrite = true)
|
||||
@@ -81,13 +90,16 @@ internal class ScheduleStore(private val directory: File) {
|
||||
try {
|
||||
if (!candidate.exists()) continue
|
||||
val root = JSONObject(candidate.readText(Charsets.UTF_8))
|
||||
require(root.getInt("schema") == SCHEMA && root.getString("user") == userId && root.getInt("channel") == number)
|
||||
require(root.getInt("schema") in OLDEST_READABLE..SCHEMA && root.getString("user") == userId && root.getInt("channel") == number)
|
||||
return parse(root)
|
||||
} catch (_: Exception) { /* Damaged: try the last good copy. */ }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/** The schema of [target] when a newer Orbit wrote it, else null. */
|
||||
private fun newerSchema(target: File): Int? = runCatching { JSONObject(target.readText(Charsets.UTF_8)).getInt("schema") }.getOrNull()?.takeIf { it > SCHEMA }
|
||||
|
||||
private fun root(userId: String, number: Int) = JSONObject().put("schema", SCHEMA).put("user", userId).put("channel", number)
|
||||
|
||||
private fun encodeCursor(cursor: ScheduleCursor) = JSONObject().put("last", cursor.lastSeriesId ?: JSONObject.NULL)
|
||||
|
||||
Reference in New Issue
Block a user