BayernMessenger/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/EventEntityQueries.kt

99 lines
3.5 KiB
Kotlin
Raw Normal View History

2019-01-18 10:12:08 +00:00
/*
* Copyright 2019 New Vector Ltd
2019-01-18 10:12:08 +00:00
*
* 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
2019-01-18 10:12:08 +00:00
*
* 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.
2019-01-18 10:12:08 +00:00
*/
package im.vector.matrix.android.internal.database.query
import im.vector.matrix.android.internal.database.model.EventEntity
2018-11-29 17:35:24 +00:00
import im.vector.matrix.android.internal.database.model.EventEntity.LinkFilterMode.*
2018-10-26 13:12:38 +00:00
import im.vector.matrix.android.internal.database.model.EventEntityFields
import io.realm.Realm
import io.realm.RealmList
import io.realm.RealmQuery
import io.realm.Sort
2018-10-26 13:12:38 +00:00
import io.realm.kotlin.where
internal fun EventEntity.Companion.where(realm: Realm, eventId: String): RealmQuery<EventEntity> {
2019-07-09 13:33:31 +00:00
return realm.where<EventEntity>()
.equalTo(EventEntityFields.EVENT_ID, eventId)
}
2019-05-21 13:42:09 +00:00
internal fun EventEntity.Companion.where(realm: Realm, eventIds: List<String>): RealmQuery<EventEntity> {
2019-07-09 13:33:31 +00:00
return realm.where<EventEntity>()
.`in`(EventEntityFields.EVENT_ID, eventIds.toTypedArray())
2019-05-21 13:42:09 +00:00
}
2018-11-29 17:35:24 +00:00
internal fun EventEntity.Companion.where(realm: Realm,
roomId: String? = null,
type: String? = null,
linkFilterMode: EventEntity.LinkFilterMode = LINKED_ONLY): RealmQuery<EventEntity> {
2018-10-26 13:12:38 +00:00
val query = realm.where<EventEntity>()
2018-11-07 19:36:19 +00:00
if (roomId != null) {
query.equalTo(EventEntityFields.ROOM_ID, roomId)
2018-11-07 19:36:19 +00:00
}
if (type != null) {
2018-10-26 13:12:38 +00:00
query.equalTo(EventEntityFields.TYPE, type)
}
2018-11-29 17:35:24 +00:00
return when (linkFilterMode) {
LINKED_ONLY -> query.equalTo(EventEntityFields.IS_UNLINKED, false)
2018-11-29 17:35:24 +00:00
UNLINKED_ONLY -> query.equalTo(EventEntityFields.IS_UNLINKED, true)
BOTH -> query
2018-11-29 17:35:24 +00:00
}
}
2019-05-29 16:43:33 +00:00
2019-06-19 08:46:59 +00:00
internal fun EventEntity.Companion.types(realm: Realm,
typeList: List<String> = emptyList()): RealmQuery<EventEntity> {
val query = realm.where<EventEntity>()
query.`in`(EventEntityFields.TYPE, typeList.toTypedArray())
return query
}
internal fun RealmQuery<EventEntity>.next(from: Int? = null, strict: Boolean = true): EventEntity? {
if (from != null) {
if (strict) {
this.greaterThan(EventEntityFields.STATE_INDEX, from)
} else {
this.greaterThanOrEqualTo(EventEntityFields.STATE_INDEX, from)
}
}
return this
.sort(EventEntityFields.STATE_INDEX, Sort.ASCENDING)
.findFirst()
}
internal fun RealmQuery<EventEntity>.prev(since: Int? = null, strict: Boolean = false): EventEntity? {
if (since != null) {
if (strict) {
this.lessThan(EventEntityFields.STATE_INDEX, since)
} else {
this.lessThanOrEqualTo(EventEntityFields.STATE_INDEX, since)
}
}
return this
.sort(EventEntityFields.STATE_INDEX, Sort.DESCENDING)
.findFirst()
}
internal fun RealmList<EventEntity>.find(eventId: String): EventEntity? {
2019-07-09 13:33:31 +00:00
return this.where()
.equalTo(EventEntityFields.EVENT_ID, eventId)
.findFirst()
}
internal fun RealmList<EventEntity>.fastContains(eventId: String): Boolean {
return this.find(eventId) != null
}