From f493ce44f21f3d2c0826f55ac5d5bc2c93ddffca Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 11 Jul 2019 15:30:01 +0200 Subject: [PATCH] RealmLiveEntity: passes the results and changeSet instead of filtering as it's more efficient --- .../database/RealmLiveEntityObserver.kt | 22 +++---------------- .../session/group/GroupSummaryUpdater.kt | 16 +++++++++++--- .../room/EventRelationsAggregationUpdater.kt | 19 ++++++++-------- .../session/room/prune/EventsPruner.kt | 20 ++++++++++------- .../session/user/UserEntityUpdater.kt | 14 +++++++----- 5 files changed, 47 insertions(+), 44 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/RealmLiveEntityObserver.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/RealmLiveEntityObserver.kt index 8be0e697fb..af842c67da 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/RealmLiveEntityObserver.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/RealmLiveEntityObserver.kt @@ -19,6 +19,7 @@ package im.vector.matrix.android.internal.database import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.internal.util.createBackgroundHandler import io.realm.OrderedCollectionChangeSet +import io.realm.OrderedRealmCollectionChangeListener import io.realm.Realm import io.realm.RealmConfiguration import io.realm.RealmObject @@ -33,7 +34,7 @@ internal interface LiveEntityObserver { } internal abstract class RealmLiveEntityObserver(protected val realmConfiguration: RealmConfiguration) - : LiveEntityObserver { + : LiveEntityObserver, OrderedRealmCollectionChangeListener> { private companion object { val BACKGROUND_HANDLER = createBackgroundHandler("LIVE_ENTITY_BACKGROUND") @@ -50,9 +51,7 @@ internal abstract class RealmLiveEntityObserver(protected val r val realm = Realm.getInstance(realmConfiguration) backgroundRealm.set(realm) val queryResults = query.createQuery(realm).findAll() - queryResults.addChangeListener { t, changeSet -> - onChanged(t, changeSet) - } + queryResults.addChangeListener(this) results = AtomicReference(queryResults) } } @@ -73,19 +72,4 @@ internal abstract class RealmLiveEntityObserver(protected val r return isStarted.get() } - private fun onChanged(realmResults: RealmResults, changeSet: OrderedCollectionChangeSet) { - val insertionIndexes = changeSet.insertions - val updateIndexes = changeSet.changes - val deletionIndexes = changeSet.deletions - val inserted = realmResults.filterIndexed { index, _ -> insertionIndexes.contains(index) } - val updated = realmResults.filterIndexed { index, _ -> updateIndexes.contains(index) } - val deleted = realmResults.filterIndexed { index, _ -> deletionIndexes.contains(index) } - processChanges(inserted, updated, deleted) - } - - /** - * Do quick treatment or delegate on a task - */ - protected abstract fun processChanges(inserted: List, updated: List, deleted: List) - } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupSummaryUpdater.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupSummaryUpdater.kt index 5418448019..fcfe9f0ade 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupSummaryUpdater.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupSummaryUpdater.kt @@ -22,13 +22,20 @@ import androidx.work.WorkManager import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.auth.data.Credentials import im.vector.matrix.android.internal.database.RealmLiveEntityObserver +import im.vector.matrix.android.internal.database.mapper.asDomain +import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.model.GroupEntity import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.di.SessionDatabase +import im.vector.matrix.android.internal.session.room.prune.PruneEventTask +import im.vector.matrix.android.internal.task.configureWith import im.vector.matrix.android.internal.worker.WorkManagerUtil import im.vector.matrix.android.internal.worker.WorkManagerUtil.matrixOneTimeWorkRequestBuilder import im.vector.matrix.android.internal.worker.WorkerParamsFactory +import io.realm.OrderedCollectionChangeSet import io.realm.RealmConfiguration +import io.realm.RealmResults +import timber.log.Timber import javax.inject.Inject private const val GET_GROUP_DATA_WORKER = "GET_GROUP_DATA_WORKER" @@ -40,8 +47,12 @@ internal class GroupSummaryUpdater @Inject constructor(private val context: Cont override val query = Monarchy.Query { GroupEntity.where(it) } - override fun processChanges(inserted: List, updated: List, deleted: List) { - val newGroupIds = inserted.map { it.groupId } + override fun onChange(results: RealmResults, changeSet: OrderedCollectionChangeSet) { + val newGroupIds = changeSet.insertions + .asSequence() + .mapNotNull { results[it]?.groupId} + .toList() + val getGroupDataWorkerParams = GetGroupDataWorker.Params(credentials.userId, newGroupIds) val workData = WorkerParamsFactory.toData(getGroupDataWorkerParams) @@ -55,5 +66,4 @@ internal class GroupSummaryUpdater @Inject constructor(private val context: Cont .enqueue() } - } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/EventRelationsAggregationUpdater.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/EventRelationsAggregationUpdater.kt index 76fea187b3..9d642d3a89 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/EventRelationsAggregationUpdater.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/EventRelationsAggregationUpdater.kt @@ -25,7 +25,9 @@ import im.vector.matrix.android.internal.database.query.types import im.vector.matrix.android.internal.di.SessionDatabase import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith +import io.realm.OrderedCollectionChangeSet import io.realm.RealmConfiguration +import io.realm.RealmResults import timber.log.Timber import javax.inject.Inject @@ -50,19 +52,18 @@ internal class EventRelationsAggregationUpdater @Inject constructor(@SessionData ) } - override fun processChanges(inserted: List, updated: List, deleted: List) { - Timber.v("EventRelationsAggregationUpdater called with ${inserted.size} insertions") - val domainInserted = inserted - .map { it.asDomain() } + override fun onChange(results: RealmResults, changeSet: OrderedCollectionChangeSet) { + Timber.v("EventRelationsAggregationUpdater called with ${changeSet.insertions.size} insertions") + val insertedDomains = changeSet.insertions + .asSequence() + .mapNotNull { results[it]?.asDomain() } + .toList() val params = EventRelationsAggregationTask.Params( - domainInserted, + insertedDomains, credentials.userId ) - - task.configureWith(params) - .executeBy(taskExecutor) - + task.configureWith(params).executeBy(taskExecutor) } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/EventsPruner.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/EventsPruner.kt index 630332874d..91d3c4e7c8 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/EventsPruner.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/EventsPruner.kt @@ -26,9 +26,12 @@ import im.vector.matrix.android.internal.database.query.types import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.di.SessionDatabase import im.vector.matrix.android.internal.session.SessionScope +import im.vector.matrix.android.internal.session.room.EventRelationsAggregationTask import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith +import io.realm.OrderedCollectionChangeSet import io.realm.RealmConfiguration +import io.realm.RealmResults import timber.log.Timber import javax.inject.Inject @@ -44,18 +47,19 @@ internal class EventsPruner @Inject constructor(@SessionDatabase realmConfigurat override val query = Monarchy.Query { EventEntity.types(it, listOf(EventType.REDACTION)) } - override fun processChanges(inserted: List, updated: List, deleted: List) { - Timber.v("Event pruner called with ${inserted.size} insertions") - val redactionEvents = inserted.map { it.asDomain() } + override fun onChange(results: RealmResults, changeSet: OrderedCollectionChangeSet) { + Timber.v("Event pruner called with ${changeSet.insertions.size} insertions") + + val insertedDomains = changeSet.insertions + .asSequence() + .mapNotNull { results[it]?.asDomain() } + .toList() val params = PruneEventTask.Params( - redactionEvents, + insertedDomains, credentials.userId ) - - pruneEventTask.configureWith(params) - .executeBy(taskExecutor) - + pruneEventTask.configureWith(params).executeBy(taskExecutor) } } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserEntityUpdater.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserEntityUpdater.kt index 254381f6ac..5c722863c9 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserEntityUpdater.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserEntityUpdater.kt @@ -22,13 +22,13 @@ import im.vector.matrix.android.internal.database.RealmLiveEntityObserver import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.model.EventEntityFields import im.vector.matrix.android.internal.database.query.types -import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.di.SessionDatabase -import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.TaskThread import im.vector.matrix.android.internal.task.configureWith +import io.realm.OrderedCollectionChangeSet import io.realm.RealmConfiguration +import io.realm.RealmResults import io.realm.Sort import javax.inject.Inject @@ -42,15 +42,19 @@ internal class UserEntityUpdater @Inject constructor(@SessionDatabase realmConfi .types(it, listOf(EventType.STATE_ROOM_MEMBER)) .sort(EventEntityFields.STATE_INDEX, Sort.DESCENDING) .distinct(EventEntityFields.STATE_KEY) - } - override fun processChanges(inserted: List, updated: List, deleted: List) { - val roomMembersEvents = inserted.map { it.eventId } + override fun onChange(results: RealmResults, changeSet: OrderedCollectionChangeSet) { + val roomMembersEvents = changeSet.insertions + .asSequence() + .mapNotNull { results[it]?.eventId } + .toList() + val taskParams = UpdateUserTask.Params(roomMembersEvents) updateUserTask .configureWith(taskParams) .executeOn(TaskThread.IO) .executeBy(taskExecutor) } + } \ No newline at end of file