mirror of
https://github.com/element-hq/element-android
synced 2024-10-23 03:06:33 +03:00
Cleanup some Realm queries
This commit is contained in:
parent
849e7c613c
commit
65333e6031
9 changed files with 57 additions and 25 deletions
|
@ -19,16 +19,14 @@ package im.vector.matrix.android.internal.crypto.store.db.query
|
||||||
import im.vector.matrix.android.internal.crypto.store.db.model.CryptoRoomEntity
|
import im.vector.matrix.android.internal.crypto.store.db.model.CryptoRoomEntity
|
||||||
import im.vector.matrix.android.internal.crypto.store.db.model.CryptoRoomEntityFields
|
import im.vector.matrix.android.internal.crypto.store.db.model.CryptoRoomEntityFields
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
|
import io.realm.kotlin.createObject
|
||||||
import io.realm.kotlin.where
|
import io.realm.kotlin.where
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get or create a room
|
* Get or create a room
|
||||||
*/
|
*/
|
||||||
internal fun CryptoRoomEntity.Companion.getOrCreate(realm: Realm, roomId: String): CryptoRoomEntity {
|
internal fun CryptoRoomEntity.Companion.getOrCreate(realm: Realm, roomId: String): CryptoRoomEntity {
|
||||||
return getById(realm, roomId)
|
return getById(realm, roomId) ?: realm.createObject(roomId)
|
||||||
?: let {
|
|
||||||
realm.createObject(CryptoRoomEntity::class.java, roomId)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -20,18 +20,20 @@ import im.vector.matrix.android.internal.crypto.store.db.model.DeviceInfoEntity
|
||||||
import im.vector.matrix.android.internal.crypto.store.db.model.DeviceInfoEntityFields
|
import im.vector.matrix.android.internal.crypto.store.db.model.DeviceInfoEntityFields
|
||||||
import im.vector.matrix.android.internal.crypto.store.db.model.createPrimaryKey
|
import im.vector.matrix.android.internal.crypto.store.db.model.createPrimaryKey
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
|
import io.realm.kotlin.createObject
|
||||||
import io.realm.kotlin.where
|
import io.realm.kotlin.where
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get or create a device info
|
* Get or create a device info
|
||||||
*/
|
*/
|
||||||
internal fun DeviceInfoEntity.Companion.getOrCreate(realm: Realm, userId: String, deviceId: String): DeviceInfoEntity {
|
internal fun DeviceInfoEntity.Companion.getOrCreate(realm: Realm, userId: String, deviceId: String): DeviceInfoEntity {
|
||||||
|
val key = DeviceInfoEntity.createPrimaryKey(userId, deviceId)
|
||||||
|
|
||||||
return realm.where<DeviceInfoEntity>()
|
return realm.where<DeviceInfoEntity>()
|
||||||
.equalTo(DeviceInfoEntityFields.PRIMARY_KEY, DeviceInfoEntity.createPrimaryKey(userId, deviceId))
|
.equalTo(DeviceInfoEntityFields.PRIMARY_KEY, key)
|
||||||
.findFirst()
|
.findFirst()
|
||||||
?: let {
|
?: realm.createObject<DeviceInfoEntity>(key)
|
||||||
realm.createObject(DeviceInfoEntity::class.java, DeviceInfoEntity.createPrimaryKey(userId, deviceId)).apply {
|
.apply {
|
||||||
this.deviceId = deviceId
|
this.deviceId = deviceId
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ package im.vector.matrix.android.internal.crypto.store.db.query
|
||||||
import im.vector.matrix.android.internal.crypto.store.db.model.UserEntity
|
import im.vector.matrix.android.internal.crypto.store.db.model.UserEntity
|
||||||
import im.vector.matrix.android.internal.crypto.store.db.model.UserEntityFields
|
import im.vector.matrix.android.internal.crypto.store.db.model.UserEntityFields
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
|
import io.realm.kotlin.createObject
|
||||||
import io.realm.kotlin.where
|
import io.realm.kotlin.where
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,9 +29,7 @@ internal fun UserEntity.Companion.getOrCreate(realm: Realm, userId: String): Use
|
||||||
return realm.where<UserEntity>()
|
return realm.where<UserEntity>()
|
||||||
.equalTo(UserEntityFields.USER_ID, userId)
|
.equalTo(UserEntityFields.USER_ID, userId)
|
||||||
.findFirst()
|
.findFirst()
|
||||||
?: let {
|
?: realm.createObject(userId)
|
||||||
realm.createObject(UserEntity::class.java, userId)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2019 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.matrix.android.internal.database.query
|
||||||
|
|
||||||
|
import im.vector.matrix.android.internal.database.model.BreadcrumbsEntity
|
||||||
|
import io.realm.Realm
|
||||||
|
import io.realm.kotlin.createObject
|
||||||
|
import io.realm.kotlin.where
|
||||||
|
|
||||||
|
internal fun BreadcrumbsEntity.Companion.get(realm: Realm): BreadcrumbsEntity? {
|
||||||
|
return realm.where<BreadcrumbsEntity>().findFirst()
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun BreadcrumbsEntity.Companion.getOrCreate(realm: Realm): BreadcrumbsEntity {
|
||||||
|
return get(realm) ?: realm.createObject()
|
||||||
|
}
|
|
@ -20,6 +20,7 @@ import im.vector.matrix.android.internal.database.model.ReadMarkerEntity
|
||||||
import im.vector.matrix.android.internal.database.model.ReadMarkerEntityFields
|
import im.vector.matrix.android.internal.database.model.ReadMarkerEntityFields
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import io.realm.RealmQuery
|
import io.realm.RealmQuery
|
||||||
|
import io.realm.kotlin.createObject
|
||||||
import io.realm.kotlin.where
|
import io.realm.kotlin.where
|
||||||
|
|
||||||
internal fun ReadMarkerEntity.Companion.where(realm: Realm, roomId: String): RealmQuery<ReadMarkerEntity> {
|
internal fun ReadMarkerEntity.Companion.where(realm: Realm, roomId: String): RealmQuery<ReadMarkerEntity> {
|
||||||
|
@ -28,6 +29,5 @@ internal fun ReadMarkerEntity.Companion.where(realm: Realm, roomId: String): Rea
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun ReadMarkerEntity.Companion.getOrCreate(realm: Realm, roomId: String): ReadMarkerEntity {
|
internal fun ReadMarkerEntity.Companion.getOrCreate(realm: Realm, roomId: String): ReadMarkerEntity {
|
||||||
return where(realm, roomId).findFirst()
|
return where(realm, roomId).findFirst() ?: realm.createObject(roomId)
|
||||||
?: realm.createObject(ReadMarkerEntity::class.java, roomId)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ import im.vector.matrix.android.internal.database.model.ReadReceiptEntity
|
||||||
import im.vector.matrix.android.internal.database.model.ReadReceiptEntityFields
|
import im.vector.matrix.android.internal.database.model.ReadReceiptEntityFields
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import io.realm.RealmQuery
|
import io.realm.RealmQuery
|
||||||
|
import io.realm.kotlin.createObject
|
||||||
import io.realm.kotlin.where
|
import io.realm.kotlin.where
|
||||||
|
|
||||||
internal fun ReadReceiptEntity.Companion.where(realm: Realm, roomId: String, userId: String): RealmQuery<ReadReceiptEntity> {
|
internal fun ReadReceiptEntity.Companion.where(realm: Realm, roomId: String, userId: String): RealmQuery<ReadReceiptEntity> {
|
||||||
|
@ -44,10 +45,11 @@ internal fun ReadReceiptEntity.Companion.createUnmanaged(roomId: String, eventId
|
||||||
|
|
||||||
internal fun ReadReceiptEntity.Companion.getOrCreate(realm: Realm, roomId: String, userId: String): ReadReceiptEntity {
|
internal fun ReadReceiptEntity.Companion.getOrCreate(realm: Realm, roomId: String, userId: String): ReadReceiptEntity {
|
||||||
return ReadReceiptEntity.where(realm, roomId, userId).findFirst()
|
return ReadReceiptEntity.where(realm, roomId, userId).findFirst()
|
||||||
?: realm.createObject(ReadReceiptEntity::class.java, buildPrimaryKey(roomId, userId)).apply {
|
?: realm.createObject<ReadReceiptEntity>(buildPrimaryKey(roomId, userId))
|
||||||
this.roomId = roomId
|
.apply {
|
||||||
this.userId = userId
|
this.roomId = roomId
|
||||||
}
|
this.userId = userId
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildPrimaryKey(roomId: String, userId: String) = "${roomId}_$userId"
|
private fun buildPrimaryKey(roomId: String, userId: String) = "${roomId}_$userId"
|
||||||
|
|
|
@ -21,6 +21,7 @@ import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import io.realm.RealmQuery
|
import io.realm.RealmQuery
|
||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
|
import io.realm.kotlin.createObject
|
||||||
import io.realm.kotlin.where
|
import io.realm.kotlin.where
|
||||||
|
|
||||||
internal fun RoomSummaryEntity.Companion.where(realm: Realm, roomId: String? = null): RealmQuery<RoomSummaryEntity> {
|
internal fun RoomSummaryEntity.Companion.where(realm: Realm, roomId: String? = null): RealmQuery<RoomSummaryEntity> {
|
||||||
|
@ -32,8 +33,7 @@ internal fun RoomSummaryEntity.Companion.where(realm: Realm, roomId: String? = n
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun RoomSummaryEntity.Companion.getOrCreate(realm: Realm, roomId: String): RoomSummaryEntity {
|
internal fun RoomSummaryEntity.Companion.getOrCreate(realm: Realm, roomId: String): RoomSummaryEntity {
|
||||||
return where(realm, roomId).findFirst()
|
return where(realm, roomId).findFirst() ?: realm.createObject(roomId)
|
||||||
?: realm.createObject(RoomSummaryEntity::class.java, roomId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun RoomSummaryEntity.Companion.getDirectRooms(realm: Realm): RealmResults<RoomSummaryEntity> {
|
internal fun RoomSummaryEntity.Companion.getDirectRooms(realm: Realm): RealmResults<RoomSummaryEntity> {
|
||||||
|
|
|
@ -19,6 +19,7 @@ import com.zhuinden.monarchy.Monarchy
|
||||||
import im.vector.matrix.android.internal.database.model.BreadcrumbsEntity
|
import im.vector.matrix.android.internal.database.model.BreadcrumbsEntity
|
||||||
import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
|
import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
|
||||||
import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields
|
import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields
|
||||||
|
import im.vector.matrix.android.internal.database.query.getOrCreate
|
||||||
import im.vector.matrix.android.internal.database.query.where
|
import im.vector.matrix.android.internal.database.query.where
|
||||||
import im.vector.matrix.android.internal.task.Task
|
import im.vector.matrix.android.internal.task.Task
|
||||||
import im.vector.matrix.android.internal.util.awaitTransaction
|
import im.vector.matrix.android.internal.util.awaitTransaction
|
||||||
|
@ -41,8 +42,7 @@ internal class DefaultSaveBreadcrumbsTask @Inject constructor(
|
||||||
override suspend fun execute(params: SaveBreadcrumbsTask.Params) {
|
override suspend fun execute(params: SaveBreadcrumbsTask.Params) {
|
||||||
monarchy.awaitTransaction { realm ->
|
monarchy.awaitTransaction { realm ->
|
||||||
// Get or create a breadcrumbs entity
|
// Get or create a breadcrumbs entity
|
||||||
val entity = realm.where(BreadcrumbsEntity::class.java).findFirst()
|
val entity = BreadcrumbsEntity.getOrCreate(realm)
|
||||||
?: realm.createObject(BreadcrumbsEntity::class.java)
|
|
||||||
|
|
||||||
// And save the new received list
|
// And save the new received list
|
||||||
entity.recentRoomIds = RealmList<String>().apply { addAll(params.recentRoomIds) }
|
entity.recentRoomIds = RealmList<String>().apply { addAll(params.recentRoomIds) }
|
||||||
|
|
|
@ -18,6 +18,7 @@ package im.vector.matrix.android.internal.session.user.accountdata
|
||||||
|
|
||||||
import com.zhuinden.monarchy.Monarchy
|
import com.zhuinden.monarchy.Monarchy
|
||||||
import im.vector.matrix.android.internal.database.model.BreadcrumbsEntity
|
import im.vector.matrix.android.internal.database.model.BreadcrumbsEntity
|
||||||
|
import im.vector.matrix.android.internal.database.query.get
|
||||||
import im.vector.matrix.android.internal.session.sync.model.accountdata.BreadcrumbsContent
|
import im.vector.matrix.android.internal.session.sync.model.accountdata.BreadcrumbsContent
|
||||||
import im.vector.matrix.android.internal.task.Task
|
import im.vector.matrix.android.internal.task.Task
|
||||||
import im.vector.matrix.android.internal.util.fetchCopied
|
import im.vector.matrix.android.internal.util.fetchCopied
|
||||||
|
@ -43,8 +44,7 @@ internal class DefaultUpdateBreadcrumbsTask @Inject constructor(
|
||||||
// Get the current breadcrumbs in DB
|
// Get the current breadcrumbs in DB
|
||||||
val bc = monarchy.fetchCopied { realm ->
|
val bc = monarchy.fetchCopied { realm ->
|
||||||
// Get the breadcrumbs entity, if any
|
// Get the breadcrumbs entity, if any
|
||||||
realm.where(BreadcrumbsEntity::class.java).findFirst()
|
BreadcrumbsEntity.get(realm)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Modify the list to add the roomId first
|
// Modify the list to add the roomId first
|
||||||
|
@ -55,6 +55,7 @@ internal class DefaultUpdateBreadcrumbsTask @Inject constructor(
|
||||||
bc.recentRoomIds.add(0, params.roomId)
|
bc.recentRoomIds.add(0, params.roomId)
|
||||||
bc.recentRoomIds.take(MAX_BREADCRUMBS_ROOMS_NUMBER)
|
bc.recentRoomIds.take(MAX_BREADCRUMBS_ROOMS_NUMBER)
|
||||||
} else {
|
} else {
|
||||||
|
// FIXME It can remove the previous breadcrumbs, if not synced yet
|
||||||
listOf(params.roomId)
|
listOf(params.roomId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue