2019-01-18 10:12:08 +00:00
|
|
|
/*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Copyright 2019 New Vector Ltd
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +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
|
|
|
*
|
2019-01-25 13:04:59 +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
|
|
|
*/
|
|
|
|
|
2018-10-16 13:52:30 +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
|
2018-10-16 13:52:30 +00:00
|
|
|
import io.realm.Realm
|
2018-10-26 10:31:47 +00:00
|
|
|
import io.realm.RealmList
|
2018-10-18 16:38:11 +00:00
|
|
|
import io.realm.RealmQuery
|
2018-10-22 13:17:50 +00:00
|
|
|
import io.realm.Sort
|
2018-10-26 13:12:38 +00:00
|
|
|
import io.realm.kotlin.where
|
2018-10-16 13:52:30 +00:00
|
|
|
|
2018-11-08 10:04:40 +00:00
|
|
|
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)
|
2018-10-25 14:55:41 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2019-01-03 16:10:02 +00:00
|
|
|
query.equalTo(EventEntityFields.ROOM_ID, roomId)
|
2018-11-07 19:36:19 +00:00
|
|
|
}
|
2018-10-22 17:36:29 +00:00
|
|
|
if (type != null) {
|
2018-10-26 13:12:38 +00:00
|
|
|
query.equalTo(EventEntityFields.TYPE, type)
|
2018-10-19 18:34:54 +00:00
|
|
|
}
|
2018-11-29 17:35:24 +00:00
|
|
|
return when (linkFilterMode) {
|
2019-01-14 15:18:39 +00:00
|
|
|
LINKED_ONLY -> query.equalTo(EventEntityFields.IS_UNLINKED, false)
|
2018-11-29 17:35:24 +00:00
|
|
|
UNLINKED_ONLY -> query.equalTo(EventEntityFields.IS_UNLINKED, true)
|
2019-01-14 15:18:39 +00:00
|
|
|
BOTH -> query
|
2018-11-29 17:35:24 +00:00
|
|
|
}
|
2018-10-16 13:52:30 +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
|
|
|
|
}
|
|
|
|
|
2018-11-22 10:20:50 +00:00
|
|
|
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)
|
|
|
|
}
|
2018-10-16 13:52:30 +00:00
|
|
|
}
|
2018-11-14 18:32:42 +00:00
|
|
|
return this
|
2018-11-22 10:20:50 +00:00
|
|
|
.sort(EventEntityFields.STATE_INDEX, Sort.ASCENDING)
|
2018-11-14 18:32:42 +00:00
|
|
|
.findFirst()
|
|
|
|
}
|
|
|
|
|
2019-03-21 19:21:45 +00:00
|
|
|
internal fun RealmQuery<EventEntity>.prev(since: Int? = null, strict: Boolean = false): EventEntity? {
|
2018-11-22 10:20:50 +00:00
|
|
|
if (since != null) {
|
|
|
|
if (strict) {
|
|
|
|
this.lessThan(EventEntityFields.STATE_INDEX, since)
|
|
|
|
} else {
|
|
|
|
this.lessThanOrEqualTo(EventEntityFields.STATE_INDEX, since)
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 16:25:28 +00:00
|
|
|
return this
|
2018-11-13 18:17:59 +00:00
|
|
|
.sort(EventEntityFields.STATE_INDEX, Sort.DESCENDING)
|
2018-10-22 17:36:29 +00:00
|
|
|
.findFirst()
|
|
|
|
}
|
2018-10-23 16:25:28 +00:00
|
|
|
|
2018-11-15 17:32:39 +00:00
|
|
|
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()
|
2018-11-15 17:32:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 16:10:02 +00:00
|
|
|
internal fun RealmList<EventEntity>.fastContains(eventId: String): Boolean {
|
2018-11-15 17:32:39 +00:00
|
|
|
return this.find(eventId) != null
|
2018-10-26 10:31:47 +00:00
|
|
|
}
|