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.ChunkEntity
|
2018-10-26 13:12:38 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.ChunkEntityFields
|
|
|
|
import im.vector.matrix.android.internal.database.model.RoomEntityFields
|
2018-10-16 13:52:30 +00:00
|
|
|
import io.realm.Realm
|
2018-10-18 16:38:11 +00:00
|
|
|
import io.realm.RealmQuery
|
|
|
|
import io.realm.RealmResults
|
2018-11-29 17:35:24 +00:00
|
|
|
import io.realm.kotlin.createObject
|
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 ChunkEntity.Companion.where(realm: Realm, roomId: String): RealmQuery<ChunkEntity> {
|
2018-10-26 13:12:38 +00:00
|
|
|
return realm.where<ChunkEntity>()
|
|
|
|
.equalTo("${ChunkEntityFields.ROOM}.${RoomEntityFields.ROOM_ID}", roomId)
|
2018-10-18 16:38:11 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 17:32:39 +00:00
|
|
|
internal fun ChunkEntity.Companion.find(realm: Realm, roomId: String, prevToken: String? = null, nextToken: String? = null): ChunkEntity? {
|
|
|
|
val query = where(realm, roomId)
|
|
|
|
if (prevToken != null) {
|
|
|
|
query.equalTo(ChunkEntityFields.PREV_TOKEN, prevToken)
|
2018-10-18 16:38:11 +00:00
|
|
|
}
|
2018-11-15 17:32:39 +00:00
|
|
|
if (nextToken != null) {
|
|
|
|
query.equalTo(ChunkEntityFields.NEXT_TOKEN, nextToken)
|
2018-10-18 16:38:11 +00:00
|
|
|
}
|
2018-11-15 17:32:39 +00:00
|
|
|
return query.findFirst()
|
2018-10-18 16:38:11 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 10:04:40 +00:00
|
|
|
internal fun ChunkEntity.Companion.findLastLiveChunkFromRoom(realm: Realm, roomId: String): ChunkEntity? {
|
2018-10-18 16:38:11 +00:00
|
|
|
return where(realm, roomId)
|
2019-03-19 18:45:32 +00:00
|
|
|
.equalTo(ChunkEntityFields.IS_LAST_FORWARD, true)
|
2018-11-13 09:50:53 +00:00
|
|
|
.findFirst()
|
2018-10-16 13:52:30 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 10:04:40 +00:00
|
|
|
internal fun ChunkEntity.Companion.findAllIncludingEvents(realm: Realm, eventIds: List<String>): RealmResults<ChunkEntity> {
|
2018-10-26 13:12:38 +00:00
|
|
|
return realm.where<ChunkEntity>()
|
2019-07-05 14:07:12 +00:00
|
|
|
.`in`(ChunkEntityFields.TIMELINE_EVENTS.EVENT_ID, eventIds.toTypedArray())
|
2018-10-18 16:38:11 +00:00
|
|
|
.findAll()
|
2018-11-29 17:35:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 10:37:14 +00:00
|
|
|
internal fun ChunkEntity.Companion.findIncludingEvent(realm: Realm, eventId: String): ChunkEntity? {
|
|
|
|
return findAllIncludingEvents(realm, listOf(eventId)).firstOrNull()
|
|
|
|
}
|
|
|
|
|
2018-11-29 17:35:24 +00:00
|
|
|
internal fun ChunkEntity.Companion.create(realm: Realm, prevToken: String?, nextToken: String?): ChunkEntity {
|
|
|
|
return realm.createObject<ChunkEntity>().apply {
|
|
|
|
this.prevToken = prevToken
|
|
|
|
this.nextToken = nextToken
|
|
|
|
}
|
2018-10-16 13:52:30 +00:00
|
|
|
}
|