Delete on cascade

This commit is contained in:
Benoit Marty 2021-02-02 21:23:16 +01:00 committed by Benoit Marty
parent cfdfd3a8af
commit 3b65761110
23 changed files with 101 additions and 60 deletions

View File

@ -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 🗣:
-

View File

@ -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)

View File

@ -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?) {

View File

@ -46,4 +46,9 @@ internal open class DeviceInfoEntity(
val users: RealmResults<UserEntity>? = null
companion object
fun deleteOnCascade() {
trustLevelEntity?.deleteFromRealm()
deleteFromRealm()
}
}

View File

@ -29,4 +29,10 @@ internal open class KeyInfoEntity(
*/
var signatures: String? = null,
var trustLevelEntity: TrustLevelEntity? = null
) : RealmObject()
) : RealmObject() {
fun deleteOnCascade() {
trustLevelEntity?.deleteFromRealm()
deleteFromRealm()
}
}

View File

@ -24,8 +24,14 @@ internal open class UserEntity(
@PrimaryKey var userId: String? = null,
var devices: RealmList<DeviceInfoEntity> = 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()
}
}

View File

@ -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<UserEntity>()
.equalTo(UserEntityFields.USER_ID, userId)
.findFirst()
?.deleteFromRealm()
?.deleteOnCascade()
}

View File

@ -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

View File

@ -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

View File

@ -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)
}

View File

@ -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()
}

View File

@ -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<RoomEntity>? = null
companion object
fun deleteOnCascade(deleteStateEvents: Boolean, canDeleteRoot: Boolean) {
assertIsManaged()
if (deleteStateEvents) {
stateEvents.deleteAllFromRealm()
}
timelineEvents.forEach { it.deleteOnCascade(canDeleteRoot) }
deleteFromRealm()
}
}

View File

@ -30,4 +30,12 @@ internal open class EventAnnotationsSummaryEntity(
) : RealmObject() {
companion object
fun deleteOnCascade() {
reactionsSummary.deleteAllFromRealm()
editSummary?.deleteFromRealm()
referencesSummaryEntity?.deleteFromRealm()
pollResponseSummary?.deleteFromRealm()
deleteFromRealm()
}
}

View File

@ -39,4 +39,9 @@ internal open class PushRuleEntity(
val parent: RealmResults<PushRulesEntity>? = null
companion object
fun deleteOnCascade() {
conditions?.forEach { it.deleteFromRealm() }
deleteFromRealm()
}
}

View File

@ -34,4 +34,9 @@ internal open class PushRulesEntity(
}
companion object
fun deleteOnCascade() {
pushRules.toList().forEach { it.deleteOnCascade() }
deleteFromRealm()
}
}

View File

@ -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()
}
}

View File

@ -33,4 +33,9 @@ internal open class ReadReceiptsSummaryEntity(
val timelineEvent: RealmResults<TimelineEventEntity>? = null
companion object
fun deleteOnCascade() {
readReceipts.deleteAllFromRealm()
deleteFromRealm()
}
}

View File

@ -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<ChunkEntity>? = null
companion object
fun deleteOnCascade(canDeleteRoot: Boolean) {
assertIsManaged()
if (canDeleteRoot) {
root?.deleteFromRealm()
}
annotations?.deleteOnCascade()
readReceipts?.deleteOnCascade()
deleteFromRealm()
}
}

View File

@ -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

View File

@ -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

View File

@ -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)
}
}
}

View File

@ -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")
}

View File

@ -113,7 +113,7 @@ internal class UserAccountDataSyncHandler @Inject constructor(
val pushRules = event.content.toModel<GetPushRulesResponse>() ?: return
realm.where(PushRulesEntity::class.java)
.findAll()
.deleteAllFromRealm()
.forEach { it.deleteOnCascade() }
// Save only global rules for the moment
val globalRules = pushRules.global