Close K/V DB after restoring records
also don't log stack trace when not finding backup type
This commit is contained in:
parent
ef5d1c3bc8
commit
79777a7b6f
4 changed files with 31 additions and 17 deletions
|
@ -64,13 +64,12 @@ class KvDbManagerImpl(private val context: Context) : KvDbManager {
|
|||
}
|
||||
}
|
||||
|
||||
interface KVDb {
|
||||
interface KVDb : AutoCloseable {
|
||||
fun put(key: String, value: ByteArray)
|
||||
fun get(key: String): ByteArray?
|
||||
fun getAll(): List<Pair<String, ByteArray>>
|
||||
fun delete(key: String)
|
||||
fun vacuum()
|
||||
fun close()
|
||||
}
|
||||
|
||||
class KVDbImpl(context: Context, fileName: String) :
|
||||
|
|
|
@ -98,20 +98,26 @@ internal class KVRestore(
|
|||
val isAutoRestore = state.packageInfo.packageName == MAGIC_PACKAGE_MANAGER &&
|
||||
pmPackageName != null
|
||||
return try {
|
||||
val db = if (isAutoRestore) getCachedRestoreDb(state) else downloadRestoreDb(state)
|
||||
val out = outputFactory.getBackupDataOutput(data)
|
||||
val records = if (isAutoRestore) {
|
||||
val keys = listOf(ANCESTRAL_RECORD_KEY, GLOBAL_METADATA_KEY, pmPackageName)
|
||||
Log.d(TAG, "Single package restore, restrict restore keys to $pmPackageName")
|
||||
db.getAll().filter { it.first in keys }
|
||||
val database = if (isAutoRestore) {
|
||||
getCachedRestoreDb(state)
|
||||
} else {
|
||||
db.getAll()
|
||||
downloadRestoreDb(state)
|
||||
}
|
||||
records.sortedBy { it.first }.forEach { (key, value) ->
|
||||
val size = value.size
|
||||
Log.v(TAG, " ... key=$key size=$size")
|
||||
out.writeEntityHeader(key, size)
|
||||
out.writeEntityData(value, size)
|
||||
database.use { db ->
|
||||
val out = outputFactory.getBackupDataOutput(data)
|
||||
val records = if (isAutoRestore) {
|
||||
val keys = listOf(ANCESTRAL_RECORD_KEY, GLOBAL_METADATA_KEY, pmPackageName)
|
||||
Log.d(TAG, "Single package restore, restrict restore keys to $pmPackageName")
|
||||
db.getAll().filter { it.first in keys }
|
||||
} else {
|
||||
db.getAll()
|
||||
}
|
||||
records.sortedBy { it.first }.forEach { (key, value) ->
|
||||
val size = value.size
|
||||
Log.v(TAG, " ... key=$key size=$size")
|
||||
out.writeEntityHeader(key, size)
|
||||
out.writeEntityData(value, size)
|
||||
}
|
||||
}
|
||||
TRANSPORT_OK
|
||||
} catch (e: UnsupportedVersionException) {
|
||||
|
|
|
@ -225,9 +225,16 @@ internal class RestoreCoordinator(
|
|||
full.initializeState(version, state.token, name, packageInfo)
|
||||
state.currentPackage = packageName
|
||||
TYPE_FULL_STREAM
|
||||
} else throw IOException("No data found for $packageName. Skipping.")
|
||||
} else throw IOException("No data found for $packageName. Skipping...")
|
||||
}
|
||||
null -> {
|
||||
Log.i(TAG, "No backup type found for $packageName. Skipping...")
|
||||
state.backupMetadata.packageMetadataMap[packageName]?.backupType?.let { s ->
|
||||
Log.w(TAG, "State was ${s.name}")
|
||||
}
|
||||
failedPackages.add(packageName)
|
||||
return nextRestorePackage()
|
||||
}
|
||||
null -> throw IOException("No backup type found for $packageName. Skipping.")
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Error finding restore data for $packageName.", e)
|
||||
|
|
|
@ -11,9 +11,9 @@ import com.stevesoltys.seedvault.header.VERSION
|
|||
import com.stevesoltys.seedvault.header.VersionHeader
|
||||
import com.stevesoltys.seedvault.header.getADForKV
|
||||
import com.stevesoltys.seedvault.plugins.LegacyStoragePlugin
|
||||
import com.stevesoltys.seedvault.plugins.StoragePlugin
|
||||
import com.stevesoltys.seedvault.transport.backup.KVDb
|
||||
import com.stevesoltys.seedvault.transport.backup.KvDbManager
|
||||
import com.stevesoltys.seedvault.plugins.StoragePlugin
|
||||
import io.mockk.Runs
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
|
@ -148,6 +148,7 @@ internal class KVRestoreTest : RestoreTest() {
|
|||
every { output.writeEntityHeader(key2, data2.size) } returns 42
|
||||
every { output.writeEntityData(data2, data2.size) } returns data2.size
|
||||
|
||||
every { db.close() } just Runs
|
||||
every { dbManager.deleteDb(packageInfo.packageName, true) } returns true
|
||||
streamsGetClosed()
|
||||
|
||||
|
@ -159,6 +160,7 @@ internal class KVRestoreTest : RestoreTest() {
|
|||
output.writeEntityData(data, data.size)
|
||||
output.writeEntityHeader(key2, data2.size)
|
||||
output.writeEntityData(data2, data2.size)
|
||||
db.close()
|
||||
dbManager.deleteDb(packageInfo.packageName, true)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue