mirror of
https://github.com/element-hq/element-android
synced 2024-11-26 19:35:42 +03:00
update rust migration to support lazy
This commit is contained in:
parent
1a8581a78e
commit
a744ad1f60
5 changed files with 54 additions and 42 deletions
|
@ -963,7 +963,6 @@ class KeysBackupTest : InstrumentedTest {
|
|||
val signatures = keysBackup2.getCurrentVersion()?.toKeysVersionResult()?.getAuthDataAsMegolmBackupAuthData()?.signatures
|
||||
Log.d("#E2E", "keysBackup2 signatures: $signatures")
|
||||
|
||||
|
||||
// - Validate the old device from the new one
|
||||
cryptoTestHelper.verifyNewSession(cryptoTestData.firstSession, aliceSession2)
|
||||
|
||||
|
|
|
@ -65,6 +65,15 @@ class ElementAndroidToElementRMigrationTest : InstrumentedTest {
|
|||
|
||||
@Test
|
||||
fun given_a_valid_crypto_store_realm_file_then_migration_should_be_successful() {
|
||||
testMigrate(false)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given_a_valid_crypto_store_realm_file_no_lazy_then_migration_should_be_successful() {
|
||||
testMigrate(true)
|
||||
}
|
||||
|
||||
private fun testMigrate(migrateGroupSessions: Boolean) {
|
||||
val realmName = "crypto_store_migration_16.realm"
|
||||
val migration = RealmCryptoStoreMigration(object : Clock {
|
||||
override fun epochMillis() = 0L
|
||||
|
@ -86,7 +95,7 @@ class ElementAndroidToElementRMigrationTest : InstrumentedTest {
|
|||
val deviceId = metaData.deviceId!!
|
||||
val olmAccount = metaData.getOlmAccount()!!
|
||||
|
||||
val extractor = MigrateEAtoEROperation()
|
||||
val extractor = MigrateEAtoEROperation(migrateGroupSessions)
|
||||
|
||||
val targetFile = File(configurationFactory.root, "rust-sdk")
|
||||
|
||||
|
@ -101,14 +110,16 @@ class ElementAndroidToElementRMigrationTest : InstrumentedTest {
|
|||
assertTrue(crossSigningStatus.hasSelfSigning)
|
||||
assertTrue(crossSigningStatus.hasUserSigning)
|
||||
|
||||
val inboundGroupSessionEntities = realm!!.where<OlmInboundGroupSessionEntity>().findAll()
|
||||
assertEquals(inboundGroupSessionEntities.size, machine.roomKeyCounts().total.toInt())
|
||||
if (migrateGroupSessions) {
|
||||
val inboundGroupSessionEntities = realm!!.where<OlmInboundGroupSessionEntity>().findAll()
|
||||
assertEquals(inboundGroupSessionEntities.size, machine.roomKeyCounts().total.toInt())
|
||||
|
||||
val backedUpInboundGroupSessionEntities = realm!!
|
||||
.where<OlmInboundGroupSessionEntity>()
|
||||
.equalTo(OlmInboundGroupSessionEntityFields.BACKED_UP, true)
|
||||
.findAll()
|
||||
assertEquals(backedUpInboundGroupSessionEntities.size, machine.roomKeyCounts().backedUp.toInt())
|
||||
val backedUpInboundGroupSessionEntities = realm!!
|
||||
.where<OlmInboundGroupSessionEntity>()
|
||||
.equalTo(OlmInboundGroupSessionEntityFields.BACKED_UP, true)
|
||||
.findAll()
|
||||
assertEquals(backedUpInboundGroupSessionEntities.size, machine.roomKeyCounts().backedUp.toInt())
|
||||
}
|
||||
}
|
||||
|
||||
// @Test
|
||||
|
|
|
@ -20,11 +20,11 @@ import io.realm.RealmConfiguration
|
|||
import timber.log.Timber
|
||||
import java.io.File
|
||||
|
||||
class MigrateEAtoEROperation {
|
||||
class MigrateEAtoEROperation(private val migrateGroupSessions: Boolean = false) {
|
||||
|
||||
fun execute(cryptoRealm: RealmConfiguration, sessionFilesDir: File, passphrase: String?): File {
|
||||
// to remove unused warning
|
||||
Timber.v("Not used in kotlin crypto $cryptoRealm ${"*".repeat(passphrase?.length ?: 0)}")
|
||||
Timber.v("Not used in kotlin crypto $cryptoRealm ${"*".repeat(passphrase?.length ?: 0)} lazy:$migrateGroupSessions")
|
||||
// no op in kotlinCrypto
|
||||
return sessionFilesDir
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ import kotlin.system.measureTimeMillis
|
|||
|
||||
private val charset = Charset.forName("UTF-8")
|
||||
|
||||
internal class ExtractMigrationDataUseCase {
|
||||
internal class ExtractMigrationDataUseCase(val migrateGroupSessions: Boolean = false) {
|
||||
|
||||
fun extractData(realm: Realm, importPartial: ((MigrationData) -> Unit)) {
|
||||
return try {
|
||||
|
@ -143,35 +143,37 @@ internal class ExtractMigrationDataUseCase {
|
|||
Timber.i("Migration: rust import time $writeTime")
|
||||
}
|
||||
|
||||
// We don't migrate outbound session directly after migration
|
||||
// We don't migrate outbound session by default directly after migration
|
||||
// We are going to do it lazyly when decryption fails
|
||||
// var migratedInboundGroupSessionCount = 0
|
||||
// readTime = 0
|
||||
// writeTime = 0
|
||||
// measureTimeMillis {
|
||||
// realm.where<OlmInboundGroupSessionEntity>()
|
||||
// .findAll()
|
||||
// .chunked(chunkSize) { chunk ->
|
||||
// val export: List<PickledInboundGroupSession>
|
||||
// measureTimeMillis {
|
||||
// export = chunk.mapNotNull { it.toPickledInboundGroupSession(pickleKey) }
|
||||
// }.also {
|
||||
// readTime += it
|
||||
// }
|
||||
// migratedInboundGroupSessionCount+=export.size
|
||||
// measureTimeMillis {
|
||||
// importPartial(
|
||||
// baseExtract.copy(inboundGroupSessions = export)
|
||||
// )
|
||||
// }.also {
|
||||
// writeTime += it
|
||||
// }
|
||||
// }
|
||||
// }.also {
|
||||
// Timber.i("Migration: took $it ms to migrate $migratedInboundGroupSessionCount group sessions")
|
||||
// Timber.i("Migration: extract time $readTime")
|
||||
// Timber.i("Migration: rust import time $writeTime")
|
||||
// }
|
||||
if (migrateGroupSessions) {
|
||||
var migratedInboundGroupSessionCount = 0
|
||||
readTime = 0
|
||||
writeTime = 0
|
||||
measureTimeMillis {
|
||||
realm.where<OlmInboundGroupSessionEntity>()
|
||||
.findAll()
|
||||
.chunked(chunkSize) { chunk ->
|
||||
val export: List<PickledInboundGroupSession>
|
||||
measureTimeMillis {
|
||||
export = chunk.mapNotNull { it.toPickledInboundGroupSession(pickleKey) }
|
||||
}.also {
|
||||
readTime += it
|
||||
}
|
||||
migratedInboundGroupSessionCount += export.size
|
||||
measureTimeMillis {
|
||||
importPartial(
|
||||
baseExtract.copy(inboundGroupSessions = export)
|
||||
)
|
||||
}.also {
|
||||
writeTime += it
|
||||
}
|
||||
}
|
||||
}.also {
|
||||
Timber.i("Migration: took $it ms to migrate $migratedInboundGroupSessionCount group sessions")
|
||||
Timber.i("Migration: extract time $readTime")
|
||||
Timber.i("Migration: rust import time $writeTime")
|
||||
}
|
||||
}
|
||||
|
||||
// return baseExtract
|
||||
}
|
||||
|
|
|
@ -23,14 +23,14 @@ import org.matrix.rustcomponents.sdk.crypto.ProgressListener
|
|||
import timber.log.Timber
|
||||
import java.io.File
|
||||
|
||||
class MigrateEAtoEROperation {
|
||||
class MigrateEAtoEROperation(private val migrateGroupSessions: Boolean = false) {
|
||||
|
||||
fun execute(cryptoRealm: RealmConfiguration, rustFilesDir: File, passphrase: String?): File {
|
||||
// Temporary code for migration
|
||||
if (!rustFilesDir.exists()) {
|
||||
rustFilesDir.mkdir()
|
||||
// perform a migration?
|
||||
val extractMigrationData = ExtractMigrationDataUseCase()
|
||||
val extractMigrationData = ExtractMigrationDataUseCase(migrateGroupSessions)
|
||||
val hasExitingData = extractMigrationData.hasExistingData(cryptoRealm)
|
||||
if (!hasExitingData) return rustFilesDir
|
||||
|
||||
|
|
Loading…
Reference in a new issue