Merge pull request #6410 from vector-im/feature/fga/load_room_members_by_chunk

Feature/fga/load room members by chunk
This commit is contained in:
ganfra 2022-06-30 08:56:43 +02:00 committed by GitHub
commit 778269f831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 39 deletions

1
changelog.d/6394.misc Normal file
View File

@ -0,0 +1 @@
Let LoadRoomMembersTask insert by chunk to release db.

View File

@ -58,7 +58,7 @@ internal class DefaultMembershipService @AssistedInject constructor(
} }
override suspend fun loadRoomMembersIfNeeded() { override suspend fun loadRoomMembersIfNeeded() {
val params = LoadRoomMembersTask.Params(roomId, Membership.LEAVE) val params = LoadRoomMembersTask.Params(roomId, excludeMembership = Membership.LEAVE)
loadRoomMembersTask.execute(params) loadRoomMembersTask.execute(params)
} }

View File

@ -43,6 +43,7 @@ import org.matrix.android.sdk.internal.session.sync.SyncTokenStore
import org.matrix.android.sdk.internal.task.Task import org.matrix.android.sdk.internal.task.Task
import org.matrix.android.sdk.internal.util.awaitTransaction import org.matrix.android.sdk.internal.util.awaitTransaction
import org.matrix.android.sdk.internal.util.time.Clock import org.matrix.android.sdk.internal.util.time.Clock
import timber.log.Timber
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
@ -105,12 +106,13 @@ internal class DefaultLoadRoomMembersTask @Inject constructor(
} }
private suspend fun insertInDb(response: RoomMembersResponse, roomId: String) { private suspend fun insertInDb(response: RoomMembersResponse, roomId: String) {
val chunks = response.roomMemberEvents.chunked(500)
chunks.forEach { roomMemberEvents ->
monarchy.awaitTransaction { realm -> monarchy.awaitTransaction { realm ->
Timber.v("Insert ${roomMemberEvents.size} member events in room $roomId")
// We ignore all the already known members // We ignore all the already known members
val roomEntity = RoomEntity.where(realm, roomId).findFirst()
?: realm.createObject(roomId)
val now = clock.epochMillis() val now = clock.epochMillis()
for (roomMemberEvent in response.roomMemberEvents) { for (roomMemberEvent in roomMemberEvents) {
if (roomMemberEvent.eventId == null || roomMemberEvent.stateKey == null || roomMemberEvent.type == null) { if (roomMemberEvent.eventId == null || roomMemberEvent.stateKey == null || roomMemberEvent.type == null) {
continue continue
} }
@ -127,10 +129,14 @@ internal class DefaultLoadRoomMembersTask @Inject constructor(
} }
roomMemberEventHandler.handle(realm, roomId, roomMemberEvent, false) roomMemberEventHandler.handle(realm, roomId, roomMemberEvent, false)
} }
}
}
monarchy.awaitTransaction { realm ->
val roomEntity = RoomEntity.where(realm, roomId).findFirst()
?: realm.createObject(roomId)
roomEntity.membersLoadStatus = RoomMembersLoadStatusType.LOADED roomEntity.membersLoadStatus = RoomMembersLoadStatusType.LOADED
roomSummaryUpdater.update(realm, roomId, updateMembers = true) roomSummaryUpdater.update(realm, roomId, updateMembers = true)
} }
if (cryptoSessionInfoProvider.isRoomEncrypted(roomId)) { if (cryptoSessionInfoProvider.isRoomEncrypted(roomId)) {
deviceListManager.onRoomMembersLoadedFor(roomId) deviceListManager.onRoomMembersLoadedFor(roomId)
} }

View File

@ -83,23 +83,21 @@ internal class RoomMemberEventHandler @Inject constructor(
userId: String, userId: String,
roomMember: RoomMemberContent roomMember: RoomMemberContent
) { ) {
val existingRoomMemberSummary = RoomMemberSummaryEntity.where(realm, roomId, userId).findFirst()
if (existingRoomMemberSummary != null) {
existingRoomMemberSummary.displayName = roomMember.displayName
existingRoomMemberSummary.avatarUrl = roomMember.avatarUrl
existingRoomMemberSummary.membership = roomMember.membership
} else {
val presenceEntity = UserPresenceEntity.where(realm, userId).findFirst()
val roomMemberEntity = RoomMemberEntityFactory.create( val roomMemberEntity = RoomMemberEntityFactory.create(
roomId, roomId,
userId, userId,
roomMember, roomMember,
// When an update is happening, insertOrUpdate replace existing values with null if they are not provided, presenceEntity
// but we want to preserve presence record value and not replace it with null
getExistingPresenceState(realm, roomId, userId)
) )
realm.insertOrUpdate(roomMemberEntity) realm.insert(roomMemberEntity)
} }
/**
* Get the already existing presence state for a specific user & room in order NOT to be replaced in RoomMemberSummaryEntity
* by NULL value.
*/
private fun getExistingPresenceState(realm: Realm, roomId: String, userId: String): UserPresenceEntity? {
return RoomMemberSummaryEntity.where(realm, roomId, userId).findFirst()?.userPresenceEntity
} }
private fun saveUserEntityLocallyIfNecessary( private fun saveUserEntityLocallyIfNecessary(

View File

@ -33,6 +33,7 @@ import kotlinx.coroutines.withContext
import okhttp3.internal.closeQuietly import okhttp3.internal.closeQuietly
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.timeline.Timeline import org.matrix.android.sdk.api.session.room.timeline.Timeline
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings
@ -388,7 +389,7 @@ internal class DefaultTimeline(
} }
private suspend fun loadRoomMembersIfNeeded() { private suspend fun loadRoomMembersIfNeeded() {
val loadRoomMembersParam = LoadRoomMembersTask.Params(roomId) val loadRoomMembersParam = LoadRoomMembersTask.Params(roomId, excludeMembership = Membership.LEAVE)
try { try {
loadRoomMembersTask.execute(loadRoomMembersParam) loadRoomMembersTask.execute(loadRoomMembersParam)
} catch (failure: Throwable) { } catch (failure: Throwable) {