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-11-29 10:52:36 +00:00
|
|
|
package im.vector.matrix.android.internal.session.room.timeline
|
|
|
|
|
|
|
|
import arrow.core.Try
|
|
|
|
import com.zhuinden.monarchy.Monarchy
|
2019-03-15 18:27:56 +00:00
|
|
|
import im.vector.matrix.android.internal.database.helper.addAll
|
|
|
|
import im.vector.matrix.android.internal.database.helper.addOrUpdate
|
|
|
|
import im.vector.matrix.android.internal.database.helper.addStateEvents
|
|
|
|
import im.vector.matrix.android.internal.database.helper.deleteOnCascade
|
|
|
|
import im.vector.matrix.android.internal.database.helper.isUnlinked
|
|
|
|
import im.vector.matrix.android.internal.database.helper.merge
|
2018-11-29 10:52:36 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.ChunkEntity
|
|
|
|
import im.vector.matrix.android.internal.database.model.RoomEntity
|
2018-11-29 17:35:24 +00:00
|
|
|
import im.vector.matrix.android.internal.database.query.create
|
2018-11-29 10:52:36 +00:00
|
|
|
import im.vector.matrix.android.internal.database.query.find
|
|
|
|
import im.vector.matrix.android.internal.database.query.findAllIncludingEvents
|
|
|
|
import im.vector.matrix.android.internal.database.query.where
|
|
|
|
import im.vector.matrix.android.internal.util.tryTransactionSync
|
2019-03-15 18:27:56 +00:00
|
|
|
import io.realm.kotlin.createObject
|
2018-11-29 10:52:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
internal class TokenChunkEventPersistor(private val monarchy: Monarchy) {
|
|
|
|
|
|
|
|
fun insertInDb(receivedChunk: TokenChunkEvent,
|
|
|
|
roomId: String,
|
2019-01-10 10:37:14 +00:00
|
|
|
direction: PaginationDirection): Try<Boolean> {
|
2018-11-29 10:52:36 +00:00
|
|
|
|
2019-01-10 10:37:14 +00:00
|
|
|
if (receivedChunk.events.isEmpty() && receivedChunk.stateEvents.isEmpty()) {
|
|
|
|
return Try.just(false)
|
|
|
|
}
|
2018-11-29 10:52:36 +00:00
|
|
|
return monarchy
|
|
|
|
.tryTransactionSync { realm ->
|
|
|
|
val roomEntity = RoomEntity.where(realm, roomId).findFirst()
|
2019-03-15 18:27:56 +00:00
|
|
|
?: realm.createObject(roomId)
|
2018-11-29 10:52:36 +00:00
|
|
|
|
2018-11-29 17:35:24 +00:00
|
|
|
val nextToken: String?
|
|
|
|
val prevToken: String?
|
|
|
|
if (direction == PaginationDirection.FORWARDS) {
|
|
|
|
nextToken = receivedChunk.end
|
|
|
|
prevToken = receivedChunk.start
|
|
|
|
} else {
|
|
|
|
nextToken = receivedChunk.start
|
|
|
|
prevToken = receivedChunk.end
|
2018-11-29 10:52:36 +00:00
|
|
|
}
|
2018-11-29 17:35:24 +00:00
|
|
|
val prevChunk = ChunkEntity.find(realm, roomId, nextToken = prevToken)
|
|
|
|
val nextChunk = ChunkEntity.find(realm, roomId, prevToken = nextToken)
|
2018-11-29 10:52:36 +00:00
|
|
|
|
2019-03-13 21:30:05 +00:00
|
|
|
// The current chunk is the one we will keep all along the merge processChanges.
|
2018-11-29 17:35:24 +00:00
|
|
|
// We try to look for a chunk next to the token,
|
|
|
|
// otherwise we create a whole new one
|
2018-11-29 10:52:36 +00:00
|
|
|
|
2018-11-29 17:35:24 +00:00
|
|
|
var currentChunk = if (direction == PaginationDirection.FORWARDS) {
|
|
|
|
prevChunk?.apply { this.nextToken = nextToken }
|
2019-03-15 18:27:56 +00:00
|
|
|
?: ChunkEntity.create(realm, prevToken, nextToken)
|
2018-11-29 17:35:24 +00:00
|
|
|
} else {
|
|
|
|
nextChunk?.apply { this.prevToken = prevToken }
|
2019-03-15 18:27:56 +00:00
|
|
|
?: ChunkEntity.create(realm, prevToken, nextToken)
|
2018-11-29 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 16:10:02 +00:00
|
|
|
currentChunk.addAll(roomId, receivedChunk.events, direction, isUnlinked = currentChunk.isUnlinked())
|
2018-11-29 17:35:24 +00:00
|
|
|
|
|
|
|
// Then we merge chunks if needed
|
|
|
|
if (currentChunk != prevChunk && prevChunk != null) {
|
|
|
|
currentChunk = handleMerge(roomEntity, direction, currentChunk, prevChunk)
|
|
|
|
} else if (currentChunk != nextChunk && nextChunk != null) {
|
|
|
|
currentChunk = handleMerge(roomEntity, direction, currentChunk, nextChunk)
|
|
|
|
} else {
|
|
|
|
val newEventIds = receivedChunk.events.mapNotNull { it.eventId }
|
|
|
|
ChunkEntity
|
|
|
|
.findAllIncludingEvents(realm, newEventIds)
|
|
|
|
.filter { it != currentChunk }
|
|
|
|
.forEach { overlapped ->
|
|
|
|
currentChunk = handleMerge(roomEntity, direction, currentChunk, overlapped)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
roomEntity.addOrUpdate(currentChunk)
|
2018-12-11 14:35:46 +00:00
|
|
|
roomEntity.addStateEvents(receivedChunk.stateEvents, isUnlinked = currentChunk.isUnlinked())
|
2018-11-29 10:52:36 +00:00
|
|
|
}
|
2019-01-10 10:37:14 +00:00
|
|
|
.map { true }
|
2018-11-29 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 17:35:24 +00:00
|
|
|
private fun handleMerge(roomEntity: RoomEntity,
|
|
|
|
direction: PaginationDirection,
|
|
|
|
currentChunk: ChunkEntity,
|
|
|
|
otherChunk: ChunkEntity): ChunkEntity {
|
|
|
|
|
|
|
|
// We always merge the bottom chunk into top chunk, so we are always merging backwards
|
|
|
|
return if (direction == PaginationDirection.BACKWARDS) {
|
2019-01-07 18:38:49 +00:00
|
|
|
currentChunk.merge(roomEntity.roomId, otherChunk, PaginationDirection.BACKWARDS)
|
2018-11-29 17:35:24 +00:00
|
|
|
roomEntity.deleteOnCascade(otherChunk)
|
|
|
|
currentChunk
|
|
|
|
} else {
|
2019-01-07 18:38:49 +00:00
|
|
|
otherChunk.merge(roomEntity.roomId, currentChunk, PaginationDirection.BACKWARDS)
|
2018-11-29 17:35:24 +00:00
|
|
|
roomEntity.deleteOnCascade(currentChunk)
|
|
|
|
otherChunk
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 10:52:36 +00:00
|
|
|
|
|
|
|
}
|