Merge pull request #5813 from vector-im/feature/bma/query

Improve code in file TimelineEventEntityQueries.kt
This commit is contained in:
Benoit Marty 2022-04-21 21:11:30 +02:00 committed by GitHub
commit 96350b0ed0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 18 deletions

View file

@ -19,9 +19,10 @@ package org.matrix.android.sdk.internal.database.helper
import io.realm.Realm
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.query.where
internal fun TimelineEventEntity.Companion.nextId(realm: Realm): Long {
val currentIdNum = realm.where(TimelineEventEntity::class.java).max(TimelineEventEntityFields.LOCAL_ID)
val currentIdNum = TimelineEventEntity.where(realm).max(TimelineEventEntityFields.LOCAL_ID)
return if (currentIdNum == null) {
1
} else {

View file

@ -29,26 +29,35 @@ 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
internal fun TimelineEventEntity.Companion.where(realm: Realm, roomId: String, eventId: String): RealmQuery<TimelineEventEntity> {
return realm.where<TimelineEventEntity>()
internal fun TimelineEventEntity.Companion.where(realm: Realm): RealmQuery<TimelineEventEntity> {
return realm.where()
}
internal fun TimelineEventEntity.Companion.where(realm: Realm,
roomId: String,
eventId: String): RealmQuery<TimelineEventEntity> {
return where(realm)
.equalTo(TimelineEventEntityFields.ROOM_ID, roomId)
.equalTo(TimelineEventEntityFields.EVENT_ID, eventId)
}
internal fun TimelineEventEntity.Companion.where(realm: Realm, roomId: String, eventIds: List<String>): RealmQuery<TimelineEventEntity> {
return realm.where<TimelineEventEntity>()
internal fun TimelineEventEntity.Companion.where(realm: Realm,
roomId: String,
eventIds: List<String>): RealmQuery<TimelineEventEntity> {
return where(realm)
.equalTo(TimelineEventEntityFields.ROOM_ID, roomId)
.`in`(TimelineEventEntityFields.EVENT_ID, eventIds.toTypedArray())
}
internal fun TimelineEventEntity.Companion.whereRoomId(realm: Realm,
roomId: String): RealmQuery<TimelineEventEntity> {
return realm.where<TimelineEventEntity>()
return where(realm)
.equalTo(TimelineEventEntityFields.ROOM_ID, roomId)
}
internal fun TimelineEventEntity.Companion.findWithSenderMembershipEvent(realm: Realm, senderMembershipEventId: String): List<TimelineEventEntity> {
return realm.where<TimelineEventEntity>()
internal fun TimelineEventEntity.Companion.findWithSenderMembershipEvent(realm: Realm,
senderMembershipEventId: String): List<TimelineEventEntity> {
return where(realm)
.equalTo(TimelineEventEntityFields.SENDER_MEMBERSHIP_EVENT_ID, senderMembershipEventId)
.findAll()
}
@ -110,12 +119,12 @@ internal fun RealmQuery<TimelineEventEntity>.filterTypes(filterTypes: List<Strin
return if (filterTypes.isEmpty()) {
this
} else {
this.`in`(TimelineEventEntityFields.ROOT.TYPE, filterTypes.toTypedArray())
`in`(TimelineEventEntityFields.ROOT.TYPE, filterTypes.toTypedArray())
}
}
internal fun RealmList<TimelineEventEntity>.find(eventId: String): TimelineEventEntity? {
return this.where()
return where()
.equalTo(TimelineEventEntityFields.EVENT_ID, eventId)
.findFirst()
}
@ -132,3 +141,14 @@ internal fun RealmQuery<TimelineEventEntity>.filterSendStates(sendStates: List<S
val sendStatesStr = sendStates.map { it.name }.toTypedArray()
return `in`(TimelineEventEntityFields.ROOT.SEND_STATE_STR, sendStatesStr)
}
/**
* Find all TimelineEventEntity items where sender is in senderIds collection, excluding state events
*/
internal fun TimelineEventEntity.Companion.findAllFrom(realm: Realm,
senderIds: Collection<String>): RealmResults<TimelineEventEntity> {
return where(realm)
.`in`(TimelineEventEntityFields.ROOT.SENDER, senderIds.toTypedArray())
.isNull(TimelineEventEntityFields.ROOT.STATE_KEY)
.findAll()
}

View file

@ -19,7 +19,6 @@ package org.matrix.android.sdk.internal.session.room.timeline
import androidx.lifecycle.LiveData
import com.zhuinden.monarchy.Monarchy
import io.realm.Sort
import io.realm.kotlin.where
import org.matrix.android.sdk.api.session.events.model.isImageMessage
import org.matrix.android.sdk.api.session.events.model.isVideoMessage
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
@ -29,6 +28,7 @@ import org.matrix.android.sdk.internal.database.mapper.TimelineEventMapper
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.query.where
import org.matrix.android.sdk.internal.database.query.whereRoomId
import org.matrix.android.sdk.internal.di.SessionDatabase
import org.matrix.android.sdk.internal.task.TaskExecutor
import javax.inject.Inject
@ -53,8 +53,7 @@ internal class TimelineEventDataSource @Inject constructor(private val realmSess
fun getAttachmentMessages(roomId: String): List<TimelineEvent> {
// TODO pretty bad query.. maybe we should denormalize clear type in base?
return realmSessionProvider.withRealm { realm ->
realm.where<TimelineEventEntity>()
.equalTo(TimelineEventEntityFields.ROOM_ID, roomId)
TimelineEventEntity.whereRoomId(realm, roomId)
.sort(TimelineEventEntityFields.DISPLAY_INDEX, Sort.ASCENDING)
.findAll()
?.mapNotNull { timelineEventMapper.map(it).takeIf { it.root.isImageMessage() || it.root.isVideoMessage() } }

View file

@ -43,10 +43,10 @@ import org.matrix.android.sdk.internal.database.model.PushRulesEntity
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.TimelineEventEntity
import org.matrix.android.sdk.internal.database.model.TimelineEventEntityFields
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.findAllFrom
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
@ -206,10 +206,7 @@ internal class UserAccountDataSyncHandler @Inject constructor(
// See https://spec.matrix.org/latest/client-server-api/#client-behaviour-22 :
// "Once ignored, the client will no longer receive events sent by that user, with the exception of state events"
// So just delete all non-state events from our local storage.
realm.where(TimelineEventEntity::class.java)
.`in`(TimelineEventEntityFields.ROOT.SENDER, userIds.toTypedArray())
.isNull(TimelineEventEntityFields.ROOT.STATE_KEY)
.findAll()
TimelineEventEntity.findAllFrom(realm, userIds)
.also { Timber.d("Deleting ${it.size} TimelineEventEntity from ignored users") }
.forEach {
it.deleteOnCascade(true)