Always use SnapshotManager to load snapshots to benefit from cache

This commit is contained in:
Torsten Grote 2024-09-17 17:10:43 -03:00
parent a1baa6f9d2
commit fe8d458890
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
5 changed files with 22 additions and 30 deletions

View file

@ -26,12 +26,11 @@ import com.stevesoltys.seedvault.metadata.BackupType
import com.stevesoltys.seedvault.metadata.DecryptionFailedException
import com.stevesoltys.seedvault.metadata.MetadataManager
import com.stevesoltys.seedvault.metadata.MetadataReader
import com.stevesoltys.seedvault.proto.Snapshot
import com.stevesoltys.seedvault.repo.Loader
import com.stevesoltys.seedvault.repo.SnapshotManager
import com.stevesoltys.seedvault.repo.getBlobHandles
import com.stevesoltys.seedvault.settings.SettingsManager
import com.stevesoltys.seedvault.transport.D2D_TRANSPORT_FLAGS
import com.stevesoltys.seedvault.transport.DEFAULT_TRANSPORT_FLAGS
import com.stevesoltys.seedvault.repo.getBlobHandles
import com.stevesoltys.seedvault.ui.notification.BackupNotificationManager
import org.calyxos.seedvault.core.backends.AppBackupFileType
import org.calyxos.seedvault.core.backends.Backend
@ -67,7 +66,7 @@ internal class RestoreCoordinator(
private val metadataManager: MetadataManager,
private val notificationManager: BackupNotificationManager,
private val backendManager: BackendManager,
private val loader: Loader,
private val snapshotManager: SnapshotManager,
private val kv: KVRestore,
private val full: FullRestore,
private val metadataReader: MetadataReader,
@ -91,15 +90,10 @@ internal class RestoreCoordinator(
for (handle in fileHandles) {
try {
val backup = when (handle) {
is AppBackupFileType.Snapshot -> {
val snapshot = loader.loadFile(handle).use { inputStream ->
Snapshot.parseFrom(inputStream)
}
RestorableBackup(
repoId = handle.repoId,
snapshot = snapshot,
)
}
is AppBackupFileType.Snapshot -> RestorableBackup(
repoId = handle.repoId,
snapshot = snapshotManager.loadSnapshot(handle),
)
is LegacyAppBackupFile.Metadata -> {
val metadata = backend.load(handle).use { inputStream ->
metadataReader.readMetadata(inputStream, handle.token)

View file

@ -20,7 +20,7 @@ val restoreModule = module {
metadataManager = get(),
notificationManager = get(),
backendManager = get(),
loader = get(),
snapshotManager = get(),
kv = get(),
full = get(),
metadataReader = get(),

View file

@ -26,6 +26,7 @@ import com.stevesoltys.seedvault.repo.AppBackupManager
import com.stevesoltys.seedvault.repo.BackupReceiver
import com.stevesoltys.seedvault.repo.Loader
import com.stevesoltys.seedvault.repo.SnapshotCreator
import com.stevesoltys.seedvault.repo.SnapshotManager
import com.stevesoltys.seedvault.transport.backup.BackupCoordinator
import com.stevesoltys.seedvault.transport.backup.FullBackup
import com.stevesoltys.seedvault.transport.backup.InputFactory
@ -76,6 +77,7 @@ internal class CoordinatorIntegrationTest : TransportTest() {
private val legacyPlugin = mockk<LegacyStoragePlugin>()
private val backend = mockk<Backend>()
private val loader = mockk<Loader>()
private val snapshotManager = mockk<SnapshotManager>()
private val backupReceiver = mockk<BackupReceiver>()
private val kvBackup = KVBackup(
settingsManager = settingsManager,
@ -121,7 +123,7 @@ internal class CoordinatorIntegrationTest : TransportTest() {
metadataManager,
notificationManager,
backendManager,
loader,
snapshotManager,
kvRestore,
fullRestore,
metadataReader

View file

@ -22,7 +22,7 @@ import com.stevesoltys.seedvault.metadata.BackupType
import com.stevesoltys.seedvault.metadata.MetadataReader
import com.stevesoltys.seedvault.metadata.PackageMetadata
import com.stevesoltys.seedvault.proto.copy
import com.stevesoltys.seedvault.repo.Loader
import com.stevesoltys.seedvault.repo.SnapshotManager
import com.stevesoltys.seedvault.transport.TransportTest
import com.stevesoltys.seedvault.ui.notification.BackupNotificationManager
import io.mockk.Runs
@ -45,8 +45,6 @@ import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Assertions.fail
import org.junit.jupiter.api.Test
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStream
import kotlin.random.Random
@ -56,7 +54,7 @@ internal class RestoreCoordinatorTest : TransportTest() {
private val notificationManager: BackupNotificationManager = mockk()
private val backendManager: BackendManager = mockk()
private val backend = mockk<Backend>()
private val loader = mockk<Loader>()
private val snapshotManager = mockk<SnapshotManager>()
private val kv = mockk<KVRestore>()
private val full = mockk<FullRestore>()
private val metadataReader = mockk<MetadataReader>()
@ -68,7 +66,7 @@ internal class RestoreCoordinatorTest : TransportTest() {
metadataManager = metadataManager,
notificationManager = notificationManager,
backendManager = backendManager,
loader = loader,
snapshotManager = snapshotManager,
kv = kv,
full = full,
metadataReader = metadataReader,
@ -261,9 +259,6 @@ internal class RestoreCoordinatorTest : TransportTest() {
fun `startRestore() loads snapshots for auto-restore`() = runBlocking {
val handle = AppBackupFileType.Snapshot(repoId, getRandomByteArray(32).toHexString())
val info = FileInfo(handle, 1)
val snapshotBytes = ByteArrayOutputStream().apply {
snapshot.writeTo(this)
}.toByteArray()
every { backendManager.backendProperties } returns safStorage
every { safStorage.isUnavailableUsb(context) } returns false
@ -278,7 +273,7 @@ internal class RestoreCoordinatorTest : TransportTest() {
val callback = lambda<(FileInfo) -> Unit>().captured
callback(info)
}
coEvery { loader.loadFile(handle) } returns ByteArrayInputStream(snapshotBytes)
coEvery { snapshotManager.loadSnapshot(handle) } returns snapshot
assertEquals(TRANSPORT_OK, restore.startRestore(token, pmPackageInfoArray))
}
@ -287,9 +282,6 @@ internal class RestoreCoordinatorTest : TransportTest() {
fun `startRestore() errors when it can't find snapshots`() = runBlocking {
val handle = AppBackupFileType.Snapshot(repoId, getRandomByteArray(32).toHexString())
val info = FileInfo(handle, 1)
val snapshotBytes = ByteArrayOutputStream().apply { // snapshot has different token
snapshot.copy { token = this@RestoreCoordinatorTest.token - 1 }.writeTo(this)
}.toByteArray()
every { backendManager.backendProperties } returns safStorage
every { safStorage.isUnavailableUsb(context) } returns false
@ -304,12 +296,14 @@ internal class RestoreCoordinatorTest : TransportTest() {
val callback = lambda<(FileInfo) -> Unit>().captured
callback(info)
}
coEvery { loader.loadFile(handle) } returns ByteArrayInputStream(snapshotBytes)
coEvery { snapshotManager.loadSnapshot(handle) } returns snapshot.copy {
token = this@RestoreCoordinatorTest.token - 1 // unexpected token
}
assertEquals(TRANSPORT_ERROR, restore.startRestore(token, pmPackageInfoArray))
coVerify {
loader.loadFile(handle) // really loaded snapshot
snapshotManager.loadSnapshot(handle) // really loaded snapshot
}
}

View file

@ -21,6 +21,7 @@ import com.stevesoltys.seedvault.encodeBase64
import com.stevesoltys.seedvault.header.HeaderReaderImpl
import com.stevesoltys.seedvault.metadata.MetadataReaderImpl
import com.stevesoltys.seedvault.repo.Loader
import com.stevesoltys.seedvault.repo.SnapshotManager
import com.stevesoltys.seedvault.toByteArrayFromHex
import com.stevesoltys.seedvault.transport.TransportTest
import com.stevesoltys.seedvault.transport.backup.KvDbManager
@ -57,6 +58,7 @@ internal class RestoreV0IntegrationTest : TransportTest() {
private val notificationManager = mockk<BackupNotificationManager>()
private val backendManager: BackendManager = mockk()
private val loader = mockk<Loader>()
private val snapshotManager = mockk<SnapshotManager>()
@Suppress("Deprecation")
private val legacyPlugin = mockk<LegacyStoragePlugin>()
@ -79,7 +81,7 @@ internal class RestoreV0IntegrationTest : TransportTest() {
metadataManager = metadataManager,
notificationManager = notificationManager,
backendManager = backendManager,
loader = loader,
snapshotManager = snapshotManager,
kv = kvRestore,
full = fullRestore,
metadataReader = metadataReader,