From cfdfd3a8af11974a379676f6156b40acf1878aec Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 2 Feb 2021 21:22:54 +0100 Subject: [PATCH 1/5] internal --- .../sdk/internal/crypto/crosssigning/Extensions.kt | 6 +++--- .../sdk/internal/crypto/model/CryptoCrossSigningKey.kt | 4 ++-- .../org/matrix/android/sdk/internal/crypto/tools/Tools.kt | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/crosssigning/Extensions.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/crosssigning/Extensions.kt index e494cb5b31..cf2d6aa269 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/crosssigning/Extensions.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/crosssigning/Extensions.kt @@ -21,11 +21,11 @@ import org.matrix.android.sdk.internal.crypto.model.CryptoDeviceInfo import org.matrix.android.sdk.internal.util.JsonCanonicalizer import timber.log.Timber -fun CryptoDeviceInfo.canonicalSignable(): String { +internal fun CryptoDeviceInfo.canonicalSignable(): String { return JsonCanonicalizer.getCanonicalJson(Map::class.java, signalableJSONDictionary()) } -fun CryptoCrossSigningKey.canonicalSignable(): String { +internal fun CryptoCrossSigningKey.canonicalSignable(): String { return JsonCanonicalizer.getCanonicalJson(Map::class.java, signalableJSONDictionary()) } @@ -40,7 +40,7 @@ fun String.fromBase64(): ByteArray { /** * Decode the base 64. Return null in case of bad format. Should be used when parsing received data from external source */ -fun String.fromBase64Safe(): ByteArray? { +internal fun String.fromBase64Safe(): ByteArray? { return try { Base64.decode(this, Base64.DEFAULT) } catch (throwable: Throwable) { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/CryptoCrossSigningKey.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/CryptoCrossSigningKey.kt index 202aa55624..606d2e3fc0 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/CryptoCrossSigningKey.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/CryptoCrossSigningKey.kt @@ -63,7 +63,7 @@ data class CryptoCrossSigningKey( ) } - data class Builder( + internal data class Builder( val userId: String, val usage: KeyUsage, private var base64Pkey: String? = null, @@ -97,7 +97,7 @@ data class CryptoCrossSigningKey( } } -enum class KeyUsage(val value: String) { +internal enum class KeyUsage(val value: String) { MASTER("master"), SELF_SIGNING("self_signing"), USER_SIGNING("user_signing") diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/tools/Tools.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/tools/Tools.kt index 4c1e896a21..052b3f4e72 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/tools/Tools.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/tools/Tools.kt @@ -21,7 +21,7 @@ import org.matrix.olm.OlmPkEncryption import org.matrix.olm.OlmPkSigning import org.matrix.olm.OlmUtility -fun withOlmEncryption(block: (OlmPkEncryption) -> T): T { +internal fun withOlmEncryption(block: (OlmPkEncryption) -> T): T { val olmPkEncryption = OlmPkEncryption() try { return block(olmPkEncryption) @@ -30,7 +30,7 @@ fun withOlmEncryption(block: (OlmPkEncryption) -> T): T { } } -fun withOlmDecryption(block: (OlmPkDecryption) -> T): T { +internal fun withOlmDecryption(block: (OlmPkDecryption) -> T): T { val olmPkDecryption = OlmPkDecryption() try { return block(olmPkDecryption) @@ -39,7 +39,7 @@ fun withOlmDecryption(block: (OlmPkDecryption) -> T): T { } } -fun withOlmSigning(block: (OlmPkSigning) -> T): T { +internal fun withOlmSigning(block: (OlmPkSigning) -> T): T { val olmPkSigning = OlmPkSigning() try { return block(olmPkSigning) @@ -48,7 +48,7 @@ fun withOlmSigning(block: (OlmPkSigning) -> T): T { } } -fun withOlmUtility(block: (OlmUtility) -> T): T { +internal fun withOlmUtility(block: (OlmUtility) -> T): T { val olmUtility = OlmUtility() try { return block(olmUtility) From 3b65761110996b97f5e3957849b52d66d68b270d Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 2 Feb 2021 21:23:16 +0100 Subject: [PATCH 2/5] Delete on cascade --- CHANGES.md | 1 + .../crypto/store/db/RealmCryptoStore.kt | 8 +++++--- .../store/db/model/CrossSigningInfoEntity.kt | 7 ++++++- .../crypto/store/db/model/DeviceInfoEntity.kt | 5 +++++ .../crypto/store/db/model/KeyInfoEntity.kt | 8 +++++++- .../crypto/store/db/model/UserEntity.kt | 10 ++++++++-- .../store/db/query/UserEntitiesQueries.kt | 6 +++--- .../sdk/internal/database/DatabaseCleaner.kt | 17 ++--------------- .../database/helper/ChunkEntityHelper.kt | 6 ------ .../database/helper/RoomEntityHelper.kt | 7 +------ .../helper/TimelineEventEntityHelper.kt | 9 --------- .../sdk/internal/database/model/ChunkEntity.kt | 10 ++++++++++ .../model/EventAnnotationsSummaryEntity.kt | 8 ++++++++ .../internal/database/model/PushRuleEntity.kt | 5 +++++ .../internal/database/model/PushRulesEntity.kt | 5 +++++ .../sdk/internal/database/model/PusherEntity.kt | 7 ++++++- .../database/model/ReadReceiptsSummaryEntity.kt | 5 +++++ .../database/model/TimelineEventEntity.kt | 11 +++++++++++ .../internal/session/pushers/GetPushersTask.kt | 3 ++- .../session/pushers/SavePushRulesTask.kt | 2 +- .../room/timeline/TokenChunkEventPersistor.kt | 9 ++++----- .../internal/session/sync/RoomSyncHandler.kt | 10 +++++----- .../session/sync/UserAccountDataSyncHandler.kt | 2 +- 23 files changed, 101 insertions(+), 60 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index fcf6659d92..9c72ff3574 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ Bugfix 🐛: - Widgets: Support $matrix_widget_id parameter (#2748) - Data for Worker overload (#2721) - Fix multiple tasks + - Object deletion in database is not complete (#2759) Translations 🗣: - diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt index 369a4976c9..8dbccb061b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt @@ -293,7 +293,8 @@ internal class RealmCryptoStore @Inject constructor( realm.insertOrUpdate(entity) } // Ensure all other devices are deleted - u.devices.deleteAllFromRealm() + u.devices.toList().forEach { it.deleteOnCascade() } + u.devices.clear() u.devices.addAll(new) } } @@ -309,7 +310,7 @@ internal class RealmCryptoStore @Inject constructor( .let { userEntity -> if (masterKey == null || selfSigningKey == null) { // The user has disabled cross signing? - userEntity.crossSigningInfoEntity?.deleteFromRealm() + userEntity.crossSigningInfoEntity?.deleteOnCascade() userEntity.crossSigningInfoEntity = null } else { var shouldResetMyDevicesLocalTrust = false @@ -1633,7 +1634,8 @@ internal class RealmCryptoStore @Inject constructor( } else { // Just override existing, caller should check and untrust id needed val existing = CrossSigningInfoEntity.getOrCreate(realm, userId) - existing.crossSigningKeys.deleteAllFromRealm() + existing.crossSigningKeys.toList().forEach { it.deleteOnCascade() } + existing.crossSigningKeys.clear() existing.crossSigningKeys.addAll( info.crossSigningKeys.map { crossSigningKeysMapper.map(it) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/CrossSigningInfoEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/CrossSigningInfoEntity.kt index fdd3e94754..5a14ede0ad 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/CrossSigningInfoEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/CrossSigningInfoEntity.kt @@ -16,10 +16,10 @@ package org.matrix.android.sdk.internal.crypto.store.db.model -import org.matrix.android.sdk.internal.crypto.model.KeyUsage import io.realm.RealmList import io.realm.RealmObject import io.realm.annotations.PrimaryKey +import org.matrix.android.sdk.internal.crypto.model.KeyUsage internal open class CrossSigningInfoEntity( @PrimaryKey @@ -29,6 +29,11 @@ internal open class CrossSigningInfoEntity( companion object + fun deleteOnCascade() { + crossSigningKeys.toList().forEach { it.deleteOnCascade() } + deleteFromRealm() + } + fun getMasterKey() = crossSigningKeys.firstOrNull { it.usages.contains(KeyUsage.MASTER.value) } fun setMasterKey(info: KeyInfoEntity?) { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/DeviceInfoEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/DeviceInfoEntity.kt index 571b9bb05f..eddf0b81c6 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/DeviceInfoEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/DeviceInfoEntity.kt @@ -46,4 +46,9 @@ internal open class DeviceInfoEntity( val users: RealmResults? = null companion object + + fun deleteOnCascade() { + trustLevelEntity?.deleteFromRealm() + deleteFromRealm() + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/KeyInfoEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/KeyInfoEntity.kt index 8f2357223e..527adaf847 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/KeyInfoEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/KeyInfoEntity.kt @@ -29,4 +29,10 @@ internal open class KeyInfoEntity( */ var signatures: String? = null, var trustLevelEntity: TrustLevelEntity? = null -) : RealmObject() +) : RealmObject() { + + fun deleteOnCascade() { + trustLevelEntity?.deleteFromRealm() + deleteFromRealm() + } +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/UserEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/UserEntity.kt index 52c30a27cc..a5336ea848 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/UserEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/UserEntity.kt @@ -24,8 +24,14 @@ internal open class UserEntity( @PrimaryKey var userId: String? = null, var devices: RealmList = RealmList(), var crossSigningInfoEntity: CrossSigningInfoEntity? = null, - var deviceTrackingStatus: Int = 0) - : RealmObject() { + var deviceTrackingStatus: Int = 0 +) : RealmObject() { companion object + + fun deleteOnCascade() { + devices.toList().forEach { it.deleteOnCascade() } + crossSigningInfoEntity?.deleteOnCascade() + deleteFromRealm() + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/query/UserEntitiesQueries.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/query/UserEntitiesQueries.kt index a1f8bd7262..4db03e123e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/query/UserEntitiesQueries.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/query/UserEntitiesQueries.kt @@ -16,11 +16,11 @@ package org.matrix.android.sdk.internal.crypto.store.db.query -import org.matrix.android.sdk.internal.crypto.store.db.model.UserEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.UserEntityFields import io.realm.Realm import io.realm.kotlin.createObject import io.realm.kotlin.where +import org.matrix.android.sdk.internal.crypto.store.db.model.UserEntity +import org.matrix.android.sdk.internal.crypto.store.db.model.UserEntityFields /** * Get or create a user @@ -39,5 +39,5 @@ internal fun UserEntity.Companion.delete(realm: Realm, userId: String) { realm.where() .equalTo(UserEntityFields.USER_ID, userId) .findFirst() - ?.deleteFromRealm() + ?.deleteOnCascade() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/DatabaseCleaner.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/DatabaseCleaner.kt index e305c7ea38..e780702229 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/DatabaseCleaner.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/DatabaseCleaner.kt @@ -56,7 +56,7 @@ internal class DatabaseCleaner @Inject constructor(@SessionDatabase private val } } - private suspend fun cleanUp(realm: Realm, threshold: Long) { + private fun cleanUp(realm: Realm, threshold: Long) { val numberOfEvents = realm.where(EventEntity::class.java).findAll().size val numberOfTimelineEvents = realm.where(TimelineEventEntity::class.java).findAll().size Timber.v("Number of events in db: $numberOfEvents | Number of timeline events in db: $numberOfTimelineEvents") @@ -76,20 +76,7 @@ internal class DatabaseCleaner @Inject constructor(@SessionDatabase private val chunk.numberOfTimelineEvents = chunk.numberOfTimelineEvents - eventsToRemove.size eventsToRemove.forEach { val canDeleteRoot = it.root?.stateKey == null - if (canDeleteRoot) { - it.root?.deleteFromRealm() - } - it.readReceipts?.readReceipts?.deleteAllFromRealm() - it.readReceipts?.deleteFromRealm() - it.annotations?.apply { - editSummary?.deleteFromRealm() - pollResponseSummary?.deleteFromRealm() - referencesSummaryEntity?.deleteFromRealm() - reactionsSummary.deleteAllFromRealm() - } - it.annotations?.deleteFromRealm() - it.readReceipts?.deleteFromRealm() - it.deleteFromRealm() + it.deleteOnCascade(canDeleteRoot) } // We reset the prevToken so we will need to fetch again. chunk.prevToken = null diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt index f764c4da4b..b4935cfdcc 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt @@ -38,12 +38,6 @@ import io.realm.Sort import io.realm.kotlin.createObject import timber.log.Timber -internal fun ChunkEntity.deleteOnCascade() { - assertIsManaged() - this.timelineEvents.deleteAllFromRealm() - this.deleteFromRealm() -} - internal fun ChunkEntity.merge(roomId: String, chunkToMerge: ChunkEntity, direction: PaginationDirection) { assertIsManaged() val localRealm = this.realm diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/RoomEntityHelper.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/RoomEntityHelper.kt index a4108f0966..724f307e3b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/RoomEntityHelper.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/RoomEntityHelper.kt @@ -19,12 +19,7 @@ package org.matrix.android.sdk.internal.database.helper import org.matrix.android.sdk.internal.database.model.ChunkEntity import org.matrix.android.sdk.internal.database.model.RoomEntity -internal fun RoomEntity.deleteOnCascade(chunkEntity: ChunkEntity) { - chunks.remove(chunkEntity) - chunkEntity.deleteOnCascade() -} - -internal fun RoomEntity.addOrUpdate(chunkEntity: ChunkEntity) { +internal fun RoomEntity.addIfNecessary(chunkEntity: ChunkEntity) { if (!chunks.contains(chunkEntity)) { chunks.add(chunkEntity) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/TimelineEventEntityHelper.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/TimelineEventEntityHelper.kt index 6f4dac182c..90e867749e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/TimelineEventEntityHelper.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/TimelineEventEntityHelper.kt @@ -18,7 +18,6 @@ package org.matrix.android.sdk.internal.database.helper import org.matrix.android.sdk.internal.database.model.TimelineEventEntity import org.matrix.android.sdk.internal.database.model.TimelineEventEntityFields -import org.matrix.android.sdk.internal.extensions.assertIsManaged import io.realm.Realm internal fun TimelineEventEntity.Companion.nextId(realm: Realm): Long { @@ -29,11 +28,3 @@ internal fun TimelineEventEntity.Companion.nextId(realm: Realm): Long { currentIdNum.toLong() + 1 } } - -internal fun TimelineEventEntity.deleteOnCascade() { - assertIsManaged() - root?.deleteFromRealm() - annotations?.deleteFromRealm() - readReceipts?.deleteFromRealm() - deleteFromRealm() -} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ChunkEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ChunkEntity.kt index 9770352a95..de7333bef2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ChunkEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ChunkEntity.kt @@ -21,6 +21,7 @@ import io.realm.RealmObject import io.realm.RealmResults import io.realm.annotations.Index import io.realm.annotations.LinkingObjects +import org.matrix.android.sdk.internal.extensions.assertIsManaged internal open class ChunkEntity(@Index var prevToken: String? = null, // Because of gaps we can have several chunks with nextToken == null @@ -42,4 +43,13 @@ internal open class ChunkEntity(@Index var prevToken: String? = null, val room: RealmResults? = null companion object + + fun deleteOnCascade(deleteStateEvents: Boolean, canDeleteRoot: Boolean) { + assertIsManaged() + if (deleteStateEvents) { + stateEvents.deleteAllFromRealm() + } + timelineEvents.forEach { it.deleteOnCascade(canDeleteRoot) } + deleteFromRealm() + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/EventAnnotationsSummaryEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/EventAnnotationsSummaryEntity.kt index 3e5e277613..4caf6bdd0d 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/EventAnnotationsSummaryEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/EventAnnotationsSummaryEntity.kt @@ -30,4 +30,12 @@ internal open class EventAnnotationsSummaryEntity( ) : RealmObject() { companion object + + fun deleteOnCascade() { + reactionsSummary.deleteAllFromRealm() + editSummary?.deleteFromRealm() + referencesSummaryEntity?.deleteFromRealm() + pollResponseSummary?.deleteFromRealm() + deleteFromRealm() + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRuleEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRuleEntity.kt index 85375c8064..2a24208d97 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRuleEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRuleEntity.kt @@ -39,4 +39,9 @@ internal open class PushRuleEntity( val parent: RealmResults? = null companion object + + fun deleteOnCascade() { + conditions?.forEach { it.deleteFromRealm() } + deleteFromRealm() + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRulesEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRulesEntity.kt index 21e3510cd2..52fd97b408 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRulesEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRulesEntity.kt @@ -34,4 +34,9 @@ internal open class PushRulesEntity( } companion object + + fun deleteOnCascade() { + pushRules.toList().forEach { it.deleteOnCascade() } + deleteFromRealm() + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PusherEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PusherEntity.kt index f85c01c48a..e4fb474599 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PusherEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PusherEntity.kt @@ -15,8 +15,8 @@ */ package org.matrix.android.sdk.internal.database.model -import org.matrix.android.sdk.api.session.pushers.PusherState import io.realm.RealmObject +import org.matrix.android.sdk.api.session.pushers.PusherState // TODO // at java.lang.Thread.run(Thread.java:764) @@ -53,4 +53,9 @@ internal open class PusherEntity( } companion object + + fun deleteOnCascade() { + data?.deleteFromRealm() + deleteFromRealm() + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ReadReceiptsSummaryEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ReadReceiptsSummaryEntity.kt index 98b4329076..a3693720e4 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ReadReceiptsSummaryEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ReadReceiptsSummaryEntity.kt @@ -33,4 +33,9 @@ internal open class ReadReceiptsSummaryEntity( val timelineEvent: RealmResults? = null companion object + + fun deleteOnCascade() { + readReceipts.deleteAllFromRealm() + deleteFromRealm() + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/TimelineEventEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/TimelineEventEntity.kt index 7bd0dbbb8f..fbafb9c5eb 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/TimelineEventEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/TimelineEventEntity.kt @@ -20,6 +20,7 @@ import io.realm.RealmObject import io.realm.RealmResults import io.realm.annotations.Index import io.realm.annotations.LinkingObjects +import org.matrix.android.sdk.internal.extensions.assertIsManaged internal open class TimelineEventEntity(var localId: Long = 0, @Index var eventId: String = "", @@ -38,4 +39,14 @@ internal open class TimelineEventEntity(var localId: Long = 0, val chunk: RealmResults? = null companion object + + fun deleteOnCascade(canDeleteRoot: Boolean) { + assertIsManaged() + if (canDeleteRoot) { + root?.deleteFromRealm() + } + annotations?.deleteOnCascade() + readReceipts?.deleteOnCascade() + deleteFromRealm() + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/GetPushersTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/GetPushersTask.kt index 4c7d370446..b66e04b4cd 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/GetPushersTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/GetPushersTask.kt @@ -41,7 +41,8 @@ internal class DefaultGetPushersTask @Inject constructor( monarchy.awaitTransaction { realm -> // clear existings? realm.where(PusherEntity::class.java) - .findAll().deleteAllFromRealm() + .findAll() + .forEach { it.deleteOnCascade() } response.pushers?.forEach { jsonPusher -> jsonPusher.toEntity().also { it.state = PusherState.REGISTERED diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/SavePushRulesTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/SavePushRulesTask.kt index 6ba769a3b7..fd433c0803 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/SavePushRulesTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/SavePushRulesTask.kt @@ -40,7 +40,7 @@ internal class DefaultSavePushRulesTask @Inject constructor(@SessionDatabase pri // clear current push rules realm.where(PushRulesEntity::class.java) .findAll() - .deleteAllFromRealm() + .forEach { it.deleteOnCascade() } // Save only global rules for the moment val globalRules = params.pushRules.global diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt index 1a497b8835..19572edc80 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt @@ -22,10 +22,9 @@ import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.room.model.RoomMemberContent import org.matrix.android.sdk.api.session.room.send.SendState -import org.matrix.android.sdk.internal.database.helper.addOrUpdate +import org.matrix.android.sdk.internal.database.helper.addIfNecessary import org.matrix.android.sdk.internal.database.helper.addStateEvent import org.matrix.android.sdk.internal.database.helper.addTimelineEvent -import org.matrix.android.sdk.internal.database.helper.deleteOnCascade import org.matrix.android.sdk.internal.database.helper.merge import org.matrix.android.sdk.internal.database.mapper.toEntity import org.matrix.android.sdk.internal.database.model.ChunkEntity @@ -172,7 +171,7 @@ internal class TokenChunkEventPersistor @Inject constructor(@SessionDatabase pri val currentLastForwardChunk = ChunkEntity.findLastForwardChunkOfRoom(realm, roomId) if (currentChunk != currentLastForwardChunk) { currentChunk.isLastForward = true - currentLastForwardChunk?.deleteOnCascade() + currentLastForwardChunk?.deleteOnCascade(deleteStateEvents = false, canDeleteRoot = false) RoomSummaryEntity.where(realm, roomId).findFirst()?.apply { latestPreviewableEvent = RoomSummaryEventsHelper.getLatestPreviewableEvent(realm, roomId) } @@ -235,7 +234,7 @@ internal class TokenChunkEventPersistor @Inject constructor(@SessionDatabase pri } } chunksToDelete.forEach { - it.deleteOnCascade() + it.deleteOnCascade(deleteStateEvents = false, canDeleteRoot = false) } val roomSummaryEntity = RoomSummaryEntity.getOrCreate(realm, roomId) val shouldUpdateSummary = roomSummaryEntity.latestPreviewableEvent == null @@ -244,7 +243,7 @@ internal class TokenChunkEventPersistor @Inject constructor(@SessionDatabase pri roomSummaryEntity.latestPreviewableEvent = RoomSummaryEventsHelper.getLatestPreviewableEvent(realm, roomId) } if (currentChunk.isValid) { - RoomEntity.where(realm, roomId).findFirst()?.addOrUpdate(currentChunk) + RoomEntity.where(realm, roomId).findFirst()?.addIfNecessary(currentChunk) } } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt index 456b0f9c26..4fb7708ed1 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt @@ -30,9 +30,8 @@ import org.matrix.android.sdk.api.session.room.send.SendState import org.matrix.android.sdk.internal.crypto.DefaultCryptoService import org.matrix.android.sdk.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM import org.matrix.android.sdk.internal.crypto.algorithms.olm.OlmDecryptionResult -import org.matrix.android.sdk.internal.database.helper.addOrUpdate +import org.matrix.android.sdk.internal.database.helper.addIfNecessary import org.matrix.android.sdk.internal.database.helper.addTimelineEvent -import org.matrix.android.sdk.internal.database.helper.deleteOnCascade import org.matrix.android.sdk.internal.database.mapper.asDomain import org.matrix.android.sdk.internal.database.mapper.toEntity import org.matrix.android.sdk.internal.database.model.ChunkEntity @@ -175,7 +174,7 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle syncLocalTimestampMillis, isInitialSync ) - roomEntity.addOrUpdate(chunkEntity) + roomEntity.addIfNecessary(chunkEntity) } val hasRoomMember = roomSync.state?.events?.firstOrNull { it.type == EventType.STATE_ROOM_MEMBER @@ -263,7 +262,8 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle val leftMember = RoomMemberSummaryEntity.where(realm, roomId, userId).findFirst() val membership = leftMember?.membership ?: Membership.LEAVE roomEntity.membership = membership - roomEntity.chunks.deleteAllFromRealm() + roomEntity.chunks.forEach { it.deleteOnCascade(deleteStateEvents = true, canDeleteRoot = true) } + roomEntity.chunks.clear() roomTypingUsersHandler.handle(realm, roomId, null) roomChangeMembershipStateDataSource.setMembershipFromSync(roomId, Membership.LEAVE) roomSummaryUpdater.update(realm, roomId, membership, roomSync.summary, roomSync.unreadNotifications) @@ -340,7 +340,7 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle } } // Finally delete the local echo - sendingEventEntity.deleteOnCascade() + sendingEventEntity.deleteOnCascade(true) } else { Timber.v("Can't find corresponding local echo for tx:$it") } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/UserAccountDataSyncHandler.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/UserAccountDataSyncHandler.kt index 0e549172f3..880dbdcb88 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/UserAccountDataSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/UserAccountDataSyncHandler.kt @@ -113,7 +113,7 @@ internal class UserAccountDataSyncHandler @Inject constructor( val pushRules = event.content.toModel() ?: return realm.where(PushRulesEntity::class.java) .findAll() - .deleteAllFromRealm() + .forEach { it.deleteOnCascade() } // Save only global rules for the moment val globalRules = pushRules.global From 424035f56f3014686feff8a7ee6177ecd7af126b Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 3 Feb 2021 09:07:41 +0100 Subject: [PATCH 3/5] Better clearing of RealmLists --- .../sdk/internal/crypto/store/db/RealmCryptoStore.kt | 7 +++---- .../crypto/store/db/model/CrossSigningInfoEntity.kt | 3 ++- .../sdk/internal/crypto/store/db/model/UserEntity.kt | 3 ++- .../android/sdk/internal/database/model/ChunkEntity.kt | 3 ++- .../sdk/internal/database/model/PushRuleEntity.kt | 2 +- .../sdk/internal/database/model/PushRulesEntity.kt | 3 ++- .../android/sdk/internal/extensions/RealmExtensions.kt | 10 ++++++++++ .../sdk/internal/session/sync/RoomSyncHandler.kt | 4 ++-- 8 files changed, 24 insertions(+), 11 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt index 8dbccb061b..bb14180baa 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt @@ -94,6 +94,7 @@ import org.matrix.android.sdk.internal.di.CryptoDatabase import org.matrix.android.sdk.internal.di.DeviceId import org.matrix.android.sdk.internal.di.MoshiProvider import org.matrix.android.sdk.internal.di.UserId +import org.matrix.android.sdk.internal.extensions.clearWith import org.matrix.android.sdk.internal.session.SessionScope import org.matrix.olm.OlmAccount import org.matrix.olm.OlmException @@ -293,8 +294,7 @@ internal class RealmCryptoStore @Inject constructor( realm.insertOrUpdate(entity) } // Ensure all other devices are deleted - u.devices.toList().forEach { it.deleteOnCascade() } - u.devices.clear() + u.devices.clearWith { it.deleteOnCascade() } u.devices.addAll(new) } } @@ -1634,8 +1634,7 @@ internal class RealmCryptoStore @Inject constructor( } else { // Just override existing, caller should check and untrust id needed val existing = CrossSigningInfoEntity.getOrCreate(realm, userId) - existing.crossSigningKeys.toList().forEach { it.deleteOnCascade() } - existing.crossSigningKeys.clear() + existing.crossSigningKeys.clearWith { it.deleteOnCascade() } existing.crossSigningKeys.addAll( info.crossSigningKeys.map { crossSigningKeysMapper.map(it) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/CrossSigningInfoEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/CrossSigningInfoEntity.kt index 5a14ede0ad..2d91c32d90 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/CrossSigningInfoEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/CrossSigningInfoEntity.kt @@ -20,6 +20,7 @@ import io.realm.RealmList import io.realm.RealmObject import io.realm.annotations.PrimaryKey import org.matrix.android.sdk.internal.crypto.model.KeyUsage +import org.matrix.android.sdk.internal.extensions.clearWith internal open class CrossSigningInfoEntity( @PrimaryKey @@ -30,7 +31,7 @@ internal open class CrossSigningInfoEntity( companion object fun deleteOnCascade() { - crossSigningKeys.toList().forEach { it.deleteOnCascade() } + crossSigningKeys.clearWith { it.deleteOnCascade() } deleteFromRealm() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/UserEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/UserEntity.kt index a5336ea848..a815efd8cb 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/UserEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/UserEntity.kt @@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal.crypto.store.db.model import io.realm.RealmList import io.realm.RealmObject import io.realm.annotations.PrimaryKey +import org.matrix.android.sdk.internal.extensions.clearWith internal open class UserEntity( @PrimaryKey var userId: String? = null, @@ -30,7 +31,7 @@ internal open class UserEntity( companion object fun deleteOnCascade() { - devices.toList().forEach { it.deleteOnCascade() } + devices.clearWith { it.deleteOnCascade() } crossSigningInfoEntity?.deleteOnCascade() deleteFromRealm() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ChunkEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ChunkEntity.kt index de7333bef2..567cb3ca32 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ChunkEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ChunkEntity.kt @@ -22,6 +22,7 @@ import io.realm.RealmResults import io.realm.annotations.Index import io.realm.annotations.LinkingObjects import org.matrix.android.sdk.internal.extensions.assertIsManaged +import org.matrix.android.sdk.internal.extensions.clearWith internal open class ChunkEntity(@Index var prevToken: String? = null, // Because of gaps we can have several chunks with nextToken == null @@ -49,7 +50,7 @@ internal open class ChunkEntity(@Index var prevToken: String? = null, if (deleteStateEvents) { stateEvents.deleteAllFromRealm() } - timelineEvents.forEach { it.deleteOnCascade(canDeleteRoot) } + timelineEvents.clearWith { it.deleteOnCascade(canDeleteRoot) } deleteFromRealm() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRuleEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRuleEntity.kt index 2a24208d97..cf824ba85d 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRuleEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRuleEntity.kt @@ -41,7 +41,7 @@ internal open class PushRuleEntity( companion object fun deleteOnCascade() { - conditions?.forEach { it.deleteFromRealm() } + conditions?.deleteAllFromRealm() deleteFromRealm() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRulesEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRulesEntity.kt index 52fd97b408..3728856663 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRulesEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRulesEntity.kt @@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.database.model import org.matrix.android.sdk.api.pushrules.RuleKind import io.realm.RealmList import io.realm.RealmObject +import org.matrix.android.sdk.internal.extensions.clearWith internal open class PushRulesEntity( var scope: String = "", @@ -36,7 +37,7 @@ internal open class PushRulesEntity( companion object fun deleteOnCascade() { - pushRules.toList().forEach { it.deleteOnCascade() } + pushRules.clearWith { it.deleteOnCascade() } deleteFromRealm() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/extensions/RealmExtensions.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/extensions/RealmExtensions.kt index 0718096fd5..e52e32e16a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/extensions/RealmExtensions.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/extensions/RealmExtensions.kt @@ -16,8 +16,18 @@ package org.matrix.android.sdk.internal.extensions +import io.realm.RealmList import io.realm.RealmObject internal fun RealmObject.assertIsManaged() { check(isManaged) { "${javaClass.simpleName} entity should be managed to use this function" } } + +/** + * Clear a RealmList by deleting all its items calling the provided lambda + */ +internal fun RealmList.clearWith(delete: (T) -> Unit) { + while (!isEmpty()) { + first()?.let { delete.invoke(it) } + } +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt index 4fb7708ed1..2daaf5d81c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt @@ -47,6 +47,7 @@ import org.matrix.android.sdk.internal.database.query.getOrNull import org.matrix.android.sdk.internal.database.query.where import org.matrix.android.sdk.internal.di.MoshiProvider import org.matrix.android.sdk.internal.di.UserId +import org.matrix.android.sdk.internal.extensions.clearWith import org.matrix.android.sdk.internal.session.DefaultInitialSyncProgressService import org.matrix.android.sdk.internal.session.mapWithProgress import org.matrix.android.sdk.internal.session.room.membership.RoomChangeMembershipStateDataSource @@ -262,8 +263,7 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle val leftMember = RoomMemberSummaryEntity.where(realm, roomId, userId).findFirst() val membership = leftMember?.membership ?: Membership.LEAVE roomEntity.membership = membership - roomEntity.chunks.forEach { it.deleteOnCascade(deleteStateEvents = true, canDeleteRoot = true) } - roomEntity.chunks.clear() + roomEntity.chunks.clearWith { it.deleteOnCascade(deleteStateEvents = true, canDeleteRoot = true) } roomTypingUsersHandler.handle(realm, roomId, null) roomChangeMembershipStateDataSource.setMembershipFromSync(roomId, Membership.LEAVE) roomSummaryUpdater.update(realm, roomId, membership, roomSync.summary, roomSync.unreadNotifications) From 30be44077490f19262cacfa05d06577343d812fe Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 8 Feb 2021 12:37:38 +0100 Subject: [PATCH 4/5] Sugar: defined extensions fo deleteOnCascade() methods, as they have to be called in a transaction --- .../store/db/model/CrossSigningInfoEntity.kt | 10 +++++----- .../crypto/store/db/model/DeviceInfoEntity.kt | 10 +++++----- .../crypto/store/db/model/KeyInfoEntity.kt | 9 ++++----- .../crypto/store/db/model/UserEntity.kt | 12 +++++------ .../internal/database/model/ChunkEntity.kt | 18 ++++++++--------- .../model/EventAnnotationsSummaryEntity.kt | 16 +++++++-------- .../internal/database/model/PushRuleEntity.kt | 10 +++++----- .../database/model/PushRulesEntity.kt | 10 +++++----- .../internal/database/model/PusherEntity.kt | 10 +++++----- .../model/ReadReceiptsSummaryEntity.kt | 10 +++++----- .../database/model/TimelineEventEntity.kt | 20 +++++++++---------- 11 files changed, 67 insertions(+), 68 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/CrossSigningInfoEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/CrossSigningInfoEntity.kt index 2d91c32d90..8599c972e9 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/CrossSigningInfoEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/CrossSigningInfoEntity.kt @@ -30,11 +30,6 @@ internal open class CrossSigningInfoEntity( companion object - fun deleteOnCascade() { - crossSigningKeys.clearWith { it.deleteOnCascade() } - deleteFromRealm() - } - fun getMasterKey() = crossSigningKeys.firstOrNull { it.usages.contains(KeyUsage.MASTER.value) } fun setMasterKey(info: KeyInfoEntity?) { @@ -62,3 +57,8 @@ internal open class CrossSigningInfoEntity( info?.let { crossSigningKeys.add(it) } } } + +internal fun CrossSigningInfoEntity.deleteOnCascade() { + crossSigningKeys.clearWith { it.deleteOnCascade() } + deleteFromRealm() +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/DeviceInfoEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/DeviceInfoEntity.kt index eddf0b81c6..61870ec486 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/DeviceInfoEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/DeviceInfoEntity.kt @@ -46,9 +46,9 @@ internal open class DeviceInfoEntity( val users: RealmResults? = null companion object - - fun deleteOnCascade() { - trustLevelEntity?.deleteFromRealm() - deleteFromRealm() - } +} + +internal fun DeviceInfoEntity.deleteOnCascade() { + trustLevelEntity?.deleteFromRealm() + deleteFromRealm() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/KeyInfoEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/KeyInfoEntity.kt index 527adaf847..9133413589 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/KeyInfoEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/KeyInfoEntity.kt @@ -29,10 +29,9 @@ internal open class KeyInfoEntity( */ var signatures: String? = null, var trustLevelEntity: TrustLevelEntity? = null -) : RealmObject() { +) : RealmObject() - fun deleteOnCascade() { - trustLevelEntity?.deleteFromRealm() - deleteFromRealm() - } +internal fun KeyInfoEntity.deleteOnCascade() { + trustLevelEntity?.deleteFromRealm() + deleteFromRealm() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/UserEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/UserEntity.kt index a815efd8cb..df9482bf96 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/UserEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/UserEntity.kt @@ -29,10 +29,10 @@ internal open class UserEntity( ) : RealmObject() { companion object - - fun deleteOnCascade() { - devices.clearWith { it.deleteOnCascade() } - crossSigningInfoEntity?.deleteOnCascade() - deleteFromRealm() - } +} + +internal fun UserEntity.deleteOnCascade() { + devices.clearWith { it.deleteOnCascade() } + crossSigningInfoEntity?.deleteOnCascade() + deleteFromRealm() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ChunkEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ChunkEntity.kt index 567cb3ca32..68533a3c19 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ChunkEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ChunkEntity.kt @@ -44,13 +44,13 @@ internal open class ChunkEntity(@Index var prevToken: String? = null, val room: RealmResults? = null companion object - - fun deleteOnCascade(deleteStateEvents: Boolean, canDeleteRoot: Boolean) { - assertIsManaged() - if (deleteStateEvents) { - stateEvents.deleteAllFromRealm() - } - timelineEvents.clearWith { it.deleteOnCascade(canDeleteRoot) } - deleteFromRealm() - } +} + +internal fun ChunkEntity.deleteOnCascade(deleteStateEvents: Boolean, canDeleteRoot: Boolean) { + assertIsManaged() + if (deleteStateEvents) { + stateEvents.deleteAllFromRealm() + } + timelineEvents.clearWith { it.deleteOnCascade(canDeleteRoot) } + deleteFromRealm() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/EventAnnotationsSummaryEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/EventAnnotationsSummaryEntity.kt index 4caf6bdd0d..33f26d439f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/EventAnnotationsSummaryEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/EventAnnotationsSummaryEntity.kt @@ -30,12 +30,12 @@ internal open class EventAnnotationsSummaryEntity( ) : RealmObject() { companion object - - fun deleteOnCascade() { - reactionsSummary.deleteAllFromRealm() - editSummary?.deleteFromRealm() - referencesSummaryEntity?.deleteFromRealm() - pollResponseSummary?.deleteFromRealm() - deleteFromRealm() - } +} + +internal fun EventAnnotationsSummaryEntity.deleteOnCascade() { + reactionsSummary.deleteAllFromRealm() + editSummary?.deleteFromRealm() + referencesSummaryEntity?.deleteFromRealm() + pollResponseSummary?.deleteFromRealm() + deleteFromRealm() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRuleEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRuleEntity.kt index cf824ba85d..5bfe2833f8 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRuleEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRuleEntity.kt @@ -39,9 +39,9 @@ internal open class PushRuleEntity( val parent: RealmResults? = null companion object - - fun deleteOnCascade() { - conditions?.deleteAllFromRealm() - deleteFromRealm() - } +} + +internal fun PushRuleEntity.deleteOnCascade() { + conditions?.deleteAllFromRealm() + deleteFromRealm() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRulesEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRulesEntity.kt index 3728856663..571bc71c27 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRulesEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PushRulesEntity.kt @@ -35,9 +35,9 @@ internal open class PushRulesEntity( } companion object - - fun deleteOnCascade() { - pushRules.clearWith { it.deleteOnCascade() } - deleteFromRealm() - } +} + +internal fun PushRulesEntity.deleteOnCascade() { + pushRules.clearWith { it.deleteOnCascade() } + deleteFromRealm() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PusherEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PusherEntity.kt index e4fb474599..af8e4f2d37 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PusherEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/PusherEntity.kt @@ -53,9 +53,9 @@ internal open class PusherEntity( } companion object - - fun deleteOnCascade() { - data?.deleteFromRealm() - deleteFromRealm() - } +} + +internal fun PusherEntity.deleteOnCascade() { + data?.deleteFromRealm() + deleteFromRealm() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ReadReceiptsSummaryEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ReadReceiptsSummaryEntity.kt index a3693720e4..9ca4adc33e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ReadReceiptsSummaryEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/ReadReceiptsSummaryEntity.kt @@ -33,9 +33,9 @@ internal open class ReadReceiptsSummaryEntity( val timelineEvent: RealmResults? = null companion object - - fun deleteOnCascade() { - readReceipts.deleteAllFromRealm() - deleteFromRealm() - } +} + +internal fun ReadReceiptsSummaryEntity.deleteOnCascade() { + readReceipts.deleteAllFromRealm() + deleteFromRealm() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/TimelineEventEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/TimelineEventEntity.kt index fbafb9c5eb..30bbde70c2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/TimelineEventEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/TimelineEventEntity.kt @@ -39,14 +39,14 @@ internal open class TimelineEventEntity(var localId: Long = 0, val chunk: RealmResults? = null companion object - - fun deleteOnCascade(canDeleteRoot: Boolean) { - assertIsManaged() - if (canDeleteRoot) { - root?.deleteFromRealm() - } - annotations?.deleteOnCascade() - readReceipts?.deleteOnCascade() - deleteFromRealm() - } +} + +internal fun TimelineEventEntity.deleteOnCascade(canDeleteRoot: Boolean) { + assertIsManaged() + if (canDeleteRoot) { + root?.deleteFromRealm() + } + annotations?.deleteOnCascade() + readReceipts?.deleteOnCascade() + deleteFromRealm() } From f2619df13d40b9e869ca94c997a8d938f5dce8f5 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 8 Feb 2021 14:25:33 +0100 Subject: [PATCH 5/5] Import and cleanup --- .../sdk/internal/crypto/store/db/RealmCryptoStore.kt | 1 + .../crypto/store/db/query/UserEntitiesQueries.kt | 1 + .../android/sdk/internal/database/DatabaseCleaner.kt | 9 +++++---- .../sdk/internal/session/pushers/GetPushersTask.kt | 1 + .../sdk/internal/session/pushers/SavePushRulesTask.kt | 1 + .../session/room/timeline/TokenChunkEventPersistor.kt | 1 + .../android/sdk/internal/session/sync/RoomSyncHandler.kt | 1 + .../internal/session/sync/UserAccountDataSyncHandler.kt | 9 ++++----- 8 files changed, 15 insertions(+), 9 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt index bb14180baa..b9213ba758 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt @@ -83,6 +83,7 @@ import org.matrix.android.sdk.internal.crypto.store.db.model.UserEntity import org.matrix.android.sdk.internal.crypto.store.db.model.UserEntityFields import org.matrix.android.sdk.internal.crypto.store.db.model.WithHeldSessionEntity import org.matrix.android.sdk.internal.crypto.store.db.model.createPrimaryKey +import org.matrix.android.sdk.internal.crypto.store.db.model.deleteOnCascade import org.matrix.android.sdk.internal.crypto.store.db.query.create import org.matrix.android.sdk.internal.crypto.store.db.query.delete import org.matrix.android.sdk.internal.crypto.store.db.query.get diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/query/UserEntitiesQueries.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/query/UserEntitiesQueries.kt index 4db03e123e..5a3b8e5397 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/query/UserEntitiesQueries.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/query/UserEntitiesQueries.kt @@ -21,6 +21,7 @@ import io.realm.kotlin.createObject import io.realm.kotlin.where import org.matrix.android.sdk.internal.crypto.store.db.model.UserEntity import org.matrix.android.sdk.internal.crypto.store.db.model.UserEntityFields +import org.matrix.android.sdk.internal.crypto.store.db.model.deleteOnCascade /** * Get or create a user diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/DatabaseCleaner.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/DatabaseCleaner.kt index e780702229..8b4ce6106b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/DatabaseCleaner.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/DatabaseCleaner.kt @@ -16,6 +16,10 @@ package org.matrix.android.sdk.internal.database +import io.realm.Realm +import io.realm.RealmConfiguration +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import org.matrix.android.sdk.internal.database.helper.nextDisplayIndex import org.matrix.android.sdk.internal.database.model.ChunkEntity import org.matrix.android.sdk.internal.database.model.ChunkEntityFields @@ -23,14 +27,11 @@ import org.matrix.android.sdk.internal.database.model.EventEntity import org.matrix.android.sdk.internal.database.model.RoomEntity import org.matrix.android.sdk.internal.database.model.TimelineEventEntity import org.matrix.android.sdk.internal.database.model.TimelineEventEntityFields +import org.matrix.android.sdk.internal.database.model.deleteOnCascade import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.session.SessionLifecycleObserver import org.matrix.android.sdk.internal.session.room.timeline.PaginationDirection import org.matrix.android.sdk.internal.task.TaskExecutor -import io.realm.Realm -import io.realm.RealmConfiguration -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch import timber.log.Timber import javax.inject.Inject diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/GetPushersTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/GetPushersTask.kt index b66e04b4cd..125c8f0022 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/GetPushersTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/GetPushersTask.kt @@ -19,6 +19,7 @@ import com.zhuinden.monarchy.Monarchy import org.matrix.android.sdk.api.session.pushers.PusherState import org.matrix.android.sdk.internal.database.mapper.toEntity import org.matrix.android.sdk.internal.database.model.PusherEntity +import org.matrix.android.sdk.internal.database.model.deleteOnCascade import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.network.GlobalErrorReceiver import org.matrix.android.sdk.internal.network.executeRequest diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/SavePushRulesTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/SavePushRulesTask.kt index fd433c0803..6a4b891ecf 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/SavePushRulesTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/SavePushRulesTask.kt @@ -21,6 +21,7 @@ import org.matrix.android.sdk.api.pushrules.RuleSetKey import org.matrix.android.sdk.api.pushrules.rest.GetPushRulesResponse import org.matrix.android.sdk.internal.database.mapper.PushRulesMapper import org.matrix.android.sdk.internal.database.model.PushRulesEntity +import org.matrix.android.sdk.internal.database.model.deleteOnCascade import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.task.Task import org.matrix.android.sdk.internal.util.awaitTransaction diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt index 19572edc80..c38dcd00a7 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt @@ -31,6 +31,7 @@ import org.matrix.android.sdk.internal.database.model.ChunkEntity import org.matrix.android.sdk.internal.database.model.EventInsertType import org.matrix.android.sdk.internal.database.model.RoomEntity import org.matrix.android.sdk.internal.database.model.RoomSummaryEntity +import org.matrix.android.sdk.internal.database.model.deleteOnCascade import org.matrix.android.sdk.internal.database.query.copyToRealmOrIgnore import org.matrix.android.sdk.internal.database.query.create import org.matrix.android.sdk.internal.database.query.find diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt index 2daaf5d81c..6d1b3ae034 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt @@ -39,6 +39,7 @@ import org.matrix.android.sdk.internal.database.model.CurrentStateEventEntity import org.matrix.android.sdk.internal.database.model.EventInsertType import org.matrix.android.sdk.internal.database.model.RoomEntity import org.matrix.android.sdk.internal.database.model.RoomMemberSummaryEntity +import org.matrix.android.sdk.internal.database.model.deleteOnCascade import org.matrix.android.sdk.internal.database.query.copyToRealmOrIgnore import org.matrix.android.sdk.internal.database.query.find import org.matrix.android.sdk.internal.database.query.findLastForwardChunkOfRoom diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/UserAccountDataSyncHandler.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/UserAccountDataSyncHandler.kt index 880dbdcb88..449d47abe5 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/UserAccountDataSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/UserAccountDataSyncHandler.kt @@ -16,8 +16,10 @@ package org.matrix.android.sdk.internal.session.sync -import com.squareup.moshi.Moshi import com.zhuinden.monarchy.Monarchy +import io.realm.Realm +import io.realm.RealmList +import io.realm.kotlin.where import org.matrix.android.sdk.api.pushrules.RuleScope import org.matrix.android.sdk.api.pushrules.RuleSetKey import org.matrix.android.sdk.api.pushrules.rest.GetPushRulesResponse @@ -37,6 +39,7 @@ import org.matrix.android.sdk.internal.database.model.RoomSummaryEntity import org.matrix.android.sdk.internal.database.model.RoomSummaryEntityFields import org.matrix.android.sdk.internal.database.model.UserAccountDataEntity import org.matrix.android.sdk.internal.database.model.UserAccountDataEntityFields +import org.matrix.android.sdk.internal.database.model.deleteOnCascade import org.matrix.android.sdk.internal.database.query.getDirectRooms import org.matrix.android.sdk.internal.database.query.getOrCreate import org.matrix.android.sdk.internal.database.query.where @@ -50,9 +53,6 @@ import org.matrix.android.sdk.internal.session.sync.model.accountdata.IgnoredUse import org.matrix.android.sdk.internal.session.sync.model.accountdata.UserAccountDataSync import org.matrix.android.sdk.internal.session.user.accountdata.DirectChatsHelper import org.matrix.android.sdk.internal.session.user.accountdata.UpdateUserAccountDataTask -import io.realm.Realm -import io.realm.RealmList -import io.realm.kotlin.where import timber.log.Timber import javax.inject.Inject @@ -60,7 +60,6 @@ internal class UserAccountDataSyncHandler @Inject constructor( @SessionDatabase private val monarchy: Monarchy, @UserId private val userId: String, private val directChatsHelper: DirectChatsHelper, - private val moshi: Moshi, private val updateUserAccountDataTask: UpdateUserAccountDataTask) { fun handle(realm: Realm, accountData: UserAccountDataSync?) {